Case file

second-read

A Django application for annotating medical images under a four-eyes review process. A reviewer can never approve their own reading, readers cannot see each other's work, and agreement between two readings is measured both as pixel overlap and object by object. DICOM uploads are de-identified by keeping eleven technical attributes and discarding the rest, and every change goes into a hash-chained audit log that reports the first edited or deleted event. 54 tests, run in CI on SQLite and PostgreSQL.

Author
Henry CartoMSc · Software, Medical & eHealth Engineer
Role
Sole engineer.
Stack
Python · Django 5.1 · Django REST Framework · pydicom · Pillow · NumPy · PostgreSQL or SQLite · pytest · GitHub Actions
State
Working application with CI on SQLite and PostgreSQL. Generated images only.

Problem

Training data for medical imaging models comes from people outlining structures on images. Before an outline can be trusted it needs a second person to check it, a record of who changed what, and a measure of how much independent readers agree.

second-read implements that workflow end to end in Django, with the rules enforced on the server rather than left to the user interface.

Review screen: anna's reading with ben's outlines dashed in white, and agreement per label for artefact, lymphocyte cluster and tumour region

The reviewer's screen. anna's reading is drawn solid and ben's dashed. ben marked an artefact that anna did not, so that label scores zero; the two outlines that both readers drew match at IoU 0.80 and 0.93.

What it does

Uploads in DICOM, PNG, JPEG or TIFF are converted to a de-identified PNG, and the original file is not kept. Readers annotate with boxes and polygons against a fixed label set, in the browser or through the REST API. A task moves from draft to submitted, then to approved or rejected; a rejection needs a comment and sends the work back for correction.

A reviewer can compare two independent readings of the same image, both as pixel overlap (IoU, Dice) and as object-level agreement (precision, recall, F1 after matching). Approved annotations, and only those, export as COCO JSON with provenance on each annotation. Every change is written to an append-only, hash-chained audit log that detects edits and deletions.

Annotation screen: one box labelled artefact on a synthetic tissue tile, status draft

An annotator's own reading. Other readers' work stays hidden until review.

Design decisions

The rules live in one service layer

One module holds every state transition and permission check. The API, the admin and the demo seeder all go through it, so there is no second path that skips a rule or forgets to write the audit log. The admin shows tasks read-only for the same reason.

Four eyes, without exceptions

A reviewer cannot decide on a task they annotated. This holds for superusers too, and a test covers it.

Independent readings

An annotator only ever sees their own task on an image. Another reader's work returns 404, not 403, so its existence is not revealed either. Only reviewers see all readings, and the agreement endpoint refuses to compare a reading that is still a draft.

Review queue listing two submitted readings of image 1, by anna and ben

The review queue: submitted readings by others, oldest first. A reviewer's own readings never appear in it.

De-identification by whitelist

DICOM headers carry names, IDs, birth dates and institution names. Deleting a list of known identifying tags always misses one, so the ingest does the opposite: it copies out eleven technical attributes, such as modality, pixel spacing and window settings, and throws the rest away with the original bytes. Files that declare burned-in annotation are refused, because identifying text inside the pixels cannot be removed by filtering headers. JPEG and PNG uploads are re-encoded, which drops EXIF data.

Tamper-evident audit log

Each event stores the SHA-256 of its own content plus the previous event's hash. The ORM refuses updates and deletes. If someone edits or deletes rows with raw SQL, GET /api/audit/verify/ reports the first broken event and whether it was modified or removed. Audit writes happen in the same transaction as the change they describe, and the last event is row-locked so concurrent writers keep the chain linear.

Two kinds of agreement

Pixel overlap alone hides disagreements about how many objects there are. One reader outlines a region as one lesion, another splits it into three, and IoU is still 1.0. So agreement is reported per label at two levels: the union masks (IoU, Dice) and one-to-one instance matching at a configurable IoU threshold (precision, recall, F1). A label only one reader used counts as total disagreement, not missing data.

Only approved work leaves the system

The COCO export includes approved tasks only and records the annotator and task id on every annotation, so a training set can be traced back to who drew what and who approved it.

Study page with three synthetic H&E tiles and an upload field for DICOM, PNG, JPEG or TIFF

A study of generated tiles. All images in the demo and the tests are generated; there is no patient data in the repository.

Tests

54 tests with pytest, run in CI on every push against both SQLite and PostgreSQL 16, together with ruff and a check for missing migrations.

They cover the workflow, including the happy path with its exact audit trail, four eyes for superusers too, frozen submitted tasks, reject, fix and resubmit, and no double decisions; the audit chain, including ORM refusal of changes and detection of raw SQL edits and deletions; ingest, where patient name, ID, birth date and institution are checked to be absent from the stored metadata and pixels, along with windowing, MONOCHROME1 inversion, multi-frame files, refused burned-in annotation, EXIF removal and garbage input; agreement against known IoU and Dice values, including a count disagreement hidden by pixel overlap; the API's blinded readers and status codes per rule; and every page rendering for the right role and refusing the wrong one.

Limits

These are deliberate scope cuts. Whole-slide images are out of scope: gigapixel pathology slides need a tiled viewer and tiled storage, and this works on single images or tiles up to 100 megapixels. Multi-frame DICOM files are reduced to the middle frame, where a real radiology tool would let the reader scroll the series. Compressed DICOM (JPEG 2000, JPEG-LS) is refused with a clear error until extra decoders are installed.

The audit chain is tamper-evident, not tamper-proof. Someone with database access can rewrite the entire chain consistently; anchoring the latest hash outside the database would close that gap. The audit trail and review rules follow ideas from GxP and 21 CFR Part 11, but this is a portfolio project, not a validated system, and it makes no regulatory claim.