How maxkunev/dicom works
Purpose and entry points
This is a Python DICOM de-identification gateway: it accepts DICOM uploads, anonymizes identifiers and dates, persists normalized imaging metadata, stores a sanitized DICOM file, and returns patient-centric imaging metadata through REST. The ASGI application is rooted in app/main.py; DICOM endpoints are organized in app/routers/dicoms.py.
HTTP/API boundary
POST /dicom/ receives multipart .dcm uploads and orchestrates validation, transformation, persistence, and file output. GET /dicom/{anon_patient_id} traverses persisted de-identified records into a nested FHIR-inspired response. The router boundary is app/routers/dicoms.py, registered by app/main.py; the intended runtime is Uvicorn/FastAPI.
DICOM ingestion and validation
Raw uploads cross from untrusted file input into pydicom parsing. The parsing stage rejects unreadable or incomplete inputs and requires study, series, and SOP instance identifiers before downstream writes. DICOM-specific pipeline logic resides in app/dicom_scripts.py; stored sanitized DICOMs are placed under app/dicom/dicom_filed.py / the app/dicom/ storage area.
De-identification and consistency layer
The core privacy boundary transforms raw DICOM metadata before durable storage. It assigns a generated anonymous patient identity, replaces patient name, maps original Study/Series/SOP UIDs to stable anonymous UIDs, applies a stable per-patient random date shift, and removes private/identifying tags. This is implemented centrally in app/dicom_scripts.py, with persistent mapping state represented in app/models.py.
Relational metadata model
SQLAlchemy models normalize the imaging hierarchy: Patient → Study → Series → Instance, supplemented by UID mapping and date-shift records. This enables repeated uploads from the same source patient or study to preserve anonymized references and lets the GET endpoint reconstruct a nested response without rereading DICOM files. Model definitions are in app/models.py; engine/session setup is in app/database.py.
Persistence flow and transaction boundary
After de-identification, the service writes two durable outputs: relational metadata through SQLAlchemy and the scrubbed DICOM artifact to filesystem storage. Database schema evolution is managed with Alembic from alembic/env.py and initial migration alembic/versions/fbe9720b0617_init_commit.py. The prototype uses local SQLite (mydb.db), so database and file storage are local-process infrastructure rather than external services.
Operational constraints
Material dependencies are FastAPI/ASGI, pydicom, SQLAlchemy, Alembic, SQLite, and Uvicorn, declared in requirements.txt. This is explicitly an educational, local-oriented prototype: no container deployment, CI, validated FHIR serialization, or clinical-grade DICOM anonymization guarantees are present. Sample valid and invalid DICOM inputs under samples/ support manual ingestion verification.
Main entry points: dicoms.py DICOM router · models.py Imaging metadata model · dicom_scripts.py Validation & de-identification · main.py FastAPI application
Components 14
ASGI API
- Uvicorn
requirements.txt - FastAPI application
app/main.py - DICOM router · Handles POST /dicom/ and GET /dicom/{anon_patient_id}.
app/routers/dicoms.py - Patient imaging response · Nested metadata reconstructed from persisted records.
app/routers/dicoms.py
DICOM pipeline
- DICOM upload · Multipart .dcm input.
- Validation & de-identification · Parses, validates, anonymizes tags, shifts dates, and maps UIDs.
app/dicom_scripts.py - pydicom
requirements.txt - Sample DICOM files · Valid and invalid ingestion samples.
Local persistence
- Imaging metadata model · Patient → Study → Series → Instance plus UID and date-shift mappings.
app/models.py - Database session
app/database.py - SQLite metadata DB · Prototype database stored in mydb.db.
- Sanitized DICOM storage · Stores scrubbed DICOM artifacts.
app/dicom/dicom_filed.py - Alembic migration environment
alembic/env.py - Initial schema migration
alembic/versions/fbe9720b0617_init_commit.py
Connections 15
- Uvicorn → FastAPI application: serves
- FastAPI application → DICOM router: registers
- DICOM upload → DICOM router: POST /dicom/
- DICOM router → Validation & de-identification: ingests
- Validation & de-identification → pydicom: parses
- Validation & de-identification → Imaging metadata model: creates normalized records
- Imaging metadata model → Database session: uses
- Database session → SQLite metadata DB: persists
- Validation & de-identification → Sanitized DICOM storage: writes sanitized file
- DICOM router → Imaging metadata model: queries by anonymous patient
- Imaging metadata model → Patient imaging response: reconstructs hierarchy
- DICOM router → Patient imaging response: GET /dicom/{anon_patient_id}
- Alembic migration environment → Imaging metadata model: loads metadata
- Initial schema migration → SQLite metadata DB: creates schema
- Sample DICOM files → DICOM upload: manual ingestion