-
Notifications
You must be signed in to change notification settings - Fork 286
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 cube co-realisation. #2967
Merged
Merged
Add cube co-realisation. #2967
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
13bdf2e
Add cube co-realisation.
pp-mo 499972c
Add tests, common code for masked constant conversion.
pp-mo 926d2db
Added whatsnew for co_realise_cubes.
pp-mo 4f2b226
Fix to ensure all dm.data results are ndarrays.
pp-mo 6345c29
Review changes; comments and docstrings review; no return values.
pp-mo d508335
Fix docstring.
pp-mo 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
3 changes: 3 additions & 0 deletions
3
docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-Mar-08_co_realise_cubes.txt
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,3 @@ | ||
* Added new function :func:`iris.co_realise_cubes` to compute multiple lazy | ||
values in a single operation, avoiding repeated re-loading of data or | ||
re-calculation of expressions. |
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,86 @@ | ||
# (C) British Crown Copyright 2018, 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 function :func:`iris._lazy data.co_realise_cubes`.""" | ||
|
||
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 | ||
|
||
from mock import MagicMock | ||
import numpy as np | ||
|
||
from iris.cube import Cube | ||
from iris._lazy_data import as_lazy_data | ||
|
||
from iris._lazy_data import co_realise_cubes | ||
|
||
|
||
class ArrayAccessCounter(object): | ||
def __init__(self, array): | ||
self.dtype = array.dtype | ||
self.shape = array.shape | ||
self._array = array | ||
self.access_count = 0 | ||
|
||
def __getitem__(self, keys): | ||
self.access_count += 1 | ||
return self._array[keys] | ||
|
||
|
||
class Test_co_realise_cubes(tests.IrisTest): | ||
def test_empty(self): | ||
# Ensure that 'no args' case does not raise an error. | ||
co_realise_cubes() | ||
|
||
def test_basic(self): | ||
real_data = np.arange(3.) | ||
cube = Cube(as_lazy_data(real_data)) | ||
co_realise_cubes(cube) | ||
self.assertFalse(cube.has_lazy_data()) | ||
self.assertArrayAllClose(cube.core_data(), real_data) | ||
|
||
def test_multi(self): | ||
real_data = np.arange(3.) | ||
cube_base = Cube(as_lazy_data(real_data)) | ||
cube_inner = cube_base + 1 | ||
result_a = cube_base + 1 | ||
result_b = cube_inner + 1 | ||
co_realise_cubes(result_a, result_b) | ||
# Check that target cubes were realised. | ||
self.assertFalse(result_a.has_lazy_data()) | ||
self.assertFalse(result_b.has_lazy_data()) | ||
# Check that other cubes referenced remain lazy. | ||
self.assertTrue(cube_base.has_lazy_data()) | ||
self.assertTrue(cube_inner.has_lazy_data()) | ||
|
||
def test_combined_access(self): | ||
wrapped_array = ArrayAccessCounter(np.arange(3.)) | ||
lazy_array = as_lazy_data(wrapped_array) | ||
derived_a = lazy_array + 1 | ||
derived_b = lazy_array + 2 | ||
cube_a = Cube(derived_a) | ||
cube_b = Cube(derived_b) | ||
co_realise_cubes(cube_a, cube_b) | ||
# Though used twice, the source data should only get fetched once. | ||
self.assertEqual(wrapped_array.access_count, 1) | ||
|
||
|
||
if __name__ == '__main__': | ||
tests.main() |
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.
Could combine this line with line 30.Actually I guess you're following the convention that the method being tested is imported on its own line?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.
👍