-
-
Notifications
You must be signed in to change notification settings - Fork 47
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
Implement support for ndimage.map_coordinates #237
Open
m-albert
wants to merge
10
commits into
dask:main
Choose a base branch
from
m-albert:map_coords
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
47cb0bb
Add dask_image.ndinterp.map_coordinates + some tests.
m-albert 42be4a8
Tick function in coverage doc
m-albert 4c00976
Added comments explaining the steps taken. Increased verbosity. Simpl…
m-albert 9b8b057
Extended `map_coordinates` to work also on
m-albert 22cedf9
Rename image to input
m-albert e42f6af
Rename image to input consistently.
m-albert 431de07
Added tests for different combinations of numpy/dask inputs
m-albert e46dc31
Vectorized coordinate-to-chunk association
m-albert 85331a2
Improve comments and refactor
m-albert ec29c42
Merge branch 'main' into map_coords
jakirkham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
tests/test_dask_image/test_ndinterp/test_map_coordinates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
from packaging import version | ||
|
||
import dask.array as da | ||
import numpy as np | ||
import pytest | ||
import scipy | ||
import scipy.ndimage | ||
|
||
import dask_image.ndinterp | ||
|
||
# mode lists for the case with prefilter = False | ||
_supported_modes = ['constant', 'nearest'] | ||
_unsupported_modes = ['wrap', 'reflect', 'mirror'] | ||
|
||
# mode lists for the case with prefilter = True | ||
_supported_prefilter_modes = ['constant'] | ||
_unsupported_prefilter_modes = _unsupported_modes + ['nearest'] | ||
|
||
have_scipy16 = version.parse(scipy.__version__) >= version.parse('1.6.0') | ||
|
||
# additional modes are present in SciPy >= 1.6.0 | ||
if have_scipy16: | ||
_supported_modes += ['grid-constant'] | ||
_unsupported_modes += ['grid-mirror', 'grid-wrap'] | ||
_unsupported_prefilter_modes += ['grid-constant', 'grid-mirror', | ||
'grid-wrap'] | ||
|
||
|
||
def validate_map_coordinates_general(n=2, | ||
interp_order=1, | ||
interp_mode='constant', | ||
coord_len=12, | ||
coord_chunksize=6, | ||
coord_offset=0., | ||
im_shape_per_dim=12, | ||
im_chunksize_per_dim=6, | ||
random_seed=0, | ||
prefilter=False, | ||
): | ||
|
||
if interp_order > 1 and interp_mode == 'nearest' and not have_scipy16: | ||
# not clear on the underlying cause, but this fails on older SciPy | ||
pytest.skip("requires SciPy >= 1.6.0") | ||
|
||
# define test image | ||
np.random.seed(random_seed) | ||
image = np.random.random([im_shape_per_dim] * n) | ||
image_da = da.from_array(image, chunks=im_chunksize_per_dim) | ||
|
||
# define test coordinates | ||
coords = np.random.random((n, coord_len)) * im_shape_per_dim + coord_offset | ||
|
||
# ndimage result | ||
ints_scipy = scipy.ndimage.map_coordinates( | ||
image, | ||
coords, | ||
output=None, | ||
order=interp_order, | ||
mode=interp_mode, | ||
cval=0.0, | ||
prefilter=prefilter) | ||
|
||
# dask-image result | ||
ints_dask = dask_image.ndinterp.map_coordinates( | ||
image_da, | ||
coords, | ||
order=interp_order, | ||
mode=interp_mode, | ||
cval=0.0, | ||
prefilter=prefilter) | ||
|
||
ints_dask_computed = ints_dask.compute() | ||
|
||
assert np.allclose(ints_scipy, ints_dask_computed) | ||
|
||
|
||
@pytest.mark.parametrize("n", | ||
[1, 2, 3, 4]) | ||
@pytest.mark.parametrize("random_seed", | ||
range(2)) | ||
def test_map_coordinates_basic(n, | ||
random_seed, | ||
): | ||
|
||
kwargs = dict() | ||
kwargs['n'] = n | ||
kwargs['random_seed'] = random_seed | ||
|
||
validate_map_coordinates_general(**kwargs) | ||
|
||
|
||
@pytest.mark.timeout(3) | ||
def test_map_coordinates_large_input(): | ||
|
||
# define large test image | ||
image_da = da.random.random([1000] * 3, chunks=200) | ||
|
||
# define sparse test coordinates | ||
coords = np.random.random((3, 2)) * 1000 | ||
|
||
# dask-image result | ||
dask_image.ndinterp.map_coordinates( | ||
image_da, | ||
coords).compute() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This currently assumes that coordinates is a small enough array that it will fit in memory, but what if both
image
andcoordinates
are large arrays and would both not fit in memory? Or is it unlikely that this would be the case?