XChroma.data_spectro module#

XChroma.data_spectro.DataSpecro

A class representing spectral data and providing methods for absorbance calculations and data processing.

class XChroma.data_spectro.DataSpecro(wavelengths: ~numpy.ndarray = None, intensities: ~numpy.ndarray = None, scaledxdata: ~numpy.ndarray = None, zero: ~numpy.ndarray = <factory>, static: ~numpy.ndarray = <factory>, lavg: ~collections.deque = <factory>, avg_i: ~numpy.ndarray = None, temp_dat: ~collections.deque = <factory>, times_temp_dat: ~collections.deque = <factory>)[source]#

Bases: object

A class representing spectral data and providing methods for absorbance calculations and data processing.

compute_absorbance(intensities: ndarray, static: ndarray = None, zero: ndarray = None) ndarray[source]#

Compute absorbance based on intensity, static, and zero data.

The absorbance is calculated using the following formula:

\[A = -log_{10}\left(\frac{I_{\text{intensities}} - I_{\text{static}}}{I_{\text{zero}} - I_{\text{static}}}\right)\]

Where:

  • \(( A )\) is the absorbance,

  • \(( I_{\text{intensities}} )\) are the measured intensities,

  • \(( I_{\text{static}} )\) are the static intensities (background),

  • \(( I_{\text{zero}} )\) are the baseline intensities (zero).

Parameters:
  • intensities (np.ndarray) – Array of intensity values.

  • static (np.ndarray, optional) – Static intensity values, default is self.static if not provided.

  • zero (np.ndarray, optional) – Baseline intensity values, default is self.zero if not provided.

Returns:

The absorbance values computed from the input intensities.

Return type:

np.ndarray

save_data(data: ndarray, cycle: int, spectype: str, csv_path: str = 'Spectrums.csv')[source]#

Saves spectral data to a CSV file, including a timestamp, cycle numbr, and measurement type (on/off/static/zero).

The function will first check if the CSV file exists. If it doesn’t, it will create a new file and write the necessary headers (timestamp, cycle, type, and wavelengths). Then, it appends a new row containing the timestamp, cycle, measurement type, and absorbance or intensity data.

Parameters:
  • data (np.ndarray) – Absorbance or intensity values, ordered like the wavlengths.

  • cycle (int) – Measurement cycle number.

  • spectype (str) – Measurement type (“on”, “off”, “static”, “zero”).

  • csv_path (str, optional) – File path for saving. The default is “Spectrums.csv”.

Return type:

None.

synthetic_data()[source]#

Generate synthetic data for the spectrometer.

The synthetic data is created using the (arbitrary) following formula:

\[I_{\text{synthetic}} = 0.1 + 0.25 \cdot | N(0, 1)| + 10 \cdot \left(\sin\left(\frac{t}{10}\right)\right)^2 \cdot \exp\left(-\frac{(\lambda - \mu)^2}{2 \cdot \sigma^2}\right)\]
Returns:

The generated synthetic intensity data.

Return type:

np.ndarray

temporal_abs(roi: tuple, ydata: ndarray, xdata: ndarray, static: ndarray, zero: ndarray)[source]#

Calculate temporal absorbance for a specific region of interest (ROI) in the data.

This method computes absorbance for a subset of the data defined by the ROI and stores the results over time.

Parameters:
  • roi (tuple) – A tuple defining the region of interest (start, end) in the x-axis (wavelength or other units).

  • ydata (np.ndarray) – The y-axis data (typically intensities or absorbance).

  • xdata (np.ndarray) – The x-axis data (wavelengths or other units).

  • static (np.ndarray) – The static intensity data for baseline correction.

  • zero (np.ndarray) – The zero intensity data for baseline correction.

Returns:

The indices of the region of interest, a list of temporal absorbance data, and a list of timestamps corresponding to the temporal absorbance data.

Return type:

tuple

update_moving_avg(avg_window: int = 10)[source]#

Update the moving average of the intensities array using a deque.

This method maintains a sliding window of intensity data to calculate the moving average. It optimizes memory usage by efficiently managing the window size.

Parameters:

avg_window (int, optional) – The size of the moving average window. Default is 10.

Return type:

None

avg_i: ndarray = None#

The moving average of the intensities.

intensities: ndarray = None#

An array containing the intensity values corresponding to the wavelengths.

lavg: deque#

A deque to hold the windowed data for moving average calculations.

scaledxdata: ndarray = None#

Scaled version of the x-axis data (wavelengths or other units).

static: ndarray#

Static intensity data, default is an array of zeros.

temp_dat: deque#

A deque to hold the temporally averaged absorbance data.

times_temp_dat: deque#

A deque to hold the timestamps of the temporal absorbance data.

wavelengths: ndarray = None#

An array containing the wavelengths of the spectral data.

zero: ndarray#

Baseline data for the spectral intensities, default is an array of ones.