Skip to main content

medh5

PyPI version Python versions License: MIT CI Documentation Typed Code style: ruff

One medical imaging sample — a subject, at every timepoint, with all of its ground truth — in a single self-describing HDF5 file.

Multi-modality images, segmentation in five encodings, detection boxes, keypoints, contours, meshes, classification, registration between visits, provenance and quality records, and per-object integrity digests — and, from format 1.1, the clinical history around the images, with when each fact became known, so a model trained at a cutoff reads only what was available then. Format versions 1.0 and 1.1, with normative specifications and a 163-case conformance suite any implementation can run.

import medh5

# A subject with two visits: images, segmentation, registration, tracking.
with medh5.open("subject.medh5") as s:
    s.identity.subject_id                              # "BRATS-GLI-01234"
    s.at("tp1").images["CT_tp1"].read(physical=True)   # HU, not raw counts
    s.annotations["organs"].dense(["liver", "spleen"]) # any encoding, one API
    s.transform_between("tp0", "tp1")                  # resolved via frames
    s.tracks("lesion")                                 # lesions joined across visits

Install

pip install medh5

That is all most people need: the Python package and the medh5 command line, enough to create, read, validate and inspect files. HDF5 and its compression filters are built in, so NumPy is the only other dependency. pip downloads it ready-built for Python 3.10 and later on Linux (x86_64, ARM64), macOS (Intel, Apple silicon) and Windows (x64).

Add an extra for each tool you connect medh5 to:

To… Install
train with PyTorch pip install "medh5[torch]"
load samples into MONAI pip install "medh5[monai]"
import or export NIfTI and nnU-Net pip install "medh5[nifti]"
import DICOM series, SEG and RTSTRUCT pip install "medh5[dicom]"
export DICOM SEG pip install "medh5[dicomseg]"
open files directly with h5py pip install "medh5[h5py]"

Extras combine: pip install "medh5[torch,nifti,dicom]".

Only the command line, without Python? brew install XwK-P/medh5/medh5, or download it from the latest release. Converting DICOM or NIfTI files still needs the Python package.

Writing Rust? cargo add medh5 adds the format engine itself. The Python package and the command line run the same code, so all three read and write the same bytes. Building it compiles HDF5, which needs Rust 1.85.1 or later, a C compiler and CMake.

Other systems, building from source, the 1.x line and troubleshooting are on the install page.

Documentation

medh5.readthedocs.io — tutorials, how-to guides, the Python and CLI reference, and the normative specifications.

Write your first sample · How-to guides · Python API · CLI · Specification 1.0, 1.1 and the task and cache contract

What the format is for

  • One file per subject, not per scan — every visit in one place, so longitudinal work has a referent and splitting by file cannot leak a patient.
  • Geometry is stated once and never guessed — declared grids, boxes at voxel edges, and converters that refuse rather than invent.
  • Absence is not silence — a class examined and not found is recorded as such, which is a different training signal from one nobody examined.
  • Every claim is checkable — per-object digests, a Merkle content_id that survives recompression, a stable diagnostic-code table, and a 163-case conformance corpus.
  • Reading a patch is fast — a 64³ multi-class patch in ~2.3 ms, and O(1) foreground sampling once build_index() has run.
  • What was known, when — labs, reports and their revisions, diagnoses and assessments on one subject clock, each with when it happened and when it became available; strict prospective selection decides what a row may read, and tasks and feature caches are pinned to the sample versions they read.

The reasoning behind each.

Write a sample

import numpy as np
import medh5
from medh5 import LabelClass, LabelSet

# Stand-ins for a real CT and its masks.
ct = np.random.default_rng(0).integers(-1000, 1500, (64, 96, 96)).astype(np.int16)
liver = np.zeros(ct.shape, bool); liver[10:40, 20:70, 20:70] = True
lesion = np.zeros(ct.shape, bool); lesion[20:26, 35:45, 35:45] = True

labels = LabelSet("demo-v1", version="1.0.0", classes=[
    LabelClass(1, "liver", "Liver", category="organ"),
    LabelClass(2, "spleen", "Spleen", category="organ"),
    LabelClass(3, "lesion", "Lesion", parents=[1], category="lesion"),
])

with medh5.create("case_0001.medh5", sample_id="case_0001",
                  subject_id="DEMO-0001") as w:
    w.label_set(labels)
    w.add_timepoint("tp0", label="baseline", days_from_baseline=0)
    w.add_grid("ct", shape=ct.shape, spacing=(2.0, 0.8, 0.8),
               origin=(-64.0, -38.4, -38.4), timepoint="tp0")
    w.add_image("CT", ct, grid="ct", modality="CT",
                value_type="quantitative", value_units="HU")
    w.add_segmentation("organs", grid="ct",
                       masks={"liver": liver, "lesion": lesion},
                       annotated_classes=["liver", "spleen", "lesion"])
    w.build_index()   # optional; foreground sampling is O(1) only with it

annotated_classes names the spleen although there is no spleen mask: that records "we looked and found none". The encoding is chosen by measuring the class overlap graph — liver and lesion overlap, so it picks one that can represent that — and the write is atomic.

Train on it

from torch.utils.data import DataLoader
from medh5.torch import PatchDataset, collate, worker_init_fn
from medh5.sampling import PatchSampler

sampler = PatchSampler((96, 96, 96), strategy="balanced",
                       foreground_classes=["liver", "lesion"])
dataset = PatchDataset(paths, sampler, images=["CT"],
                       annotations={"organs": ["liver", "lesion"]},
                       samples_per_volume=8)

loader = DataLoader(dataset, batch_size=2, num_workers=8,
                    worker_init_fn=worker_init_fn, collate_fn=collate)

worker_init_fn drops handles inherited across a fork. It is recommended rather than required: the handle cache is PID-keyed and re-checks ownership on every access, so a forked worker abandons the parent's handles on first use rather than reading through or closing them.

With a clinical history (format 1.1), a task asks a question at a cutoff, and the batch holds only what was known then — images from the visit available at the cutoff, the report version current at it, missing modalities and censored targets as masks rather than zeros:

from torch.utils.data import DataLoader
from medh5.task import TaskManifest
from medh5.torch import ClinicalTaskDataset, collate_clinical

task = TaskManifest.load("cohort/progression.task.json")
assert task.preflight().ok                  # every pin, clock and duplicate checked
train = ClinicalTaskDataset(task, partition="train")
batch = next(iter(DataLoader(train, batch_size=8, collate_fn=collate_clinical)))
batch["present"]["mr"], batch["events"]["mask"], batch["target"]["observed"]

See clinical history and training on clinical tasks.

Command line

pip install medh5 puts medh5 on the path; so does the standalone binary, which is the same code compiled ahead of time.

medh5 info case.medh5                  # grids, images, annotations, coverage
medh5 validate case.medh5 --level integrity
medh5 verify case.medh5                # digests and content_id
medh5 timeline case.medh5              # visits and intervals
medh5 track case.medh5 --class lesion  # per-lesion volumes across visits

medh5 dataset index studies/ -o cohort.json
medh5 dataset split cohort.json --group-by group_id --stratify-by site_id --write-claims
medh5 dataset index studies/ -o cohort.json  # record the claims just written
medh5 dataset stats cohort.json --partition train --workers 8
medh5 dataset check cohort.json --deep

medh5 convert from-dicom /studies out/     # one sample per patient, all visits
medh5 convert from-nifti case.medh5 --image CT=ct.nii.gz
medh5 convert from-rtstruct plan.dcm case.medh5 --rasterize
medh5 migrate old/*.medh5 -o new/ --group-by subject --subject-key extra.patient_id

medh5 scrub out/*.medh5 --apply --date-shift-days -117
medh5 pack cohort/*.medh5 -o shard.medh5c
medh5 recompress cohort/*.medh5 --profile training
medh5 bench                                # reproduce the performance targets
medh5 conformance publish suite/           # the suite, for another implementation

medh5 clinical select case.medh5 --cutoff-hours 24    # what was known then (1.1)
medh5 task preflight progression.task.json           # every row: in or out, and why
medh5 cache validate reports.medh5cache --task progression.task.json

Interoperability

Format
NIfTI affine and voxels bit-identical on round trip; RAS↔LPS is a sign flip, never a resample
DICOM slices ordered by geometry, spacing measured between origins, modality LUT stored not applied, tags on an explicit allow-list; slices that disagree about orientation, spacing or rescale are refused rather than read off the first one
DICOM SEG frames placed by geometry; segments matched by label, not number; import preserves overlap and FRACTIONAL, export writes BINARY
RTSTRUCT contours stay contours; rasterisation is opt-in and recorded in provenance
nnU-Net v2 class ids kept; region labels become label-set DAG parents; dataset.json round-trips
MONAI to_metatensor gives a MetaTensor with the correct affine
0.x medh5 migrate, reporting every decision and every guess

Every conversion returns a report distinguishing what it decided from the data and where it guessed — the encoding chosen, the class ids minted, a half-voxel convention changed, a timepoint order inferred rather than read.

COCO is deliberately unsupported: it has no world geometry, spacing or frame of reference, so importing means inventing a grid and exporting means discarding the geometry that makes a medical annotation reproducible.

Reading it without medh5

import h5py, json, hdf5plugin       # pip install "medh5[h5py]"; hdf5plugin for Blosc2

with h5py.File("case_0001.medh5") as f:
    doc = json.loads(f["meta"][()])
    doc["identity"]["subject_id"]
    dict(f["grids"]["ct"].attrs)     # spacing, origin, direction
    f["images"]["CT"][10:20]

medh5 recompress --profile portable writes gzip, readable by any HDF5 build.

Versioning

The format is 1.1: 1.0 plus the optional clinical profile, and nothing else. A sample is written at the lowest version its content needs, so imaging-only data is still 1.0, which 1.x reads; a reader opens a later minor as a projection, never amending it. A minor version may add optional objects, profiles, encodings and diagnostic codes; it may not change what an existing one means (spec §16). The package follows semantic versioning from 1.0.0: 2.0 moved the implementation to the Rust engine and changed the Python API at the HDF5 boundary (what changed).

0.x files are not readable by 1.0 and are not meant to be — medh5 migrate converts them once. See Migrate from 0.x.

Contributing

See CONTRIBUTING.md for the development setup, the checks a change has to pass, and how the specification and documentation are kept in step with the code. Release notes are in CHANGELOG.md.

License

MIT

Metadata

Release files for medh5 2.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for medh5 2.0.3
File Size Uploaded
medh5-2.0.3.tar.gz 2.3 MB Details

Built distributions (wheels)

Table of built distributions (wheels) for medh5 2.0.3
File
medh5-2.0.3-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
medh5-2.0.3-cp310-abi3-manylinux_2_28_x86_64.whl CPython 3.10 abi3 Linux glibc 2.28+ x86-64 Details
medh5-2.0.3-cp310-abi3-manylinux_2_28_aarch64.whl CPython 3.10 abi3 Linux glibc 2.28+ ARM64 Details
medh5-2.0.3-cp310-abi3-macosx_11_0_x86_64.whl CPython 3.10 abi3 macOS 11.0+ x86-64 Details
medh5-2.0.3-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details

Total release size: 52.9 MB

Release files / medh5-2.0.3.tar.gz

Download URL medh5-2.0.3.tar.gz
Size 2.3 MB
Tags Source
SHA-256 checksum
How to use checksums
1c13033327e1de938261971f2bad12c4f842e00d3586b1fea05084f76de86634
BLAKE2b-256 checksum
How to use checksums
dc9b6a28e8cbdfc9e967864d6bdaea339d18ccd6f4fbb96fa381b9b1e53c794d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / medh5-2.0.3-cp310-abi3-win_amd64.whl

Download URL medh5-2.0.3-cp310-abi3-win_amd64.whl
Size 11.2 MB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
b471dce9f5976c56d659a75dd652f6596c2a352b754d4dd98c7732f2e1244ba7
BLAKE2b-256 checksum
How to use checksums
8f1d2bff72a44fbe0851c953e7dadccd88a2d0af7b60b9c450bd7c1dca9a22fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / medh5-2.0.3-cp310-abi3-manylinux_2_28_x86_64.whl

Download URL medh5-2.0.3-cp310-abi3-manylinux_2_28_x86_64.whl
Size 10.6 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
d26dceb8c06187d590fe2cafe9e0005ac8df7153d5e42347971b9ba3127e6ae7
BLAKE2b-256 checksum
How to use checksums
26de0a579f089fdf98adaaff22bc13ef61af59446efffa9f5f603206c0993064
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / medh5-2.0.3-cp310-abi3-manylinux_2_28_aarch64.whl

Download URL medh5-2.0.3-cp310-abi3-manylinux_2_28_aarch64.whl
Size 9.8 MB
Tags CPython 3.10 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
8dbe933fc7cc5038a4db55dd1765778089b0937e157496001490ba6bf77895c8
BLAKE2b-256 checksum
How to use checksums
de40a20da3c3dc2e4139c40d08bca582693245669aa05826d47a04348f9c9f7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / medh5-2.0.3-cp310-abi3-macosx_11_0_x86_64.whl

Download URL medh5-2.0.3-cp310-abi3-macosx_11_0_x86_64.whl
Size 10.0 MB
Tags CPython 3.10 abi3 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
173ef1636faa7f10d1fb9d9c8bb7b3658708a956e3c6dfdadb7d5c1938a338f0
BLAKE2b-256 checksum
How to use checksums
0a7c10505717819108a9977586f268b9f78247923d02768922b3034e41bb29d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release files / medh5-2.0.3-cp310-abi3-macosx_11_0_arm64.whl

Download URL medh5-2.0.3-cp310-abi3-macosx_11_0_arm64.whl
Size 9.1 MB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
bd6e2f23b77e82af9586e452938fd90314384f8600dcdba883e16d18c2e7ad73
BLAKE2b-256 checksum
How to use checksums
128366e48461cbf4801f5d0e55deae2c5db228e4588fff091485c5583308bf58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 14, 2026.

Transparency log

Release history Release notifications | RSS feed

2.1.0

11 release files

This release

2.0.3 This release

6 release files

2.0.2

6 release files

2.0.1

6 release files

2.0.0

6 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page