Cookbook
Each entry here is one job: the steps, a sample that runs, and what you get back. Every sample object is synthetic (an invented patient, fake UIDs) and encoded as base64, so no entry needs a file of yours on disk. The recipes cover indexing, routing keys, pixel metadata, de-identification, the FHIR and HL7 v2 bridge, and writing bytes back out. Read Known limitations before you trust any of this on real data.
Triage warnings
You want to log or triage every tolerated deviation without your pipeline throwing on a vendor quirk.
-
Parse leniently, the default, and read
ds.warnings. Every recoverable deviation is there with a stablecodeand a byte offset; only a member ofFATAL_CODESthrows.import { parseDicom, WARNING_CODES } from "@cosyte/dicom";// A synthetic object with the preamble omitted: a tolerated quirk.const ds = parseDicom(Buffer.from("AgAAAFVMBAAcAAAAAgAQAFVJFAAxLjIuODQwLjEwMDA4LjEuMi4xAAgAYABDUwIAQ1QQACAATE8GAE1STi00Mg==","base64",),);// The object parsed; the deviation is recorded, not hidden.ds.series.modality; // => "CT"ds.warnings.map((w) => w.code); // => ["DICOM_MISSING_PREAMBLE"]ds.warnings.some((w) => w.code === WARNING_CODES.DICOM_MISSING_PREAMBLE); // => true -
Compare on
WARNING_CODESmembers rather than strings, and give aswitchover the codes adefaultbranch: the registry gains codes between releases. -
Log
w.codeandw.position, which carry nothing from the document. A message carries only structural substitutions; Keeping PHI out of logs says exactly what each may contain. -
To be stricter for a trusted sender, pass
{ strict: true }or a source profile that escalates chosen codes. An escalated warning throws aDicomParseErrorwhosecodeis the warning's own, and that error carries the rawsnippetthe warning does not; Error handling has the details.import { parseDicom, DicomParseError, FATAL_CODES } from "@cosyte/dicom";const buf = Buffer.from("AgAAAFVMBAAcAAAAAgAQAFVJFAAxLjIuODQwLjEwMDA4LjEuMi4xAAgAYABDUwIAQ1QQACAATE8GAE1STi00Mg==","base64",);let code: string | undefined;try {parseDicom(buf, { strict: true });} catch (err) {if (err instanceof DicomParseError) code = err.code;}code; // => "DICOM_MISSING_PREAMBLE"(code ?? "") in FATAL_CODES; // => false
Read a veterinary patient
Your objects come from a veterinary practice, and you need the species, the breed, whether the animal is neutered, and who is responsible for it, from headers that often do not follow the standard to the letter.
Attributes read (PS3.6 2026c): (0010,2201) Patient Species Description, (0010,2202) Patient
Species Code Sequence, (0010,2203) Patient's Sex Neutered, (0010,2292) Patient Breed
Description, (0010,2293) Patient Breed Code Sequence, (0010,2297) Responsible Person,
(0010,2298) Responsible Person Role, (0010,2299) Responsible Organization.
-
Read them off
ds.patient. Each is typed-absent when the object does not carry it, so on a human patient none appears. The code sequences are read throughreadCode, so a code arrives as the sameCodedConcepttriplet the rest of the package uses. -
Read
ds.patient.issues. A value the view cannot read as written does not throw: it is kept as written or left absent, and reported there with a code fromPATIENT_ISSUE_CODES.
import { parseDicom, PATIENT_ISSUE_CODES } from "@cosyte/dicom";
// A synthetic veterinary object: invented codes in a private 99SYNTH scheme, invented names.
const p = parseDicom(
Buffer.from(
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABESUNNAgAAAFVMBAAcAAAAAgAQAFVJFAAxLjIuODQwLjEwMDA4LjEuMi4xABAAEABQTg4AU1lOVEhFVElDXlBFVCAQACAATE8MAFNZTlRILVBFVC0xIBAAASJMTxIAU1lOVEhFVElDIFNQRUNJRVMgEAACIlNRAAD//////v8A4DwAAAAIAAABU0gKAFNZTlNQRUNJRVMIAAIBU0gIADk5U1lOVEggCAAEAUxPEgBTeW50aGV0aWMgc3BlY2llcyD+/93gAAAAABAAAyJDUwgATkVVVEVSRUQQAJIiTE8QAFNZTlRIRVRJQyBCUkVFRCAQAJMiU1EAAP/////+/wDgOAAAAAgAAAFTSAgAU1lOQlJFRUQIAAIBU0gIADk5U1lOVEggCAAEAUxPEABTeW50aGV0aWMgYnJlZWQg/v/d4AAAAAAQAJciUE4QAFNZTlRIRVRJQ15PV05FUiAQAJgiQ1MGAE9XTkVSIBAAmSJMTw4AU0FNUExFIENMSU5JQyA=",
"base64",
),
).patient;
p.speciesDescription; // => "SYNTHETIC SPECIES"
p.speciesCode?.codeValue; // => "SYNSPECIES"
p.speciesCode?.codingSchemeDesignator; // => "99SYNTH"
p.breedDescription; // => "SYNTHETIC BREED"
p.breedCode?.codeMeaning; // => "Synthetic breed"
p.responsiblePerson?.role; // => "OWNER"
p.responsiblePerson?.organization; // => "SAMPLE CLINIC" /* synthetic */
// The sender wrote NEUTERED, which is not one of the two values the standard allows.
// The view keeps it as written and reports it; nothing throws.
p.sexNeutered; // => "NEUTERED"
p.issues?.map((i) => i.code); // => ["DICOM_PATIENT_VALUE_NOT_ENUMERATED"]
p.issues?.[0]?.tag; // => "00102203"
p.issues?.[0]?.code === PATIENT_ISSUE_CODES.DICOM_PATIENT_VALUE_NOT_ENUMERATED; // => true
What each issue code means:
| Code | What the view did |
|---|---|
DICOM_PATIENT_VALUE_NOT_ENUMERATED | Patient's Sex Neutered holds a value other than ALTERED or UNALTERED. The value is kept as written. |
DICOM_PATIENT_VALUE_WRONG_TYPE | The element is written in a VR its value does not decode from, such as OB. The field is left absent. |
DICOM_PATIENT_CODE_SEQUENCE_EMPTY | A code sequence holds no item. The code is left absent. |
DICOM_PATIENT_CODE_SEQUENCE_EXTRA_ITEMS | A code sequence holds more than one item. The first is read. |
DICOM_PATIENT_CODE_INCOMPLETE | The code item has no code value in any of its three forms, no meaning, or no coding scheme designator beside a non-URN value. What it has is kept. |
- Responsible Person Role is not checked against a list. The view keeps whatever role the sender wrote, and reports nothing about it.
responsiblePersonis identifying. It names a person and an organization, so log none of it. An issue'smessageis built from its code and tag alone, never from a value, so the issues are safe to log.- De-identification. The Annex E table we carry removes
(0010,2297)Responsible Person and(0010,2299)Responsible Organization, and(0010,2203)Patient's Sex Neutered unless you pass theRetainPatientCharacteristicsoption. It has no row for the species, the breed or the role, sodeidentify()keeps them.
Check the dates on a de-identified object
You received an object that someone de-identified, and before you use its dates you need to know whether they are real, modified, or gone.
-
Read
(0028,0303)Longitudinal Temporal Information Modified. A run ofdeidentify()writes it in one of three states:REMOVEDwhen no Retain Longitudinal Temporal Information Option was active,UNMODIFIEDwhenRetainLongitudinalTemporalwas, andMODIFIEDwhenRetainLongitudinalTemporalModifiedDateswas.import { parseDicom, deidentify } from "@cosyte/dicom";const source = parseDicom(Buffer.from("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABESUNNAgAAAFVMBAAcAAAAAgAQAFVJFAAxLjIuODQwLjEwMDA4LjEuMi4xAAgAFgBVSRoAMS4yLjg0MC4xMDAwOC41LjEuNC4xLjEuMgAIABgAVUkeADEuMi44MjYuMC4xLjM2ODAwNDMuOC40OTguMTExAAgAIABEQQgAMTkwMDAxMDEIADAAVE0GADA5MDAwMAgAUABTSAgAQUNDLTAwMDEIAGAAQ1MCAENUEAAQAFBOCABEb2VeSmFuZRAAIABMTwYATVJOLTQyEAAhAExPDABTQU1QTEUtSE9TUCAgAA0AVUkeADEuMi44MjYuMC4xLjM2ODAwNDMuOC40OTguMS4xACAADgBVSR4AMS4yLjgyNi4wLjEuMzY4MDA0My44LjQ5OC4xLjIAIAARAElTAgAyICgAAgBVUwIAAQAoAAQAQ1MMAE1PTk9DSFJPTUUyICgAEABVUwIAAAIoABEAVVMCAAACKAAwAERTCAAwLjVcMC41ICgAAAFVUwIAEAAoAAEBVVMCAAwAKAACAVVTAgALACgAAwFVUwIAAQAoAFIQRFMGAC0xMDI0ICgAUxBEUwIAMSA=","base64",),);// Three de-identified copies, one per state, as a sender would produce them.const removed = deidentify(source).dataset;const unmodified = deidentify(source, { retain: ["RetainLongitudinalTemporal"] }).dataset;const modified = deidentify(source, {retain: ["RetainLongitudinalTemporalModifiedDates"],}).dataset;const datesState = (ds: typeof removed) => {const value = ds.get("00280303")?.value;return value?.kind === "strings" ? value.values[0] : undefined;};datesState(removed); // => "REMOVED"removed.study.date; // => undefineddatesState(unmodified); // => "UNMODIFIED"unmodified.study.date?.raw; // => "19000101" /* synthetic */datesState(modified); // => "MODIFIED" -
Treat
UNMODIFIEDdates as real ones. TreatMODIFIEDas a statement about the table column the sender used, not proof that any date was shifted: this library performs no date transformation, and a sender that ranRetainLongitudinalTemporalModifiedDateswith us receivedDICOM_DEIDENT_DATES_NOT_TRANSFORMEDsaying the shift was theirs to do. -
Treat an object with no
(0028,0303)as one whose dates you cannot judge: it was not de-identified by a run that records the state.
De-identification explains the attribute and the two Options, and Known limitations states the residual of the modified-dates branch.