Skip to content

Commit 9746fe6

Browse files
committed
Only delete .bvec/.bval files if all bvals are (near) zero.
Print out a warning if they are saved in destination folder if it is not `dwi`
1 parent 9784a81 commit 9746fe6

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

heudiconv/convert.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,17 @@ def save_converted_files(res, item_dicoms, bids_options, outtype, prefix, outnam
679679
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
680680
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
681681
else:
682-
os.remove(res.outputs.bvecs)
683-
os.remove(res.outputs.bvals)
684-
lgr.debug("%s and %s were removed since not dwi", res.outputs.bvecs, res.outputs.bvals)
682+
if bvals_are_zero(res.outputs.bvals):
683+
os.remove(res.outputs.bvecs)
684+
os.remove(res.outputs.bvals)
685+
lgr.debug("%s and %s were removed since not dwi", res.outputs.bvecs, res.outputs.bvals)
686+
else:
687+
lgr.info("Diffusion-weighted image saved in non dwi folder (%s)", prefix_dirname)
688+
lgr.info(".bvec and .bval files will be generated. This is NOT BIDS compliant")
689+
outname_bvecs, outname_bvals = prefix + '.bvec', prefix + '.bval'
690+
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
691+
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
692+
685693

686694
if isinstance(res_files, list):
687695
res_files = sorted(res_files)
@@ -811,3 +819,23 @@ def add_taskname_to_infofile(infofiles):
811819

812820
# write to outfile
813821
save_json(infofile, meta_info)
822+
823+
824+
def bvals_are_zero(bval_file, threshold=5):
825+
"""Checks if all entries in a bvals file are zero (or below the threshold).
826+
Returns True if that is the case, otherwise returns False
827+
828+
Parameters
829+
----------
830+
bval_file : file with the bvals
831+
threshold : b-value below which they are considered to be zero. (Default: 5)
832+
833+
Returns
834+
-------
835+
True if all are zero; False otherwise.
836+
"""
837+
838+
with open(bval_file) as f:
839+
bvals = f.read().split()
840+
841+
return all(float(b) <= threshold for b in bvals)

0 commit comments

Comments
 (0)