Skip to content

Commit

Permalink
A few more comments from @Garyfallidis.
Browse files Browse the repository at this point in the history
Also - incorporate recent changes from upstream (including changes in
tools/make_examples.py).
  • Loading branch information
arokem committed Aug 23, 2015
1 parent f6ee706 commit d94c7aa
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 70 deletions.
61 changes: 11 additions & 50 deletions dipy/tracking/streamline.py
Expand Up @@ -2,6 +2,7 @@

import numpy as np
from nibabel.affines import apply_affine
import dipy.tracking.utils as ut
from dipy.tracking.streamlinespeed import set_number_of_points
from dipy.tracking.streamlinespeed import length
from dipy.tracking.streamlinespeed import compress_streamlines
Expand Down Expand Up @@ -119,61 +120,21 @@ def select_random_set_of_streamlines(streamlines, select):
return [streamlines[i] for i in index]


def reduce_rois(rois, include):
"""Reduce multiple ROIs to one inclusion and one exclusion ROI
Parameters
----------
rois : list or ndarray
A list of 3D arrays, each with shape (x, y, z) corresponding to the
shape of the brain volume, or a 4D array with shape (n_rois, x, y,
z). Non-zeros in each volume are considered to be within the region.
include: array or list
A list or 1D array of boolean marking inclusion or exclusion
criteria.
Returns
-------
include_roi : boolean 3D array
An array marking the inclusion mask.
exclude_roi : boolean 3D array
An array marking the exclusion mask
Note
----
The include_roi and exclude_roi can be used to perfom the operation: "(A
or B or ...) and not (X or Y or ...)", where A, B are inclusion regions
and X, Y are exclusion regions.
"""
include_roi = np.zeros(rois[0].shape, dtype=bool)
exclude_roi = np.zeros(rois[0].shape, dtype=bool)

for i in range(len(rois)):
if include[i]:
include_roi |= rois[i]
else:
exclude_roi |= rois[i]

return include_roi, exclude_roi


def select_by_roi(streamlines, rois, include, mode=None, affine=None,
def select_by_rois(streamlines, rois, include, mode=None, affine=None,
tol=None):
"""Select streamlines based on logical relations with several regions of
interest (ROIs). For example, select streamlines that pass near ROI1,
but only if they do not pass near ROI2.
Parameters
----------
streamlines: list
streamlines : list
A list of candidate streamlines for selection
rois: list or ndarray
rois : list or ndarray
A list of 3D arrays, each with shape (x, y, z) corresponding to the
shape of the brain volume, or a 4D array with shape (n_rois, x, y,
z). Non-zeros in each volume are considered to be within the region
include: array or list
include : array or list
A list or 1D array of boolean values marking inclusion or exclusion
criteria. If a streamline is near any of the inclusion ROIs, it
should evaluate to True, unless it is also near any of the exclusion
Expand Down Expand Up @@ -211,7 +172,7 @@ def select_by_roi(streamlines, rois, include, mode=None, affine=None,
See also
--------
:func:`dipy.tracking.utils.near_roi`
:func:`reduce_rois`
:func:`dipy.tracking.utils.reduce_rois`
Examples
--------
Expand All @@ -226,28 +187,28 @@ def select_by_roi(streamlines, rois, include, mode=None, affine=None,
>>> mask2 = np.zeros_like(mask1)
>>> mask1[0, 0, 0] = True
>>> mask2[1, 0, 0] = True
>>> selection = select_by_roi(streamlines, [mask1, mask2], [True, True],
>>> selection = select_by_rois(streamlines, [mask1, mask2], [True, True],
... tol=1)
>>> list(selection) # The result is a generator
[array([[ 0. , 0. , 0.9],
[ 1.9, 0. , 0. ]]), array([[ 0., 0., 0.],
[ 0., 1., 1.],
[ 0., 2., 2.]])]
>>> selection = select_by_roi(streamlines, [mask1, mask2], [True, False],
>>> selection = select_by_rois(streamlines, [mask1, mask2], [True, False],
... tol=0.87)
>>> list(selection)
[array([[ 0., 0., 0.],
[ 0., 1., 1.],
[ 0., 2., 2.]])]
>>> selection = select_by_roi(streamlines, [mask1, mask2],
>>> selection = select_by_rois(streamlines, [mask1, mask2],
... [True, True],
... mode="both_end",
... tol=1.0)
>>> list(selection)
[array([[ 0. , 0. , 0.9],
[ 1.9, 0. , 0. ]])]
>>> mask2[0, 2, 2] = True
>>> selection = select_by_roi(streamlines, [mask1, mask2],
>>> selection = select_by_rois(streamlines, [mask1, mask2],
... [True, True],
... mode="both_end",
... tol=1.0)
Expand All @@ -268,7 +229,7 @@ def select_by_roi(streamlines, rois, include, mode=None, affine=None,
w_s += " inclusion ROI. Setting to: %s" % dist_to_corner
warn(w_s)
tol = dtc
include_roi, exclude_roi = reduce_rois(rois, include)
include_roi, exclude_roi = ut.reduce_rois(rois, include)
include_roi_coords = np.array(np.where(include_roi)).T
x_include_roi_coords = apply_affine(affine, include_roi_coords)
exclude_roi_coords = np.array(np.where(exclude_roi)).T
Expand Down
35 changes: 17 additions & 18 deletions dipy/tracking/tests/test_streamline.py
Expand Up @@ -16,7 +16,7 @@
transform_streamlines,
select_random_set_of_streamlines,
compress_streamlines,
select_by_roi)
select_by_rois)


streamline = np.array([[82.20181274, 91.36505890, 43.15737152],
Expand Down Expand Up @@ -410,7 +410,6 @@ def test_select_random_streamlines():
assert_equal(len(new_streamlines), 3)


<<<<<<< HEAD
def compress_streamlines_python(streamline, tol_error=0.01,
max_segment_length=10):
"""
Expand Down Expand Up @@ -545,8 +544,9 @@ def test_compress_streamlines():
max_segment_length=np.inf)
assert_equal(len(cspecial_streamline), len(cstreamline_python))
assert_array_almost_equal(cspecial_streamline, cstreamline_python)
=======
def test_select_by_roi():


def test_select_by_rois():
streamlines = [np.array([[0, 0., 0.9],
[1.9, 0., 0.]]),
np.array([[0.1, 0., 0],
Expand All @@ -562,70 +562,70 @@ def test_select_by_roi():
mask1[0, 0, 0] = True
mask2[1, 0, 0] = True

selection = select_by_roi(streamlines, [mask1], [True],
selection = select_by_rois(streamlines, [mask1], [True],
tol=1)

npt.assert_array_equal(list(selection), [streamlines[0],
streamlines[1]])

selection = select_by_roi(streamlines, [mask1, mask2], [True, True],
selection = select_by_rois(streamlines, [mask1, mask2], [True, True],
tol=1)

npt.assert_array_equal(list(selection), [streamlines[0],
streamlines[1]])

selection = select_by_roi(streamlines, [mask1, mask2], [True, False])
selection = select_by_rois(streamlines, [mask1, mask2], [True, False])

npt.assert_array_equal(list(selection), [streamlines[1]])

# Setting tolerance too low gets overridden:
selection = select_by_roi(streamlines, [mask1, mask2], [True, False],
selection = select_by_rois(streamlines, [mask1, mask2], [True, False],
tol=0.1)
npt.assert_array_equal(list(selection), [streamlines[1]])


selection = select_by_roi(streamlines, [mask1, mask2], [True, True],
selection = select_by_rois(streamlines, [mask1, mask2], [True, True],
tol=0.87)

npt.assert_array_equal(list(selection), [streamlines[1]])

mask3 = np.zeros_like(mask1)
mask3[0, 2, 2] = 1
selection = select_by_roi(streamlines, [mask1, mask2, mask3],
selection = select_by_rois(streamlines, [mask1, mask2, mask3],
[True, True, False], tol=1.0)

npt.assert_array_equal(list(selection), [streamlines[0]])

# Select using only one ROI
selection = select_by_roi(streamlines, [mask1], [True], tol=0.87)
selection = select_by_rois(streamlines, [mask1], [True], tol=0.87)
npt.assert_array_equal(list(selection), [streamlines[1]])

selection = select_by_roi(streamlines, [mask1], [True], tol=1.0)
selection = select_by_rois(streamlines, [mask1], [True], tol=1.0)
npt.assert_array_equal(list(selection), [streamlines[0],
streamlines[1]])

# Use different modes:
selection = select_by_roi(streamlines, [mask1, mask2, mask3],
selection = select_by_rois(streamlines, [mask1, mask2, mask3],
[True, True, False],
mode="all",
tol=1.0)
npt.assert_array_equal(list(selection), [streamlines[0]])


selection = select_by_roi(streamlines, [mask1, mask2, mask3],
selection = select_by_rois(streamlines, [mask1, mask2, mask3],
[True, True, False],
mode="either_end",
tol=1.0)
npt.assert_array_equal(list(selection), [streamlines[0]])

selection = select_by_roi(streamlines, [mask1, mask2, mask3],
selection = select_by_rois(streamlines, [mask1, mask2, mask3],
[True, True, False],
mode="both_end",
tol=1.0)
npt.assert_array_equal(list(selection), [streamlines[0]])

mask2[0, 2, 2] = True
selection = select_by_roi(streamlines, [mask1, mask2, mask3],
selection = select_by_rois(streamlines, [mask1, mask2, mask3],
[True, True, False],
mode="both_end",
tol=1.0)
Expand All @@ -639,12 +639,11 @@ def generate_sl(streamlines):
for sl in streamlines:
yield sl

selection = select_by_roi(generate_sl(streamlines), [mask1], [True],
selection = select_by_rois(generate_sl(streamlines), [mask1], [True],
tol=1.0)
npt.assert_array_equal(list(selection), [streamlines[0],
streamlines[1]])

>>>>>>> Move select_by_roi into tracking.streamline


if __name__ == '__main__':
Expand Down
42 changes: 41 additions & 1 deletion dipy/tracking/utils.py
Expand Up @@ -599,7 +599,7 @@ def streamline_near_roi(streamline, roi_coords, tol, mode='any'):
"both_end" : both end points are within tol from ROI.
Returns
Returns
-------
out : boolean
"""
Expand Down Expand Up @@ -892,3 +892,43 @@ def move_streamlines(streamlines, output_space, input_space=None):

for sl in streamlines:
yield np.dot(sl, lin_T) + offset


def reduce_rois(rois, include):
"""Reduce multiple ROIs to one inclusion and one exclusion ROI
Parameters
----------
rois : list or ndarray
A list of 3D arrays, each with shape (x, y, z) corresponding to the
shape of the brain volume, or a 4D array with shape (n_rois, x, y,
z). Non-zeros in each volume are considered to be within the region.
include : array or list
A list or 1D array of boolean marking inclusion or exclusion
criteria.
Returns
-------
include_roi : boolean 3D array
An array marking the inclusion mask.
exclude_roi : boolean 3D array
An array marking the exclusion mask
Note
----
The include_roi and exclude_roi can be used to perfom the operation: "(A
or B or ...) and not (X or Y or ...)", where A, B are inclusion regions
and X, Y are exclusion regions.
"""
include_roi = np.zeros(rois[0].shape, dtype=bool)
exclude_roi = np.zeros(rois[0].shape, dtype=bool)

for i in range(len(rois)):
if include[i]:
include_roi |= rois[i]
else:
exclude_roi |= rois[i]

return include_roi, exclude_roi
2 changes: 1 addition & 1 deletion tools/make_examples.py
Expand Up @@ -109,7 +109,7 @@ def show():
if not os.path.isdir('fig'):
os.mkdir('fig')

for script in glob('*.py'):
for script in validated_examples:
figure_basename = os.path.join('fig', os.path.splitext(script)[0])
print script

Expand Down

0 comments on commit d94c7aa

Please sign in to comment.