Skip to content

Commit

Permalink
minor improvements. also randomising the file if no input is given. i…
Browse files Browse the repository at this point in the history
…ntroduced parameter to pass a file location
  • Loading branch information
angshu committed Jul 9, 2021
1 parent 98d18c0 commit ceaf5f5
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Pacs_Simulator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ You may also generate a sample DICOM file with minimum patient detail and relate
This is useful when you do not want to run the simulator, but want to upload a DICOM file manually using "dcmsnd" command yourself.


> java -cp target/pacssimulator-1.0-SNAPSHOT-jar-with-dependencies.jar org.bahmni.pacssimulator.DicomFileGenerator [patientId] [givenName] [familyName] [accessionNumber]
> java -cp target/pacssimulator-1.0-SNAPSHOT-jar-with-dependencies.jar org.bahmni.pacssimulator.DicomFileGenerator [patientId] [givenName] [familyName] [accessionNumber] [filepath]
example

> java -cp target/pacssimulator-1.0-SNAPSHOT-jar-with-dependencies.jar org.bahmni.pacssimulator.DicomFileGenerator GAN123 Gajendra Singh ORD-309
> java -cp target/pacssimulator-1.0-SNAPSHOT-jar-with-dependencies.jar org.bahmni.pacssimulator.DicomFileGenerator GAN123 Gajendra Singh ORD-309 /tmp/test.dcm
The above should tell you the location of the generated file.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
Expand Down Expand Up @@ -64,32 +67,43 @@ public File writeFile(DicomObject dicomObject) {
}

public File generateFor(String patientId, String givenName, String familyName, String orderId) throws URISyntaxException, IOException, DicomException {
UIDGenerator uidGenerator = new UIDGenerator();

DicomInputStream dicomInputStream = new DicomInputStream(ClassLoader.getSystemResourceAsStream(fileName));
DicomObject dicomObject = dicomInputStream.readDicomObject();

// String patientId = ormMessage.getPATIENT().getPID().getPatientID().getIDNumber().getValue();
String studyId = UUID.randomUUID().toString();
String instanceUid = uidGenerator.getNewSOPInstanceUID(studyId,"1","1").toString();
String seriesInstanceUid = uidGenerator.getNewSeriesInstanceUID(studyId, "1").toString();
String studyInstanceUid = uidGenerator.getNewStudyInstanceUID(studyId).toString();

dicomObject.putString(Tag.PatientName, VR.PN, givenName + " " + familyName);
dicomObject.putString(Tag.PatientID, VR.LO, patientId);
dicomObject.putString(Tag.AccessionNumber, VR.LO, orderId);
dicomObject.putString(Tag.StudyID, VR.LO, studyId);
dicomObject.putString(Tag.MediaStorageSOPInstanceUID, VR.UI, instanceUid);
dicomObject.putString(Tag.SOPInstanceUID, VR.UI, instanceUid);
dicomObject.putString(Tag.SeriesInstanceUID, VR.UI, seriesInstanceUid);
dicomObject.putString(Tag.StudyInstanceUID, VR.UI, studyInstanceUid);
File someFile = Paths.get(fileName).toFile();
InputStream inputStream;
if (someFile.exists()) {
inputStream = new FileInputStream(someFile);
} else {
inputStream = ClassLoader.getSystemResourceAsStream(fileName);
}
if (inputStream == null) {
throw new RuntimeException("Could not load file");
}
try {
UIDGenerator uidGenerator = new UIDGenerator();
DicomInputStream dicomInputStream = new DicomInputStream(inputStream);
DicomObject dicomObject = dicomInputStream.readDicomObject();
// String patientId = ormMessage.getPATIENT().getPID().getPatientID().getIDNumber().getValue();
String studyId = UUID.randomUUID().toString();
String instanceUid = uidGenerator.getNewSOPInstanceUID(studyId, "1", "1").toString();
String seriesInstanceUid = uidGenerator.getNewSeriesInstanceUID(studyId, "1").toString();
String studyInstanceUid = uidGenerator.getNewStudyInstanceUID(studyId).toString();

// System.out.println("The Dicom file for patient"+patientId+givenName+familyName+orderId);
// System.out.println(dicomObject.toString());
dicomObject.putString(Tag.PatientName, VR.PN, givenName + " " + familyName);
dicomObject.putString(Tag.PatientID, VR.LO, patientId);
dicomObject.putString(Tag.AccessionNumber, VR.LO, orderId);
dicomObject.putString(Tag.StudyID, VR.LO, studyId);
dicomObject.putString(Tag.MediaStorageSOPInstanceUID, VR.UI, instanceUid);
dicomObject.putString(Tag.SOPInstanceUID, VR.UI, instanceUid);
dicomObject.putString(Tag.SeriesInstanceUID, VR.UI, seriesInstanceUid);
dicomObject.putString(Tag.StudyInstanceUID, VR.UI, studyInstanceUid);

dicomObject.putString(Tag.StudyDate, VR.DT, new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
// System.out.println("The Dicom file for patient"+patientId+givenName+familyName+orderId);
// System.out.println(dicomObject.toString());

return writeFile(dicomObject);
dicomObject.putString(Tag.StudyDate, VR.DT, new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
return writeFile(dicomObject);
} finally {
inputStream.close();
}
}

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Random;

public class DicomFileGenerator {

public static void main(String[] args) {

String[] sampleFiles = { "U_2015_05_26_14_18_35.dcm", "U_2015_05_26_14_23_16.dcm"};
int i = new Random().nextInt(sampleFiles.length - 0) + 1;

System.out.println("arguments passed: " + String.join(",", args));

String patientId = "GAN203006";
String givenName = "Rutgar";
String familyName = "Ragos";
String accessionNumber = "ORD-307";
String dicomFilePath = sampleFiles[i-1];
//String dicomFilePath = "/Users/angshus/work/bahmni/utilities/Pacs_Simulator/0002.DCM";

if (args.length > 0) {
patientId = args[0];
Expand All @@ -27,12 +34,15 @@ public static void main(String[] args) {
if (args.length > 3) {
accessionNumber = args[3];
}
if (args.length > 4) {
dicomFilePath = args[4];
}
}

System.out.println(String.format("Creating sample DICOM file with patientId:%s, givenName:%s, familyName:%s, accessionNumber:%s",
patientId, givenName, familyName, accessionNumber));
System.out.println(String.format("Creating sample DICOM file with patientId:%s, givenName:%s, familyName:%s, accessionNumber:%s, file:%s",
patientId, givenName, familyName, accessionNumber, dicomFilePath));
//loaded from classpath, so put file in resources directory
DicomFile dicomFile = new DicomFile("U_2015_05_26_14_18_35.dcm");
DicomFile dicomFile = new DicomFile(dicomFilePath);
File file = null;
try {
file = dicomFile.generateFor(patientId, givenName, familyName, accessionNumber);
Expand Down

0 comments on commit ceaf5f5

Please sign in to comment.