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
Deform streamlines #1398
Deform streamlines #1398
Changes from 8 commits
50541c2
fb99a3d
681daa8
60db661
ff595d0
f6b6569
ebd4a6c
4404aa4
b5321e7
ab699b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
|
||
from nose.tools import assert_true, assert_equal, assert_almost_equal | ||
from numpy.testing import (assert_array_equal, assert_array_almost_equal, | ||
assert_raises, run_module_suite) | ||
assert_raises, run_module_suite, assert_allclose) | ||
|
||
from dipy.tracking.streamline import Streamlines | ||
import dipy.tracking.utils as ut | ||
|
@@ -24,7 +24,8 @@ | |
compress_streamlines, | ||
select_by_rois, | ||
orient_by_rois, | ||
values_from_volume) | ||
values_from_volume, | ||
deform_streamlines) | ||
|
||
|
||
streamline = np.array([[82.20181274, 91.36505890, 43.15737152], | ||
|
@@ -516,6 +517,56 @@ def test_unlist_relist_streamlines(): | |
assert_array_equal(streamlines[i], streamlines2[i]) | ||
|
||
|
||
def test_deform_streamlines(): | ||
# streamlines needs to be a list of streamlines | ||
A = np.array([[1, 2, 3], [1, 2, 3.]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this still used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deleted |
||
|
||
# Create Random deformation field | ||
deformation_field = np.random.randn(200, 200, 200, 3) | ||
# shape = deformation_field.shape | ||
# for i in range(shape[0]): | ||
# for j in range(shape[1]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove these commented lines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
# for k in range(shape[2]): | ||
# deformation_field[i][j][k] = np.random.randn(3) | ||
# Specify stream2grid and grid2world | ||
stream2grid = np.array([[np.random.randn(1)[0], 0, 0, 0], | ||
[0, np.random.randn(1)[0], 0, 0], | ||
[0, 0, np.random.randn(1)[0], 0], | ||
[0, 0, 0, 1]]) | ||
grid2world = np.array([[np.random.randn(1)[0], 0, 0, 0], | ||
[0, np.random.randn(1)[0], 0, 0], | ||
[0, 0, np.random.randn(1)[0], 0], | ||
[0, 0, 0, 1]]) | ||
stream2world = np.dot(stream2grid, grid2world) | ||
|
||
# Deform streamlines (let two grid spaces be the same for simplicity) | ||
new_streamlines = deform_streamlines(streamlines, | ||
deformation_field, | ||
stream2grid, | ||
grid2world, | ||
stream2grid, | ||
grid2world) | ||
|
||
# Interpolate displacements onto original streamlines | ||
streamlines_in_grid = transform_streamlines(streamlines, stream2grid) | ||
displacements = values_from_volume(deformation_field, streamlines_in_grid) | ||
|
||
# Put new_streamlines into world space | ||
new_streamlines_world = transform_streamlines(new_streamlines, | ||
stream2world) | ||
|
||
# Subtract displacements from new_streamlines in world space | ||
orig_streamlines_world = list(np.subtract(new_streamlines_world, | ||
displacements)) | ||
|
||
# Put orig_streamlines_world into voxmm | ||
orig_streamlines = transform_streamlines(orig_streamlines_world, | ||
np.linalg.inv(stream2world)) | ||
# All close because of floating pt inprecision | ||
for o, s in zip(orig_streamlines, streamlines): | ||
assert_allclose(s, o, rtol=1e-10, atol=0) | ||
|
||
|
||
def test_center_and_transform(): | ||
A = np.array([[1, 2, 3], [1, 2, 3.]]) | ||
streamlines = [A for i in range(10)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's up to you but I find
displacements + stream_in_world
easier to read thannp.add(...)
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced with list comprehension - adding element wise