-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracto.py
1570 lines (1338 loc) · 102 KB
/
tracto.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
tensor2metric -adc -fa -ad -rd -cl -cp -cs -value -vector tensor -mask image -debug
population_template -type "rigid_affine" -voxel_size jsp,jsp,jsp input_dir template # input = all input images
tcksample [ options ] tracks image output_values
values_from_volume(data, streamlines, affine) to sample along trackts
"""
from dipy.io.image import load_nifti_data, load_nifti, save_nifti
from dipy.io.streamline import load_tck, load_trk, save_trk, save_tck
from dipy.direction import peaks
import matplotlib.pyplot as plt
import dipy.data as dpd
import copy
from wm_query import query
import multiprocessing
from joblib import Parallel, delayed
import dipy.tracking.streamline as dts
import dipy.tracking.streamlinespeed as dps
from dipy.viz import window, actor
from scipy.ndimage import binary_dilation, binary_erosion
from dipy.align.imaffine import (transform_centers_of_mass, AffineMap, MutualInformationMetric, AffineRegistration)
from dipy.align.transforms import (TranslationTransform3D, RigidTransform3D, AffineTransform3D)
from dipy.data import get_sphere
from dipy.reconst.csdeconv import (ConstrainedSphericalDeconvModel, auto_response_ssst, response_from_mask_ssst, mask_for_response_ssst, recursive_response)
from dipy.tracking.stopping_criterion import ThresholdStoppingCriterion, CmcStoppingCriterion
import dipy.direction.peaks as dp
from dipy.reconst import sfm
import AFQ.registration as dipy_syn_reg
import dipy.core.gradients as dpg
from dipy.tracking.streamline import set_number_of_points
from dipy.direction import ProbabilisticDirectionGetter, BootDirectionGetter
from dipy.data import default_sphere
from dipy.core.geometry import cart2sphere
from dipy.io.stateful_tractogram import Space, StatefulTractogram
from dipy.core.gradients import gradient_table
from dipy.data import get_fnames
from dipy.io.gradients import read_bvals_bvecs
from dipy.reconst import shm
from dipy.tracking import utils
from dipy.tracking.local_tracking import LocalTracking, ParticleFilteringTracking
from dipy.tracking.stopping_criterion import BinaryStoppingCriterion
from dipy.tracking.streamline import Streamlines
from cmtk import *
from pathlib import Path
import pandas as pd
import numpy as np
import datetime
import subprocess
import copy as cp
import pickle, json
import bz2
import _pickle as cPickle
import sys
import os
import gc
f_path = "/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/"
f_path="/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/ELIKOPY_subset_new/PROJECT/"
f_path="/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/elikopy_subset_new2/PROJECT/"
# f_path = "C:\\Users\\rimez\\OneDrive\\Documents\\TFE\\Datas\\Trash\\subset\\ELIKOPY_subset\\PROJECT\\"
patient_list = ['H_0','H_1','H_2','H_3','H_4','V_100','V_101','V_102','V_103']
patient_list = ['U_57'] # 0 11 1 64 20 40 57 58
patient_list = ["U_5","U_7","U_8","U_9"] # "U_4", "U_18",
patient_list = ["H_0","H_1","H_2","H_3","H_4"]
patient_list = ["C_1"]
steps = ['fod','track','sift','whole']
steps = ['reg_5TT','dipy']
steps = ["preproc","5TT","reg_5TT","rois"] # ,"reg_prob"]
steps = ["reg_prob","rois"]
steps = ["preproc","5TT","reg_5TT","dipy"]
# steps = ["preproc"]
# steps = ["rois"]
n_jobs = -1
wm_mask = "yes" ; reg_step = "none" ; n_streams = 50000
algos = ["iFOD2", # default, prob with trilin interp of FOD in SH basis
"SD_STREAM", # det with SD of trilin interp of FOD
"Tensor_Det", # det with dmri: trilin interp of tensor eigenvector
"Tensor_Prob"] # prob with dmri: bootstrap + trilin interp of tensor eigenvector
refresh = False
density_map_files = []
density_path = "/auto/home/users/d/r/drimez/AFQ_data/Natbrainlab/"
with os.scandir(density_path) as my_it:
for an_entry in my_it:
density_map_files.append(density_path + an_entry.name)
tracking_algo = "dipy_prob" ; model="_sparse" ; filtering = False ; postrack=False ; reg = False ; resp=True
def compressed_pickle(title, data):
with bz2.BZ2File(title + '.pbz2', 'w') as f:
cPickle.dump(data, f)
f.close()
def decompress_pickle(filename):
data = bz2.BZ2File(filename+".pbz2", 'rb')
data = cPickle.load(data)
return data
def recursor(path,patient,iterator):
# if iterator is os.scandir("dir"), entry will be the list of contents in "dir" directory
for entry in iterator:
new_path = os.path.join(path,entry.name)
if entry.is_file():
if entry.name.split('.')[-1] == "tck":
# it's a tck file so let's convert it
# new_trk_path = os.path.join(path,".".join(entry.name.split('.')[:-1])+".trk")
# tck2trk = mrt.MRTrix2TrackVis()
# tck2trk.inputs.in_file = new_path
# tck2trk.inputs.out_filename = new_trk_path
# tck2trk.inputs.image_file = f_path + "subjects/" + patient + "/tracking/preproc/" + patient + "_dmri_preproc.nii.gz"
# tck2trk.run() # matrix_file to apply fsl affine
pass
# it's not a file so we create a new folder and iterate over entry's contents
else:
with os.scandir(new_path) as new_it:
recursor(new_path,new_it)
new_it.close()
def backup(filename,patient):
new_path = f_path + "subjects/" + patient + "/tracking/"
if not os.path.isdir(new_path + "backup/"):
os.mkdir(new_path + "backup/")
new_path = f_path + "subjects/" + patient + "/tracking/"
name, ext = os.path.splitext(filename)
name = name.split("tracking/")[-1]
exists = True ; count = -1
while exists:
count += 1
exists = os.path.exists(filename + "_" + str(count) + ".tck")
os.system('mv ' + new_path + name + ext + " " + new_path + "backup/" + name + "_" + str(count) + ext)
def bootstrap(data_to_boot,fun_to_boot):
data = np.array([data_to_boot]).flatten()
data = np.nan_to_num(data)
if np.all(data==0):
return 0
def boot_iter(data, fun_to_boot, size_):
sample = np.random.choice(data, size=size_, replace=True)
return np.nan_to_num(fun_to_boot(sample))
size_ = len(data)
result_array = Parallel(n_jobs=-1,pre_dispatch='n_jobs',require="sharedmem")( #,require="sharedmem"
delayed(boot_iter)(data, fun_to_boot, size_)
for _ in range(min(len(data),5000)*10) )
biaised_result = np.nan_to_num(fun_to_boot(data))
boot_1 = np.nan_to_num(np.nanmean(result_array))
gc.collect()
return 0.368*biaised_result + 0.632*boot_1
def generate_rois(f_path=f_path,p=None,wm_path=None,reg=False,mov=None,prefix="",replace=None): # replace must be in patient's space
if not reg in ("True",True):
log_prefix = "Tracking"
# affine registration with flirt
path = f_path + "subjects/" + p + "/tracking/preproc%s/"%prefix + p
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Affine registration launched for white matter parcellation\n")
f = open(f_path + "subjects/" + p + "/tracking/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Affine registration launched for white matter parcellation \n")
f.close()
if replace is None:
# cerebellum volbrain parcellation by default /
rep_folder = f_path + "VolBrain/Results_native/native_%s_T1"%p
replace = f_path + "wm_masks/C_1/native_lab_ln_crop_mmni_mfjob377386.nii.gz"
replace_mask = f_path + "wm_masks/C_1/native_tissue_ln_crop_mmni_mfjob377386.nii.gz"
"""
files = os.listdir(rep_folder)
replace = np.array(files)[np.array([True if ("native" in file_.split('_')) and ("lab" in file_.split('_')) else False for file_ in files])]
replace = rep_folder + replace[0]
replace_mask = np.array(files)[np.array([True if ("native" in file_.split('_')) and ("tissue" in file_.split('_')) else False for file_ in files])]
replace_mask = rep_folder + replace_mask[0]
"""
original_labels, org_lab_affine = load_nifti(wm_path)
"""
mrcmd = "mrregister -type rigid " + replace + " " + wm_path + " -mask1 " + replace_mask + " -transformed " + path + "_reg_cb.nii.gz -force"
"""
# new_labels = np.concatenate(( np.zeros((len(new_labels),(len(original_labels[0])-len(new_labels[0]))//2 +4,(len(original_labels[0,0]) ))), new_labels , np.zeros((len(new_labels),(len(original_labels[0])-len(new_labels[0]))//2 -4,(len(original_labels[0,0]) )))), axis=1)
new_labels, affine = load_nifti(replace)
new_labels = np.array([np.rot90(_) for _ in new_labels])
"""
new_labels2 = copy.copy(new_labels)
new_labels2[new_labels2!=0] = 1
save_nifti(path+"_reg_cbmask.nii.gz",new_labels2,affine)
"""
to_add = np.zeros_like(original_labels)
to_add = np.zeros_like(new_labels).astype(np.int64)
vol2free_df = pd.read_csv( "/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/volbrain_trad.txt",
sep=",", header=None, index_col=False).values
vol2free = {str(old_lab): new_lab for old_lab, new_lab in zip(vol2free_df[:,1],vol2free_df[:,0])}
intersect = [8, 47]
original_labels[original_labels==8] = 7
original_labels[original_labels==47] = 46
original_labels[original_labels==9] = 12
original_labels[original_labels==10] = 12
original_labels[original_labels==48] = 51
original_labels[original_labels==49] = 51
cb = np.zeros_like(original_labels)
cb[original_labels==7] = 1
cb[original_labels==46] = 1
save_nifti(path+"_reg_wmparc_formask.nii.gz", original_labels, org_lab_affine)
original_labels, org_lab_affine = load_nifti(wm_path)
original_labels[original_labels==8] = 0
original_labels[original_labels==47] = 0
counter = 0 ; new_lab = np.unique(new_labels)
for org_label, trad in vol2free.items():
to_add[new_labels.astype(float)==float(trad)] = float(org_label)
def reg_func(moving, static, moving_affine, static_affine, static_mask=None, moving_mask=None):
"""Convenience function for registration using a pipeline.
Uses variables in global scope, except for static_mask and moving_mask.
"""
from dipy.align import affine_registration
# pipeline = [translation, rigid]
affreg = AffineRegistration(level_iters=[200,50,20])
static_masked, moving_masked = static, moving
if static_mask is not None:
static_masked = static*static_mask
if moving_mask is not None:
moving_masked = moving*moving_mask
"""
transform = transform_centers_of_mass(static_masked, static_affine,
moving_masked, moving_affine)
starting_affine = transform.affine
"""
xform, xopt, fopt = affreg.optimize(static_masked, moving_masked,
RigidTransform3D(), None,
static_affine, moving_affine,
starting_affine=np.array( ((1,0,0,0), (0,0,-1,0), (0,1,0,0), (0,0,0,1)) ),
ret_metric=True )
starting_affine = xform.affine
"""
xformed_img, reg_affine = affine_registration(moving, static,
moving_affine=moving_affine,
static_affine=static_affine,
nbins=32, metric='MI',
pipeline=pipeline,
level_iters=level_iters,
sigmas=sigmas,
factors=factors,
static_mask=static_mask,
moving_mask=moving_mask)
"""
affine_map = AffineMap(starting_affine,
static.shape, static_affine,
moving.shape, moving_affine)
resampled = affine_map.transform(moving,interpolation="nearest")
print(resampled.shape,np.array( ((1,0,0,0), (0,0,-1,0), (0,1,0,0), (0,0,0,1)) ))
return resampled, static_affine # xformed_img, reg_affine
xformed_img, reg_affine = reg_func(to_add.astype(type(original_labels[0,0,0])), cb, affine, org_lab_affine) #, static_mask=cb)
original_labels[np.logical_and(xformed_img!=0,np.logical_or(original_labels==7,original_labels==46))] = 0
xformed_img[np.logical_and(xformed_img!=0,original_labels!=0)] = 0
save_nifti(path+"_wmparc.nii.gz", (original_labels+xformed_img).astype(type(original_labels[0,0,0])), org_lab_affine)
save_nifti(path+"_reg_cb.nii.gz", xformed_img.astype(type(original_labels[0,0,0])), org_lab_affine)
# save_nifti(path+"_wmparc.nii.gz", original_labels, org_lab_affine)
registered_t1_path = path + "_afreg_t1.nii.gz"
# registered_wm_path = path + "_afreg_wm_parc.nii.gz"
moving = f_path + "wm_masks/" + p + "/T1.nii.gz"
static = f_path + "subjects/" + p + "/T1/" + p + "_T1_corr_projected.nii.gz"
# static = f_path + "subjects/" + p + "/dMRI/preproc/" + p + "_dmri_preproc.nii.gz"
# static = f_path + "subjects/%s/tracking/preproc/%s_dmri_upsampled.nii.gz"%(p,p)
if (reg in ("affine")) or True:
flirt_t1 = "flirt -in {newvol} -ref {refvol} -out {outvol} -omat {invol2refvol}.mat -dof 6 -v".format(newvol=moving,
refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
"""
flirt_t1 += "flirt -in {newvol} -ref {refvol} -out {outvol} -init {invol2refvol}.mat -omat {invol2refvol}.mat -dof 6 ; ".format(newvol=moving,
refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
flirt_t1 += "flirt -in {newvol} -ref {refvol} -out {outvol} -init {invol2refvol}.mat -omat {invol2refvol}.mat -dof 9".format(newvol=moving,
refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
flirt_t1_aff = "flirt -in {newvol} -ref {refvol} -out {outvol} -init {invol2refvol}.mat -omat {invol2refvol}.mat -dof 12".format(newvol=moving,
refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
"""
flirt_wm_aff = "flirt -in {newvol} -ref {refvol} -out {outvol} -init {invol2refvol}.mat -dof 6 -interp nearestneighbour -applyxfm -v".format(newvol=wm_path, refvol=static, outvol=path + "_fsl_affreg_wmparc" , invol2refvol=path + "_afreg")
flirt_convert = "c3d_affine_tool -ref {refvol} -src {newvol} {invol2refvol}.mat -fsl2ras -oitk {invol2refvol}.tfm".format(newvol=moving, refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
ants_wm = "antsApplyTransforms --reference-image %s --input %s --output %s.nii.gz"%(static,path+"_new_wmparc.nii.gz",path + "_reg_wmparc_formask") + " --transform %s.tfm"%(path + "_afreg") + " --interpolation GenericLabel ; "
# ants_wm = "antsApplyTransforms --reference-image %s --input %s --output %s.nii.gz -v"%(static,path+"_reg_cbmask.nii.gz",path + "_reg_cbmask") + " --transform %s.tfm"%(path + "_afreg") + " --interpolation GenericLabel ; "
ants_wm = "antsApplyTransforms --reference-image %s --input %s --output %s.nii.gz -v"%(static,path+"_wmparc.nii.gz",path + "_reg_wmparc") + " --transform %s.tfm"%(path + "_afreg") + " --interpolation GenericLabel ; "
flirt_command = flirt_t1 + " ; " + flirt_wm_aff + " ; " + flirt_convert + " ; " + ants_wm # registration then conversion to ANTs
print(flirt_command)
bashcmd = flirt_command.split()
process = subprocess.Popen(flirt_command, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
"""
# syn registration with ants
ants_cmd = "antsRegistration --float 1 --dimensionality 3 --use-histogram-matching 1 --transform SyN[0.2,3,0] --convergence [30x99x15x15,1e-6,10] --shrink-factors 8x4x2x1 --smoothing-sigmas 4x3x2x1mm"
if reg in ("affine","defo"):
moving = path + "_afreg_t1.nii.gz"
output = path + "_reg_t1"
ants_t1 = ants_cmd + " --metric CC[%s,%s]"%(static,moving) + " --initial-moving-transform %s.tfm"%(path + "_afreg") + " --output [%s,%s.nii.gz]"%(output,output) + " --write-composite-transform 1"
moving = wm_path
output = path + "_reg_wmparc"
if reg in ("apply"):
moving = wm_path
output = f_path + "subjects/" + p + "/tracking/preproc%s/"%prefix + wm_path.split(".")[0].split("/")[-1] + "_reg"
ants_wm = "antsApplyTransforms --reference-image %s --input %s --output %s.nii.gz"%(static,moving,output) + " --transform %s"%(path + "_reg_t1Composite.h5") + " --interpolation GenericLabel "
ants_cmd = ants_t1 + " ; " if not reg == "apply" else ""
ants_cmd += ants_wm
bashcmd = ants_cmd.split()
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Deformable registration launched for white matter parcellation\n")
f = open(f_path + "subjects/" + p + "/tracking/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Deformable registration launched for white matter parcellation\n" + ants_cmd)
f.close()
process = subprocess.Popen(ants_cmd, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
"""
"""
def reg_csf(p,reg=False):
wm_path = None
if not reg:
log_prefix = "Tracking"
# affine registration with flirt
path = f_path + "subjects/" + p + "/tracking/preproc/" + p
registered_t1_path = path + "_afreg_t1.nii.gz"
# registered_wm_path = path + "_afreg_wm_parc.nii.gz"
moving = f_path + "subjects/" + p + "/T1_vertige/" + p + "_T1_brain.nii.gz"
static = f_path + "subjects/" + p + "/T1_vertige/" + p + "_T1_corr_projected.nii.gz"
flirt_t1 = "flirt -in {newvol} -ref {refvol} -out {outvol} -omat {invol2refvol}.mat -dof 9".format(newvol=moving,
refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
flirt_t1_aff = "flirt -in {newvol} -ref {refvol} -out {outvol} -init {invol2refvol}.mat -omat {invol2refvol}.mat -dof 12".format(newvol=moving,
refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
# flirt_convert = "c3d_affine_tool -ref {refvol} -src {newvol} {invol2refvol}.mat -fsl2ras -o {invol2refvol}.h5".format(newvol=moving, refvol=static, outvol=registered_t1_path, invol2refvol=path + "_afreg")
flirt_command = flirt_t1 + " ; " + flirt_t1_aff # + " ; " + flirt_convert registration then conversion to ANTs
bashcmd = flirt_command.split()
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Affine registration launched for white matter parcellation\n")
f = open(f_path + "subjects/" + p + "/tracking/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Affine registration launched for white matter parcellation\n")
f.close()
process = subprocess.Popen(flirt_command, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
# syn registration with ants
ants_cmd = "antsRegistration --float 1 --dimensionality 3 --use-histogram-matching 1 --transform SyN[0.2,3,0] --convergence [30x99x11,1e-6,10] --shrink-factors 8x4x2 --smoothing-sigmas 2x2x2vox"
moving = path + "_afreg_t1.nii.gz"
output = path + "_reg_t1"
ants_t1 = ants_cmd + " --metric CC[%s,%s]"%(static,moving) + " --initial-moving-transform [%s,%s,1]"%(static,moving) + " --output [%s,%s.nii.gz]"%(output,output) + " --write-composite-transform 1"
moving = wm_path
output = path + "_reg_wm_parc"
ants_wm = ants_cmd + " --interpolation GenericLabel" + " --metric CC[%s,%s]"%(static,moving) + " --initial-moving-transform %s"%(path + "_reg_t1Composite.h5") + " --output [%s,%s.nii.gz]"%(output,output)
ants_cmd = ants_t1 + " ; " + ants_wm
bashcmd = ants_cmd.split()
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Deformable registration launched for white matter parcellation\n")
f = open(f_path + "subjects/" + p + "/tracking/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Deformable registration launched for white matter parcellation\n")
f.close()
process = subprocess.Popen(ants_cmd, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
"""
def _resample_tg(tg, n_points):
# reformat for dipy's set_number_of_points
if isinstance(tg, np.ndarray):
if len(tg.shape) > 2:
streamlines = tg.tolist()
streamlines = [np.asarray(item) for item in streamlines]
elif isinstance(tg, list):
streamlines = [np.asarray(item) for item in tg]
else:
streamlines = tg.streamlines
return dps.set_number_of_points(streamlines, n_points)
def dipy_tracto(p,n_iterations=12,model=model,f_path=f_path,density_map_files=density_map_files,fod=True,single_bdl=None,filtering=filtering,postrack=postrack,resp=None):
if isinstance(fod,str):
fod = fod=="True"
if isinstance(resp,str):
resp = resp=="True"
if isinstance(n_iterations,str):
n_iterations = int(n_iterations)
if model=="_prob" and single_bdl is None:
n_iterations = 25
# loads dmri and white matter mask
wm_path = f_path + "wm_masks/%s/wm.seg.nii.gz"%(p)
T1_path = f_path + "wm_masks/%s/T1.nii.gz"%(p)
bck_path = f_path + "subjects/%s/tracking/preproc/%s_bck_mask.nii.gz"%(p,p)
csf_pve = f_path + "subjects/" + p + "/tracking/preproc/" + p + "_csf_pve.nii.gz"
gm_pve = f_path + "subjects/" + p + "/tracking/preproc/" + p + "_gm_pve.nii.gz"
wm_pve = f_path + "subjects/" + p + "/tracking/preproc/" + p + "_wm_pve.nii.gz"
wm_dil = f_path + "subjects/" + p + "/tracking/preproc/" + p + "_wm_pve_dil.nii.gz"
wm_dil_th = f_path + "subjects/" + p + "/tracking/preproc/" + p + "_wm_pve_dil_th.nii.gz"
gm_pve_sub = f_path + "subjects/" + p + "/tracking/preproc/" + p + "_gm_pve_sub.nii.gz"
csf_path_2 = f_path + "subjects/%s/tracking/preproc/%s_csf_mask.nii.gz"%(p,p)
gm_path_2 = f_path + "subjects/%s/tracking/preproc/%s_gm_mask.nii.gz"%(p,p)
cbmask = f_path + "subjects/%s/tracking/preproc/%s_reg_cbmask.nii.gz"%(p,p)
seg_path = f_path + "subjects/%s/masks_vertige/%s_segmentation.nii.gz"%(p,p)
dmri_root = f_path + "subjects/%s/dMRI/preproc/%s_dmri_preproc"%(p,p)
dire = f_path + "subjects/%s/tracking/preproc/"%p
ref_t1 = f_path + "subjects/" + p + "/T1/" + p + "_T1_corr_projected.nii.gz"
bvals, bvecs = read_bvals_bvecs(dmri_root+".bval", dmri_root+".bvec")
gtab = gradient_table(bvals, bvecs)
dmri_preproc = f_path + "subjects/%s/dMRI/preproc/%s_dmri_preproc.nii.gz"%(p,p)
data, ref_affine, ref_image = load_nifti(dmri_preproc, return_img=True)
registered_data_wm, _ = load_nifti(dire+"/"+p+"_wm_dil.nii.gz")
# conv_bckgd = "mrconvert " + bck_path + " " + bck_path.split(".")[0] + ".mif.gz -force"
# conv_csf += "mrconvert " + csf_path_2.split(".")[0] + ".mif.gz " + csf_path_2 + " -force ; "
# conv_csf += " ; maskfilter " + csf_path_2.split(".")[0] + "_eroded.nii.gz erode -npass 1 " + csf_path_2.split(".")[0] + "_eroded.nii.gz -force ; "
# conv = conv_bckgd + " ; " + conv_csf
# bashcmd = conv.split()
# process = subprocess.Popen(conv, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=subprocess.STDOUT)
# wait until finish
# out, error = process.communicate()
####### generate streamlines
# constrained spherical deconvolution
# CSA odf + stopping criterion
# gfa = csa_model.fit(data, mask=white_matter).gfa
# stopping_criterion = ThresholdStoppingCriterion(gfa, .25)
output_name = f_path + "subjects/" + p + "/tracking/" + p + "_dipy" + model
if not postrack:
if not os.path.isdir(f_path+"subjects/%s/tracking/qcontrol"%p):
os.mkdir(f_path+"subjects/%s/tracking/qcontrol"%p)
response_wm_mask, _ = load_nifti(dire+"/"+p+"_wm_new.nii.gz")
if resp==True and False:
response, ratio = response_from_mask_ssst(gtab, data, response_wm_mask)
print("WM mask response: " + str(response))
print("ratio: " + str(ratio))
csd_model = ConstrainedSphericalDeconvModel(gtab, response, convergence=1000000)
csd_fit = csd_model.fit(data, mask=registered_data_wm)
fod = csd_fit.odf(default_sphere)
pmf = fod.clip(min=0)
save_nifti(f_path+"subjects/%s/tracking/qcontrol/csd_odfs_wm.nii.gz"%p,pmf,ref_affine)
"""
scene = window.Scene()
fodf_spheres = actor.odf_slicer(pmf, sphere=default_sphere, scale=0.9, norm=False, colormap='plasma')
scene.add(fodf_spheres)
print('Saving illustration as csd_odfs.png')
window.record(scene, out_path=f_path+"subjects/%s/tracking/qcontrol/csd_odfs_wm.png"%p, size=(600, 600))
"""
response, ratio = auto_response_ssst(gtab, data, roi_radii=10, fa_thr=0.7)
print("WM mask response: " + str(response))
print("ratio: " + str(ratio))
csd_model = ConstrainedSphericalDeconvModel(gtab, response, convergence=1000000)
csd_fit = csd_model.fit(data, mask=registered_data_wm)
fod = csd_fit.odf(default_sphere)
pmf = fod.clip(min=0)
save_nifti(f_path+"subjects/%s/tracking/qcontrol/csd_odfs_auto.nii.gz"%p,pmf,ref_affine)
"""
scene.clear()
scene = window.Scene()
fodf_spheres = actor.odf_slicer(pmf, sphere=default_sphere, scale=0.9, norm=False, colormap='plasma')
scene.add(fodf_spheres)
print('Saving illustration as csd_odfs.png')
window.record(scene, out_path=f_path+"subjects/%s/tracking/qcontrol/csd_odfs_auto.png"%p, size=(600, 600))
scene.clear()
"""
sys.setrecursionlimit(10000)
response = None ; csd_model = None ; pmf = None
if model == "" or True:
if not os.path.exists(f_path+"subjects/%s/tracking/FOD/%s_response%s.pbz2"%(p,p,model)) or True:
print("Launching response estimation")
fa_thr = 0.7 if model=="" else 0.8
response_mask = mask_for_response_ssst(gtab, data, roi_radii=response_wm_mask.shape[0], fa_thr=0.8)
response_mask[response_wm_mask==0] = 0
response, ratio = response_from_mask_ssst(gtab, data, response_mask)
print("WM mask response: " + str(response))
print("ratio: " + str(ratio))
compressed_pickle(f_path+"subjects/%s/tracking/FOD/%s_response%s"%(p,p,model),response)
else:
response = decompress_pickle(f_path+"subjects/%s/tracking/FOD/%s_response%s"%(p,p,model))
if (not os.path.exists(f_path+"subjects/%s/tracking/FOD/%s_csd_model.pbz2"%(p,p))) or True:
print("Launching CSD estimation")
csd_model = ConstrainedSphericalDeconvModel(gtab, response, convergence=1000000)
csd_fit = csd_model.fit(data, mask=registered_data_wm)
compressed_pickle(f_path+"subjects/%s/tracking/FOD/%s_csd_model%s"%(p,p,model),csd_model)
else:
csd_model = decompress_pickle(f_path+"subjects/%s/tracking/FOD/%s_csd_model"%(p,p))
if not os.path.exists(f_path+"subjects/%s/tracking/FOD/%s_fod%s.pbz2"%(p,p,model)) and model=="_prob":
print("Launching FOD fit")
fod = csd_fit.odf(default_sphere)
pmf = fod.clip(min=0)
compressed_pickle(f_path+"subjects/%s/tracking/FOD/%s_fod%s"%(p,p,model),pmf)
elif model=="_prob":
pmf = decompress_pickle(f_path+"subjects/%s/tracking/FOD/%s_fod%s"%(p,p,model))
else:
if os.path.exists(f_path+"subjects/%s/tracking/FOD/%s_mrtrix_fod.nii.gz"%(p,p)):
pmf, _ = load_nifti(f_path+"subjects/%s/tracking/FOD/%s_mrtrix_fod.nii.gz"%(p,p))
else:
data = "data_1" if p[0]=="H" else "data_2"
rep = "dwi2response tournier /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/dMRI/preproc/{p}_dmri_preproc.nii.gz /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_response.txt -mask /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/preproc/{p}_wm_new.nii.gz -fslgrad /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bvec /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bval -lmax 12 -force".format(p=p,data=data)
fod = "dwi2fod csd /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/dMRI/preproc/{p}_dmri_preproc.nii.gz /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_response.txt /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_fod.nii.gz -lmax 12 -mask /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/preproc/{p}_wm_new.nii.gz -fslgrad /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bvec /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bval -directions /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/H_0/tracking/FOD/sphere.txt -force".format(p=p,data=data)
cmd = rep + " ; " + fod
cmd.split()
process = subprocess.Popen(cmd, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
pmf, _ = load_nifti(f_path+"subjects/%s/tracking/FOD/%s_mrtrix_fod.nii.gz"%(p,p))
stopping_criterion = BinaryStoppingCriterion(registered_data_wm)
# gfa = csd_model.gfa
# stopping_criterion = ThresholdStoppingCriterion(gfa, .1)
if resp:
"""
scene = window.Scene()
fodf_spheres = actor.odf_slicer(pmf, sphere=default_sphere, scale=0.9, norm=False, colormap='plasma')
scene.add(fodf_spheres)
print('Saving illustration as csd_odfs.png')
window.record(scene, out_path=f_path+"subjects/%s/tracking/qcontrol/csd_odfs_wmfa.png"%p, size=(600, 600))
response_rec = recursive_response( gtab, data, mask= response_wm_mask==1,
sh_order=8, peak_thr=0.01, init_fa=0.08,
init_trace=0.0021, iter=8, convergence=0.001,
parallel=True , num_processes=-1 )
response_rec2 = response_rec.on_sphere(default_sphere)
print("WM mask response: " + str(response_rec2))
# print("ratio: " + str(response_rec[0][0]/response_rec[0][1]))
csd_model_rec = ConstrainedSphericalDeconvModel(gtab, response_rec, convergence=10000)
csd_fit_rec = csd_model_rec.fit(data, mask=response_mask)
fod_rec = csd_fit_rec.odf(default_sphere)
pmf_rec = fod_rec.clip(min=0)
save_nifti(f_path+"subjects/%s/tracking/qcontrol/csd_odfs_rec.nii.gz"%p,pmf,ref_affine)
# scene.rm(response_actor)
scene.clear()
scene = window.Scene()
fodf_spheres = actor.odf_slicer(pmf_rec, sphere=default_sphere, scale=0.9, norm=False, colormap='plasma')
scene.add(fodf_spheres)
print('Saving illustration as csd_odfs.png')
window.record(scene, out_path=f_path+"subjects/%s/tracking/qcontrol/csd_odfs_rec.png"%p, size=(600, 600))
"""
resp=False
"""
scene.clear()
scene = window.Scene()
fodf_peaks = actor.peak_slicer(peak_model.peak_dirs, peak_model.peak_values,colors=None)
scene.add(fodf_peaks)
print('Saving illustration as csd_peaks.png')
window.record(scene, out_path=f_path+"subjects/%s/tracking/qcontrol/csd_peaks.png"%p, size=(600, 600))
"""
"""
if not single_bdl is None:
density_map_files = [density_map_files]
else:
density_path = "/auto/home/users/d/r/drimez/AFQ_data/subjects/%s/"%p
with os.scandir(density_path) as my_it:
for an_entry in my_it:
density_map_files.append(density_path + an_entry.name)
"""
def single_iter(n_iter,single_bdl=None,pmf=pmf,registered_data_wm=registered_data_wm,stopping_criterion=stopping_criterion, csd_model=csd_model,
ref_image=ref_image,output_name=output_name,model=model,f_path=f_path,density_map_files=density_map_files,filtering=filtering):
peak_model = None
if model == "_prob":
rel_th = np.random.rand(1)*0.1 + 0.8 # between 0.8 and 0.9
max_angle = np.random.rand(1)*5 + 20
peak_model = ProbabilisticDirectionGetter.from_pmf( pmf, max_angle=max_angle, sphere=default_sphere, pmf_threshold=0.2,
relative_peak_threshold = rel_th, min_separation_angle=5 )
elif model == "_sparse":
sf_model = sfm.SparseFascicleModel(gtab, sphere=peaks.default_sphere, l1_ratio=0.3, alpha=0.001, response=response[0])
sf_fit = sf_model.fit(data)
sf_odf = sf_fit.odf(peaks.default_sphere)
peak_model = peaks.peaks_from_model(model=sf_model, data=data, sphere=peaks.default_sphere,
relative_peak_threshold=.8, min_separation_angle=30,
mask=registered_data_wm, parallel=True)
elif model == "_boot":
peak_model = BootDirectionGetter.from_data(data, csd_model, max_angle=25, sphere=small_sphere)
else:
rel_th = np.random.rand(1)*0.15 + 0.75 # between 0.7 and 0.9
peak_model = peaks.peaks_from_model(model=csd_model, data=data, sphere=peaks.default_sphere,
relative_peak_threshold=rel_th, min_separation_angle=15,
mask=registered_data_wm, parallel=True, normalize_peaks=True,
num_processes=1)
affine = np.eye(4)
# seeds from dilated wm
seeding_mask = copy.copy(registered_data_wm)
seeds_count = 1 # seeding_mask.sum()/4
if not (single_bdl is None):
density_map_img, _ = load_nifti(density_map_files[0])
seeding_mask[density_map_img<=0.01] = 0
seeds_count = 4 # 5*np.sum(seeding_mask>0.01)
seeds = utils.random_seeds_from_mask( seeding_mask, affine, seed_count_per_voxel=True,
seeds_count = int(seeds_count) )#
if filtering:
pve_path = f_path + "subjects/" + p + "/tracking/preproc/" + p
pve_csf_data, _ = load_nifti(pve_path + "_csf_pve.nii.gz")
pve_gm_data, _ = load_nifti(pve_path + "_gm_pve.nii.gz")
pve_wm_data, _, voxel_size = load_nifti(pve_path + "_wm_pve.nii.gz", return_voxsize=True)
voxel_size = np.average(voxel_size[1:4])
stopping_criterion = CmcStoppingCriterion.from_pve(pve_wm_data, pve_gm_data, pve_csf_data,
step_size=0.5, average_voxel_size=voxel_size)
model = model + "_PFT"
pve_path = f_path + "subjects/" + p + "/tracking/preproc/" + p
pve_csf_data, _ = load_nifti(pve_path + "_csf_pve.nii.gz")
pve_gm_data, _ = load_nifti(pve_path + "_gm_pve.nii.gz")
pve_wm_data, _, voxel_size = load_nifti(pve_path + "_wm_pve.nii.gz", return_voxsize=True)
voxel_size = np.average(voxel_size[1:4])
stopping_criterion = CmcStoppingCriterion.from_pve(pve_wm_data, pve_gm_data, pve_csf_data,
step_size=0.2, average_voxel_size=voxel_size)
streamline_generator = ParticleFilteringTracking(peak_model, stopping_criterion, seeds, affine, step_size=0.2,
maxlen=1000, pft_back_tracking_dist=2, pft_front_tracking_dist=1,
particle_count=25, return_all=False)
else:
step_size = 0.5 if model=="_prob" and True else 0.5
streamline_generator = LocalTracking(peak_model, stopping_criterion, seeds, affine=affine, step_size=step_size)
streamlines = Streamlines(streamline_generator)
sft = StatefulTractogram(streamlines, ref_image, Space.VOX)
is_saved = save_tck(sft, output_name + "_%s.tck"%n_iter)
# crop to non-dilated wm after filtering tracks that pass through bcgd and not through csf
crop = "tckresample " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -step_size 0.5 -force ; "%n_iter if False and model=="_prob" else ""
crop = "tckedit " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -include "%n_iter + gm_path_2 + " -exclude " + csf_path_2 # " -include " + cbmask +
if n_iter%2==0:
crop = crop + " -force ; tckedit " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -mask "%n_iter + dire+"/"+p+"_wm_new.nii.gz -force ; "
else:
crop = crop + " -force ; tckedit " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -mask "%n_iter + dire+"/"+p+"_wm_allnew.nii.gz -force ; "
crop = crop + "tckedit " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -minlength 20 -force ; "%n_iter # removes short tracks
"""
if n_iter%15==0:
t1 = load_tck(output_name + "_%s.tck"%n_iter, f_path + "subjects/%s/dMRI/preproc/"%p + p + "_dmri_preproc.nii.gz")
crop += "tcksift " + output_name + "_%s.tck "%n_iter + f_path + "subjects/" + p + "/tracking/preproc/" + p + "_fod.nii.gz " + output_name + "_%s.tck "%n_iter + "-term_number " + str(len(t1.streamlines))
"""
# resamples the streamlines
# crop = crop + "tckresample " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -step_size -force ; "%n_iter
bashcmd = crop.split()
process = subprocess.Popen(crop, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
"""
t1 = load_tck(output_name + "_%s.tck"%n_iter, dmri_preproc)
# seg_obj = Segmentation(seg_algo="AFQ",prob_threshold=0.1, return_idx=True, dany=False)
save_tck(t1,output_name + "_%s.tck"%n_iter)
"""
return True
SIFT = False ; cpu_count = max(1,3) ; done = False # multiprocessing.cpu_count()
if n_iterations>=cpu_count and (single_bdl is None): # no sigle_bdle in loop
for n_iter in range(n_iterations):
# done = Parallel(n_jobs=min(5,cpu_count),verbose=60,pre_dispatch="n_jobs")(delayed(single_iter)(n_iter=subiter,single_bdl=None) for subiter in np.arange(n_iter*cpu_count,(n_iter+1)*5))
#for subiter in np.arange(n_iter*cpu_count,(n_iter+1)*cpu_count):
# print(" ====== Tracking: %s / %s ======= "%(subiter,n_iterations))
done = single_iter(n_iter=n_iter,single_bdl=single_bdl)
if done:
def check_size(output_name=output_name,SIFT=SIFT,dmri_preproc=dmri_preproc,f_path=f_path,n_iter=n_iter,n_iterations=n_iterations,model=model):
if os.path.exists(output_name + ".trk"):
if os.path.getsize(output_name + ".trk")>=750000000 and not SIFT and n_iter<=n_iterations//2 and False:
SIFT = True
t1 = load_tck(output_name + ".tck", dmri_preproc)
sift = "tcksift " + output_name + ".tck " + f_path + "subjects/" + p + "/tracking/preproc/" + p + "_fod.nii.gz " + output_name + ".tck " + "-term_number " + str(len(t1.streamlines)//2)
# saved += output_name + "_%s.tck "%n_iter
# resamples the streamlines
# crop = crop + "tckresample " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -step_size -force ; "%n_iter
bashcmd = sift.split()
process = subprocess.Popen(sift, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=subprocess.stderr)
# wait until finish
out, error = process.communicate()
elif os.path.getsize(output_name + ".trk")>=900000000 and model=="_prob":
print("File size exceeded")
return True
elif os.path.getsize(output_name + ".trk")>=450000000 and not model=="_prob":
print("File size exceeded")
return True
return False
saved = "" ; check_ = False
with os.scandir(f_path + "subjects/%s/tracking/"%p) as it:
for entry in it:
if len(entry.name.split('.'))>1:
if entry.name.split('.')[-1]=="tck" \
and ("prob" in entry.name.split('_')
or model == ""):
check_ = check_size()
if not check_:
t1 = load_tck(entry.path , dmri_preproc)
trk = None
if os.path.exists(output_name + ".trk"):
t2 = load_trk(output_name + ".trk", dmri_preproc)
trk = StatefulTractogram( list(t1.streamlines) + list(t2.streamlines),
dmri_preproc,
Space.RASMM,
data_per_streamline={k: (list(t1.data_per_streamline[k]) + list(t2.data_per_streamline[k]))
for k in t2.data_per_streamline.keys() })
else:
trk = t1
save_trk(trk,output_name + ".trk")
os.system("rm "+ entry.path)
if check_:
break
# ensures no correspondances with classical streamlines when highlighting thalamic projections
"""
if n_iter%2==0 or single_bdl:
idx_above_prob = np.arange(len(t1.streamlines))[np.all(probs < 0.9, axis=-1)]
t1 = StatefulTractogram( list(t1.streamlines[idx_above_prob]) ,
f_path + "subjects/%s/dMRI/preproc/"%p + p + "_dmri_preproc.nii.gz",
Space.RASMM )
pass
# select streamlines with probability maps
else:
fgarray = np.array(_resample_tg(t1, 100))
probs = np.zeros((len(t1.streamlines), len(density_map_files)) )
for i_dens_map, density_map in enumerate(density_map_files):
if i_dens_map%5==0:
print("[ prob ] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": starting map %s, %s / %s\n"%(density_map.split('/')[-1].split('.')[0], i_dens_map, len(density_map_files)) )
density_map_img, _ = load_nifti(density_map)
fiber_probabilities = dts.values_from_volume(density_map_img, fgarray, np.eye(4))
probs[:,i_dens_map] = np.mean(fiber_probabilities,axis=-1)
prob_th = 0.005 if model=="_prob" else 0.01
idx_above_prob = np.arange(len(t1.streamlines))[np.any(probs >= prob_th, axis=-1).flatten()]
t1 = StatefulTractogram( list(t1.streamlines[idx_above_prob]) ,
f_path + "subjects/%s/dMRI/preproc/"%p + p + "_dmri_preproc.nii.gz",
Space.RASMM )
query(model,"_"+str(n_iter))
if os.path.exists(f_path + "subjects/" + patient + "/tracking/" + patient + "_dipy" + mod + "_cleaned%s.trk"%("_"+str(n_iter))):
os.system("rm "+ output_name + ".trk")
"""
"""
# merges results
with os.scandir(f_path + "subjects/%s/tracking/"%p) as it:
for entry in it:
if os.path.isfile(f_path + "subjects/%s/tracking/"%p + entry.name) \
and entry.name.split('.')[-1]=="trk" \
and ("prob" in entry.name.split('_') or model == ""):# \
#and "cleaned" in entry.name.split('_'):
trk = None ; t1 = load_trk(f_path + "subjects/%s/tracking/"%p + entry.name, dmri_preproc)
if os.path.exists(output_name + ".trk"):
t2 = load_trk(output_name + ".trk", dmri_preproc)
trk = StatefulTractogram( list(t1.streamlines) + list(t2.streamlines),
dmri_preproc,
Space.RASMM,
data_per_streamline={k: (list(t1.data_per_streamline[k]) + list(t2.data_per_streamline[k]))
for k in t2.data_per_streamline.keys() })
else:
trk = t1
save_trk(trk,output_name + ".trk")
"""
if not done:
print("Hello")
if not single_bdl is None:
if not os.path.isdir(f_path + "subjects/%s/tracking/Solo/"%p):
os.mkdir(f_path + "subjects/%s/tracking/Solo/"%p)
output_name = f_path + "subjects/%s/tracking/Solo/"%p + p + "_dipy" + model + "_" + single_bdl
for n_iter in range(n_iterations):
print(" ====== Tracking: %s / %s ======= "%(n_iter,n_iterations))
single_iter(n_iter=n_iter,single_bdl=single_bdl)
if os.path.exists(output_name + ".trk"):
def check_size(output_name=output_name,SIFT=SIFT,dmri_preproc=dmri_preproc,f_path=f_path,n_iter=n_iter,n_iterations=n_iterations,model=model):
if os.path.getsize(output_name + ".trk")>=750000000 and not SIFT and n_iter<=n_iterations//2 and False:
SIFT = True
t1 = load_tck(output_name + "_%s.tck"%n_iter, f_path + "subjects/%s/dMRI/preproc/"%p + p + "_dmri_preproc.nii.gz")
sift = "tcksift " + output_name + "_%s.tck "%n_iter + f_path + "subjects/" + p + "/tracking/preproc/" + p + "_fod.nii.gz " + output_name + "_%s.tck "%n_iter + "-term_number " + str(len(t1.streamlines)//2)
# saved += output_name + "_%s.tck "%n_iter
# resamples the streamlines
# crop = crop + "tckresample " + output_name + "_%s.tck "%n_iter + output_name + "_%s.tck -step_size -force ; "%n_iter
bashcmd = sift.split()
process = subprocess.Popen(sift, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
elif os.path.getsize(output_name + ".trk")>=900000000 and model=="_prob":
print("File size exceeded")
os.system("rm " + "/".join(output_name.split("/")[:-1]) + "*.tck")
return True
elif os.path.getsize(output_name + ".trk")>=450000000 and not model=="_prob":
print("File size exceeded")
os.system("rm " + "/".join(output_name.split("/")[:-1]) + "*.tck")
return True
return False
"""
saved = ""
for subiter in np.arange(n_iter*5,(n_iter+1)*5):
saved += output_name + "_%s.tck "%subiter
merge = "tckedit " + saved + " " + output_name + ".tck ; rm " + saved
bashcmd = merge.split()
process = subprocess.Popen(merge, universal_newlines=True, shell=True, stdout=sys.stdout, stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
t1 = load_tck(output_name + ".tck", f_path + "subjects/%s/dMRI/preproc/"%p + p + "_dmri_preproc.nii.gz")
"""
saved = "" ; check_ = False
with os.scandir(f_path + "subjects/%s/tracking/"%p) as it:
for entry in it:
if len(entry.name.split('.'))>1:
if entry.name.split('.')[-1]=="tck" \
and ("prob" in entry.name.split('_') or model == ""):
t1 = load_tck(entry.path, dmri_preproc)
trk = None
if os.path.exists(output_name + ".trk"):
t2 = load_trk(output_name + ".trk", dmri_preproc)
trk = StatefulTractogram( list(t1.streamlines) + list(t2.streamlines),
dmri_preproc,
Space.RASMM,
data_per_streamline={k: (list(t1.data_per_streamline[k]) + list(t2.data_per_streamline[k]))
for k in t2.data_per_streamline.keys() })
else:
trk = t1
save_trk(trk,output_name + ".trk")
os.system("rm "+ entry.path)
check_ = check_size()
if check_:
break
if check_:
break
"""
query(model,"_"+str(n_iter))
if os.path.exists(f_path + "subjects/" + patient + "/tracking/" + patient + "_dipy" + mod + "_cleaned%s.trk"%("_"+str(n_iter))):
os.system("rm "+ output_name + ".trk")
"""
# merges results
with os.scandir(f_path + "subjects/%s/tracking/"%p) as it:
for entry in it:
if os.path.isfile(f_path + "subjects/%s/tracking/"%p + entry.name) \
and entry.name.split('.')[-1]=="trk" \
and ("prob" in entry.name.split('_') or model == "") \
and "cleaned" in entry.name.split('_'):
trk = None ; t1 = load_trk(f_path + "subjects/%s/tracking/"%p + entry.name, dmri_preproc)
if os.path.exists(output_name + ".trk"):
t2 = load_trk(output_name + ".trk", dmri_preproc)
trk = StatefulTractogram( list(t1.streamlines) + list(t2.streamlines),
dmri_preproc,
Space.RASMM,
data_per_streamline={k: (list(t1.data_per_streamline[k]) + list(t2.data_per_streamline[k]))
for k in t2.data_per_streamline.keys() })
else:
trk = t1
save_trk(trk,output_name + ".trk")
return True
def tracto(f_path=f_path,patient_list=patient_list,wm_mask=wm_mask,steps=steps,n_streams=n_streams,n_jobs=n_jobs,notlist=False,reg=reg,density_map_files=density_map_files,parrallel=False):
if "FOD" in steps:
response_list = ""
patient_list = json.load(open("/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/subj_list.json.json","r"))
for p in patient_list:
data = "data_1" if p[0]=="H" else "data_2"
resp = "dwi2response tournier /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/preproc/{p}_dmri_upsampled.nii.gz /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_response.txt -mask /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/preproc/{p}_wm_new.nii.gz -fslgrad /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bvec /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bval -lmax 12 -peak_ratio 0.01 -force".format(p=p,data=data)
cmd = rep
process = subprocess.Popen(step_6, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
response_list += "/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_response.txt "
os.system("responsemean " + + "/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/group_average_response.txt")
"""
for p in patient_list:
fod = "dwi2fod csd /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/preproc/{p}_dmri_upsampled.nii.gz /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_response.txt /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/FOD/{p}_mrtrix_fod.nii.gz -lmax 12 -mask /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/{p}/tracking/preproc/{p}_wm_new.nii.gz -fslgrad /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bvec /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/{data}/{p}.bval -directions /CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/subjects/H_0/tracking/FOD/sphere.txt -force".format(p=p,data=data)
cmd = rep fod
process = subprocess.Popen(step_6, universal_newlines=True, shell=True, stdout=sys.stdout,
stderr=subprocess.STDOUT)
# wait until finish
out, error = process.communicate()
"""
# notlist = True
f_path = "/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/PROJECT/"
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_list = patient_list if isinstance(patient_list,list) else [patient_list]
"""
patient_list = ["U_"+str(_) for _ in range(78) if os.path.exists(f_path+"subjects/U_"+str(_)+"/dMRI/preproc/U_"+str(_)+"_dmri_preproc.nii.gz") \
and not os.path.exists("/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/ELIKOPY_subset/PROJECT/subjects/U_%s/tracking/preproc/U_%s_reg_wmparc.nii.gz"%(_,_))]
"""
if steps==["rois"] and not parrallel and False:
Parallel(n_jobs=n_jobs,verbose=500,pre_dispatch='2*n_jobs')( delayed(tracto)( f_path,[patient],wm_mask,["rois"],n_streams,n_jobs,
notlist,reg,density_map_files,parrallel=True )
for patient in patient_list )
else:
for patient in patient_list:
path = f_path + "subjects/" + patient + "/"
new_path = path + "tracking/"
if not os.path.isdir(new_path):
os.mkdir(new_path)
elif os.path.isdir(new_path+"preproc") and "preproc" in steps and not refresh:
if os.path.exists("/CECI/proj/pilab/PermeableAccess/vertige_LEWuQhzYs9/ELIKOPY_subset/PROJECT/subjects/%s/tracking/preproc/%s_reg_wmparc.nii.gz"%(patient,patient)):
if not os.path.exists(new_path + "ROIs/metrics.csv"):
steps = ["rois"]
else:
print(patient+" already done",file=open("/auto/home/users/d/r/drimez/PREPROC.txt","a"))
break
elif len(os.listdir(new_path+"preproc"))>15 and not refresh:
print(patient+" already done",file=open("/auto/home/users/d/r/drimez/PREPROC.txt","a"))
break
log_prefix = "Tracking"
print("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Tracking launched for patient " + patient + "\n")
f = open(new_path + "/logs.txt", "a+")
f.write("[" + log_prefix + "] " + datetime.datetime.now().strftime(
"%d.%b %Y %H:%M:%S") + ": Tracking launched for patient " + patient + "\n")
f.close()
if not os.path.isdir(new_path+"preproc"):