# maxkunev/dicom architecture

> Architecture diagram of the GitHub repository maxkunev/dicom, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/maxkunev/dicom
- Repository: https://github.com/maxkunev/dicom

## Overview

#### 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: [`app/routers/dicoms.py`](https://github.com/maxkunev/dicom/blob/main/app/routers/dicoms.py) (DICOM router), [`app/models.py`](https://github.com/maxkunev/dicom/blob/main/app/models.py) (Imaging metadata model), [`app/dicom_scripts.py`](https://github.com/maxkunev/dicom/blob/main/app/dicom_scripts.py) (Validation & de-identification), [`app/main.py`](https://github.com/maxkunev/dicom/blob/main/app/main.py) (FastAPI application)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["ASGI API"]
  node_uvicorn{{"Uvicorn<br/>ASGI server<br/>[requirements.txt]"}}
  node_main["FastAPI application<br/>[main.py]"]
  node_router["DICOM router<br/>REST endpoints<br/>[dicoms.py]"]
  node_get_response["Patient imaging response<br/>FHIR-inspired response<br/>[dicoms.py]"]
end

subgraph group_pipeline["DICOM pipeline"]
  node_upload["DICOM upload<br/>untrusted input"]
  node_dicom_logic["Validation &amp; de-identification<br/>DICOM pipeline<br/>[dicom_scripts.py]"]
  node_pydicom["pydicom<br/>DICOM parser<br/>[requirements.txt]"]
  node_samples["Sample DICOM files<br/>manual test inputs"]
end

subgraph group_persistence["Local persistence"]
  node_models["Imaging metadata model<br/>SQLAlchemy models<br/>[models.py]"]
  node_database["Database session<br/>SQLAlchemy setup<br/>[database.py]"]
  node_sqlite[("SQLite metadata DB<br/>local database")]
  node_dicom_storage["Sanitized DICOM storage<br/>local filesystem<br/>[dicom_filed.py]"]
  node_alembic_env["Alembic migration environment<br/>schema migration<br/>[env.py]"]
  node_initial_migration["Initial schema migration<br/>Alembic revision"]
end

node_uvicorn -->|"serves"| node_main
node_main -->|"registers"| node_router
node_upload -->|"POST /dicom/"| node_router
node_router -->|"ingests"| node_dicom_logic
node_dicom_logic -->|"parses"| node_pydicom
node_dicom_logic -->|"creates normalized records"| node_models
node_models -->|"uses"| node_database
node_database -->|"persists"| node_sqlite
node_dicom_logic -->|"writes sanitized file"| node_dicom_storage
node_router -->|"queries by anonymous patient"| node_models
node_models -->|"reconstructs hierarchy"| node_get_response
node_router -->|"GET /dicom/&#123;anon_patient_id&#125;"| node_get_response
node_alembic_env -->|"loads metadata"| node_models
node_initial_migration -->|"creates schema"| node_sqlite
node_samples -.->|"manual ingestion"| node_upload

click node_uvicorn "https://github.com/maxkunev/dicom/blob/main/requirements.txt"
click node_main "https://github.com/maxkunev/dicom/blob/main/app/main.py"
click node_router "https://github.com/maxkunev/dicom/blob/main/app/routers/dicoms.py"
click node_dicom_logic "https://github.com/maxkunev/dicom/blob/main/app/dicom_scripts.py"
click node_pydicom "https://github.com/maxkunev/dicom/blob/main/requirements.txt"
click node_models "https://github.com/maxkunev/dicom/blob/main/app/models.py"
click node_database "https://github.com/maxkunev/dicom/blob/main/app/database.py"
click node_dicom_storage "https://github.com/maxkunev/dicom/blob/main/app/dicom/dicom_filed.py"
click node_get_response "https://github.com/maxkunev/dicom/blob/main/app/routers/dicoms.py"
click node_alembic_env "https://github.com/maxkunev/dicom/blob/main/alembic/env.py"
click node_initial_migration "https://github.com/maxkunev/dicom/blob/main/alembic/versions/fbe9720b0617_init_commit.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_uvicorn,node_main,node_router,node_get_response toneBlue
class node_upload,node_dicom_logic,node_pydicom,node_samples toneAmber
class node_models,node_database,node_sqlite,node_dicom_storage,node_alembic_env,node_initial_migration toneMint
```

## Components

### ASGI API

- [Uvicorn](https://github.com/maxkunev/dicom/blob/main/requirements.txt): ASGI server · `requirements.txt`
- [FastAPI application](https://github.com/maxkunev/dicom/blob/main/app/main.py): application · `app/main.py`
- [DICOM router](https://github.com/maxkunev/dicom/blob/main/app/routers/dicoms.py): REST endpoints · Handles POST /dicom/ and GET /dicom/{anon_patient_id}. · `app/routers/dicoms.py`
- [Patient imaging response](https://github.com/maxkunev/dicom/blob/main/app/routers/dicoms.py): FHIR-inspired response · Nested metadata reconstructed from persisted records. · `app/routers/dicoms.py`

### DICOM pipeline

- **DICOM upload**: untrusted input · Multipart .dcm input.
- [Validation & de-identification](https://github.com/maxkunev/dicom/blob/main/app/dicom_scripts.py): DICOM pipeline · Parses, validates, anonymizes tags, shifts dates, and maps UIDs. · `app/dicom_scripts.py`
- [pydicom](https://github.com/maxkunev/dicom/blob/main/requirements.txt): DICOM parser · `requirements.txt`
- **Sample DICOM files**: manual test inputs · Valid and invalid ingestion samples.

### Local persistence

- [Imaging metadata model](https://github.com/maxkunev/dicom/blob/main/app/models.py): SQLAlchemy models · Patient → Study → Series → Instance plus UID and date-shift mappings. · `app/models.py`
- [Database session](https://github.com/maxkunev/dicom/blob/main/app/database.py): SQLAlchemy setup · `app/database.py`
- **SQLite metadata DB**: local database · Prototype database stored in mydb.db.
- [Sanitized DICOM storage](https://github.com/maxkunev/dicom/blob/main/app/dicom/dicom_filed.py): local filesystem · Stores scrubbed DICOM artifacts. · `app/dicom/dicom_filed.py`
- [Alembic migration environment](https://github.com/maxkunev/dicom/blob/main/alembic/env.py): schema migration · `alembic/env.py`
- [Initial schema migration](https://github.com/maxkunev/dicom/blob/main/alembic/versions/fbe9720b0617_init_commit.py): Alembic revision · `alembic/versions/fbe9720b0617_init_commit.py`

## Connections

- 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
