Skip to content

Commit c6b4a68

Browse files
committed
verify bval size
1 parent ebd5ece commit c6b4a68

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

dwiqc/tasks/qsiprep_EQ.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from executors.models import Job
1010
from datetime import datetime
1111
from pathlib import Path
12+
import nibabel as nib
13+
import numpy as np
1214

1315
date = datetime.today().strftime('%Y-%m-%d')
1416

@@ -169,6 +171,8 @@ def run_eddy_quad(self, eddy_quad_dir):
169171
shutil.copy(new_bvec, eddy_quad_dir)
170172
os.rename(f'{eddy_quad_dir}/{os.path.basename(new_bvec)}', f'{eddy_quad_dir}/{self._sub}_{self._ses}.eddy_rotated_bvecs')
171173

174+
self.verify_bval_nii_match(eddy_quad_dir)
175+
172176
logging.info('Running eddy_quad for the 2nd time...')
173177
proc1 = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
174178
proc1.communicate()
@@ -186,6 +190,33 @@ def match_bvec(self, directory):
186190
if file.endswith('.bvec') and str(self._run) in file:
187191
return Path(directory, file)
188192

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+
189220

190221
def parse_json(self, eddy_dir):
191222
logging.info('parsing qc.json file.')

0 commit comments

Comments
 (0)