forked from ivmartel/dwv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotationGroupFactory.js
More file actions
1793 lines (1639 loc) · 56.7 KB
/
annotationGroupFactory.js
File metadata and controls
1793 lines (1639 loc) · 56.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
dateToDateObj,
getDicomDate,
dateToTimeObj,
getDicomTime,
} from '../dicom/dicomDate.js';
import {safeGet} from '../dicom/dataElement.js';
import {
ValueTypes,
RelationshipTypes,
ContinuityOfContents,
getSRContent,
getDicomSRContentItem,
getContentTemplate,
DicomSRContent,
getSRContentFromValue
} from '../dicom/dicomSRContent.js';
import {
DcmCodes,
getDcmDicomCode,
getColourCode,
getQuantificationName,
getQuantificationUnit,
DicomCode,
getMeasurementUnitsCode,
isEqualCode
} from '../dicom/dicomCode.js';
import {
isVersionInBounds,
getDwvVersionFromImplementationClassUID
} from '../dicom/dicomParser.js';
import {MeasuredValue} from '../dicom/dicomMeasuredValue.js';
import {NumericMeasurement} from '../dicom/dicomNumericMeasurement.js';
import {getAsSimpleElements} from '../dicom/dicomTag.js';
import {getElementsFromJSONTags} from '../dicom/dicomWriter.js';
import {ImageReference} from '../dicom/dicomImageReference.js';
import {SopInstanceReference} from '../dicom/dicomSopInstanceReference.js';
import {
GraphicTypes,
getScoordFromShape,
getShapeFromScoord,
SpatialCoordinate
} from '../dicom/dicomSpatialCoordinate.js';
import {SpatialCoordinate3D} from '../dicom/dicomSpatialCoordinate3D.js';
import {BIG_EPSILON_EXPONENT} from '../math/matrix.js';
import {precisionRound} from '../utils/string.js';
import {logger} from '../utils/logger.js';
import {Annotation} from './annotation.js';
import {AnnotationGroup} from './annotationGroup.js';
import {Point2D, Point3D} from '../math/point.js';
// doc imports
/* eslint-disable no-unused-vars */
import {DataElement} from '../dicom/dataElement.js';
import {BidimensionalLine} from '../math/bidimensionalLine.js';
/* eslint-enable no-unused-vars */
/**
* Related DICOM tag keys.
*/
const TagKeys = {
ImplementationClassUID: '00020012',
StudyInstanceUID: '0020000D',
StudyID: '00200010',
SeriesInstanceUID: '0020000E',
SeriesNumber: '00200011',
Modality: '00080060',
PatientName: '00100010',
PatientID: '00100020',
PatientBirthDate: '00100030',
PatientSex: '00100040',
CurrentRequestedProcedureEvidenceSequence: '0040A375'
};
/**
* Merge two tag lists.
*
* @param {object} tags1 Base list, will be modified.
* @param {object} tags2 List to merge.
*/
function mergeTags(tags1, tags2) {
const keys2 = Object.keys(tags2);
for (const tagName2 of keys2) {
if (tags1[tagName2] !== undefined) {
logger.debug('Overwritting tag: ' + tagName2);
}
tags1[tagName2] = tags2[tagName2];
}
}
/**
* Response evaluation class.
*/
export class ResponseEvaluation {
/**
* Current response.
*
* @type {DicomCode}
*/
current;
/**
* Measurement of response (mm).
*
* @type {number}
*/
measure;
}
/**
* CAD report class.
*/
export class CADReport {
/**
* @type {AnnotationGroup[]}
*/
annotationGroups;
/**
* @type {ResponseEvaluation[]}
*/
responseEvaluations;
/**
* @type {string}
*/
comment;
}
/**
* {@link AnnotationGroup} factory.
*/
export class AnnotationGroupFactory {
/**
* Possible warning created by checkElements.
*
* @type {string|undefined}
*/
#warning;
/**
* Get a warning string if elements are not as expected.
* Created by checkElements.
*
* @returns {string|undefined} The warning.
*/
getWarning() {
return this.#warning;
}
/**
* Check if input elements contain a dwv 0.34 annotation.
*
* @param {Object<string, DataElement>} dataElements The DICOM data elements.
* @returns {boolean} True if the elements contain a dwv 0.34 annotation.
*/
#isDwv034AnnotationDicomSR(dataElements) {
// version
const classUID =
safeGet(dataElements, TagKeys.ImplementationClassUID);
const dwvVersion = getDwvVersionFromImplementationClassUID(classUID);
const isDwv034 = typeof dwvVersion !== 'undefined' &&
isVersionInBounds(dwvVersion, '0.34.0', '0.35.0-beta.21');
// content template
const contentTemplate = getContentTemplate(dataElements);
// root SR concept
let rootConcept;
const srContent = getSRContent(dataElements);
if (typeof srContent.conceptNameCode !== 'undefined') {
rootConcept = srContent.conceptNameCode.value;
}
// dwv 0.34 annotations do not have template
return isDwv034 &&
typeof contentTemplate === 'undefined' &&
rootConcept === DcmCodes.MeasurementGroup.value;
}
/**
* Check if input elements contain a TID 1500 annotation.
* Ref: {@link https://dicom.nema.org/medical/Dicom/2022a/output/chtml/part16/chapter_A.html#sect_TID_1500}.
*
* @param {Object<string, DataElement>} dataElements The DICOM data elements.
* @returns {boolean} True if the elements contain a TID 1500 annotation.
*/
#isTid1500AnnotationDicomSR(dataElements) {
// content template
const contentTemplate = getContentTemplate(dataElements);
const isTid1500Template = contentTemplate === 'DCMR-1500';
// root SR concept
let rootConcept;
const srContent = getSRContent(dataElements);
if (typeof srContent.conceptNameCode !== 'undefined') {
rootConcept = srContent.conceptNameCode.value;
}
const isImagingMeasurementReport =
rootConcept === DcmCodes.ImagingMeasurementReport.value;
let res = false;
if (isTid1500Template && isImagingMeasurementReport) {
// check for at least one:
// ImagingMeasurements
// - MeasurementGroup
// - ImageRegion (SCOORD) OR TID 300 Measure
const imagingMeas = srContent.contentSequence.find(
this.#isImagingMeasurementsItem
);
if (typeof imagingMeas !== 'undefined') {
const measGroup = imagingMeas.contentSequence.find(
this.#isMeasurementGroupItem
);
if (typeof measGroup !== 'undefined') {
const imageRegion = measGroup.contentSequence.find(
this.#isImageRegionItem
);
const measure = measGroup.contentSequence.find(
this.#isTid300Measurement
);
if (typeof imageRegion !== 'undefined' ||
typeof measure !== 'undefined'
) {
res = true;
}
}
}
}
return res;
}
/**
* Check if input elements contain a TID 4100 template.
* Ref: {@link https://dicom.nema.org/medical/Dicom/2022a/output/chtml/part16/chapter_A.html#sect_TID_4100}.
*
* @param {Object<string, DataElement>} dataElements The DICOM data elements.
* @returns {boolean} True if the elements contain a TID 4100 template.
*/
#isTid4100DicomSR(dataElements) {
// content template
const contentTemplate = getContentTemplate(dataElements);
return contentTemplate === 'DCMR-4100';
}
/**
* Check dicom elements.
*
* @param {Object<string, DataElement>} dataElements The DICOM data elements.
* @returns {string|undefined} A possible warning.
*/
checkElements(dataElements) {
// reset
this.#warning = undefined;
if (!this.#isDwv034AnnotationDicomSR(dataElements) &&
!this.#isTid1500AnnotationDicomSR(dataElements) &&
!this.#isTid4100DicomSR(dataElements)) {
this.#warning = 'Not a dwv supported annotation';
}
return this.#warning;
}
/**
* Add the source image to an annotation.
*
* @param {Annotation} annotation The annotation.
* @param {DicomSRContent} content The content to add.
*/
#addSourceImageToAnnotation(annotation, content) {
if (content.valueType === ValueTypes.image &&
content.relationshipType === RelationshipTypes.selectedFrom) {
annotation.referencedSopClassUID =
content.value.referencedSOPSequence.referencedSOPClassUID;
annotation.referencedSopInstanceUID =
content.value.referencedSOPSequence.referencedSOPInstanceUID;
if (typeof content.value.referencedFrameNumber !== 'undefined') {
annotation.referencedFrameNumber =
parseInt(content.value.referencedFrameNumber, 10);
}
}
}
/**
* Add ids to an annotation folowing the (wrong)
* dwv 0.34 format.
*
* @param {Annotation} annotation The annotation.
* @param {DicomSRContent} content The content to add.
*/
#addDwv034IdToAnnotation(annotation, content) {
// annotation id
if (content.hasHeader(
ValueTypes.uidref,
getDcmDicomCode(DcmCodes.TrackingIdentifier),
RelationshipTypes.hasProperties
)) {
annotation.trackingId = content.value;
// use it as uid
annotation.trackingUid = content.value;
}
}
/**
* Add ids to an annotation.
*
* @param {Annotation} annotation The annotation.
* @param {DicomSRContent} content The content to add.
*/
#addIdsToAnnotation(annotation, content) {
// annotation id
if (content.hasHeader(
ValueTypes.text,
getDcmDicomCode(DcmCodes.TrackingIdentifier),
RelationshipTypes.hasObsContext
)) {
annotation.trackingId = content.value;
}
// annotation uid
if (content.hasHeader(
ValueTypes.uidref,
getDcmDicomCode(DcmCodes.TrackingUniqueIdentifier),
RelationshipTypes.hasObsContext
)) {
annotation.trackingUid = content.value;
}
}
/**
* Add content to an annotation.
*
* @param {Annotation} annotation The annotation.
* @param {DicomSRContent} content The content to add.
* @param {boolean} [isDwv034] True if the content was written using dwv 0.34,
* defaults to false.
*/
#addContentToAnnotation(annotation, content, isDwv034) {
if (typeof isDwv034 === 'undefined') {
isDwv034 = false;
}
let relationshipType = RelationshipTypes.hasConceptMod;
if (isDwv034) {
relationshipType = RelationshipTypes.hasProperties;
}
// text expr
if (content.hasHeader(
ValueTypes.text,
getDcmDicomCode(DcmCodes.ShortLabel),
relationshipType
)) {
annotation.textExpr = content.value;
// optional label position
const scoord = content.contentSequence.find(function (item) {
return item.hasHeader(
ValueTypes.scoord,
getDcmDicomCode(DcmCodes.ReferencePoints),
RelationshipTypes.hasProperties
);
});
if (typeof scoord !== 'undefined') {
annotation.labelPosition = new Point2D(
scoord.value.graphicData[0],
scoord.value.graphicData[1]
);
}
}
// color
if (content.hasHeader(
ValueTypes.text,
getColourCode(),
relationshipType
)) {
annotation.colour = content.value;
}
// reference points
if (content.hasHeader(
ValueTypes.scoord,
getDcmDicomCode(DcmCodes.ReferencePoints),
relationshipType) &&
content.value.graphicType === GraphicTypes.multipoint
) {
const points = [];
for (let i = 0; i < content.value.graphicData.length; i += 2) {
points.push(new Point2D(
content.value.graphicData[i],
content.value.graphicData[i + 1]
));
}
annotation.referencePoints = points;
}
// plane points
if (content.hasHeader(
ValueTypes.scoord3d,
getDcmDicomCode(DcmCodes.ReferenceGeometry),
RelationshipTypes.contains) &&
content.value.graphicType === GraphicTypes.multipoint
) {
const data = content.value.graphicData;
const points = [];
const nPoints = Math.floor(data.length / 3);
for (let i = 0; i < nPoints; ++i) {
const j = i * 3;
points.push(new Point3D(data[j], data[j + 1], data[j + 2]));
}
annotation.planePoints = points;
}
// quantification
this.#addQuantificationToAnnotation(annotation, content);
// meta
this.#addMetaToAnnotation(annotation, content);
}
/**
* Add quantification to an annotation.
*
* @param {Annotation} annotation The annotation.
* @param {DicomSRContent} content The content to add.
* @param {string} [relationshipType] The content relationshipType, defaults
* to 'CONTAINS'.
*/
#addQuantificationToAnnotation(annotation, content, relationshipType) {
let relation;
if (typeof relationshipType === 'undefined') {
relation = RelationshipTypes.contains;
}
if (content.valueType === ValueTypes.num &&
content.relationshipType === relation) {
const quantifName =
getQuantificationName(content.conceptNameCode);
if (typeof quantifName !== 'undefined') {
const measuredValue = content.value.measuredValue;
const quantifUnit = getQuantificationUnit(
measuredValue.measurementUnitsCode);
if (typeof annotation.quantification === 'undefined') {
annotation.quantification = {};
}
annotation.quantification[quantifName] = {
value: measuredValue.numericValue,
unit: quantifUnit
};
}
}
}
/**
* Add meta to an annotation.
*
* @param {Annotation} annotation The annotation.
* @param {DicomSRContent} content The content to add.
*/
#addMetaToAnnotation(annotation, content) {
if ((content.valueType === ValueTypes.code ||
content.valueType === ValueTypes.text) &&
content.relationshipType === RelationshipTypes.contains) {
annotation.addMetaItem(
content.conceptNameCode,
content.value
);
}
}
/**
* Check if a DicomSRContent is an 'ImagingMeasurements'.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) CONTAINER: (126010, DCM, 'Imaging Measurements').
*/
#isImagingMeasurementsItem(item) {
return item.hasHeader(
ValueTypes.container,
getDcmDicomCode(DcmCodes.ImagingMeasurements),
RelationshipTypes.contains
);
}
/**
* Check if a DicomSRContent is a 'MeasurementGroup'.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) CONTAINER: (125007, DCM, 'Measurement Group').
*/
#isMeasurementGroupItem(item) {
return item.hasHeader(
ValueTypes.container,
getDcmDicomCode(DcmCodes.MeasurementGroup),
RelationshipTypes.contains
);
}
/**
* Check if a DicomSRContent is a 'SingleImageFinding' code.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (INFERRED FROM) CODE: (111059, DCM, 'Single Image Finding').
*/
#isSingleImageFindingItem(item) {
return item.hasHeader(
ValueTypes.code,
getDcmDicomCode(DcmCodes.SingleImageFinding),
RelationshipTypes.inferredFrom
);
}
/**
* Check if a DicomSRContent is a 'ResponseEvaluation' container.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (HAS PROPERTIES) CODE: (112020, DCM, 'Response Evaluation').
*/
#isResponseEvaluationItem(item) {
return item.hasHeader(
ValueTypes.container,
getDcmDicomCode(DcmCodes.ResponseEvaluation),
RelationshipTypes.hasProperties
);
}
/**
* Check if a DicomSRContent is a 'Comment' text.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) TEXT: (121106, DCM, 'Comment').
*/
#isCommentItem(item) {
return item.hasHeader(
ValueTypes.text,
getDcmDicomCode(DcmCodes.Comment),
RelationshipTypes.contains
);
}
/**
* Check if a DicomSRContent is a 'CurrentResponse' code.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) CODE: (112048, DCM, 'Current Response').
*/
#isCurrentResponseItem(item) {
return item.hasHeader(
ValueTypes.code,
getDcmDicomCode(DcmCodes.CurrentResponse),
RelationshipTypes.contains
);
}
/**
* Check if a DicomSRContent is a 'MeasurementOfResponse' number.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) NUM: (112051, DCM, 'Measurement Of Response').
*/
#isMeasurementOfResponseItem(item) {
return item.hasHeader(
ValueTypes.num,
getDcmDicomCode(DcmCodes.MeasurementOfResponse),
RelationshipTypes.contains
);
}
/**
* Check if a DicomSRContent is a 'CADProcessingAndFindingsSummary' code.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) CODE: (111017, DCM, 'CAD Processing and Findings Summary').
*/
#isCadProcessingSummaryItem(item) {
return item.hasHeader(
ValueTypes.code,
getDcmDicomCode(DcmCodes.CADProcessingAndFindingsSummary),
RelationshipTypes.contains
);
}
/**
* Check if a DicomSRContent is an 'ImageRegion'.
*
* @param {DicomSRContent} item The item to check.
* @returns {boolean} True if item has the properties:
* (CONTAINS) SCOORD: (111030, DCM, 'Image Region').
*/
#isImageRegionItem(item) {
return item.hasHeader(
ValueTypes.scoord,
getDcmDicomCode(DcmCodes.ImageRegion),
RelationshipTypes.contains
);
}
/**
* Check is a measurement group follows TID 1410:
* it must contain an image region.
* Ref: {@link https://dicom.nema.org/medical/Dicom/2022a/output/chtml/part16/chapter_A.html#sect_TID_1410}.
*
* @param {DicomSRContent} content The SR content.
* @returns {boolean} True if an image region was found.
*/
#isTid1410MeasGroup(content) {
const scoord = content.contentSequence.find(
this.#isImageRegionItem
);
return typeof scoord !== 'undefined';
}
/**
* Convert a TID 1410 measurement group into an annotation.
*
* @param {DicomSRContent} content The SR content.
* @returns {Annotation|undefined} The annotation.
*/
#tid1410MeasGroupToAnnotation(content) {
let annotation;
// get shape from scoord
const scoord = content.contentSequence.find(
this.#isImageRegionItem
);
if (typeof scoord !== 'undefined') {
annotation = new Annotation();
// shape
annotation.mathShape = getShapeFromScoord(scoord.value);
// shape source image
const fromImage = scoord.contentSequence.find(function (item) {
return item.valueType === ValueTypes.image &&
item.relationshipType === RelationshipTypes.selectedFrom;
});
if (typeof fromImage !== 'undefined') {
this.#addSourceImageToAnnotation(annotation, fromImage);
}
for (const item of content.contentSequence) {
// shape ids
this.#addIdsToAnnotation(annotation, item);
// shape extra
this.#addContentToAnnotation(annotation, item);
}
}
return annotation;
}
/**
* Check is an SR content follows TID 300: it must be
* a 'contains' numeric value with an 'inferred from' scoord.
* Ref: {@link https://dicom.nema.org/medical/Dicom/2022a/output/chtml/part16/chapter_A.html#sect_TID_300}.
*
* @param {DicomSRContent} item The SR content.
* @returns {boolean} True if TID 300 measure.
*/
#isTid300Measurement(item) {
// no specific concept
const isContainedNum =
item.valueType === ValueTypes.num &&
item.relationshipType === RelationshipTypes.contains;
let scoord;
if (isContainedNum) {
// no specific concept (?)
scoord = item.contentSequence.find(function (subItem) {
return subItem.valueType === ValueTypes.scoord &&
subItem.relationshipType === RelationshipTypes.inferredFrom;
});
}
return typeof scoord !== 'undefined';
}
/**
* Check is an SR content follows TID 1400: it must be
* a 'has properties' numeric value with an 'inferred from' scoord.
* Ref: {@link https://dicom.nema.org/medical/Dicom/current/output/chtml/part16/chapter_A.html#sect_TID_1400}.
*
* @param {DicomSRContent} item The SR content.
* @returns {boolean} True if TID 300 measure.
*/
#isTid1400LinearMeasurement(item) {
// no specific concept
const isHasPropNum =
item.valueType === ValueTypes.num &&
item.relationshipType === RelationshipTypes.hasProperties;
let scoord;
if (isHasPropNum) {
// no specific concept (?)
scoord = item.contentSequence.find(function (subItem) {
return subItem.valueType === ValueTypes.scoord &&
subItem.relationshipType === RelationshipTypes.inferredFrom;
});
}
return typeof scoord !== 'undefined';
}
/**
* Check is a measurement group follows TID 1501:
* it must contain a measure.
* Ref: {@link https://dicom.nema.org/medical/Dicom/2022a/output/chtml/part16/chapter_A.html#sect_TID_1501}.
*
* @param {DicomSRContent} content The SR content.
* @returns {boolean} True if a measure was found.
*/
#isTid1501MeasGroup(content) {
const measure = content.contentSequence.find(
this.#isTid300Measurement
);
return typeof measure !== 'undefined';
}
/**
* Convert a TID 1501 measurement group into an annotation.
*
* @param {DicomSRContent} content The SR content.
* @returns {Annotation|undefined} The annotation.
*/
#tid1501MeasGroupToAnnotation(content) {
let annotation;
// just use the first measure to get the scoord
// (expecting all measures to refer to the same scoord)
const measure = content.contentSequence.find(
this.#isTid300Measurement
);
if (typeof measure !== 'undefined') {
annotation = new Annotation();
// shape
// no specific concept (?)
const scoord = measure.contentSequence.find(function (subItem) {
return subItem.valueType === ValueTypes.scoord &&
subItem.relationshipType === RelationshipTypes.inferredFrom;
});
annotation.mathShape = getShapeFromScoord(scoord.value);
// special point/arrow case
// TODO: not very valid...
if (annotation.mathShape instanceof Point2D &&
scoord.value.graphicData.length >= 4
) {
annotation.referencePoints = [
new Point2D(scoord.value.graphicData[2], scoord.value.graphicData[3])
];
}
// shape source image
// no specific concept (?)
const fromImage = scoord.contentSequence.find(function (item) {
return item.valueType === ValueTypes.image &&
item.relationshipType === RelationshipTypes.selectedFrom;
});
if (typeof fromImage !== 'undefined') {
this.#addSourceImageToAnnotation(annotation, fromImage);
}
}
// shape extra
if (typeof annotation !== 'undefined') {
for (const item of content.contentSequence) {
// add ids
this.#addIdsToAnnotation(annotation, item);
// add quantification
this.#addQuantificationToAnnotation(annotation, measure);
// add meta
this.#addMetaToAnnotation(annotation, item);
}
}
return annotation;
}
/**
* Convert a TID 4104 image finding into an annotation.
* Similar to tid1501MeasGroupToAnnotation apart from measure
* code and quantification relationship.
*
* @param {DicomSRContent} content The SR content.
* @returns {Annotation|undefined} The annotation.
*/
#singleImageFindingToAnnotation(content) {
let annotation;
// just use the first measure to get the scoord
// (expecting all measures to refer to the same scoord)
const measure = content.contentSequence.find(
this.#isTid1400LinearMeasurement
);
if (typeof measure !== 'undefined') {
annotation = new Annotation();
// shape
// no specific concept (?)
const scoord = measure.contentSequence.find(function (subItem) {
return subItem.valueType === ValueTypes.scoord &&
subItem.relationshipType === RelationshipTypes.inferredFrom;
});
annotation.mathShape = getShapeFromScoord(scoord.value);
// special point/arrow case
// TODO: not very valid...
if (annotation.mathShape instanceof Point2D &&
scoord.value.graphicData.length >= 4
) {
annotation.referencePoints = [
new Point2D(scoord.value.graphicData[2], scoord.value.graphicData[3])
];
}
// shape source image
// no specific concept (?)
const fromImage = scoord.contentSequence.find(function (item) {
return item.valueType === ValueTypes.image &&
item.relationshipType === RelationshipTypes.selectedFrom;
});
if (typeof fromImage !== 'undefined') {
this.#addSourceImageToAnnotation(annotation, fromImage);
}
}
// shape extra
if (typeof annotation !== 'undefined') {
for (const item of content.contentSequence) {
// add ids
this.#addIdsToAnnotation(annotation, item);
// add quantification
this.#addQuantificationToAnnotation(
annotation, measure, RelationshipTypes.hasProperties);
// add meta
this.#addMetaToAnnotation(annotation, item);
}
}
return annotation;
}
/**
* Convert an imaging measurement into an annotation group.
* Supports TID1500 > TID1410 ("Planar ROI Measurements
* and Qualitative Evaluations”) or TID1501 (“Measurement and
* Qualitative Evaluation Group”).
*
* @param {DicomSRContent} content The SR content.
* @returns {AnnotationGroup|undefined} The annotation group.
*/
#imagingMeasToAnnotationGroup(content) {
if (content.contentSequence.length === 0) {
return undefined;
}
const item0 = content.contentSequence[0];
let isTid1410 = false;
let isTid1501 = false;
if (this.#isMeasurementGroupItem(item0)) {
isTid1410 = this.#isTid1410MeasGroup(item0);
isTid1501 = this.#isTid1501MeasGroup(item0);
}
const annotations = [];
let hasMeasGroup = false;
for (const item of content.contentSequence) {
// measurement group content
if (this.#isMeasurementGroupItem(item)) {
hasMeasGroup = true;
let annotation;
if (isTid1410) {
annotation = this.#tid1410MeasGroupToAnnotation(item);
} else if (isTid1501) {
annotation = this.#tid1501MeasGroupToAnnotation(item);
}
if (typeof annotation !== 'undefined') {
annotations.push(annotation);
}
}
}
let annotationGroup;
if (!hasMeasGroup) {
logger.warn('No measurement groups in TID 1500 SR');
} else {
if (annotations.length !== 0) {
annotationGroup = new AnnotationGroup(annotations);
} else {
logger.warn('No valid measurement groups in TID 1500 SR');
}
}
return annotationGroup;
}
/**
* Convert a CAD processing summary into an annotation group.
*
* @param {DicomSRContent} content The SR content.
* @returns {AnnotationGroup[]|undefined} The annotation group.
*/
#cadProcessingSummaryToAnnotationGroups(content) {
// get annotations
const annotations = [];
let hasMeasGroup = false;
for (const item of content.contentSequence) {
// measurement group content
if (this.#isSingleImageFindingItem(item)) {
hasMeasGroup = true;
const annotation = this.#singleImageFindingToAnnotation(item);
if (typeof annotation !== 'undefined') {
annotations.push(annotation);
}
}
}
// TODO: split annotations according to referenced image.
// create group
const annotationGroups = [];
if (!hasMeasGroup) {
logger.warn('No image findings in TID 4100 SR');
} else {
if (annotations.length !== 0) {
annotationGroups.push(new AnnotationGroup(annotations));
} else {
logger.warn('No valid measurement groups in TID 4100 SR');
}
}
return annotationGroups;
}
/**
* Convert a DICOM SR content of type SCOORD into an annotation.
*
* @param {DicomSRContent} content The input SCOORD.
* @returns {Annotation} The annotation.
*/
#dwv034ScoordToAnnotation(content) {
const annotation = new Annotation();
// shape
annotation.mathShape = getShapeFromScoord(content.value);
for (const item of content.contentSequence) {
// shape source image
this.#addSourceImageToAnnotation(annotation, item);
// shape id
this.#addDwv034IdToAnnotation(annotation, item);
// shape extra
this.#addContentToAnnotation(annotation, item, true);
}
return annotation;
}
/**
* Convert a DICOM SR content folowing TID 1500 into a list of annotations.
*
* Structure: 'Imaging Measurement Report' >
* 'Imaging Measurements' > 'Measurement Group'.
*
* Measurement Group can follow TID1410 "Planar ROI Measurements
* and Qualitative Evaluations” (scoord and measurements at
* same level) or TID1501 “Measurement and Qualitative Evaluation Group”
* (measurements with scoord child).
*
* @param {DicomSRContent} content The input SR content.
* @returns {AnnotationGroup|undefined} The annotation group.
*/
#tid1500ToAnnotationGroup(content) {
if (!(content.valueType === ValueTypes.container &&
isEqualCode(
content.conceptNameCode,
getDcmDicomCode(DcmCodes.ImagingMeasurementReport)
)
)) {
logger.warn('Not the expected TID 1500 SR content header');
}
let annotationGroup;
// imaging measurements content
const imagingMeas = content.contentSequence.find(
this.#isImagingMeasurementsItem
);
if (typeof imagingMeas !== 'undefined') {
annotationGroup = this.#imagingMeasToAnnotationGroup(imagingMeas);
} else {
logger.warn('No imaging measurements in TID 1500 SR');
}
return annotationGroup;
}
/**
* Get the CAD processing and findings summary from
* and input SR content.
*
* @param {DicomSRContent} content The input SR content.
* @returns {DicomSRContent|undefined} The summary.
*/
#getTid4100Summary(content) {
if (!(content.valueType === ValueTypes.container &&
isEqualCode(
content.conceptNameCode,
getDcmDicomCode(DcmCodes.ChestCADReport)
)
)) {