From d94c7aab377dbfdaa3e541a41f528f288cf767d1 Mon Sep 17 00:00:00 2001 From: arokem Date: Sun, 23 Aug 2015 09:51:35 -0700 Subject: [PATCH] A few more comments from @Garyfallidis. Also - incorporate recent changes from upstream (including changes in tools/make_examples.py). --- dipy/tracking/streamline.py | 61 +++++--------------------- dipy/tracking/tests/test_streamline.py | 35 +++++++-------- dipy/tracking/utils.py | 42 +++++++++++++++++- tools/make_examples.py | 2 +- 4 files changed, 70 insertions(+), 70 deletions(-) diff --git a/dipy/tracking/streamline.py b/dipy/tracking/streamline.py index b3a034a953..29187877d4 100644 --- a/dipy/tracking/streamline.py +++ b/dipy/tracking/streamline.py @@ -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 @@ -119,47 +120,7 @@ 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, @@ -167,13 +128,13 @@ def select_by_roi(streamlines, rois, include, mode=None, affine=None, 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 @@ -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 -------- @@ -226,20 +187,20 @@ 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) @@ -247,7 +208,7 @@ def select_by_roi(streamlines, rois, include, mode=None, affine=None, [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) @@ -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 diff --git a/dipy/tracking/tests/test_streamline.py b/dipy/tracking/tests/test_streamline.py index fb527a2533..86e84e5a96 100644 --- a/dipy/tracking/tests/test_streamline.py +++ b/dipy/tracking/tests/test_streamline.py @@ -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], @@ -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): """ @@ -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], @@ -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) @@ -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__': diff --git a/dipy/tracking/utils.py b/dipy/tracking/utils.py index de53f3f40d..33bb8749e3 100644 --- a/dipy/tracking/utils.py +++ b/dipy/tracking/utils.py @@ -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 """ @@ -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 diff --git a/tools/make_examples.py b/tools/make_examples.py index 2cdc0c39dd..2bf88fc79b 100755 --- a/tools/make_examples.py +++ b/tools/make_examples.py @@ -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