hyperspy.io_plugins.emd module

class hyperspy.io_plugins.emd.EMD(signals=None, user=None, microscope=None, sample=None, comments=None)

Bases: object

Class for storing electron microscopy datasets.

The EMD class can hold an arbitrary amount of datasets in the signals dictionary. These are saved as HyperSpy Signal instances. Global metadata are saved in four dictionaries (user, microscope, sample, comments). To print relevant information about the EMD instance use the log_info() function. EMD instances can be loaded from and saved to emd-files, an hdf5 standard developed at Lawrence Berkeley National Lab (https://emdatasets.com/).

signals

Dictionary which contains all datasets as Signal instances.

Type

dictionary

user

Dictionary which contains user related metadata.

Type

dictionary

microscope

Dictionary which contains microscope related metadata.

Type

dictionary

sample

Dictionary which contains sample related metadata.

Type

dictionary

comments

Dictionary which contains additional commentary metadata.

Type

dictionary

add_signal(signal, name=None, metadata=None)

Add a HyperSpy signal to the EMD instance and make sure all metadata is present.

Parameters
  • signal (Signal) – HyperSpy signal which should be added to the EMD instance.

  • name (string, optional) – Name of the (used as a key for the signals dictionary). If not specified, signal.metadata.General.title will be used. If this is an empty string, both name and signal title are set to ‘dataset’ per default. If specified, name overwrites the signal title.

  • metadata (dictionary) – Dictionary which holds signal specific metadata which will be added to the signal.

Returns

Return type

None

Notes

This is the preferred way to add signals to the EMD instance. Directly adding to the signals dictionary is possible but does not make sure all metadata are correct. This method is also called in the standard constructor on all entries in the signals dictionary!

classmethod load_from_emd(filename, lazy=False, dataset_name=None)

Construct EMD object from an emd-file.

Parameters
  • filename (str) – The name of the emd-file from which to load the signals. Standard file extesnion is ‘.emd’.

  • False (bool, optional) – If False (default) loads data to memory. If True, enables loading only if requested.

  • dataset_name (str or iterable, optional) – Only add dataset with specific name. Note, this has to be the full group path in the file. For example ‘/experimental/science_data’. If the dataset is not found, an IOError with the possible datasets will be raised. Several names can be specified in the form of a list.

Returns

emd – A EMD object containing the loaded signals.

Return type

EMD

log_info()

( all relevant information about the EMD instance.

save_to_emd(filename='datacollection.emd')

Save EMD data in a file with emd(hdf5)-format.

Parameters

filename (string, optional) – The name of the emd-file in which to store the signals. The default is ‘datacollection.emd’.

Returns

Return type

None

class hyperspy.io_plugins.emd.EMD_NCEM

Bases: object

Class for reading and writing the Berkeley variant of the electron microscopy datasets (EMD) file format. It reads files EMD NCEM, including files generated by the prismatic software.

dictionaries

List of dictionaries which are passed to the file_reader.

Type

list

static _get_emd_group_type(group)

Return the value of the ‘emd_group_type’ attribute if it exist, otherwise returns False

static _parse_axis(axis_data)

Estimate, offset, scale from a 1D array

static _read_dataset(dataset)

Read dataset and use the h5py AsStrWrapper when the dataset is of string type (h5py 3.0 and newer)

_read_emd_version(group)

Return the group version if the group is an EMD group, otherwise return None.

classmethod find_dataset_paths(file, supported_dataset=True)

Find the paths of all groups containing valid EMD data.

Parameters
  • file (hdf5 file handle) –

  • supported_dataset (bool, optional) – If True (default), returns the paths of all supported datasets, otherwise returns the path of the non-supported other dataset. This is relevant for groups containing auxiliary dataset(s) which are not supported by HyperSpy or described in the EMD NCEM dataset specification.

Returns

datasets – List of path to these group.

Return type

list

read_file(file, lazy=None, dataset_path=None, stack_group=None)

Read the data from an emd file

Parameters
  • file (file handle) – Handle of the file to read the data from.

  • lazy (bool, optional) – Load the data lazily. The default is False.

  • dataset_path (None, str or list of str) – Path of the dataset. If None, load all supported datasets, otherwise the specified dataset. The default is None.

  • stack_group (bool, optional) – Stack datasets of groups with common name. Relevant for emd file version >= 0.5 where groups can be named ‘group0000’, ‘group0001’, etc.

write_file(file, signal, **kwargs)

Write signal to file.

Parameters
  • file (str of h5py file handle) – If str, filename of the file to write, otherwise a h5py file handle

  • signal (instance of hyperspy signal) – The signal to save.

  • **kwargs (dict) – Keyword argument are passed to the h5py.Group.create_dataset method.

class hyperspy.io_plugins.emd.FeiEMDReader(filename=None, select_type=None, first_frame=0, last_frame=None, sum_frames=True, sum_EDS_detectors=True, rebin_energy=1, SI_dtype=None, load_SI_image_stack=False, lazy=False)

Bases: object

Class for reading FEI electron microscopy datasets.

The FeiEMDReader reads EMD files saved by the FEI Velox software package.

dictionaries

List of dictionaries which are passed to the file_reader.

Type

list

im_type

String specifying whether the data is an image, spectrum or spectrum image.

Type

string

_read_image(image_group, image_sub_group_key)

Return a dictionary ready to parse of return to io module

class hyperspy.io_plugins.emd.FeiSpectrumStream(stream_group, reader)

Bases: object

Read spectrum image stored in FEI’s stream format

Once initialized, the instance of this class supports numpy style indexing and slicing of the data stored in the stream format.

stream_to_array(stream_data, spectrum_image=None)

Convert stream to array.

Parameters
  • stream_data (array) –

  • spectrum_image (array or None) – If array, the data from the stream are added to the array. Otherwise it creates a new array and returns it.

stream_to_sparse_array(stream_data)

Convert stream in sparse array

Parameters

stream_data (array) –

hyperspy.io_plugins.emd.file_reader(filename, lazy=False, **kwds)

Read EMD file, which can be a NCEM or a Velox variant of the EMD format.

Parameters
  • filename (str) – Filename of the file to write.

  • lazy (bool) – Open the data lazily. Default is False.

  • **kwds (dict) – Keyword argument pass to the EMD NCEM or EMD Velox reader. See user guide or the docstring of the load function for more information.

hyperspy.io_plugins.emd.file_writer(filename, signal, **kwds)

Write signal to EMD NCEM file.

Parameters
  • file (str of h5py file handle) – If str, filename of the file to write, otherwise a h5py file handle

  • signal (instance of hyperspy signal) – The signal to save.

  • **kwargs (dict) – Dictionary containing metadata which will be written as attribute of the root group.

hyperspy.io_plugins.emd.is_EMD_NCEM(file)
Parameters

file (h5py file handle) – DESCRIPTION.

Returns

DESCRIPTION.

Return type

bool

hyperspy.io_plugins.emd.is_EMD_Velox(file)

Function to check if the EMD file is an Velox file.

Parameters

file (string or HDF5 file handle) – The name of the emd-file from which to load the signals. Standard file extension is ‘emd’.

Returns

Return type

True if the file is a Velox file, otherwise False

hyperspy.io_plugins.emd.read_emd_version(group)

Function to read the emd file version from a group. The EMD version is saved in the attributes ‘version_major’ and ‘version_minor’.

Parameters

group (hdf5 group) – The group to extract the version from.

Returns

file version – Empty string if the file version is not defined in this group

Return type

str