ncempy.io package

Submodules

ncempy.io.read module

param filename:The path and name of the file to attempt to load. This chooses the Reader() function based on the file suffix.
type filename:str or pathlib.Path
param dsetNum:The data set number to load if there are multiple data sets in a file. This is implemented for EMD and DM files only.
type dsetNum:int

ncempy.io.emd module

This module provides an interface to the Berkeley EMD file format.

See https://emdatasets.com/ for more details.

  • It fully supports v0.2 for reading and writing

Note

General users:
Use the simplified emd.emdReader() function to load the data and meta data as a python dictionary.
Advanced users and developers:
Access the file internals through the emd.fileDEMD() class.
ncempy.io.emd.defaultDims(data, pixel_size=None)[source]

A helper function that can generate a properly setup dim tuple with default values to allow quick writing of EMD files without the need to create these dim vectors manually.

Parameters:
  • data (ndarray) – The data that will be written to the EMD file. This is used to get the number of dims and their shape
  • pixel_size (tuple, optional) – A tuple of pixel sizes. Must have same length as the number of dimensions of data.
Returns:

dims – A properly formatted tuple of dim vectors used as input to emd.emdFile.put_emdgroup()

Return type:

list

ncempy.io.emd.emdReader(filename, dsetNum=0)[source]

A simple helper function to read in the data and metadata in a structured format similar to the other ncempy readers.

Note

Note fully implemented yet. Work in progress.

Parameters:
  • filename (str or pathlib.Path) – The path to the file as a string.
  • dsetNum (int) – The index of the data set to load.
Returns:

Data and metadata as a dictionary similar to other ncempy readers.

Return type:

dict

Example

Simply load all data and metadata from a data set in an EMD Velox file
>> import ncempy.io as nio >> emd0 = nio.emd.emdReader(‘filename.emd’, dsetNum = 0)
ncempy.io.emd.emdWriter(filename, data, pixel_size=None, overwrite=False)[source]

Simple method to write data to a file formatted as an EMD v0.2. The only possible metadata to write is the pixel size for each dimension. Use the emd.fileEMD() class for more complex operations. The file must not already exist.

Parameters:
  • filename (str or pathlib.Path) – The path and file name to write the data to.
  • data (ndarray) – The data as an ndarray to write to the file.
  • pixel_size (tuple) – A tuple with the same length as the number of dims in data. len(pixel_size) == data.ndim
  • overwrite (boolean) – If file exists, overwrite it.
class ncempy.io.emd.fileEMD(filename, readonly=True)[source]

Class to represent Berkeley EMD files.

Implemented for spec 0.2 using the recommended layout for metadata.

file_name

The name of the file

Type:str
file_path

A pathlib.Path object for the open file

Type:pathlib.Path
file_hdl

The h5py file handle which provides direct access to the underlying h5py file structure.

Type:h5py.File
version

A 2-tuple of the major and minor EMD version as ints.

Type:tuple
data

A link to the data group in the EMD file.

Type:h5py.Group
microscope

A link to the microscope EMD group with metadata.

Type:h5py.Group
sample

A link to the sample EMD group with metadata.

Type:h5py.Group
user

A link to the user EMD group with metadata.

Type:h5py.Group
comments

A link to the comments in this EMD file. A time stamp is included with the comment.

Type:h5py.Group
list_emds

A list of the EMD groups in the file.

Type:list

Examples

Open an Berkeley EMD file using the advanced interface. See the emdReader function for a more convenient way to load the data. We show how to list the available data sets, load a 3D data set and plot the first image. >> from ncempy.io import emd >> with emd.fileEMD(‘filename.emd’) as emd1: >> [print(dataGroup.name) for dataGroup in emd1.list_emds] # print all available EMD datasets >> data1, dims1 = emd1.get_emdgroup(0) # load the first full data array and dimension information

find_emdgroups(parent)[source]

Find all emd_data_type groups within the group parent and return a list of references to their HDF5 groups.

Parameters:parent (h5py.Group) – Handle to the parent group.
Returns:A list of h5py.Group handles to children groups being emd_data_type groups.
Return type:list
get_emddims(group)[source]

Get the emdtype dimensions saved in in group.

Parameters:group (h5py.Group) – Reference to the emdtype HDF5 group.
Returns:List of dimension vectors plus labels and units.
Return type:tuple
get_emdgroup(group)[source]

Get the emd data saved in the requested group.

Note

The memmap keyword has been removed. Please use get_memmap().

Parameters:group (h5py._hl.group.Group or int) – Reference to the HDF5 group to load. If int is used then the item corresponding to self.list_emds is loaded
Returns:
None or tuple containing:
: np.ndarray
The data of the emdtype group.
: list
List of [0] dimension vectors, [1] labels and [2] units.
Return type:tuple or None
get_memmap(group)[source]

Opens a new fileEMD object and returns the requested EMD group. This prevents the file from being closed. The original HDF5 file is left open. The file pointer is lost, but it will be closed once the EMD group is deleted or removed. See also get_emdgroup().

Parameters:group (h5py._hl.group.Group or int) – Reference to the HDF5 group to load. If int is used then the item corresponding to self.list_emds is loaded
put_comment(msg, timestamp=None)[source]

Create a comment in the EMD file.

If timestamp already exists, the msg is appended to existing comment.

Parameters:
  • msg (str) – String of the message to save.
  • timestamp (str/None) – Timestamp used as the key, defaults to the current UTC time.
put_emdgroup(label, data, dims, parent=None, overwrite=False, **kwargs)[source]

Put an emdtype dataset into the EMD file.

Parameters:
  • label (str) – Label for the emdtype group containing the dataset.
  • data (np.ndarray) – Numpy array containing the data.
  • dims (tuple) – Tuple containing the necessary dims as ((vec, name, units), (vec, name, units), …)
  • parent (h5py.Group or None) – Parent for the emdtype group, if None it will be written to /data.
  • overwrite (bool) – Set to force overwriting entry in EMD file.
  • **kwargs (various) – Keyword arguments to be passed to h5py.create_dataset(), e.g. for compression.
Returns:

Group referencing this emdtype dataset or None if failed.

Return type:

h5py.Group or None

write_dim(label, dim, parent)[source]

Auxiliary function to write a dim dataset to parent.

Input is not checked for sanity, so handle exceptions in call.

Parameters:
  • label (str) – Label for dataset, usually dim1, dim2, dimN.
  • dim (tuple) – Tuple containing (data, name, units).
  • parent (h5py.Group) – HDF5 handle to parent group.
Returns:

HDF5 dataset handle referencing this dim.

Return type:

h5py.Group

ncempy.io.dm module

A module to load data and meta data from DM3 and DM4 files into python as written by Gatan’s Digital Micrograph program.

Note

General users:
Use the simplified dm.dmReader() function to load the data and meta data as a python dictionary.
Advanced users and developers:
Access the file internals through the dm.fileDM() class.
On Memory mode:
The fileDM support and “on memory” mode that pre-loads the file data in memory and read operations during header parsing are performed against memory. This can significantly improve performance when the file resides in a parallel file system (PFS) because latency of seek operations PFSs is very high.
ncempy.io.dm.dmReader(filename, dSetNum=0, verbose=False, on_memory=True)[source]

A simple function to parse the file and read the requested dataset. Most users will want to use this function to simplify reading data directly into memory and to retriece the spatial axes (i.e. energy axis).

Parameters:
  • filename (str) – The filename to open and read into memory
  • dSetNum (int) – The number of the data set to read. Almost always should be = 0. Default = 0
  • verbose (bool) – Allow extra printing to see file internals. Default = False
  • on_memory (bool, default True) – Whether to use the on_memory option of fileDM. Usually provides much faster data reading.

Notes

Use the coords key for spatial axes (i.e. energy loss for spectra).

Returns:A dictionary of keys where the data is in the ‘data’ key. Other metadata is contained in other named keys such as ‘pixelSize’. ‘coords’ contains the coordinate axes with the proper origin and scale applied (i.e. the energy loss axis for EELS data)
Return type:dict

Example

Load data from a single image dm3 file and display it >> from ncempy.io import dm >> im0 = dm.dmReader(‘filename.dm3’) >> plt.imshow(im0[‘data’]) #show the single image from the data file

class ncempy.io.dm.fileDM(filename, verbose=False, on_memory=True)[source]

Opens the file and reads in the header. Data is loaded using the getDataset method.

xSize, ySize, zSize

The shape of the data for each data set in the file. Each value is the same as the shape attribute for an ndarray.

Type:list
zSize2

The shape of the 4th dimension for a 4D file (i.e. 4D-STEM data)

Type:list
dataSize

The total number of bytes in each dataset. Similar to numpy’s nbytes attribute for an ndarray.

Type:list
dataOffset

The starting byte number for each dataset. This can provide fast access directly to the data if needed by seeking to this byte number.

Type:list
dataShape

The total number of dimensions in eahc dataset. Similar to numpy’s ndims attribute for an ndarray.

Type:list
file_name

The name of the file

Type:str
file_path

A pathlib.Path object for the open file

Type:pathlib.Path
fid

The file handle.

Type:file
numObjects

The number of datasets in the file. Often (but not always) the file contains a thumbnail and the raw data. The thumbnail will always be the first object. See thumbnail attribute.

Type:list
thumbnail

Tells whether the first object or dataset in the file is a thumbnail. If true then this object is always skipped in methods and it is assumed that the user wants to skip this. Can retrive this thumbnail using a built-in method if desired.

Type:bool
scale

The real size of the pixel. Real and reciprical space are supported.

Type:list
scaleUnit

The unit name as a string for each dimension of each dataset.

Type:list
origin

The origin for the real or reciprocal space scaling for each dimension. Be careful, this value is actually meant to be scaled by the scale before being used. See ncempy.io.dmReader() for proper handling of this especially for spectroscopy data.

Type:list
allTags

Contains all tags in the DM file as key value pairs.

Type:dictionary

Examples

Read data from a file containing a single image into memory >> from ncempy.io import dm >> with dm.fileDM(‘filename.dm4’) as dmFile1: >> dataSet = dmFile1.getDataset(0)

Example of reading a full multi-image DM3 file into memory: >> with dm.fileDM(‘imageSeries.dm3’)as dmFile2: >> series = dmFile2.getDataset(0)

fromfile(*args, **kwargs)[source]

Reads data from a file or memory map. Calls np.fromfile and np.frombuffer depending on the on_memory mode of the fileDM.

Note

This is essentially a passthrough function to Numpy’s frombuffer and fromfile depending on the class variable on_memory.

Parameters:
  • *args – dtype and count are required
  • **kwargs
Returns:

Data read from the file as a 1d ndarray.

Return type:

ndarray

getDataset(index)[source]

Retrieve a dataset from the DM file.

Notes

Most DM3 and DM4 files contain a small “thumbnail” as the first dataset written as RGB data. This function ignores that dataset if it exists. To retrieve the thumbnail use the getThumbnail() function.

The pixelOrigin returned is not actually the start of the coordinates. The start of the energy axis for EELS (for example) will be pixelSize * pixelOrigin. dmReader() returns the correct coordinates. The correct origin is: pixelSize * pixelOrigin and be careful about the sign as it seems some datasets might use -pixelOrigin in the previous equation.

Parameters:index (int) – The number of the data set to retrieve ignoring the thumbnail. If a thumbnail exists then index = 0 actually corresponds to the second data set in a DM file.
Returns:A dictionary of the data and meta data. The data is associated with the ‘data’ key in the dictionary.
Return type:dict
getMemmap(index)[source]

Return a numpy memmap object (read-only) for the dataset requested. This is very useful for very large datasets to avoid loading the entire data set into memory. No meta data is returned.

Parameters:index (int) – The number of the dataset in the DM file.
Returns:A read-only numpy memmap object with access to the data. The file will continue to be open as long as the memmap is open. Delete the memmap to close the file.
Return type:numpy.memmap
getMetadata(index)[source]

Get the useful metadata in the file. This parses the allTags dictionary and retrieves only the useful information about hte experimental parameters. This is a (useful) subset of the information contains in the allTags attribute.

Note: some DM files contain extra information called the Tecnai Microscope Info. This is added to the metadata dictionary as a string.

Parameters:index (int) – The number of the dataset to get the metadata from.
getSlice(index, sliceZ, sliceZ2=0)[source]

Retrieve a slice of a dataset from the DM file. The data set will have a shape according to 3D = [sliceZ,Y,X] or 4D: [sliceZ2,sliceZ,Y,X]

Note: Most DM3 and DM4 files contain a small “thumbnail” as the first dataset written as RGB data. This function ignores that dataset if it exists. To retrieve the thumbnail use the getThumbnail() function.

Warning: DM4 files with 4D data sets are written as [X,Y,Z1,Z2]. This code currently gets the [X,Y] slice. Getting the [Z1,Z2] slice is not yet implemented. Use the getMemmap() function to retrieve arbitrary slices of large data sets.

Parameters:
  • index (int) – The number of the dataset in the DM file.
  • sliceZ (int) – The slice to get along the first dimension (C-ordering) for 3D datasets or 4D datasets.
  • sliceZ2 (int) – For 4D dataset
Returns:

A dictionary containing meta data and the data.

Return type:

dict

getThumbnail()[source]

Read the thumbnail saved as the first dataset in the DM file as an RGB array. This is not fully tested. Be careful using this.

Returns:Numpy array of size [3,Y,X] which is an RGB thumbnail.
Return type:ndarray
parseHeader()[source]

Parse the header by reading the root tag group. This ensures the file pointer is in the correct place.

seek(fid, offset, from_what=0)[source]

Positions the reading head for fid. fid can be a file or memory map. Follows the same convention as file.seek

Parameters:
  • fid (file id) – File or memory map.
  • offset (int) – Number of bytes to move the head forward (positive value) or backwards (negative value).
  • from_what (int) – Reference point to use in the head movement. 0: for beginning of the file (default behavior), 1: from the current head position, and 2: from the end of the file.
tell()[source]

Return the current position in the file. Switches mode based on on_memory mode.

Returns:pos – The current position in the file.
Return type:int
writeTags(new_folder_path_for_tags=None)[source]

Write out all tags as human readable text to a text file in the same directory (or a user definable directory) and with a the same name as the DM file.

Parameters:new_folder_path_for_tags (str or pathlib.Path, Optional) – Allow user to define a different path than the directory of the current file.

ncempy.io.emdVelox module

Provides an interface to Velox EMD datasets. Not to be confused with Berkeley EMD data sets (see emd.py) instead.

The reader for EMD Berkeley and Velox files will be combined in the near future once they are fully tested separately.

Currently limited to only images. This file can not load spectra.

Note

General users:
Use the simplified emdVelox.emdVeloxReader() function to load the data and meta data as a python dictionary.
Advanced users and developers:
Access the file internals through the emd.fileEMDVelox() class.
ncempy.io.emdVelox.emdVeloxReader(filename, dsetNum=0)[source]

A simple helper function to read in the data and metadata in a structured format similar to the other ncempy readers.

Note

Not fully implemented yet. Work in progress. Important metadata is missing, but you can get the data.

Parameters:
  • filename (str or pathlib.Path) – The path to the file.
  • dsetNum (int, default = 0) – The index of the data set to load.
Returns:

Data and metadata as a dictionary similar to other ncempy readers.

Return type:

dict

Example

Load all data and metadata from a data set in an EMD file
>> import ncempy.io as nio >> emd0 = nio.emdVelox.emdVeloxReader(‘filename.emd’, dsetNum = 0)
class ncempy.io.emdVelox.fileEMDVelox(filename)[source]

Class to represent Velox EMD files. It uses the h5py caching functionality to increase the default cache size from 1MB to 10MB. This significantly improves file reading for EMDVelox files which are written with Fortran- style ordering and an inefficient choice of chunking.

list_data

A list containing each h5py data group that can be loaded.

Type:list
_file_hdl

The File handle from h5py.File.

Type:h5py.File
metaDataJSON

The full metadata for the most recently loaded data set. Note that you have to load a data set for this to be populated or run parseMetaData(num).

Type:dict
file_name

The name of the file

Type:str
file_path

A pathlib.Path object for the open file

Type:pathlib.Path

Examples

Open an EMD Velox file containing 1 image. >> import ncempy.io as nio >> with nio.emdVelox.fileEMDVelox(‘1435 1.2 Mx STEM HAADF-DF4-DF2-BF.emd’) as emd1: >> print(emd1) # print information about the file >> im0, metadata0 = emd1.get_dataset(0)

get_dataset(group, memmap=False)[source]

Get the data from a group and the associated metadata.

Parameters:
  • group (HDF5 dataset or int) – The link to the HDF5 dataset in the file or an integer for the number of the dataset. The list of datasets is held in the list_data attribute populated on class init.
  • memmap (bool, default = False) – If False (default), then a numpy ndarray is returned. If True the HDF5 data set object is returned and data is loaded from disk as needed.
Returns:

A tuple containing the data as a ndarray or a HDF5 dataset object. The second argument is a python dict of metadata.

Return type:

tuple (ndarray or HDF5 dataset, dict)

parseMetaData(group)[source]

Parse metadata in a data group. Determines the pixelSize and detector name. The EMDVelox data sets have extensive metadata stored as a JSON type string.

Parameters:group (h5py.Group or int) – The h5py group to load the metadata from which is easily retrived from the list_data attribute. If input is an int then the group corresponding to list_data attribute is used. The string metadata is loaded and parsed by the json module into a dictionary.
Returns:md – The JSON information in the file returned as a python dictionary.
Return type:dict

ncempy.io.mrc module

A module to read MRC files in python and numpy. Written according to MRC specification at http://bio3d.colorado.edu/imod/betaDoc/mrc_format.txt Also works with FEI MRC files which include a special header block with experimental information.

Note

General users:
Use the simplified mrc.mrcReader() function to load the data and meta data as a python dictionary.
Advanced users and developers:
Access the file internals through the mrc.fileMRC() class.
ncempy.io.mrc.appendData(filename, data)[source]

Append a binary set of data to the end of a MRC file. This should only be used in conjunction with writeHeader() above.

Parameters:
  • filename (str) – Name of the MRC file with pre-initiated header and some data already written.
  • data (ndarray) – Data to append to the file.
ncempy.io.mrc.emd2mrc(filename, dsetPath)[source]

Convert EMD data set into MRC data set. The final data type is float32 for convenience.

Parameters:
  • filename (str) – The name of the EMD file
  • dsetPath (str) – The HDF5 path to the top group holding the data. ex. ‘/data/raw/’
class ncempy.io.mrc.fileMRC(filename, verbose=False)[source]

Read in the data in MRC format and other useful information like metadata. Follows the specification published at http://bio3d.colorado.edu/imod/betaDoc/mrc_format.txt

file_name

The name of the file

Type:str
file_path

A pathlib.Path object for the open file

Type:pathlib.Path
fid

The file handle to the opened MRC file.

Type:file
mrcType

The internal MRC data type.

Type:int
dataType

The numpy dtype corresponding to the mrcType.

Type:np.dtype
dataSize

The number of pixels along each dimension. Corresponds to the shape attribute of a np.ndarray

Type:np.ndarray
gridSize

The size of the grid. Usually the same as dataSize

Type:np.ndarray
volumeSize

The size of the volume along each direction in Angstroms.

Type:np.ndarray
voxelSize

The size of the voxel along each direction in Angstroms.

Type:np.ndarray
cellAngles

The angles of the cell. Ignored in most cases including ncempy.

Type:np.ndarray
axisOrientations

Mapping the orientations of the data to real space directions X, Y, Z. Ignored by ncempy

Type:np.ndarray
minMaxMean

The minimum, maximum and mean value of the data to avoid computing this every time.

Type:np.ndarray
extra

Extra mbinary metadata if it exists.

Type:np.ndarray
FEIinfo

A dictionary of metadata used by FEI (Thermo Fischer) microsocpes for important metadata. This metadata overwrites the voxelsize attribute if it exists.

Type:dict
dataOffset

The integer offset in bytes to the start of the raw data.

Type:int
dataOut

Will hold the data and metadata to output to the user after getDataset() call.

Type:dict
v

More output for debugging. False by default

Type:bool

Examples

Read in all data and metadata into memory. >> import ncempy.io as nio >> mrc0 = nio.mrc.mrcReader(‘file.mrc’)

Low level operations to get 1 slice of the 3D data >> import ncempy.io as nio >> with nio.mrc.fileMRC(‘file.mrc’) as f1: >> single_slice = f1.getSlice(0)

getDataset()[source]

Read in the full data block and reshape to an ndarray with C-style ordering.

getMemmap()[source]

Return a numpy memmap object (read-only) for the dataset. This is very useful for very large datasets to avoid loading the entire data set into memory. No meta data is returned.

Returns:A read-only numpy memmap object with access to the data on disk.
Return type:numpy.core.memmap
getSlice(num)[source]

Read in a slice of an MRC file. Useful for parsing through a large file without reading the entire data set into memory.

Parameters:num (int) – Get the requested image.
Returns:out – A 2D slice or a 3D set of slices along the first index
Return type:ndarray
Raises:IndexError – If num > the number of slices.
parseHeader()[source]

Read the header information which includes data type, data size, data shape, and metadata.

Note

This header uses Fortran-style ordering. Numpy uses C-style ordering. The header is read in and then some attributes are reversed [::-1] at the end for output to the user to match C-ordering in numpy.

Todo

Implement special dtype to read the entire header at once. Read everything at once using special dtype. ~5x faster than multiple np.fromfile() reads:

Untested but works in theory headerDtype = np.dtype([(‘head1’,’10int32’),(‘head2’,’6float32’),(‘axisOrientations’,’3int32’), (‘minMaxMean’,’3int32’),(‘extra’,’32int32’)]) head = np.fromfile(self.fid,dtype=headerDtype,count=1)

ncempy.io.mrc.mrc2emd(file_name)[source]

Write an MRC file as an HDF5 file in EMD format with same file name and .emd ending. Header information is retained as attributes.

Parameters:file_name (str) – The name of the file to convert from MRC to EMD format.
Returns:out – 1 if successful.
Return type:int

Todo

Update this to use ncempy.emd class

ncempy.io.mrc.mrc2raw(file_name)[source]

Convert an MRC type file to raw binary. The entire data is read into memory and then written to a new raw file in the same location with the data type and shape (C-ordering) written into the filename. No other meta data is retained.

Parameters:file_name (str or pathlib.Path) – The file name to convert.
ncempy.io.mrc.mrcReader(file_name)[source]

A simple function to read open a MRC, parse the header, and read the full data set.

Parameters:file_name (str or pathlib.Path) – The path to the file to load.
Returns:out – A dictionary containing the data and interesting metadata. The data is attached to the ‘data’ key.
Return type:dict

Example

Read in all data from disk into memory. This assumes the dataset is 3 dimensional: >> from ncempy.io.mrc import mrcReader >> import matplotlib.pyplot as plt >> mrc1 = mrcReader(‘filename.mrc’) >> plt.imshow(mrc1[‘data’][0, :, :]) # show the first image in the data set

ncempy.io.mrc.mrcWriter(filename, data, pixelSize, forceWrite=False)[source]

Write out a MRC type file according to the specification at http://bio3d.colorado.edu/imod/doc/mrc_format.txt

Parameters:
  • filename (str or pathlib.Path) – The name or Path of the file to write out to.
  • data (ndarray) – The array data to write to disk.
  • pixelSize (tuple) – The size of the pixel along each direction (in Angstroms) as a 3 element vector (sizeZ,sizeY,sizeX).
  • forceWrite (bool) – This will write the data as a C-contiguous array. It is not suggested to use this option and it will be removed in future versions.
ncempy.io.mrc.writeHeader(filename, shape, dtype, pixelSize)[source]

Write out a MRC type file header according to the specification at http://bio3d.colorado.edu/imod/doc/mrc_format.txt. This is useful for initializing an MRC file and then writing to it manually or see appendData() function below.

Parameters:
  • filename (str) – The name of the EMD file
  • shape (tuple) – The shape of the data to write
  • dtype (numpy.dtype) – The dtype to write out the data as. Only some numpy dtypes are supported byt his format. It is suggested to use np.float32 in most cases for maximum compatibility.
  • pixelSize (tuple) – The size of the pixel along each direction (in Angstroms) as a 3 element vector (sizeX,sizeY,sizeZ). sizeZ could be the angular step for a tilt series

ncempy.io.ser module

This module provides an interface to the SER file format written by FEI and Therm0 Fischer’s program TIA.

It reads STEM and TEM images and other datasets.

It is based on information provided by Dr Chris Boothroyd and work by Peter Ercius’ original serReader code in Matlab.

Note

General users:
Use the simplified ser.serReader() function to load the data and meta data as a python dictionary.
Advanced users and developers:
Access the file internals through the ser.fileSER() class.
exception ncempy.io.ser.NotSERError[source]

Exception if a file is not in SER file format.

class ncempy.io.ser.fileSER(filename, verbose=False)[source]

Class to represent SER files (read only).

_file_hdl

The open file as a raw stream.

Type:file
_emi

A dictionary of metadata from the EMI file accompanying the SER file.

Type:dict
file_name

The name of the file

Type:str
file_path

A pathlib.Path object for the open file

Type:pathlib.Path
head

Header information for the SER file as a dictionary. Provides direct access to data offsets and other internal file information.

Type:dict

Note

For most users, we suggest using the ser.serReader() function to load the full data set into memory. Otherwise, this class provides low level access to the SER file data and metadata and internals.

Examples

Read data from a single image into memory using the low level API. >> import matplotlib.pyplot as plt >> import ncempy.io as nio >> with nio.ser.fileSER(‘filename.ser’) as ser1: >> data, metadata = ser1.getDataset(0)

SER files are internally structured such that each image in a series is a different data set. Thus, time series data should be read as the following: >> with ser.fileSER(‘filename_1.ser’) as ser1: >> image0, metadata0 = ser1.getDataset(0) >> image1, metadata1 = ser1.getDataset(1)

getDataset(index, verbose=False)[source]

Retrieve data and meta data for one image or spectra from the file.

Parameters:
  • index (int) – Index of dataset.
  • verbose (bool, optional) – True to get extensive output while reading the file.
Returns:

dataset – Tuple contains data as np.ndarray and metadata (pixel size, etc.) as a dict.

Return type:

tuple, 2 elements in form (data metadata)

readHeader(verbose=False)[source]

Read and return the SER files header.

Parameters:verbose (bool) – True to get extensive output while reading the file.
Returns:The header of the SER file as dict.
Return type:dict
writeEMD(filename)[source]

Write SER data to an EMD file.

Parameters:filename (str or pathlib.Path) – Name of the EMD file.
ncempy.io.ser.read_emi(filename)[source]

Read the meta data from an emi file.

Parameters:filename (str or pathlib.Path) – Path to the emi file.
Returns:Dictionary of experimental metadata stored in the EMI file.
Return type:dict
ncempy.io.ser.serReader(filename)[source]
Simple function to parse the file and read all datasets. This is a one function implementation to load all data
in a ser file.
Parameters:filename (str) – The filename of the SER file containing the data.
Returns:dataOut – A dictionary containing the data and meta data. The data is accessed using the ‘data’ key and is a 1, 2, 3, or 4 dimensional numpy ndarray.
Return type:dict

Examples

Load a single image data set and show the image: >> import ncempy.io as nio >> ser1 = nio.ser.serReader(‘filename_1.ser’) >> plt.imshow(ser1[‘data’]) # show the single image from the data file