Code documentation
Modules
Generic classes
The baseclass module provides generic classes for processing Raman data and storing spectral data
- class ramCOH.raman.baseclass.RamanProcessing(x: ndarray[Any, dtype[_ScalarType_co]], y: ndarray[Any, dtype[_ScalarType_co]], laser: float | None = None)[source]
Generic class for processing Raman spectral data
- Parameters:
x (array) – 1D array with x-axis data
y (array of float or int) – 1D array with intensity data
laser (float, optional) – laser wavelenghth in nm
- x
1D array with x-axis data
- Type:
array
- noise
calculated average noise on the baseline corrected spectrum
- Type:
float
- laser
laser wavelength in nm
- Type:
float, optional
- processing
dictionary of bool indicating which data treatments have been applied
- Type:
dict
- birs
(n, 2) shaped array with left and right boundaries of the last used baseline interpolation regions
- Type:
ndarray
- peaks
list of best-fit parameters of peaks fitted to the spectrum by either deconvolution or simple peak fitting
- Type:
list of dict
- _spectrumSelect
default spectrum selected for processing. The selection hierarchy is raw -> interference_corrected -> interpolated, where the last available spectrum is selected
- Type:
str
- baselineCorrect(baseline_regions=None, smooth_factor=1, **kwargs) None[source]
Baseline correction with natural smoothing splines from
csaps.csaps()fitted to interpolation regions.Results are stored in
signal.- Parameters:
baseline_regions (ndarray, optional) – (n, 2) array for n interpolation regions, where each row is [lower_limit, upper_limit]
smooth_factor (float, default 1) – baseline smoothing factor between 0-1. As the value approaches 0, the baseline becomes linear. It is passed to the smooth parameter of
csaps()as 1e-6 * smooth_factor**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
- calculate_noise(baseline_regions: ndarray[Any, dtype[_ScalarType_co]] | None = None) None[source]
Calculate noise on a baseline corrected spectrum.
Noise is calculated within baseline interpolation regions as 2 standard deviations on the baseline corrected spectrum.
Results are stored in
noise- Parameters:
baseline_regions (ndarray, optional) – (n, 2) shapped array with regions where noise will be calculated. If not set, the last set baseline interpolation regions will be used.
- deconvolve(*, peak_height, residuals_threshold: float = 10, baseline0: bool = True, min_amplitude: int | float = 3, min_peak_width: int | float = 4, fit_window: int = 4, max_iterations: int = 5, noise: float | None = None, inplace=True, **kwargs) None[source]
Deconvolve the spectrum into its constituent peaks.
Initial guesses for peak centers, amplitudes and widths are made with
scipy.signal.find_peaks(). The spectrum is subdivided into multiple windows with width =fit_window\(\times\) guessed width, where overlapping windows are merged. Within each windowmixed Gaussian-Lorentzianpeaks are iteratively fitted to the spectrum. With each new iteration an additional peak is summed with previous peaks. Iterations are stopped whenmax_iteratonis reached or when the residuals have improved less thenresiduals_threshold.Results are stored in
peaksas a list of dictionaries with fitted parameters for each individual peak.- Parameters:
peak_height (float or int) – minimum absolute peak height for initial guesses
residuals_threshold (float or int, default 10) – fit iterations are stopped when the root-mean squared error has decreased less than
residuals_threshold% compared to the previous iteration.baseline0 (bool, default True) – fix the baselevel at 0
min_amplitude (int or float, default 3) – minium amplitude of fitted peaks as a factor of noise on y.
min_peak_width (int, default 8) – minimum full width of fitted peaks in x-axis steps
fit_window (int, default 4) – width parameter of individual fit frames. Actual width is calculated as
fit_window\(\times\) guessed full width.max_iterations (int, default 5) – maximum fit iterations per window. Each iteration a new peak is added and
max_iterationsnoise (float, optional) – average noise on the spectrum. If no value is given it will be calculated with
calculate_noiseinplace (bool, default True) – return fitted peaks when False
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
x_min (float or int, optional) – x-axis lower limit of deconvolved spectrum
x_max (float or int, optional) – x-axis upper limit of deconvolved spectrum
- fit_peaks(peak_prominence=3, fit_window=12, curve='GL', **kwargs) None[source]
Find the best fit curves for peaks in the spectrum.
Does not take into account peak overlap, use
deconvolve()instead if you expect overlapping peaks. Initial guesses for peak centers, amplitudes and widths are made withscipy.signal.find_peaks()Results are stored in
peaksas a list of dictionaries with fitted parameters for each individual peak.- Parameters:
peak_prominence (float or int, default 3) – minimum peak prominence of fitted peaks, passed to
find_peaks()fit_window (int, default 12) – Intervals across which peaks are fitted range from (peak center - (fit_window * half width)) to (peak center + (fit_window * half width))
curve (str, default 'GL') –
curve shape. Options are:
’GL’ for mixed Gaussian-Lorentzian/pseudo-Voigt (default),
’G’ for Gaussian and
’L’ for Lorentzian
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
- interpolate(interpolate: List[Tuple[float, float]], smooth_factor=1, add_noise=True, output=False, use=False, **kwargs) None[source]
Interpolate across one or more regions
Interpolated signal is calculated fitting smoothing splines from
csaps.csaps()to the rest of the spectrum.Results are stored in
signal- Parameters:
interpolate (list of lists) – list of interpolation regions in pairs of [upper limit, lower limits]
smooth_factor (float, default 1) – Smoothes the interpolation; as it approaches 0 the interpolation becomes linear. Passed on to the smooth parameter in
csaps()as smooth_factor * 1e-5add_noise (bool, default True) – add spectrum noise to the interpolated sections
use (bool, default False) – set interpolated spectrum as source for further processing
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
output (bool, default False) – return interpolated sections as as tuple(x, y)
- normalise(inplace=False, **kwargs)[source]
Normalise spectrum to maximum intensity \(\times\) 100. The raw spectrum will be used if keyword argment
yis not set.If inplace is set to True, the smoothed spectrum will overwrite the original spectrum.
- Parameters:
inplace (bool, default False) – set normalised spectrum inplace
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
- smooth(type='gaussian', kernel_width=9, inplace=False, **kwargs) None[source]
Smoothing with either a moving average or with a Gaussian kernel. Note that each application shortens the spectrum by one kernel width. The raw spectrum will be used if keyword argment
yis not set.If inplace is set to True, the smoothed spectrum will overwrite the original spectrum.
- Parameters:
type (str) – Smoothing kernel type: ‘gaussian’ or ‘moving_average’
kernel_width (int, default 9) – Size of the smoothing kernel in steps along the x-axis
inplace (bool, default False) – Return the smoothed array
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
- class ramCOH.raman.baseclass.Signal(x: ndarray[Any, dtype[_ScalarType_co]], y: ndarray[Any, dtype[_ScalarType_co]])[source]
Container for spectral data
- Parameters:
x (array) – 1D array with x-axis data
y (array) – 1D array with intensity data
- x
x-axis data
- Type:
array of float
- raw
unprocessed intensity data
- Type:
array of float
- names
names of all current spectra
- Type:
list of str
- all
all current spectra as name: values
- Type:
dict
- interpolate_spectrum(old_x: ndarray[Any, dtype[_ScalarType_co]], old_y: ndarray[Any, dtype[_ScalarType_co]]) ndarray[Any, dtype[_ScalarType_co]][source]
Linearly interpolate a spectrum to match its x-axis with
x
- set_with_interpolation(name: str, x: ndarray[Any, dtype[_ScalarType_co]], y: ndarray[Any, dtype[_ScalarType_co]]) None[source]
Add a new spectrum with interpolated intensities.
Interpolation is calculated with
interpolate_spectrum()
Glass
The glass module provides a Raman processing class for processing spectra of silicate glasses, with tailored algorithms for quantifying their water content.
- class ramCOH.raman.glass.Glass(x: ndarray[Any, dtype[_ScalarType_co]], y: ndarray[Any, dtype[_ScalarType_co]], **kwargs)[source]
A subclass of
RamanProcessing, extended with methods for quantifying water contents of silicate glasses- Parameters:
x (array) – 1D array with x-axis data
y (array of float or int) – 1D array with intensity data
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
laser (float, optional) – laser wavelength in nm
- birs_default
(n, 2) shaped array with left and right boundaries of default baseline interpolation regions
- Type:
ndarray
- Si_SNR
silicate region signal:noise ratio
- Type:
float
- H2O_SNR
water region signal:noise ratio
- Type:
float
- SiH2Oareas
Area underneath peaks in the silicate and water regions
- Type:
tuple(float, float)
- _get_Si_H2O_regions() Tuple[Tuple[float, float], Tuple[float, float]][source]
Limits of the silicate region are calculated from the upper boundary of the lowest wavenumber baseline interpolation region (bir) and the last bir boundary < 1500 cm-1.
Limits of the water region are calculated from the first bir boundary > 1500 cm-1 and the lower limit of the highest wavenumber bir.
If not enough birs are set, the silicate region is set to 200-1400 cm-1 and the water region to 2000-4000 cm-1
- Returns:
silicate region boundaries, water region boundaries
- Return type:
Tuple[float, float], Tuple[float, float]
- calculate_SNR() None[source]
Calculate signal to noise ratios for silicate and raman peaks
Noise is calculated with self.calculate_noise and maxima within the silicate and water regions are used as signals.
silicate and water regions are set with
_get_Si_H2O_regions()and results are stored inSi_SNRandH2O_SNR.
- calculate_SiH2Oareas() Tuple[float, float][source]
Calculate areas underneath peaks in the silicate and water regions
Areas are calculated by trapezoidal integration of regions set by
_get_Si_H2O_regions()Results are stored inSiH2Oareas.- Returns:
silicate region area, water region area
- Return type:
float, float
- longCorrect(T_C=23.0, normalisation=False, inplace=True, **kwargs) None[source]
Long correction of Raman signal intensities
Correction for temperature and excitation line effects[1] according to:
\[I = I_{obs} * \left\{ \nu^{3}_{0} \left[ 1 - exp(-hc\nu/kT) \right] \nu / (\nu_{0} - \nu)^{4} \right\}\]where:
\(I\) = corrected intensity
\(I_{obs}\) = observed intensity
\(\nu_{0}\) = wavenumber of the incident laser (\(m^{-1}\))
\(\nu\) = measured wavenumber (\(m^{-1}\))
\(T\) = temperature in degrees Kelvin
with constants:
\(h\) = Planck constant
\(k\) = Boltzmann constant
\(c\) = speed of light
With constant values taken from
scipy.constants. Results are stored insignal.- Parameters:
T_C (float, default 23.0) – temperature during analysis in degrees celsius
normalisation (bool, default False) – normalise Long corrected spectrum to total area
inplace (bool, default True) – return Long corrected spectrum when False
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
References
- subtract_interference(interference: ndarray[Any, dtype[_ScalarType_co]], interval: Tuple[float | int, float | int], smoothing: float, inplace=True, use=False, **kwargs) None[source]
Subtract interfering signals
Glass and interfering signal are unmixed by minimising the difference between the unmixed spectrum and an interpolated, ideal spectrum. Interpolated signal is calculated across
intervalwith smoothing splines fromcsaps.csaps(), The interpolation region should be a region with intefering peaks bracketed by unaffected signal.Results are stored in
signal. as interference_corrected- Parameters:
interference (array) – intensities of intefering signal, x-axis must match with original spectrum
interval (tuple of float) – lower and upper limit of minimisation interval
smoothing (float) – smoothing of interpolation across minimisation interval
inplace (bool, default True) – return unmixed spectrum if False
use (bool, default False) – set unmixed spectrum as source for further processing
**kwargs (dict, optional) – Optional keyword arguments, see Other parameters
y (str, Optional) – name of the spectrum to be treated
CO2
The CO2 module provides a Raman processing class for for processing CO2Raman data
- class ramCOH.raman.co2.CO2(x: ndarray[Any, dtype[_ScalarType_co]], y: ndarray[Any, dtype[_ScalarType_co]], laser: float | None = None)[source]
A subclass of
RamanProcessing, extended with methods for fitting the CO2 Fermi diad.- diad
fitted peak parameters of the diad peaks
- Type:
tuple of dictionaries
- diad_split
splitting of the Fermi diad in cm-1
- Type:
float
- FermiDiad(peak_height=20, fit_window=20, min_peak_width=4, **kwargs)[source]
Fit the Fermi diad with mixed Gaussian-Lorentzian curves.
The spectrum is fitted with peaks with
deconvolve. The peak with the higest amplitude at wavenumbers <1330cm-1 is used as the lower diad peak and the peak with the highest amplitude at wavenumbers >1330cm-1 as the upper diad peak. Diad splitting is calculated as the absolute difference between the fitted centers of the diad peaks.Results are stored in
peaks,diadanddiad_split.Analyses of gas+liquid CO2 mixtures also result in mixed diad peaks, be careful with interpreting diad splitting if this is the case.
- Parameters:
peak_height (float or in, default 20) – minimum absolute height of peaks included in initial guesses, passed to
find_peaks()fit_window (int, default 20) – width parameter of the x-axis window within which peaks are fitted. Actual width is calculated as
fit_window\(\times\) guessed width. passed todeconvolve()min_peak_width (int, default 4) – minimum full width of fitted peaks in x-axis steps assed to
deconvolve()**kwargs (dict, optional) – Optional keyword arguments, passed to
deconvolve
- diad_curves(window=8, stepsize=0.5) Tuple[ndarray[Any, dtype[_ScalarType_co]], ndarray[Any, dtype[_ScalarType_co]]][source]
Get diad curves from fitted parameters
- Parameters:
window (int, default 8) – width parameter of the x-axis window within which curves are calculated. Actual width is calculated as
window\(\times\) peak width.stepsize (float) – x-axis stepsize
- Returns:
(x, 2) shaped arrays with columns x, y for each diad peak.
- Return type:
npt.NDArray, npt.NDArray
Neon
The neon module provides a Raman processing class for processing neon emission spectra.
- class ramCOH.raman.neon.Neon(x: ndarray[Any, dtype[_ScalarType_co]], y: ndarray[Any, dtype[_ScalarType_co]], laser: float)[source]
A subclass of
RamanProcessing, extended with methods for Raman streching and offset correction.- emission_peaks
theoretical neon emission lines (cm-1)
- Type:
array of float
- observed_peaks
observed neon emission lines (cm-1)
- Type:
array of float
- correction_factor
stretching correction factor (cm-1)
- Type:
float
- offset
offset correction factor (cm-1)
- Type:
float
- neonCorrection(left_nm=565.666, right_nm=574.83, search_window=6, **kwargs)[source]
Calculate stretching and offset correction factors between observed and theoretical Neon emission lines.
Compare theoretical neon emission lines near the calibration lines
left_nmandright_nmwith observed peaks. Correction for stretching is calculated as the ratio of the inter calibration line distances of theoretical and observed lines. Offset correction is calculated as the difference between the observed and theoretical position ofleft_nm.Results are stored in
correction_factorandoffset.- Parameters:
left_nm (float, default 565.666) – position of the left calibration line in nm
right_nm (float, default 574.83) – position of the right calibration line in nm
search_window (int) – Half-width (cm-1) of the peak search window around theoretical emission lines .
Curves
Curve shape functions
- ramCOH.signal_processing.curves.GaussLorentz(x, amplitude, center, width, baselevel, shape)[source]
Mixed Gaussian-Lorentzian (Pseudo-Voigt) curve
\[f(x) = Gaussian(x, A * \alpha, b, c, d) + Lorentzian(x, A * (1 - \alpha), b, c, d)\]- where:
\(A\) = amplitude
\(b\) = center
\(c\) = width (standard deviation)
\(d\) = baselevel
\(\alpha\) = shape
with parameterisations from
Gaussian()andLorentzian()- Parameters:
x (float or array-like) – x-axis
amplitude (float) – maximum peak height
center (float) – peak center
width (float) – peak width as half-width at full maximum
baselevel (float, default 0) – peak baselevel
shape (float) – mixing parameter between 0 and 1
- Returns:
Pseudo-Voigt curve evaluated at x
- Return type:
float, array-like
- ramCOH.signal_processing.curves.Gaussian(x, amplitude, center, width, baselevel=0)[source]
Gaussian curve
\[f(x) = A * exp \left( - \frac{(x - b)^{2}}{2c^2} \right) + d\]- where:
\(A\) = amplitude
\(b\) = center
\(c\) = width (standard deviation)
\(d\) = baselevel
- Parameters:
x (float or array-like) – x-axis
amplitude (float) – maximum peak height
center (float) – peak center
width (float) – peak width as standard deviation
baselevel (float, default 0) – peak baselevel
- Returns:
Gaussian curve evaluated at x
- Return type:
float, array-like
- ramCOH.signal_processing.curves.Lorentzian(x, amplitude, center, width, baselevel=0)[source]
Three-parameter Lorentzian curve
\[f(x) = A\left(\frac{c^2}{(x-b)^2 + c^2}\right) + d\]- where:
\(A\) = amplitude
\(b\) = center
\(c\) = width (standard deviation)
\(d\) = baselevel
- Parameters:
x (float or array-like) – x-axis
amplitude (float) – maximum peak height
center (float) – peak center
width (float) – peak width as half-width at full maximum
baselevel (float, default 0) – peak baselevel
- Returns:
Lorentzian curve evaluated at x
- Return type:
float, array-like