-
Notifications
You must be signed in to change notification settings - Fork 71
Add verification of out-of-bound with map_coordinates #1270
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -30,6 +30,7 @@ | |
| import numpy as np | ||
| from scipy.ndimage import map_coordinates | ||
|
Contributor
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. same |
||
|
|
||
| from scilpy.image.volume_space_management import map_coordinates_in_volume | ||
| from scilpy.tractanalysis.streamlines_metrics import compute_tract_counts_map | ||
| from scilpy.io.utils import (add_overwrite_arg, | ||
| add_verbose_arg, assert_headers_compatible, | ||
|
|
@@ -64,9 +65,9 @@ def _build_arg_parser(): | |
| metavar=('R', 'G', 'B'), type=float, | ||
| help='Color streamlines with uniform coloring.') | ||
| sub_color.add_argument('--reference_coloring', | ||
| metavar='COLORBAR', | ||
| help='Color streamlines with reference coloring ' | ||
| '(0-255).') | ||
| metavar='COLORMAP', | ||
| help='Color streamlines with reference coloring. ' | ||
| 'Name of a matlab colormap. (0-255).') | ||
| p.add_argument('--roi', nargs='+', action='append', | ||
| help='Path to a ROI file (.nii or nii.gz).') | ||
| p.add_argument('--right', action='store_true', | ||
|
|
@@ -261,14 +262,16 @@ def main(): | |
| args.uniform_coloring[1] / 255.0, | ||
| args.uniform_coloring[2] / 255.0) | ||
| elif args.reference_coloring: | ||
| # Sending to vox space, center origin for interpolation | ||
| sft.to_vox() | ||
| streamlines_vox = sft.get_streamlines_copy() | ||
| coords_vox = np.vstack(sft.streamlines).T | ||
| # Back to normal space | ||
| sft.to_rasmm() | ||
|
|
||
| normalized_data = reference_data / np.max(reference_data) | ||
| cmap = get_lookup_table(args.reference_coloring) | ||
| values = map_coordinates(normalized_data, | ||
| streamlines_vox.streamlines._data.T, | ||
| order=1, mode='nearest') | ||
| values = map_coordinates_in_volume(normalized_data, coords_vox, | ||
| order=1) | ||
| colors = cmap(values)[:, 0:3] | ||
| else: | ||
| colors = None | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import numpy as np | ||
| import logging | ||
|
|
||
| import numpy as np | ||
| from numba_kdtree import KDTree | ||
| from numba import njit | ||
| from scipy.ndimage import map_coordinates | ||
|
|
||
| from scilpy.tracking.fibertube_utils import (streamlines_to_segments, | ||
| point_in_cylinder, | ||
| sphere_cylinder_intersection) | ||
|
|
@@ -16,6 +19,39 @@ | |
| from dipy.reconst.shm import sf_to_sh | ||
|
|
||
|
|
||
| def map_coordinates_in_volume(data, points, order): | ||
| """ | ||
| Uses map_coordinates, from scipy. But by default, in scipy, half of the | ||
| border voxels are considered out-of-bound. Using mode=nearest to make sure | ||
| we interpolate correctly in border voxels. Verifying if some coordinates | ||
| are *actually* out-of-bound first. | ||
| See here for more explanation: https://github.com/scilus/scilpy/pull/1102 | ||
| An alternative is to use dipy's trilinear function, but in some cases | ||
| scipy's function is easier to use. | ||
| Parameters | ||
| ---------- | ||
| data: np.ndarray | ||
| The volume | ||
| points: np.ndarray | ||
| The coordinates in vox space, center origin. Shape: [3, N] | ||
| order: int | ||
| The order of the interpolation | ||
| Returns | ||
| ------- | ||
| data: np.ndarray | ||
| The interpolated data. | ||
| """ | ||
| if (np.any(np.logical_or(points[0] < 0, points[0] > data.shape[0])) or | ||
| np.any(np.logical_or(points[1] < 0, points[1] > data.shape[1])) or | ||
| np.any(np.logical_or(points[2] < 0,points[2] > data.shape[2]))) : | ||
| logging.warning("Careful! You are interpolating outside of boundaries " | ||
|
Contributor
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. I don't like this as it deviates from the expected behavior of the function. I would instead try to match the expected behavior of |
||
| "of your volume. Using padding to nearest value.") | ||
| return map_coordinates(data, points, order=order, mode='nearest') | ||
|
|
||
|
|
||
| class DataVolume(object): | ||
| """ | ||
| Class to access/interpolate data from nibabel object | ||
|
|
||
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.
still needed ?