Warning

You are reading the documentation related to the development version. Go here if you are looking for the documentation of the stable release.

spectrochempy.Optimize

class Optimize(*, log_level='WARNING', warm_start=False, amplitude_mode='height', autoampl=False, autobase=False, callback_every=10, constraints=None, dry=False, max_fun_calls=0, max_iter=500, method='least_squares', script='')[source]

Non-linear Least-Square Optimization and Curve-Fitting.

Works on a 1D or 2D dataset.

# TODO: complete this description

Parameters
  • log_level (any of ["INFO", "DEBUG", "WARNING", "ERROR"], optional, default: "WARNING") – The log level at startup. It can be changed later on using the set_log_level method or by changing the log_level attribute.

  • warm_start (bool, optional, default: False) – When fitting repeatedly on the same dataset, but for multiple parameter values (such as to find the value maximizing performance), it may be possible to reuse previous model learned from the previous parameter value, saving time.

    When warm_start is True, the existing fitted model attributes is used to initialize the new model in a subsequent call to fit.

  • amplitude_mode (any value of [ 'area' , 'height' ], optional, default: 'height') – Initial amplitude setting mode.

  • autoampl (bool, optional, default: False) – Whether to apply an automatic amplitude correction.

  • autobase (bool, optional, default: False) – Whether to apply an automatic baseline correction.

  • callback_every (int, optional, default: 10) – Number of iteration between each callback report. Used for printing or display intermediate results.

  • constraints (any value, optional, default: None) – Constraints.

  • dry (bool, optional, default: False) – If True perform a dry run. Mainly used to check the validity of the input parameters.

  • max_fun_calls (int, optional, default: 0) – Maximum number of function calls at each iteration.

  • max_iter (int, optional, default: 500) – Maximum number of fitting iteration.

  • method (any value of [ 'least_squares' , 'leastsq' , 'simplex' , 'basinhopping' ], optional, default: 'least_squares') – Optimization method (see scipy.optimize docs for details).

  • script (str, optional, default: '') – Script defining models and parameters for fitting.

Attributes Summary

X

Return the X input dataset (eventually modified by the model).

Y

The Y input.

amplitude_mode

Initial amplitude setting mode.

autoampl

Whether to apply an automatic amplitude correction.

autobase

Whether to apply an automatic baseline correction.

callback_every

Number of iteration between each callback report.

components

NDDataset with components in feature space (n_components, n_features).

config

traitlets.config.Config object.

constraints

Constraints.

dry

If True perform a dry run.

fp

A trait whose value must be an instance of a specified class.

log

Return log output.

max_fun_calls

Maximum number of function calls at each iteration.

max_iter

Maximum number of fitting iteration.

method

Optimization method (see scipy.optimize docs for details).

modeldata

An instance of a Python list.

n_components

Number of components that were fitted.

name

Object name

script

Script defining models and parameters for fitting.

usermodels

User defined models.

Methods Summary

fit(X)

Perform a non-linear optimization of the X dataset.

fit_transform(X[, Y])

Fit the model with X and apply the dimensionality reduction on X.

get_components([n_components])

Return the component's dataset: (selected n_components, n_features).

inverse_transform([X_transform])

Transform data back to its original space.

parameters([replace, removed, default])

Alias for params method.

params([default])

Current or default configuration values.

plotmerit([X, X_hat])

Plot the input (X), reconstructed (X_hat) and residuals.

predict()

Return the fitted model.

reconstruct([X_transform])

Transform data back to its original space.

reduce([X])

Apply dimensionality reduction to X.

reset()

Reset configuration parameters to their default values

to_dict()

Return config value in a dict form.

transform([X])

Apply dimensionality reduction to X.

Attributes Documentation

X

Return the X input dataset (eventually modified by the model).

Y

The Y input.

amplitude_mode

Initial amplitude setting mode.

autoampl

Whether to apply an automatic amplitude correction.

autobase

Whether to apply an automatic baseline correction.

callback_every

Number of iteration between each callback report. Used for printing or display intermediate results.

components

NDDataset with components in feature space (n_components, n_features).

See also

get_components

Retrieve only the specified number of components.

config

traitlets.config.Config object.

constraints

Constraints.

dry

If True perform a dry run. Mainly used to check the validity of the input parameters.

fp

A trait whose value must be an instance of a specified class.

The value can also be an instance of a subclass of the specified class.

Subclasses can declare default classes by overriding the klass attribute

log

Return log output.

max_fun_calls

Maximum number of function calls at each iteration.

max_iter

Maximum number of fitting iteration.

method

Optimization method (see scipy.optimize docs for details).

modeldata

An instance of a Python list.

n_components

Number of components that were fitted.

name

Object name

script

Script defining models and parameters for fitting.

usermodels

User defined models.

Methods Documentation

fit(X)[source]

Perform a non-linear optimization of the X dataset.

Parameters

X (NDDataset or array-like of shape (n_observations, n_features)) – Training data.

Returns

self – The fitted instance itself.

See also

fit_transform

Fit the model with an input dataset X and apply the dimensionality reduction on X.

fit_reduce

Alias of fit_transform (Deprecated).

fit_transform(X, Y=None, **kwargs)[source]

Fit the model with X and apply the dimensionality reduction on X.

Parameters
Returns

NDDataset – Dataset with shape (n_observations, n_components).

Other Parameters

n_components (int, optional) – The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in the fit process.

get_components(n_components=None)

Return the component’s dataset: (selected n_components, n_features).

Parameters

n_components (int, optional, default: None) – The number of components to keep in the output dataset. If None, all calculated components are returned.

Returns

NDDataset – Dataset with shape (n_components, n_features)

inverse_transform(X_transform=None, **kwargs)

Transform data back to its original space.

In other words, return an input X_original whose reduce/transform would be X_transform.

Parameters
  • X_transform (array-like of shape (n_observations, n_components), optional) – Reduced X data, where n_observations is the number of observations and n_components is the number of components. If X_transform is not provided, a transform of X provided in fit is performed first.

  • **kwargs (keyword parameters, optional) – See Other Parameters.

Returns

NDDataset – Dataset with shape (n_observations, n_features).

Other Parameters

n_components (int, optional) – The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in the fit process.

See also

reconstruct

Alias of inverse_transform (Deprecated).

parameters(replace="params", removed="0.7.1") def parameters(self, default=False)[source]

Alias for params method.

params(default=False)[source]

Current or default configuration values.

Parameters

default (bool, optional, default: False) – If default is True, the default parameters are returned, else the current values.

Returns

dict – Current or default configuration values.

plotmerit(X=None, X_hat=None, **kwargs)[source]

Plot the input (X), reconstructed (X_hat) and residuals.

\(X\) and \(\hat{X}\) can be passed as arguments. If not, the X attribute is used for \(X\)and \(\hat{X}\)is computed by the inverse_transform method

Parameters
  • X (NDDataset, optional) – Original dataset. If is not provided (default), the X attribute is used and X_hat is computed using inverse_transform.

  • X_hat (NDDataset, optional) – Inverse transformed dataset. if X is provided, X_hat must also be provided as compuyed externally.

  • **kwargs (keyword parameters, optional) – See Other Parameters.

Returns

Axes – Matplotlib subplot axe.

Other Parameters
  • colors (tuple or ndarray of 3 colors, optional) – Colors for X , X_hat and residuals E . in the case of 2D, The default colormap is used for X . By default, the three colors are NBlue , NGreen and NRed (which are colorblind friendly).

  • offset (float, optional, default: None) – Specify the separation (in percent) between the \(X\) , \(X_hat\) and \(E\).

  • nb_traces (int or 'all', optional) – Number of lines to display. Default is 'all'.

  • **others (Other keywords parameters) – Parameters passed to the internal plot method of the X dataset.

predict()[source]

Return the fitted model.

Returns

NDDataset – The fitted model.

reconstruct(X_transform=None, **kwargs)[source]

Transform data back to its original space.

In other words, return an input X_original whose reduce/transform would be X_transform.

Parameters
  • X_transform (array-like of shape (n_observations, n_components), optional) – Reduced X data, where n_observations is the number of observations and n_components is the number of components. If X_transform is not provided, a transform of X provided in fit is performed first.

  • **kwargs (keyword parameters, optional) – See Other Parameters.

Returns

NDDataset – Dataset with shape (n_observations, n_features).

Other Parameters

n_components (int, optional) – The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in the fit process.

See also

reconstruct

Alias of inverse_transform (Deprecated).

Notes

Deprecated in version 0.6.

reduce(X=None, **kwargs)[source]

Apply dimensionality reduction to X.

Parameters
Returns

NDDataset – Dataset with shape (n_observations, n_components).

Other Parameters

n_components (int, optional) – The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in the fit process.

Notes

Deprecated in version 0.6.

reset()[source]

Reset configuration parameters to their default values

to_dict()[source]

Return config value in a dict form.

Returns

dict – A regular dictionary.

transform(X=None, **kwargs)

Apply dimensionality reduction to X.

Parameters
Returns

NDDataset – Dataset with shape (n_observations, n_components).

Other Parameters

n_components (int, optional) – The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in the fit process.

Examples using spectrochempy.Optimize

Analysis CP NMR spectra

Analysis CP NMR spectra

Processing NMR spectra (slicing, baseline correction, peak picking, peak fitting)

Processing NMR spectra (slicing, baseline correction, peak picking, peak fitting)

Processing Relaxation measurement

Processing Relaxation measurement

Fitting 1D dataset

Fitting 1D dataset