Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame of reference UID fix #2490

Merged
merged 5 commits into from
Dec 2, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,25 @@ 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) {
michaelkain marked this conversation as resolved.
Show resolved Hide resolved
String value;
if (UIDs != null && UIDs.size() != 0
&& UIDs.get(attributes.getString(tagInt)) != null) {
value = UIDs.get(attributes.getString(tagInt));
LOG.debug("Existing DICOM metadata UID with tag {} reused: {}", tagInt, 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 DICOM metadata UID with tag {} generated for DICOM study/exam: {}", tagInt, newUID);
UIDs.put(attributes.getString(tagInt), value);
}
anonymizeTagAccordingToVR(attributes, tagInt, value);
}

}