chemotools.adaptation.functions.divide_by_reference#

chemotools.adaptation.functions.divide_by_reference(X: ndarray, reference: float | ndarray) → ndarray[source]#

Divide every row of X by a reference spectrum.

Useful for single-beam transmission corrections or ratiometric normalisation where each sample is divided by a simultaneously measured reference channel.

Parameters:
  • X (np.ndarray of shape (n_samples, n_features)) – Input spectra.

  • reference (float or np.ndarray) –

    Reference to divide by. Accepted shapes:

    • scalar float — divides every element.

    • (1, n_features) — shared reference broadcast across all samples.

    • (n_samples, 1) — per-sample scalar divisor.

    • (n_samples, n_features) — per-sample full spectrum.

    1-D inputs are rejected; use .reshape(1, -1) for a shared spectrum or .reshape(-1, 1) for a per-sample scalar. Zero values are rejected because division by zero produces non-finite output.

Returns:

X_corrected – X / reference.

Return type:

np.ndarray of shape (n_samples, n_features)

Raises:

ValueError – If reference contains zero values.

Examples

>>> import numpy as np
>>> from chemotools.adaptation.functions import divide_by_reference
>>> X = np.array([[2.0, 4.0, 6.0], [8.0, 10.0, 12.0]])
>>> ref = np.array([[2.0, 2.0, 2.0]])
>>> divide_by_reference(X, ref)
array([[1., 2., 3.],
       [4., 5., 6.]])