-
Notifications
You must be signed in to change notification settings - Fork 300
Generic lazy data handling. #2356
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ numpy | |
| pyke | ||
| udunits2 | ||
| cf_units | ||
| dask | ||
|
|
||
| # Iris build dependencies | ||
| setuptools | ||
|
|
||
This file contains hidden or 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,71 @@ | ||
| # (C) British Crown Copyright 2017, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Routines for lazy data handling. | ||
|
|
||
| To avoid replicating implementation-dependent test and conversion code. | ||
|
|
||
| """ | ||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa | ||
|
|
||
| import dask.array as da | ||
|
|
||
|
|
||
| def is_lazy_data(data): | ||
| """ | ||
| Return whether the argument is an Iris 'lazy' data array. | ||
|
|
||
| At present, this means simply a Dask array. | ||
| We determine this by checking for a "compute" property. | ||
|
|
||
| """ | ||
| return hasattr(data, 'compute') | ||
|
|
||
|
|
||
| def as_concrete_data(data): | ||
| """ | ||
| Return the actual content of the argument, as a numpy array. | ||
|
|
||
| If lazy, return the realised data, otherwise return the argument unchanged. | ||
|
|
||
| """ | ||
| if is_lazy_data(data): | ||
| data = data.compute() | ||
| return data | ||
|
|
||
|
|
||
| # A magic value, borrowed from biggus | ||
| _MAX_CHUNK_SIZE = 8 * 1024 * 1024 * 2 | ||
|
|
||
|
|
||
| def as_lazy_data(data): | ||
| """ | ||
| Return a lazy equivalent of the argument, as a lazy array. | ||
|
|
||
| For an existing dask array, return it unchanged. | ||
| Otherwise, return the argument wrapped with dask.array.from_array. | ||
| This assumes the underlying object has numpy-array-like properties. | ||
|
|
||
| .. Note:: | ||
|
|
||
| For now at least, chunksize is set to an arbitrary fixed value. | ||
|
|
||
| """ | ||
| if not is_lazy_data(data): | ||
| data = da.from_array(data, chunks=_MAX_CHUNK_SIZE) | ||
| return data | ||
This file contains hidden or 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,20 @@ | ||
| # (C) British Crown Copyright 2017, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """Unit tests for the :mod:`iris._lazy_data` module.""" | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa |
This file contains hidden or 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,48 @@ | ||
| # (C) British Crown Copyright 2017, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """Test :meth:`iris._lazy data.as_concrete_data` method.""" | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
| import numpy as np | ||
| import dask.array as da | ||
|
|
||
| from iris._lazy_data import is_lazy_data, as_concrete_data | ||
|
|
||
|
|
||
| class Test_as_concrete_data(tests.IrisTest): | ||
| def test_lazy(self): | ||
| lazy_values = np.arange(30).reshape((2, 5, 3)) | ||
| lazy_array = da.from_array(lazy_values, 1e6) | ||
| result = as_concrete_data(lazy_array) | ||
| self.assertFalse(is_lazy_data(result)) | ||
| self.assertArrayAllClose(result, lazy_values) | ||
|
|
||
| def test_real(self): | ||
| real_array = np.arange(24).reshape((2, 3, 4)) | ||
| result = as_concrete_data(real_array) | ||
| self.assertFalse(is_lazy_data(result)) | ||
| self.assertIs(result, real_array) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() |
This file contains hidden or 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,50 @@ | ||
| # (C) British Crown Copyright 2017, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """Test :meth:`iris._lazy data.as_lazy_data` method.""" | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
|
|
||
| import numpy as np | ||
| import dask.array as da | ||
|
|
||
| from iris._lazy_data import as_lazy_data, as_concrete_data, is_lazy_data | ||
|
|
||
|
|
||
| class Test_as_lazy_data(tests.IrisTest): | ||
| def test_lazy(self): | ||
| lazy_values = np.arange(30).reshape((2, 5, 3)) | ||
| lazy_array = da.from_array(lazy_values, 1e6) | ||
| result = as_lazy_data(lazy_array) | ||
| self.assertTrue(is_lazy_data(result)) | ||
| self.assertIs(result, lazy_array) | ||
|
|
||
| def test_real(self): | ||
| real_array = np.arange(24).reshape((2, 3, 4)) | ||
| result = as_lazy_data(real_array) | ||
| self.assertTrue(is_lazy_data(result)) | ||
| self.assertArrayAllClose(as_concrete_data(result), | ||
| real_array) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() |
This file contains hidden or 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,44 @@ | ||
| # (C) British Crown Copyright 2017, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """Test :meth:`iris._lazy data.is_lazy_data` method.""" | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
| import numpy as np | ||
| import dask.array as da | ||
|
|
||
| from iris._lazy_data import is_lazy_data | ||
|
|
||
|
|
||
| class Test_is_lazy_data(tests.IrisTest): | ||
| def test_lazy(self): | ||
| lazy_values = np.arange(30).reshape((2, 5, 3)) | ||
| lazy_array = da.from_array(lazy_values, 1e6) | ||
| self.assertTrue(is_lazy_data(lazy_array)) | ||
|
|
||
| def test_real(self): | ||
| real_array = np.arange(24).reshape((2, 3, 4)) | ||
| self.assertFalse(is_lazy_data(real_array)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ numpy | |
| pyke | ||
| udunits2 | ||
| cf_units | ||
| dask | ||
|
|
||
| # Iris build dependencies | ||
| setuptools | ||
|
|
||
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 would rather this was not part of the module doc string; i understand the sentiment, but it doesn't seem like useful long term documentation.
I would prefer
or something similarly positive
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 see what you mean.
However, this module is not particularly tied to the cube.data interface as such.
Could we have something like "This module provides lazy array handling, independent of a specific implementation such as dask or biggus." @marqh ?
I'm not so keen on the specific namechecking of dask/biggus, but I think that makes it much clearer what we are actually talking about.