What the export writes#

session.export(folder) writes de-identified copies of the session’s instances to a new directory. Until export(), the session writes only the store, isocenter.log and files you ask for (a configuration, a key, a report, a manifest or a cohort table). This page describes the files: where they land, how they are encoded, what the export changes on the way out, and what the two checking options add.

Where files land#

<folder>/Subject_<PatientID>/Study_<date>_<description>_<uid>/Series_<number>_<modality>_<description>_<uid>/<SOPInstanceUID>.dcm

The directory names are built from the values being exported, so run anonymize() first. The Series number and modality are the first instance’s own Series Number and Modality after the configuration’s rules. A removed number reads 0, an empty one NoNumber, and a removed or empty modality OT. Without anonymize(), the real Patient ID and descriptions appear in the paths. After anonymize() the Patient ID is the ANON_… pseudonym, so the folder reads Subject_ANON_…. A patient whose files carried no Patient ID is written under Subject_UnknownPatient.

The filename is the SOP Instance UID, because InstanceNumber is not unique within a series. A redacted instance takes a new SOP Instance UID, so its filename is not its source’s.

An instance whose SOP Instance UID cannot name a file is not written. The UID is used as it stands, never cleaned. So a UID is refused when it holds anything but letters, digits, _, - and . (a path separator, a space, a control character), or when it begins with .. An empty UID is refused too, and so is one that is not text: a value that your code assigned to instance.sop_instance_uid and that is not a str. A file that states (0008,0018) under a VR that is not text (OB, US, SQ) never reaches the export. ingest() refuses that file, with an ERROR row that names the element and the VR, and ingests the rest of the folder (#1022).

No file is written for an instance whose UID is refused, inside the folder or outside it. The instance is in ExportSummary.failures under its UID (UNKNOWN for an empty one), with one ERROR row (Export failed for instance …: FileNameRefused: SOP Instance UID (0008,0018) cannot name a file …). The row grades the run REVIEW_REQUIRED, and the other instances are written. A UID of digits and dots is never refused. Under the default configuration, anonymize() replaces every SOP Instance UID with a 2.25. UID. So the refusal is met by an export with no anonymize() before it, or under a configuration that keeps (0008,0018). Releases before 1.0.0rc16 wrote such a file where its name pointed, which could be outside the export folder (GHSA-2rc2-r9r5-x7hm). DicomExporter.write_tree() refuses the same UIDs, and raises its RuntimeError after writing the rest. A UID that is not text meets the refusal there. At session.export(), the save that begins the export raises ValueError for it first.

Two instances whose file names the export volume treats as one name are one file, and the export says so. A UID of digits and dots cannot meet this. A UID that holds letters can. SOPa.dcm and SOPA.dcm are one file on a volume that ignores letter case (the default on macOS and Windows), and two files on a volume that does not. Some volumes also treat differently composed Unicode letters as one. Each write replaces the one before it, so the folder holds one file, carrying whichever instance was written last. After the batch, the export asks the volume which of the files it wrote are one file (the same device and inode under different planned names). Then the export reads that file’s SOP Instance UID back.

ExportSummary.written_uids then holds only the instance whose content the file carries. Each other instance is in ExportSummary.failures under its UID. One ERROR row names them all (2 exported instances (SOPA, SOPa) were written under file names this volume resolves to one file: … That file carries SOPa; the other was not delivered.), and the row grades the run REVIEW_REQUIRED. If the file cannot be read back as any of them, none is reported written and the row says that instead. The names are never compared as text, so a volume that keeps them apart writes two files and no row. Under the default configuration, anonymize() gives every instance its own 2.25. UID, and this cannot happen. This case is met by an export with no anonymize() before it, or under a configuration that keeps (0008,0018). Releases before this one wrote the one file, listed both UIDs as written and wrote no row (#1020). DicomExporter.write_tree() returns no summary and writes no rows, so it does not report this. It writes the one file and returns. Two instances that share one SOP Instance UID are a different case with its own ERROR row (… share SOP Instance UID … and were written to one file …), and both stay in written_uids under the one UID.

Each file is written under a temporary name and renamed when complete, so a crash never leaves a partial file under a real name. A stray *.tmp left by a killed worker is safe to delete.

export() returns an ExportSummary of what it wrote and raises ExportError if it planned files and reports none of them written. A partial export returns what it wrote. A failed instance has an ERROR audit row keyed on its own UID, with one exception. Instances replaced in a file that several names resolved to (above) share one row for the whole group. That row is keyed on the UID of the instance that the file carries, which is in written_uids, or on MULTIPLE when the file could not be read back. So a UID in failures does not always key a row. The row’s text names every UID of the group.

ExportError normally means the folder holds none of the planned files. In one case it holds one. In that case, every planned instance was written under names that the volume resolved to one file, and that file could not be read back as any of them. None is reported written. The EXPORT row reads wrote 0 of N planned instances, and the MULTIPLE row says what happened. The exception’s message says “none is reported as written” (it said “nothing reached disk” before #1020).

Compression#

The export writes two transfer syntaxes and no others: JPEG 2000 Lossless (1.2.840.10008.1.2.4.90) with use_compression=True, the default, and Explicit VR Little Endian (1.2.840.10008.1.2.1) with use_compression=False. A file with no pixel data, a file of floating-point pixels, and an image of 32- or 64-bit integer samples are always Explicit VR Little Endian. Both syntaxes state each element’s VR, so a private element is read back under the VR the export wrote for it, never under a reader’s dictionary guess (#1107). An uncompressed export of a native little-endian source with whole-byte samples, Planar Configuration 0 (or none, read as 0) and a label other than YBR_FULL_422 carries the source’s Pixel Data bytes unchanged. A big-endian, encapsulated or 1-bit source is decoded and written in the export’s own layout. A source in any other syntax is decoded at ingest and re-encoded into one of those two, and nothing records the source syntax. Codec support lists which decoder reads each source syntax.

Compression is lossless, but it changes how colour images are labelled:

  • An RGB image is encoded with JPEG 2000’s reversible colour transform and declared YBR_RCT, as the standard requires. A YBR source that decodes to RGB is already stored as RGB.

  • 32- and 64-bit integer images cannot be compressed exactly. JPEG 2000 Lossless here is exact only to 25 bits. Under use_compression=True, each such instance is written uncompressed, Explicit VR Little Endian, and the export names it at INFO, with no audit row. The samples are written exactly. Other instances of the same export are still compressed (#771).

  • A 16-bit colour image is compressed too, and pydicom cannot read it with Pillow alone. With pylibjpeg-openjpeg installed, pydicom reads it exactly. The export names each such instance at INFO.

If a recipient’s reader cannot handle JPEG 2000, export with use_compression=False.

A source that was compressed lossily is re-encoded losslessly from its decoded samples. Its LossyImageCompression (0028,2110) is carried only if the source declared it. So a near-lossless JPEG-LS source that did not declare it exports with no record that it was lossy.

What the export changes on the way out#

  • De-identification markers. An instance whose patient, study and own status are de-identified under the policy in force gets up to three markers:

    • Patient Identity Removed (0012,0062) YES

    • a De-identification Method (0012,0063) value that names Isocenter and the policy

    • Longitudinal Temporal Information Modified (0028,0303), read from the file’s own dates

    The markers describe the attributes, not the pixels. What an exported file says about itself says when each is written.

  • Patient, study and series tags are written onto each file from the patient, study and series that own them. Series Number (0020,0011), Modality (0008,0060), Study Time (0008,0030) and the equipment tags come from the instance, which is what anonymize() edits. So a configuration’s rule on any of them reaches the file. Series.series_number and Series.modality are the source’s values as ingested and are not written. Study.study_time, which only a caller can set, is not written either. An instance with no Study Time is written with an empty one.

  • A file with no Modality, because the configuration removed or emptied it or the source carried none, is written as it is, with one WARNING row that says it is not conformant. Modality is Type 1. The row grades the run REVIEW_REQUIRED.

  • A CT, MR or PET image missing a Type 1 element other than Modality is not written. For CT Image Storage (1.2.840.10008.5.1.4.1.1.2) the export checks six Type 1 elements: SOP Class UID (0008,0016), SOP Instance UID (0008,0018), Series Instance UID (0020,000E), Image Position (Patient) (0020,0032), Image Orientation (Patient) (0020,0037) and Pixel Spacing (0028,0030). For MR Image Storage (1.2.840.10008.5.1.4.1.1.4) and PET Image Storage (1.2.840.10008.5.1.4.1.1.128), it checks the three geometry elements of the Image Plane module. For those, the row reads Missing 0020,0032 in ImagePlane (#879).

    When an instance reaches the write with one of the checked elements absent or empty, the instance is withheld. Typically a configuration rule removed or emptied the element, or the source lacked it. No file is written for the instance, and one ERROR row names it, for example Export failed for instance 2.25.…: ValueError: Validation Errors: ['[Type 1 Error] Missing 0020,0032 in CTImage']. The row grades the run REVIEW_REQUIRED, and an export that wrote nothing raises ExportError, as above.

    Series Instance UID never trips this check, because the series that owns it stamps it on every file. Because of that stamp, the loader refuses some rules on Series Instance UID (0020,000E) or Study Instance UID (0020,000D): a REMOVE or EMPTY rule, or a REPLACE with a value:. The refusal is a ValueError when the configuration is loaded, and it names REPLACE with no value: key (#877). Releases before that fix loaded the rule, and the export wrote the source UID. Under REMOVE, it did so with a PASS grade and (0012,0062) YES. Keep the profile’s REPLACE on both tags, which writes a replacement UID.

    This differs from Modality on purpose (#874). Modality is metadata that you may choose to hide. The geometry is what makes the pixels usable, so a file without it is not written. No other SOP class is checked. An Enhanced MR image, say, keeps its geometry in the per-frame functional groups. On such an image, a rule that removes a top-level Image Position (Patient) is applied, and the file is written without it, with no row. Releases before #879 checked CT alone, and wrote an MR or PET image that way. Configuration says which rules this is.

  • Photometric Interpretation spelled in lower case or with a leading space (' rgb ') is written as the Code String it spells (RGB), with an INFO line. Whitespace is removed from the two ends: the space, and also a tab, a line feed, a carriage return, a no-break space and every other character Python’s str.strip() removes. When one that is not a space was removed, the INFO line names it by code point and by end (the whitespace that is not a space removed from its end (U+0009)). Only the letters a to z are upper-cased. A label that holds another character the file can hold (a ß, a tab in the middle) keeps that character and is written as held. It gets the WARNING row that a label no transfer syntax defines gets.

    A Code String is written one byte to a character, so the file can hold U+0000 to U+00FF and nothing above. A label that holds a character above that (a dotless ı, a long ſ) is written as it was before #612. It is upper-cased by Unicode’s rules (monochrome2ı as MONOCHROME2I), with one more WARNING row. That row names the character by code point, the label written, and any whitespace other than the space that was removed from its ends. The row is raised only when the file holds that spelling. Over one sample per pixel, the export writes MONOCHROME2 whatever was declared, and says nothing. Where that spelling cannot be held either (fullwidth letters), the instance is not exported and its ERROR row says so.

    Every sentence quotes a label in ASCII, a character outside it as its escape ('RG\xdf'). Only a label held as text is respelled. A label held as bytes, which a file brings in by stating a binary VR for (0028,0004), is written as held, with no note. A label of YBR_FULL_422 over three full samples per pixel is written YBR_FULL, at the top level and in an Icon Image Sequence item.

  • A file pydicom refuses to write is not exported, and its ERROR row quotes what pydicom raised, never the Python traceback pydicom attaches to it. Where pydicom’s sentence names the element, the row does (With tag (0042,0011) got exception: …). pydicom’s sentence does not always name it. A top-level text value that the file cannot encode gives TypeError: function takes exactly 5 arguments (1 given). A value that a number VR cannot hold gives the bare word OSError. Neither names an element (#1063).

  • A private element whose value no longer fits the VR recorded for it at ingest (after a REPLACE, typically) is written under a VR that holds it. One WARNING row per instance names the tags and both VRs.

  • Icon images. Nothing scans or redacts a small preview image (an Icon Image Sequence item), so the export removes the ones that could show redacted pixels. An instance’s own icon is removed from that instance’s file when the instance was redacted or has redaction zones applied by this export. Every other nested icon is removed from every file once any instance in the store was redacted, or a loaded rule’s zones match a series in the store. One example is an icon under Referenced Image Sequence, which is a thumbnail of a different image. A "*" rule matches every series, with a Device Serial Number or without. A rule for a scanner the store does not hold removes nothing. Each removal writes a DATA_LOSS row and grades the run REVIEW_REQUIRED.

  • Recoverable identities. When reversible anonymization locked identities into the Encrypted Attributes Sequence (0400,0500), the export keeps them and logs a WARNING with the count. Anyone who holds the key can read the identities from the exported files.

DicomExporter.write_tree() writes a graph as it stands. It applies the same owner tags and the same Type 1 check, and raises RuntimeError for an instance that fails that check. It applies none of the export’s other checks, redaction zones or markers. session.export() is the way to write de-identified output.

check_burned_in=True#

export(folder, check_burned_in=True) runs audit() first. It then withholds every instance that still carries an identifier under the policy in force, on itself or on its patient, study or series. On a session that has not run anonymize(), that is every instance carrying a value any rule acts on.

Without a burned_in_text policy, it checks tags, not pixels. Burned-in text is then handled by redaction zones, and found by scan_pixel_content() (Burned-in text (OCR)).

With a burned_in_text policy, it also reads the pixels (#1108). Every planned instance in the policy’s scope (#1121) is read with OCR as it is about to be written, after the configured zones. That read decides the instance, whatever the last redact() recorded. Under a narrowed target, “text” is text read as one of its kinds. A target that names names needs the nlp extra and its model. Without them, the export raises NlpUnavailableError before the tag scan. An instance out of scope is not read and is written with no row:

The read

report

redact

withhold

finds text

written, with a row

withheld, with a row

withheld, with a row

finds nothing

written

written

written

cannot read it in full

written, with a row

withheld, with a row

withheld, with a row

The rows are WARNING rows naming the instance, the number of regions and their frames, never the text. The read changes nothing in the session. Under redact, the export does not redact, and its row names the remedy. redact() reads and redacts an instance it has not read under this policy, or whose recorded text no redaction has blanked. An instance whose read after redaction still found text keeps a current record, which a plain redact() does not read again. That instance needs redact(force=True). Until a redaction blanks what the read finds, every check_burned_in=True export withholds it. Under withhold, redact() changes no pixel and is no remedy. The instance is withheld while its text is found.

The EXPORT row counts the instances that the read withheld apart from the ones the tag scan withheld. Section 5 of the report gains one line per such export. The line says the policy’s scope, how many instances the read read and could not read, how many were out of scope, how many it found text on, and how many it withheld or wrote with a row. A read that found nothing costs no PASS. With the ocr extra or the tesseract binary unavailable, export(check_burned_in=True) raises OcrUnavailableError before the tag scan, the save and any row. Plain export() needs no OCR, and reads each instance’s record instead.

Each withheld instance writes one WARNING audit row that names the instance and the level that carried the identifier, never the value. So the report grades REVIEW_REQUIRED. “Instances Written” counts withheld instances as requested (“1 of 2 requested”). An export that withheld everything returns an empty ExportSummary and does not raise. An instance outside subset is not withheld. It was never asked for.

verify_readback=True#

export(folder, verify_readback=True) decodes every file it writes through the same decoder ingest() uses, and compares every pixel sample with what it meant to write. It also refuses:

  • a Photometric Interpretation the file’s transfer syntax does not admit, for example YBR_ICT on an uncompressed file, or YBR_PARTIAL_422 and YBR_PARTIAL_420 under either syntax the export writes. The label is judged on a file with no pixel data too.

  • a YBR_FULL image whose samples are 16-bit or signed 8-bit, which ingest() cannot read back, because pydicom’s colour conversion takes unsigned 8-bit samples only.

An instance that fails verification is not delivered. It gets an ERROR audit row keyed on its UID, appears in ExportSummary.failures, and grades the run REVIEW_REQUIRED.

Without verification, an inadmissible Photometric Interpretation is written as declared with a WARNING row, which also grades REVIEW_REQUIRED. So turning verification on can cost you a file the default export would have delivered. The 16-bit or signed 8-bit YBR_FULL file is conformant DICOM, so the default export writes it, with an INFO line saying Isocenter cannot read it back and no audit row.

WFDB#

session.export(folder, format="wfdb") writes waveform instances as PhysioNet WFDB records instead. Waveforms & WFDB describes that output.