Skip to content

Commit be1b766

Browse files
authored
Merge pull request #89 from effigies/fix/double_mode_partial_fix
FIX: Center phase maps around central mode, avoiding FoV-related outliers
2 parents 42a273a + f4a2966 commit be1b766

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

.circleci/config.yml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -374,33 +374,36 @@ jobs:
374374
fi
375375
376376
test_package:
377-
machine:
378-
image: circleci/classic:201711-01
377+
docker:
378+
- image: circleci/python:3.7.4
379379
working_directory: /tmp/src/sdcflows
380380
steps:
381381
- checkout
382382
- run:
383383
name: Setup Python environment with virtualenvs
384384
command: |
385-
pyenv global 3.5.2
386-
python3 -m pip install --upgrade virtualenv
385+
python -m pip install --user --upgrade virtualenv pip
387386
- run:
388387
name: Prepare build environment
389388
command: |
390389
virtualenv --python=python3 /tmp/build
391390
source /tmp/build/bin/activate
392391
python3 -m pip install "setuptools>=30.3.0" "pip>=10.0.1" twine docutils
393392
- run:
394-
name: Prepare install environment
393+
name: Prepare install environments
395394
command: |
396-
virtualenv --python=python3 /tmp/install
397-
source /tmp/install/bin/activate
395+
virtualenv --python=python3 /tmp/install_sdist
396+
source /tmp/install_sdist/bin/activate
397+
python3 -m pip install "setuptools>=30.3.0" "pip>=10.0.1"
398+
deactivate
399+
virtualenv --python=python3 /tmp/install_wheel
400+
source /tmp/install_wheel/bin/activate
398401
python3 -m pip install "setuptools>=30.3.0" "pip>=10.0.1"
399402
- run:
400403
name: Build SDCflows in build environment
401404
command: |
402405
source /tmp/build/bin/activate
403-
python setup.py sdist
406+
python setup.py sdist bdist_wheel
404407
- store_artifacts:
405408
path: /tmp/src/sdcflows/dist
406409
- run:
@@ -411,11 +414,25 @@ jobs:
411414
- run:
412415
name: Install sdist package into install environment and check version
413416
command: |
414-
source /tmp/install/bin/activate
417+
source /tmp/install_sdist/bin/activate
415418
THISVERSION=$( python get_version.py )
416419
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
417420
pip install dist/sdcflows*.tar.gz
418-
which sdcflows | grep install\\/bin
421+
which sdcflows | grep install_sdist\\/bin
422+
INSTALLED_VERSION=$(sdcflows --version)
423+
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
424+
INSTALLED_VERSION=${INSTALLED_VERSION#*"sdcflows v"}
425+
echo "VERSION: \"$THISVERSION\""
426+
echo "INSTALLED: \"$INSTALLED_VERSION\""
427+
test "$INSTALLED_VERSION" = "v$THISVERSION"
428+
- run:
429+
name: Install wheel into install environment and check version
430+
command: |
431+
source /tmp/install_wheel/bin/activate
432+
THISVERSION=$( python get_version.py )
433+
THISVERSION=${CIRCLE_TAG:-$THISVERSION}
434+
pip install dist/sdcflows*.whl
435+
which sdcflows | grep install_wheel\\/bin
419436
INSTALLED_VERSION=$(sdcflows --version)
420437
INSTALLED_VERSION=${INSTALLED_VERSION%$'\r'}
421438
INSTALLED_VERSION=${INSTALLED_VERSION#*"sdcflows v"}

sdcflows/interfaces/fmap.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,31 @@ 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
664+
from scipy import stats
665665
im = nb.load(in_file)
666666
data = im.get_fdata(dtype='float32')
667667
hdr = im.header.copy()
668668

669-
# First center data around 0.0.
670-
data -= mode(data, axis=None)[0][0]
669+
dmin, dmax = data.min(), data.max()
670+
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]
674+
675+
# Center data around 0.0
676+
data -= mode
677+
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")
671683

672684
# Scale lower tail
673-
data[data < 0] = - np.pi * data[data < 0] / data[data < 0].min()
685+
data[neg] = - np.pi * data[neg] / data[neg].min()
674686

675687
# Scale upper tail
676-
data[data > 0] = np.pi * data[data > 0] / data[data > 0].max()
688+
data[pos] = np.pi * data[pos] / data[pos].max()
677689

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

0 commit comments

Comments
 (0)