ModelBase

class mcalf.models.base.ModelBase[source]

Bases: object

Base class for spectral line model fitting.

Warning: This class should not be used directly. Use derived classes instead.

array

Array holding spectra.

Type

ndarray, dimensions are [‘time’, ‘row’, ‘column’, ‘spectra’]

background

Array holding spectral backgrounds.

Type

ndarray, dimensions are [‘time’, ‘row’, ‘column’]

Methods Summary

fit_spectrum(spectrum, **kwargs)

Fits the specified spectrum array

get_spectra([time, row, column, spectrum, …])

Gets corrected spectra from the spectral array

load_array(array[, names])

Load an array of spectra.

load_background(array[, names])

Load an array of spectral backgrounds.

test(X, y)

Test the accuracy of the trained neural network.

train(X, y)

Fit the neural network model to spectra matrix X and spectra labels y.

Methods Documentation

fit_spectrum(spectrum, **kwargs)[source]

Fits the specified spectrum array

Passes the spectrum argument to the fit method. For easily iterating over a list of spectra.

Parameters
  • spectrum (ndarray of ndim=1) – The explicit spectrum.

  • **kwargs (dictionary, optional) – Extra keyword arguments to pass to fit.

Returns

result – Result of the fit.

Return type

FitResult

See also

fit()

General fitting method

get_spectra(time=None, row=None, column=None, spectrum=None, correct=True, background=False)[source]

Gets corrected spectra from the spectral array

Takes either a set of indices or an explicit spectrum and optionally applied corrections and background removal.

Parameters
  • time (int or iterable, optional, default=None) – The time index. The index can be either a single integer index or an iterable. E.g. a list, a NumPy array, a Python range, etc. can be used.

  • row (int or iterable, optional, default=None) – The row index. See comment for time parameter.

  • column (int or iterable, optional, default=None) – The column index. See comment for time parameter.

  • spectrum (ndarray of ndim=1, optional, default=None) – The explicit spectrum.

  • correct (bool, optional, default=True) – Whether to reinterpolate the spectrum and apply the prefilter correction (if exists).

  • background (bool, optional, default=False) – Whether to include the background in the outputted spectra. Only removes the background if the relevant background array has been loaded. Does not remove background is processing an explicit spectrum.

load_array(array, names=None)[source]

Load an array of spectra.

Load array with dimension names names into the array parameter of the model object.

Parameters
  • array (ndarray of ndims > 1) – An array containing at least two spectra.

  • names (list of str, length = array.ndims) – List of dimension names for array. Valid dimension names are ‘time’, ‘row’, ‘column’ and ‘wavelength’. ‘wavelength’ is a required dimension.

See also

load_background()

Load an array of spectral backgrounds

load_background(array, names=None)[source]

Load an array of spectral backgrounds.

Load array with dimension names names into background parameter of the model object.

Parameters
  • array (ndarray of ndim>0) – An array containing at least two backgrounds.

  • names (list of str, length = array.ndims) – List of dimension names for array. Valid dimension names are ‘time’, ‘row’ and ‘column’.

See also

load_array()

Load and array of spectra

test(X, y)[source]

Test the accuracy of the trained neural network.

Prints a table of results showing:
  1. the percentage of predictions that equal the target labels;

  2. the average classification deviation and standard deviation from the ground truth classification

    for each labelled classification;

  3. the average classification deviation and standard deviation overall.

If the model object has an output parameter, it will create a CSV file (self.output/neural_network/test.csv) listing the predictions and ground truth data.

Parameters
  • X (ndarray or sparse matrix of shape (n_spectra, n_wavelengths)) – The input spectra.

  • y (ndarray of shape (n_spectra,) or (n_spectra, n_outputs)) – The target class labels.

See also

train()

Train the neural network

train(X, y)[source]

Fit the neural network model to spectra matrix X and spectra labels y.

Calls the fit method on the neural_network parameter of the model object.

Parameters
  • X (ndarray or sparse matrix of shape (n_spectra, n_wavelengths)) – The input spectra.

  • y (ndarray of shape (n_spectra,) or (n_spectra, n_outputs)) – The target class labels.

See also

test()

Test how well the neural network has been trained