Loading a .mat reconstruction
RawMRISeries (and everything built on it — MRSSeries, MRSISeries, MRSWashinSeries) reads
its recon dict from load_mat_file under the hood. Scanner reconstructions come in two MATLAB
file formats depending on size and MATLAB version — plain (v5/v7, scipy-readable) and v7.3
(HDF5-based, needs mat73) — and load_mat_file hides that distinction behind one call.
| Function | What it does here |
|---|---|
load_mat_file() | loads either .mat format, returns a plain dict |
read_mat_header() | reads only the header fields MNUtils uses, fast on either format |
from mnutils.utils import data_loaders as dl
from mnutils.utils import file_helpers as fh
from mnutils.testing import build_fake_exam
mrsi_missing_series_exam = build_fake_exam("mrsi_missing_series_exam")The current format¶
mat_path = fh.get_mat_data_from_series(mrsi_missing_series_exam, 7)
data = dl.load_mat_file(mat_path)
print(type(data), sorted(data.keys())[:5])
print(data["spec"].shape)<class 'dict'> ['__globals__', '__header__', '__version__', 'bb', 'h']
(700, 16, 16, 16)
The older (v7.3) format¶
Older reconstructions saved as MATLAB v7.3 fail the plain scipy.io.loadmat path and fall back
to mat73 transparently — same call, same return type.
legacy_data = dl.load_mat_file(build_fake_exam("legacy_matlab73_example"))
print("pyr" in legacy_data)True
Reading just the header¶
Series identification and the RawMRISeries properties (bandwidth, centre_frequency, …) need a
few dozen numbers from the scanner header h, not the whole file. That matters on v7.3: h alone
is ~1600 tiny HDF5 datasets, and a full load_mat_file spends ~0.45 s per file opening them —
about 9 s for an exam with 20 raw series, again for every object built from the same file.
read_mat_header opens only the allowlisted fields (HEADER_FIELDS), ~20x faster, and returns
the same nested dict you would get from load_mat_file(path)["h"], pruned.
header = dl.read_mat_header(mat_path)
print(header["series"]["se_desc"], "|", sorted(header))
print(len(header["rdb_hdr"]), "of", len(dl.HEADER_FIELDS), "allowlisted fields are under rdb_hdr")MRSI 3D Rolf fidall2 | ['exam', 'image', 'mrconfig', 'rdb_hdr', 'series']
16 of 27 allowlisted fields are under rdb_hdr
RawMRISeries.header is exactly this. Its recon dict (everything else in the file, h pruned to
the same fields) loads on first access. To read a field that isn’t allowlisted, pass
fields=[...] here or use load_mat_file for the full header; a new RawMRISeries header
property needs its field added to HEADER_FIELDS.
When the path isn’t a real .mat file¶
| Symptom | Cause | Fix |
|---|---|---|
FileNotFoundError | path doesn’t exist | double-check the series ID / get_mat_data_from_series result |
ValueError: ...does not have a .mat extension | wrong file passed | load_mat_file only accepts .mat |
RuntimeError: mat73 is required... | v7.3 file, but mat73 isn’t installed | mat73 is a core dependency (see pyproject.toml) — reinstall the env |