-
Notifications
You must be signed in to change notification settings - Fork 77
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
Add support for dask and zarr arrays #805
Open
ejeschke
wants to merge
15
commits into
ejeschke:main
Choose a base branch
from
naojsoft:dask-support
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 all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e1aabf3
Add support for dask and zarr arrays
ejeschke 6fb0027
fix typo in doc
ejeschke 8e6012d
Apply suggestions from code review
ejeschke d6576a5
Incorporate changes from @pllim code review
ejeschke 4522b6a
Update documentation
ejeschke 5cda4d6
Fix up tests for trcalc
ejeschke 9b6912d
Fixes for robustness of array equality checks
ejeschke 81343d3
Update tests for numpy as well
ejeschke 242d3f9
Improve efficiency of test data generation
ejeschke b061db9
fix for rebase
ejeschke daeda06
fix numpy deprecation warnings in tests
ejeschke 2ff12ae
fix numpy deprecation warning
ejeschke 2212935
reverse some fixes in tests
ejeschke 1e11861
fix typecheck tests for trcalc module
ejeschke 47b6630
adjustment for rebase
ejeschke 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
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
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,85 @@ | ||
import hashlib | ||
import numpy as np | ||
import pytest | ||
|
||
da = pytest.importorskip('dask.array') | ||
pllim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from ginga import AstroImage, trcalc | ||
from ginga.misc import log | ||
|
||
|
||
class TestDask: | ||
def setup_class(self): | ||
self.logger = log.get_logger("TestDask", null=True) | ||
|
||
def _getdata(self, shape, data_np=None): | ||
if data_np is None: | ||
data_np = np.min(np.indices(shape), axis=0) | ||
data_np = data_np.reshape(shape) | ||
data_dk = da.from_array(data_np, chunks=100) | ||
return data_dk | ||
|
||
def test_dask_slice_trcalc(self): | ||
"""Test that we can get a subslice of a dask array. | ||
pllim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
arr_dk = self._getdata((1000, 500)) | ||
|
||
x_slice, y_slice = slice(12, 499, 3), slice(10, 951, 11) | ||
view = (y_slice, x_slice) | ||
data_dk = trcalc.fancy_index(arr_dk, view) | ||
# type is preserved for slicing dask arrays | ||
assert isinstance(data_dk, da.core.Array) | ||
pllim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert data_dk.shape == (86, 163) | ||
data_np = data_dk.compute() | ||
assert isinstance(data_np[0, 0], np.integer) | ||
res = '177e1ed261ea24df277511078631ec0f95dfc3e781ac15b2d200f0f0040282ae' | ||
m = hashlib.sha256() | ||
m.update(str(data_np.tolist()).encode()) | ||
assert m.hexdigest() == res | ||
pllim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_dask_slice_aimg(self): | ||
"""Test that we can get a subslice of an AstroImage object. | ||
""" | ||
aimg = AstroImage.AstroImage(logger=self.logger) | ||
aimg.set_data(self._getdata((700, 800))) | ||
|
||
x_slice, y_slice = slice(12, 800, 8), slice(0, 700, 10) | ||
view = (y_slice, x_slice) | ||
data_np = aimg._slice(view) | ||
assert isinstance(data_np, np.ndarray) | ||
assert data_np.shape == (70, 99) | ||
assert isinstance(data_np[0, 0], np.integer) | ||
res = 'd6f0e61dc54f0c888c8f79d94ead85f8d3c4736efede289ff9946e0091960524' | ||
m = hashlib.sha256() | ||
m.update(str(data_np.tolist()).encode()) | ||
assert m.hexdigest() == res | ||
|
||
def test_dask_aimg_get_data_xy(self): | ||
"""Test that we can get a single value from an AstroImage object. | ||
""" | ||
aimg = AstroImage.AstroImage(logger=self.logger) | ||
aimg.set_data(self._getdata((5, 5), data_np=np.arange(0, 25))) | ||
|
||
val = int(aimg.get_data_xy(3, 3)) | ||
assert isinstance(val, int) | ||
assert val == 18 | ||
|
||
def test_dask_fancy_scale(self): | ||
"""Test that we can get a fancy superslice of a dask array. | ||
""" | ||
arr_dk = self._getdata((5, 5, 5)) | ||
|
||
p1 = (0, 0, 0) | ||
p2 = (5, 5, 5) | ||
new_dims = (51, 51, 51) | ||
|
||
data_np, scales = trcalc.get_scaled_cutout_wdhtdp(arr_dk, p1, p2, | ||
new_dims, | ||
logger=self.logger) | ||
assert isinstance(data_np, np.ndarray) | ||
assert data_np.shape == new_dims | ||
assert isinstance(data_np[0, 0, 0], np.integer) | ||
res = '4d6bb43463f435d76d226c38314fa22a5ba540b7db785b1ccfd2c75d84063fc4' | ||
m = hashlib.sha256() | ||
m.update(str(data_np.tolist()).encode()) | ||
assert m.hexdigest() == res |
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,79 @@ | ||
import hashlib | ||
import numpy as np | ||
|
||
from ginga import AstroImage, trcalc | ||
from ginga.misc import log | ||
|
||
|
||
class TestNumpy: | ||
def setup_class(self): | ||
self.logger = log.get_logger("TestNumpy", null=True) | ||
|
||
def _getdata(self, shape, data_np=None): | ||
if data_np is None: | ||
data_np = np.min(np.indices(shape), axis=0) | ||
data_np = data_np.reshape(shape) | ||
return data_np | ||
|
||
def test_numpy_slice_trcalc(self): | ||
"""Test that we can get a subslice of an array. | ||
""" | ||
arr_np = self._getdata((1000, 500)) | ||
|
||
x_slice, y_slice = slice(12, 499, 3), slice(10, 951, 11) | ||
view = (y_slice, x_slice) | ||
data_np = trcalc.fancy_index(arr_np, view) | ||
assert isinstance(data_np, np.ndarray) | ||
assert data_np.shape == (86, 163) | ||
pllim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert isinstance(data_np[0, 0], np.integer) | ||
res = '177e1ed261ea24df277511078631ec0f95dfc3e781ac15b2d200f0f0040282ae' | ||
m = hashlib.sha256() | ||
m.update(str(data_np.tolist()).encode()) | ||
assert m.hexdigest() == res | ||
|
||
def test_numpy_slice_aimg(self): | ||
"""Test that we can get a subslice of an AstroImage object. | ||
""" | ||
aimg = AstroImage.AstroImage(logger=self.logger) | ||
aimg.set_data(self._getdata((700, 800))) | ||
|
||
x_slice, y_slice = slice(12, 800, 8), slice(0, 700, 10) | ||
view = (y_slice, x_slice) | ||
data_np = aimg._slice(view) | ||
assert isinstance(data_np, np.ndarray) | ||
assert data_np.shape == (70, 99) | ||
assert isinstance(data_np[0, 0], np.integer) | ||
res = 'd6f0e61dc54f0c888c8f79d94ead85f8d3c4736efede289ff9946e0091960524' | ||
m = hashlib.sha256() | ||
m.update(str(data_np.tolist()).encode()) | ||
assert m.hexdigest() == res | ||
|
||
def test_numpy_aimg_getdata_xy(self): | ||
"""Test that we can get a single value from an AstroImage object. | ||
""" | ||
aimg = AstroImage.AstroImage(logger=self.logger) | ||
aimg.set_data(self._getdata((5, 5), data_np=np.arange(0, 25))) | ||
|
||
val = int(aimg.get_data_xy(3, 3)) | ||
assert isinstance(val, int) | ||
assert val == 18 | ||
|
||
def test_numpy_fancy_scale(self): | ||
"""Test that we can get a fancy superslice of a numpy array. | ||
""" | ||
arr_np = self._getdata((5, 5, 5)) | ||
|
||
p1 = (0, 0, 0) | ||
p2 = (5, 5, 5) | ||
new_dims = (51, 51, 51) | ||
|
||
data_np, scales = trcalc.get_scaled_cutout_wdhtdp(arr_np, p1, p2, | ||
new_dims, | ||
logger=self.logger) | ||
assert isinstance(data_np, np.ndarray) | ||
assert data_np.shape == new_dims | ||
assert isinstance(data_np[0, 0, 0], np.integer) | ||
res = '4d6bb43463f435d76d226c38314fa22a5ba540b7db785b1ccfd2c75d84063fc4' | ||
m = hashlib.sha256() | ||
m.update(str(data_np.tolist()).encode()) | ||
assert m.hexdigest() == res |
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 |
---|---|---|
@@ -1,81 +1,60 @@ | ||
|
||
import hashlib | ||
import numpy as np | ||
|
||
from ginga import trcalc | ||
|
||
|
||
class TestTrcalc: | ||
|
||
def _2ddata(self): | ||
data = np.zeros((10, 10), dtype=int) | ||
for i in range(10): | ||
for j in range(10): | ||
data[i, j] = min(i, j) | ||
return data | ||
|
||
def _3ddata(self): | ||
data = np.zeros((10, 10, 10), dtype=int) | ||
for i in range(10): | ||
for j in range(10): | ||
for k in range(10): | ||
data[i, j, k] = min(i, j, k) | ||
return data | ||
def _2ddata(self, shape, data_np=None): | ||
if data_np is None: | ||
data_np = np.asarray([min(i, j) | ||
for i in range(shape[0]) | ||
for j in range(shape[1])]) | ||
data_np = data_np.reshape(shape) | ||
return data_np | ||
|
||
def _3ddata(self, shape, data_np=None): | ||
if data_np is None: | ||
data_np = np.asarray([min(i, j, k) | ||
for i in range(shape[0]) | ||
for j in range(shape[1]) | ||
for k in range(shape[2])]) | ||
data_np = data_np.reshape(shape) | ||
return data_np | ||
|
||
def test_get_scaled_cutout_wdht_view(self): | ||
|
||
data = self._2ddata() | ||
data = self._2ddata((10, 10)) | ||
p1 = (2, 2) | ||
p2 = (4, 4) | ||
nd = (8, 10) | ||
|
||
res = np.asarray([[2, 2, 2, 2, 2, 2, 2, 2], | ||
[2, 2, 2, 2, 2, 2, 2, 2], | ||
[2, 2, 2, 2, 2, 2, 2, 2], | ||
[2, 2, 2, 2, 2, 2, 2, 2], | ||
[2, 2, 2, 3, 3, 3, 3, 3], | ||
[2, 2, 2, 3, 3, 3, 3, 3], | ||
[2, 2, 2, 3, 3, 3, 3, 3], | ||
[2, 2, 2, 3, 3, 3, 4, 4], | ||
[2, 2, 2, 3, 3, 3, 4, 4], | ||
[2, 2, 2, 3, 3, 3, 4, 4]]) | ||
|
||
view, scales = trcalc.get_scaled_cutout_wdht_view(data.shape, | ||
p1[0], p1[1], | ||
p2[0], p2[1], | ||
nd[0], nd[1]) | ||
new_data = data[view] | ||
new_data = trcalc.fancy_index(data, view) | ||
assert new_data.shape == (10, 8) | ||
assert np.allclose(new_data, res) | ||
assert isinstance(new_data[0, 0], np.integer) | ||
res = 'dc025d4e14db5529c581cbe24f0616721bb33f63aabcfcc0d432edf00d8cdc2d' | ||
m = hashlib.sha256() | ||
m.update(str(new_data.tolist()).encode()) | ||
assert m.hexdigest() == res | ||
|
||
def test_get_scaled_cutout_wdhtdp_view(self): | ||
|
||
data = self._3ddata() | ||
data = self._3ddata((10, 10, 10)) | ||
p1 = (0, 0, 0) | ||
p2 = (9, 9, 9) | ||
nd = (4, 4, 4) | ||
|
||
res = np.asarray([[[0, 0, 0, 0], | ||
[0, 0, 0, 0], | ||
[0, 0, 0, 0], | ||
[0, 0, 0, 0]], | ||
|
||
[[0, 0, 0, 0], | ||
[0, 2, 2, 2], | ||
[0, 2, 2, 2], | ||
[0, 2, 2, 2]], | ||
|
||
[[0, 0, 0, 0], | ||
[0, 2, 2, 2], | ||
[0, 2, 5, 5], | ||
[0, 2, 5, 5]], | ||
|
||
[[0, 0, 0, 0], | ||
[0, 2, 2, 2], | ||
[0, 2, 5, 5], | ||
[0, 2, 5, 7]]]) | ||
|
||
view, scales = trcalc.get_scaled_cutout_wdhtdp_view(data.shape, | ||
p1, p2, nd) | ||
new_data = data[view] | ||
new_data = trcalc.fancy_index(data, view) | ||
assert new_data.shape == (4, 4, 4) | ||
assert np.allclose(new_data, res) | ||
assert isinstance(new_data[0, 0, 0], np.integer) | ||
res = 'c01c00af06fb2dc5c8cd6cf96927ba6ddd8d2caba3fc33074c9eaab5cc0ac498' | ||
m = hashlib.sha256() | ||
m.update(str(new_data.tolist()).encode()) | ||
assert m.hexdigest() == res |
Oops, something went wrong.
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.
I just learned from @Cadair that providing a
name=
is very important, so thatdask
doesn't try to generate a name from data hash and eats up all the computing time.