hyperspy.axes module
- class hyperspy.axes.AxesManager(axes_list)
Bases:
traits.has_traits.HasTraits
Contains and manages the data axes.
It supports indexing, slicing, subscripting and iteration. As an iterator, iterate over the navigation coordinates returning the current indices. It can only be indexed and sliced to access the DataAxis objects that it contains. Standard indexing and slicing follows the “natural order” as in Signal, i.e. [nX, nY, …,sX, sY,…] where n indicates a navigation axis and s a signal axis. In addition AxesManager support indexing using complex numbers a + bj, where b can be one of 0, 1, 2 and 3 and a a valid index. If b is 3 AxesManager is indexed using the order of the axes in the array. If b is 1(2), indexes only the navigation(signal) axes in the natural order. In addition AxesManager supports subscription using axis name.
- coordinates
Get and set the current coordinates if the navigation dimension is not 0. If the navigation dimension is 0 it raises AttributeError when attempting to set its value.
- Type
- indices
Get and set the current indices if the navigation dimension is not 0. If the navigation dimension is 0 it raises AttributeError when attempting to set its value.
- Type
- signal_axes, navigation_axes
Contain the corresponding DataAxis objects
- Type
Examples
Create a spectrum with random data
>>> s = hs.signals.Signal1D(np.random.random((2,3,4,5))) >>> s.axes_manager <Axes manager, axes: (4, 3, 2|5)> Name | size | index | offset | scale | units ================ | ====== | ====== | ======= | ======= | ====== <undefined> | 4 | 0 | 0 | 1 | <undefined> <undefined> | 3 | 0 | 0 | 1 | <undefined> <undefined> | 2 | 0 | 0 | 1 | <undefined> ---------------- | ------ | ------ | ------- | ------- | ------ <undefined> | 5 | | 0 | 1 | <undefined> >>> s.axes_manager[0] <Unnamed 0th axis, size: 4, index: 0> >>> s.axes_manager[3j] <Unnamed 2nd axis, size: 2, index: 0> >>> s.axes_manager[1j] <Unnamed 0th axis, size: 4, index: 0> >>> s.axes_manager[2j] <Unnamed 3rd axis, size: 5> >>> s.axes_manager[1].name = "y" >>> s.axes_manager["y"] <y axis, size: 3, index: 0> >>> for i in s.axes_manager: ... print(i, s.axes_manager.indices) ... (0, 0, 0) (0, 0, 0) (1, 0, 0) (1, 0, 0) (2, 0, 0) (2, 0, 0) (3, 0, 0) (3, 0, 0) (0, 1, 0) (0, 1, 0) (1, 1, 0) (1, 1, 0) (2, 1, 0) (2, 1, 0) (3, 1, 0) (3, 1, 0) (0, 2, 0) (0, 2, 0) (1, 2, 0) (1, 2, 0) (2, 2, 0) (2, 2, 0) (3, 2, 0) (3, 2, 0) (0, 0, 1) (0, 0, 1) (1, 0, 1) (1, 0, 1) (2, 0, 1) (2, 0, 1) (3, 0, 1) (3, 0, 1) (0, 1, 1) (0, 1, 1) (1, 1, 1) (1, 1, 1) (2, 1, 1) (2, 1, 1) (3, 1, 1) (3, 1, 1) (0, 2, 1) (0, 2, 1) (1, 2, 1) (1, 2, 1) (2, 2, 1) (2, 2, 1) (3, 2, 1) (3, 2, 1)
- _get_data_slice(fill=None)
Return a tuple of slice objects to slice the data.
- _remove_one_axis(axis)
Remove the given Axis.
- Raises
ValueError – If the Axis is not present.
- _set_axis_attribute_values(attr, values)
Set the given attribute of all the axes to the given value(s)
- Parameters
attr (string) – The DataAxis attribute to set.
values (any) – If iterable, it must have the same number of items as axes are in this AxesManager instance. If not iterable, the attribute of all the axes are set to the given value.
- _sort_axes()
Sort _axes to align them.
When the data are aligned with the axes the axes order in self._axes is [nav_n, nav_n-1, …, nav_0, sig_m, sig_m-1 …, sig_0]. This method sort the axes in this way. Warning: this doesn’t sort the data axes.
- property axes_are_aligned_with_data
Verify if the data axes are aligned with the signal axes.
When the data are aligned with the axes the axes order in self._axes is [nav_n, nav_n-1, …, nav_0, sig_m, sig_m-1 …, sig_0].
- Returns
aligned
- Return type
- convert_units(axes=None, units=None, same_units=True, factor=0.25)
Convert the scale and the units of the selected axes. If the unit of measure is not supported by the pint library, the scale and units are not changed.
- Parameters
axes ({int | string | iterable of DataAxis | None}) – Default = None Convert to a convenient scale and units on the specified axis. If int, the axis can be specified using the index of the axis in axes_manager. If string, argument can be navigation or signal to select the navigation or signal axes. The axis name can also be provided. If None, convert all axes.
units ({list of string of the same length than axes | str | None}) – Default = None If list, the selected axes will be converted to the provided units. If str, the navigation or signal axes will be converted to the provided units. If None, the scale and the units are converted to the appropriate scale and units to avoid displaying scalebar with >3 digits or too small number. This can be tweaked by the factor argument.
same_units (bool) – If True, force to keep the same units if the units of the axes differs. It only applies for the same kind of axis, navigation or signal. By default the converted units of the first axis is used for all axes. If False, convert all axes individually.
factor (float (default: 0.25)) – ‘factor’ is an adjustable value used to determine the prefix of the units. The product factor * scale * size is passed to the pint to_compact method to determine the prefix.
- create_axes(axes_list)
Given a list of dictionaries defining the axes properties create the DataAxis instances and add them to the AxesManager.
The index of the axis in the array and in the _axes lists can be defined by the index_in_array keyword if given for all axes. Otherwise it is defined by their index in the list.
See also
_append_axis
- 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.
Navigation sliders to control the index of the navigation axes.
- Parameters
title (str) –
%s –
%s –
Set hotkeys for controlling the indices of the navigator plot
- remove(axes)
Remove one or more axes
- set_signal_dimension(value)
Set the dimension of the signal.
- Raises
ValueError – If value if greater than the number of axes or is negative.
- update_axes_attributes_from(axes, attributes=['scale', 'offset', 'units'])
Update the axes attributes to match those given.
The axes are matched by their index in the array. The purpose of this method is to update multiple axes triggering any_axis_changed only once.
- Parameters
axes (iterable of DataAxis instances.) – The axes to copy the attributes from.
attributes (iterable of strings.) – The attributes to copy.
- class hyperspy.axes.DataAxis(size, index_in_array=None, name=<undefined>, scale=1.0, offset=0.0, units=<undefined>, navigate=<undefined>)
Bases:
traits.has_traits.HasTraits
,hyperspy.axes.UnitConversion
- _get_array_slices(slice_)
Returns a slice to slice the corresponding data axis without changing the offset and scale of the DataAxis.
- _slice_me(slice_)
Returns a slice to slice the corresponding data axis and change the offset and scale of the DataAxis accordingly.
- 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.
- update_from(axis, attributes=['scale', 'offset', 'units'])
Copy values of specified axes fields from the passed AxesManager.
- Parameters
axis (DataAxis) – The DataAxis instance to use as a source for values.
attributes (iterable container of strings.) – The name of the attribute to update. If the attribute does not exist in either of the AxesManagers, an AttributeError will be raised.
- Returns
- Return type
A boolean indicating whether any changes were made.
- value2index(value, rounding=<built-in function round>)
Return the closest index to the given value if between the limit.
- Parameters
value (number or numpy array) –
- Returns
index
- Return type
integer or numpy array
- Raises
ValueError – If any value is out of the axis limits.
- value_range_to_indices(v1, v2)
Convert the given range to index range.
When a value is out of the axis limits, the endpoint is used instead. v1 must be preceding v2 in the axis values
if the axis scale is positive, it means v1 < v2
if the axis scale is negative, it means v1 > v2
- hyperspy.axes.generate_axis(offset, scale, size, offset_index=0)
Creates an axis given the offset, scale and number of channels
Alternatively, the offset_index of the offset channel can be specified.
- class hyperspy.axes.ndindex_nat(*shape)
Bases:
numpy.ndindex
- hyperspy.axes.serpentine_iter(shape)
Similar to np.ndindex, but yields indices in serpentine pattern, like snake game
Code by Stackoverflow user Paul Panzer, from https://stackoverflow.com/questions/57366966/