-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding dicom example Signed-off-by: vsoch <vsoch@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
149 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Dicom Changes | ||
|
||
This is a quick example to show how to do a diff between dicom headers. | ||
|
||
## Usage | ||
|
||
This assumes you have a Python environment with compspec installed. | ||
Install deid: | ||
|
||
```bash | ||
pip install -r requirements.txt | ||
``` | ||
|
||
And then run the example | ||
|
||
```bash | ||
$ python run.py | ||
``` | ||
``` | ||
Result for image1.dcm vs. image4.dcm group 'header' | ||
{ | ||
"changed_node_value": [ | ||
[ | ||
"A", | ||
"B", | ||
"id12", | ||
"id12", | ||
"field-value", | ||
"F", | ||
"M", | ||
"header:image->field:(0010, 0040)->field-name:PatientSex->field-value:F", | ||
"header:image->field:(0010, 0040)->field-name:PatientSex->field-value:M" | ||
] | ||
] | ||
} | ||
Result for image4.dcm vs. image1.dcm group 'header' | ||
{ | ||
"changed_node_value": [ | ||
[ | ||
"A", | ||
"B", | ||
"id12", | ||
"id12", | ||
"field-value", | ||
"M", | ||
"F", | ||
"header:image->field:(0010, 0040)->field-name:PatientSex->field-value:M", | ||
"header:image->field:(0010, 0040)->field-name:PatientSex->field-value:F" | ||
] | ||
] | ||
} | ||
``` |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
__author__ = "Vanessa Sochat" | ||
__copyright__ = "Copyright 2022, Vanessa Sochat" | ||
__license__ = "MPL 2.0" | ||
|
||
# This is the base model for deriving facts from ast in json | ||
|
||
from deid.dicom.parser import DicomParser | ||
import compspec.graph | ||
import compspec.solver | ||
|
||
|
||
class DicomGraphs(compspec.graph.GraphGroup): | ||
""" | ||
A namespace to hold more than one graph about dicom files. | ||
""" | ||
|
||
def __init__(self, dcm, ns): | ||
self.dcm = DicomParser(dcm) | ||
self.dcm.parse() | ||
self.ns = ns | ||
super().__init__() | ||
|
||
def extract(self): | ||
""" | ||
Extract named groups into different graphs | ||
""" | ||
# First try just the header | ||
g = compspec.graph.Graph() | ||
root = g.new_node("header", self.ns) | ||
|
||
for name, field in self.dcm.fields.items(): | ||
f, _ = g.gen("field", name, parent=root.nodeid) | ||
n, _ = g.gen("field-name", field.name, parent=f.nodeid) | ||
g.gen("field-value", field.element.value, parent=n.nodeid) | ||
|
||
# Save the named graph - the header is tested first so we can stop | ||
# if there are differences. | ||
self.graphs["header"] = g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
deid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
__author__ = "Vanessa Sochat" | ||
__copyright__ = "Copyright 2022, Vanessa Sochat" | ||
__license__ = "MPL 2.0" | ||
|
||
from compspec.runner import Difference | ||
from model import DicomGraphs | ||
|
||
import os | ||
import sys | ||
import json | ||
|
||
here = os.path.abspath(os.path.dirname(__file__)) | ||
sys.path.insert(0, here) | ||
|
||
|
||
def main(): | ||
dicom_dir = os.path.join(here, "lib") | ||
dicoms = os.listdir(dicom_dir) | ||
graphs = {} | ||
for dcm in dicoms: | ||
dcm_file = os.path.join(dicom_dir, dcm) | ||
if not os.path.exists(dcm_file): | ||
sys.exit(f"{dcm_file} does not exist.") | ||
graphs[dcm] = DicomGraphs(dcm_file, "image") | ||
|
||
# Run the diff! This is an iterative diff, meaning we are comparing subgraphs | ||
for nameA, A in graphs.items(): | ||
for nameB, B in graphs.items(): | ||
if nameA == nameB: | ||
continue | ||
for group, graph in A: | ||
if group in B: | ||
gA = A[group] | ||
gB = B[group] | ||
runner = Difference(gA, gB, "A", "B", quiet=True) | ||
result = runner.run() | ||
print(f"Result for {nameA} vs. {nameB} group '{group}'") | ||
print(json.dumps(result, indent=4)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters