9
9
from executors .models import Job
10
10
from datetime import datetime
11
11
from pathlib import Path
12
+ import nibabel as nib
13
+ import numpy as np
12
14
13
15
date = datetime .today ().strftime ('%Y-%m-%d' )
14
16
@@ -169,6 +171,8 @@ def run_eddy_quad(self, eddy_quad_dir):
169
171
shutil .copy (new_bvec , eddy_quad_dir )
170
172
os .rename (f'{ eddy_quad_dir } /{ os .path .basename (new_bvec )} ' , f'{ eddy_quad_dir } /{ self ._sub } _{ self ._ses } .eddy_rotated_bvecs' )
171
173
174
+ self .verify_bval_nii_match (eddy_quad_dir )
175
+
172
176
logging .info ('Running eddy_quad for the 2nd time...' )
173
177
proc1 = subprocess .Popen (command , shell = True , stdout = subprocess .PIPE )
174
178
proc1 .communicate ()
@@ -186,6 +190,33 @@ def match_bvec(self, directory):
186
190
if file .endswith ('.bvec' ) and str (self ._run ) in file :
187
191
return Path (directory , file )
188
192
193
+ def verify_bval_nii_match (self , eddy_quad_dir ):
194
+ bval_file_path = Path (eddy_quad_dir , self .match_bval (eddy_quad_dir ))
195
+ nii_file_path = Path (eddy_quad_dir , f'{ self ._sub } _{ self ._ses } .nii.gz' )
196
+
197
+ bvals = np .genfromtxt (bvalsFile , dtype = float )
198
+ nii_file = nib .load (nii_file_path )
199
+
200
+ if nii_file .shape [3 ] != np .max (bvals .shape ):
201
+ new_bvals = self .adjust_bvals_shape (bvals , nii_file )
202
+ np .savetxt (f'{ eddy_quad_dir } /{ self ._sub } .bval' , new_bvals , fmt = '%d' )
203
+ return
204
+ else :
205
+ return
206
+
207
+ def adjust_bvals_shape (self , bvals , nii_file ):
208
+
209
+ target_shape = nii_file .shape [3 ]
210
+ current_shape = np .max (bvals .shape )
211
+
212
+ if current_shape < target_shape :
213
+ additional_zeros = np .zeros (target_shape - current_shape )
214
+ bvals = np .append (bvals , additional_zeros )
215
+ elif current_shape > target_shape :
216
+ bvals = bvals [:target_shape ]
217
+
218
+ return bvals
219
+
189
220
190
221
def parse_json (self , eddy_dir ):
191
222
logging .info ('parsing qc.json file.' )
0 commit comments