hyperspy.models package

Submodules

hyperspy.models.edsmodel module

class hyperspy.models.edsmodel.EDSModel(spectrum, auto_background=True, auto_add_lines=True, *args, **kwargs)

Bases: hyperspy.models.model1d.Model1D

Build and fit a model of an EDS Signal1D.

Parameters:
  • spectrum (an EDSSpectrum (or any EDSSpectrum subclass) instance.) –
  • auto_add_lines (boolean) – If True, automatically add Gaussians for all X-rays generated in the energy range by an element, using the edsmodel.add_family_lines method.
  • auto_background (boolean) – If True, adds automatically a polynomial order 6 to the model, using the edsmodel.add_polynomial_background method.
  • extra arguments are passed to the Model creator. (Any) –

Example

>>> m = s.create_model()
>>> m.fit()
>>> m.fit_background()
>>> m.calibrate_energy_axis('resolution')
>>> m.calibrate_xray_lines('energy', ['Au_Ma'])
>>> m.calibrate_xray_lines('sub_weight',['Mn_La'], bound=10)
add_family_lines(xray_lines='from_elements')

Create the Xray-lines instances and configure them appropiately

If a X-ray line is given, all the the lines of the familiy is added. For instance if Zn Ka is given, Zn Kb is added too. The main lines (alpha) is added to self.xray_lines

Parameters:xray_lines ({None, 'from_elements', list of string}) – If None, if metadata contains xray_lines list of lines use those. If ‘from_elements’, add all lines from the elements contains in metadata. Alternatively, provide an iterable containing a list of valid X-ray lines symbols. (eg. (‘Al_Ka’,’Zn_Ka’)).
add_polynomial_background(order=6)

Add a polynomial background.

the background is added to self.background_components

Parameters:order (int) – The order of the polynomial
as_dictionary(fullcopy=True)

Returns a dictionary of the model, including all components, degrees of freedom (dof) and chi-squared (chisq) with values.

Parameters:fullcopy (Bool (optional, True)) – Copies of objects are stored, not references. If any found, functions will be pickled and signals converted to dictionaries
Returns:
  • dictionary (a complete dictionary of the model, which includes at)
  • least the following fields
    components : list
    a list of dictionaries of components, one per
    _whitelist : dictionary
    a dictionary with keys used as references for saved attributes, for more information, see hyperspy.misc.export_dictionary.export_to_dictionary()
    • any field from _whitelist.keys() *

Examples

>>> s = signals.Signal1D(np.random.random((10,100)))
>>> m = s.create_model()
>>> l1 = components1d.Lorentzian()
>>> l2 = components1d.Lorentzian()
>>> m.append(l1)
>>> m.append(l2)
>>> d = m.as_dictionary()
>>> m2 = s.create_model(dictionary=d)
calibrate_energy_axis(calibrate='resolution', xray_lines='all_alpha', **kwargs)

Calibrate the resolution, the scale or the offset of the energy axis by fitting.

Parameters:
  • calibrate ('resolution' or 'scale' or 'offset') – If ‘resolution’, fits the width of Gaussians place at all x-ray lines. The width is given by a model of the detector resolution, obtained by extrapolating the energy_resolution_MnKa in metadata metadata. This method will update the value of energy_resolution_MnKa. If ‘scale’, calibrate the scale of the energy axis If ‘offset’, calibrate the offset of the energy axis
  • xray_lines (list of str or 'all_alpha') – The Xray lines. If ‘all_alpha’, fit all using all alpha lines
  • **kwargs (extra key word arguments) – All extra key word arguments are passed to fit or multifit, depending on the value of kind.
calibrate_xray_lines(calibrate='energy', xray_lines='all', bound=1, kind='single', **kwargs)

Calibrate individually the X-ray line parameters.

The X-ray line energy, the weight of the sub-lines and the X-ray line width can be calibrated.

Parameters:
  • calibrate ('energy' or 'sub_weight' or 'width') – If ‘energy’, calibrate the X-ray line energy. If ‘sub_weight’, calibrate the ratio between the main line alpha and the other sub-lines of the family If ‘width’, calibrate the X-ray line width.
  • xray_lines (list of str or 'all') – The Xray lines. If ‘all’, fit all lines
  • bounds (float) – for ‘energy’ and ‘width’ the bound in energy, in eV for ‘sub_weight’ Bound the height of the peak to fraction of its height
  • kind ({'single', 'multi'}) – If ‘single’ fit only the current location. If ‘multi’ use multifit.
  • **kwargs (extra key word arguments) – All extra key word arguments are passed to fit or multifit, depending on the value of kind.
disable_xray_lines()

Disable the X-ray lines components.

enable_xray_lines()

Enable the X-ray lines components.

fit_background(start_energy=None, end_energy=None, windows_sigma=(4.0, 3.0), kind='single', **kwargs)

Fit the background in the energy range containing no X-ray line.

After the fit, the background is fixed.

Parameters:
  • start_energy ({float, None}) – If float, limit the range of energies from the left to the given value.
  • end_energy ({float, None}) – If float, limit the range of energies from the right to the given value.
  • windows_sigma (tuple of two float) – The (lower, upper) bounds around each X-ray line, each as a float, to define the energy range free of X-ray lines.
  • kind ({'single', 'multi'}) – If ‘single’ fit only the current location. If ‘multi’ use multifit.
  • **kwargs (extra key word arguments) – All extra key word arguments are passed to fit or
fix_background()

Fix the background components.

fix_sub_xray_lines_weight(xray_lines='all')

Fix the weight of a sub X-ray lines to the main X-ray lines

Establish the twin on the height of sub-Xray lines (non alpha)

fix_xray_lines_energy(xray_lines='all')

Fix the X-ray line energy (shift or centre of the Gaussian)

Parameters:
  • xray_lines (list of str, 'all', or 'all_alpha') – The Xray lines. If ‘all’, fit all lines. If ‘all_alpha’ fit all using all alpha lines.
  • bound (float) – the bound around the actual energy, in keV or eV
fix_xray_lines_width(xray_lines='all')

Fix the X-ray line width (sigma of the Gaussian)

Parameters:
  • xray_lines (list of str, 'all', or 'all_alpha') – The Xray lines. If ‘all’, fit all lines. If ‘all_alpha’ fit all using all alpha lines.
  • bound (float) – the bound around the actual energy, in keV or eV
free_background()

Free the yscale of the background components.

free_sub_xray_lines_weight(xray_lines='all', bound=0.01)

Free the weight of a sub X-ray lines

Remove the twin on the height of sub-Xray lines (non alpha)

Parameters:
  • xray_lines (list of str or 'all') – The Xray lines. If ‘all’, fit all lines
  • bounds (float) – Bound the height of the peak to a fraction of its height
free_xray_lines_energy(xray_lines='all', bound=0.001)

Free the X-ray line energy (shift or centre of the Gaussian)

Parameters:
  • xray_lines (list of str or 'all') – The Xray lines. If ‘all’, fit all lines
  • bound (float) – the bound around the actual energy, in keV or eV
free_xray_lines_width(xray_lines='all', bound=0.01)

Free the X-ray line width (sigma of the Gaussian)

Parameters:
  • xray_lines (list of str or 'all') – The Xray lines. If ‘all’, fit all lines
  • bound (float) – the bound around the actual energy, in keV or eV
get_lines_intensity(xray_lines=None, plot_result=False, **kwargs)

Return the fitted intensity of the X-ray lines.

Return the area under the gaussian corresping to the X-ray lines

Parameters:
  • xray_lines (list of str or None or 'from_metadata') – If None, all main X-ray lines (alpha) If ‘from_metadata’, take the Xray_lines stored in the metadata of the spectrum. Alternatively, provide an iterable containing a list of valid X-ray lines symbols.
  • plot_result (bool) – If True, plot the calculated line intensities. If the current object is a single spectrum it prints the result instead.
  • kwargs – The extra keyword arguments for plotting. See utils.plot.plot_signals
Returns:

intensities – A list containing the intensities as Signal subclasses.

Return type:

list

Examples

>>> m.multifit()
>>> m.get_lines_intensity(["C_Ka", "Ta_Ma"])
remove(thing)

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)
spectrum
units_factor

hyperspy.models.edssemmodel module

class hyperspy.models.edssemmodel.EDSSEMModel(spectrum, auto_background=True, auto_add_lines=True, *args, **kwargs)

Bases: hyperspy.models.edsmodel.EDSModel

Build and fit a model to EDS data acquired in the SEM.

Parameters:
  • spectrum (an EDSSEMSpectrum instance) –
  • auto_add_lines (boolean) – If True, automatically add Gaussians for all X-rays generated in the energy range by an element, using the edsmodel.add_family_lines method.
  • auto_background (boolean) – If True, adds automatically a polynomial order 6 to the model, using the edsmodel.add_polynomial_background method.
  • extra arguments are passed to the Model constructor. (Any) –

hyperspy.models.edstemmodel module

class hyperspy.models.edstemmodel.EDSTEMModel(spectrum, auto_background=True, auto_add_lines=True, *args, **kwargs)

Bases: hyperspy.models.edsmodel.EDSModel

Build and fit a model to EDS data acquired in the TEM.

Parameters:
  • spectrum (an EDSTEMSpectrum instance) –
  • auto_add_lines (boolean) – If True, automatically add Gaussians for all X-rays generated in the energy range by an element, using the edsmodel.add_family_lines method.
  • auto_background (boolean) – If True, adds automatically a polynomial order 6 to the model, using the edsmodel.add_polynomial_background method.
  • extra arguments are passed to the Model constructor. (Any) –

hyperspy.models.eelsmodel module

class hyperspy.models.eelsmodel.EELSModel(signal1D, auto_background=True, auto_add_edges=True, ll=None, GOS=None, dictionary=None)

Bases: hyperspy.models.model1d.Model1D

Build an EELS model

Parameters:
  • spectrum (a Signal1D (or any Signal1D subclass) instance) –
  • auto_background (boolean) – If True, and if spectrum is an EELS instance adds automatically a powerlaw to the model and estimate the parameters by the two-area method.
  • auto_add_edges (boolean) – If True, and if spectrum is an EELS instance, it will automatically add the ionization edges as defined in the Signal1D instance. Adding a new element to the spectrum using the components.EELSSpectrum.add_elements method automatically add the corresponding ionisation edges to the model.
  • ll ({None, EELSSpectrum}) – If an EELSSPectrum is provided, it will be assumed that it is a low-loss EELS spectrum, and it will be used to simulate the effect of multiple scattering by convolving it with the EELS spectrum.
  • GOS ({'hydrogenic', 'Hartree-Slater', None}) – The GOS to use when auto adding core-loss EELS edges. If None it will use the Hartree-Slater GOS if they are available, otherwise it will use the hydrogenic GOS.
  • dictionary ({dict, None}) – A dictionary to be used to recreate a model. Usually generated using hyperspy.model.as_dictionary()
append(component)

Add component to Model.

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

Disable the background components.

disable_edges(edges_list=None)

Disable the edges listed in edges_list. If edges_list is None (default) all the edges with onset in the spectrum energy region will be disabled.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
disable_fine_structure(edges_list=None)

Disable the fine structure of the edges listed in edges_list. If edges_list is None (default) the fine structure of all the edges with onset in the spectrum energy region will be disabled.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
disable_free_onset_energy(edges_list=None)

Disable the automatic freeing of the onset_energy parameter during a smart fit for the edges listed in edges_list. If edges_list is None (default) the onset_energy of all the edges with onset in the spectrum energy region will not be freed. Note that if their atribute edge.onset_energy.free is True, the parameter will be free during the smart fit.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
enable_background()

Enable the background componets.

enable_edges(edges_list=None)

Enable the edges listed in edges_list. If edges_list is None (default) all the edges with onset in the spectrum energy region will be enabled.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
enable_fine_structure(edges_list=None)

Enable the fine structure of the edges listed in edges_list. If edges_list is None (default) the fine structure of all the edges with onset in the spectrum energy region will be enabled.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
enable_free_onset_energy(edges_list=None)

Enable the automatic freeing of the onset_energy parameter during a smart fit for the edges listed in edges_list. If edges_list is None (default) the onset_energy of all the edges with onset in the spectrum energy region will be freeed.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
fit(fitter=None, method='ls', grad=False, bounded=False, ext_bounding=False, update_plot=False, kind='std', **kwargs)

Fits the model to the experimental data

Parameters:
  • fitter ({None, "leastsq", "odr", "mpfit", "fmin"}) – The optimizer to perform the fitting. If None the fitter defined in the Preferences is used. leastsq is the most stable but it does not support bounding. mpfit supports bounding. fmin is the only one that supports maximum likelihood estimation, but it is less robust than the Levenberg–Marquardt based leastsq and mpfit, and it is better to use it after one of them to refine the estimation.
  • method ({'ls', 'ml'}) – Choose ‘ls’ (default) for least squares and ‘ml’ for maximum-likelihood estimation. The latter only works with fitter = ‘fmin’.
  • grad (bool) – If True, the analytical gradient is used if defined to speed up the estimation.
  • ext_bounding (bool) – If True, enforce bounding by keeping the value of the parameters constant out of the defined bounding area.
  • bounded (bool) – If True performs bounded optimization if the fitter supports it. Currently only mpfit support bounding.
  • update_plot (bool) – If True, the plot is updated during the optimization process. It slows down the optimization but it permits to visualize the optimization evolution.
  • kind ({'std', 'smart'}) – If ‘std’ (default) performs standard fit. If ‘smart’ performs smart_fit
  • **kwargs (key word arguments) – Any extra key word argument will be passed to the chosen fitter

See also

multifit(), smart_fit()

fit_background(start_energy=None, only_current=True, **kwargs)

Fit the background to the first active ionization edge in the energy range.

Parameters:
  • start_energy ({float, None}, optional) – If float, limit the range of energies from the left to the given value. Default None.
  • only_current (bool, optional) – If True, only fit the background at the current coordinates. Default True.
  • **kwargs (extra key word arguments) – All extra key word arguments are passed to fit or multifit.
fix_edges(edges_list=None)

Fixes all the parameters of the edges given in edges_list. If edges_list is None (default) all the edges will be fixed.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
fix_fine_structure(edges_list=None)

Fixes all the parameters of the edges given in edges_list. If edges_list is None (default) all the edges will be fixed.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
free_edges(edges_list=None)

Frees all the parameters of the edges given in edges_list. If edges_list is None (default) all the edges will be freeed.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
free_fine_structure(edges_list=None)

Frees all the parameters of the edges given in edges_list. If edges_list is None (default) all the edges will be freeed.

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
quantify()

Prints the value of the intensity of all the independent active EELS core loss edges defined in the model

remove(component)

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_fine_structure_data(edges_list=None)

Remove the fine structure data from the fitting routine as defined in the fine_structure_width parameter of the component.EELSCLEdge

Parameters:edges_list (None or list of EELSCLEdge or list of edge names) – If None, the operation is performed on all the edges in the model. Otherwise, it will be performed only on the listed components.
resolve_fine_structure(preedge_safe_window_width=2, i1=0)

Adjust the fine structure of all edges to avoid overlapping

This function is called automatically everytime the position of an edge changes

Parameters:preedge_safe_window_width (float) – minimum distance between the fine structure of an ionization edge and that of the following one. Default 2 (eV).
resume_auto_fine_structure_width(update=True)

Enable the automatic adjustament of the core-loss edges fine structure width.

Parameters:update (bool, optional) – If True, also execute the automatic adjustment (default).
set_all_edges_intensities_positive()
signal1D
smart_fit(start_energy=None, **kwargs)

Fits everything in a cascade style.

Parameters:
  • start_energy ({float, None}) – If float, limit the range of energies from the left to the given value.
  • **kwargs (key word arguments) – Any extra key word argument will be passed to the fit method. See the fit method documentation for a list of valid arguments.

See also

fit(), multifit()

suspend_auto_fine_structure_width()

Disable the automatic adjustament of the core-loss edges fine structure width.

two_area_background_estimation(E1=None, E2=None, powerlaw=None)

Estimates the parameters of a power law background with the two area method.

Parameters:
  • E1 (float) –
  • E2 (float) –
  • powerlaw (PowerLaw component or None) – If None, it will try to guess the right component from the background components of the model
unset_all_edges_intensities_positive()

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

hyperspy.models.model2d module

class hyperspy.models.model2d.Model2D(signal2D, dictionary=None)

Bases: hyperspy.model.BaseModel

Model and data fitting for two dimensional signals.

A model is constructed as a linear combination of components2D that are added to the model using append() or extend(). There are many predifined components available in the in the components2D 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.

Note that methods are not yet defined for plotting 2D models or using gradient based optimisation methods - these will be added soon.

signal

Signal2D 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.

fit, multifit

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

See also

Base, Model1D

Example

add_signal_range(*args, **kwargs)
disable_adjust_position()
enable_adjust_position(components=None, fix_them=True, show_label=True)
plot(plot_components=False)
remove_signal_range(*args, **kwargs)
reset_signal_range()
reset_the_signal_range()
set_signal_range(*args, **kwargs)
signal

Module contents