Skip to content

Commit

Permalink
Merge pull request #89 from effigies/fix/double_mode_partial_fix
Browse files Browse the repository at this point in the history
FIX: Center phase maps around central mode, avoiding FoV-related outliers
  • Loading branch information
effigies authored Feb 14, 2020
2 parents 42a273a + f4a2966 commit be1b766
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
37 changes: 27 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -374,33 +374,36 @@ jobs:
fi
test_package:
machine:
image: circleci/classic:201711-01
docker:
- image: circleci/python:3.7.4
working_directory: /tmp/src/sdcflows
steps:
- checkout
- run:
name: Setup Python environment with virtualenvs
command: |
pyenv global 3.5.2
python3 -m pip install --upgrade virtualenv
python -m pip install --user --upgrade virtualenv pip
- run:
name: Prepare build environment
command: |
virtualenv --python=python3 /tmp/build
source /tmp/build/bin/activate
python3 -m pip install "setuptools>=30.3.0" "pip>=10.0.1" twine docutils
- run:
name: Prepare install environment
name: Prepare install environments
command: |
virtualenv --python=python3 /tmp/install
source /tmp/install/bin/activate
virtualenv --python=python3 /tmp/install_sdist
source /tmp/install_sdist/bin/activate
python3 -m pip install "setuptools>=30.3.0" "pip>=10.0.1"
deactivate
virtualenv --python=python3 /tmp/install_wheel
source /tmp/install_wheel/bin/activate
python3 -m pip install "setuptools>=30.3.0" "pip>=10.0.1"
- run:
name: Build SDCflows in build environment
command: |
source /tmp/build/bin/activate
python setup.py sdist
python setup.py sdist bdist_wheel
- store_artifacts:
path: /tmp/src/sdcflows/dist
- run:
Expand All @@ -411,11 +414,25 @@ jobs:
- run:
name: Install sdist package into install environment and check version
command: |
source /tmp/install/bin/activate
source /tmp/install_sdist/bin/activate
THISVERSION=$( python get_version.py )
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
pip install dist/sdcflows*.tar.gz
which sdcflows | grep install\\/bin
which sdcflows | grep install_sdist\\/bin
INSTALLED_VERSION=$(sdcflows --version)
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
INSTALLED_VERSION=${INSTALLED_VERSION#*"sdcflows v"}
echo "VERSION: \"$THISVERSION\""
echo "INSTALLED: \"$INSTALLED_VERSION\""
test "$INSTALLED_VERSION" = "v$THISVERSION"
- run:
name: Install wheel into install environment and check version
command: |
source /tmp/install_wheel/bin/activate
THISVERSION=$( python get_version.py )
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
pip install dist/sdcflows*.whl
which sdcflows | grep install_wheel\\/bin
INSTALLED_VERSION=$(sdcflows --version)
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
INSTALLED_VERSION=${INSTALLED_VERSION#*"sdcflows v"}
Expand Down
22 changes: 17 additions & 5 deletions sdcflows/interfaces/fmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,19 +661,31 @@ 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.stats import mode
from scipy import stats
im = nb.load(in_file)
data = im.get_fdata(dtype='float32')
hdr = im.header.copy()

# First center data around 0.0.
data -= mode(data, axis=None)[0][0]
dmin, dmax = data.min(), data.max()

# 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[data < 0] = - np.pi * data[data < 0] / data[data < 0].min()
data[neg] = - np.pi * data[neg] / data[neg].min()

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

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

0 comments on commit be1b766

Please sign in to comment.