Skip to content

Commit 28b084f

Browse files
authored
Merge pull request #88 from effigies/fix/rads2rads
FIX: Adjust au2rads to handle input data with 2pi range
2 parents b1e9cad + 410365a commit 28b084f

File tree

1 file changed

+6
-28
lines changed

1 file changed

+6
-28
lines changed

sdcflows/interfaces/fmap.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -661,42 +661,20 @@ def _delta_te(in_values, te1=None, te2=None):
661661

662662
def au2rads(in_file, newpath=None):
663663
"""Convert the input phase difference map in arbitrary units (a.u.) to rads."""
664-
from scipy import stats
665664
im = nb.load(in_file)
666-
data = im.get_fdata(dtype='float32')
665+
data = im.get_fdata(caching='unchanged') # Read as float64 for safety
667666
hdr = im.header.copy()
668667

669-
dmin, dmax = data.min(), data.max()
668+
# Rescale to [0, 2*pi]
669+
data = (data - data.min()) * (2 * np.pi / (data.max() - data.min()))
670670

671-
# Find the mode ignoring outliers on the far max/min, to allow for junk outside the FoV
672-
fudge = 0.05 * (dmax - dmin)
673-
mode = stats.mode(data[(data > dmin + fudge) & (data < dmax - fudge)])[0][0]
674-
675-
# Center data around 0.0
676-
data -= mode
677-
678-
# Provide a less opaque error if we still can't figure it out
679-
neg = data < 0
680-
pos = data > 0
681-
if not (neg.any() and pos.any()):
682-
raise ValueError("Could not find an appropriate mode to center phase values around")
683-
684-
# Scale lower tail
685-
data[neg] = - np.pi * data[neg] / data[neg].min()
686-
687-
# Scale upper tail
688-
data[pos] = np.pi * data[pos] / data[pos].max()
689-
690-
# Offset to 0 - 2pi
691-
data += np.pi
692-
693-
# Clip
694-
data = np.clip(data, 0.0, 2 * np.pi)
671+
# Round to float32 and clip
672+
data = np.clip(np.float32(data), 0.0, 2 * np.pi)
695673

696674
hdr.set_data_dtype(np.float32)
697675
hdr.set_xyzt_units('mm')
698676
out_file = fname_presuffix(in_file, suffix='_rads', newpath=newpath)
699-
nb.Nifti1Image(data, im.affine, hdr).to_filename(out_file)
677+
nb.Nifti1Image(data, None, hdr).to_filename(out_file)
700678
return out_file
701679

702680

0 commit comments

Comments
 (0)