@@ -69,8 +69,12 @@ def create_bfiles(self, inputs_dir):
69
69
if not fmap_files :
70
70
self ._layout .get (subject = self ._sub , session = self ._ses , suffix = 'epi' , extension = '.nii' , return_type = 'filename' )
71
71
72
+ # truncate down the fmap files to include just b0 volumes
73
+
74
+ truncated_fmaps = self .truncate_fmaps (fmap_files )
75
+
72
76
# get the basename of the file and then remove the extension
73
- for fmap in fmap_files :
77
+ for fmap in truncated_fmaps :
74
78
75
79
basename = os .path .basename (fmap )
76
80
@@ -116,6 +120,56 @@ def create_bfiles(self, inputs_dir):
116
120
# this method serves to create the accompanying spec file for prequal
117
121
# the contents of the file depends on the SeriesDescription stored in the metadata
118
122
123
+ def truncate_fmaps (self , fmap_files ):
124
+ """
125
+ method that will extract all the b0 volumes from the scans designated as BIDS fieldmaps
126
+ """
127
+ self .get_fsl_sif ()
128
+
129
+ truncated_fmaps = []
130
+
131
+ for fmap in fmap_files :
132
+ fmap_basename = os .path .basename (fmap )
133
+ fmap_dir = os .path .dirname (fmap )
134
+ cmd = [
135
+ 'singularity' ,
136
+ 'exec' ,
137
+ '--pwd' , fmap_dir ,
138
+ self ._fsl_sif ,
139
+ '/APPS/fsl/bin/fslselectvols' ,
140
+ '-i' , fmap_basename ,
141
+ '-o' , fmap_basename ,
142
+ '--vols=0'
143
+ ]
144
+ #logger.info(f'running {json.dumps(cmd, indent=2)} on {fmap}')
145
+
146
+ cmdline = subprocess .list2cmdline (cmd )
147
+ logger .info (f'running { cmdline } ' )
148
+ proc = subprocess .Popen (cmdline , shell = True , stdout = subprocess .PIPE )
149
+ proc .communicate ()
150
+ if proc .returncode > 0 :
151
+ logger .critical (f'fslselectvols command failed' )
152
+ raise subprocess .CalledProcessError (returncode = proc .returncode , cmd = cmdline )
153
+
154
+ truncated_fmaps .append (fmap_dir + f'/{ fmap_basename } ' )
155
+
156
+
157
+ return truncated_fmaps
158
+
159
+
160
+ def get_fsl_sif (self ):
161
+
162
+ if self ._container_dir :
163
+ try :
164
+ self ._fsl_sif = f'{ self ._container_dir } /fsl_6.0.4.sif'
165
+ except FileNotFoundError :
166
+ logger .error (f'{ self ._container_dir } /fsl_6.0.4.sif does not exist. Verify the path and file name.' )
167
+ sys .exit (1 )
168
+ else :
169
+ home_dir = os .path .expanduser ("~" )
170
+ self ._fsl_sif = os .path .join (home_dir , '.config/dwiqc/containers/fsl_6.0.4.sif' )
171
+
172
+
119
173
def create_spec (self , inputs_dir ):
120
174
dwi_file = self ._layout .get (subject = self ._sub , session = self ._ses , run = self ._run , suffix = 'dwi' , extension = '.nii.gz' )
121
175
#dwi_file = layout.get()[10]
0 commit comments