← Back to 4D-STEM imaging and reconstruction methods · 4d User Manual
Relevance of the file format
A file format is not only a container: it determines which properties of the measurement are preserved. Four properties are relevant for electron microscopy data and form the basis of the following comparison.
- Bit depth and number type. Detector counts require 16 bits; reconstructions and phase maps require 32-bit floating point. An 8-bit format (JPEG, GIF) discards the dynamic range on which quantitative analysis depends and cannot represent negative values.
- Lossy or lossless compression. Lossless formats return exactly the stored values. JPEG does not: it discards high spatial frequencies, which in a noisy micrograph or a diffraction pattern carry the signal of interest.
- Image origin. Row 0 is at the top in all conventional picture formats and at the bottom in MRC. An incorrect assumption mirrors the image and inverts the handedness of every quantity derived from it.
- Header. Pixel size, accelerating voltage, dose and camera settings are preserved only if the format provides fields for them.
Overview of the formats
File sizes are given for a single 1024 × 1024 image. "Raw" denotes the pixel data alone. The compressed sizes assume noisy detector data, which compress considerably less than a photograph, since noise is incompressible. This is a frequent source of unexpectedly large archives of micrographs.
| Format | Compression | Bit depths | Origin | Metadata in the header | 1024 × 1024 |
|---|---|---|---|---|---|
| HDF5 | Optional, lossless, per chunk: gzip, Blosc2, bitshuffle+LZ4, Zstd, LZF | any: 8/16/32/64-bit integer or float, complex | no convention; determined by the writing software | unlimited, user-defined attributes at any node of the tree | 2.1 MB at 16 bit raw; counting data typically 5–10× smaller compressed |
| MRC | None in the format (files are frequently gzipped as a whole) | int8, int16, uint16, float16, float32, complex | bottom-left | fixed 1024-byte header plus optional extended header | 2.1 MB at 16 bit, 4.2 MB at 32-bit float, plus 1 KB header |
| TIFF | Optional, lossless: LZW, Deflate, PackBits (JPEG-in-TIFF is lossy) | 1–64 bit, integer or float, any number of samples | top-left (Orientation tag 274 may specify otherwise) | tags: size, bit depth, resolution, description, date, private tags | 2.1 MB at 16 bit raw; ≈ 1.6–2.0 MB with LZW/Deflate on noisy data |
| EER | Lossless, event-based; the defining property of the format | event positions rather than pixel values | top-left (TIFF container) | TIFF tags plus an XML metadata block | scales with the number of electrons, not pixels; see below |
| PNG | Lossless, Deflate with per-row prediction filters | 8 and 16 bit, greyscale or RGB(A); no float | top-left | size, bit depth, optional physical pixel size and text chunks | ≈ 1.5–2.0 MB at 16 bit, ≈ 0.7–1.0 MB at 8 bit, for noisy data |
| JPEG | Lossy, DCT-based, quality-dependent | 8 bit per channel | top-left (EXIF Orientation may rotate or mirror) | JFIF and EXIF blocks: camera, date, orientation, thumbnail | ≈ 0.2–0.7 MB at quality 90; noise increases the size |
| GIF | Lossless LZW, restricted to a 256-colour palette | 8-bit palette (≤ 256 colours) | top-left | screen descriptor, palette, per-frame delays | ≈ 0.6–1.0 MB after quantisation to 256 grey levels |
Image origin conventions
HDF5 (.h5, .hdf5): the native format of 4d
HDF5 is a hierarchical container rather than an image format. A file holds a tree of groups (analogous to directories), datasets (n-dimensional arrays of any type) and attributes (metadata attached to either). This generality allows a complete 4D-STEM acquisition, including scan positions, diffraction patterns, masks and precomputed maps, to be stored in one file.
| Compression | Per-chunk filters, all lossless: gzip/Deflate, Blosc2, bitshuffle + LZ4, Zstd, LZF, with optional byte-shuffle pre-filters. The chunking also determines the read speed: a chunk shaped like one diffraction pattern allows 4d to load a single pattern without reading the remainder of the dataset. |
| Bit depth | Any machine type, including 64-bit floats and complex numbers. Datasets can be extendable and sparse. |
| Origin | Undefined by the format. The convention is determined by the layout used by the writing software. |
| Header | A superblock with B-trees and heaps, handled by the library. Scientific metadata are stored in user-defined attributes, so the layout must be agreed upon by convention: the 4d convention, EMD, NeXus. |
| 1024 × 1024 | Raw size plus a few kilobytes of structure. Compression of counting-mode data, in which most pixels are zero, routinely yields factors of 5–10; for dense 16-bit data, factors of 1.1–2 are typical. |
4d stores datasets in its version 2.0 convention with Blosc2 compression. The group layout is described in §7 of the main manual.
MRC (.mrc, .mrcs, .map)
MRC is the standard format of cryo-EM. It consists of a fixed 1024-byte header,
an optional extended header whose length is stated in the fixed header, and
the raw pixel data. It stores 2D images, image stacks (.mrcs) and
3D volumes (.map) in the same structure, so that a reconstruction
and a particle stack are read identically.
| Compression | None. The format stores raw values, which makes it fast to memory-map and produces large files. Whole files are frequently gzipped for transfer (.mrc.gz); variants such as MRCZ add Blosc compression but are not part of the standard. |
| Bit depth | Determined by the mode field: 0 = int8, 1 = int16, 2 = float32, 3 and 4 = complex, 6 = uint16, 12 = float16. Mode 2 is the default for reconstructions, mode 6 for raw camera frames. |
| Origin | Bottom-left by convention; see the figure above. |
| Header | 1024 bytes of fixed fields: image size (nx, ny, nz), mode, start indices, sampling (mx, my, mz), the unit-cell dimensions from which the pixel size is derived, axis order, minimum, maximum, mean and RMS of the data, space group, size of the extended header, the origin, the MAP stamp, a machine stamp specifying the byte order, and up to ten 80-character text labels. An optional extended header follows, used by Thermo Fisher (FEI1/FEI2) and SerialEM for per-frame metadata such as tilt angle, dose and stage position. |
| 1024 × 1024 | 1.05 MB (mode 0), 2.10 MB (modes 1/6), 4.19 MB (mode 2), each plus the 1 KB header. |
TIFF (.tif, .tiff)
TIFF is a tagged container rather than a single format. An 8-byte header points to a chain of image file directories, each a list of tags describing one image. This flexibility makes it the usual export format of camera software, and it is also the reason that two TIFF files may require entirely different code to read.
| Compression | Optional and normally lossless: LZW, Deflate (ZIP), PackBits run-length; newer writers add Zstd and LERC. JPEG compression within a TIFF is possible and lossy; it should not be used for data. On noisy micrographs, lossless compression typically reduces the size by only 5–20 %. |
| Bit depth | 1 to 64 bits, unsigned, signed or floating point, with one or more samples per pixel. TIFF is one of the few general-purpose formats that stores 32-bit float directly. |
| Origin | Top-left by default. Tag 274 (Orientation) can declare any of eight arrangements; many scientific readers ignore it, so a file that depends on it may appear mirrored. |
| Header | Byte-order mark (II or MM), magic number 42 (43 for BigTIFF), and the offset of the first directory. Tags then specify width, height, bits per sample, sample format, compression, strip or tile layout, X/Y resolution and unit. Free-text tags (ImageDescription, Software, DateTime) are used by ImageJ, SerialEM and camera vendors to store pixel size, dose and acquisition settings, often as XML or in a private tag block. |
| 1024 × 1024 | 1.05 / 2.10 / 4.19 MB raw at 8 / 16 / 32 bit, plus a few hundred bytes of tags; ≈ 1.6–2.0 MB for a noisy 16-bit image with Deflate. |
EER (.eer): Electron Event Representation
EER is based on a different representation of the image. Instead of integrating the electrons arriving during a frame into pixel counts, a Falcon 4-class detector records each detected electron as an event: its position, refined to a sub-pixel grid, and the frame in which it arrived. The file is a losslessly compressed list of these events.
| Compression | Lossless and intrinsic: the gaps between successive events are run-length encoded and entropy coded. Since a low-dose frame is almost entirely empty, this is far more compact than storing predominantly zero pixel arrays, by an order of magnitude or more relative to an equivalent raw movie. |
| Bit depth | Not applicable: there are no pixel values, only event positions. The image is generated by rendering the events onto a grid of chosen sampling, either the native sensor sampling or 2× / 4× finer using the sub-pixel positions, with as many frames per rendered image as the dose requires. |
| Origin | Top-left, as implied by the TIFF container; in practice the convention is fixed by the rendering library. |
| Header | EER is wrapped in a TIFF structure, so standard TIFF tags describe the frames, private tags carry the compressed event bitstreams, and an XML block holds the acquisition metadata: sensor size, number of frames, frame rate, dose rate. |
| Size | Proportional to the number of detected electrons, not to the pixel count; approximately one to two bytes per electron. A complete low-dose exposure typically occupies a few hundred MB, whereas the equivalent unpacked movie occupies several GB. Expressed per 1024 × 1024 area at 1 Å pixels and 40 e−/Ų, the events of the complete exposure amount to a few tens of MB, compared with 2.1 MB for the single 16-bit integrated image rendered from them. |
The practical consequence is that the EER file should be retained. Dose weighting, motion correction and the choice of fractionation can all be repeated from the events, which is not possible once they have been integrated into fixed frames.
PNG (.png)
| Compression | Lossless Deflate, preceded by a per-row prediction filter (each row is coded as its difference from the row above or the pixel to the left). Efficient for smooth images and line art, inefficient for noise. |
| Bit depth | 8 or 16 bit, greyscale or colour, with an optional alpha channel. No floating point and no negative values, so a phase map must be scaled into an integer range before storage. |
| Origin | Top-left; rows are stored top to bottom (optionally interlaced). |
| Header | An 8-byte signature followed by chunks. IHDR carries width, height, bit depth and colour type; pHYs can record the physical pixel size; tEXt/iTXt hold free text. There is no standard field for microscope metadata, which are therefore usually lost. |
| 1024 × 1024 | ≈ 0.7–1.0 MB for a noisy 8-bit micrograph, ≈ 1.5–2.0 MB at 16 bit; a smooth or largely empty image can be a tenth of that. |
PNG is the appropriate format for figures, screenshots and content intended for a manuscript or a manual: it is lossless, universally readable and does not introduce artefacts as JPEG does. It is not suitable for data, since the calibration is not stored with the image.
JPEG (.jpg, .jpeg)
| Compression | Lossy. The image is divided into 8 × 8 blocks, cosine-transformed, and the high-frequency coefficients are quantised. At quality 90 the loss is not visible in a photograph; in a micrograph it removes the fine detail that constitutes the measurement and introduces block edges and ringing around strong features. |
| Bit depth | 8 bit per channel. A 16-bit detector image must first be scaled to 256 levels, which alone excludes the format from quantitative use. |
| Origin | Top-left. The EXIF Orientation tag may request a rotation or mirror, and viewers are inconsistent in applying it. |
| Header | Marker segments: JFIF (version, pixel density), optionally EXIF (camera, date, exposure, orientation, GPS, thumbnail), the quantisation and Huffman tables, and the frame header with the dimensions. |
| 1024 × 1024 | ≈ 0.2–0.7 MB at quality 90 for a noisy image. Noise reduces the compression efficiency, so a micrograph is several times larger than a photograph of the same size. Quality 50 halves the size again, with clearly visible artefacts. |
GIF (.gif)
| Compression | Lossless LZW, applied only after the image has been reduced to a palette of at most 256 colours, which for a micrograph means 256 grey levels. The information loss occurs in the quantisation, not in the compression. |
| Bit depth | 8-bit palette indices. One palette entry can be transparent; there is no alpha channel and no 16-bit mode. |
| Origin | Top-left, with each frame positioned by its own left/top offsets within the logical screen. |
| Header | GIF87a/GIF89a signature, a logical screen descriptor (size, background colour, palette flags), the global colour table, and per-frame descriptors with delay times and disposal methods for animation. |
| 1024 × 1024 | ≈ 0.6–1.0 MB for a noisy greyscale image; LZW can exceed the raw size on pure noise. A few tens of kilobytes for a plot or a diagram. |
The remaining application of GIF is a short animation that plays in any viewer, such as a tilt series or a through-focus series for a presentation. For all other purposes, PNG is superior in every respect.
Other formats
| DM3 / DM4 | Gatan DigitalMicrograph. A tagged tree carrying the image together with comprehensive microscope metadata. DM4 uses 64-bit offsets for large files. |
| SER / EMI | Thermo Fisher (formerly FEI) TIA. The .ser file holds the data, the companion .emi file the metadata; the pair must be kept together. |
| EMD | Electron Microscopy Datasets: a documented convention within HDF5, readable by any HDF5 tool. Also the native format of Velox. |
| NeXus | A further HDF5 convention, originating from neutron and synchrotron science and increasingly used for electron data. |
| SMV / ADSC | A simple header-plus-data format from X-ray detectors, used by some electron-diffraction pipelines. |
| TVIPS | Movie format of TVIPS cameras: a header followed by concatenated frames. |
| OME-TIFF | TIFF with a standardised XML metadata block in the ImageDescription tag; the light-microscopy solution to the metadata problem. |
Recommendations
- Retain the original data. The data as written by the detector (EER events, the raw movie, the HDF5 file) should be archived, and all other files treated as derived. No conversion adds information.
- Data in HDF5 or MRC, pictures in PNG. HDF5 when the metadata and the structure are relevant, MRC when the cryo-EM tool chain requires it, PNG for visual presentation.
- Measurements should not be stored as JPEG or GIF. Eight bits and lossy compression are not a storage decision but a measurement decision, and the loss is irreversible.
- Verify the vertical orientation once per pipeline. A deliberately asymmetric image should be converted between MRC and TIFF/PNG and inspected. The result should be documented.
- Compression gains are limited. Detector noise is incompressible: lossless compression of a dense micrograph reduces the size by tens of percent, not by factors. Counting-mode and event data are the exception, since their sparsity is genuine and the gains are correspondingly large.
- Record the pixel size. If the format cannot store it (PNG, JPEG, GIF), it should be recorded alongside the file; otherwise the image is a picture rather than a measurement.