-
Notifications
You must be signed in to change notification settings - Fork 316
iris.util.reverse on cubes #3155
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f48dfa5
make cube reversing official
rcomer 57d38e0
review: test conventions, etc.
rcomer f575fd2
review: enable coord specification
rcomer b34afef
add whatsnew
rcomer 60fb0f1
review: AssertRaises --> AssertRaisesRegexp
rcomer 2080d4f
cube error handling
rcomer 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
1 change: 1 addition & 0 deletions
1
docs/iris/src/whatsnew/contributions_2.2.0/newfeature_2018-Oct-03_reverse_cube.txt
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 @@ | ||
| * :func:`iris.util.reverse` can now be used to reverse a cube by specifying one or more coordinates. |
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
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,146 @@ | ||
| # (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.util.reverse`.""" | ||
|
|
||
| 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 unittest | ||
|
|
||
| import iris | ||
| from iris.util import reverse | ||
| import numpy as np | ||
|
|
||
|
|
||
| class Test_array(tests.IrisTest): | ||
| def test_simple_array(self): | ||
| a = np.arange(12).reshape(3, 4) | ||
| self.assertArrayEqual(a[::-1], reverse(a, 0)) | ||
| self.assertArrayEqual(a[::-1, ::-1], reverse(a, [0, 1])) | ||
| self.assertArrayEqual(a[:, ::-1], reverse(a, 1)) | ||
| self.assertArrayEqual(a[:, ::-1], reverse(a, [1])) | ||
| self.assertRaises(ValueError, reverse, a, []) | ||
| self.assertRaises(ValueError, reverse, a, -1) | ||
| self.assertRaises(ValueError, reverse, a, 10) | ||
| self.assertRaises(ValueError, reverse, a, [-1]) | ||
| self.assertRaises(ValueError, reverse, a, [0, -1]) | ||
| self.assertRaises(TypeError, reverse, a, 'latitude') | ||
|
|
||
| def test_single_array(self): | ||
| a = np.arange(36).reshape(3, 4, 3) | ||
| self.assertArrayEqual(a[::-1], reverse(a, 0)) | ||
| self.assertArrayEqual(a[::-1, ::-1], reverse(a, [0, 1])) | ||
| self.assertArrayEqual(a[:, ::-1, ::-1], reverse(a, [1, 2])) | ||
| self.assertArrayEqual(a[..., ::-1], reverse(a, 2)) | ||
| self.assertRaises(ValueError, reverse, a, -1) | ||
| self.assertRaises(ValueError, reverse, a, 10) | ||
| self.assertRaises(ValueError, reverse, a, [-1]) | ||
| self.assertRaises(ValueError, reverse, a, [0, -1]) | ||
| self.assertRaises(TypeError, reverse, a, 'latitude') | ||
|
|
||
|
|
||
| class Test_cube(tests.IrisTest): | ||
| def setUp(self): | ||
| # On this cube pair, the coordinates to perform operations on have | ||
| # matching long names but the points array on one cube is reversed | ||
| # with respect to that on the other. | ||
| data = np.arange(12).reshape(3, 4) | ||
| self.a1 = iris.coords.DimCoord([1, 2, 3], long_name='a') | ||
| self.b1 = iris.coords.DimCoord([1, 2, 3, 4], long_name='b') | ||
| a2 = iris.coords.DimCoord([3, 2, 1], long_name='a') | ||
| b2 = iris.coords.DimCoord([4, 3, 2, 1], long_name='b') | ||
| self.span = iris.coords.AuxCoord(np.arange(12).reshape(3, 4), | ||
| long_name='spanning') | ||
|
|
||
| self.cube1 = iris.cube.Cube( | ||
| data, dim_coords_and_dims=[(self.a1, 0), (self.b1, 1)], | ||
| aux_coords_and_dims=[(self.span, (0, 1))]) | ||
|
|
||
| self.cube2 = iris.cube.Cube( | ||
| data, dim_coords_and_dims=[(a2, 0), (b2, 1)]) | ||
|
|
||
| def test_cube_dim(self): | ||
| cube1_reverse0 = reverse(self.cube1, 0) | ||
| cube1_reverse1 = reverse(self.cube1, 1) | ||
| cube1_reverse_both = reverse(self.cube1, (0, 1)) | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[::-1], cube1_reverse0.data) | ||
| self.assertArrayEqual(self.cube2.coord('a').points, | ||
| cube1_reverse0.coord('a').points) | ||
| self.assertArrayEqual(self.cube1.coord('b').points, | ||
| cube1_reverse0.coord('b').points) | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[:, ::-1], cube1_reverse1.data) | ||
| self.assertArrayEqual(self.cube1.coord('a').points, | ||
| cube1_reverse1.coord('a').points) | ||
| self.assertArrayEqual(self.cube2.coord('b').points, | ||
| cube1_reverse1.coord('b').points) | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[::-1, ::-1], | ||
| cube1_reverse_both.data) | ||
| self.assertArrayEqual(self.cube2.coord('a').points, | ||
| cube1_reverse_both.coord('a').points) | ||
| self.assertArrayEqual(self.cube2.coord('b').points, | ||
| cube1_reverse_both.coord('b').points) | ||
|
|
||
| def test_cube_coord(self): | ||
| cube1_reverse0 = reverse(self.cube1, self.a1) | ||
| cube1_reverse1 = reverse(self.cube1, 'b') | ||
| cube1_reverse_both = reverse(self.cube1, (self.a1, self.b1)) | ||
| cube1_reverse_spanning = reverse(self.cube1, 'spanning') | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[::-1], cube1_reverse0.data) | ||
| self.assertArrayEqual(self.cube2.coord('a').points, | ||
| cube1_reverse0.coord('a').points) | ||
| self.assertArrayEqual(self.cube1.coord('b').points, | ||
| cube1_reverse0.coord('b').points) | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[:, ::-1], cube1_reverse1.data) | ||
| self.assertArrayEqual(self.cube1.coord('a').points, | ||
| cube1_reverse1.coord('a').points) | ||
| self.assertArrayEqual(self.cube2.coord('b').points, | ||
| cube1_reverse1.coord('b').points) | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[::-1, ::-1], | ||
| cube1_reverse_both.data) | ||
| self.assertArrayEqual(self.cube2.coord('a').points, | ||
| cube1_reverse_both.coord('a').points) | ||
| self.assertArrayEqual(self.cube2.coord('b').points, | ||
| cube1_reverse_both.coord('b').points) | ||
|
|
||
| self.assertArrayEqual(self.cube1.data[::-1, ::-1], | ||
| cube1_reverse_spanning.data) | ||
| self.assertArrayEqual(self.cube2.coord('a').points, | ||
| cube1_reverse_spanning.coord('a').points) | ||
| self.assertArrayEqual(self.cube2.coord('b').points, | ||
| cube1_reverse_spanning.coord('b').points) | ||
| self.assertArrayEqual( | ||
| self.span.points[::-1, ::-1], | ||
| cube1_reverse_spanning.coord('spanning').points) | ||
|
|
||
| self.assertRaises(iris.exceptions.CoordinateNotFoundError, reverse, | ||
| self.cube1, 'latitude') | ||
| self.assertRaises(ValueError, reverse, self.cube1, []) | ||
| self.assertRaises(TypeError, reverse, self.cube1, self.cube1) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.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
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.
Uh oh!
There was an error while loading. Please reload this page.