chemotools.adaptation.validation.check_metadata_function#

chemotools.adaptation.validation.check_metadata_function(func: Callable[[...], Any], X, *, metadata: Mapping[str, Any] | None = None, preserve_features: bool = True) → ndarray[source]#

Validate a metadata function on representative data.

The function is called once as func(X_checked, **metadata). Its output is validated as a finite, numeric, 2-D array that preserves the number of input samples and, by default, the number of input features.

Parameters:
  • func (callable) – Function invoked as func(X_checked, **metadata).

  • X (array-like of shape (n_samples, n_features)) – Representative input data.

  • metadata (mapping of str to object, default=None) – Representative keyword arguments passed to func. Use None when the function requires no metadata. The names "X" and "y" are reserved by the estimator API and cannot be used as metadata keys.

  • preserve_features (bool, default=True) – If True, require the output to have the same number of features as X. The number of samples is always required to match.

Returns:

result – Validated output produced by func.

Return type:

np.ndarray

Raises:
  • TypeError – If func is not callable or its signature cannot be inspected.

  • ValueError – If X is invalid, func cannot be called with the supplied arguments, its output is not a finite numeric 2-D array, or its output does not preserve the required dimensions.

Notes

This check executes user-provided code. Exceptions raised by func are propagated unchanged, and functions with side effects will perform those side effects once. The validated input and output may be converted to NumPy arrays by sklearn.utils.validation.check_array().

Examples

>>> import numpy as np
>>> from chemotools.adaptation.functions import subtract_reference
>>> from chemotools.adaptation.validation import check_metadata_function
>>> X = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> reference = np.array([[0.5, 0.5]])
>>> check_metadata_function(
...     subtract_reference, X, metadata={"reference": reference}
... )
array([[0.5, 1.5],
       [2.5, 3.5]])

See also

chemotools.adaptation.MetadataFunctionTransformer

Wrap a function for scikit-learn metadata routing.