MHD#

IO interface to mhd / raw files.

Note

Using the function write_header_from_source() a mhd header for a numpy binary array can be created to enable loading the numpy file into imagej.

class Source(location, name=None)[source]#

Bases: Source

Mhd/raw array source.

as_array()[source]#
as_buffer()[source]#
as_memmap()[source]#
as_real()[source]#
as_virtual()[source]#

Return virtual source without array data to pass in parallel processing.

Returns

sourceSource class

The source class without array data.

metadata(info=None)[source]#

Returns metadata from this mhd file.

Arguments

infolist or all

Optional list of keywords, if all return full tif metadata, if None return default set info.

Returns

metadatadict

Dictionary with the metadata.

property array#

The underlying data array.

Returns

arrayarray

The underlying data array of this source.

property dtype#

The data type of the source.

Returns

dtypedtype

The data type of the source.

property element_strides#

The strides of the array elements.

Returns

stridestuple

Strides of the array elements.

Note

The strides of the elements module itemsize instead of bytes.

property location#

The location where the data of the source is stored.

Returns

locationstr or None

Returns the location of the data source or None if this source lives in memory only.

property name#

The name of this source.

Returns

namestr

Name of this source.

property offset#

The offset of the memory map in the file.

Returns

offsetint

Offset of the memory map in the file.

property order#

The order of how the data is stored in the source.

Returns

orderstr

Returns ‘C’ for C contiguous and ‘F’ for fortran contiguous, None otherwise.

property shape#

The shape of the source.

Returns

shapetuple

The shape of the source.

class VirtualSource(source=None, shape=None, dtype=None, order=None, location=None, name=None)[source]#

Bases: VirtualSource

as_buffer()[source]#
as_real()[source]#
as_virtual()[source]#

Return virtual source without array data to pass in parallel processing.

Returns

sourceSource class

The source class without array data.

property name#

The name of this source.

Returns

namestr

Name of this source.

create(location=None, shape=None, dtype=None, array=None, as_source=True, **kwargs)[source]#
header_from_source(source=None, location=None, header=None, return_header_and_raw_file=False)[source]#

Create a mhd header dictionary for a source.

Arguments

sourceSource specification

Source file or class to create a mhd header file for.

locationstr or None

Location of the mhd file. If None, the source location with extension ‘mhd’ is used.

headerdict or None

Optional additional entries for the header file.

return_header_and_raw_filebool

If True also return the header file.

Returns

headerdict

The mhd header dictionary.

Note

This function can be used to make npy files readable in image processing software tools that allow to import mhd files, such ash ImageJ.

is_mhd(source)[source]#

Checks if this source is an MHD source.

read(source, slicing=None, **kwargs)[source]#

Read data from a mhd file.

Arguments

sourcestr

The name of the mhd file.

slicingslice, Slice or None

An optional sub-slice to consider.

Returns

arrayarray

The image data in the tif file as a buffer.

write(sink, data, slicing=None, **kwargs)[source]#

Write specialization for mhd files.

write_header_from_source(source, location=None, header=None)[source]#

Create a mhd header file for a source file.

Arguments

sourceSource specification

Source file or class to create a mhd header file for.

locationstr or None

Location of the mhd file. If None, the source location with extension ‘mhd’ is used.

headerdict or None

Optional additional entries for the header file.

Returns

filenamestr

The filename of the mhd header.