nirs4all.operators.methods.n4m_ops module

Thin nirs4all operators dispatching to the nirs4all-methods engine.

The numerics live entirely in the portable C++ core (libn4m) and are reached through the n4m ctypes binding. These wrappers own no numerical logic: they only translate sklearn fit / transform / predict calls into the binding’s own sklearn-contract objects and surface JSON-serializable parameters via get_params so both nirs4all engines can round-trip them.

class nirs4all.operators.methods.n4m_ops.MethodsPLS(n_components: int = 2, cv: int = 5, scale_x: bool = True)[source]

Bases: RegressorMixin, BaseEstimator

PLS regression backed by the nirs4all-methods engine.

Single-target moment-based PLS computed by the portable libn4m core. Numerically matches scikit-learn’s sklearn.cross_decomposition.PLSRegression to floating-point precision for a fixed component count.

Parameters:
  • n_components (int, default=2) – Number of latent variables to fit.

  • cv (int, default=5) – Cross-validation folds used by the native component-selection sweep. Use cv >= 2; cv=1 is rejected by the engine.

  • scale_x (bool, default=True) – Scale features to unit variance (matches sklearn scale=True).

fit(X: ndarray, y: ndarray) → MethodsPLS[source]

Fit the native PLS model on (X, y).

The native engine is single-target (PLS1); y must be 1D or a single-column 2D array.

get_params(deep: bool = True) → dict[str, Any][source]

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

predict(X: ndarray) → ndarray[source]

Predict the target from fitted native PLS coefficients.

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → MethodsPLS

Configure whether metadata should be requested to be passed to the score method.

Note that this method is only relevant when this estimator is used as a sub-estimator within a meta-estimator and metadata routing is enabled with enable_metadata_routing=True (see sklearn.set_config()). Please check the User Guide on how the routing mechanism works.

The options for each parameter are:

  • True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.

  • False: metadata is not requested and the meta-estimator will not pass it to score.

  • None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.

  • str: metadata should be passed to the meta-estimator with this given alias instead of the original name.

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object

class nirs4all.operators.methods.n4m_ops.MethodsSNV(with_mean: bool = True, with_std: bool = True, ddof: int = 0)[source]

Bases: TransformerMixin, BaseEstimator

Standard Normal Variate backed by the nirs4all-methods engine.

Row-wise scatter correction computed by the portable libn4m core. Drop-in replacement for the pure-Python nirs4all.operators.transforms.StandardNormalVariate, numerically matching it (and scikit-learn) to floating-point precision.

Parameters:
  • with_mean (bool, default=True) – Center each sample before scaling.

  • with_std (bool, default=True) – Scale each sample to unit variance.

  • ddof (int, default=0) – Delta degrees of freedom for the per-sample standard deviation.

fit(X: ndarray, y: Any = None) → MethodsSNV[source]

No-op fit; SNV is stateless (row-wise).

get_params(deep: bool = True) → dict[str, Any][source]

Get parameters for this estimator.

Parameters:

deep (bool, default=True) – If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

transform(X: ndarray) → ndarray[source]

Return the SNV-normalised spectra from the native engine.

nirs4all.operators.methods.n4m_ops.methods_binding_status() → dict[str, Any][source]

Return the installed n4m binding status consumed by these operators.

The result is intentionally JSON-serializable so tests, CLIs, and runtime diagnostics can report a useful blocker without importing or reimplementing any nirs4all-methods numerical logic.