Density.from_file#

classmethod Density.from_file(filename, subset=None, use_memmap=False)[source]#

Reads a file into a Density instance.

Parameters:
filenamestr

Path to a file in CCP4/MRC, EM, HDF5 or a format supported by skimage.io.imread. The file can be gzip compressed.

subsettuple of slices, optional

Slices representing the desired subset along each dimension.

use_memmapbool, optional

Memory map the data contained in filename to save memory.

Returns:
Density

Class instance representing the data in filename.

Notes

If filename ends “.em” or “.h5” it will be parsed as EM or HDF5 file. Otherwise, the default reader is CCP4/MRC and on failure skimage.io.imread is used regardless of extension. The later does not extract origin or sampling_rate information from the file.

References

[1]

Burnley T et al., Acta Cryst. D, 2017

[2]

Nickell S. et al, Journal of Structural Biology, 2005.

Examples

Density.from_file() reads files in CCP4/MRC, EM, or a format supported by skimage.io.imread and converts them into a Density instance. The following outlines how to read a file in the CCP4/MRC format [1]:

>>> from tme import Density
>>> Density.from_file("/path/to/file.mrc")

In some cases, you might want to read only a specific subset of the data. This can be achieved by passing a tuple of slices to the subset parameter. For example, to read only the first 50 voxels along each dimension:

>>> subset_slices = (slice(0, 50), slice(0, 50), slice(0, 50))
>>> Density.from_file("/path/to/file.mrc", subset=subset_slices)

Memory mapping can be used to read the file from disk without loading it entirely into memory. This is particularly useful for large datasets or when working with limited memory resources:

>>> Density.from_file("/path/to/large_file.mrc", use_memmap=True)

Note that use_memmap will be ignored if the file is gzip compressed.

If the input file has an .em or .em.gz extension, it will automatically be parsed as EM file [2].

>>> Density.from_file("/path/to/file.em")
>>> Density.from_file("/path/to/file.em.gz")

If the file format is not CCP4/MRC or EM, Density.from_file() attempts to use skimage.io.imread to read the file [3], which does not extract origin or sampling_rate information from the file:

>>> Density.from_file("/path/to/other_format.tif")