@dcmjs-org/fhir 1.0.0-beta.4 → 1.0.0-beta.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,288 +0,0 @@
1
- // packages/fhir/src/imagingStudy.js
2
- //
3
- // FHIR R4B ImagingStudy from naturalized DICOM dataset(s). The DICOM
4
- // study/series/instance hierarchy maps directly onto
5
- // ImagingStudy.series[].instance[] — this is deliberately a thin
6
- // restatement of the Part 10 elements as FHIR-flavored JSON.
7
- // Based on the IHE Radiology Technical Framework (MADO) mapping.
8
-
9
- import { validationLog, toStringFirst, toFloatFirst } from "@dcmjs-org/core";
10
-
11
- import {
12
- DICOM_UID_SYSTEM,
13
- uidToUrn,
14
- dicomDateTimeToIso,
15
- parsePersonName,
16
- modalityCoding
17
- } from "./helpers.js";
18
-
19
- function instanceFromDataset(dataset) {
20
- const sopInstanceUid = toStringFirst(dataset.SOPInstanceUID);
21
- if (!sopInstanceUid) {
22
- return null;
23
- }
24
-
25
- const sopClassUrn = uidToUrn(dataset.SOPClassUID);
26
- if (!sopClassUrn) {
27
- validationLog.warn(
28
- `@dcmjs-org/fhir: skipping instance ${sopInstanceUid} — ` +
29
- "no SOPClassUID (instance.sopClass is required 1..1)"
30
- );
31
- return null;
32
- }
33
-
34
- const instance = {
35
- uid: sopInstanceUid,
36
- sopClass: {
37
- system: "urn:ietf:rfc:3986",
38
- code: sopClassUrn
39
- }
40
- };
41
-
42
- const number = unsignedIntFirst(dataset.InstanceNumber);
43
- if (number !== null) {
44
- instance.number = number;
45
- }
46
- const numberOfFrames = unsignedIntFirst(dataset.NumberOfFrames);
47
- if (numberOfFrames !== null) {
48
- instance.numberOfFrames = numberOfFrames;
49
- }
50
- const title =
51
- toStringFirst(dataset.SeriesDescription) ||
52
- toStringFirst(dataset.StudyDescription);
53
- if (title) {
54
- instance.title = title;
55
- }
56
-
57
- return instance;
58
- }
59
-
60
- function seriesShellFromDataset(dataset) {
61
- const series = {
62
- uid: toStringFirst(dataset.SeriesInstanceUID),
63
- instance: []
64
- };
65
-
66
- const number = unsignedIntFirst(dataset.SeriesNumber);
67
- if (number !== null) {
68
- series.number = number;
69
- }
70
- // The caller skips a dataset with no Modality (series.modality is 1..1).
71
- series.modality = modalityCoding(dataset.Modality);
72
- const description = toStringFirst(dataset.SeriesDescription);
73
- if (description) {
74
- series.description = description;
75
- }
76
- const started = dicomDateTimeToIso(
77
- dataset.SeriesDate,
78
- dataset.SeriesTime,
79
- dataset.TimezoneOffsetFromUTC
80
- );
81
- if (started) {
82
- series.started = started;
83
- }
84
- // A DICOM defined term is not a SNOMED display, so it has no system.
85
- const bodyPart = toStringFirst(dataset.BodyPartExamined);
86
- if (bodyPart) {
87
- series.bodySite = { display: bodyPart };
88
- }
89
- const laterality = toStringFirst(dataset.Laterality);
90
- if (laterality) {
91
- series.laterality = LATERALITY_CODING[laterality] || {
92
- display: laterality
93
- };
94
- }
95
-
96
- return series;
97
- }
98
-
99
- /** DICOM Laterality (0020,0060) → SNOMED CT (PS3.16 CID 244). */
100
- const LATERALITY_CODING = {
101
- L: { system: "http://snomed.info/sct", code: "7771000", display: "Left" },
102
- R: { system: "http://snomed.info/sct", code: "24028007", display: "Right" },
103
- B: {
104
- system: "http://snomed.info/sct",
105
- code: "51440002",
106
- display: "Right and left"
107
- }
108
- };
109
-
110
- function warnSkip(dataset, reason) {
111
- validationLog.warn(
112
- "@dcmjs-org/fhir: skipping dataset " +
113
- `${
114
- toStringFirst(dataset.SOPInstanceUID) || "<no SOPInstanceUID>"
115
- } — ${reason}`
116
- );
117
- }
118
-
119
- /** The first value as a FHIR unsignedInt, or null. */
120
- function unsignedIntFirst(value) {
121
- const number = toFloatFirst(value);
122
- return Number.isInteger(number) && number >= 0 ? number : null;
123
- }
124
-
125
- /**
126
- * Build one FHIR ImagingStudy from one or more naturalized datasets
127
- * belonging to the same study. Instances are grouped by
128
- * SeriesInstanceUID. The first dataset with a StudyInstanceUID anchors the
129
- * study-level identity.
130
- *
131
- * The mapper never fabricates a value that FHIR requires (FHIR-USR-3). It
132
- * skips, with a validationLog warning: a dataset of another study, a
133
- * dataset with no SeriesInstanceUID or no Modality (series.uid and
134
- * series.modality are 1..1), and an instance with no SOPClassUID
135
- * (instance.sopClass is 1..1).
136
- *
137
- * @param {Object[]} datasets - naturalized datasets (same StudyInstanceUID)
138
- * @param {Object} [options]
139
- * @param {Object} [options.subject] - FHIR Reference ({ reference, display })
140
- * to attach as ImagingStudy.subject — generic FHIR, supplied by the caller
141
- * @returns {Object|null} FHIR ImagingStudy, or null for empty input
142
- */
143
- export function imagingStudyFromDatasets(datasets, options = {}) {
144
- const all = (datasets || []).filter(Boolean);
145
- if (all.length === 0) {
146
- return null;
147
- }
148
-
149
- const first =
150
- all.find(dataset => toStringFirst(dataset.StudyInstanceUID)) || all[0];
151
- const anchorStudyUid = toStringFirst(first.StudyInstanceUID);
152
- const list = all.filter(dataset => {
153
- const studyUid = toStringFirst(dataset.StudyInstanceUID);
154
- if (studyUid && studyUid !== anchorStudyUid) {
155
- warnSkip(
156
- dataset,
157
- `StudyInstanceUID ${studyUid} is not the study ` +
158
- `${anchorStudyUid} of this ImagingStudy`
159
- );
160
- return false;
161
- }
162
- return true;
163
- });
164
-
165
- // No imaging identity at all → not a study
166
- const hasIdentity = list.some(
167
- dataset =>
168
- toStringFirst(dataset.StudyInstanceUID) ||
169
- toStringFirst(dataset.SeriesInstanceUID) ||
170
- toStringFirst(dataset.SOPInstanceUID)
171
- );
172
- if (!hasIdentity) {
173
- return null;
174
- }
175
-
176
- const seriesMap = new Map();
177
- list.forEach(dataset => {
178
- const seriesUid = toStringFirst(dataset.SeriesInstanceUID);
179
- if (!seriesUid) {
180
- warnSkip(dataset, "no SeriesInstanceUID (series.uid is 1..1)");
181
- return;
182
- }
183
- if (!toStringFirst(dataset.Modality)) {
184
- warnSkip(dataset, "no Modality (series.modality is 1..1)");
185
- return;
186
- }
187
- if (!seriesMap.has(seriesUid)) {
188
- seriesMap.set(seriesUid, seriesShellFromDataset(dataset));
189
- }
190
- const instance = instanceFromDataset(dataset);
191
- if (instance) {
192
- seriesMap.get(seriesUid).instance.push(instance);
193
- }
194
- });
195
-
196
- const series = Array.from(seriesMap.values()).map(shell => {
197
- const entry = { ...shell };
198
- entry.numberOfInstances = entry.instance.length;
199
- if (entry.instance.length === 0) {
200
- delete entry.instance;
201
- }
202
- return entry;
203
- });
204
-
205
- const numberOfInstances = series.reduce(
206
- (sum, entry) => sum + (entry.numberOfInstances || 0),
207
- 0
208
- );
209
-
210
- const study = {
211
- resourceType: "ImagingStudy",
212
- status: "available",
213
- numberOfSeries: series.length,
214
- numberOfInstances,
215
- series
216
- };
217
-
218
- const identifiers = [];
219
- const studyUid = toStringFirst(first.StudyInstanceUID);
220
- if (studyUid) {
221
- identifiers.push({
222
- use: "official",
223
- system: DICOM_UID_SYSTEM,
224
- value: uidToUrn(studyUid)
225
- });
226
- }
227
- const accessionNumber = toStringFirst(first.AccessionNumber);
228
- if (accessionNumber) {
229
- identifiers.push({
230
- use: "usual",
231
- type: {
232
- coding: [
233
- {
234
- system: "http://terminology.hl7.org/CodeSystem/v2-0203",
235
- code: "ACSN"
236
- }
237
- ]
238
- },
239
- value: accessionNumber
240
- });
241
- }
242
- if (identifiers.length > 0) {
243
- study.identifier = identifiers;
244
- }
245
-
246
- const started = dicomDateTimeToIso(
247
- first.StudyDate,
248
- first.StudyTime,
249
- first.TimezoneOffsetFromUTC
250
- );
251
- if (started) {
252
- study.started = started;
253
- }
254
- const description = toStringFirst(first.StudyDescription);
255
- if (description) {
256
- study.description = description;
257
- }
258
- const referrer = parsePersonName(first.ReferringPhysicianName);
259
- if (referrer && referrer.text) {
260
- study.referrer = { display: referrer.text };
261
- }
262
- if (options.subject && options.subject.reference) {
263
- study.subject = options.subject;
264
- }
265
-
266
- // Study-level modality union (distinct series modalities)
267
- const modalities = [];
268
- series.forEach(entry => {
269
- if (
270
- entry.modality &&
271
- !modalities.some(m => m.code === entry.modality.code)
272
- ) {
273
- modalities.push(entry.modality);
274
- }
275
- });
276
- if (modalities.length > 0) {
277
- study.modality = modalities;
278
- }
279
-
280
- return study;
281
- }
282
-
283
- /**
284
- * Single-dataset convenience wrapper.
285
- */
286
- export function imagingStudyFromDataset(dataset, options = {}) {
287
- return imagingStudyFromDatasets(dataset ? [dataset] : [], options);
288
- }
package/src/index.js DELETED
@@ -1,184 +0,0 @@
1
- // packages/fhir/src/index.js
2
- //
3
- // @dcmjs-org/fhir — FHIR both ways.
4
- //
5
- // PRELIMINARY API: the exports can change in a minor release while
6
- // packages/fhir/README.md has the preliminary notice.
7
- //
8
- // The sink: naturalized DICOM Part 10 datasets
9
- // (DicomMetaDictionary.naturalizeDataset output) → FHIR R4B resources.
10
- // The source: a FHIR Patient → DICOM patient-module attributes
11
- // (patientToDataset), for demographics injection.
12
- //
13
- // Deliberately simple: standard FHIR, nothing deployment-specific.
14
- // Consumers assign resource ids, storage references, and meta.tags.
15
- //
16
- // const { patient, imagingStudy, documentReference } = toFhir(dataset);
17
- // const bundle = toBundle([dataset1, dataset2]);
18
- // const attrs = patientToDataset(patientResource); // → { PatientName, ... }
19
-
20
- import { toStringFirst, validationLog } from "@dcmjs-org/core";
21
- import { patientFromDataset } from "./patient.js";
22
- import {
23
- imagingStudyFromDataset,
24
- imagingStudyFromDatasets
25
- } from "./imagingStudy.js";
26
- import { documentReferenceFromDataset } from "./documentReference.js";
27
- import { assertSupportedFhirVersion } from "./helpers.js";
28
-
29
- export * from "./helpers.js";
30
- export { patientFromDataset } from "./patient.js";
31
- export { patientToDataset } from "./patientToDataset.js";
32
- export {
33
- imagingStudyFromDataset,
34
- imagingStudyFromDatasets
35
- } from "./imagingStudy.js";
36
- export { documentReferenceFromDataset } from "./documentReference.js";
37
-
38
- /**
39
- * Map one naturalized dataset to its FHIR resources.
40
- *
41
- * Encapsulated document instances (e.g. Encapsulated PDF from a PACS) map
42
- * to a DocumentReference and no ImagingStudy; image instances map to an
43
- * ImagingStudy and no DocumentReference. The 3-key shape is stable either
44
- * way.
45
- *
46
- * Without `options.subject`, the ImagingStudy or DocumentReference
47
- * references the Patient of the result as `{ reference: "./patient" }`, a
48
- * path relative to the returned object (FHIR-USR-10). The consumer replaces
49
- * it when it stores the resources.
50
- *
51
- * @param {Object} dataset - DicomMetaDictionary.naturalizeDataset output
52
- * @param {Object} [options]
53
- * @param {string} [options.fhirVersion='R4B'] - R4/R4B only; throws otherwise
54
- * @param {Object} [options.subject] - FHIR Reference for
55
- * ImagingStudy.subject / DocumentReference.subject
56
- * @param {boolean} [options.includeData=true] - inline document bytes as
57
- * base64 in DocumentReference attachments
58
- * @returns {{ patient: Object|null, imagingStudy: Object|null,
59
- * documentReference: Object|null }}
60
- */
61
- export function toFhir(dataset, options = {}) {
62
- assertSupportedFhirVersion(options);
63
- const patient = patientFromDataset(dataset);
64
- const resourceOptions = withSubject(options, patient && "./patient");
65
- const documentReference = documentReferenceFromDataset(
66
- dataset,
67
- resourceOptions
68
- );
69
- return {
70
- patient,
71
- imagingStudy: documentReference
72
- ? null
73
- : imagingStudyFromDataset(dataset, resourceOptions),
74
- documentReference
75
- };
76
- }
77
-
78
- /** The options with a relative `subject`, unless the caller gave one. */
79
- function withSubject(options, reference) {
80
- if (options.subject || !reference) {
81
- return options;
82
- }
83
- return { ...options, subject: { reference } };
84
- }
85
-
86
- /**
87
- * Map one or more datasets (one study's worth of instances) to a FHIR
88
- * collection Bundle: at most one Patient, a DocumentReference for each
89
- * encapsulated document, and one aggregated ImagingStudy.
90
- *
91
- * The first dataset with a patient module gives the Patient, and the first
92
- * dataset with a StudyInstanceUID gives the study. A dataset with a
93
- * different PatientID or StudyInstanceUID is skipped with a validationLog
94
- * warning (FHIR-USR-3). Each entry has `fullUrl: "./entry/<index>"`, a path
95
- * relative to the returned Bundle, and without `options.subject` the
96
- * ImagingStudy and the DocumentReferences reference the Patient by its
97
- * `fullUrl` (FHIR-USR-10).
98
- *
99
- * @param {Object[]} datasets - naturalized datasets
100
- * @param {Object} [options] - as toFhir
101
- * @returns {Object} FHIR Bundle (type: collection)
102
- */
103
- export function toBundle(datasets, options = {}) {
104
- assertSupportedFhirVersion(options);
105
-
106
- const all = (datasets || []).filter(Boolean);
107
- const resources = [];
108
-
109
- let patient = null;
110
- let patientId = null;
111
- for (const dataset of all) {
112
- patient = patientFromDataset(dataset);
113
- if (patient) {
114
- patientId = toStringFirst(dataset.PatientID);
115
- break;
116
- }
117
- }
118
- const studyUid = all
119
- .map(dataset => toStringFirst(dataset.StudyInstanceUID))
120
- .find(Boolean);
121
- const list = all.filter(dataset =>
122
- sameContext(dataset, patientId, studyUid)
123
- );
124
- if (patient) {
125
- resources.push(patient);
126
- }
127
- const resourceOptions = withSubject(options, patient && "./entry/0");
128
-
129
- // Encapsulated documents each become a DocumentReference; the
130
- // remaining (image) datasets aggregate into one ImagingStudy.
131
- const imageDatasets = [];
132
- for (const dataset of list) {
133
- const documentReference = documentReferenceFromDataset(
134
- dataset,
135
- resourceOptions
136
- );
137
- if (documentReference) {
138
- resources.push(documentReference);
139
- } else {
140
- imageDatasets.push(dataset);
141
- }
142
- }
143
-
144
- const imagingStudy = imagingStudyFromDatasets(
145
- imageDatasets,
146
- resourceOptions
147
- );
148
- if (imagingStudy) {
149
- resources.push(imagingStudy);
150
- }
151
-
152
- return {
153
- resourceType: "Bundle",
154
- type: "collection",
155
- total: resources.length,
156
- entry: resources.map((resource, index) => ({
157
- fullUrl: `./entry/${index}`,
158
- resource
159
- }))
160
- };
161
- }
162
-
163
- /** True when the dataset belongs to the patient and study of the Bundle. */
164
- function sameContext(dataset, patientId, studyUid) {
165
- const datasetPatientId = toStringFirst(dataset.PatientID);
166
- const datasetStudyUid = toStringFirst(dataset.StudyInstanceUID);
167
- const reason =
168
- patientId && datasetPatientId && datasetPatientId !== patientId
169
- ? `PatientID ${datasetPatientId} is not ${patientId}`
170
- : studyUid && datasetStudyUid && datasetStudyUid !== studyUid
171
- ? `StudyInstanceUID ${datasetStudyUid} is not ${studyUid}`
172
- : null;
173
- if (reason) {
174
- validationLog.warn(
175
- "@dcmjs-org/fhir: toBundle skips dataset " +
176
- `${
177
- toStringFirst(dataset.SOPInstanceUID) ||
178
- "<no SOPInstanceUID>"
179
- } — ${reason}`
180
- );
181
- return false;
182
- }
183
- return true;
184
- }
package/src/patient.js DELETED
@@ -1,71 +0,0 @@
1
- // packages/fhir/src/patient.js
2
- //
3
- // FHIR R4B Patient from the DICOM patient module (group 0010) of a
4
- // naturalized dataset. Emits standard FHIR only — no ids, no meta.tags,
5
- // no storage references. Id assignment and any deployment-specific
6
- // decoration are the consumer's job.
7
-
8
- import { toStringFirst } from "@dcmjs-org/core";
9
- import {
10
- dicomDateTimeToIso,
11
- parsePersonName,
12
- personNameToHumanName,
13
- sexToGender
14
- } from "./helpers.js";
15
-
16
- /**
17
- * Build a FHIR Patient resource from a naturalized DICOM dataset.
18
- * @param {Object} dataset - DicomMetaDictionary.naturalizeDataset output
19
- * @returns {Object|null} FHIR Patient, or null when the dataset carries
20
- * no usable patient module (no PatientName and no PatientID)
21
- */
22
- export function patientFromDataset(dataset) {
23
- if (!dataset) {
24
- return null;
25
- }
26
-
27
- const patientId = toStringFirst(dataset.PatientID);
28
- const humanName = personNameToHumanName(
29
- parsePersonName(dataset.PatientName)
30
- );
31
-
32
- if (!patientId && !humanName) {
33
- return null;
34
- }
35
-
36
- const patient = {
37
- resourceType: "Patient"
38
- };
39
-
40
- if (patientId) {
41
- patient.identifier = [
42
- {
43
- use: "usual",
44
- type: {
45
- coding: [
46
- {
47
- system: "http://terminology.hl7.org/CodeSystem/v2-0203",
48
- code: "MR",
49
- display: "Medical Record Number"
50
- }
51
- ],
52
- text: "Medical Record Number"
53
- },
54
- value: patientId
55
- }
56
- ];
57
- }
58
-
59
- if (humanName) {
60
- patient.name = [humanName];
61
- }
62
-
63
- const birthDate = dicomDateTimeToIso(dataset.PatientBirthDate);
64
- if (birthDate) {
65
- patient.birthDate = birthDate;
66
- }
67
-
68
- patient.gender = sexToGender(dataset.PatientSex);
69
-
70
- return patient;
71
- }
@@ -1,80 +0,0 @@
1
- // packages/fhir/src/patientToDataset.js
2
- //
3
- // The FHIR source direction: a FHIR Patient resource → DICOM patient-module
4
- // attributes (group 0010) in naturalized keyword form. Inverse of
5
- // patient.js's patientFromDataset, and the primitive behind demographics
6
- // injection ("apply this Patient to these instances").
7
- //
8
- // The result rules are FHIR-USR-6 in packages/fhir/README.md.
9
-
10
- import {
11
- genderToSex,
12
- humanNameToPersonName,
13
- isoDateToDicom
14
- } from "./helpers.js";
15
-
16
- const MR_IDENTIFIER_CODE = "MR";
17
-
18
- /**
19
- * Pick the name to use: `official` wins, then `usual`, then the first
20
- * entry with no `use` at all, then the first entry — so a resource
21
- * carrying both a married (official) and maiden name maps to the married
22
- * one, never the maiden.
23
- */
24
- function selectName(names) {
25
- if (!Array.isArray(names) || names.length === 0) {
26
- return null;
27
- }
28
- for (const use of ["official", "usual"]) {
29
- const match = names.find(name => name && name.use === use);
30
- if (match) {
31
- return match;
32
- }
33
- }
34
- return names.find(name => name && !name.use) || names[0];
35
- }
36
-
37
- /**
38
- * Pick the identifier for PatientID: an MR-typed identifier wins
39
- * (type.coding code "MR", the shape patientFromDataset emits), else the
40
- * first identifier carrying a value.
41
- */
42
- function selectIdentifier(identifiers) {
43
- if (!Array.isArray(identifiers) || identifiers.length === 0) {
44
- return null;
45
- }
46
- const mrTyped = identifiers.find(identifier =>
47
- identifier?.type?.coding?.some(
48
- coding => coding && coding.code === MR_IDENTIFIER_CODE
49
- )
50
- );
51
- return mrTyped || identifiers.find(identifier => identifier?.value) || null;
52
- }
53
-
54
- /**
55
- * Map a FHIR Patient resource to DICOM patient-module attributes.
56
- *
57
- * @param {Object} patient - FHIR Patient (R4/R4B)
58
- * @returns {{ PatientName: string, PatientID: string,
59
- * PatientBirthDate: string, PatientSex: string }} naturalized keyword
60
- * attributes; absent resource fields are "" (deterministic overwrite)
61
- * @throws when the input is not a Patient resource
62
- */
63
- export function patientToDataset(patient) {
64
- if (!patient || patient.resourceType !== "Patient") {
65
- throw new Error(
66
- "patientToDataset expects a FHIR Patient resource " +
67
- `(got resourceType "${patient?.resourceType}")`
68
- );
69
- }
70
-
71
- const name = selectName(patient.name);
72
- const identifier = selectIdentifier(patient.identifier);
73
-
74
- return {
75
- PatientName: (name && humanNameToPersonName(name)) || "",
76
- PatientID: identifier?.value || "",
77
- PatientBirthDate: isoDateToDicom(patient.birthDate) || "",
78
- PatientSex: genderToSex(patient.gender)
79
- };
80
- }