Home

Awesome

mrc

License PyPI Python Version CI codecov

Read and write .dv (deltavision) and some mrc image file format.

Note, this module is designed to read the MRC variant used by deltavision microscopes (.dv) and the IVE library from UCSF. For the MRC2014 file format frequently used for structural biology, see mrcfile

(though dv and mrc formats are very similar, priism/dv files that evolved from the IVE library at UCSF have a slightly different header format, preventing mrcfile from working).

Install

pip install mrc

usage and API

new API: Oct 2021

DVFile is a rewrite of the reader, and will be the only maintained reader going forward. It does not write files (see the legacy API for that).

from mrc import DVFile, imread
import numpy as np

my_array = imread('some_file.dv')                          # read to numpy array
my_array = imread('some_file.dv', dask=True)               # read to dask array
my_array = imread('some_file.dv', xarray=True)             # read to xarray
my_array = imread('some_file.dv', xarray=True, dask=True)  # read to dask-xarray

# or open a file with DVFile
f = DVFile('some_file.dv')

# attributes:   # example output
f.path          # 'some_file.dv'
f.shape         # (10, 2, 256, 256)
f.ndim          # 4
f.dtype         # np.dtype('uint16')
f.sizes         # {'T': 10, 'C': 2, 'Y': 256, 'X': 256}

# array output
f.asarray()                # in-memory np.ndarray
np.asarray(f)              # alternative to f.asarray()
f.to_dask()                # delayed dask.array.Array
f.to_xarray()              # in-memory xarray.DataArray, with labeled axes/coords
f.to_xarray(delayed=True)  # delayed xarray.DataArray

# metadata
f.hdr           # Header as a named tuple
f.ext_hdr       # (optional) extended header info
f.voxel_size    # VoxelSize(x=0.65, y=0.65, z=1.0)

f.close()       # don't forget to close when done!
f.closed        # boolean, whether the file is closed

# ... or you can use it as a context manager
with DVFile('some_file.dv') as dvf:
    xarr = dvf.to_xarray()

legacy API

The following older API still exists in this package under the mrc namespace.

This module was extracted from the priithon package, written by Sebastian Haase.

import mrc
import numpy as np

# Read a dv file
arr = mrc.imread('/path/to/file.dv')
# just a numpy array with the data...
isinstance(arr, np.ndarray)  # True

# additional info in stored in the arr.Mrc object.
# print it
arr.Mrc.info()
# or access particular fields:
print(arr.Mrc.header)
# dv files may have additional info in the extended header:
arr.Mrc.extended_header
# for instsance, timestamps for each channel at each timepoint:
arr.Mrc.extended_header['timeStampSeconds']

# or you can write a numpy array to DV format
arr = np.random.rand(23,3,256,256).astype('single')
mrc.imsave("/path/to/output.dv", arr,
    metadata={
        'dx': 0.08,
        'dy': 0.08,
        'dz': 0.125,
        'wave': [445, 528, 615, 0, 0]
    }
)

Priism (DV) MRC Header Format

this information is archived from the no-longer-existing page at http://www.msg.ucsf.edu/IVE/IVE4_HTML/priism_mrc_header.html

The MRC header is 1024 bytes layed out as described below. Each field is in one of these formats:

n Is a 2-byte signed integer (NPY_INT16)

i Is a 4-byte signed integer (NPY_INT32)

f Is a 4-byte floating-point value in IEEE format (NPY_FLOAT32)

cn Is a string of characters that is n bytes long.

Byte NumbersVariable TypeVariable NameContents
1 - 4iNumColnumber of columns (fastest-varying dimension; normally mapped to x)
5 - 8iNumRownumber of rows (second fastest-varying dimension; normally mapped to y)
9 - 12inumber of sections (slowest-varying dimension; normally mapped to the remaining dimensions, z, wavelength, and time)
13 - 16iPixelTypedata type (see Pixel Data Types)
17 - 20imxstindex of the first column (normally mapped to x) in the data; zero by default
21 - 24imystindex of the first row (normally mapped to y) in the data; zero by default
25 - 28imzstindex of the first section (normally treated as the first z) in the data; zero by default
29 - 32imxnumber of intervals in the fastest-varying direction (normally x)
33 - 36imynumber of intervals in the second fastest-varying direction (normally y)
37 - 40imznumber of intervals in the slowest varying direction (normally treated as z)
41 - 44fdxpixel spacing times sampling interval for fastest-varying direction (first cell dimension in Angstroms for crystallographic data)
45 - 48fdypixel spacing times sampling interval for second fastest-varying direction (second cell dimension in Angstroms for crystallographic data)
49 - 52fdzpixel spacing times sampling interval slowest-varying direction (third cell dimension in Angstroms for crystallographic data)
53 - 56falphacell angle (alpha) in degrees; defaults to 90
57 - 60fbetacell angle (beta) in degrees; defaults to 90
61 - 64fgammacell angle (gamma) in degrees; defaults to 90
65 - 68icolumn axis (1 = x, 2 = y, 3 = z; defaults to 1)
69 - 72irow axis (1 = x, 2 = y, 3 = z; defaults to 2)
73 - 76isection axis (1 = x, 2 = y, 3 = z; defaults to 3)
77 - 80fminminimum intensity of the 1st wavelength image
81 - 84fmaxmaximum intensity of the 1st wavelength image
85 - 88fmeanmean intensity of the first wavelength image
89 - 92inspgspace group number (for crystallography)
93 - 96inextextended header size in bytes.
97 - 98ndvidID value (-16224)
99 - 100nnblankunused
101 - 104intststarting time index
105 - 128c24blank24 bytes long blank section
129 - 130nNumIntegersnumber of 4 byte integers stored in the extended header per section.
131 - 132nNumFloatsnumber of 4 byte floating-point numbers stored in the extended header per section.
133 - 134nsubnumber of sub-resolution data sets stored within the image typically 1)
135 - 136nzfacreduction quotient for the z axis of the sub-resolution images
137 - 140fmin2minimum intensity of the 2nd wavelength image
141 - 144fmax2maximum intensity of the 2nd wavelength image
145 - 148fmin3minimum intensity of the 3rd wavelength image
149 - 152fmax3maximum intensity of the 3rd wavelength image
153 - 156fmin4minimum intensity of the 4th wavelength image
157 - 160fmax4maximum intensity of the 4th wavelength image
161 - 162nimage typesee Image Types
163 - 164nLensNumlens identification number
165 - 166nn1depends on the image type
167 - 168nn2depends on the image type
169 - 170nv1depends on the image type
171 - 172nv2depends on the image type
173 - 176fmin5minimum intensity of the 5th wavelength image
177 - 180fmax5maximum intensity of the 5th wavelength image
181 - 182nNumTimesnumber of time points
183 - 184nImgSequenceimage sequence (0 = ZTW, 1 = WZT, 2 = ZWT)
185 - 188fx axis tilt angle (degrees)
189 - 192fy axis tilt angle (degrees)
193 - 196fz axis tilt angle (degrees)
197 - 198nNumWavesnumber of wavelengths
199 - 200nwave1wavelength 1 in nm
201 - 202nwave2wavelength 2 in nm
203 - 204nwave3wavelength 3 in nm
205 - 206nwave4wavelength 4 in nm
207 - 208nwave5wavelength 5 in nm
209 - 212fz0z origin (um for optical, Angstroms for EM)
213 - 216fx0x origin (um for optical, Angstroms for EM)
217 - 220fy0y origin (um for optical, Angstroms for EM)
221 - 224iNumTitlesnumber of titles (valid numbers are between 0 and 10)
225 - 304c80title 1
305 - 384c80title 2
385 - 464c80title 3
465 - 544c80title 4
545 - 624c80title 5
625 - 704c80title 6
705 - 784c80title 7
785 - 864c80title 8
865 - 944c80title 9
945 - 1024c80title 10

Pixel Data Types

The data type used for image pixel values, stored as a signed 32-bit integer in bytes 13 through 16, is designated by one of the code numbers in the following table.

Data TypeNumpy TypeDescription
0NPY_UINT81-byte unsigned integer
1NPY_INT162-byte signed integer
2NPY_FLOAT324-byte floating-point (IEEE)
34-byte complex value as 2 2-byte signed integers
4NPY_COMPLEX648-byte complex value as 2 4-byte floating-point (IEEE) values
52-byte signed integer (unclear)
6NPY_UINT162-byte unsigned integer
7NPY_INT324-byte signed integer

Type codes 5, 6, and 7 are not standard MRC types and are not likely to be correctly interpreted by other software that uses MRC files.

Image Types

The type of a Priism image is given by the signed 16-bit integer in header bytes 161 and 162. The meaning of these types is given in the table below. The floating-point attributes, v1 and v2, used by some image types are stored as 16-bit signed integers in the header; to do so the values are multiplied by 100 and rounded to the nearest integer when stored and are divided by 100 when retrieved.

0 (IM_NORMAL_IMAGES)

Used for normal image data.

1 (IM_TILT_SERIES)

Used for single axis tilt series with a uniform angle increment. n1 specifies the tilt axis (1 for x, 2 for y, 3 for z) and v1 the angle increment in degrees. n2 relates the coordinates in the tilt series to coordinates in a 3D volume: the assumed center of rotation is the z origin from the header plus n2 times one half of the z pixel spacing from the header. v2 is always zero.

2 (IM_STEREO_TILT_SERIES)

Used for stereo tilt series. n1 specifies the tilt axis (1 for x, 2 for y, 3 for z), v1 the angle increment in degrees, and v2 is the angular separation in degrees for the stereo pairs. n2 is always zero.

3 (IM_AVERAGED_IMAGES)

Used for averaged images. n1 is the number of averaged sections and n2 is the number of sections between averaged sections. v1 and v2 are always zero.

4 (IM_AVERAGED_STEREO_PAIRS)

Used for averaged stereo pairs. n1 is the number of averaged sections, n2 is the number of sections between averaged sections, and v2 is the angular separation in degrees for the stereo pairs. v2 is always zero.

5 (IM_EM_TILT_SERIES)

Used for EM tomography data. The tilt angles are stored in the extended header.

20 (IM_MULTIPOSITION)

Used for images of well plates. The following quantities are bit-encoded in n1 (valid range for each is show in parentheses): iwell (0-3), ishape (0-1), ibin (0-15), ispeed (0-2), igain (0-3), and mag (0-1). n2 is the number of fields per well. v1 is the fill factor (.01 to 1.5 in .01 steps). v2 is not used.

8000 (IM_PUPIL_FUNCTION)

Used for images of pupil functions. n1 and n2 are not used. v1 is the numerical aperture times ten. v2 is the immersion media refractive index times one hundred. The pixel spacings and origin have units of cycles per micron rather than microns.

Credits

This package was created by Sebastian Haase as a part of the priithon package. It is updated and maintained by Talley Lambert.