Model1D#
- class hyperspy.models.model1d.Model1D(signal1D, dictionary=None)#
Bases:
BaseModel
Model and data fitting for one dimensional signals.
A model is constructed as a linear combination of
components1D
that are added to the model usingappend()
orextend()
. There are many predifined components available in thecomponents1D
module. If needed, new components can be created easily using theExpression
component or by using the code of existing components as a template.Once defined, the model can be fitted to the data using
fit()
ormultifit()
. Once the optimizer reaches the convergence criteria or the maximum number of iterations the new value of the component parameters are stored in the components.It is possible to access the components in the model by their name or by the index in the model. An example is given at the end of this docstring.
Methods
fit_component
(component[, signal_range, ...])Fit the given component in the given signal range.
enable_adjust_position
([components, ...])Allow changing the x position of component by dragging a vertical line that is plotted in the signal model figure
Disable the interactive adjust position feature
plot
([plot_components, plot_residual])Plot the current spectrum to the screen and a map with a cursor to explore the SI.
set_signal_range
(*args, **kwargs)Use only the selected spectral range defined in its own units in the fitting routine.
remove_signal_range
(*args, **kwargs)Removes the data in the given range from the data range that will be used by the fitting rountine.
Resets the data range
add_signal_range
(*args, **kwargs)Adds the data in the given range from the data range that will be used by the fitting rountine.
Examples
In the following example we create a histogram from a normal distribution and fit it with a gaussian component. It demonstrates how to create a model from a
Signal1D
instance, add components to it, adjust the value of the parameters of the components, fit the model to the data and access the components in the model.>>> s = hs.signals.Signal1D( ... np.random.normal(scale=2, size=10000)).get_histogram() >>> g = hs.model.components1D.Gaussian() >>> m = s.create_model() >>> m.append(g) >>> m.print_current_values() Model1D: histogram CurrentComponentValues: Gaussian Active: True Parameter Name | Free | Value | Std | Min | Max | Linear ============== | ======= | ========== | ========== | ========== | ========== | ====== A | True | 1.0 | None | 0.0 | None | True centre | True | 0.0 | None | None | None | False sigma | True | 1.0 | None | 0.0 | None | False >>> g.centre.value = 3 >>> m.print_current_values() Model1D: histogram CurrentComponentValues: Gaussian Active: True Parameter Name | Free | Value | Std | Min | Max | Linear ============== | ======= | ========== | ========== | ========== | ========== | ====== A | True | 1.0 | None | 0.0 | None | True centre | True | 3.0 | None | None | None | False sigma | True | 1.0 | None | 0.0 | None | False >>> g.sigma.value 1.0 >>> m.fit() >>> g.sigma.value 1.9779042300856682 >>> m[0].sigma.value 1.9779042300856682 >>> m["Gaussian"].centre.value -0.072121936813224569
- add_signal_range(*args, **kwargs)#
Adds the data in the given range from the data range that will be used by the fitting rountine.
- disable_adjust_position()#
Disable the interactive adjust position feature
See also
- disable_plot_components()#
Disable interactive adjustment of the position of the components that have a well defined position. Use after
plot()
.
- enable_adjust_position(components=None, fix_them=True, show_label=True)#
Allow changing the x position of component by dragging a vertical line that is plotted in the signal model figure
- Parameters:
- components
None
,list
ofComponent
If None, the position of all the active components of the model that has a well defined x position with a value in the axis range will get a position adjustment line. Otherwise the feature is added only to the given components. The components can be specified by name, index or themselves.
- fix_thembool, default
True
If True the position parameter of the components will be temporarily fixed until adjust position is disable. This can be useful to iteratively adjust the component positions and fit the model.
- show_labelbool, default
True
If True, a label showing the component name is added to the plot next to the vertical line.
- components
See also
- enable_plot_components()#
Enable interactive adjustment of the position of the components that have a well defined position. Use after
plot()
.
- fit_component(component, signal_range='interactive', estimate_parameters=True, fit_independent=False, only_current=True, display=True, toolkit=None, **kwargs)#
Fit the given component in the given signal range.
This method is useful to obtain starting parameters for the components. Any keyword arguments are passed to the fit method.
- Parameters:
- component
Component
The component must be in the model, otherwise an exception is raised. The component can be specified by name, index or itself.
- signal_range
str
,tuple
ofNone
If
'interactive'
the signal range is selected using the span selector on the spectrum plot. The signal range can also be manually specified by passing a tuple of floats (left, right). If None the current signal range is used. Note that ROIs can be used in place of a tuple.- estimate_parametersbool, default
True
If True will check if the component has an estimate_parameters function, and use it to estimate the parameters in the component.
- fit_independentbool, default
False
If True, all other components are disabled. If False, all other component paramemeters are fixed.
- displaybool
If True, display the user interface widgets. If False, return the widgets container in a dictionary, usually for customisation or testing.
- toolkit
str
, iterable ofstr
orNone
If None (default), all available widgets are displayed or returned. If string, only the widgets of the selected toolkit are displayed if available. If an interable of toolkit strings, the widgets of all listed toolkits are displayed or returned.
- **kwargs
dict
All extra keyword arguments are passed to the py:meth:~hyperspy.model.BaseModel.fit or py:meth:~hyperspy.model.BaseModel.multifit method, depending if
only_current
is True or False.
- component
Examples
Signal range set interactivly
>>> s = hs.signals.Signal1D([0, 1, 2, 4, 8, 4, 2, 1, 0]) >>> m = s.create_model() >>> g1 = hs.model.components1D.Gaussian() >>> m.append(g1) >>> m.fit_component(g1)
Signal range set through direct input
>>> m.fit_component(g1, signal_range=(1, 7))
- plot(plot_components=False, plot_residual=False, **kwargs)#
Plot the current spectrum to the screen and a map with a cursor to explore the SI.
- remove(things)#
Remove component from model.
Examples
>>> s = hs.signals.Signal1D(np.empty(1)) >>> m = s.create_model() >>> g1 = hs.model.components1D.Gaussian() >>> g2 = hs.model.components1D.Gaussian() >>> m.extend([g1, g2])
You could remove
g1
like this>>> m.remove(g1)
Or like this:
>>> m.remove(0)
- remove_signal_range(*args, **kwargs)#
Removes the data in the given range from the data range that will be used by the fitting rountine.
- reset_signal_range()#
Resets the data range
See also