MetadataFunctionTransformer#
- class chemotools.adaptation.MetadataFunctionTransformer(func: Callable[[...], ndarray], metadata: Sequence[str] = (), validate: bool = True)[source]
Bases:
DocLinkMixin,TransformerMixin,BaseEstimatorApply 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
Pipelinevia the metadata-routing API.- Parameters:
func (Callable[..., np.ndarray]) – Function applied during
transform. It must acceptXas its first positional argument and each requested metadata value as a keyword argument. The function is responsible for returning a numeric, 2-Dnp.ndarraywith the same number of samples and features asX. Usechemotools.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 totransformbut not listed here are ignored. Every required keyword argument offuncmust be listed, and the corresponding value must be supplied when callingtransform. The names"X"and"y"are reserved by the estimator API and cannot be requested as metadata.validate (bool, default=True) – If
True, validateXas a numeric, 2-D array duringfitandtransform, and require the number of features to remain consistent. This does not validate the output offunc. IfFalse, passXtofuncunchanged. In both cases,fitmust be called beforetransform.
- Variables:
n_features_in (int) – Number of features seen during
fit. Only set whenvalidate=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 withsklearn.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 duringtransform. Consequently, errors that depend on metadata values or callable execution are raised duringtransform. Usechemotools.adaptation.validation.check_metadata_function()to validate a callable eagerly on representative inputs.See also
chemotools.adaptation.validation.check_metadata_functionValidate a custom metadata function on representative inputs.
chemotools.adaptation.functionsPredefined 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
funcduring fitting.
- Returns:
self
- Return type:
MetadataFunctionTransformer
- transform(X: Any, **metadata: Any) ndarray[source]
Apply
functoX, forwarding any requested metadata keys.- Parameters:
X (array-like of shape (n_samples, n_features)) – Data to transform.
**metadata (Any) – Metadata values passed to
funcas keyword arguments. Only keys listed inself.metadataare forwarded.
- Returns:
X_transformed – Result returned by
func(X, **kwargs). Output type and shape are the responsibility offuncand 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_transformto ensure that**metadatais forwarded to bothfitandtransform.- 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
fitandtransform.
- 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.metadataas a requested parameter for bothfitandtransform, enabling sklearn’s metadata routing to propagate them through aPipeline.- Returns:
request – The populated routing object.
- Return type:
MetadataRequest