Skip to main content

dichotomise Version License CI

DIChOtoMise is a command-line tool for checking, sorting, renaming, de-identifying, and archiving DICOM exports from Siemens XA60+ systems.

Table of contents


The default export

For a given study, the XA60+ export places DICOMs from each series into sub-folders named:

<ProtocolName>_<SeriesNumber>_MR/

The DICOMs inside are named 1.dcm, 2.dcm, and so on. The numbering starts again in every series folder and recurs across subject exports.

subject-export/
  DWI_21_MR/
    1.dcm
    2.dcm
  fMRI_22_MR/
    1.dcm
    2.dcm

1.dcm in one series may be entirely unrelated to 1.dcm in another. The files can contain different acquisitions and have different file sizes.

Why repeated filenames are positively dreadful

TL;DR

Naming files 1.dcm, 2.dcm, 3.dcm is the worst possible choice. It is a regression from something that previously worked well.

Export File naming
Older exports Distinct .IMA filenames
XA30 exports Distinct .dcm filenames
XA60+ exports 1.dcm, 2.dcm, … repeated in every series folder

Every DICOM carries a globally unique SOP Instance UID. The XA60 export retains that identifier in the header but does not use it to distinguish the exported filename. The file's name identifies it only while its surrounding folder structure remains intact. That is a data-integrity problem at the point of export, before a researcher runs a pipeline or moves a file.

FAIR principle F1 calls for globally unique, persistent identifiers for data. The DICOM header supplies one; the exported filename hides it from ordinary file operations. BIDS takes the opposite approach to filenames: its applicable entities identify the data within the filename itself.

The new export format is the antithesis of good data-handling practice

  1. Give each file a usable identity. XA60+ assigns the same name to unrelated DICOMs. A file separated from its folder cannot be identified by name, although its SOP Instance UID remains in its header. This undermines findability at the file-system level and makes safe handling depend on reading DICOM metadata every time.

  2. Keep the file and its context in agreement. The export uses a series folder to provide context that the filename lacks. If that folder contradicts the DICOM header, the name offers no independent clue.

    Example Incident 1 shows that this disagreement occurred in a single export with no user intervention.

  3. Make collisions visible before they cost data. When exports are combined, flattened or restored into one directory, repeated names collide. Depending on the operation and its settings, a collision can overwrite a file or stop the transfer. Either way, the filename cannot distinguish the DICOMs involved. Safe reuse demands an explicit identity check, not trust in the exported names.

  4. Preserve the evidence needed to audit and reuse data. Matching filenames do not establish that two DICOMs contain the same instance or the same scan content. Counters alone cannot verify completeness, detect a misplaced instance or establish where a file came from. FAIR R1.2 calls for detailed provenance; losing a file's folder context makes that provenance harder to recover from the export, even though metadata remains in the DICOM.

    Example Incident 2 shows why a plausible filename sequence cannot serve as an audit.

Two example incidents (from amongst several)

We noticed inconsistencies in scanner exports and inspected the DICOM metadata manually. Both incidents below were present before anyone copied, moved or processed the files. They are examples of the intermittent, inconsistent errors we have encountered, not an exhaustive list. These occur in product sequences and C2Ps alike.

Incident 1: DICOM placed in the wrong series

300.dcm appeared in the folder for Series 21. We checked its DICOM Series Number and Instance UID and found that the file belonged to Series 22. The export's folder contradicted the file's metadata.

Incident 2: Interleaved DWI duplication

We expected 81 DICOMs in a DWI series, but the export contained 162. Manual inspection found that duplicates were randomly interleaved with the original files, rather than appended as a second sequence. The filename range and file count alone could not show which files were duplicated.

dichotomise to the rescue

dichotomise addresses this failure at ingestion. It reads identity from DICOM metadata rather than trusting exported filenames or series folders. It preserves the original files, audits for duplicate content and inconsistent placement or metadata, separates files needing review, and writes clearly named, verified output.

The tool is intended for any DICOM export that needs structured auditing, sorting and validation. Its immediate motivation is the XA60+ export: a globally unique identifier is already inside each DICOM, yet the scanner hands researchers filenames that cannot distinguish one file from another outside a fragile folder hierarchy.

What it does

One command runs the full pipeline, in order:

  1. capture — groups the source directory's DICOM files by (PatientID, StudyInstanceUID) from their own headers, never from folder names. Nothing is copied yet; every file stays exactly where the export left it.
  2. source_archive — archives each subject's untouched DICOM files, straight from the source directory, with a checksum — before anything else is read from or written to them, so a later copy can never silently diverge from what this archive preserves.
  3. audit — flags duplicate scan content (by comparing everything except each file's own unique ID), files sitting in the wrong series folder, and inconsistent patient, study, or series metadata within a physical DICOM folder. The CLI prints a Rich inventory table, and the reports include a spreadsheet-friendly CSV.
  4. sift — splits files into retained and review, logically, before the final archive is ever built, so a flagged file is never silently included in the archive you share. This is a classification step only (no files move yet); there is no need to delete and re-export a whole series to exclude one bad file — sift already excludes it, and the untouched source archive (in archives/) remains the reference if you ever need the original file back.
  5. rectify — the pipeline's one copy of each file's bytes: it reads retained files straight from the source directory and writes them into a sorted, clearly named tree: <series number>-<series description>/<series>_<series ID>_<instance>_e<echo>.dcm. Review files are renamed the same way (surfacing what a misfiled or duplicate file's own metadata claims, even when that metadata is wrong), and are then always archived with a checksum into archives/ as their own review-archive, independent of --keep-working-files.
  6. sanitise (optional, --sanitise) — replaces patient identity according to a chosen policy; see Sanitisation below. When used, the review-archive is de-identified with the same policy and the same replacement identifiers as the main archive, so a sanitised run never leaks real PHI through its review files either.
  7. finalise — independently re-reads the finished output tree (not a cached record from earlier stages) to catch any corruption introduced by copying or renaming, then archives it with a checksum.

This is a from-scratch, simplified rewrite of the original dichotomise, aimed at being easy to read, debug, and extend, including for someone new to Python. It implements a single end-to-end pipeline; there is no separate expert/stage-by-stage command.

Installation

dichotomise requires Python 3.11+ and uv.

If you already have Python and pip:

python -m pip install --user uv

Otherwise, install uv directly:

curl -LsSf https://astral.sh/uv/install.sh | sh

Install from PyPI (after release)

uv tool install --python 3.11 dichotomise
dichotomise --help

This creates an isolated environment for dichotomise. If Python 3.11 is not available, uv downloads it automatically.

Install from a source checkout

git clone https://github.com/srikash/dichotomise.git
cd dichotomise
uv venv --python 3.11
uv sync --locked
uv run dichotomise --help

Container images

Every tagged release also publishes a Docker image and an Apptainer/Singularity image to GHCR — useful for HPC clusters and containerized pipelines where installing Python packages directly isn't an option.

# Docker
docker run --rm -v "$PWD:/data" ghcr.io/srikash/dichotomise:2.6.0 --help

# Apptainer / Singularity
apptainer run oras://ghcr.io/srikash/dichotomise:2.6.0-sif --help

Mount your source and output directories under /data (the image's working directory) so --source-dir/--out-dir paths resolve inside the container. :latest tracks the most recent release; pin a version tag for reproducible runs.

scripts/dichotomise-container wraps this so you don't have to think about image references or mounts: it takes the exact same options as native dichotomise (its --help output is byte-identical, since it delegates straight into the containerized CLI), auto-detects docker or apptainer, and rewrites --source-dir/--out-dir/ --mapping-file into the right bind mounts for you.

./scripts/dichotomise-container --source-dir ./study/sub-001 --out-dir ./dichotomise-runs

Set DICHOTOMISE_ENGINE=docker|apptainer to force an engine, and DICHOTOMISE_VERSION=2.6.0 (default latest) to pin a release.

Usage

dichotomise --source-dir ./study/sub-001 --out-dir ./dichotomise-runs

To inspect an export without creating an output directory or saving a report:

dichotomise --qc --source-dir ./study/sub-001

--qc reads the DICOMs in place and prints the Rich audit table in the terminal. It never copies, changes, archives, or writes files.

--source-dir accepts either one subject/session export or a scanner export containing multiple subjects — they are discovered from DICOM metadata, so folder names do not need to be sensible.

Flag Purpose
--source-dir (required) Raw DICOM directory to process.
--out-dir Parent directory for the timestamped output folder. Required unless --qc is used.
--qc Audit the source DICOMs in place and print tables only. Cannot be combined with processing flags.
--sanitise Replace patient identity before final archiving. This uses minimal unless a policy is chosen.
--sanitise-policy JSON policy name: minimal (the default), standard, full, retain, custom, or a policy you add yourself. Both custom and custom.json are accepted. Implies --sanitise.
--subj-id First numerical replacement ID. For one study, 6 produces sub-0006. With several studies, IDs are enumerated automatically as sub-0006, sub-0007, sub-0008, and so on; the CLI logs a warning.
--new-id Replacement ID for one study. ADNC0751 becomes sub-ADNC0751; an existing sub- prefix is retained.
--random-name Generate a random replacement name using the minimal sanitisation policy.
--mapping One or more PatientID:replacement_id pairs. Repeat the flag or separate pairs with commas. The PatientID must exactly match the DICOM PatientID.
--mapping-file JSON object mapping DICOM PatientID to replacement ID, for example {"source-01": "sub-0001"}. A full path may be given with or without the .json suffix.
--keep-working-files Keep the processed DICOM files written during rectify/sanitise (working/) instead of deleting them once the archives are verified.
--keep-unzipped Also keep each study's final (optionally sanitised) files unarchived, in final/ — independent of --keep-working-files — for a later BIDS conversion or other tooling that wants plain files rather than a tarball.

For one study, use --subj-id, --random-name, or --new-id. For several studies, use --subj-id (automatic enumeration), --random-name, --mapping, or --mapping-file; --new-id remains intentionally limited to one study.

docs/example-mapping.json is a ready-to-copy mapping-file example. Its keys must match the source DICOM PatientID values; its values are the exact replacement labels to write. Its instructions field is ignored by the CLI.

Output structure

Every run creates one timestamped, UTC output folder beneath --out-dir:

<run-timestamp>_dichotomise_outputs/
  archives/
    <patient-id>_<6char-hex>_source-archive_<run-timestamp>.tar.gz
    <patient-id>_<6char-hex>_source-archive_<run-timestamp>.sha256
    <subject-label>_<6char-hex>_dichotomised-archive_<run-timestamp>.tar.gz
    <subject-label>_<6char-hex>_dichotomised-archive_<run-timestamp>.sha256
    <subject-label>_<6char-hex>_review-archive_<run-timestamp>.tar.gz        # only if review files exist
    <subject-label>_<6char-hex>_review-archive_<run-timestamp>.sha256       # only if review files exist
  working/                   # temporary, removed unless --keep-working-files
  final/                      # only with --keep-unzipped
    <subject-label>_<6char-hex>/   # the same tree that archives/*_dichotomised-archive.tar.gz holds, unarchived
  reports/
    <subject-label>_<6char-hex>/
      stage-01-report.json
      stage-01-audit.csv
      stage-02-report.json
      stage-03-report.json
      source-manifest.csv
      retained-manifest.csv
      review-manifest.csv     # only if review files exist
  run-status.json             # in_progress, complete, or failed

run-status.json lets you distinguish a complete result from one left by a failed or interrupted run. It contains no patient details.

A multi-subject run produces one source archive and one dichotomised archive per study, both under archives/. Each source archive name uses the original patient ID plus a random six-character hexadecimal suffix. The source archive contains untouched DICOM files and their original headers, including patient identity; use the sanitised dichotomised archive for sharing.

Processed archive and report names use the study's <subject-label> plus a random six-character hexadecimal suffix. <subject-label> is the real PatientID, unless --sanitise was used, in which case it is the replacement label. A sanitised archive's filename and DICOM headers therefore do not expose the original identifier.

If audit/sift found any duplicate or misfiled files for a subject, they are renamed the same way as the main archive's files and archived separately into archives/ as a review-archive tarball with its own checksum. This archive always survives, independent of --keep-working-files, and is de-identified whenever the run itself is sanitised. When a subject has no review files, no review-archive is created.

Reports

Each study has a directory under reports/ containing:

  • stage-01-report.json — audit totals, duplicate and misfiled folders, and one summary per physical DICOM folder.
  • stage-01-audit.csv — the same per-folder audit summary for spreadsheets, including duplicate/misfiled counts and differing metadata fields.
  • stage-02-report.json — files retained versus copied to review/, how many review files needed a disambiguating -dupNN suffix when renamed (review_rename_collisions), and the name/checksum/sanitised state of that subject's review-archive in archives/ (null/false when there were no review files to archive).
  • stage-03-report.json — archive checksum and a series_inventory with the first original and final DICOM filename for every series.
  • source-manifest.csv / retained-manifest.csv / review-manifest.csv (the last only if review files exist) — one row per file with file, size_bytes, date_modified, and date_created. On Linux, date_created is the filesystem's metadata-change time (st_ctime), not a true creation time, since Linux exposes no creation time via Python's os.stat.

The audit table and CSV flag differences in Patient ID/name, study UID/date/ time, and series UID/number/description/protocol within a DICOM folder.

Compression is always .tar.gz.

Sanitisation

--sanitise applies a named policy — a small JSON file describing what happens to each DICOM field: kept, removed, replaced with a fixed or run-specific value, given a newly generated identifier (consistent across every file for that subject), a birth date scrambled to an approximate but different year, or a randomly generated placeholder name.

Five policies ship in src/dichotomise/pydcm/policies/:

Policy What happens
retain Nothing changed.
standard Identity, patient address, accession number, institution, and device-operator fields removed; every UID reissued; birth date scrambled by ±1 year (day/month randomised too); demographic fields (e.g. sex) and all scan-descriptive text (protocol name, series/study description, etc.) kept.
full Everything standard does, plus scan-descriptive text and the device serial number also removed.
minimal (the default level) Everything standard does, but a generated pseudonym is used for both patient name (Abrahall^Gracious) and patient ID/output name (abrahall_gracious); the birth date is the scan date rather than scrambled.
custom A worked, commented example for building your own — not used automatically.

Full detail — including the real scanner-export comparison these were built from, the policy file schema, and how to write your own — is in docs/sanitise-policies.md. In short: copy custom.json to <your-policy-name>.json in the same folder, edit it, and run with --sanitise-policy <your-policy-name>.

Layout

src/dichotomise/
  cli.py               # the `dichotomise` command (Click + Rich)
  pipeline.py           # runs every stage in order
  run.py                 # output paths for one run
  errors.py              # error types
  stages/                 # one file per pipeline stage
  pydcm/                   # DICOM-specific metadata and sanitisation logic
    policies/                # the sanitisation policy JSON files
    names.py                 # a self-contained adjective+surname placeholder-name generator
  utils/                    # generic filesystem/archive/console helpers

Development

uv run pytest
uv run ruff format .
uv run ruff check .
uv run mypy src

tests/data/ (real, non-synthetic scan exports used for some tests) is gitignored and never committed — it may contain identifying information.

The dichotomise workflow

flowchart TD
    SOURCE["Raw scanner export"] --> CAPTURE["capture<br/>Groups DICOM by subject, in place"]
    CAPTURE --> ARCHIVE["source_archive<br/>Per-study verified tarball + checksum (archives/), straight from the source files"]
    ARCHIVE --> AUDIT["audit<br/>Structural and metadata QA per subject"]
    AUDIT --> REPORT1["stage-01-report.json<br/>stage-01-audit.csv"]
    AUDIT --> SIFT["sift<br/>Splits retained vs review, logically"]
    SIFT --> REPORT2["stage-02-report.json"]
    SIFT --> RECTIFY["rectify<br/>Copies retained files into a metadata-sorted, renamed DICOM tree"]
    SIFT --> REVIEW_RECTIFY["rectify_review<br/>Copies and renames review files the same way"]
    RECTIFY --> SANITISE["sanitise (optional, --sanitise)<br/>Replaces patient identity per policy"]
    REVIEW_RECTIFY --> SANITISE_REVIEW["sanitise_review (optional, --sanitise)<br/>Same policy, shared replacement identifiers"]
    RECTIFY --> FINALISE["finalise<br/>Re-verifies output, archives it (archives/)"]
    SANITISE --> FINALISE
    REVIEW_RECTIFY --> REVIEW_ARCHIVE["finalise_review<br/>Checksummed review-archive.tar.gz (archives/), only if non-empty"]
    SANITISE_REVIEW --> REVIEW_ARCHIVE
    FINALISE --> REPORT3["stage-03-report.json"]
    FINALISE --> ARCHIVES["archives/dichotomised-archive.tar.gz"]

The project is licensed under the MIT License.

Metadata

Release files for dichotomise 2.6.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 dichotomise 2.6.0
File Size Uploaded Provenance Actions
dichotomise-2.6.0.tar.gz 71.9 kB Without provenance Details for dichotomise-2.6.0.tar.gz

Built distribution (wheel)

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

Total release size: 128.6 kB

Release files / dichotomise-2.6.0.tar.gz

Download URL dichotomise-2.6.0.tar.gz
Size 71.9 kB
Tags Source
SHA-256 checksum
How to use checksums
22dd06c5ed588e30d6e5cdb26a3c7dc3051a5c6d896bae0ddb92ca750172b301
BLAKE2b-256 checksum
How to use checksums
ad38799ef61a82121590e87524fb85202bcda914b6445178fabc6dcef48f63cb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / dichotomise-2.6.0-py3-none-any.whl

Download URL dichotomise-2.6.0-py3-none-any.whl
Size 56.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7005f60ba0e9bb78c3bff796f44de622ac8cee416f9447380ee662f316c4bdec
BLAKE2b-256 checksum
How to use checksums
c21f5c09ee0c19f8dfc3a81df797c0975e085d06a33d6b560ffbfad0d4bd6edd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.23 {"installer":{"name":"uv","version":"0.12.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for dichotomise 2.6.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.

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