HyperSpy
What this library is for
HyperSpy is a Python library for multidimensional microscopy and spectroscopy
data. It provides Signal1D, Signal2D, lazy signals, axes management,
metadata handling, visualization, decomposition, model fitting, ROIs, and file
I/O through the HyperSpy ecosystem.
When to use this vs. alternatives
- Use HyperSpy when the task needs microscopy/spectroscopy signal objects,
navigation/signal axis semantics, metadata, lazy loading, decomposition,
model fitting, ROIs, or file I/O for microscope data.
- Use eXSpy with HyperSpy for EELS and EDS domain-specific methods in HyperSpy
2.x. EELS/EDS signal subclasses moved to the extension ecosystem; do not
assume old
hs.signals.EELSSpectrum examples work without eXSpy installed.
- Use py4DSTEM for 4D-STEM diffraction datacube workflows such as virtual
diffraction imaging, Bragg disk detection, COM/DPC, strain, and phase
retrieval. Use HyperSpy for broader signal handling and EELS/EDX workflows.
- Use NumPy/scikit-image only for narrow operations after HyperSpy has preserved
axes, metadata, units, and lazy-data behavior.
Canonical workflow
Load data as a HyperSpy signal, inspect axes and metadata, set signal type when
needed, then use signal methods rather than treating the data as a bare array.
import hyperspy.api as hs
s = hs.load("spectrum_image.dm4", lazy=True)
print(s)
print(s.axes_manager)
print(s.metadata)
# Name/calibrate axes before quantitative processing.
energy = s.axes_manager.signal_axes[0]
energy.name = "Energy loss"
energy.units = "eV"
# Convert to the intended signal type when appropriate.
# For EELS/EDS in HyperSpy 2.x, install/import eXSpy so these classes register.
s.set_signal_type("EELS")
s.add_elements(("C", "O"))
s.plot()
# Work lazily on large datasets and compute only when needed.
roi = hs.roi.RectangularROI(left=0, right=50, top=0, bottom=50)
subset = roi(s)
mean_spectrum = subset.mean(axis=s.axes_manager.navigation_axes)
mean_spectrum.plot()
s.save("processed.zspy")
For deeper examples, read:
Key conventions and gotchas
- HyperSpy distinguishes navigation axes from signal axes. Most operations act
on the signal axes and iterate over navigation axes. Always inspect
s.axes_manager before indexing, averaging, decomposing, or fitting.
- The displayed dimension order may differ from raw
s.data.shape; HyperSpy
represents dimensions as (navigation | signal).
- In HyperSpy 2.x, EELS, EDS, and dielectric-function signal classes live in
eXSpy, and holography signal classes live in HoloSpy. Install/import the
extension when those domain-specific methods are needed.
- Use
lazy=True for large microscope datasets. Avoid calling s.data or
.compute() early unless the memory cost is understood.
- Axis calibration and units are analysis state, not decoration. Set
name,
scale, offset, and units before integrating, background fitting,
alignment, or model fitting.
- File readers can load original microscope metadata, but not every format maps
all fields into HyperSpy's metadata model. Inspect both
metadata and
original_metadata before relying on acquisition parameters.
- Binned vs unbinned signal state affects noise models, integration, and EELS/EDS
quantification. Check and set noise properties deliberately.
Anti-patterns
- Do not reduce a multidimensional spectrum image with raw
np.mean(s.data, ...)
unless you have first mapped HyperSpy's navigation and signal axes. Use
HyperSpy signal methods and axis-aware reductions.
- Do not copy pre-HyperSpy-2.0 EELS/EDS examples without checking whether eXSpy
is installed and the relevant signal type is registered.
- Do not load huge DM/EMD/BCF datasets eagerly by default. Use lazy loading and
compute only the reduced result.
- Do not run decomposition or blind source separation without checking
navigation size, noise normalization choices, component count, and whether
preprocessing changed physical interpretability.
- Do not perform EELS/EDS quantification before setting microscope parameters,
energy calibration, elements/lines, and background model assumptions.
Diagnostic checks
Before trusting outputs, the agent should:
- Print
s, s.axes_manager, signal type, navigation shape, signal shape, dtype,
lazy/eager state, and file provenance.
- Verify axis names, units, scale, offset, and signal vs navigation dimensions.
- Inspect
metadata and original_metadata for microscope/acquisition
parameters, especially beam energy, convergence/collection angles, dwell time,
detector settings, and calibration.
- For EELS/EDS, confirm eXSpy is installed, set the correct signal type, add
elements/lines, and record microscope parameters and background model.
- For lazy workflows, estimate memory before
.compute() and save intermediate
outputs in .zspy/.hspy when reproducibility matters.
- For decomposition/model fitting, inspect residuals, component maps/spectra,
explained variance, and physical plausibility rather than accepting defaults.
Pointers to deeper material
1---2name: hyperspy3description: Use when the user is working with multidimensional microscopy or spectroscopy data, HyperSpy Signal objects, navigation vs signal axes, lazy signals, HSPY/ZSpy/DM3/DM4/EMD/TIFF/BCF/SER file loading, dimensionality reduction, model fitting, ROIs, EELS/EDS data through the eXSpy extension, or preserving microscope metadata during analysis. Prefer HyperSpy over raw NumPy/Pandas when signal axes, navigation axes, metadata, lazy loading, and microscopy signal semantics matter.4---56# HyperSpy78## What this library is for910HyperSpy is a Python library for multidimensional microscopy and spectroscopy11data. It provides `Signal1D`, `Signal2D`, lazy signals, axes management,12metadata handling, visualization, decomposition, model fitting, ROIs, and file13I/O through the HyperSpy ecosystem.1415## When to use this vs. alternatives1617- Use HyperSpy when the task needs microscopy/spectroscopy signal objects,18 navigation/signal axis semantics, metadata, lazy loading, decomposition,19 model fitting, ROIs, or file I/O for microscope data.20- Use eXSpy with HyperSpy for EELS and EDS domain-specific methods in HyperSpy21 2.x. EELS/EDS signal subclasses moved to the extension ecosystem; do not22 assume old `hs.signals.EELSSpectrum` examples work without eXSpy installed.23- Use py4DSTEM for 4D-STEM diffraction datacube workflows such as virtual24 diffraction imaging, Bragg disk detection, COM/DPC, strain, and phase25 retrieval. Use HyperSpy for broader signal handling and EELS/EDX workflows.26- Use NumPy/scikit-image only for narrow operations after HyperSpy has preserved27 axes, metadata, units, and lazy-data behavior.2829## Canonical workflow3031Load data as a HyperSpy signal, inspect axes and metadata, set signal type when32needed, then use signal methods rather than treating the data as a bare array.3334```python35import hyperspy.api as hs3637s = hs.load("spectrum_image.dm4", lazy=True)38print(s)39print(s.axes_manager)40print(s.metadata)4142# Name/calibrate axes before quantitative processing.43energy = s.axes_manager.signal_axes[0]44energy.name = "Energy loss"45energy.units = "eV"4647# Convert to the intended signal type when appropriate.48# For EELS/EDS in HyperSpy 2.x, install/import eXSpy so these classes register.49s.set_signal_type("EELS")50s.add_elements(("C", "O"))51s.plot()5253# Work lazily on large datasets and compute only when needed.54roi = hs.roi.RectangularROI(left=0, right=50, top=0, bottom=50)55subset = roi(s)56mean_spectrum = subset.mean(axis=s.axes_manager.navigation_axes)57mean_spectrum.plot()5859s.save("processed.zspy")60```6162For deeper examples, read:6364- User guide: https://hyperspy.org/hyperspy-doc/current/user_guide/index.html65- Axes handling: https://hyperspy.org/hyperspy-doc/current/user_guide/axes.html66- Loading/saving: https://hyperspy.org/hyperspy-doc/current/user_guide/io.html67- Lazy data: https://hyperspy.org/hyperspy-doc/current/user_guide/big_data.html68- eXSpy EELS/EDS: https://hyperspy.org/exspy/6970## Key conventions and gotchas7172- HyperSpy distinguishes navigation axes from signal axes. Most operations act73 on the signal axes and iterate over navigation axes. Always inspect74 `s.axes_manager` before indexing, averaging, decomposing, or fitting.75- The displayed dimension order may differ from raw `s.data.shape`; HyperSpy76 represents dimensions as `(navigation | signal)`.77- In HyperSpy 2.x, EELS, EDS, and dielectric-function signal classes live in78 eXSpy, and holography signal classes live in HoloSpy. Install/import the79 extension when those domain-specific methods are needed.80- Use `lazy=True` for large microscope datasets. Avoid calling `s.data` or81 `.compute()` early unless the memory cost is understood.82- Axis calibration and units are analysis state, not decoration. Set `name`,83 `scale`, `offset`, and `units` before integrating, background fitting,84 alignment, or model fitting.85- File readers can load original microscope metadata, but not every format maps86 all fields into HyperSpy's metadata model. Inspect both `metadata` and87 `original_metadata` before relying on acquisition parameters.88- Binned vs unbinned signal state affects noise models, integration, and EELS/EDS89 quantification. Check and set noise properties deliberately.9091## Anti-patterns9293- Do not reduce a multidimensional spectrum image with raw `np.mean(s.data, ...)`94 unless you have first mapped HyperSpy's navigation and signal axes. Use95 HyperSpy signal methods and axis-aware reductions.96- Do not copy pre-HyperSpy-2.0 EELS/EDS examples without checking whether eXSpy97 is installed and the relevant signal type is registered.98- Do not load huge DM/EMD/BCF datasets eagerly by default. Use lazy loading and99 compute only the reduced result.100- Do not run decomposition or blind source separation without checking101 navigation size, noise normalization choices, component count, and whether102 preprocessing changed physical interpretability.103- Do not perform EELS/EDS quantification before setting microscope parameters,104 energy calibration, elements/lines, and background model assumptions.105106## Diagnostic checks107108Before trusting outputs, the agent should:109110- Print `s`, `s.axes_manager`, signal type, navigation shape, signal shape, dtype,111 lazy/eager state, and file provenance.112- Verify axis names, units, scale, offset, and signal vs navigation dimensions.113- Inspect `metadata` and `original_metadata` for microscope/acquisition114 parameters, especially beam energy, convergence/collection angles, dwell time,115 detector settings, and calibration.116- For EELS/EDS, confirm eXSpy is installed, set the correct signal type, add117 elements/lines, and record microscope parameters and background model.118- For lazy workflows, estimate memory before `.compute()` and save intermediate119 outputs in `.zspy`/`.hspy` when reproducibility matters.120- For decomposition/model fitting, inspect residuals, component maps/spectra,121 explained variance, and physical plausibility rather than accepting defaults.122123## Pointers to deeper material124125- Documentation: https://hyperspy.org/hyperspy-doc/current/126- User guide: https://hyperspy.org/hyperspy-doc/current/user_guide/index.html127- API reference: https://hyperspy.org/hyperspy-doc/current/reference/index.html128- eXSpy documentation: https://hyperspy.org/exspy/129- Source repository: https://github.com/hyperspy/hyperspy130- Paper: de la Peña et al. (2021), "hyperspy/hyperspy: Release v1.6.5".131 https://doi.org/10.5281/zenodo.5608741