hyperspy.component module

class hyperspy.component.Component(parameter_name_list, linear_parameter_list=None)

Bases: HasTraits

_compute_constant_term()

Gets the value of any (non-free) constant term, with convolution

property _constant_term

Get value of any (non-free) constant term of the component. Returns 0 for most components.

_load_dictionary(dic)

Load data from dictionary.

Parameters:

dict (dict) –

A dictionary containing at least the following fields:

  • _id_name: _id_name of the original parameter, used to create the dictionary. Has to match with the self._id_name

  • parameters: a list of dictionaries, one per parameter of the component (see as_dictionary() documentation for more details)

  • _whitelist: a dictionary, which keys are used as keywords to match with the parameter attributes. For more information see load_from_dictionary()

  • any field from _whitelist.keys()

Returns:

twin_dict – Dictionary of ‘id’ values from input dictionary as keys with all of the parameters of the component, to be later used for setting up correct twins.

Return type:

dict

property active_is_multidimensional

In multidimensional signals it is possible to store the value of the active attribute at each navigation index.

as_dictionary(fullcopy=True)

Returns component as a dictionary. For more information on method and conventions, see export_to_dictionary()

Parameters:

fullcopy (Bool (optional, False)) – Copies of objects are stored, not references. If any found, functions will be pickled and signals converted to dictionaries

Returns:

dic – A dictionary, containing at least the following fields:

  • parameters: a list of dictionaries of the parameters, one per component.

  • _whitelist: a dictionary with keys used as references saved attributes, for more information, see export_to_dictionary()

  • any field from _whitelist.keys()

Return type:

dict

export(folder=None, format='hspy', save_std=False, only_free=True)

Plot the value of the parameters of the model

Parameters:
  • folder (str or None) – The path to the folder where the file will be saved. If None the current folder is used by default.

  • format (str) – The extension of the file format, default “hspy”.

  • save_std (bool) – If True, also the standard deviation will be saved.

  • only_free (bool) – If True, only the value of the parameters that are free will be exported.

Notes

The name of the files will be determined by each the Component and each Parameter name attributes. Therefore, it is possible to customise the file names modify the name attributes.

fetch_values_from_array(p, p_std=None, onlyfree=False)

Fetch the parameter values from an array p and optionally standard deviation from p_std. Places them component.parameter.value and …std, according to their position in the component.

Parameters:
  • p (array) – array containing new values for the parameters in a component

  • p_std (array, optional) – array containing the corresponding standard deviation.

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.

init_parameters(parameter_name_list, linear_parameter_list=None)

Initialise the parameters of the component.

Parameters:
  • parameter_name_list (list) – The list of parameter names.

  • linear_parameter_list (list, optional) – The list of linear parameter. The default is None.

Return type:

None.

plot(only_free=True)

Plot the value of the parameters of the model

Parameters:

only_free (bool) –

If True, only the value of the parameters that are free will

be plotted

print_current_values(only_free=False, fancy=True)

Prints the current values of the component’s parameters.

Parameters:
  • only_free (bool) – If True, only free parameters will be printed.

  • fancy (bool) – If True, attempts to print using html rather than text in the notebook.

set_parameters_free(parameter_name_list=None, only_linear=False, only_nonlinear=False)

Sets parameters in a component to free.

Parameters:
  • parameter_name_list (None or list of str, optional) – If None, will set all the parameters to free. If list of strings, will set all the parameters with the same name as the strings in parameter_name_list to free.

  • only_linear (bool) – If True, only sets a parameter free if it is linear

  • only_nonlinear (bool) – If True, only sets a parameter free if it is nonlinear

Examples

>>> v1 = hs.model.components1D.Voigt()
>>> v1.set_parameters_free()
>>> v1.set_parameters_free(parameter_name_list=['area','centre'])
>>> v1.set_parameters_free(linear=True)
set_parameters_not_free(parameter_name_list=None, only_linear=False, only_nonlinear=False)

Sets parameters in a component to not free.

Parameters:
  • parameter_name_list (None or list of str, optional) – If None, will set all the parameters to not free. If list of strings, will set all the parameters with the same name as the strings in parameter_name_list to not free.

  • only_linear (bool) – If True, only sets a parameter not free if it is linear

  • only_nonlinear (bool) – If True, only sets a parameter not free if it is nonlinear

Examples

>>> v1 = hs.model.components1D.Voigt()
>>> v1.set_parameters_not_free()
>>> v1.set_parameters_not_free(parameter_name_list=['area','centre'])
>>> v1.set_parameters_not_free(only_linear=True)
class hyperspy.component.NoneFloat(default_value=<traits.trait_type._NoDefaultSpecifiedType object>, **metadata)

Bases: CFloat

TraitType initializer

This is the only method normally called directly by client code. It defines the trait. The default implementation accepts an optional, unvalidated default value, and caller-supplied trait metadata.

Override this method whenever a different method signature or a validated default value is needed.

default_value = None

The default value for the trait:

validate(object, name, value)

Validates that a specified value is valid for this trait.

Note: The ‘fast validator’ version performs this check in C.

class hyperspy.component.Parameter

Bases: HasTraits

Model parameter

value

The value of the parameter for the current location. The value for other locations is stored in map.

Type:

float or array

bmin, bmax

Lower and upper bounds of the parameter value.

Type:

float

twin

If it is not None, the value of the current parameter is a function of the given Parameter. The function is by default the identity function, but it can be defined by twin_function

Type:

{None, Parameter}

twin_function_expr

Expression of the twin_function that enables setting a functional relationship between the parameter and its twin. If twin is not None, the parameter value is calculated as the output of calling the twin function with the value of the twin parameter. The string is parsed using sympy, so permitted values are any valid sympy expressions of one variable. If the function is invertible the twin inverse function is set automatically.

Type:

str

twin_inverse_function_expr

Expression of the twin_inverse_function that enables setting the value of the twin parameter. If twin is not None, its value is set to the output of calling the twin inverse function with the value provided. The string is parsed using sympy, so permitted values are any valid sympy expressions of one variable.

Type:

str

twin_function

Setting this attribute manually is deprecated in HyperSpy newer than 1.1.2. It will become private in HyperSpy 2.0. Please use ``twin_function_expr`` instead.

Type:

function

twin_inverse_function

Setting this attribute manually is deprecated in HyperSpy newer than 1.1.2. It will become private in HyperSpy 2.0. Please use ``twin_inverse_function_expr`` instead.

Type:

function

ext_force_positive

If True, the parameter value is set to be the absolute value of the input value i.e. if we set Parameter.value = -3, the value stored is 3 instead. This is useful to bound a value to be positive in an optimization without actually using an optimizer that supports bounding.

Type:

bool

ext_bounded

Similar to ext_force_positive, but in this case the bounds are defined by bmin and bmax. It is a better idea to use an optimizer that supports bounding though.

Type:

bool

connect, disconnect(function)

Call the functions connected when the value attribute changes.

_create_array()

Create the map array to store the information in multidimensional datasets.

_load_dictionary(dictionary)

Load data from dictionary.

Parameters:

dict (dict) –

A dictionary containing at least the following fields:

  • _id_name: _id_name of the original parameter, used to create the dictionary. Has to match with the self._id_name.

  • _whitelist: a dictionary, which keys are used as keywords to match with the parameter attributes. For more information see load_from_dictionary()

  • any field from _whitelist.keys().

Returns:

id_value – the ID value of the original parameter, to be later used for setting up the correct twins

Return type:

int

as_dictionary(fullcopy=True)

Returns parameter as a dictionary, saving all attributes from self._whitelist.keys() For more information see py:meth:~hyperspy.misc.export_dictionary.export_to_dictionary

Parameters:

fullcopy (Bool (optional, False)) – Copies of objects are stored, not references. If any found, functions will be pickled and signals converted to dictionaries

Returns:

  • _id_name: _id_name of the original parameter, used to create the dictionary. Has to match with the self._id_name

  • _twins: a list of ids of the twins of the parameter

  • _whitelist: a dictionary, which keys are used as keywords to match with the parameter attributes. For more information see export_to_dictionary()

  • any field from _whitelist.keys()

Return type:

A dictionary, containing at least the following fields

as_signal(field='values')

Get a parameter map as a signal object.

Please note that this method only works when the navigation dimension is greater than 0.

Parameters:

field ({'values', 'std', 'is_set'}) – Field to return as signal.

Raises:

NavigationDimensionError – If the navigation dimension is 0

assign_current_value_to_all(mask=None)

Assign the current value attribute to all the indices, setting parameter.map for all parameters in the component.

Takes the current parameter.value and sets it for all indices in parameter.map[‘values’].

Parameters:

mask ({None, boolean numpy array}) – Set only the indices that are not masked i.e. where mask is False.

default_traits_view()

Returns the name of the default traits view for the object’s class.

export(folder=None, name=None, format='hspy', save_std=False)

Save the data to a file. All the arguments are optional.

Parameters:
  • folder (str or None) – The path to the folder where the file will be saved. If None the current folder is used by default.

  • name (str or None) – The name of the file. If None the Components name followed by the Parameter name attributes will be used by default. If a file with the same name exists the name will be modified by appending a number to the file path.

  • save_std (bool) – If True, also the standard deviation will be saved

  • format (str) – The extension of any file format supported by HyperSpy, default hspy.

fetch()

Fetch the stored value and std attributes from the parameter.map[‘values’] and …[‘std’] if parameter.map[‘is_set’] is True for that index. Updates parameter.value and parameter.std. If not stored, then .value and .std will remain from their previous values, i.e. from a fit in a previous pixel.

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.

plot(**kwargs)

Plot parameter signal.

Parameters:

**kwargs – Any extra keyword arguments are passed to the signal plot.

Example

>>> parameter.plot() 

Set the minimum and maximum displayed values

>>> parameter.plot(vmin=0, vmax=1) 
store_current_value_in_array()

Store the value and std attributes.

hyperspy.component._get_scaling_factor(signal, axis, parameter)

Convenience function to get the scaling factor required to take into account binned and/or non-uniform axes.

Parameters:
  • signal (BaseSignal) –

  • axis (BaseDataAxis) –

  • parameter (float or numpy array) – The axis value at which scaling factor is evaluated (ignored if the axis is uniform)

Return type:

scaling_factor

hyperspy.component.convolve_component_values(component_values, model)

Convolve component with model convolution axis.

Multiply by np.ones in order to handle case where component_values is a single constant