forked from QIICR/dcm2tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabulate.py
60 lines (49 loc) · 1.73 KB
/
tabulate.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
import os, sys, json, pandas
from QDBDParser import QDBDParser
from SRCDParser import SRCDParser
import json
#dcmqiPath = '/Users/fedorov/local/builds/dcmqi-refactored/dcmqi-build/bin'
dcmqiPath = '/Users/BillClifford/local/builds/dcmqi-mac/bin'
tempPath = '.'
# Inputs:
# #1 - DB schema file from https://app.quickdatabasediagrams.com
# (remove all layout components from the bottom if exporting
# from the web, or use the included in the repo schema.qdbd)
# #2 - directory with the files from TCIA QIN-HEADNECK collection
#
# Output: one csv file per table defined in the schema
# attributes not found will be empty!
def main():
tablesParser = QDBDParser(sys.argv[1])
tablesRules = tablesParser.getTablesSchema()
tables = {}
for t in tablesRules.keys():
tables[t] = []
for root,dirs,files in os.walk(sys.argv[2]):
for f in files:
dcmName = os.path.join(root,f)
print dcmName
try:
dicomParser = SRCDParser(dcmName, tablesRules, tempPath=tempPath, dcmqiPath=dcmqiPath)
except:
"Failed to read as DICOM:",dcmName
continue
dicomParser.parse()
dcmFileTables = dicomParser.getTables()
for t in dcmFileTables:
# print "Appending", dcmFileTables[t].values
# print dicomTables[t]
tableOrRow = dcmFileTables[t]
if isinstance(tableOrRow,dict):
tables[t].append(tableOrRow)
elif isinstance(tableOrRow,list):
for row in tableOrRow:
tables[t].append(row)
for t in tables.keys():
if len(tables[t]):
tables[t] = pandas.DataFrame(tables[t])
for t in tables.keys():
if type(tables[t]) == pandas.DataFrame:
tables[t].to_csv(t+".tsv",index=False,sep='\t')
if __name__ == '__main__':
main()