Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add functionality to clip streamlines between ROIs in orient_by_rois #1093

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 31 additions & 16 deletions dipy/tracking/streamline.py
Expand Up @@ -260,7 +260,7 @@ def select_by_rois(streamlines, rois, include, mode=None, affine=None,
yield sl


def _orient_generator(out, roi1, roi2):
def _orient_generator(out, roi1, roi2, clip=False):
"""
Helper function to `orient_by_rois`

Expand All @@ -273,12 +273,17 @@ def _orient_generator(out, roi1, roi2):
min1 = np.argmin(dist1, 0)
min2 = np.argmin(dist2, 0)
if min1[0] > min2[0]:
yield sl[::-1]
out = sl[::-1]
if clip:
out = out[min2:min1]
else:
yield sl
out = sl
if clip:
out = out[min1:min2]
yield out


def _orient_list(out, roi1, roi2):
def _orient_list(out, roi1, roi2, clip=False):
"""
Helper function to `orient_by_rois`

Expand All @@ -295,11 +300,17 @@ def _orient_list(out, roi1, roi2):
min2 = np.argmin(dist2, 0)
if min1[0] > min2[0]:
out[idx] = sl[::-1]
if clip:
out[idx] = out[idx][min2:min1]
else:
if clip:
out[idx] = out[idx][min1:min2]

return out


def orient_by_rois(streamlines, roi1, roi2, in_place=False,
as_generator=False, affine=None):
as_generator=False, affine=None, clip=False):
"""Orient a set of streamlines according to a pair of ROIs

Parameters
Expand All @@ -319,6 +330,8 @@ def orient_by_rois(streamlines, roi1, roi2, in_place=False,
Whether to return a generator as output. Default: False
affine : ndarray
Affine transformation from voxels to streamlines. Default: identity.
clip : bool
Whether to clip the streamlines between the ROIs

Returns
-------
Expand All @@ -328,20 +341,22 @@ def orient_by_rois(streamlines, roi1, roi2, in_place=False,

Examples
--------
>>> streamlines = [np.array([[0, 0., 0],
... [1, 0., 0.],
... [2, 0., 0.]]),
... np.array([[2, 0., 0.],
... [1, 0., 0],
... [0, 0, 0.]])]
>>> streamlines = [np.array([[0, 0, 0],
... [0.5, 0, 0],
... [1, 0, 0],
... [2, 0, 0]], dtype=float),
... np.array([[2, 0, 0],
... [1, 0, 0],
... [0, 0, 0]], dtype=float)]
>>> roi1 = np.zeros((4, 4, 4), dtype=bool)
>>> roi2 = np.zeros_like(roi1)
>>> roi1[0, 0, 0] = True
>>> roi2[1, 0, 0] = True
>>> orient_by_rois(streamlines, roi1, roi2)
[array([[ 0., 0., 0.],
[ 1., 0., 0.],
[ 2., 0., 0.]]), array([[ 0., 0., 0.],
[array([[ 0. , 0. , 0. ],
[ 0.5, 0. , 0. ],
[ 1. , 0. , 0. ],
[ 2. , 0. , 0. ]]), array([[ 0., 0., 0.],
[ 1., 0., 0.],
[ 2., 0., 0.]])]

Expand All @@ -360,7 +375,7 @@ def orient_by_rois(streamlines, roi1, roi2, in_place=False,
if in_place:
w_s = "Cannot return a generator when in_place is set to True"
raise ValueError(w_s)
return _orient_generator(streamlines, roi1, roi2)
return _orient_generator(streamlines, roi1, roi2, clip=clip)

# If it's a generator on input, we may as well generate it
# here and now:
Expand All @@ -373,7 +388,7 @@ def orient_by_rois(streamlines, roi1, roi2, in_place=False,
# Make a copy, so you don't change the output in place:
out = deepcopy(streamlines)

return _orient_list(out, roi1, roi2)
return _orient_list(out, roi1, roi2, clip=clip)


def _extract_vals(data, streamlines, affine=None, threedvec=False):
Expand Down
13 changes: 13 additions & 0 deletions dipy/tracking/tests/test_streamline.py
Expand Up @@ -785,6 +785,7 @@ def test_select_by_rois():

def test_orient_by_rois():
streamlines = [np.array([[0, 0., 0],
[0.5, 0, 0],
[1, 0., 0.],
[2, 0., 0.]]),
np.array([[2, 0., 0.],
Expand Down Expand Up @@ -816,6 +817,18 @@ def test_orient_by_rois():
npt.assert_equal(new_streamlines, flipped_sl)
npt.assert_(new_streamlines is not streamlines)

# Clip to the ROIs:
new_streamlines = orient_by_rois(streamlines,
mask1_vol,
mask2_vol,
in_place=False,
affine=None,
as_generator=False,
clip=True)

expected = [streamlines[0][0:2], streamlines[1][::-1][0:1]]
npt.assert_equal(new_streamlines, expected)

# Test with affine:
x_flipped_sl = [s + affine[:3, 3] for s in flipped_sl]
new_streamlines = orient_by_rois(x_streamlines,
Expand Down