Skip to content

Simulation API

Use the simulation API to create spectral images with known component maps and spectra. You can use bundled emission curves offline and choose whether to work with a clean image or simulate a noisy acquisition.

If you want to… Use
Load a bundled FPbase emission curve load_offline_emission_spectrum()
Interpolate a curve at specific wavelengths resample_spectrum()
Integrate a curve over detector bands SpectralDetector.sample()
Mix spectra that are already on a shared detector grid simulate_linear_mixture()
Add count noise to an existing clean detector image add_confocal_noise()
Sample emission curves, mix components, and add noise in one call simulate_noisy_image()

Emission curves and detector sampling

load_offline_emission_spectrum() returns a pair of one-dimensional arrays containing wavelengths in nanometres and relative emission intensities. resample_spectrum() interpolates intensity at each requested wavelength. In contrast, SpectralDetector.sample() integrates intensity across the width of each band, producing one value per detector channel in input intensity times nanometres. Neither operation normalizes the result.

The built-in 980_32ch profile is an idealized model of one LSM 980 setting with band edges inferred from channel labels. For a custom detector with known contiguous bands, supply their edges to SpectralDetector(). from_centers() can infer edges when only channel centers are known. See Load and prepare spectra for saving sampled curves as reusable references.

SpectralDetector

SpectralDetector(bin_edges, *, channel_centers=None, name=None, nominal_resolution_nm=None)

Integrate spectral intensity within contiguous detector channels.

bin_edges has one more value than the number of channels. The sample method treats the supplied intensity as a piecewise-linear spectral density and returns its integral in each channel. The spectrum is taken to be zero outside its measured wavelength range.

bin_edges property

bin_edges

Wavelength boundaries of the channels, in nm.

channel_centers property

channel_centers

Nominal wavelength labels of the channels, in nm.

n_channels property

n_channels

from_centers classmethod

from_centers(channel_centers, *, name=None, nominal_resolution_nm=None)

Infer contiguous bin edges halfway between adjacent center labels.

The two outer edges are extrapolated by half the nearest center spacing. At least two centers are needed to infer a width.

from_profile classmethod

from_profile(name)

Build a named ideal detector profile.

sample

sample(wavelengths, intensities)

Return the integrated signal in each spectral channel.

Source samples are connected by straight lines. Input intensity is not normalized; output values have intensity-times-nm units. This is deterministic binning and does not add measurement noise.

load_offline_emission_spectrum(name, *, directory=None)

Load a saved FPbase emission curve by fluorophore name.

By default this reads the FPbase snapshot bundled with Lumic. Pass directory to use another collection of the same CSV files. This function has the same name -> (wavelengths, intensities) interface as lumic.fpbase.fetch_emission_spectrum, so scene geometry does not depend on where its spectra come from.

resample_spectrum(wavelengths, intensities, target_wavelengths)

Interpolate a spectrum onto a wavelength grid.

Values outside the measured wavelength range are set to zero. Input intensities are preserved as supplied; this function does not normalize.

Mix components and simulate noise

Simulation arrays use channels last. Component maps have shape (Y, X, K), the component matrix has shape (K, C), and the output detector image has shape (Y, X, C). The names and component maps follow the row order of the component matrix. When wrapping the output in a SpectralImage, pass axes="YXC".

simulate_linear_mixture() uses the component spectra at their supplied scale. If each row sums to one, the component maps can represent expected counts per component. simulate_noisy_image() integrates the input curves and normalizes the sampled rows for you. It keeps the clean expected counts, the noisy image, and the basis used to form the clean image.

import numpy as np

from lumic.simulation import (
    SpectralDetector,
    load_offline_emission_spectrum,
    simulate_noisy_image,
)

names = ("EGFP", "mCherry")
detector = SpectralDetector.from_profile("980_32ch")
curves = [load_offline_emission_spectrum(name) for name in names]
component_counts = np.array([[[100.0, 50.0], [50.0, 100.0]]])
result = simulate_noisy_image(
    component_counts,
    curves,
    detector,
    fluorophore_names=names,
    rng=np.random.default_rng(2026),
)

clean = result.expected_counts       # (1, 2, 32)
noisy = result.noisy_image            # (1, 2, 32)
basis = result.ideal.spectra          # (2, 32), unit-sum detector spectra

Noise is drawn in count units. The Poisson draw models shot noise. Set gaussian_sigma above zero to add independent Gaussian noise with that standard deviation. Observations below zero after adding noise are clipped to zero. Pass a seeded generator to reproduce a draw.

simulate_linear_mixture(abundances, spectra, wavelengths, fluorophore_names=None)

Form a noiseless (y, x, wavelength) image as abundances @ spectra.

add_confocal_noise(expected_counts, *, gaussian_sigma=0.0, rng=None)

Draw one noisy measurement for every pixel and spectral channel.

expected_counts is the clean detector image in expected count units. Draw Poisson counts, add independent zero-mean Gaussian noise in the same units, then clip negative observed values to zero. The input is unchanged. Pass a seeded numpy.random.Generator for reproducible results.

simulate_noisy_image(component_counts, component_spectra, detector, *, fluorophore_names=None, gaussian_sigma=0.0, rng=None)

Simulate recorded counts from per-pixel fluorophore contributions.

component_counts[y, x, k] is the expected number of recorded counts supplied by component k at that pixel, across all detector channels. component_spectra holds one (wavelengths, intensities) pair per component. Each spectrum is integrated by detector and normalized to unit sum, so the clean image sums to component_counts.sum(axis=-1).

The result retains that clean image and an independently noisy image. Spectra with no signal in the detector's wavelength range are rejected.

Result containers

SyntheticDataset holds the clean image and the ground truth used to form it. In NoisySimulationResult, ideal is that clean dataset. sampled_spectra holds the integrated rows before normalization, and source_spectra holds the original wavelength and intensity pairs.

SyntheticDataset dataclass

An ideal spectral image and the ground truth used to construct it.

Image axes are (y, x, wavelength) and abundance axes are (y, x, fluorophore).

image instance-attribute

abundances instance-attribute

spectra instance-attribute

wavelengths instance-attribute

fluorophore_names instance-attribute

NoisySimulationResult dataclass

Clean count image, noisy measurement, and detector ground truth.

ideal.abundances contains expected per-component recorded counts and ideal.spectra contains the normalized detector spectra. The sampled spectra retain their original integrated intensity scale, and source_spectra retains the curves before detector sampling.

ideal instance-attribute

expected_counts property

Clean expected detector counts, with axes (y, x, channel).

noisy_image instance-attribute

detector instance-attribute

source_spectra instance-attribute

sampled_spectra instance-attribute

gaussian_sigma instance-attribute

For a complete workflow, see Simulate a spectral image and Evaluate simulation recovery.