-
Notifications
You must be signed in to change notification settings - Fork 0
/
resting_pipeline.py
executable file
·1490 lines (1315 loc) · 77.6 KB
/
resting_pipeline.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
#!/bin/env python3
# -*- coding: iso-8859-1 -*-
import numpy as np
import numpy.ma
import nibabel
from scipy import signal
import os, sys, subprocess
import string, random
import re
import networkx as nx
from optparse import OptionParser, OptionGroup
import logging
import math
from scipy import ndimage as nd
from shutil import copyfile
logging.basicConfig(format='%(asctime)s %(message)s ', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.INFO)
usage ="""
resting_pipeline.py --func /path/to/run4.bxh --steps all --outpath /here/ -p func
Program to run through Nan-kuei Chen's resting state analysis pipeline:
steps:
0 - convert data to nii in LAS orientation ( we suggest LAS if you are skipping this step )
1 - slice time correction
2 - motion correction, then regress out motion parameter
3 - skull stripping
4 - normalize data
5 - regress out WM/CSF
6 - lowpass filter
7 - do parcellation and produce correlation matrix from label file
* or split it up:
7a - do parcellation from label file
7b - produce correlation matrix [--func option is ignored if step 7b
is run by itself unless --dvarsthreshold is specified, and
--corrts overrides default location for input parcellation
results (outputpath/corrlabel_ts.txt)]
8 - functional connectivity density mapping
"""
parser = OptionParser(usage=usage)
parser.add_option("-f", "--func", action="store", type="string", dest="funcfile",help="bxh ( or nifti ) file for functional run", metavar="/path/to/BXH")
parser.add_option("--throwaway", action="store", type="int", dest="throwaway",help="number of timepoints to dis-regard from beginning of run", metavar="4")
parser.add_option("--t1", action="store", type="string", dest="anatfile",help="bxh ( or nifti ) file for the anatomical T1", metavar="/path/to/BXH")
parser.add_option("-p", "--prefix", action="store", type="string", dest="prefix",help="prefix for all resulting images, defaults to name of input", metavar="func")
parser.add_option("-s", "--steps", action="store", type="string", dest="steps",help="comma seperated string of steps. 'all' will run everything, default is all", metavar="0,1,2,3", default='all')
parser.add_option("-o","--outpath", action="store",type="string", dest="outpath",help="location to store output files", metavar="PATH", default='PWD')
parser.add_option("--sliceorder", action="store",type="string", dest="sliceorder",help="sliceorder if slicetime correction ( odd=interleaved (1,3,5,2,4,6), up=ascending, down=descending, even=interleaved (2,4,6,1,3,5) ). Default is to read this from input image, if available.", metavar="string")
parser.add_option("--tr", action="store", type="float", dest="tr_ms",help="TR of functional data in MSEC", metavar="MSEC")
parser.add_option("--ref", action="store", type="string", dest="flirtref",help="pointer to FLIRT reference image if not using standard brain", metavar="FILE")
parser.add_option("--flirtdof", action="store", type="int", dest="flirtdof",help="How many degrees-of-freedom for FLIRT to use when registering T1 (anatomical) to reference image (default is 12). Registering functional to anatomical always uses 6 dof.", metavar="NUM")
parser.add_option("--flirtmat", action="store", type="string", dest="flirtmat",help="a pre-defined flirt matrix to apply to your functional data. (ie: func2standard.mat)", metavar="FILE")
parser.add_option("--refwm", action="store", type="string", dest="refwm",help="pointer to WM mask of reference image if not using standard brain", metavar="FILE")
parser.add_option("--refcsf", action="store", type="string", dest="refcsf",help="pointer to CSF mask of reference image if not using standard brain", metavar="FILE")
parser.add_option("--refgm", action="store", type="string", dest="refgm",help="pointer to GM mask of reference image if not using standard brain", metavar="FILE")
parser.add_option("--refbrainmask", action="store", type="string", dest="refbrainmask",help="pointer to brain mask of reference image if not using standard brain", metavar="FILE")
parser.add_option("--refacpoint", action="store", type="string", dest="refac",help="AC point of reference image if not using standard MNI brain", metavar="45,63,36", default="45,63,36")
parser.add_option("--betfval", action="store", type="float", dest="betfval",help="f value to use while skull stripping. default is 0.4", metavar="0.4", default='0.4')
parser.add_option("--anatbetfval", action="store", type="float", dest="anatbetfval",help="f value to use while skull stripping ANAT. default is 0.5", metavar="0.5", default='0.5')
parser.add_option("--lpfreq", action="store", type="float", dest="lpfreq",help="frequency cutoff for lowpass filtering in HZ. default is .08hz", metavar="0.08", default='0.08')
parser.add_option("--corrlabel", action="store", type="string", dest="corrlabel",help="pointer to 3D label containing ROIs for the correlation search. default is the 116 region AAL label file", metavar="FILE")
parser.add_option("--corrtext", action="store", type="string", dest="corrtext",help="pointer to text file containing names/indices for ROIs for the correlation search. default is the 116 region AAL label txt file", metavar="FILE")
parser.add_option("--corrts", action="store", type="string", dest="corrts",help="If using step 7b by itself, this is the path to parcellation output (default is to use OUTPATH/corrlabel_ts.txt), which will be used as input to the correlation.", metavar="FILE")
parser.add_option("--dvarsthreshold", action="store", type="string", dest="dvarsthreshold",help="If specified, this reprsents a DVARS threshold either in BOLD units, or if ending in a '%' character, as a percentage of mean global signal intensity (over the brain mask). Any volume contributing to a DVARS value greater than this threshold will be excluded (\"scrubbed\") from the (final) correlation step. DVARS calculation is performed on the results of the last pre-processing step, and is calculated as described by Power, J.D., et al., \"Spurious but systematic correlations in functional connectivity MRI networks arise from subject motion\", NeuroImage(2011). Note: data is only excluded during the final correlation, and so will never affect any operations that require the full signal, like regression, etc.", metavar="THRESH")
parser.add_option("--dvarsnumneighbors", action="store", type="int", dest="dvarsnumneighbors",help="If --dvarsthreshold is specified, then --dvarsnumnumneighbors specifies how many neighboring volumes, before and after the initially excluded volumes, should also be excluded. Default is 0.", metavar="NUMNEIGHBORS")
parser.add_option("--fdthreshold", action="store", type="float", dest="fdthreshold",help="If specified, this reprsents a FD threshold in mm. Any volume contributing to a FD value greater than this threshold will be excluded (\"scrubbed\") from the (final) correlation step. FD calculation is performed on the results of the last pre-processing step, and is calculated as described by Power, J.D., et al., \"Spurious but systematic correlations in functional connectivity MRI networks arise from subject motion\", NeuroImage(2011). Note: data is only excluded during the final correlation, and so will never affect any operations that require the full signal, like regression, etc.", metavar="THRESH")
parser.add_option("--fdnumneighbors", action="store", type="int", dest="fdnumneighbors",help="If --fdthreshold is specified, then --fdnumnumneighbors specifies how many neighboring volumes, before and after the initially excluded volumes, should also be excluded. Default is 0.", metavar="NUMNEIGHBORS")
parser.add_option("--motionthreshold", action="store", type="float", dest="motionthreshold",help="If specified, any volume whose motion parameters indicate a movement greater than this threshold (in mm) will be excluded (\"scrubbed\") from the (final) correlation step. Volume-to-volume movement is calculated per pair of neighboring volumes from the three rotational and three translational parameters generated by mcflirt. Motion for a pair of neighboring volumes is calculated as the maximum displacement (due to the combined rotation and translation) of any voxel on the 50mm-radius sphere surrounding the center of rotation. Note: data is only excluded during the final correlation, and so will never affect any operations that require the full signal, like regression, etc.", metavar="THRESH")
parser.add_option("--motionnumneighbors", action="store", type="int", dest="motionnumneighbors",help="If --motionthreshold is specified, then --motionnumnumneighbors specifies how many neighboring volumes, before and after the initially excluded volumes, should also be excluded. Default is 1.", metavar="NUMNEIGHBORS")
parser.add_option("--motionpar", action="store", type="string", dest="motionpar",help="If --motionthreshold is specified, then --motionpar specifies the .par file from which the motion parameters are extracted. If you allow this script to perform motion correction, then this option is ignored.", metavar="FILE.par")
parser.add_option("--scrubop", action="store", choices=('and', 'or'), dest="scrubop", help="If --motionthreshold, --dvarsthreshold, or --fdthreshold are specified, then --scrubop specifies the aggregation operator used to determine the final list of excluded volumes. Default is 'or', which means a volume will be excluded if *any* of its thresholds are exceeded, whereas 'and' means all the thresholds must be exceeded to be excluded.")
parser.add_option("--powerscrub", action="store_true", dest="powerscrub", help="Equivalent to specifying --fdthreshold=0.5 --fdnumneighbors=0 --dvarsthreshold=0.5% --dvarsnumneigbhors=0 --scrubop='and', to mimic the method used in the Power et al. article. Any conflicting options specified before or after this will override these.", default=False)
parser.add_option("--scrubkeepminvols", action="store", type="int", dest="scrubkeepminvols",help="If --motionthreshold, --dvarsthreshold, or --fdthreshold are specified, then --scrubminvols specifies the minimum number of volumes that should pass the threshold before doing any correlation. If the minimum is not met, then the script exits with an error. Default is to have no minimum.", metavar="NUMVOLS")
parser.add_option("--fcdmthresh", action="store", type="float", dest="fcdmthresh",help="R-value threshold to be used in functional connectivity density mapping ( step8 ). Default is set to 0.6. Algorithm from Tomasi et al, PNAS(2010), vol. 107, no. 21. Calculates the fcdm of functional data from last completed step, inside a dilated gray matter mask", metavar="THRESH", default=0.6)
parser.add_option("--cleanup", action="store_true", dest="cleanup",help="delete files from intermediate steps?")
print(("Command-line: " + " ".join([repr(x) for x in sys.argv])))
options, args = parser.parse_args()
if len(args) > 0:
sys.stderr.write("Too many arguments! Try --help.")
raise SystemExit()
if '-h' in sys.argv:
parser.print_help()
raise SystemExit()
if not (options.funcfile) or '-help' in sys.argv:
print("Input file ( --func ) is required to begin. Try --help ")
raise SystemExit()
class RestPipe:
def __init__(self):
self.initialize()
for i in self.steps:
logging.info('starting step' + i)
if i == '0':
self.step0()
elif i == '1':
self.step1()
elif i == '2':
self.step2()
elif i == '3':
self.step3()
elif i == '4':
self.step4()
elif i == '5':
self.step5()
elif i == '6':
self.step6()
elif i == '7a':
self.step7a()
elif i == '7b':
self.step7b()
elif i == '7':
self.step7()
elif i == '8':
self.step8()
if options.cleanup is not None:
self.cleanup()
def initialize(self):
#if all was defined, set those steps
if (options.steps == 'all'):
self.steps = ['0','1','2','3','4','5','6','7']
else:
#convert unicode str, push into obj
self.steps = options.steps.split(',')
for i in range(len(self.steps)):
self.steps[i] = str(self.steps[i])
self.needfunc = True
if len(self.steps) == 1 and '7b' in self.steps:
self.needfunc = False
#check bxh if provided
self.origbxh = None
self.thisnii = None
if options.funcfile is not None and self.needfunc:
if not ( os.path.isfile(options.funcfile)):
print(("File does not exist: " + options.funcfile))
raise SystemExit()
else:
self.origbxh = str(options.funcfile)
#t1
self.t1bxh = None
self.t1nii = None
if options.anatfile is not None:
if not ( os.path.isfile(options.anatfile)):
print(("File does not exist: " + options.anatfile))
raise SystemExit()
else:
fileExt = os.path.splitext(options.anatfile)[-1]
if fileExt == '.bxh':
self.t1bxh = str(options.anatfile)
elif fileExt == '.gz' or fileExt == '.nii':
self.t1nii = str(options.anatfile)
thisproc = subprocess.Popen(["fslwrapbxh " + self.t1nii],shell=True).wait()
extInd = None
if self.t1nii[-4:] == '.nii':
extInd = -4
elif self.t1nii[-7:] == '.nii.gz':
extInd = -7
else:
raise SystemExit("File does not have correct extension: %s" % (self.t1nii,))
self.t1bxh = self.t1nii[0:extInd] + '.bxh'
if not os.path.isfile(self.t1bxh):
raise SystemExit("Could not create .bxh file for %s" % (self.t1nii,))
if options.prefix is not None:
self.prefix = str(options.prefix)
else:
(funchead, functail) = os.path.split(str(options.funcfile))
if functail.endswith('.nii.gz'):
self.prefix = functail[0:-7]
self.thisnii = options.funcfile
elif functail.endswith('.nii'):
self.prefix = functail[0:-4]
self.thisnii = options.funcfile
else:
comps = functail.split('.')
if len(comps) == 1:
self.prefix = functail
else:
self.prefix = '.'.join(comps[0:-2])
#grab TR if it needs to be forced
self.tr_ms = options.tr_ms
#set a basedir where this file is located
self.basedir = re.sub('\/bin','',os.path.dirname(os.path.realpath(__file__)))
if options.flirtdof is not None:
self.flirtdof = str(options.flirtdof)
else:
self.flirtdof = str(12)
#reference image for normalization
if options.flirtref is not None:
for fname in [options.refwm, options.refcsf, options.flirtref, options.refbrainmask]:
if fname is not None:
if not ( os.path.isfile(fname) ):
print(("File does not exist: " + fname))
raise SystemExit()
else:
print("If using nonstandard reference, CSF and WM masks are required. Try --help")
raise SystemExit()
logging.info('Using ' + options.refac + ' for AC point/centroid calculation')
self.flirtref = str(options.flirtref)
self.refwm = str(options.refwm)
self.refcsf = str(options.refcsf)
self.refgm = str(options.refgm)
self.refac = str(options.refac)
self.refbrainmask = str(options.refbrainmask)
else:
self.flirtref = os.path.join(os.environ['FSLDIR'],'data','standard','MNI152_T1_2mm_brain.nii.gz')
# self.refwm = os.path.join(os.environ['FSLDIR'],'data','standard','MNI152_T1_2mm_brain_pve_2.nii.gz')
# self.refcsf = os.path.join(os.environ['FSLDIR'],'data','standard','MNI152_T1_2mm_brain_pve_0.nii.gz')
# self.refgm = os.path.join(os.environ['FSLDIR'],'data','standard','MNI152_T1_2mm_brain_pve_1.nii.gz')
self.refwm = os.path.join(self.basedir,'data','MNI152_T1_2mm_brain_pve_2.nii.gz')
self.refcsf = os.path.join(self.basedir,'data','MNI152_T1_2mm_brain_pve_0.nii.gz')
self.refgm = os.path.join(self.basedir,'data','MNI152_T1_2mm_brain_pve_1.nii.gz')
self.refac = str(options.refac)
self.refbrainmask = os.path.join(os.environ['FSLDIR'],'data','standard','MNI152_T1_2mm_brain_mask.nii.gz')
if ( '0' in self.steps ) and (self.origbxh is None) and ( self.thisnii is not None ):
if self.tr_ms is not None:
logging.info('requesting step0, but no bxh provided. Creating one from ' + self.thisnii )
thisproc = subprocess.Popen(["fslwrapbxh " + self.thisnii],shell=True).wait()
tmpfname = re.split('(\.nii$|\.nii\.gz$)',self.thisnii)[0] + ".bxh"
if os.path.isfile( tmpfname ):
self.origbxh = tmpfname
else:
logging.info("BXH creation failed")
raise SystemExit()
else:
logging.info("Please provide --tr option when starting from nifti, we don't trust TR derrived from existing nifti files.")
raise SystemExit()
#grab correlation label, or assign the AAL brain
if options.corrlabel is not None:
if options.corrtext is None:
raise SystemExit("If --corrlabel is specified, --corrtext must also be specified.")
if not ( os.path.isfile(options.corrlabel) ):
print(("File does not exist: " + options.corrlabel))
raise SystemExit()
elif not ( os.path.isfile(options.corrtext) ):
print(("File does not exist: " + options.corrtext))
raise SystemExit()
else:
self.corrlabel = str(options.corrlabel)
self.corrtext = str(options.corrtext)
else:
self.corrlabel = os.path.join(self.basedir,'data','aal_MNI_V4.nii')
self.corrtext = os.path.join(self.basedir,'data','aal_MNI_V4.txt')
# self.corrlabel = os.path.join('/usr','local','packages','MATLAB','WFU_PickAtlas_3.0.1','wfu_pickatlas','MNI_atlas_templates','aal_MNI_V4.nii')
# self.corrtext = os.path.join('/usr','local','packages','MATLAB','WFU_PickAtlas_3.0.1','wfu_pickatlas','MNI_atlas_templates','aal_MNI_V4.txt')
#a pre-defined flirt matrix for normalization
if options.flirtmat is not None:
if not ( os.path.isfile(options.flirtmat) ):
print(("File does not exist: " + options.flirtmat))
raise SystemExit()
else:
self.flirtmat = str(options.flirtmat)
else:
self.flirtmat = None
#grab low-pass filter input
self.lpfreq = options.lpfreq
#f value to use in bet for skull stripping
self.betfval = options.betfval
self.anatbetfval = options.anatbetfval
if options.sliceorder:
self.sliceorder = options.sliceorder
self.dosliceorder = True
else:
self.dosliceorder = False
self.throwaway = options.throwaway
self.scrubop = 'or'
self.dvarsthreshold = None
self.dvarsnumneighbors = 0
self.fdthreshold = None
self.fdnumneighbors = 0
self.motionthreshold = None
self.motionnumneighbors = 1
self.fcdmthresh = 0.6
if options.powerscrub:
# these override any previously set options
self.scrubop = 'and'
self.dvarsthreshold = '0.5%'
self.dvarsnumneighbors = 0
self.fdthreshold = 0.5
self.fdnumneighbors = 0
if options.scrubop is not None:
self.scrubop = options.scrubop
if options.dvarsthreshold is not None:
self.dvarsthreshold = options.dvarsthreshold
if self.dvarsthreshold is not None:
checkval = self.dvarsthreshold
if checkval[-1] == '%':
checkval = checkval[0:-1]
try:
_ = float(checkval)
except:
logging.error("--dvarsthreshold must be a floating-point number (optionally followed by '%')")
raise SystemExit()
if options.dvarsnumneighbors is not None:
self.dvarsnumneighbors = options.dvarsnumneighbors
if options.fdthreshold is not None:
self.fdthreshold = options.fdthreshold
if options.fdnumneighbors is not None:
self.fdnumneighbors = options.fdnumneighbors
if options.motionthreshold is not None:
self.motionthreshold = options.motionthreshold
if options.motionnumneighbors is not None:
self.motionnumneighbors = options.motionnumneighbors
self.scrubkeepminvols = options.scrubkeepminvols
self.mcparams = options.motionpar
if self.motionthreshold != None:
if '2' not in self.steps:
if options.motionpar == None:
logging.info("--motionpar option is required when using --motionthreshold if you are skipping the motion correction step (step 2).")
raise SystemExit()
self.mcparams = options.motionpar
if options.fcdmthresh is not None:
self.fcdmthresh = float(options.fcdmthresh)
#array for files to delete later
self.toclean = []
self.slicefile = None
self.doslicetiming = False
#self.sliceorder = None
self.xdim = None
self.ydim = None
self.zdim = None
self.tdim = None
#self.thisnii = None
self.prevprefix = None
#last preflight check for all potentially required files
#if these aren't defined by options they get default values
for fname in [self.flirtref, self.refwm, self.refcsf, self.corrlabel]:
if not os.path.isfile(fname):
print(("File does not exist: " + fname))
raise SystemExit()
#place to put temp stuff
if ( os.getenv('TMPDIR') ):
self.tmpdir = os.getenv('TMPDIR')
else:
self.tmpdir = '/tmp'
#parse the bxh to get some values
if self.origbxh is not None:
#first try to get slicetiming
try:
tempst = os.path.join(self.tmpdir,'slicetiming.txt')
popenobj = subprocess.Popen(['bxh_slicetiming','--fsl',self.origbxh,tempst], stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = popenobj.communicate()
lines = stdoutdata.splitlines()
if os.path.isfile(tempst):
self.slicefile = os.path.abspath(tempst)
self.doslicetiming = True
except Exception as e:
logging.error("could not produce slicetiming, will try slice order")
popenobj = subprocess.Popen(['dumpheader', self.origbxh], stdout=subprocess.PIPE)
(stdoutdata, stderrdata) = popenobj.communicate()
lines = stdoutdata.splitlines()
for line in lines:
willrotate = False
#grab the tuple version
mobj = re.search("Dimension.*\((\w)\):\s*(\(.*\))(\S+) to (\(.*\))(\S+), ([0-9]+) steps(, direction \(([-0-9.]+), ([-0-9.]+), ([-0-9.]+)\))?",line.decode('utf-8'))
if mobj is None:
#grab the non-tuple version for T-dim
mobj = re.search("Dimension.*\((\w)\):\s*([-0-9.]+)(\S+) to ([-0-9.]+)(\S+), ([0-9]+) steps(, direction)",line.decode('utf-8'))
# mobj = re.search("Dimension.*\((\w)\):\s*([-0-9.]+)(\S+) to ([-0-9.]+)(\S+), ([0-9]+) steps(, direction \(([-0-9.]+), ([-0-9.]+), ([-0-9.]+)\))?", line.decode('utf-8'))
if mobj:
(dimname, firstpos, firstunits, lastpos, lastunits, numsteps, dirclause, dirR, dirA, dirS) = mobj.groups() + tuple([None] * (10 - len(mobj.groups())))
# (dimname, firstpos, firstunits, lastpos, lastunits, numsteps, dirclause, dirR, dirA, dirS) = mobj.groups()
if '0' in self.steps and '1' in self.steps and (dimname == 'x' or dimname == 'y' or dimname == 'z') and dirR != None and dirA != None and dirS != None:
# reorientation step (0) will reorient to LAS.
# if we are doing slice timing correction (1),
# make sure reorientation will not change the
# slice dimension, since slicetimer does not
# work on any dimension other than the third.
dirR = float(dirR)
dirA = float(dirA)
dirS = float(dirS)
dirmax = dirR
if abs(dirA) > abs(dirmax): dirmax = dirA
if abs(dirS) > abs(dirmax): dirmax = dirS
if ((dimname == 'x' and dirR != dirmax) or
(dimname == 'y' and dirA != dirmax) or
(dimname == 'z' and dirS != dirmax)):
sys.stderr.write("ERROR: reorientation will change slice dimension and so slice timing correction will not work!\n")
raise SystemExit()
if dimname == 'x':
self.xdim = int(numsteps)
elif dimname == 'y':
self.ydim = int(numsteps)
elif dimname == 'z':
self.zdim = int(numsteps)
elif dimname == 't':
self.tdim = int(numsteps)
if self.tr_ms is None:
self.tr_ms = (float(lastpos) - float(firstpos)) / self.tdim
if firstunits == 'ms':
pass
elif firstunits == 's':
self.tr_ms *= 1000.0
elif firstunits == 'us':
self.tr_ms /= 1000.0
else:
sys.stderr.write("Unexpected temporal units in image header: '%s'" % firstunits)
raise SystemExit()
mobj = re.search("acqdata: sliceorder = (.*)", line.decode('utf-8'))
if mobj:
(self.sliceorder,) = mobj.groups()
self.dosliceorder = True
mobj = re.search(" Filename: (.*\.nii(\.gz)?)", line.decode('utf-8'))
if mobj:
fname = mobj.group(1)
testpath = os.path.join( '/'.join(self.origbxh.split('/')[0:-1]), fname)
#testpath = fname;
if os.path.isfile(testpath):
self.thisnii = testpath
elif os.path.isfile(testpath + '.gz'):
self.thisnii = testpath + '.gz'
elif '0' in self.steps:
self.thisnii = None
else:
print(("Please provide a BXH that points to a NIFTI file: " + self.origbxh))
raise SystemExit()
else:
if (self.thisnii is not None) and ( self.tr_ms is not None ):
thishdr = nibabel.load(self.thisnii).header
thisshape = thishdr.get_data_shape()
if len(thisshape) == 4:
self.xdim = thisshape[0]
self.ydim = thisshape[1]
self.zdim = thisshape[2]
self.tdim = thisshape[3]
else:
logging.info("Functional data has incorrect dimensions. Expected 4D, received : " + str(len(thisshape)) + " D")
raise SystemExit()
elif ( [ step for step in ['0', '1', '5', '6'] if step in self.steps ] ):
logging.info("Please provide --tr option when starting from nifti, we don't trust TR derrived from existing nifti files.")
raise SystemExit()
#make output directory
if (options.outpath == 'PWD'):
self.outpath = os.environ['PWD']
else:
self.outpath = str(options.outpath)
if not ( os.path.exists(self.outpath) ):
os.mkdir( self.outpath )
#if they are skipping 0, make sure there's NII data
if '0' not in self.steps and self.needfunc:
if self.thisnii is None:
newfile = os.path.join(self.outpath,self.prefix)
#thisprocstr = str("bxh2analyze --overwrite --niigz -s " + self.origbxh + " " + newfile)
thisprocstr = str("bxhselect --overwrite " + self.origbxh + " " + newfile + ".bxh")
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(newfile + ".nii.gz"):
self.thisnii = newfile + ".nii.gz"
if self.t1nii is None and self.t1bxh is not None:
fileName = self.t1bxh.split('/')[-1].split('.')[0]
newfile = os.path.join(self.outpath,fileName)
#thisprocstr = str("bxh2analyze --overwrite --niigz -s " + self.t1bxh + " " + newfile)
thisprocstr = str("bxhselect --overwrite " + self.t1bxh + " " + newfile + ".bxh")
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(newfile + ".nii.gz"):
self.t1nii = newfile + ".nii.gz"
if os.path.isfile(newfile + ".bxh"):
self.t1bxh = newfile + ".bxh"
#try to determine sliceorder if step1
if '1' in self.steps:
if self.doslicetiming and self.slicefile:
copyfile(self.slicefile,os.path.join(self.outpath,'slicetiming.txt'))
self.slicefile = os.path.join(self.outpath,'slicetiming.txt')
logging.info("using slicetiming.txt")
elif self.dosliceorder and re.search('\d+\,+',str(self.sliceorder)):
slicefile = os.path.join(self.outpath,'sliceorder.txt')
f = open(slicefile, 'w')
for i in self.sliceorder.split(','):
f.write(i)
f.write("\n")
f.close()
self.slicefile = slicefile
elif self.dosliceorder and options.sliceorder:
#try to generate a slicefile if it wasn't in BXH
if (re.search('(odd|even|up|down)',options.sliceorder)) and (self.slicefile is None):
self.sliceorder = options.sliceorder
if self.zdim is not None:
slicefile = os.path.join(self.outpath,'sliceorder.txt')
f = open(slicefile, 'w')
if self.sliceorder == 'up': #bottomup
thisrang = list(range(1,self.zdim + 1))
for i in thisrang:
f.write(str(i))
f.write("\n")
f.close()
self.slicefile = slicefile
elif self.sliceorder == 'down': #topdown
thisrang = list(range(1,self.zdim + 1))
thisrang.reverse() #flip it
for i in thisrang:
f.write(str(i))
f.write("\n")
f.close()
self.slicefile = slicefile
elif re.search('(odd|even)',self.sliceorder): #interleaved
odds = list(range(1,self.zdim+1,2))
evens = list(range(2,self.zdim+1,2))
if self.sliceorder == 'odd': #odds first
for i in odds:
f.write(str(i))
f.write("\n")
for i in evens:
f.write(str(i))
f.write("\n")
elif self.sliceorder == 'even': #evens first
for i in evens:
f.write(str(i))
f.write("\n")
for i in odds:
f.write(str(i))
f.write("\n")
f.close()
self.slicefile = slicefile
else:
logging.info("z dimension could not be found.")
raise SystemExit()
else:
logging.info("sliceorder is incorrectly defined. use odd/even/up/down.")
raise SystemExit()
else:
logging.info("slice order not found. please use --sliceorder option")
raise SystemExit()
# If running step 7b by itself, check corrts now
self.corrts = None
if len(self.steps) == 1 and '7b' in self.steps:
# running step 7b by itself. See if --corrts is specified or
# otherwise look for default parcellation output file
if options.corrts is not None:
if not os.path.isfile(options.corrts):
print(("File does not exist: " + options.corrts))
raise SystemExit()
self.corrts = options.corrts
else:
corrtsfile = os.path.join(self.outpath,'corrlabel_ts.txt')
if not os.path.isfile(corrtsfile):
print(("You are running step 7b by itself, but can't find default input file '%s'. Please specify an alternate file with --corrts." % (corrtsfile,)))
raise SystemExit()
#get the labels from the text file
def grab_labels(self):
mylabs = open(self.corrtext,'r').readlines()
labs = []
for line in mylabs:
if len(line.split('\t')) == 2:
splitstuff = line.split('\t')
splitstuff[0] = int(splitstuff[0])
splitstuff[-1] = splitstuff[-1].strip()
labs.append(splitstuff)
return labs
#step0 is the initial LAS conversion and nifti creation
def step0(self):
logging.info('converting functional data')
tempfile = os.path.join(self.tmpdir,''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(10)) + '.bxh')
thisprocstr = str("bxhreorient --orientation=LAS " + self.origbxh + " " + tempfile)
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if self.throwaway is not None:
logging.info('disregarding acquisitions')
thisprocstr = str("bxhselect --overwrite --timeselect " + str(self.throwaway) + ": " + tempfile + " " + tempfile)
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
self.tdim = self.tdim - self.throwaway
if os.path.isfile(tempfile):
newprefix = self.prefix + "_LAS"
newfile = os.path.join(self.outpath,newprefix)
#thisprocstr = str("bxh2analyze --overwrite --niigz -s " + tempfile + " " + newfile)
thisprocstr = str("bxhselect --overwrite " + tempfile + " " + newfile + ".bxh")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(newfile + ".nii.gz"):
self.thisnii = newfile + ".nii.gz"
self.prevprefix = self.prefix
self.prefix = newprefix
else:
logging.info('conversion failed')
raise SystemExit()
else:
logging.info('orientation change failed')
raise SystemExit()
if self.t1bxh is not None or self.t1nii is not None:
logging.info('converting anatomical data')
newprefix = "t1_LAS"
newfile = os.path.join(self.outpath,newprefix)
thisprocstr = str("bxhreorient --orientation=LAS " + self.t1bxh + " " + newfile + ".bxh")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(newfile + ".nii.gz"):
self.t1nii = newfile + ".nii.gz"
else:
logging.info('anatomical conversion failed')
raise SystemExit()
#slice time correction
def step1(self):
logging.info('slice time correcting data')
newprefix = self.prefix + '_st'
newfile = os.path.join(self.outpath,newprefix)
#setup the type of file for slicetimer
fslst_type = None
if self.dosliceorder and self.slicefile:
fslst_type = " --ocustom="
elif self.doslicetiming and self.slicefile:
fslst_type = " --tcustom="
else:
logging.error('slice time correction does not have valid input')
raise SystemExit()
thisprocstr = str("slicetimer -i " + self.thisnii + " -o " + newfile + " -r " + str(self.tr_ms/1000) + fslst_type + self.slicefile)
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(newfile + ".nii.gz"):
if self.prevprefix is not None:
self.toclean.append( self.thisnii )
self.thisnii = newfile + ".nii.gz"
self.prevprefix = self.prefix
self.prefix = newprefix
logging.info('slice time correction successful')
else:
logging.info('slice time correction failed')
raise SystemExit()
#run motion correction
def step2(self):
logging.info('motion correcting correcting data')
newprefix = self.prefix + '_mcf'
newfile = os.path.join(self.outpath,newprefix)
thisprocstr = str("mcflirt -in " + self.thisnii + " -o " + newfile + " -plots")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(newfile + ".nii.gz") and os.path.isfile(newfile + ".par"):
if self.prevprefix is not None:
self.toclean.append( self.thisnii )
self.thisnii = newfile + ".nii.gz"
self.prevprefix = self.prefix
self.prefix = newprefix
self.mcparams = newfile + ".par"
logging.info('motion correction successful: ' + self.thisnii )
thisprocstr = str("fsl_tsplot -i " + self.mcparams + " -t 'MCFLIRT estimated rotations (radians)' -u 1 --start=1 --finish=3 -a x,y,z -w 640 -h 144 -o " + newfile + "_rot.png")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
thisprocstr = str("fsl_tsplot -i " + self.mcparams + " -t 'MCFLIRT estimated translations (mm)' -u 1 --start=4 --finish=6 -a x,y,z -w 640 -h 144 -o " + newfile + "_trans.png")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
logging.info('regressing out motion correction parameters')
#load mcflirt params
params = np.loadtxt(self.mcparams,unpack=True)
#load nifti data
data = nibabel.nifti1.load(self.thisnii)
data1 = data.get_fdata()
#create regressors
X = []
for index in range(6):
X.append(np.vstack([np.ones(self.tdim), params[index]]).T)
logging.info('starting linear regression')
tmp_mean = np.mean(data1, axis=3, dtype=data1.dtype)
shape = data1.shape
data1v = data1.reshape((shape[0]*shape[1], shape[2], shape[3])).transpose((1, 2, 0))
# data1v is a view in z, t, x*y order
# go slice-by-slice
for cntz in range(self.zdim):
tmp_data = data1v[cntz]
for index in range(6):
p0 = np.linalg.lstsq(X[index], tmp_data, rcond=-1)[0]
p00 = np.dot(X[index], p0) #product
tmp_data = tmp_data - p00
data1v[cntz] = tmp_data
data_mr = data1v.transpose((2, 0, 1)).reshape(shape)
del data1v
del data1
# in-place (-=, *=) operations should save memory
data_mr += tmp_mean.reshape(tmp_mean.shape + (1,))
data_mr -= np.min(data_mr)
data_mr *= (30000.0 / np.max(data_mr)).astype(data.get_data_dtype())
newNii = nibabel.Nifti1Pair(data_mr,None,data.header)
newprefix = self.prefix + 'r'
newfile = os.path.join(self.outpath, (newprefix + ".nii.gz"))
nibabel.save(newNii,newfile)
if os.path.isfile(newfile):
if self.prevprefix is not None:
self.toclean.append( self.thisnii )
self.prevprefix = self.prefix
self.prefix = newprefix
self.thisnii = newfile
logging.info('regression completed: ' + self.thisnii )
else:
logging.info('regression failed')
raise SystemExit()
else:
logging.info('motion correction failed')
raise SystemExit()
#skull strip the functional
def step3(self):
logging.info('skull stripping data')
newprefix = self.prefix + "_brain"
newfile = os.path.join(self.outpath, newprefix)
#first create mean_func
thisprocstr = str("fslmaths " + self.thisnii + " -Tmean " + os.path.join(self.outpath,'mean_func') )
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
#now skull strip the mean
thisprocstr = "bet " + os.path.join(self.outpath,'mean_func') + " " + os.path.join(self.outpath,'mean_func_brain') + " -f " + str(self.betfval) + " -m"
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
#now mask full run by results
thisprocstr = str("fslmaths " + self.thisnii + " -mas " + os.path.join(self.outpath,'mean_func_brain_mask') + " " + newfile)
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile( newfile + ".nii.gz" ):
if self.prevprefix is not None:
self.toclean.append( self.thisnii )
self.toclean.append( os.path.join(self.outpath,'mean_func.nii.gz') )
self.thisnii = newfile + ".nii.gz"
self.prevprefix = self.prefix
self.prefix = newprefix
logging.info('skull stripping completed: ' + self.thisnii )
else:
logging.info('skull stripping failed')
raise SystemExit()
#skull strip anat
if self.t1nii is not None:
logging.info('skull stripping anat')
newprefix = self.t1nii.split('/')[-1].split('.')[0] + "_brain"
newfile = os.path.join(self.outpath, newprefix)
thisprocstr = str("bet " + self.t1nii + " " + newfile + " -f " + str(self.anatbetfval))
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile( newfile + ".nii.gz" ):
self.t1nii = newfile + ".nii.gz"
logging.info('skull stripping completed: ' + self.t1nii )
else:
logging.info('skull stripping anatomical failed')
raise SystemExit()
#normalize the data
def step4(self):
logging.info('normalizing data')
newprefix = self.prefix + "_norm"
newfile = os.path.join(self.outpath, newprefix)
if self.flirtmat is not None:
#apply the flirt matrix
logging.info('applying transformation matrix ' + self.flirtmat + ' to 4D data')
thisprocstr = str("flirt -in " + self.thisnii + " -ref " + self.flirtref + " -applyxfm -init " + self.flirtmat + " -out " + newfile )
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
elif self.t1nii is not None:
#use t1 to generate flirt paramters
#first flirt the func to the t1
logging.info('flirt func to t1')
thisprocstr = str("flirt -ref " + self.t1nii + " -in " + self.thisnii + " -out " + os.path.join(self.outpath,'func2t1') + " -omat " + os.path.join(self.outpath,'func2t1.mat') + " -cost corratio -dof 6 -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -interp trilinear")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
self.toclean.append( os.path.join(self.outpath,'func2t1.nii.gz') )
#invert the mat
logging.info('inverting func2t1.mat')
thisprocstr = str("convert_xfm -inverse -omat " + os.path.join(self.outpath,'t12func.mat') + " " + os.path.join(self.outpath,'func2t1.mat') )
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
#flirt the t1 to standard
logging.info('flirt t1 to standard')
thisprocstr = str("flirt -ref " + self.flirtref + " -in " + self.t1nii + " -out " + os.path.join(self.outpath,'t12standard') + " -omat " + os.path.join(self.outpath,'t12standard.mat') + " -cost corratio -dof " + self.flirtdof + " -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -interp trilinear")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile(os.path.join(self.outpath,('t12standard' + '.nii.gz'))):
self.t1nii = os.path.join(self.outpath,('t12standard' + '.nii.gz'))
else:
logging.info('t1 normalization failed.')
raise SystemExit()
#invert the mat
logging.info('inverting t12standard.mat')
thisprocstr = str("convert_xfm -inverse -omat " + os.path.join(self.outpath,'standard2t1.mat') + " " + os.path.join(self.outpath,'t12standard.mat'))
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
#compute the func2standard mat
logging.info('computing func2standard.mat from t12standard.mat func2t1.mat')
thisprocstr = str("convert_xfm -omat " + os.path.join(self.outpath,'func2standard.mat') + " -concat " + os.path.join(self.outpath,'t12standard.mat') + " " + os.path.join(self.outpath,'func2t1.mat'))
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
#apply the transform
logging.info('creating normalized func %s' % (newprefix))
thisprocstr = str("flirt -ref " + self.flirtref + " -in " + self.thisnii + " -out " + newfile + " -applyxfm -init " + os.path.join(self.outpath,'func2standard.mat') + " -interp trilinear")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
else:
#use the functional to get the matrix
thisprocstr = str("flirt -in " + self.thisnii + " -ref " + self.flirtref + " -out " + newfile + " -omat " + (newfile + '.mat') + " -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12 -interp trilinear")
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
if os.path.isfile( newfile + '.mat' ):
#then apply output matrix to the same data with the same output name. for some reason flirt doesn't output 4D data above
logging.info('applying transformation matrix to 4D data')
thisprocstr = str("flirt -in " + self.thisnii + " -ref " + self.flirtref + " -applyxfm -init " + (newfile + '.mat') + " -out " + newfile )
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
else:
logging.info('creation if initial flirt matrix failed.')
raise SystemExit()
if os.path.isfile( newfile + '.nii.gz' ):
if self.prevprefix is not None:
self.toclean.append( self.thisnii )
self.thisnii = newfile + '.nii.gz'
logging.info('initial normalization successful: ' + self.thisnii )
self.prevprefix = self.prefix
self.prefix = newprefix
normshape = nibabel.nifti1.load(self.thisnii).shape
if len(normshape) == 4:
self.xdim = normshape[0]
self.ydim = normshape[1]
self.zdim = normshape[2]
self.tdim = normshape[3]
else:
logging.info('normalized data has wrong shape, expecting 4D, received: ' + str(len(normshape)) + 'D')
raise SystemExit()
else:
logging.info('normalization failed.')
raise SystemExit()
#regress out WM/CSF
def step5(self):
logging.info('regressing out WM/CSF signal ')
newprefix = self.prefix + '_wmcsf'
newfile = os.path.join(self.outpath,(newprefix + ".nii.gz"))
#load nifti data
data = nibabel.nifti1.load(self.thisnii)
data1 = data.get_fdata()
#mean time series for wm
wmout = os.path.join(self.outpath,"wm_ts.txt")
thisprocstr = str("fslmeants -i " + self.thisnii + " -m " + self.refwm + " -o " + wmout )
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
#mean time series for csf
csfout = os.path.join(self.outpath,"csf_ts.txt")
thisprocstr = str("fslmeants -i " + self.thisnii + " -m " + self.refcsf + " -o " + csfout )
logging.info('running: ' + thisprocstr)
subprocess.Popen(thisprocstr,shell=True).wait()
for fname in [wmout, csfout]:
if not os.path.isfile(fname):
logging.info('could not extract timeseries, quitting: ' + fname)
raise SystemExit()
wm_ts = np.loadtxt(wmout,unpack=True)
csf_ts = np.loadtxt(csfout,unpack=True)
X_wm = np.vstack([np.ones(self.tdim), wm_ts]).T
X_csf = np.vstack([np.ones(self.tdim), csf_ts]).T
logging.info('starting linear regression')
tmp_mean = np.mean(data1, axis=3, dtype=data1.dtype)
shape = data1.shape
data1v = data1.reshape((shape[0]*shape[1], shape[2], shape[3])).transpose((1, 2, 0))
# data1v is a view in z, t, x*y order
# go slice-by-slice
for cntz in range(self.zdim):
tmp_data = data1v[cntz]
# regress wm
p01 = np.linalg.lstsq(X_wm, tmp_data, rcond=-1)[0]
p001 = np.dot(X_wm, p01) #product
tmp02 = tmp_data - p001
# regress csf
p02 = np.linalg.lstsq(X_csf, tmp02, rcond=-1)[0]
p002 = np.dot(X_csf, p02) #product
tmp03 = tmp02 - p002
data1v[cntz] = tmp03
data_mr = data1v.transpose((2, 0, 1)).reshape(shape)
del data1v
del data1
data_mr += tmp_mean.reshape(tmp_mean.shape + (1,))
data_mr -= np.min(data_mr)
data_mr *= (30000.0 / np.max(data_mr)).astype(data.get_data_dtype())
newNii = nibabel.Nifti1Pair(data_mr,None,data.header)
nibabel.save(newNii,newfile)
if os.path.isfile(newfile):
if self.prevprefix is not None:
self.toclean.append(self.thisnii)
self.prevprefix = self.prefix
self.prefix = newprefix
self.thisnii = newfile
logging.info('WM/CSF regression successful: ' + self.thisnii )
else:
logging.info('WM/CSF regression failed')
raise SystemExit()
#lowpass filter
def step6(self):
logging.info('lowpass filtering data')
newprefix = "filt_" + self.prefix
newfile = os.path.join(self.outpath,(newprefix + ".nii.gz"))
freq_cutoff = self.lpfreq
#load nifti data
data = nibabel.nifti1.load(self.thisnii)
data1 = data.get_fdata()
#build filter
time_all = np.arange(0,(self.tdim*(self.tr_ms/1000))-.001,.001)
time_subTR = time_all[0:-1:int(self.tr_ms)]
length = len(time_subTR)
ccc = 1.0/(self.tr_ms/1000)/length