-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicomtable.py
executable file
·73 lines (56 loc) · 1.86 KB
/
dicomtable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python3
import argparse
import os
import re
import pydicom
import dicomtools
parser = argparse.ArgumentParser(description="Output a table with the variable fields of a DICOM set as a CSV.")
parser.add_argument("path", help="directory of DICOM files", metavar="PATH")
args = parser.parse_args()
assert os.path.isdir(args.path)
dicompaths = dicomtools.dir_list_files(args.path)
datasets = [pydicom.dcmread(dicompath, stop_before_pixels=True) for dicompath in dicompaths]
tags = set().union(*[dataset.keys() for dataset in datasets])
vartags = set()
for tag in tags:
value = None
for i, dataset in enumerate(datasets):
if i == 0:
if tag in dataset:
value = str(dataset[tag].value)
else:
if tag in dataset and str(dataset[tag].value) == value or tag not in dataset and value is None:
pass
else:
vartags.add(tag)
tags = list(vartags)
tags.sort()
properties = ["VR", "VM", "name", None, "keyword"]
print("id", end="\t")
print("\t".join([str(tag) for tag in tags]))
print("is_private", end="\t")
print("\t".join([str(tag.is_private) for tag in tags]))
dataset = datasets[0]
for i, property in enumerate(properties):
if property is not None:
print(property, end="")
for tag in tags:
if tag.is_private:
# http://dicom.nema.org/dicom/2013/output/chtml/part05/sect_7.8.html
private_creator = dataset[tag.group, tag.element >> 8].value
if property != "keyword":
print("\t{}".format(pydicom.datadict.get_private_entry(tag, private_creator)[i]), end="")
else:
print("\t", end="")
else:
print("\t{}".format(pydicom.datadict.get_entry(tag)[i]), end="")
print()
for i, dataset in enumerate(datasets):
print(i, end="")
for tag in tags:
if tag in dataset:
value = re.sub("\s+", " ", str(dataset[tag].value))
print("\t{}".format(value if len(value) < 256 else "?"), end="")
else:
print("\t", end="")
print()