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

Add ability to parse untilTag and to stopAfterTag with namified or Group,Element syntax #149

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 21 additions & 1 deletion src/DicomMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,29 @@ const encapsulatedSyntaxes = [
class DicomMessage {
static read(bufferStream, syntax, ignoreErrors) {
var dict = {};
var untilTagNormalized = Tag.fromDirtyString(untilTag);
var stopAfterTagNormalized = Tag.fromDirtyString(stopAfterTag);

try {
while (!bufferStream.end()) {
var readInfo = DicomMessage.readTag(bufferStream, syntax);
if (breakOnNextLoop === true) {
break;
}
const readInfo = DicomMessage.readTag(bufferStream, syntax);
const cleanTagString = readInfo.tag.toCleanString();

if (
untilTagNormalized &&
untilTagNormalized.toCleanString() === cleanTagString
) {
break;
}
if (
stopAfterTagNormalized &&
stopAfterTagNormalized.toCleanString() === cleanTagString
) {
breakOnNextLoop = true;
}

dict[readInfo.tag.toCleanString()] = {
vr: readInfo.vr.type,
Expand Down
32 changes: 32 additions & 0 deletions src/Tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { paddingLeft } from "./ValueRepresentation.js";
import { ValueRepresentation } from "./ValueRepresentation.js";
import { DicomMessage } from "./DicomMessage.js";
import { WriteBufferStream } from "./BufferStream.js";
import { findByName } from "./dictionary.js";

var IMPLICIT_LITTLE_ENDIAN = "1.2.840.10008.1.2";
var EXPLICIT_LITTLE_ENDIAN = "1.2.840.10008.1.2.1";
Expand Down Expand Up @@ -64,6 +65,37 @@ class Tag {
return this.is(0x7fe00010);
}

static fromDirtyString(str) {
if (!str || str.length < 1) {
return null;
}
/**
* Check first for Group,Element format.
*
* Matches various formats for string representation of tags:
* (0028,7017)
* [0028,7017]
* 0028,7017
* 00287017
* x00287017
*/
const matcher = /^x?[\[\(]?([A-Z0-9]{4}),?([A-Z0-9]{4})[\)\]]?$/;
const matches = str.match(matcher);
if (matches && matches.length > 2) {
// For given string (0028,7017), should return array like: ["(0028,7017)", "0028", "7017"]
return Tag.fromString(matches[1] + matches[2]);
} else {
/**
* If no match on Group,Element try to match on dictionary name.
*/
const match = findByName(str);
if (match) {
return Tag.fromPString(match.tag);
}
}
return null;
}

static fromString(str) {
var group = parseInt(str.substring(0, 4), 16),
element = parseInt(str.substring(4), 16);
Expand Down
9 changes: 9 additions & 0 deletions src/dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -48060,3 +48060,12 @@ const dictionary = {
};

export default dictionary;
export function findByName(tagName) {
const keys = Object.keys(dictionary);
for (let i = 0; i < keys.length; i += 1) {
if (dictionary[keys[i]].name === tagName) {
return dictionary[keys[i]];
}
}
return null;
}
129 changes: 102 additions & 27 deletions test/test_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const expect = require('chai').expect;
const dcmjs = require('../build/dcmjs');

const fs = require("fs");
const {http, https} = require("follow-redirects");
const { http, https } = require("follow-redirects");
const os = require("os");
const path = require("path");
const unzipper = require("unzipper");
Expand All @@ -14,34 +14,34 @@ fileMetaInformationVersionArray[1] = 1;

const metadata = {
"00020001": {
"Value": [
fileMetaInformationVersionArray.buffer
],
"vr": "OB"
"Value": [
fileMetaInformationVersionArray.buffer
],
"vr": "OB"
},
"00020012": {
"Value": [
"1.2.840.113819.7.1.1997.1.0"
],
"vr": "UI"
"Value": [
"1.2.840.113819.7.1.1997.1.0"
],
"vr": "UI"
},
"00020002": {
"Value": [
"1.2.840.10008.5.1.4.1.1.4"
],
"vr": "UI"
"Value": [
"1.2.840.10008.5.1.4.1.1.4"
],
"vr": "UI"
},
"00020003": {
"Value": [
DicomMetaDictionary.uid()
],
"vr": "UI"
"Value": [
DicomMetaDictionary.uid()
],
"vr": "UI"
},
"00020010": {
"Value": [
"1.2.840.10008.1.2"
],
"vr": "UI"
"Value": [
"1.2.840.10008.1.2"
],
"vr": "UI"
}
};

Expand Down Expand Up @@ -74,7 +74,7 @@ const sequenceMetadata = {
}

function downloadToFile(url, filePath) {
return new Promise( (resolve,reject) => {
return new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(filePath);
const request = https.get(url, (response) => {
response.pipe(fileStream);
Expand Down Expand Up @@ -152,9 +152,9 @@ const tests = {
const unzipPath = path.join(os.tmpdir(), "test_multiframe_1");

downloadToFile(url, zipFilePath)
.then( () => {
.then(() => {
fs.createReadStream(zipFilePath)
.pipe(unzipper.Extract( {path: unzipPath} )
.pipe(unzipper.Extract({ path: unzipPath })
.on('close', () => {
const mrHeadPath = path.join(unzipPath, "MRHead");
fs.readdir(mrHeadPath, (err, fileNames) => {
Expand Down Expand Up @@ -188,9 +188,9 @@ const tests = {
const segFilePath = path.join(os.tmpdir(), "Lesion1_onesliceSEG.dcm");

downloadToFile(ctPelvisURL, zipFilePath)
.then( () => {
.then(() => {
fs.createReadStream(zipFilePath)
.pipe(unzipper.Extract( {path: unzipPath} )
.pipe(unzipper.Extract({ path: unzipPath })
.on('close', () => {
const ctPelvisPath = path.join(unzipPath, "Series-1.2.840.113704.1.111.1916.1223562191.15");
fs.readdir(ctPelvisPath, (err, fileNames) => {
Expand All @@ -211,7 +211,7 @@ const tests = {
expect(roundedSpacing).to.equal(5);

downloadToFile(segURL, segFilePath)
.then( () => {
.then(() => {
const arrayBuffer = fs.readFileSync(segFilePath).buffer;
const dicomDict = DicomMessage.readFile(arrayBuffer);
const dataset = DicomMetaDictionary.naturalizeDataset(dicomDict.dict);
Expand All @@ -225,6 +225,81 @@ const tests = {
);
});
},

test_untilTag: () => {
const dicomJSON = `
[
{
"00100010": {
"vr": "PN",
"Value": [ "Fake Patient 1" ]
},
"0020000D": {
"vr": "UI",
"Value": [ "1.2.392.200036.9116.2.2.2.1762893313.1029997326.945873" ]
}
}
]`;
const dicomDict = new DicomDict(metadata);
const datasets = JSON.parse(dicomJSON)[0];
dicomDict.dict = datasets;
const part10Buffer = dicomDict.write();

let dicomData;
let natural;

dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer);
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural.PatientName).to.equal('Fake Patient 1');
expect(natural.StudyInstanceUID).to.equal('1.2.392.200036.9116.2.2.2.1762893313.1029997326.945873');

dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer, {
untilTag: '00100010'
});
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural).to.not.have.property('PatientName');
expect(natural).to.not.have.property('StudyInstanceUID');


dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer, {
stopAfterTag: '0010,0010'
});
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural.PatientName).to.equal('Fake Patient 1');
expect(natural).to.not.have.property('StudyInstanceUID');

dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer, {
untilTag: '(0020,000D)'
});
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural.PatientName).to.equal('Fake Patient 1');
expect(natural).to.not.have.property('StudyInstanceUID');


dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer, {
stopAfterTag: 'x0020000D'
});
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural.PatientName).to.equal('Fake Patient 1');
expect(natural.StudyInstanceUID).to.equal('1.2.392.200036.9116.2.2.2.1762893313.1029997326.945873');

dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer, {
untilTag: 'StudyInstanceUID'
});
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural.PatientName).to.equal('Fake Patient 1');
expect(natural).to.not.have.property('StudyInstanceUID');


dicomData = dcmjs.data.DicomMessage.readFile(part10Buffer, {
stopAfterTag: 'PatientName'
});
natural = DicomMetaDictionary.naturalizeDataset(dicomData.dict);
expect(natural.PatientName).to.equal('Fake Patient 1');
expect(natural).to.not.have.property('StudyInstanceUID');

console.log("Finished test_untilTag")
}
}


Expand Down