IBIS8542Model¶
-
class
mcalf.models.
IBIS8542Model
(stationary_line_core=None, absorption_guess=None, emission_guess=None, absorption_min_bound=None, emission_min_bound=None, absorption_max_bound=None, emission_max_bound=None, absorption_x_scale=None, emission_x_scale=None, neural_network=None, original_wavelengths=None, constant_wavelengths=None, delta_lambda=None, sigma=None, prefilter_response=None, prefilter_ref_main=None, prefilter_ref_wvscl=None, config=None, output=None)[source]¶ Bases:
mcalf.models.base.ModelBase
Class for working with IBIS 8542 Å calcium II spectral imaging observations
- Parameters
original_wavelengths (array_like) – One-dimensional array of wavelengths that correspond to the uncorrected spectral data.
stationary_line_core (float, optional, default = 8542.104320687517) – Wavelength of the stationary line core.
absorption_guess (array_like, length=4, optional, default = [-1000, stationary_line_core, 0.2, 0.1]) – Initial guess to take when fitting the absorption Voigt profile.
emission_guess (array_like, length=4, optional, default = [1000, stationary_line_core, 0.2, 0.1]) – Initial guess to take when fitting the emission Voigt profile.
absorption_min_bound (array_like, length=4, optional, default = [-np.inf, stationary_line_core-0.15, 1e-6, 1e-6]) – Minimum bounds for all the absorption Voigt profile parameters in order of the function’s arguments.
emission_min_bound (array_like, length=4, optional, default = [0, stationary_line_core-0.15, 1e-6, 1e-6]) – Minimum bounds for all the emission Voigt profile parameters in order of the function’s arguments.
absorption_max_bound (array_like, length=4, optional, default = [0, stationary_line_core+0.15, 1, 1]) – Maximum bounds for all the absorption Voigt profile parameters in order of the function’s arguments.
emission_max_bound (array_like, length=4, optional, default = [np.inf, stationary_line_core+0.15, 1, 1]) – Maximum bounds for all the emission Voigt profile parameters in order of the function’s arguments.
absorption_x_scale (array_like, length=4, optional, default = [1500, 0.2, 0.3, 0.5]) – Characteristic scale for all the absorption Voigt profile parameters in order of the function’s arguments.
emission_x_scale (array_like, length=4, optional, default = [1500, 0.2, 0.3, 0.5]) – Characteristic scale for all the emission Voigt profile parameters in order of the function’s arguments.
neural_network (sklearn.neural_network.MLPClassifier, optional, default = see description) – The MLPClassifier object that will be used to classify the spectra. Its default value is MLPClassifier(solver=’lbfgs’, alpha=1e-3, hidden_layer_sizes=(10, 4), random_state=1).
constant_wavelengths (array_like, length same as original_wavelengths, optional, default = see description) – The desired set of wavelengths that the spectral data should be rescaled to represent. It is assumed that these have constant spacing, but that may not be a requirement if you specify your own array. The default value is an array from the minimum to the maximum wavelength of original_wavelengths in constant steps of delta_lambda, overshooting the upper bound if the maximum wavelength has not been reached.
delta_lambda (float, optional, default = 0.05) – The step used between each value of constant_wavelengths when its default value has to be calculated.
sigma (list of array_like or bool, length=(2, n_wavelengths), optional, default = [type1, type2]) – A list of different sigma that are used to weight particular wavelengths along the spectra when fitting. The fitting method will expect to be able to choose a sigma array from this list at a specific index. It’s default value is [generate_sigma(i, constant_wavelengths, stationary_line_core) for i in [1, 2]]. See utils.generate_sigma() for more information. If bool, True will generate the default sigma value regardless of the value specified in config, and False will set sigma to be all ones, effectively disabling it.
prefilter_response (array_like, length=n_wavelengths, optional, default = see note) – Each constant wavelength scaled spectrum will be corrected by dividing it by this array. If prefilter_response is not give, and prefilter_ref_main and prefilter_ref_wvscl are not given, prefilter_response will have a default value of None.
prefilter_ref_main (array_like, optional, default = None) – If prefilter_response is not specified, this will be used along with prefilter_ref_wvscl to generate the default value of prefilter_response.
prefilter_ref_wvscl (array_like, optional, default = None) – If prefilter_response is not specified, this will be used along with prefilter_ref_main to generate the default value of prefilter_response.
config (str, optional, default = None) – Filename of a .yml file (relative to current directory) containing the initialising parameters for this object. Parameters provided explicitly to the object upon initialisation will override any provided in this file. All (or some) parameters that this object accepts can be specified in this file, except neural_network and config. Each line of the file should specify a different parameter and be formatted like emission_guess: ‘[-inf, wl-0.15, 1e-6, 1e-6]’ or original_wavelengths: ‘original.fits’ for example. When specifying a string, use ‘inf’ to represent np.inf and ‘wl’ to represent stationary_line_core as shown. If the string matches a file, utils.load_parameter() is used to load the contents of the file.
output (str, optional, default = None) – If the program wants to output data, it will place it relative to the location specified by this parameter. Some methods will only save data to a file if this parameter is not None. Such cases will be documented where relevant.
-
original_wavelengths
¶ One-dimensional array of wavelengths that correspond to the uncorrected spectral data.
- Type
array_like
-
stationary_line_core
¶ Wavelength of the stationary line core.
- Type
float, optional, default = 8542.099145376844
-
absorption_guess
¶ Initial guess to take when fitting the absorption Voigt profile.
- Type
array_like, length=4, optional, default = [-1000, stationary_line_core, 0.2, 0.1]
-
emission_guess
¶ Initial guess to take when fitting the emission Voigt profile.
- Type
array_like, length=4, optional, default = [1000, stationary_line_core, 0.2, 0.1]
-
absorption_min_bound
¶ Minimum bounds for all the absorption Voigt profile parameters in order of the function’s arguments.
- Type
array_like, length=4, optional, default = [-np.inf, stationary_line_core-0.15, 1e-6, 1e-6]
-
emission_min_bound
¶ Minimum bounds for all the emission Voigt profile parameters in order of the function’s arguments.
- Type
array_like, length=4, optional, default = [0, -np.inf, 1e-6, 1e-6]
-
absorption_max_bound
¶ Maximum bounds for all the absorption Voigt profile parameters in order of the function’s arguments.
- Type
array_like, length=4, optional, default = [0, stationary_line_core+0.15, 1, 1]
-
emission_max_bound
¶ Maximum bounds for all the emission Voigt profile parameters in order of the function’s arguments.
- Type
array_like, length=4, optional, default = [np.inf, np.inf, 1, 1]
-
absorption_x_scale
¶ Characteristic scale for all the absorption Voigt profile parameters in order of the function’s arguments.
- Type
array_like, length=4, optional, default = [1500, 0.2, 0.3, 0.5]
-
emission_x_scale
¶ Characteristic scale for all the emission Voigt profile parameters in order of the function’s arguments.
- Type
array_like, length=4, optional, default = [1500, 0.2, 0.3, 0.5]
-
neural_network
¶ The MLPClassifier object (or similar) that will be used to classify the spectra. Defaults to a GridSearchCV with MLPClassifier(solver=’lbfgs’, hidden_layer_sizes=(40,), max_iter=1000) for best alpha selected from [1e-5, 2e-5, 3e-5, 4e-5, 5e-5, 6e-5, 7e-5, 8e-5, 9e-5].
- Type
sklearn.neural_network.MLPClassifier, optional, default = see description
-
constant_wavelengths
¶ The desired set of wavelengths that the spectral data should be rescaled to represent. It is assumed that these have constant spacing, but that may not be a requirement if you specify your own array. The default value is an array from the minimum to the maximum wavelength of original_wavelengths in constant steps of delta_lambda, overshooting the upper bound if the maximum wavelength has not been reached.
- Type
array_like, length same as original_wavelengths, optional, default = see description
-
sigma
¶ A list of different sigma that are used to weight particular wavelengths along the spectra when fitting. The fitting method will expect to be able to choose a sigma array from this list at a specific index. It’s default value is [generate_sigma(i, constant_wavelengths, stationary_line_core) for i in [1, 2]]. See utils.generate_sigma() for more information.
- Type
list of array_like, length=(2, n_wavelengths), optional, default = [type1, type2]
-
prefilter_response
¶ Each constant wavelength scaled spectrum will be corrected by dividing it by this array. If prefilter_response is not give, and prefilter_ref_main and prefilter_ref_wvscl are not given, prefilter_response will have a default value of None.
- Type
array_like, length=n_wavelengths, optional, default = see note
-
output
¶ If the program wants to output data, it will place it relative to the location specified by this parameter. Some methods will only save data to a file if this parameter is not None. Such cases will be documented where relevant.
- Type
str, optional, default = None
-
quiescent_wavelength
¶ The index within the fitted parameters of the absorption Voigt line core wavelength.
- Type
int, default = 1
-
active_wavelength
¶ The index within the fitted parameters of the emission Voigt line core wavelength.
- Type
int, default = 5
Methods Summary
classify_spectra
([time, row, column, …])Classify the specified spectra
fit
([time, row, column, spectrum, profile, …])Fits the model to specified spectra
plot
([fit, time, row, column, spectrum, …])Plots the data and fitted parameters
plot_separate
(*args, **kwargs)Plot the fitted profiles separately
plot_subtraction
(*args, **kwargs)Plot the spectrum with the emission fit subtracted from it
Methods Documentation
-
classify_spectra
(time=None, row=None, column=None, spectra=None, only_normalise=False)[source]¶ Classify the specified spectra
Will also normalise each spectrum such that its intensity will range from zero to one.
- 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.
spectra (ndarray, optional, default=None) – The explicit spectra to classify. If only_normalise is False, this must be 1D.
only_normalise (bool, optional, default = False) – Whether the single spectrum given in spectra should not be interpolated and corrected.
- Returns
classifications – Array of classifications with the same time, row and column indices as spectra.
- Return type
ndarray
See also
train()
Train the neural network
test()
Test the accuracy of the neural network
get_spectra()
Get processed spectra from the objects array attribute
-
fit
(time=None, row=None, column=None, spectrum=None, profile=None, sigma=None, classifications=None, background=None, n_pools=None)[source]¶ Fits the model to specified spectra
Fits the model to an array of spectra using multiprocessing if requested.
- 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, ndim=1, optional, default=None) – The explicit spectrum to fit the model to.
profile (str, optional, default = None) – The profile to fit. (Will infer profile from classifications if omitted.)
sigma (int or array_like, optional, default = None) – Explicit sigma index or profile. See _get_sigma for details.
classifications (int, optional, default = None) – Classifications to determine the fitted profile to use (if profile not explicitly given). Will use neural network to classify them if not.
background (float, optional, default = None) – If provided, this value will be subtracted from the explicit spectrum provided in spectrum. Will not be applied to spectra found from the indices, use the load_background method instead.
n_pools (int, optional, default = None) – The number of processing pools to calculate the fitting over. This allocates the fitting of different spectra to n_pools separate worker processes. When processing a large number of spectra this will make the fitting process take less time overall. It also distributes such that each worker process has the same ratio of classifications to process. This should balance out the workload between workers. If few spectra are being fitted, performance may decrease due to the overhead associated with splitting the evaluation over separate processes. If n_pools is not an integer greater than zero, it will fit the spectrum with a for loop.
- Returns
result – Outcome of the fits returned as a list of FitResult objects
- Return type
list of FitResult, length=n_spectra
-
plot
(fit=None, time=None, row=None, column=None, spectrum=None, classification=None, background=None, sigma=None, stationary_line_core=None, output=False, **kwargs)[source]¶ Plots the data and fitted parameters
- Parameters
fit (FitResult or list or array_like, optional, default = None) – The fitted parameters to plot with the data. Can extract the necessary plot metadata from the fit object. Otherwise, fit should be the parameters to be fitted to either a Voigt or double Voigt profile depending on the number of parameters fitted.
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 length original_wavelengths, ndim=1, optional, default = None) – The explicit spectrum to plot along with a fit (if specified).
classification (int, optional, default = None) – Used to determine which sigma profile to use. See _get_sigma for more details.
background (float or array_like of length constant_wavelengths, optional, default = see note) – Background to added to the fitted profiles. If a spectrum is given, this will default to zero, otherwise the value loaded by load_background will be used.
sigma (int or array_like, optional, default = None) – Explicit sigma index or profile. See _get_sigma for details.
stationary_line_core (float, optional, default = self.stationary_line_core) – The stationary line core wavelength to mark on the plot.
output (bool or str, optional, default = False) – Whether to save the plot to a file. If true, a file of format plot_<time>_<row>_<column>.eps will be created in the current directory. If a string, that will be used as the filename. (Can change filetype like this.) If false, no file will be created.
**kwargs –
Parameters used by matplotlib and separate (see plot_separate) and subtraction (see plot_subtraction).
figsize passed to matplotlib.pyplot.figure
legend_position passed to matplotlib.pyplot.legend
dpi passed to matplotlib.pyplot.figure and matplotlib.pyplot.savefig
fontfamily passed to matplotlib.pyplot.rc(‘font’, family=`fontfamily)` if given
See also
plot_separate()
Plot the fit parameters separately
plot_subtraction()
Plot the spectrum with the emission fit subtracted from it
FitResult.plot()
Plotting method on the fit result
-
plot_separate
(*args, **kwargs)[source]¶ Plot the fitted profiles separately
If multiple profiles exist, fit them separately. See plot for more details.
See also
plot()
General plotting method
plot_subtraction()
Plot the spectrum with the emission fit subtracted from it
FitResult.plot()
Plotting method on the fit result
-
plot_subtraction
(*args, **kwargs)[source]¶ Plot the spectrum with the emission fit subtracted from it
If multiple profiles exist, subtract the fitted emission from the raw data. See plot for more details.
See also
plot()
General plotting method
plot_separate()
Plot the fit parameters separately
FitResult.plot()
Plotting method on the fit result