-
Notifications
You must be signed in to change notification settings - Fork 1
/
wm_query.py
112 lines (85 loc) · 5.73 KB
/
wm_query.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
107
108
109
110
111
112
from dipy.io.streamline import load_trk, save_trk, save_vtk, load_vtk
from dipy.io.stateful_tractogram import Space, StatefulTractogram
import subprocess
import datetime
import sys
import os
f_path = "/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/"
f_path="/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/ELIKOPY_subset/PROJECT_old/"
f_path="/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/ELIKOPY_subset_new/PROJECT/"
f_path="/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/elikopy_subset_new2/PROJECT/"
patient = "V_25" ; mod = ""
def query(mod,merge=False,single_bdle__=None,patient=patient):
wmparc = f_path + "subjects/" + patient + "/tracking/preproc/" + patient + "_reg_wmparc"
if not os.path.isdir(f_path + "subjects/" + patient + "/tracking/query/"):
os.mkdir(f_path + "subjects/" + patient + "/tracking/query/")
if not os.path.isdir(f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/"):
os.mkdir(f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/")
track_name_in = f_path + "subjects/" + patient + "/tracking/" + patient + "_dipy" + mod
track_name_out = f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/" + patient + "_dipy" + mod
# tractogram = load_trk(track_name_in + ".trk", f_path + "subjects/%s/dMRI/preproc/"%patient + patient + "_dmri_preproc.nii.gz")
# save_vtk(tractogram, track_name_in + ".vtk")
if not (single_bdle__ in (None,"None","'None'")):
track_name_out = f_path + "subjects/%s/tracking/Solo/query"%patient + mod + "/" + patient + "_dipy" + mod
if not os.path.isdir(f_path + "subjects/%s/tracking/Solo/query"%patient + mod + "/"):
os.mkdir(f_path + "subjects/%s/tracking/Solo/query"%patient + mod + "/")
wm_query_cmd = "tract_querier -t " + track_name_in + ".trk -a " + wmparc + ".nii.gz -q /auto/home/users/d/r/drimez/wmql_tracts.qry -o " + track_name_out + ".trk"
log_prefix = "WM query"
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": WM query launched for " + "dipy" + mod + " tracts\n")
f = open(f_path + "subjects/" + patient + "/tracking/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": WM query launched for " + "dipy" + mod + " tracts\n" + wm_query_cmd)
f.close()
bashcmd = wm_query_cmd.split()
process = subprocess.Popen(wm_query_cmd, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=sys.stderr)
# wait until finish
out, error = process.communicate()
# convert files into trk
"""
trk = None
with os.scandir(track_name_out) as iterator:
for entry in iterator:
if entry.name.split(".")[-1]=="vtk":
tractogram = load_vtk(f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/" + entry.name,
f_path + "subjects/%s/dMRI/preproc/"%patient + patient + "_dmri_preproc.nii.gz")
save_trk(tractogram, f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/" + ".".join(entry.name.split(".")[:-1]) + ".trk")
os.system("rm " + f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/" + entry.name)
if merge and not entry.name.split(".")[-1]=="vtk" and not entry.name.split(".")[0] in ("EP","PT_central","PT_ant","PT_post","PT_cingulate","IFOF","temporal"):
tractogram = load_trk(f_path + "subjects/" + patient + "/tracking/query/dipy" + mod + "/" + ".".join(entry.name.split(".")[:-1]) + ".trk",
f_path + "subjects/%s/dMRI/preproc/"%patient + patient + "_dmri_preproc.nii.gz")
if trk is None:
trk = tractogram
else:
trk = StatefulTractogram( list(trk.streamlines) + list(tractogram.streamlines),
f_path + "subjects/%s/dMRI/preproc/"%patient + patient + "_dmri_preproc.nii.gz",
Space.RASMM,
data_per_streamline={k: (list(trk.data_per_streamline[k]) + list(tractogram.data_per_streamline[k]))
for k in tractogram.data_per_streamline.keys() })
if not trk is None:
save_trk(trk, f_path + "subjects/" + patient + "/tracking/" + patient + "_dipy" + mod + "_cleaned.trk")
iterator.close()
"""
"""
else:
wm_query_cmd = "tract_querier -t " + track_name_in + ".trk -a " + wmparc + ".nii.gz -q /auto/home/users/d/r/drimez/wmql_tracts.qry -o " + track_name_out + ".trk"
log_prefix = "WM query"
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": WM query launched for " + "dipy" + mod + "tracts\n")
f = open(f_path + "subjects/" + patient + "/tracking/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": WM query launched for " + "dipy" + mod + "tracts\n" + wm_query_cmd)
f.close()
bashcmd = wm_query_cmd.split()
process = subprocess.Popen(wm_query_cmd, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=sys.stderr)
# wait until finish
out, error = process.communicate()
"""
if __name__ == "__main__":
args = None
if len(sys.argv)>1:
args = [str(arg) for arg in sys.argv[1:]]
query(*args)
else:
for model_type in ["_prob",""]:
query(model_type)