Skip to content

Commit

Permalink
Merge pull request #2490 from michaelkain/frameOfReferenceUID-fix
Browse files Browse the repository at this point in the history
Frame of reference UID fix
  • Loading branch information
michaelkain authored Dec 2, 2024
2 parents aafdf5f + f94e0e1 commit 2efa56a
Showing 1 changed file with 42 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ public void anonymize(ArrayList<File> dicomFiles, String profile) throws Excepti
tagsToDeleteForManufacturer = AnonymizationRulesSingleton.getInstance().getTagsToDeleteForManufacturer();
// init here for multi-threading reasons
Map<String, String> seriesInstanceUIDs = new HashMap<>();
Map<String, String> frameOfReferenceUIDs = new HashMap<>();
Map<String, String> studyInstanceUIDs = new HashMap<>();
Map<String, String> studyIds = new HashMap<>();
LOG.debug("anonymize : totalAmount={}", totalAmount);
int current = 0;
for (int i = 0; i < dicomFiles.size(); ++i) {
final File file = dicomFiles.get(i);
// Perform the anonymization
performAnonymization(file, anonymizationMap, false, "", "", seriesInstanceUIDs, studyInstanceUIDs, studyIds);
performAnonymization(file, anonymizationMap, false, "", "", seriesInstanceUIDs, frameOfReferenceUIDs, studyInstanceUIDs, studyIds);
current++;
final int currentPercent = current * 100 / totalAmount;
LOG.debug("anonymize : anonymization current percent= {} %", currentPercent);
Expand All @@ -99,14 +100,15 @@ public void anonymizeForShanoir(ArrayList<File> dicomFiles, String profile, Stri

// init here for multi-threading reasons
Map<String, String> seriesInstanceUIDs = new HashMap<>();
Map<String, String> frameOfReferenceUIDs = new HashMap<>();
Map<String, String> studyInstanceUIDs = new HashMap<>();
Map<String, String> studyIds = new HashMap<>();
LOG.debug("anonymize : totalAmount={}", totalAmount);
int current = 0;
for (int i = 0; i < dicomFiles.size(); ++i) {
final File file = dicomFiles.get(i);
// Perform the anonymization
performAnonymization(file, anonymizationMap, true, patientName, patientID, seriesInstanceUIDs, studyInstanceUIDs, studyIds);
performAnonymization(file, anonymizationMap, true, patientName, patientID, seriesInstanceUIDs, frameOfReferenceUIDs, studyInstanceUIDs, studyIds);
current++;
final int currentPercent = current * 100 / totalAmount;
LOG.debug("anonymize : anonymization current percent= {} %", currentPercent);
Expand Down Expand Up @@ -156,7 +158,7 @@ private void anonymizePatientMetaData(Attributes attributes, String patientName,
* @throws Exception
*/
public void performAnonymization(final File dicomFile, Map<String, String> anonymizationMap, boolean isShanoirAnonymization,
String patientName, String patientID, Map<String, String> seriesInstanceUIDs,
String patientName, String patientID, Map<String, String> seriesInstanceUIDs, Map<String, String> frameOfReferenceUIDs,
Map<String, String> studyInstanceUIDs, Map<String, String> studyIds) throws Exception {
DicomInputStream din = null;
DicomOutputStream dos = null;
Expand Down Expand Up @@ -213,18 +215,17 @@ public void performAnonymization(final File dicomFile, Map<String, String> anony
anonymizeTag(tagInt, action, datasetAttributes);
// even: public tags
} else if (anonymizationMap.containsKey(tagString)) {
if (tagInt == Tag.SOPInstanceUID) {
anonymizeSOPInstanceUID(tagInt, datasetAttributes, mediaStorageSOPInstanceUIDGenerated);
} else if (tagInt == Tag.SeriesInstanceUID) {
anonymizeSeriesInstanceUID(tagInt, datasetAttributes, seriesInstanceUIDs);
} else if (tagInt == Tag.StudyInstanceUID) {
anonymizeStudyInstanceUID(tagInt, datasetAttributes, studyInstanceUIDs);
} else if (tagInt == Tag.StudyID) {
anonymizeStudyId(tagInt, datasetAttributes, studyIds);
} else {
final String action = anonymizationMap.get(tagString);
anonymizeTag(tagInt, action, datasetAttributes);
}
switch (tagInt) {
case Tag.SOPInstanceUID -> anonymizeSOPInstanceUID(tagInt, datasetAttributes, mediaStorageSOPInstanceUIDGenerated);
case Tag.SeriesInstanceUID -> anonymizeUID(tagInt, datasetAttributes, seriesInstanceUIDs);
case Tag.FrameOfReferenceUID -> anonymizeUID(tagInt, datasetAttributes, frameOfReferenceUIDs);
case Tag.StudyInstanceUID -> anonymizeUID(tagInt, datasetAttributes, studyInstanceUIDs);
case Tag.StudyID -> anonymizeStudyId(tagInt, datasetAttributes, studyIds);
default -> {
final String action = anonymizationMap.get(tagString);
anonymizeTag(tagInt, action, datasetAttributes);
}
}
} else {
if (0x50000000 <= tagInt && tagInt <= 0x50FFFFFF) {
final String action = anonymizationMap.get(CURVE_DATA_TAGS);
Expand Down Expand Up @@ -350,46 +351,6 @@ private void anonymizeSOPInstanceUID(int tagInt, Attributes attributes, String m
anonymizeTagAccordingToVR(attributes, tagInt, mediaStorageSOPInstanceUID);
}

private void anonymizeSeriesInstanceUID(int tagInt, Attributes attributes, Map<String, String> seriesInstanceUIDs) {
String value;
if (seriesInstanceUIDs != null && seriesInstanceUIDs.size() != 0
&& seriesInstanceUIDs.get(attributes.getString(tagInt)) != null) {
value = seriesInstanceUIDs.get(attributes.getString(tagInt));
} else {
UIDGeneration generator = new UIDGeneration();
String newUID = null;
try {
newUID = generator.getNewUID();
} catch (Exception e) {
LOG.error(e.getMessage());
}
value = newUID;
seriesInstanceUIDs.put(attributes.getString(tagInt), value);
}
anonymizeTagAccordingToVR(attributes, tagInt, value);
}

private void anonymizeStudyInstanceUID(int tagInt, Attributes attributes, Map<String, String> studyInstanceUIDs) {
String value;
if (studyInstanceUIDs != null && studyInstanceUIDs.size() != 0
&& studyInstanceUIDs.get(attributes.getString(tagInt)) != null) {
value = studyInstanceUIDs.get(attributes.getString(tagInt));
LOG.debug("Existing StudyInstanceUID reused: {}", value);
} else {
UIDGeneration generator = new UIDGeneration();
String newUID = null;
try {
newUID = generator.getNewUID();
} catch (Exception e) {
LOG.error(e.getMessage());
}
value = newUID;
LOG.info("New StudyInstanceUID generated for DICOM study/exam: {}", newUID);
studyInstanceUIDs.put(attributes.getString(tagInt), value);
}
anonymizeTagAccordingToVR(attributes, tagInt, value);
}

private void anonymizeStudyId(int tagInt, Attributes attributes, Map<String, String> studyIds) {
String value;
if (studyIds != null && studyIds.size() != 0 && studyIds.get(attributes.getString(tagInt)) != null) {
Expand Down Expand Up @@ -523,4 +484,30 @@ else if (vr.equals(VR.AE) || vr.equals(VR.AS) || vr.equals(VR.CS) || vr.equals(V
// VR.OD = Other Double String
}

private void anonymizeUID(int tagInt, Attributes attributes, Map<String, String> UIDs) {
String value;
if (UIDs != null && UIDs.size() != 0
&& UIDs.get(attributes.getString(tagInt)) != null) {
value = UIDs.get(attributes.getString(tagInt));
// We log only concerning the studyInstanceUID
if(Tag.StudyInstanceUID == tagInt) {
LOG.debug("Existing StudyInstanceUID reused: {}", value);
}
} else {
UIDGeneration generator = new UIDGeneration();
String newUID = null;
try {
newUID = generator.getNewUID();
} catch (Exception e) {
LOG.error(e.getMessage());
}
value = newUID;
if(Tag.StudyInstanceUID == tagInt) {
LOG.info("New StudyInstanceUID generated for DICOM study/exam: {}", newUID);
}
UIDs.put(attributes.getString(tagInt), value);
}
anonymizeTagAccordingToVR(attributes, tagInt, value);
}

}

0 comments on commit 2efa56a

Please sign in to comment.