Skip to content

Commit

Permalink
add Valuekeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
lmdulz committed Jan 16, 2024
1 parent 8a4b9e8 commit fd354d4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/anonymizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { FixedValueAnonymizer } from "./fixedvalueanonymizer";
import { IDAnonymizer } from "./idanonymizer";
import { InstitutionAnonymizer } from "./institutionanonymizer";
import { PNAnonymizer } from "./pnanonymizer";
import { Protector } from "./protector";
import { PrivatTagAnonymizer } from "./privatetaganonymizer";

Check failure on line 8 in src/anonymizer.ts

View workflow job for this annotation

GitHub Actions / CI (16.x)

Cannot find module './privatetaganonymizer' or its corresponding type declarations.
import { Randomizer } from "./randomizer";
import { UIAnonymizer } from "./uianonymizer";
import { UnwantedElementStripper } from "./unwantedelements";
import { ValueKeeper } from "./valuekeeper";

type ElementHandler = (dataset: dataSet, tag: string) => boolean;

Expand All @@ -30,6 +31,7 @@ export class Anonymizer {
constructor(
patientID?: string,
protected_tags?: string[],
anonymizePrivateTags?: boolean,
id_prefix?: string,
id_suffix?: string,
seed?: string
Expand All @@ -50,7 +52,6 @@ export class Anonymizer {
//this.data = data;
this.address_anonymizer = new AddressAnonymizer(this.randomizer);
this.element_handlers = [
new Protector(protected_tags).protect,
new UnwantedElementStripper([
"00101081", //"BranchOfService",
"00102180", //"Occupation",
Expand Down Expand Up @@ -92,11 +93,16 @@ export class Anonymizer {
new FixedValueAnonymizer("00380300", "").anonymize, // CurrentPatientLocation
new DateTimeAnonymizer(this.date_offset_hours).anonymize,
];
if (patientID) {
this.element_handlers.push(new FixedValueAnonymizer("00100020", patientID).anonymize);
if (protected_tags) {
this.element_handlers.unshift(new ValueKeeper(protected_tags).keep);
}
if (this.patientID) {
this.element_handlers.push(new FixedValueAnonymizer("00100020", this.patientID).anonymize);
}
if (anonymizePrivateTags) {
this.element_handlers.push(new PrivatTagAnonymizer().anonymize);
}
}

anonymize(data: data.DicomDict) {
this.walk(data.meta, this.element_handlers);
this.walk(data.dict, this.element_handlers);
Expand All @@ -107,10 +113,6 @@ export class Anonymizer {
for (const tag of tagList) {
const element = dataset[tag];

if (this.del_private_tags(dataset, tag)) {
continue;
}

this.anonymize_element(dataset, tag, handler);

// If the element is a sequence, recursively walk through its items
Expand All @@ -133,15 +135,4 @@ export class Anonymizer {
}
}
}

del_private_tags(dataset: dataSet, data_tag: string): boolean {
const currTag = data.Tag.fromString(data_tag);
if (currTag.group() % 2 === 1) {
delete dataset[data_tag];
return true;
} else {
return false;
}
//return true
}
}
52 changes: 52 additions & 0 deletions src/valuekeeper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { dataSet, data } from "dcmjs";

class TagError extends Error {
constructor(tag: string) {
const message = `Bad tag name '${tag}'. Must be a well-known DICOM element name or a string in the form 'stuv,wxyz' where each character is a hexadecimal digit.`;
super(message);

// Set the prototype explicitly to ensure correct inheritance
Object.setPrototypeOf(this, TagError.prototype);
}
}

export class ValueKeeper {
private protected_tags: string[] = [];

constructor(keywords: string[] = []) {
for (const tag of keywords) {
let pattern = /^\(?([0-9A-F]{4}),?([0-9A-F]{4})\)?$/;
let match = tag.match(pattern);
if (match) {
this.protected_tags.push(match[1] + match[2]);
} else {
pattern = /^[a-zA-Z]+$/;

try {
match = tag.match(pattern)!;
const tempTag = data.DicomMetaDictionary.nameMap[match[0]];
if (!tempTag) {
throw new TagError("invalidTag");
} else {
this.protected_tags.push(tempTag.tag);
}
} catch (error) {
if (error instanceof TagError) {
console.error(error.message);
} else {
// Handle other types of errors
console.error("An unexpected error occurred:", error);
}
}
}
}
}

keep = (_: dataSet, data_tag: string): boolean => {
if (this.protected_tags.includes(data_tag)) {
return true;
} else {
return false;
}
};
}

0 comments on commit fd354d4

Please sign in to comment.