hyperspy.io_plugins package

Submodules

hyperspy.io_plugins.digital_micrograph module

class hyperspy.io_plugins.digital_micrograph.DigitalMicrographReader(f, verbose=False)

Bases: object

Class to read Gatan Digital Micrograph (TM) files.

Currently it supports versions 3 and 4.

dm_version, endian, tags_dict
parse_file, parse_header, get_image_dictionaries
check_data_tag_delimiter()
find_next_data_tag()
find_next_tag()
get_data_reader(enc_dtype)
get_image_dictionaries()

Returns the image dictionaries of all images in the file except the thumbnails.

Returns:
Return type:dict, None
parse_array_definition()

Reads and returns the element type and length of the array.

The position in the file must be just after the array encoded dtype.

parse_file()
parse_header()
parse_string_definition()

Reads and returns the length of the string.

The position in the file must be just after the string encoded dtype.

parse_struct_definition()

Reads and returns the struct definition tuple.

The position in the file must be just after the struct encoded dtype.

parse_tag_group(skip4=1)

Parse the root TagGroup of the given DM3 file f. Returns the tuple (is_sorted, is_open, n_tags). endian can be either ‘big’ or ‘little’.

parse_tag_header()
parse_tags(ntags, group_name='root', group_dict={})

Parse the DM file into a dictionary.

read_array(size, enc_eltype, extra=None, skip=False)

Read an array, defined by iarray, from file f with a given endianness (byte order). endian can be either ‘big’ or ‘little’.

read_simple_data(etype)

Parse the data of the given DM3 file f with the given endianness (byte order). The infoArray iarray specifies how to read the data. Returns the tuple (file address, data). The tag data is stored in the platform’s byte order: ‘little’ endian for Intel, PC; ‘big’ endian for Mac, Motorola. If skip != 0 the data is actually skipped.

read_string(length, skip=False)
Read a string defined by the infoArray iarray from
file f with a given endianness (byte order).

endian can be either ‘big’ or ‘little’.

If it’s a tag name, each char is 1-Byte; if it’s a tag data, each char is 2-Bytes Unicode,

read_struct(definition, skip=False)

Read a struct, defined by iarray, from file f with a given endianness (byte order). Returns a list of 2-tuples in the form (fieldAddress, fieldValue). endian can be either ‘big’ or ‘little’.

simple_type = (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
skipif4(n=1)
class hyperspy.io_plugins.digital_micrograph.ImageObject(imdict, file, order='C', record_by=None)

Bases: object

dtype
get_axes_dict()
get_data()
get_mapping()
get_metadata(metadata={})
intensity_calibration
names
offsets
order
record_by
scales
shape
signal_type
size
title
to_spectrum
units
unpack_new_packed_complex(data)
unpack_packed_complex(tmpdata)
hyperspy.io_plugins.digital_micrograph.file_reader(filename, record_by=None, order=None, verbose=False)

Reads a DM3 file and loads the data into the appropriate class. data_id can be specified to load a given image within a DM3 file that contains more than one dataset.

Parameters:
  • record_by (Str) – One of: SI, Image
  • order (Str) – One of ‘C’ or ‘F’

hyperspy.io_plugins.fei module

hyperspy.io_plugins.fei.convert_xml_to_dict(xml_object)
hyperspy.io_plugins.fei.dimension_array_dtype(n, DescriptionLength, UnitsLength)
hyperspy.io_plugins.fei.emi_reader(filename, dump_xml=False, verbose=False, **kwds)
hyperspy.io_plugins.fei.emixml2dtb(et, dictree)
hyperspy.io_plugins.fei.file_reader(filename, *args, **kwds)
hyperspy.io_plugins.fei.get_axes_from_position(header, data)
hyperspy.io_plugins.fei.get_calibration_from_position(position)

Compute the size, scale and offset of a linear axis from coordinates.

This function assumes rastering on a regular grid for the full size of each dimension before rastering over another one. Fox example: a11, a12, a13, a21, a22, a23 for a 2x3 grid.

Parameters:position (numpy array.) – Position coordinates of the axis. Normally as in PositionX/Y of the ser file.
Returns:axis_attr
Return type:dictionary with size, scale, offeset keys.
hyperspy.io_plugins.fei.get_data_dtype_list(file, offset, record_by)
hyperspy.io_plugins.fei.get_data_tag_dtype_list(data_type_id)
hyperspy.io_plugins.fei.get_degree(value)
hyperspy.io_plugins.fei.get_header_dtype_list(file)
hyperspy.io_plugins.fei.get_lengths(file)
hyperspy.io_plugins.fei.get_simplified_mode(mode)
hyperspy.io_plugins.fei.get_xml_info_from_emi(emi_file)
hyperspy.io_plugins.fei.guess_record_by(record_by_id)
hyperspy.io_plugins.fei.guess_units_from_mode(objects_dict, header, verbose=True)
hyperspy.io_plugins.fei.load_ser_file(filename, verbose=False)
hyperspy.io_plugins.fei.parse_ExperimentalDescription(et, dictree)
hyperspy.io_plugins.fei.parse_TrueImageHeaderInfo(et, dictree)
hyperspy.io_plugins.fei.print_struct_array_values(struct_array)
hyperspy.io_plugins.fei.readLELong(file)

Read 4 bytes as little endian integer in file

hyperspy.io_plugins.fei.readLELongLong(file)

Read 8 bytes as little endian integer in file

hyperspy.io_plugins.fei.readLEShort(file)

Read 2 bytes as little endian integer in file

hyperspy.io_plugins.fei.ser_reader(filename, objects=None, verbose=False, *args, **kwds)

Reads the information from the file and returns it in the HyperSpy required format.

hyperspy.io_plugins.hdf5 module

hyperspy.io_plugins.hdf5.dict2hdfgroup(dictionary, group, **kwds)
hyperspy.io_plugins.hdf5.file_reader(filename, record_by, mode='r', driver='core', backing_store=False, **kwds)
hyperspy.io_plugins.hdf5.file_writer(filename, signal, *args, **kwds)
hyperspy.io_plugins.hdf5.get_hspy_format_version(f)
hyperspy.io_plugins.hdf5.hdfgroup2dict(group, dictionary=None)
hyperspy.io_plugins.hdf5.hdfgroup2signaldict(group)
hyperspy.io_plugins.hdf5.write_signal(signal, group, **kwds)

hyperspy.io_plugins.image module

hyperspy.io_plugins.image.file_reader(filename, **kwds)

Read data from any format supported by PIL.

Parameters:filename (str) –
hyperspy.io_plugins.image.file_writer(filename, signal, file_format='png', **kwds)

Writes data to any format supported by PIL

Parameters:
  • filename (str) –
  • signal (a Signal instance) –
  • file_format (str) – The fileformat defined by its extension that is any one supported by PIL.

hyperspy.io_plugins.mrc module

hyperspy.io_plugins.mrc.file_reader(filename, endianess='<', **kwds)
hyperspy.io_plugins.mrc.get_data_type(index, endianess='<')
hyperspy.io_plugins.mrc.get_fei_dtype_list(endianess='<')
hyperspy.io_plugins.mrc.get_std_dtype_list(endianess='<')

hyperspy.io_plugins.msa module

hyperspy.io_plugins.msa.file_reader(filename, encoding='latin-1', **kwds)
hyperspy.io_plugins.msa.file_writer(filename, signal, format=None, separator=', ', encoding='latin-1')

hyperspy.io_plugins.netcdf module

hyperspy.io_plugins.netcdf.file_reader(filename, *args, **kwds)
hyperspy.io_plugins.netcdf.nc_hyperspy_reader_0dot1(ncfile, filename, *args, **kwds)

hyperspy.io_plugins.ripple module

hyperspy.io_plugins.ripple.correct_INCA_format(fp)
hyperspy.io_plugins.ripple.file_reader(filename, rpl_info=None, encoding='latin-1', mmap_mode='c', *args, **kwds)

Parses a Lispix (http://www.nist.gov/lispix/) ripple (.rpl) file and reads the data from the corresponding raw (.raw) file; or, read a raw file if the dictionary rpl_info is provided.

This format is often uses in EDS/EDX experiments.

Images and spectral images or data cubes that are written in the (Lispix) raw file format are just a continuous string of numbers.

Data cubes can be stored image by image, or spectrum by spectrum. Single images are stored row by row, vector cubes are stored row by row (each row spectrum by spectrum), image cubes are stored image by image.

All of the numbers are in the same format, such as 16 bit signed integer, IEEE 8-byte real, 8-bit unsigned byte, etc.

The “raw” file should be accompanied by text file with the same name and ”.rpl” extension. This file lists the characteristics of the raw file so that it can be loaded without human intervention.

Alternatively, dictionary ‘rpl_info’ containing the information can be given.

Some keys are specific to HyperSpy and will be ignored by other software.

RPL stands for “Raw Parameter List”, an ASCII text, tab delimited file in which HyperSpy reads the image parameters for a raw file.

TABLE OF RPL PARAMETERS

key type description

———- ———— ——————– # Mandatory keys: width int # pixels per row height int # number of rows depth int # number of images or spectral pts offset int # bytes to skip data-type str # ‘signed’, ‘unsigned’, or ‘float’ data-length str # bytes per pixel ‘1’, ‘2’, ‘4’, or ‘8’ byte-order str # ‘big-endian’, ‘little-endian’, or ‘dont-care’ record-by str # ‘image’, ‘vector’, or ‘dont-care’ # X-ray keys: ev-per-chan int # optional, eV per channel detector-peak-width-ev int # optional, FWHM for the Mn K-alpha line # HyperSpy-specific keys depth-origin int # energy offset in pixels depth-scale float # energy scaling (units per pixel) depth-units str # energy units, usually eV depth-name str # Name of the magnitude stored as depth width-origin int # column offset in pixels width-scale float # column scaling (units per pixel) width-units str # column units, usually nm width-name str # Name of the magnitude stored as width height-origin int # row offset in pixels height-scale float # row scaling (units per pixel) height-units str # row units, usually nm height-name str # Name of the magnitude stored as height signal str # Name of the signal stored, e.g. HAADF convergence-angle float # TEM convergence angle in mrad collection-angle float # EELS spectrometer collection semi-angle in mrad beam-energy float # TEM beam energy in keV elevation-angle float # Elevation angle of the EDS detector azimuth-angle float # Elevation angle of the EDS detector live-time float # Live time per spectrum energy-resolution float # Resolution of the EDS (FHWM of MnKa) tilt-stage float # The tilt of the stage

NOTES

When ‘data-length’ is 1, the ‘byte order’ is not relevant as there is only one byte per datum, and ‘byte-order’ should be ‘dont-care’.

When ‘depth’ is 1, the file has one image, ‘record-by’ is not relevant and should be ‘dont-care’. For spectral images, ‘record-by’ is ‘vector’. For stacks of images, ‘record-by’ is ‘image’.

Floating point numbers can be IEEE 4-byte, or IEEE 8-byte. Therefore if data-type is float, data-length MUST be 4 or 8.

The rpl file is read in a case-insensitive manner. However, when providing a dictionary as input, the keys MUST be lowercase.

Comment lines, beginning with a semi-colon ‘;’ are allowed anywhere.

The first non-comment in the rpl file line MUST have two column names: ‘name_1’<TAB>’name_2’; any name would do e.g. ‘key’<TAB>’value’.

Parameters can be in ANY order.

In the rpl file, the parameter name is followed by ONE tab (spaces are ignored) e.g.: ‘data-length’<TAB>‘2’

In the rpl file, other data and more tabs can follow the two items on each row, and are ignored.

Other keys and values can be included and are ignored.

Any number of spaces can go along with each tab.

hyperspy.io_plugins.ripple.file_writer(filename, signal, encoding='latin-1', *args, **kwds)
hyperspy.io_plugins.ripple.parse_ripple(fp)

Parse information from ripple (.rpl) file. Accepts file object ‘fp. Returns dictionary rpl_info.

hyperspy.io_plugins.ripple.read_raw(rpl_info, fp, mmap_mode='c')

Read the raw file object ‘fp’ based on the information given in the ‘rpl_info’ dictionary.

Parameters:
  • rpl_info (dict) – A dictionary containing the keywords as parsed by read_rpl
  • fp
  • mmap_mode ({None, 'r+', 'r', 'w+', 'c'}, optional) –
  • not None, then memory-map the file, using the given mode (If) –
  • numpy.memmap). The mode has no effect for pickled or ((see) –
  • files. (zipped) –
  • memory-mapped array is stored on disk, and not directly loaded (A) –
  • memory. However, it can be accessed and sliced like any (into) –
  • Memory mapping is especially useful for accessing (ndarray.) –
  • fragments of large files without reading the entire file (small) –
  • memory. (into) –
hyperspy.io_plugins.ripple.write_raw(filename, signal, record_by)

Writes the raw file object

filename : string
the filename, either with the extension or without it
record_by : string
‘vector’ or ‘image’
hyperspy.io_plugins.ripple.write_rpl(filename, keys_dictionary, encoding='ascii')

hyperspy.io_plugins.tiff module

hyperspy.io_plugins.tiff.file_reader(filename, record_by='image', **kwds)

Read data from tif files using Christoph Gohlke’s tifffile library

Parameters:
  • filename (str) –
  • record_by ({'image'}) – Has no effect because this format only supports recording by image.
hyperspy.io_plugins.tiff.file_writer(filename, signal, **kwds)

Writes data to tif using Christoph Gohlke’s tifffile library

Parameters:
  • filename (str) –
  • signal (a Signal instance) –

Module contents