The SpecModel class
This submodule contains the SpecModel class, central to the package.
The SpecModel class can be instantiated either directly or through a data
file, and hosts methods to plot the given data in diverse ways.
That class is also doted with methods to fit the spectrum’s continuum with
various polynomial functions, and extract a feature out of the spectrum.
- class specmodels.specmodels_base.SpecModel(*, data, label, ylabel='Reflectance', interpolation_order=5)[source]
Bases:
objectBase class to contain spectral data and associated methods for plotting.
- __init__(*, data, label, ylabel='Reflectance', interpolation_order=5)[source]
Instantiate a specmodel object.
- Parameters:
data (array[float]|str) – An array containing the spectral data (wavelengths v. reflectance) or a string containing the path to a file with the spectral data.
label (str) – A string shortly describing the data and used in the plots’ legends
ylabel (str) – A string describing the physical quantity associated with the data, e.g. bidirectional reflectance, reflectance factor, … . Defaults to a cookie-cutter description.
interpolation_order (int) – Interpolation order used in the B-spline representation of the data. Default is 5.
- my_interpolation(smoothness=0.0, interpolation_order=5, n_pts=10000)[source]
Set-up an interpolation of the data
Adjust the interpolation of the data via a weighted spline fit. Updates the class attributes
- Parameters:
smoothness (float, optional) – _description_. Defaults to 0.0.
interpolation_order (int, optional) – _description_. Defaults to 5.
n_pts (int, optional) – _description_. Defaults to 10000.
- Return type:
None
- check_interp()[source]
A method to compare the interpolation of the spectral data.
- Return type:
Figure
- base_plot(data=None)[source]
A method to plot the spectral data.
- Parameters:
data (list[SpecModel]|None) – A series of SpecModel objects to be plotted alongside the current object for comparison.
- Return type:
tuple[Figure,Axes]
- base_plot_norm(wl_ref, data=None, rects=None)[source]
Plot the interpolated spectrum and associated normalised version, providing a wavelength at which to normalise it.
- Parameters:
wl_ref (float) – The wavelength (in microns) at which to normalize the reflectance. Assuming the wavelength is within the range of the data, and that the reflectance at this wavelength is non-zero.
data (list[SpecModel] | None) – A series of SpecModel objects to be plotted alongside the current object for comparison.
rects (list[dict] | None) – A list of parameters defining the appropriate rectangles to draw on both plots. Adjust the width and height parameters to fit both plots’ y-axes ranges.
- Return type:
Figure
- continuum_fit(order_max=10, mask=None)[source]
Fitting continuum with polynomials
- Parameters:
order_max (int) – Optionnal - The maximum polynomial order to fit the continuum with.
mask (array[float]) – Optionnal - A Nx2 float array to discard N parts of the spectrum in the fitting.
- Returns:
An array of dictionnaries containing the parameters from the weigthed fit of the data with polynomial functions
- Return type:
array[dict]
- continuum_update_fits(order_max=10, mask=None)[source]
Update the internal ‘continuum_pn_fits’ parameter
- Parameters:
order_max (int) – The maximum polynomial order to test-fit the continuum to. Defaults to 10.
- Return type:
None
- continuum_plt_fit(mask=None)[source]
Plot the results from the fitting of the continuum
- Parameters:
mask (array[float]) – Optionnal - A Nx2 float array to discard N parts of the spectrum in the fitting.
- Return type:
None
- continuum_normalise(fnrm, ferr=None)[source]
Normalise the spectrum and return it as a new SpecModel.
- Parameters:
fnrm (array[float]) – Array of values to normalise the spectrum with, evaluated at the wavelengths of the spectrum.
- Return type:
- check_inflexion()[source]
Identify twitch and turns in a spectrum
Use spectrum derivatives to help identify spectral bands location(s).
- Return type:
Figure
- check_inflexions()[source]
Identify twitch and turns in a spectrum
Use spectrum derivatives to help identify spectral bands location(s). Plot the interpolated spectrum alongside its first two derivatives; Decorate scatter plots with zeroes of first derivatives and extrema of the second.
- Return type:
Figure
- rectify_extract_band(wrange, dlt=0.1, label='Spectral feature')[source]
Provided a wavelength interval defining an absorption/emission band, estimate the continuum between the shoulders of that feature, rectify the interpolated spectrum and return it as SpecModel object.
- Parameters:
wrange (array[float]) – A 2x1 float array defining the wavelength range bounding the spectral feature.
dlt (float|array[float]|None) – The width of wavelengths to consider around wrange’s values, to define the features’ shoulders.
label (str) – A name describing the spectral feature, passed on to the SpecModel object returned
- Returns:
A SpecModel object
- Return type:
- simple_fit(func, area=False)[source]
Wrapper around the curve_fit method.
Fit the data, i.e. a normalised band, with a given function.
- Parameters:
func (function) – a function listed in specmodels_function.
area (boolean, opt) – Set this keyword to fillplot the area of the band fitted by the given function.
- Returns:
Dictionnary containing the best fitting parameters, the associated uncertainties, the best fitting values, the average distance to the propagated uncertainties to the best fit, the residuals associated to the best fit and the normalised chi-square evaluation. fig, ax: the figure and axe of the plot illustrating the results
- Return type:
dict