Skip to content

Spectra API

Use Spectrum for a single spectral signature and SpectrumCollection for several signatures sampled on the same grid. A collection stores one row per component and one column per wavelength sample or detector channel. It keeps the shared axis and optional detector bands alongside the matrix, with a name and source information for each row.

Choose a container

Task Use
Keep or exchange one signature Spectrum with a one-dimensional values array
Group aligned signatures with their names and provenance SpectrumCollection.from_spectra()
Attach a shared grid to an existing component matrix SpectrumCollection(values, wavelengths_nm=..., detection_bands_nm=...)
Recover one signature from a collection collection.spectrum(index)

When grouping spectra, sample counts and wavelength coordinates must match exactly in the same order, or all spectra must omit wavelength coordinates. The collection does not resample, sort, or normalize its inputs. If wavelengths are unavailable, leave wavelengths_nm unset and verify channel order from the acquisition. Matching wavelength centers alone does not establish that two detector grids are compatible. Check the detector bands when available.

Save and reuse references

Both containers have save_json() and load_json() methods. Use the class that matches the file format: lumic-spectrum for a single signature, lumic-spectrum-collection for a shared-axis matrix. The latter stores the wavelength axis and detector bands once. Provenance must be JSON compatible when saved.

from lumic import Spectrum, SpectrumCollection

wavelengths = [500, 510, 520]
reference_a = Spectrum([0.2, 0.5, 0.3], wavelengths, name="A")
reference_b = Spectrum([0.5, 0.4, 0.1], wavelengths, name="B")
references = SpectrumCollection.from_spectra([reference_a, reference_b])

references.save_json("references.json")
restored = SpectrumCollection.load_json("references.json")
first_reference = restored.spectrum(0)
component_matrix = restored.values  # (2, 3), components × samples

Pass component_matrix to the solver when its columns match the detector channels of the observations. See Load and prepare spectra for a detector sampling example and Unmixing and spectral selection for fitting.

Single spectrum

Spectrum dataclass

Spectrum(values, wavelengths_nm=None, name=None, provenance=dict())

Keep a spectrum's values, wavelength labels and source information together.

values is a nonempty, finite, one-dimensional array of real intensities. Optional wavelengths_nm contains one finite, positive coordinate per value, in the same order. Use name for the label and provenance for descriptive information about acquisition or processing.

Arrays are copied at construction. Values retain their dtype, order and scale. Wavelength labels alone do not establish compatibility with an image's detector channels. Pass ordinary NumPy arrays to the solvers.

values instance-attribute

values

wavelengths_nm class-attribute instance-attribute

wavelengths_nm = None

name class-attribute instance-attribute

name = None

provenance class-attribute instance-attribute

provenance = field(default_factory=dict)

save_json

save_json(path)

Save a version 1 Lumic spectrum to a UTF-8 JSON file.

Preserve sample order, scale, and numeric dtypes up to 64 bits. Provenance must contain JSON objects with string keys, lists, strings, booleans, finite Python numbers or None; unsupported objects raise TypeError. Serialization is validated before opening the destination. An existing file is overwritten. Parent directories must exist.

Example: spectrum.save_json("reference.spectrum.json").

load_json classmethod

load_json(path)

Read a JSON spectrum written by :meth:save_json.

Validate the format version, numeric dtypes, samples and metadata. Missing wavelength coordinates remain None. No normalization, interpolation, or sorting is performed. Invalid files raise ValueError (including JSONDecodeError); file access errors propagate normally.

Example: spectrum = Spectrum.load_json("reference.spectrum.json").

Spectrum collection

SpectrumCollection dataclass

SpectrumCollection(values, wavelengths_nm=None, *, detection_bands_nm=None, names=None, row_provenance=None, provenance=None)

Ordered component spectra on a common sample axis.

Rows in values represent components and columns represent samples. The matrix must be nonempty, real and finite. Optional wavelengths_nm has one positive coordinate per sample. Optional detection_bands_nm has one pair of positive lower and upper bounds per sample. Each axis follows the matrix's column order, including any repeated coordinates. Arrays are copied and exposed as read-only views.

names and row_provenance contain one name (or None) and one mapping per row, respectively. provenance describes the whole collection. Detector bands in row provenance must agree exactly in sample order and must occur in every row or none. When present, they provide the shared detector bands and must match any explicitly supplied bands. The reserved detection_bands_nm key is stored as the shared axis and exported in the provenance of each extracted Spectrum.

values property

values

The component-by-sample matrix.

wavelengths_nm property

wavelengths_nm

The shared wavelength coordinate array, if available.

detection_bands_nm property

detection_bands_nm

The shared detector bounds, if available.

names instance-attribute

names

row_provenance property

row_provenance

Independent copies of the descriptive metadata for each row.

provenance instance-attribute

provenance

from_spectra classmethod

from_spectra(spectra, *, detection_bands_nm=None, provenance=None)

Stack Spectrum objects in iteration order, using a common dtype.

Sample counts and wavelength labels must agree exactly in order. All rows may omit wavelengths. Stacking raises ValueError if dtype promotion would change a sample. No samples are sorted, normalized or interpolated.

spectrum

spectrum(index)

Return an independent Spectrum at an integer row index.

Shared detector bands are carried in the Spectrum's provenance.

save_json

save_json(path)

Save a version 1 Lumic spectrum collection as UTF-8 JSON.

Preserve row and sample order, scale, and numeric dtypes up to 64 bits. Store the optional sample axes once for the collection. Collection and row provenance must contain only JSON-compatible values. Validate serialization before overwriting an existing file.

load_json classmethod

load_json(path)

Read a version 1 file written by :meth:save_json.

Preserve component order, sample order, and recorded numeric dtypes. Invalid files raise ValueError. File access errors propagate normally.