-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
executable file
·108 lines (84 loc) · 3.74 KB
/
convert.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
import subprocess
import os
import sys
import json
import shutil
import glob
T1W="58c33bcee13a50849b25879a"
T2W="594c0325fa1d2e5a1f0beda5"
TASK="59b685a08e5d38b0b331ddc5"
DWI="58c33c5fe13a50849b25879b"
product={}
with open("config.json") as config_json:
config = json.load(config_json)
for dataset in config["datasets"]:
storage = "wrangler"
if "storage" in dataset:
storage = dataset["storage"]
outdir=dataset["id"]
if 'outdir' in dataset:
outdir=dataset["outdir"]
if storage == "xnat":
print("converting %s" % dataset["id"])
#find information about this object from _outputs
datatype=None
for output in config["_outputs"]:
if output["dataset_id"] == dataset["id"]:
meta = output["meta"]
datatype = output["datatype"]
#figure out appropriate file prefix for brainlife datatype
name="unknown"
if datatype == T1W:
name="t1"
if datatype == T2W:
name="t2"
if datatype == TASK:
name="bold"
if datatype == DWI:
name="dwi"
print("unzipping")
ret=subprocess.run(["unzip", "-o", "-d", "dicom", "xnat.zip"], cwd=outdir)
if ret.returncode != 0:
print("failed to unzip")
sys.exit(ret.returncode)
if "xnat_scan" in meta:
print("running dcm2niix on %s" % outdir)
#-v : verbose (n/y or 0/1/2, default 0) [no, yes, logorrheic
#-z : gz compress images (y/o/i/n/3, default n) [y=pigz, o=optimal pigz, i=internal:miniz, n=no, 3=no,3D]
#-d : directory search depth. Convert DICOMs in sub-folders of in_folder? (0..9, default 5
#-w : write behavior for name conflicts (0,1,2, default 2: 0=skip duplicates, 1=overwrite, 2=add suffix)
#-b : BIDS sidecar (y/n/o [o=only: no NIfTI], default y)
#-f : filename
#. < input folder
ret=subprocess.run(["dcm2niix", "-v", "1", "-z", "o", "-d", "10", "-w", "1", "-b", "y", "-f", name, "."], cwd=outdir)
if ret.returncode != 0:
print("failed to run dcm2niix")
sys.exit(ret.returncode)
print("loading sidecar .json and creating product.json")
sidecar_jsons = glob.glob(outdir+"/*.json")
with open(sidecar_jsons[0]) as sidecar_f:
sidecar = json.load(sidecar_f)
#merge meta from output
for key in sidecar:
meta[key] = sidecar[key]
product[dataset["id"]] = {"meta": meta}
#rename file products to brainlife datatype file names
if datatype == DWI:
bvecs = glob.glob(outdir+"/*.bvec")
subprocess.call(["mv", bvecs[0], outdir+"/dwi.bvecs"])
bvals = glob.glob(outdir+"/*.bval")
subprocess.call(["mv", bvals[0], outdir+"/dwi.bvals"])
#clean up things
shutil.rmtree(outdir+"/dicom")
os.remove(sidecar_jsons[0]) #sidecar
else:
print("xnat_scan is not set.. probably not dicom.. but we need to truncate parent directories.")
os.system("mv "+outdir+"/dicom/*/*/*/*/*/*/* "+outdir)
os.system("rm -r "+outdir+"/dicom")
os.remove(outdir+"/xnat.zip")
else:
print("no conversion necessary")
with open("product.json", "w") as f:
json.dump(product, f)
print("convert.py done")