-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathanonymizer.test.js
176 lines (139 loc) · 5.62 KB
/
anonymizer.test.js
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
import dcmjs from "../src/index.js";
import fs from "fs";
import { validationLog } from "./../src/log.js";
// Ignore validation errors
validationLog.setLevel(5);
const { DicomMessage } = dcmjs.data;
const { cleanTags, getTagsNameToEmpty } = dcmjs.anonymizer;
it("test_export", () => {
expect(typeof cleanTags).toEqual("function");
});
it("test_anonymization", () => {
// given
const arrayBuffer = fs.readFileSync("test/sample-dicom.dcm").buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const tagInfo = dcmjs.data.DicomMetaDictionary.nameMap["PatientName"];
const tagNumber = tagInfo.tag,
tagString = dcmjs.data.Tag.fromPString(tagNumber).toCleanString();
const patientIDTag = dicomDict.dict[tagString];
const patientIDValue = patientIDTag.Value;
expect(JSON.stringify(patientIDValue)).toEqual(
JSON.stringify([{ Alphabetic: "Fall 3" }])
);
expect(patientIDValue.toString()).toEqual(["Fall 3"].toString());
// when
cleanTags(dicomDict.dict);
// then
expect(JSON.stringify(patientIDTag.Value)).toEqual(
JSON.stringify([{ Alphabetic: "ANON^PATIENT" }])
);
expect(patientIDTag.Value.toString()).toEqual(["ANON^PATIENT"].toString());
});
it("test_anonymization_no_change_ref", () => {
// given
const arrayBuffer = fs.readFileSync("test/sample-sr.dcm").buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
// multiple value name
const tagInfo = dcmjs.data.DicomMetaDictionary.nameMap["OtherPatientNames"];
const tagNumber = tagInfo.tag,
tagString = dcmjs.data.Tag.fromPString(tagNumber).toCleanString();
const otherPatientNamesIDTag = dicomDict.dict[tagString];
const otherPatientNamesIDValue = otherPatientNamesIDTag.Value;
const otherPatientNamesIDValueJSON = JSON.stringify(
otherPatientNamesIDValue
);
expect(JSON.stringify(otherPatientNamesIDValue)).toEqual(
JSON.stringify([
{
Alphabetic: "Doe^John",
Ideographic: "Johnny",
Phonetic: "Jonny"
},
{ Alphabetic: "Doe^Jane", Ideographic: "Janie", Phonetic: "Jayne" }
])
);
expect(otherPatientNamesIDValue.toString()).toEqual(
["Doe^John=Johnny=Jonny\\Doe^Jane=Janie=Jayne"].toString()
);
// when
cleanTags(dicomDict.dict, { "00101001": "ANON^PATIENT" });
// then
expect(JSON.stringify(otherPatientNamesIDTag.Value)).toEqual(
JSON.stringify([{ Alphabetic: "ANON^PATIENT" }])
);
expect(otherPatientNamesIDTag.Value.toString()).toEqual(
["ANON^PATIENT"].toString()
);
expect(JSON.stringify(otherPatientNamesIDValue)).toEqual(
otherPatientNamesIDValueJSON
);
});
it("test_anonymization_tagtoreplace_param", () => {
// given
const arrayBuffer = fs.readFileSync("test/sample-dicom.dcm").buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const tagInfo = dcmjs.data.DicomMetaDictionary.nameMap["PatientName"];
const tagNumber = tagInfo.tag,
tagString = dcmjs.data.Tag.fromPString(tagNumber).toCleanString();
const patientNameTag = dicomDict.dict[tagString];
const patientNameValue = patientNameTag.Value;
expect(JSON.stringify(patientNameValue)).toEqual(
JSON.stringify([{ Alphabetic: "Fall 3" }])
);
expect(patientNameValue.toString()).toEqual(["Fall 3"].toString());
var tagsToReplace = {
"00100010": "REPLACE^PATIENT"
};
// when
cleanTags(dicomDict.dict, tagsToReplace);
// then
expect(JSON.stringify(patientNameTag.Value)).toEqual(
JSON.stringify([{ Alphabetic: "REPLACE^PATIENT" }])
);
expect(patientNameTag.Value.toString()).toEqual(
["REPLACE^PATIENT"].toString()
);
});
it("test_anonymization_keep_tag", () => {
// given
const arrayBuffer = fs.readFileSync("test/sample-dicom.dcm").buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const tagInfo = dcmjs.data.DicomMetaDictionary.nameMap["SeriesDescription"];
const tagNumber = tagInfo.tag,
tagString = dcmjs.data.Tag.fromPString(tagNumber).toCleanString();
const seriesDescriptionTag = dicomDict.dict[tagString];
const seriesDescriptionValue = seriesDescriptionTag.Value;
expect(seriesDescriptionValue).toEqual(["Oberbauch *sSSH/FB/4mm"]);
var tagsToReplace = {};
var tagsToKeep = getTagsNameToEmpty();
var seriesDescription = "SeriesDescription";
if (tagsToKeep.indexOf(seriesDescription) != -1) {
tagsToKeep.splice(tagsToKeep.indexOf(seriesDescription), 1);
}
// when
cleanTags(dicomDict.dict, tagsToReplace, tagsToKeep);
// then
expect(seriesDescriptionTag.Value).toEqual(["Oberbauch *sSSH/FB/4mm"]);
});
it("test_anonymization_anonymize_tag", () => {
// given
const arrayBuffer = fs.readFileSync("test/sample-dicom.dcm").buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const tagInfo = dcmjs.data.DicomMetaDictionary.nameMap["SeriesInstanceUID"];
const tagNumber = tagInfo.tag,
tagString = dcmjs.data.Tag.fromPString(tagNumber).toCleanString();
const SeriesInstanceUIDTag = dicomDict.dict[tagString];
const SeriesInstanceUIDValue = SeriesInstanceUIDTag.Value;
expect(SeriesInstanceUIDValue).toEqual([
"1.2.276.0.50.192168001092.11156604.14547392.303"
]);
var tagsToReplace = {};
var tagsToAnon = getTagsNameToEmpty();
if (!tagsToAnon.includes("SeriesInstanceUID")) {
tagsToAnon.push("SeriesInstanceUID");
}
// when
cleanTags(dicomDict.dict, tagsToReplace, tagsToAnon);
// then
expect(SeriesInstanceUIDTag.Value).toEqual([]);
});