Skip to content

Commit

Permalink
Merge pull request #293 from mgxd/fix/fieldmap-replace
Browse files Browse the repository at this point in the history
FIX: Ensure `replace()` calls only alter the file basename
  • Loading branch information
mgxd authored Oct 21, 2022
2 parents 1c1a469 + cd4401e commit 83b064e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
28 changes: 13 additions & 15 deletions sdcflows/fieldmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ def __attrs_post_init__(self):

if fmap_types:
sources = sorted(
str(f.path)
f.path
for f in self.sources
if f.suffix in ("fieldmap", "phasediff", "phase1", "phase2")
)
Expand All @@ -325,7 +325,7 @@ def __attrs_post_init__(self):
if sum(missing_phases) == 1:
mis_ph = "phase1" if missing_phases[0] else "phase2"
hit_ph = "phase2" if missing_phases[0] else "phase1"
new_source = sources[0].replace(hit_ph, mis_ph)
new_source = sources[0].parent / sources[0].name.replace(hit_ph, mis_ph)
self.sources.append(FieldmapFile(new_source))
sources.insert(int(missing_phases[1]), new_source)

Expand All @@ -336,14 +336,13 @@ def __attrs_post_init__(self):
magnitude = f"magnitude{'' if self.method == EstimatorType.MAPPED else '1'}"
if magnitude not in suffix_set:
try:
self.sources.append(
FieldmapFile(
sources[0]
.replace("fieldmap", "magnitude")
.replace("diff", "1")
.replace("phase", "magnitude")
)
new_path = (
sources[0].parent / sources[0].name
.replace("fieldmap", "magnitude")
.replace("diff", "1")
.replace("phase", "magnitude")
)
self.sources.append(FieldmapFile(new_path))
except Exception:
raise ValueError(
"A fieldmap or phase-difference estimation type was found, "
Expand All @@ -356,13 +355,12 @@ def __attrs_post_init__(self):
and "magnitude2" not in suffix_set
):
try:
self.sources.append(
FieldmapFile(
sources[-1]
.replace("diff", "2")
.replace("phase", "magnitude")
)
new_path = (
sources[-1].parent / sources[-1].name
.replace("diff", "2")
.replace("phase", "magnitude")
)
self.sources.append(FieldmapFile(new_path))
except Exception:
if "phase2" in suffix_set:
raise ValueError(
Expand Down
19 changes: 19 additions & 0 deletions sdcflows/tests/test_fieldmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,22 @@ def test_FieldmapEstimation_missing_files(tmpdir, dsA_dir):
),
]
)


def test_FieldmapFile_filename(tmp_path, dsA_dir):
datadir = tmp_path / "phasediff"
datadir.mkdir(exist_ok=True)

fmap_path = dsA_dir / "sub-01" / "fmap"
for fl in fmap_path.glob("*"):
base = fl.name
if 'magnitude1' in base or 'magnitude2' in base or 'phasediff' in base:
print(fl.absolute(), str(datadir / base))

shutil.copy(fl.absolute(), str(datadir / base))

# Ensure the correct linked files (magnitude 1/2) are found
fm.FieldmapEstimation(
fm.FieldmapFile(datadir / "sub-01_phasediff.nii.gz")
)
fm.clear_registry()

0 comments on commit 83b064e

Please sign in to comment.