Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Datasets & I/O

The Dataset classes in quantem.core.datastructures are the common currency of quantEM: file readers return them, analysis routines consume and produce them, and visualization functions know how to display them with correct physical units.

The Dataset classes

ClassDataTypical use
DatasetN-dimensional arrayBase class
Dataset2d2D arrayImages, diffraction patterns
Dataset3d3D arrayImage stacks, tilt series
Dataset4d4D arrayGeneric 4D data
Dataset4dstem4D array4D-STEM scans (probe positions × detector)
VectorRagged point dataDetected peaks, e.g. Bragg disk positions per probe position

Each dataset stores its array together with calibration metadata:

The underlying data is accessible both as a NumPy array (.array) and as a torch tensor (.tensor), so datasets move cleanly between CPU-side plotting and GPU-side computation.

Constructing a dataset from an existing array:

import numpy as np
from quantem.core.datastructures import Dataset2d

image = Dataset2d.from_array(
    np.random.rand(256, 256),
    sampling=(0.2, 0.2),
    units=("A", "A"),
    name="example image",
)

Reading data from files

quantem.core.io provides file readers built on RosettaSciIO, which supports most vendor formats:

from quantem.core import io

image = io.read_2d("image.dm4")
scan = io.read_4dstem("scan.h5")
legacy = io.read_emdfile_to_4dstem("data.emd")

read_2d handles images and single diffraction patterns, read_4dstem handles 4D-STEM scans, and read_emdfile_to_4dstem handles legacy emdFile and py4DSTEM files.

Saving and loading quantEM objects

Datasets and analysis objects serialize to Zarr-backed files:

from quantem.core import io

io.load("reconstruction.zip")
io.print_file("reconstruction.zip")

load reconstructs the saved object, and print_file prints its structure without loading it.