Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

properly remove GE multiecho bvals/bvecs #728

Merged
merged 5 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import filelock
from nipype import Node
from nipype.interfaces.base import TraitListObject

from .bids import (
BIDS_VERSION,
Expand Down Expand Up @@ -880,19 +881,19 @@
return []

if isdefined(res.outputs.bvecs) and isdefined(res.outputs.bvals):
bvals, bvecs = res.outputs.bvals, res.outputs.bvecs
bvals = list(bvals) if isinstance(bvals, TraitListObject) else bvals
bvecs = list(bvecs) if isinstance(bvecs, TraitListObject) else bvecs
if prefix_dirname.endswith("dwi"):
outname_bvecs, outname_bvals = prefix + ".bvec", prefix + ".bval"
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
safe_movefile(bvecs, outname_bvecs, overwrite)
safe_movefile(bvals, outname_bvals, overwrite)
else:
if bvals_are_zero(res.outputs.bvals):
os.remove(res.outputs.bvecs)
os.remove(res.outputs.bvals)
lgr.debug(
"%s and %s were removed since not dwi",
res.outputs.bvecs,
res.outputs.bvals,
)
if bvals_are_zero(bvals):
to_remove = bvals + bvecs if isinstance(bvals, list) else [bvals, bvecs]
for ftr in to_remove:
os.remove(ftr)
lgr.debug("%s and %s were removed since not dwi", bvecs, bvals)

Check warning on line 896 in heudiconv/convert.py

View check run for this annotation

Codecov / codecov/patch

heudiconv/convert.py#L893-L896

Added lines #L893 - L896 were not covered by tests
else:
lgr.warning(
DW_IMAGE_IN_FMAP_FOLDER_WARNING.format(folder=prefix_dirname)
Expand All @@ -901,8 +902,8 @@
".bvec and .bval files will be generated. This is NOT BIDS compliant"
)
outname_bvecs, outname_bvals = prefix + ".bvec", prefix + ".bval"
safe_movefile(res.outputs.bvecs, outname_bvecs, overwrite)
safe_movefile(res.outputs.bvals, outname_bvals, overwrite)
safe_movefile(bvecs, outname_bvecs, overwrite)
safe_movefile(bvals, outname_bvals, overwrite)

if isinstance(res_files, list):
res_files = sorted(res_files)
Expand Down Expand Up @@ -1064,7 +1065,7 @@
save_json(infofile, meta_info)


def bvals_are_zero(bval_file: str) -> bool:
def bvals_are_zero(bval_file: str | list) -> bool:
"""Checks if all entries in a bvals file are zero (or 5, for Siemens files).

Parameters
Expand All @@ -1077,6 +1078,10 @@
True if all are all 0 or 5; False otherwise.
"""

# GE hyperband multi-echo containing diffusion info
if isinstance(bval_file, list):
return all(map(bvals_are_zero, bval_file))

with open(bval_file) as f:
bvals = f.read().split()

Expand Down
1 change: 1 addition & 0 deletions heudiconv/tests/data/non_zeros.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1000 0 1000 2000
1 change: 1 addition & 0 deletions heudiconv/tests/data/zeros.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 0 0 0
14 changes: 14 additions & 0 deletions heudiconv/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import heudiconv.convert
from heudiconv.convert import (
DW_IMAGE_IN_FMAP_FOLDER_WARNING,
bvals_are_zero,
update_complex_name,
update_multiecho_name,
update_uncombined_name,
Expand Down Expand Up @@ -287,3 +288,16 @@ def mock_populate_intended_for(
else:
# If there was no heuristic, make sure populate_intended_for was not called
assert not output.out


def test_bvals_are_zero() -> None:
"""Unit testing for heudiconv.convert.bvals_are_zero(),
which checks if non-dwi bvals are all zeros and can be removed
"""
zero_bvals = op.join(TESTS_DATA_PATH, "zeros.bval")
non_zero_bvals = op.join(TESTS_DATA_PATH, "non_zeros.bval")

assert bvals_are_zero(zero_bvals)
assert not bvals_are_zero(non_zero_bvals)
assert bvals_are_zero([zero_bvals, zero_bvals])
assert not bvals_are_zero([non_zero_bvals, zero_bvals])