hyperspy.models.model1d module

class hyperspy.models.model1d.ComponentFit(model, component, signal_range=None, estimate_parameters=True, fit_independent=False, only_current=True, **kwargs)

Bases: hyperspy.signal_tools.SpanSelectorInSignal1D

apply()
gui(display=True, toolkit=None, **kwargs)

Display or return interactive GUI element if available.

Parameters:
  • display (bool) – If True, display the user interface widgets. If False, return the widgets container in a dictionary, usually for customisation or testing.
  • toolkit (str, iterable of strings or None) – 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.
class hyperspy.models.model1d.Model1D(signal1D, dictionary=None)

Bases: hyperspy.model.BaseModel

Model and data fitting for one dimensional signals.

A model is constructed as a linear combination of components that are added to the model using append() or extend(). There are many predifined components available in the in the components module. If needed, new components can be created easily using the code of existing components as a template.

Once defined, the model can be fitted to the data using fit() or multifit(). 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.

signal

Signal1D instance – It contains the data to fit.

chisq

A Signal of floats – Chi-squared of the signal (or np.nan if not yet fit)

dof

A Signal of integers – Degrees of freedom of the signal (0 if not yet fit)

red_chisq

Signal instance – Reduced chi-squared.

components

ModelComponents instance – The components of the model are attributes of this class. This provides a convinient way to access the model components when working in IPython as it enables tab completion.

append()

Append one component to the model.

extend()

Append multiple components to the model.

remove()

Remove component from model.

as_signal()

Generate a Signal1D instance (possible multidimensional) from the model.

store_current_values()

Store the value of the parameters at the current position.

fetch_stored_values()

fetch stored values of the parameters.

update_plot()

Force a plot update. (In most cases the plot should update automatically.)

set_signal_range, remove_signal range, reset_signal_range,
add signal_range.

Customize the signal range to fit.

fit, multifit

Fit the model to the data at the current position or the full dataset.

save_parameters2file, load_parameters_from_file

Save/load the parameter values to/from a file.

plot()

Plot the model and the data.

enable_plot_components, disable_plot_components

Plot each component separately. (Use after plot.)

set_current_values_to()

Set the current value of all the parameters of the given component as the value for all the dataset.

export_results()

Save the value of the parameters in separate files.

plot_results()

Plot the value of all parameters at all positions.

print_current_values()

Print the value of the parameters at the current position.

enable_adjust_position, disable_adjust_position

Enable/disable interactive adjustment of the position of the components that have a well defined position. (Use after plot).

fit_component()

Fit just the given component in the given signal range, that can be set interactively.

set_parameters_not_free, set_parameters_free

Fit the free status of several components and parameters at once.

set_parameters_value()

Set the value of a parameter in components in a model to a specified value.

as_dictionary()

Exports the model to a dictionary that can be saved in a file.

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()
Components  Parameter       Value
Gaussian
            sigma   1.000000
            A       1.000000
            centre  0.000000
>>> g.centre.value = 3
>>> m.print_current_values()
Components  Parameter       Value
Gaussian
            sigma   1.000000
            A       1.000000
            centre  3.000000
>>> 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

Parameters:
  • x1 (None or float) –
  • x2 (None or float) –
append(thing)

Add component to Model.

Parameters:thing (Component instance.) –
disable_adjust_position()

Disables the interactive adjust position feature

disable_plot_components()
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 of components}) – 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_them (bool) – 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_label (bool, optional) – If True, a label showing the component name is added to the plot next to the vertical line.
enable_plot_components()
fit_component(component, signal_range='interactive', estimate_parameters=True, fit_independent=False, only_current=True, display=True, toolkit=None, **kwargs)

Fit just 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.

component : component instance
The component must be in the model, otherwise an exception is raised. The component can be specified by name, index or itself.
signal_range : {‘interactive’, (left_value, right_value), None}
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. If None the current signal range is used.
estimate_parameters : bool, 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_independent : bool, default False
If True, all other components are disabled. If False, all other component paramemeters are fixed.

display: bool

If True, display the user interface widgets. If False, return the widgets container in a dictionary, usually for customisation or testing.

toolkit: str, iterable of strings or None

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.

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))
low_loss
plot(plot_components=False)

Plots the current spectrum to the screen and a map with a cursor to explore the SI.

Parameters:plot_components (bool) – If True, add a line per component to the signal figure.
remove(things)

Remove component from model.

Examples

>>> s = hs.signals.Signal1D(np.empty(1))
>>> m = s.create_model()
>>> g = hs.model.components1D.Gaussian()
>>> m.append(g)

You could remove g like this

>>> m.remove(g)

Like this:

>>> m.remove("Gaussian")

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

Parameters:
  • x1 (None or float) –
  • x2 (None or float) –
reset_signal_range()

Resets the data range

reset_the_signal_range()
set_convolution_axis()

Creates an axis to use to generate the data of the model in the precise scale to obtain the correct axis and origin after convolution with the lowloss spectrum.

set_signal_range(*args, **kwargs)

Use only the selected spectral range defined in its own units in the fitting routine.

Parameters:
  • E1 (None or float) –
  • E2 (None or float) –
signal