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

Increase affine consistency in dipy.tracking.utils #1939

Merged
merged 17 commits into from Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 14 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
9 changes: 3 additions & 6 deletions dipy/tracking/local/localtracking.py
Expand Up @@ -111,12 +111,9 @@ def _tracker(self, seed, first_step, streamline):
def __iter__(self):
# Make tracks, move them to point space and return
track = self._generate_streamlines()
seeds = None
if self.save_seeds:
track, seeds = zip(*track)
return utils.move_streamlines(track,
self.affine,
seeds=seeds)

return utils.transform_tracking_output(track, self.affine,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this function? Why not transform_streamlines from dipy.tracking.streamlines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My reasoning was that transform_streamlines should do one thing and one thing only, to stay as simple as possible.

This function takes a generator as a input that can be a tuple or not, and in 99% of the case where people want to transform_streamlines it is just that.
This function with the generator, the tuple and the saving_seeds option is used only once in all dipy and is specifically for the tracking.

I think merging the two function would add to the confusion. And complexify a function that should remain as simple as simple (transform_streamlines)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, make sense!

save_seeds=self.save_seeds)

def _generate_streamlines(self):
"""A streamline generator"""
Expand Down
6 changes: 3 additions & 3 deletions dipy/tracking/local/tests/test_tracking.py
Expand Up @@ -297,7 +297,7 @@ def allclose(x, y):
npt.assert_(len(streamlines[1]) == 1) # OUTSIDEIMAGE

# Test that all points are within the image volume
seeds = seeds_from_mask(np.ones(mask.shape), density=2)
seeds = seeds_from_mask(np.ones(mask.shape), np.eye(4), density=2)
streamline_generator = LocalTracking(dg, tc, seeds, np.eye(4), 0.5,
return_all=True)
streamlines = Streamlines(streamline_generator)
Expand Down Expand Up @@ -349,7 +349,7 @@ def test_particle_filtering_tractography():
np.zeros(simple_gm.shape)])
simple_csf = np.ones(simple_wm.shape) - simple_wm - simple_gm
tc = ActTissueClassifier.from_pve(simple_wm, simple_gm, simple_csf)
seeds = seeds_from_mask(simple_wm, density=2)
seeds = seeds_from_mask(simple_wm, np.eye(4), density=2)

# Random pmf in every voxel
shape_img = list(simple_wm.shape)
Expand Down Expand Up @@ -382,7 +382,7 @@ def test_particle_filtering_tractography():
npt.assert_almost_equal(np.linalg.norm(s[i] - s[i + 1]),
step_size)
# Test that all points are within the image volume
seeds = seeds_from_mask(np.ones(simple_wm.shape), density=1)
seeds = seeds_from_mask(np.ones(simple_wm.shape), np.eye(4), density=1)
pft_streamlines_generator = ParticleFilteringTracking(
dg, tc, seeds, np.eye(4), step_size, max_cross=1, return_all=True)
pft_streamlines = Streamlines(pft_streamlines_generator)
Expand Down
8 changes: 4 additions & 4 deletions dipy/tracking/streamline.py
Expand Up @@ -7,8 +7,8 @@
import numpy as np
from nibabel.affines import apply_affine
from nibabel.streamlines import ArraySequence as Streamlines
from dipy.tracking.streamlinespeed import (set_number_of_points, length,
compress_streamlines)
from dipy.tracking.streamlinespeed import (compress_streamlines, length,
set_number_of_points)
from dipy.tracking.distances import bundles_distances_mdf
import dipy.tracking.utils as ut
from dipy.core.geometry import dist_to_corner
Expand Down Expand Up @@ -633,8 +633,8 @@ def _extract_vals(data, streamlines, affine=None, threedvec=False):
isinstance(streamlines, types.GeneratorType) or
isinstance(streamlines, Streamlines)):
if affine is not None:
streamlines = ut.move_streamlines(streamlines,
np.linalg.inv(affine))
streamlines = transform_streamlines(streamlines,
np.linalg.inv(affine))

vals = []
for sl in streamlines:
Expand Down
1 change: 1 addition & 0 deletions dipy/tracking/tests/test_life.py
Expand Up @@ -159,6 +159,7 @@ def test_fit_data():
gtab = grad.gradient_table(fbval, fbvec)
ni_data = nib.load(fdata)
data = ni_data.get_data()

tensor_streamlines = nib.streamlines.load(fstreamlines).streamlines
sft = StatefulTractogram(tensor_streamlines, ni_data, Space.RASMM)
sft.to_vox()
Expand Down
9 changes: 4 additions & 5 deletions dipy/tracking/tests/test_streamline.py
Expand Up @@ -11,11 +11,10 @@

from dipy.testing import assert_true
from numpy.testing import (assert_array_equal, assert_array_almost_equal,
assert_raises, run_module_suite, assert_allclose,
assert_raises, assert_allclose,
assert_almost_equal, assert_equal)

from dipy.tracking.streamline import Streamlines
import dipy.tracking.utils as ut
from dipy.tracking.streamline import (set_number_of_points,
length,
relist_streamlines,
Expand Down Expand Up @@ -1110,15 +1109,15 @@ def test_values_from_volume():

affine = np.eye(4)
affine[:, 3] = [-100, 10, 1, 1]
x_sl1 = ut.move_streamlines(sl1, affine)
x_sl2 = ut.move_streamlines(sl1, affine)
x_sl1 = transform_streamlines(sl1, affine)
x_sl2 = transform_streamlines(sl1, affine)

vv = values_from_volume(data, x_sl1, affine=affine)
npt.assert_almost_equal(vv, ans1, decimal=decimal)

# The generator has already been consumed so needs to be
# regenerated:
x_sl1 = list(ut.move_streamlines(sl1, affine))
x_sl1 = list(transform_streamlines(sl1, affine))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is already returning a list

vv = values_from_volume(data, x_sl1, affine=affine)
npt.assert_almost_equal(vv, ans1, decimal=decimal)

Expand Down