HyperSpy
HyperSpy is an open-source Python library, currently at version 2.4.0, providing a comprehensive framework for the interactive analysis of multidimensional datasets, particularly within scientific domains like electron microscopy. It offers tools for efficient exploration, manipulation, and visualization of complex data arrays, including those larger than available memory. The project maintains an active development pace with several minor releases annually, and significant architectural changes in major versions like 2.0.
Common errors
-
ModuleNotFoundError: No module named 'hyperspy.eds'
cause Specific signal classes and functionalities for domains like EDS (Energy-Dispersive X-ray Spectrometry) were moved out of the core `hyperspy` library in version 2.0.fixInstall the relevant HyperSpy extension package, e.g., `pip install hyperspy-eds`. Check the HyperSpy documentation for a list of available extensions. -
AttributeError: 'BaseSignal' object has no attribute 'gui'
cause The GUI components for HyperSpy were split into separate packages (`hyperspy-gui-ipywidgets`, `hyperspy-gui-traitsui`) since version 1.3 and are no longer installed by default.fixInstall the desired GUI package, e.g., `pip install hyperspy-gui-ipywidgets`, then restart your Python environment. Ensure you have the necessary GUI backend installed (e.g., `ipywidgets` for Jupyter Notebooks). -
TypeError: 'AxesManager' object is not subscriptable
cause Direct indexing of Signal objects (e.g., `s[0, 0]`) was deprecated in older versions in favor of `s.isig` and `s.inav` for explicit signal and navigation axis indexing. While some direct indexing might still work for specific cases, misusing it can lead to this error.fixUse the explicit indexing methods: `s.isig[...]` for signal dimensions and `s.inav[...]` for navigation dimensions to avoid ambiguity and ensure correct slicing and access. -
MemoryError: Unable to allocate array with shape (..., ...) and data type float64
cause This often occurs when working with very large datasets, especially when performing memory-intensive operations like calculating errors during model fitting, which by default requires large memory.fixFor fitting, pass `calculate_errors=False` to `multifit()` if errors are not immediately needed, or consider using a nonlinear optimizer in a second pass once parameters are optimized. When possible, use lazy loading or Dask arrays for out-of-core processing of large datasets, which HyperSpy supports implicitly through its `BaseSignal` abstraction. Consider increasing available RAM or processing data in smaller chunks.
Warnings
- breaking Breaking changes in HyperSpy 2.0+: The library was split into a core package and `RosettaSciIO` for I/O operations. Many domain-specific signal classes (e.g., EELS, EDS, Holography) were moved out of the core library into specialized HyperSpy extensions. Direct imports or functionality relying on these classes within the main `hyperspy` package will fail.
- breaking Python 2 support was dropped with HyperSpy 0.8.4. All versions since require Python 3.
- deprecated GUI elements based on `traitsui` and `ipywidgets` are no longer included in the core HyperSpy package by default since version 1.3. These functionalities are now provided by separate packages, `hyperspy-gui-ipywidgets` and `hyperspy-gui-traitsui`.
- deprecated The direct `hspy` API (e.g., `import hspy`) was deprecated in favor of `hyperspy.api` in version 0.8.4. While older versions might still tolerate it, it's considered an outdated pattern.
- gotcha HyperSpy distinguishes between 'navigation' and 'signal' dimensions for multidimensional arrays. Most functions operate on signal axes and iterate on navigation axes. This is a core concept that can be confusing if not understood.
Install
-
pip install hyperspy -
conda install hyperspy -c conda-forge
Imports
- hs
import hyperspy.api as hs
- Signal1D, Signal2D, BaseSignal
from hyperspy.signals import Signal1D
Quickstart
import hyperspy.api as hs import numpy as np # Load example data (e.g., an EDS spectrum image) s = hs.datasets.example_signals.EDS_TEM_Spectrum() # Or create a signal from a numpy array # data = np.random.rand(10, 10, 1024) # e.g., 10x10 image of 1024-channel spectra # s_from_array = hs.signals.Signal1D(data) # s_from_array.set_axes(axis=0, name='X', units='nm') # s_from_array.set_axes(axis=1, name='Y', units='nm') # s_from_array.set_signal_axis(0, name='Energy', units='eV') print(s) s.plot()