Skip to content

Commit a3b908f

Browse files
committed
FIX: Center phase maps around second mode when first is minimum
1 parent fc9e9cf commit a3b908f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

sdcflows/interfaces/fmap.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,30 @@ 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.stats import mode
665664
im = nb.load(in_file)
666665
data = im.get_fdata(dtype='float32')
667666
hdr = im.header.copy()
668667

669-
# First center data around 0.0.
670-
data -= mode(data, axis=None)[0][0]
668+
vals, counts = np.unique(data, return_counts=True)
669+
modes = vals[np.argsort(counts)[::-1]]
670+
671+
# Second mode (idx 1) if first node (idx 0) is minimum, else first
672+
idx = int(modes[0] == data.min())
673+
674+
# Center data around 0.0
675+
data -= modes[idx]
676+
677+
# Provide a less opaque error if we still can't figure it out
678+
neg = data < 0
679+
pos = data > 0
680+
if not (neg.any() and pos.any()):
681+
raise ValueError("Could not find an appropriate mode to center phase values around")
671682

672683
# Scale lower tail
673-
data[data < 0] = - np.pi * data[data < 0] / data[data < 0].min()
684+
data[neg] = - np.pi * data[neg] / data[neg].min()
674685

675686
# Scale upper tail
676-
data[data > 0] = np.pi * data[data > 0] / data[data > 0].max()
687+
data[pos] = np.pi * data[pos] / data[pos].max()
677688

678689
# Offset to 0 - 2pi
679690
data += np.pi

0 commit comments

Comments
 (0)