Skip to content

Commit

Permalink
Merge pull request #204 from ADIS-SA/feat/shift_by_date
Browse files Browse the repository at this point in the history
Feat/shift by date
  • Loading branch information
nroduit authored May 4, 2023
2 parents 9c6ae5e + 4394cc5 commit 628ca43
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.karnak.backend.model.profilepipe.HMAC;
import org.karnak.backend.model.profilepipe.TagActionMap;
import org.karnak.backend.util.DateFormat;
import org.karnak.backend.util.ShiftByTagDate;
import org.karnak.backend.util.ShiftDate;
import org.karnak.backend.util.ShiftRangeDate;
import org.slf4j.Logger;
Expand Down Expand Up @@ -68,18 +69,19 @@ public void profileValidation() throws Exception {
throw new Exception(
"Cannot build the profile "
+ codeName
+ " : An option must be given. Option available: [shift, shift_range]");
+ " : An option must be given. Option available: [shift, shift_range, shift_by_tag, date_format]");
}
switch (option) {
case "shift" -> ShiftDate.verifyShiftArguments(argumentEntities);
case "shift_range" -> ShiftRangeDate.verifyShiftArguments(argumentEntities);
case "shift_by_tag" -> ShiftByTagDate.verifyShiftArguments(argumentEntities);
case "date_format" -> DateFormat.verifyPatternArguments(argumentEntities);
default -> throw new Exception(
"Cannot build the profile "
+ codeName
+ " with the option given "
+ option
+ " : Option available (shift, shift_range)");
+ " : Option available (shift, shift_range, shift_by_tag, date_format)");
}
} catch (Exception e) {
throw e;
Expand Down Expand Up @@ -127,6 +129,7 @@ private String applyOption(Attributes dcmCopy, int tag, HMAC hmac) throws DateTi
return switch (option) {
case "shift" -> ShiftDate.shift(dcmCopy, tag, argumentEntities);
case "shift_range" -> ShiftRangeDate.shift(dcmCopy, tag, argumentEntities, hmac);
case "shift_by_tag" -> ShiftByTagDate.shift(dcmCopy, tag, argumentEntities, hmac);
case "date_format" -> DateFormat.format(dcmCopy, tag, argumentEntities);
default -> null;
};
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/org/karnak/backend/util/ShiftByTagDate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2020-2021 Karnak Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.karnak.backend.util;

import org.dcm4che3.data.Attributes;
import org.karnak.backend.data.entity.ArgumentEntity;
import org.karnak.backend.model.expression.ExprCondition;
import org.karnak.backend.model.profilepipe.HMAC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ShiftByTagDate {
private static final Logger LOGGER = LoggerFactory.getLogger(ShiftByTagDate.class);

private ShiftByTagDate() {
}

public static void verifyShiftArguments(List<ArgumentEntity> argumentEntities) {
// All arguments are optional
}

public static String shift(Attributes dcm, int tag, List<ArgumentEntity> argumentEntities, HMAC hmac) {
try {
verifyShiftArguments(argumentEntities);
} catch (IllegalArgumentException e) {
throw e;
}

String dcmElValue = dcm.getString(tag);
String shiftDaysTag = "";
String shiftSecondsTag = "";

for (ArgumentEntity argumentEntity : argumentEntities) {
final String key = argumentEntity.getKey();
final String value = argumentEntity.getValue();

try {
if (key.equals("days_tag")) {
shiftDaysTag = value;
}
if (key.equals("seconds_tag")) {
shiftSecondsTag = value;
}
} catch (Exception e) {
LOGGER.error("args {} is not correct", value, e);
}
}

final String shiftDaysValue = dcm.getString(ExprCondition.intFromHexString(shiftDaysTag));
final String shiftSecondsValue = dcm.getString(ExprCondition.intFromHexString(shiftSecondsTag));

int shiftDays = 0;
int shiftSeconds = 0;
try {
if (shiftDaysValue != null) {
shiftDays = Integer.parseInt(shiftDaysValue);
}
} catch (Exception e) {
LOGGER.error("args {} is not correct", shiftDaysValue, e);
}
try {
if (shiftSecondsValue != null) {
shiftSeconds = Integer.parseInt(shiftSecondsValue);
}
} catch (Exception e) {
LOGGER.error("args {} is not correct", shiftSecondsValue, e);
}

return ShiftDate.shiftValue(dcm, tag, dcmElValue, shiftDays, shiftSeconds);
}
}
15 changes: 10 additions & 5 deletions src/main/java/org/karnak/backend/util/ShiftDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,20 @@ public static String shift(Attributes dcm, int tag, List<ArgumentEntity> argumen
LOGGER.error("args {} is not correct", value, e);
}
}
return shiftValue(dcm, tag, dcmElValue, shiftDays, shiftSeconds);
}

public static String shiftValue(Attributes dcm, int tag, String dcmElValue, int shiftDays, int shiftSeconds) {
if (dcmElValue != null) {
return switch (dcm.getVR(tag)) {
case AS -> ageByDays(dcmElValue, shiftDays);
case DA -> dateByDays(dcmElValue, shiftDays);
case DT -> datetimeByDays(dcm.getDate(tag), shiftDays, shiftSeconds);
case TM -> timeBySeconds(dcmElValue, shiftSeconds);
case AS -> ShiftDate.ageByDays(dcmElValue, shiftDays);
case DA -> ShiftDate.dateByDays(dcmElValue, shiftDays);
case DT -> ShiftDate.datetimeByDays(dcm.getDate(tag), shiftDays, shiftSeconds);
case TM -> ShiftDate.timeBySeconds(dcmElValue, shiftSeconds);
default -> null;
};
} else {
}
else {
return null;
}
}
Expand Down
12 changes: 1 addition & 11 deletions src/main/java/org/karnak/backend/util/ShiftRangeDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ public static String shift(
int shiftDays = (int) hmac.scaleHash(patientID, shiftMinDays, shiftMaxDays);
int shiftSeconds = (int) hmac.scaleHash(patientID, shiftMinSeconds, shiftMaxSeconds);

if (dcmElValue != null) {
return switch (dcm.getVR(tag)) {
case AS -> ShiftDate.ageByDays(dcmElValue, shiftDays);
case DA -> ShiftDate.dateByDays(dcmElValue, shiftDays);
case DT -> ShiftDate.datetimeByDays(dcm.getDate(tag), shiftDays, shiftSeconds);
case TM -> ShiftDate.timeBySeconds(dcmElValue, shiftSeconds);
default -> null;
};
}

return null;
return ShiftDate.shiftValue(dcm, tag, dcmElValue, shiftDays, shiftSeconds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2020-2021 Karnak Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.karnak.profilepipe.option.datemanager;

import java.util.ArrayList;
import java.util.List;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.VR;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.karnak.backend.data.entity.ArgumentEntity;
import org.karnak.backend.model.profilepipe.HMAC;
import org.karnak.backend.util.ShiftByTagDate;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class ShiftByTagDateTest {

private static final Attributes dataset = new Attributes();
private static final List<ArgumentEntity> argumentEntities = new ArrayList<>();
private static final ArgumentEntity seconds_tag = new ArgumentEntity();
private static final ArgumentEntity days_tag = new ArgumentEntity();
private static final HMAC hmac = new HMAC(HMAC.generateRandomKey());

@BeforeEach
protected void setUpBeforeTest() throws Exception {
argumentEntities.clear();

dataset.clear();

dataset.setString(Tag.StudyDate, VR.DA, "20180209");
dataset.setString(Tag.StudyTime, VR.TM, "120843");
dataset.setString(Tag.PatientAge, VR.AS, "043Y");
dataset.setString(Tag.AcquisitionDateTime, VR.DT, "20180209120854.354");
dataset.setString(Tag.AcquisitionTime, VR.TM, "010134");
dataset.setString(0x00150010, VR.LT, "ADIS");
dataset.setString(0x00151010, VR.LT, "10");
dataset.setString(0x00151011, VR.LT, "500");
dataset.setString(0x00151012, VR.LT, "AAA");
dataset.setString(0x00151013, VR.LT, "BBB");
}

@Test
void shiftNoop() {
assertEquals("20180209", ShiftByTagDate.shift(dataset, Tag.StudyDate, argumentEntities, hmac));
assertEquals("120843", ShiftByTagDate.shift(dataset, Tag.StudyTime, argumentEntities, hmac));
assertEquals("043Y", ShiftByTagDate.shift(dataset, Tag.PatientAge, argumentEntities, hmac));
assertEquals(
"20180209120854.354000",
ShiftByTagDate.shift(dataset, Tag.AcquisitionDateTime, argumentEntities, hmac));
assertEquals("010134", ShiftByTagDate.shift(dataset, Tag.AcquisitionTime, argumentEntities, hmac));
}

@Test
void shiftByTag() {
days_tag.setKey("days_tag");
days_tag.setValue("(0015,1010)");
seconds_tag.setKey("seconds_tag");
seconds_tag.setValue("(0015,1011)");
argumentEntities.add(seconds_tag);
argumentEntities.add(days_tag);

assertEquals("20180130", ShiftByTagDate.shift(dataset, Tag.StudyDate, argumentEntities, hmac));
assertEquals("120023", ShiftByTagDate.shift(dataset, Tag.StudyTime, argumentEntities, hmac));
assertEquals("043Y", ShiftByTagDate.shift(dataset, Tag.PatientAge, argumentEntities, hmac));
assertEquals(
"20180130120034.354000",
ShiftByTagDate.shift(dataset, Tag.AcquisitionDateTime, argumentEntities, hmac));
assertEquals("005314", ShiftByTagDate.shift(dataset, Tag.AcquisitionTime, argumentEntities, hmac));
}

@Test
void shiftByBadTag() {
days_tag.setKey("days_tag");
days_tag.setValue("(0017,1010)");
seconds_tag.setKey("seconds_tag");
seconds_tag.setValue("(0017,1011)");
argumentEntities.add(seconds_tag);
argumentEntities.add(days_tag);

assertEquals("20180209", ShiftByTagDate.shift(dataset, Tag.StudyDate, argumentEntities, hmac));
assertEquals("120843", ShiftByTagDate.shift(dataset, Tag.StudyTime, argumentEntities, hmac));
assertEquals("043Y", ShiftByTagDate.shift(dataset, Tag.PatientAge, argumentEntities, hmac));
assertEquals(
"20180209120854.354000",
ShiftByTagDate.shift(dataset, Tag.AcquisitionDateTime, argumentEntities, hmac));
assertEquals("010134", ShiftByTagDate.shift(dataset, Tag.AcquisitionTime, argumentEntities, hmac));
}

@Test
void shiftByBadTag2() {
days_tag.setKey("days_tag");
days_tag.setValue("(0015,1012)");
seconds_tag.setKey("seconds_tag");
seconds_tag.setValue("(0015,1013)");
argumentEntities.add(seconds_tag);
argumentEntities.add(days_tag);

assertEquals("20180209", ShiftByTagDate.shift(dataset, Tag.StudyDate, argumentEntities, hmac));
assertEquals("120843", ShiftByTagDate.shift(dataset, Tag.StudyTime, argumentEntities, hmac));
assertEquals("043Y", ShiftByTagDate.shift(dataset, Tag.PatientAge, argumentEntities, hmac));
assertEquals(
"20180209120854.354000",
ShiftByTagDate.shift(dataset, Tag.AcquisitionDateTime, argumentEntities, hmac));
assertEquals("010134", ShiftByTagDate.shift(dataset, Tag.AcquisitionTime, argumentEntities, hmac));
}
}

0 comments on commit 628ca43

Please sign in to comment.