Skip to main content

dicompare

dicompare is an open-source, vendor-independent tool for automated validation and comparison of MRI acquisition protocols using DICOM metadata. It enables researchers to determine whether imaging protocols implemented at different sites are truly equivalent, similar, or meaningfully different — a task that is currently manual, error-prone, and often infeasible in large, multi-site studies.

dicompare is a collaboration between the Neurodesk and Brainlife groups.

This repository contains the core Python package, which provides a command-line interface (CLI) and Python API for building, validating, and matching protocol schemas.

For the visual web and desktop application, see dicompare-web or use the live app at dicompare.neurodesk.org or brainlife.io/dicompare.


What dicompare Does

dicompare performs structured comparisons of DICOM files to evaluate whether imaging protocols match a target reference or schema. It works directly with DICOM metadata and does not depend on scanner manufacturer formats or proprietary exam card systems.

dicompare supports validation against:

  • Reference sessions — JSON schema files generated from a reference MRI scanning session
  • Domain guidelines — Flexible guidelines for specific domains (e.g. QSM, ASL, MS/CMSC)
  • Landmark studies — A bundled schema library with protocols from HCP, ABCD, UK Biobank, and more

Installation

pip install dicompare

Alternatively, use the web app or desktop app for a visual interface with no installation required.

Command-line interface (CLI)

The package provides a unified dicompare command with four subcommands:

  • dicompare build: Generate a JSON schema from a reference DICOM session
  • dicompare check: Validate DICOM sessions against a JSON schema
  • dicompare match: Find best-matching schemas for input DICOM data from a library
  • dicompare lint: Check a schema for problems before sharing or submitting it

1. Build a JSON schema from a reference session

dicompare build /path/to/dicom/session schema.json

This creates a JSON schema describing the session based on default reference fields present in the data.

2. Check a DICOM session against a schema

dicompare check /path/to/dicom/session schema.json

The tool will output an acquisition mapping summary with confidence scores, followed by a compliance report indicating deviations from the schema. Use --auto-yes or -y to skip interactive mapping prompts:

dicompare check /path/to/dicom/session schema.json --auto-yes

Save the compliance report to a JSON file by specifying a report path:

dicompare check /path/to/dicom/session schema.json compliance_report.json

The report lists every check with the reference and input acquisition it belongs to and the mapping's cost and confidence, plus a row for each reference acquisition that could not be mapped.

dicompare check exits with status 1 if any check fails or a reference acquisition is unmapped, and 0 otherwise, so scripts and pipelines can stop on a non-compliant session. Warnings do not change the exit status.

3. Find best-matching schemas for your data

Search across a schema library to identify which protocols best match your DICOM data:

# Search the bundled schema library
dicompare match /path/to/dicom/session --library

# Search custom schema files or directories
dicompare match /path/to/dicom/session --schemas /path/to/schemas/

# Combine bundled library and custom schemas
dicompare match /path/to/dicom/session --library --schemas /path/to/custom_schema.json

This compares each input acquisition against every acquisition in the loaded schemas, ranking matches by compliance score. Options:

  • --library: Include the bundled schema library (HCP, ABCD, UK Biobank, and more)
  • --schemas PATH [PATH ...]: Path(s) to schema files or directories containing schemas
  • --top N: Number of top matches to show per acquisition (default: 5)
  • --report PATH: Save the match report to a JSON file

4. Lint a schema

dicompare lint schema.json

Checks a schema against the dicompare metaschema and best practices:

  • field constraints are compared against the canonical field registry (console display strings like A >> P, raw vendor codes, and out-of-vocabulary values will never match real DICOM data)
  • exact matches on continuous physical parameters (e.g. EchoTime) are flagged as brittle
  • rule structure is verified (valid Python, unique rule/test ids, no reads of undeclared fields)
  • every rule test case is executed through the same validation path used in production — including the check that a schema's own example values pass its own rules

Errors exit non-zero (suitable as a CI gate — the dicompare-web schema-submission workflow runs this on every community submission); warnings are informational. Use --format json or --format markdown for machine-readable output.

Python API

The dicompare package provides a comprehensive Python API for programmatic schema generation, validation, and DICOM processing.

New to the Python API? Start with the tutorial notebook Open in Colab. It builds a schema from one synthetic site, checks a second site that has drifted from it, plots the results, and shows the command-line equivalent for pipelines.

Loading DICOM data

Load a DICOM session:

from dicompare import load_dicom_session

session_df = load_dicom_session(
    session_dir="/path/to/dicom/session",
    show_progress=True
)

Load individual DICOM files:

from dicompare import load_dicom

dicom_data = load_dicom(
    dicom_paths=["/path/to/file1.dcm", "/path/to/file2.dcm"],
    show_progress=True
)

Load vendor protocol files:

Every supported protocol format has a matching importer family (load_<source>_file, load_<source>_file_schema_format, and where applicable load_<source>_session):

from dicompare import (
    load_pro_file,         # Siemens .pro (raw MrPhoenixProtocol)
    load_exar_file,        # Siemens .exar1 exports
    load_printprot_file,   # Siemens "MR print protocol" (XML/TXT console export)
    load_examcard_file,    # Philips ExamCard
    load_lxprotocol_file,  # GE LxProtocol
)

protocols = load_printprot_file("AxonDiameterProtocol.txt")

All importers translate vendor-specific values (raw enum codes, console display strings, unit conventions) to the canonical DICOM vocabulary via the field registry, so a schema built from any source validates cleanly against real DICOM data. Diffusion gradient files (.dvs, .bvec/.bval) can be attached to derive shell descriptors (DiffusionBValues, DirectionsPerShell, ...).

Build a JSON schema

from dicompare import load_dicom_session, build_schema, save_schema

# Load the reference session
session_df = load_dicom_session(
    session_dir="/path/to/dicom/session",
    show_progress=True
)

# Build the schema
json_schema = build_schema(session_df)

# Save the schema
save_schema(json_schema, "schema.json")

Validate a session against a JSON schema

from dicompare import check_session, map_session, save_report

# Optional: see which input acquisition maps to each schema acquisition
print(map_session("/path/to/dicom/session", "schema.json"))

# Map the session's acquisitions to the schema and check each one
results = check_session("/path/to/dicom/session", "schema.json")

# One row per check, plus a row for each reference acquisition left unmapped
print(results[["reference_acquisition", "input_acquisition", "mapping_confidence",
               "field", "status", "message"]])
failed = results[~results["passed"]]

# Save the same JSON report as `dicompare check --report`
save_report(results, "report.json")

check_session also accepts a session DataFrame (from load_dicom_session) and a schema dict, and takes mapping={"RefAcq": "InputAcq", ...} to set the acquisition mapping instead of finding it automatically.

To try it without data of your own, dicompare.examples.write_multisite_example() writes two small synthetic sessions, one of which deviates from the other.

The canonical field registry

dicompare.fields is the single source of truth for per-field knowledge: canonical DICOM keywords, tags, value types, units, allowed vocabularies, suggested tolerances, and vendor encodings (e.g. Siemens ucCoilCombineMode code 2 means "Adaptive Combine"). The protocol importers, the schema lint, and the dicompare-web schema editor all consume it, so field semantics cannot drift between them.

from dicompare import get_field, check_value, validate_fields

get_field("InPlanePhaseEncodingDirection").vocabulary   # ('ROW', 'COL')
check_value("CoilCombinationMethod", 2)                  # -> [".. not in the canonical vocabulary .."]
validate_fields({"InPlanePhaseEncodingDirection": "A >> P"})  # flags display strings

Export the registry as JSON (consumed by the web schema editor) with python -m dicompare.fields.

Lint a schema programmatically

import json
from dicompare import lint_schema, format_findings

schema = json.load(open("schema.json"))
findings = lint_schema(schema)
print(format_findings(findings, "markdown"))
errors = [f for f in findings if f.severity == "error"]

Writing validation rules

Schema rules are Python snippets executed in a sandbox. The preferred interface is the ctx object, which provides typed field access and collected severities:

# Inside a rule's "implementation":
for row in ctx.rows:
    bvals = row["DiffusionBValues"]        # plain list, no parsing needed
    if len([b for b in bvals if b > 0]) < 2:
        ctx.error("At least two non-zero b-value shells are required")
    elif len(bvals) < 4:
        ctx.warn("Four or more b-values are recommended")

ctx.error(...) findings fail validation; ctx.warn(...) findings surface as warnings. The legacy interface (a pandas DataFrame named value, with ValidationError / ValidationWarning raised explicitly) remains fully supported. Every rule should ship testCases — dicompare lint executes them through the production validation path.

Additional utilities

Assign acquisition and run numbers:

from dicompare import assign_acquisition_and_run_numbers

session_df = assign_acquisition_and_run_numbers(session_df)

Get DICOM tag information:

from dicompare import get_tag_info, get_all_tags_in_dataset

# Get info about a specific tag
tag_info = get_tag_info("EchoTime")
print(tag_info)  # {'tag': '(0018,0081)', 'name': 'Echo Time', 'type': 'float'}

# Get all tags in a dataset
all_tags = get_all_tags_in_dataset(dicom_metadata)

Metadata

Release files for dicompare 0.12.0

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

Source distribution (sdist)

Source distribution for dicompare 0.12.0
File Size Uploaded Provenance Actions
dicompare-0.12.0.tar.gz 369.4 kB Without provenance Details for dicompare-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dicompare 0.12.0
File Interpreter ABI Platform Actions
dicompare-0.12.0-py3-none-any.whl Python 3 none any Details for dicompare-0.12.0-py3-none-any.whl

Total release size: 778.2 kB

Release files / dicompare-0.12.0.tar.gz

Download URL dicompare-0.12.0.tar.gz
Size 369.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0dbed57af798fbc0edc6ea4247acc6989ea7f6bde14bd07b3bc052382d540634
BLAKE2b-256 checksum
How to use checksums
385fcb1aef630d4bab126c12f41ce0af8ea928454a7ee2e327f8dd8ba637d138
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / dicompare-0.12.0-py3-none-any.whl

Download URL dicompare-0.12.0-py3-none-any.whl
Size 408.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c5c471dbbf6da18263c6f9f58e54261d99c8294aca0d5d0f948b494c3eede8f
BLAKE2b-256 checksum
How to use checksums
c993c35910248a8c623458f1bae1aa8f23d89c8e5e2a47656c1e9c541475545d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Security for dicompare 0.12.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files No provenance

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