Skip to content

Commit

Permalink
adding dicom example (#16)
Browse files Browse the repository at this point in the history
* adding dicom example
Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed May 28, 2022
1 parent 6931143 commit 899ad6e
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jobs:
python examples/python/tensorflow-function-example.py
python examples/spack/example-python.py
python examples/spack/example-singularity.py
pip install -r examples/dicom/requirements.txt
python examples/dicom/run.py
cd examples/dwarf
pip install -r requirements.txt
Expand Down
52 changes: 52 additions & 0 deletions examples/dicom/README.md
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 added examples/dicom/lib/image1.dcm
Binary file not shown.
Binary file added examples/dicom/lib/image4.dcm
Binary file not shown.
38 changes: 38 additions & 0 deletions examples/dicom/model.py
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
1 change: 1 addition & 0 deletions examples/dicom/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
deid
42 changes: 42 additions & 0 deletions examples/dicom/run.py
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()
10 changes: 7 additions & 3 deletions examples/dwarf/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,16 @@ def parse_member(self, die):

is_connector = self.is_connector(die)

node = self.new_node("member", get_name(die), self.ids[die], is_connector=is_connector)
node = self.new_node(
"member", get_name(die), self.ids[die], is_connector=is_connector
)

# Just generate relation for path logic!
relation = self.new_relation(fromid=self.ids[die.get_parent()], toid=node.nodeid, relation="has")
relation = self.new_relation(
fromid=self.ids[die.get_parent()], toid=node.nodeid, relation="has"
)
self.add_relation(relation)

self.gen(
"type", underlying_type, parent=self.ids[die], is_connector=is_connector
)
Expand Down
2 changes: 1 addition & 1 deletion examples/dwarf/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

# Add remainder
for name in os.listdir(os.path.join(here, "lib")):
if name not in seen and not name.startswith('_'):
if name not in seen and not name.startswith("_"):
tests.append((name, "lib.v1.so", "lib.v2.so"))
seen.add(name)

Expand Down
4 changes: 2 additions & 2 deletions examples/nifti/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def extract(self):
data = self.nii.get_data()

# Since we cannot represent floats, we need to apply scale
# Yes this isn't precise, it's an example
# Yes this isn't precise, it's an example
for x in range(data.shape[0]):
for y in range(data.shape[1]):
for z in range(data.shape[2]):
voxel, _ = g.gen("voxel", f"{x}.{y}.{z}", parent=root.nodeid)
g.gen("value", int(data[x,y,z] * 100), parent=voxel.nodeid)
g.gen("value", int(data[x, y, z] * 100), parent=voxel.nodeid)

self.graphs["image"] = g

Expand Down
6 changes: 4 additions & 2 deletions examples/nifti/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def main():
"""
The truth (changed values) is:
x y z zeros modified
16 16 5 0.0 1.0
32 32 10 0.0 1e-09
Expand Down Expand Up @@ -44,7 +44,9 @@ def main():
# Omit default logic program for image data
else:
runner = Difference(gA, gB, "zeros", "modified", quiet=True)
result = runner.run(logic_programs=['compare-voxels.lp'], omit_default=True)
result = runner.run(
logic_programs=["compare-voxels.lp"], omit_default=True
)
print(f"Result for group '{group}'")
print(json.dumps(result, indent=4))

Expand Down

0 comments on commit 899ad6e

Please sign in to comment.