Skip to content

Commit

Permalink
Merge pull request #345 from matthew-brett/fix-viz-error
Browse files Browse the repository at this point in the history
MRG: fix to viz test error in Python 3

Fix by Bertrand.

Added some refactoring and tests.
  • Loading branch information
matthew-brett committed Aug 27, 2015
2 parents d31366b + 2b6460e commit 188a07f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 46 deletions.
92 changes: 47 additions & 45 deletions nipy/labs/viz_tools/coord_tools.py
Expand Up @@ -25,31 +25,29 @@
################################################################################

def coord_transform(x, y, z, affine):
""" Convert the x, y, z coordinates from one image space to another
space.
""" Convert x, y, z coordinates from one image space to another space.
Parameters
----------
x : number or ndarray
The x coordinates in the input space
y : number or ndarray
The y coordinates in the input space
z : number or ndarray
The z coordinates in the input space
affine : 2D 4x4 ndarray
affine that maps from input to output space.
Warning: x, y and z have Talairach ordering, not 3D numpy image ordering.
Returns
-------
x : number or ndarray
The x coordinates in the output space
y : number or ndarray
The y coordinates in the output space
z : number or ndarray
The z coordinates in the output space
Warning: The x, y and z have their Talairach ordering, not 3D
numy image ordering.
Parameters
----------
x : number or ndarray
The x coordinates in the input space
y : number or ndarray
The y coordinates in the input space
z : number or ndarray
The z coordinates in the input space
affine : 2D 4x4 ndarray
affine that maps from input to output space.
Returns
-------
x : number or ndarray
The x coordinates in the output space
y : number or ndarray
The y coordinates in the output space
z : number or ndarray
The z coordinates in the output space
"""
coords = np.c_[np.atleast_1d(x).flat,
np.atleast_1d(y).flat,
Expand Down Expand Up @@ -189,39 +187,42 @@ def _maximally_separated_subset(x, k):

def find_maxsep_cut_coords(map3d, affine, slicer='z', n_cuts=None,
threshold=None):
"""
Heuristic function to find n_cuts along a given axis, which
are maximally separated in space.
""" Heuristic finds `n_cuts` with max separation along a given axis
map3d: 3D array
Parameters
----------
map3d : 3D array
the data under consideration
slicer: string, optional (default "z")
affine : array shape (4, 4)
Affine mapping between array coordinates of `map3d` and real-world
coordinates.
slicer : string, optional
sectional slicer; possible values are "x", "y", or "z"
n_cuts: int > 1, optional (default None)
number of cuts in the plot; if no value is specified, then a default
value of 5 is forced
threshold: float, optional (default None)
thresholding to be applied to the map
n_cuts : None or int >= 1, optional
Number of cuts in the plot; if None, then a default value of 5 is
forced.
threshold : None or float, optional
Thresholding to be applied to the map. Values less than `threshold`
set to 0. If None, no thresholding applied.
Returns
-------
n_cuts: 1D array of length n_cuts
the computed n_cuts
cuts : 1D array of length `n_cuts`
the computed cuts
Raises
------
ValueError:
If `slicer` not in 'xyz'
ValueError
If `ncuts` < 1
"""

if n_cuts is None: n_cuts = 5
if n_cuts < 1: raise ValueError("n_cuts = %i < 1 is senseless." % n_cuts)

if n_cuts is None:
n_cuts = 5
if n_cuts < 1:
raise ValueError("n_cuts = %i < 1 is senseless." % n_cuts)
# sanitize slicer
if not slicer in ['x', 'y', 'z']:
if slicer not in 'xyz':
raise ValueError(
"slicer must be one of 'x', 'y', and 'z', got '%s'." % slicer)
slicer = "xyz".index(slicer)
Expand All @@ -233,7 +234,8 @@ def find_maxsep_cut_coords(map3d, affine, slicer='z', n_cuts=None,

_map3d = np.rollaxis(map3d.copy(), slicer, start=3)
_map3d = np.abs(_map3d)
_map3d[_map3d < threshold] = 0
if threshold is not None:
_map3d[_map3d < threshold] = 0

# count activated voxels per plane
n_activated_voxels_per_plane = np.array([(_map3d[..., z] > 0).sum()
Expand Down
23 changes: 22 additions & 1 deletion nipy/labs/viz_tools/test/test_coord_tools.py
Expand Up @@ -2,7 +2,10 @@
# vi: set ft=python sts=4 ts=4 sw=4 et:
import numpy as np

from ..coord_tools import coord_transform, find_cut_coords
from ..coord_tools import (coord_transform, find_cut_coords,
find_maxsep_cut_coords)

from numpy.testing import assert_array_equal

def test_coord_transform_trivial():
sform = np.eye(4)
Expand Down Expand Up @@ -32,3 +35,21 @@ def test_find_cut_coords():
(x_map, y_map, z_map))


def test_find_maxsep_cut_coords():
# Test find_maxsep_cut_coords function
assert_array_equal(
find_maxsep_cut_coords(np.ones((2, 3, 5)), np.eye(4)), range(5))
assert_array_equal(
find_maxsep_cut_coords(np.ones((2, 3, 5)), np.eye(4), threshold=1),
range(5))
assert_array_equal(
find_maxsep_cut_coords(np.ones((2, 3, 4)), np.eye(4), n_cuts=4),
range(4))
map_3d = np.ones((2, 3, 5))
map_3d[:, :, 1] = 0
assert_array_equal(
find_maxsep_cut_coords(map_3d, np.eye(4), n_cuts=4), [0, 2, 3, 4])
map_3d[:, :, 1] = 0.5
assert_array_equal(
find_maxsep_cut_coords(map_3d, np.eye(4), n_cuts=4, threshold=0.6),
[0, 2, 3, 4])

0 comments on commit 188a07f

Please sign in to comment.