Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RF/FIX: Scale all phase maps to [0, 2pi] range #88

Merged
merged 2 commits into from
Feb 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 6 additions & 28 deletions sdcflows/interfaces/fmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,42 +661,20 @@ def _delta_te(in_values, te1=None, te2=None):

def au2rads(in_file, newpath=None):
"""Convert the input phase difference map in arbitrary units (a.u.) to rads."""
from scipy import stats
im = nb.load(in_file)
data = im.get_fdata(dtype='float32')
data = im.get_fdata(caching='unchanged') # Read as float64 for safety
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm extremely comfortable using double memory for one volume to keep things deterministic.

hdr = im.header.copy()

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

# Find the mode ignoring outliers on the far max/min, to allow for junk outside the FoV
fudge = 0.05 * (dmax - dmin)
mode = stats.mode(data[(data > dmin + fudge) & (data < dmax - fudge)])[0][0]

# Center data around 0.0
data -= mode

# Provide a less opaque error if we still can't figure it out
neg = data < 0
pos = data > 0
if not (neg.any() and pos.any()):
raise ValueError("Could not find an appropriate mode to center phase values around")

# Scale lower tail
data[neg] = - np.pi * data[neg] / data[neg].min()

# Scale upper tail
data[pos] = np.pi * data[pos] / data[pos].max()

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

# Clip
data = np.clip(data, 0.0, 2 * np.pi)
# Round to float32 and clip
data = np.clip(np.float32(data), 0.0, 2 * np.pi)

hdr.set_data_dtype(np.float32)
hdr.set_xyzt_units('mm')
out_file = fname_presuffix(in_file, suffix='_rads', newpath=newpath)
nb.Nifti1Image(data, im.affine, hdr).to_filename(out_file)
nb.Nifti1Image(data, None, hdr).to_filename(out_file)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're keeping the affines in the header, there's no need to pass an affine in to override.

return out_file


Expand Down