MetadataFunctionTransformer#

class chemotools.adaptation.MetadataFunctionTransformer(func: Callable[[...], ndarray], metadata: Sequence[str] = (), validate: bool = True)[source]

Bases: DocLinkMixin, TransformerMixin, BaseEstimator

Apply a callable that consumes feature data and routed metadata.

This is useful when a preprocessing function requires per-sample or per-batch auxiliary information (e.g. reference spectra, wavelength arrays) that must be threaded through a scikit-learn Pipeline via the metadata-routing API.

Parameters:
  • func (Callable[..., np.ndarray]) – Function applied during transform. It must accept X as its first positional argument and each requested metadata value as a keyword argument. The function is responsible for returning a numeric, 2-D np.ndarray with the same number of samples and features as X. Use chemotools.adaptation.validation.check_metadata_function() to verify this contract on representative data.

  • metadata (Sequence[str], default=()) – Names of the keyword arguments requested through scikit-learn metadata routing and forwarded to func. Keys passed to transform but not listed here are ignored. Every required keyword argument of func must be listed, and the corresponding value must be supplied when calling transform. The names "X" and "y" are reserved by the estimator API and cannot be requested as metadata.

  • validate (bool, default=True) – If True, validate X as a numeric, 2-D array during fit and transform, and require the number of features to remain consistent. This does not validate the output of func. If False, pass X to func unchanged. In both cases, fit must be called before transform.

Variables:

n_features_in (int) – Number of features seen during fit. Only set when validate=True.

Examples

>>> import numpy as np
>>> from chemotools.adaptation import MetadataFunctionTransformer
>>>
>>> def subtract_reference(X, reference):
...     return X - reference
>>>
>>> rng = np.random.default_rng(0)
>>> X = rng.normal(size=(10, 50))
>>> reference = rng.normal(size=(1, 50))
>>>
>>> mft = MetadataFunctionTransformer(
...     func=subtract_reference, metadata=("reference",)
... )
>>> X_transf = mft.fit_transform(X, reference=reference)

Notes

To route metadata through a scikit-learn Pipeline, enable metadata routing globally with sklearn.set_config(enable_metadata_routing=True). This is not required when calling this estimator directly.

The callable signature is validated during fit, but the callable is executed only during transform. Consequently, errors that depend on metadata values or callable execution are raised during transform. Use chemotools.adaptation.validation.check_metadata_function() to validate a callable eagerly on representative inputs.

See also

chemotools.adaptation.validation.check_metadata_function

Validate a custom metadata function on representative inputs.

chemotools.adaptation.functions

Predefined metadata-aware functions.

fit(X, y=None, **metadata: Any)[source]

Fit the transformer by recording the number of input features.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (array-like, default=None) – Ignored. Present for API compatibility.

  • **metadata (Any) – Metadata accepted for routing compatibility. It is not forwarded to func during fitting.

Returns:

self

Return type:

MetadataFunctionTransformer

transform(X: Any, **metadata: Any) → ndarray[source]

Apply func to X, forwarding any requested metadata keys.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Data to transform.

  • **metadata (Any) – Metadata values passed to func as keyword arguments. Only keys listed in self.metadata are forwarded.

Returns:

X_transformed – Result returned by func(X, **kwargs). Output type and shape are the responsibility of func and are not validated here.

Return type:

np.ndarray of shape (n_samples, n_features)

fit_transform(X, y=None, **metadata: Any) → ndarray[source]

Fit and transform in a single step.

Overrides the default TransformerMixin.fit_transform to ensure that **metadata is forwarded to both fit and transform.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data.

  • y (array-like, default=None) – Ignored. Present for API compatibility.

  • **metadata (Any) – Keyword arguments forwarded to both fit and transform.

Returns:

X_transformed – Result returned by func.

Return type:

np.ndarray of shape (n_samples, n_features)

get_metadata_routing()[source]

Return the metadata routing configuration for this transformer.

Registers each name in self.metadata as a requested parameter for both fit and transform, enabling sklearn’s metadata routing to propagate them through a Pipeline.

Returns:

request – The populated routing object.

Return type:

MetadataRequest