Skip to content

Commit 410365a

Browse files
committed
RF: Always simply rescale to [0,2pi]
1 parent ee0fe99 commit 410365a

File tree

1 file changed

+6
-32
lines changed

1 file changed

+6
-32
lines changed

sdcflows/interfaces/fmap.py

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -661,46 +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-
if np.allclose((data.min(), data.max()), (-np.pi, np.pi), atol=0.01):
670-
# Already in rads, but wrap to 0 - 2pi
671-
data[data < 0] += 2 * np.pi
672-
else:
673-
dmin, dmax = data.min(), data.max()
668+
# Rescale to [0, 2*pi]
669+
data = (data - data.min()) * (2 * np.pi / (data.max() - data.min()))
674670

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

700674
hdr.set_data_dtype(np.float32)
701675
hdr.set_xyzt_units('mm')
702676
out_file = fname_presuffix(in_file, suffix='_rads', newpath=newpath)
703-
nb.Nifti1Image(data, im.affine, hdr).to_filename(out_file)
677+
nb.Nifti1Image(data, None, hdr).to_filename(out_file)
704678
return out_file
705679

706680

0 commit comments

Comments
 (0)