Skip to content

Commit ee0fe99

Browse files
committed
FIX: Adjust au2rads to handle input data with 2pi range
1 parent 6d2974c commit ee0fe99

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

sdcflows/interfaces/fmap.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -666,29 +666,33 @@ def au2rads(in_file, newpath=None):
666666
data = im.get_fdata(dtype='float32')
667667
hdr = im.header.copy()
668668

669-
dmin, dmax = data.min(), data.max()
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()
670674

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]
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]
674678

675-
# Center data around 0.0
676-
data -= mode
679+
# Center data around 0.0
680+
data -= mode
677681

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")
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")
683687

684-
# Scale lower tail
685-
data[neg] = - np.pi * data[neg] / data[neg].min()
688+
# Scale lower tail
689+
data[neg] = - np.pi * data[neg] / data[neg].min()
686690

687-
# Scale upper tail
688-
data[pos] = np.pi * data[pos] / data[pos].max()
691+
# Scale upper tail
692+
data[pos] = np.pi * data[pos] / data[pos].max()
689693

690-
# Offset to 0 - 2pi
691-
data += np.pi
694+
# Offset to 0 - 2pi
695+
data += np.pi
692696

693697
# Clip
694698
data = np.clip(data, 0.0, 2 * np.pi)

0 commit comments

Comments
 (0)