From fd04aa8fcd1bde049653d1f52459751e94b288f7 Mon Sep 17 00:00:00 2001 From: cpelley Date: Mon, 25 Apr 2016 14:52:42 +0100 Subject: [PATCH 1/4] ENH: Lazy cube data transpose --- lib/iris/cube.py | 5 +---- lib/iris/tests/unit/cube/test_Cube.py | 9 +++++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 0fa492353e..d7d488c429 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2806,10 +2806,7 @@ def transpose(self, new_order=None): elif len(new_order) != self.data.ndim: raise ValueError('Incorrect number of dimensions.') - # The data needs to be copied, otherwise this view of the transposed - # data will not be contiguous. Ensure not to assign via the cube.data - # setter property since we are reshaping the cube payload in-place. - self._my_data = np.transpose(self.data, new_order).copy() + self._my_data = self.lazy_data().transpose(new_order) dim_mapping = {src: dest for dest, src in enumerate(new_order)} diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index f68214e4f2..f7d48319b0 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -1410,5 +1410,14 @@ def test_fail_cell_measure_dims(self): cm_dims = self.cube.cell_measure_dims(a_cell_measure) +class Test_transpose(tests.IrisTest): + def test_lazy_data(self): + data = np.arange(12).reshape(3, 4) + cube = Cube(biggus.NumpyArrayAdapter(data)) + cube.transpose() + self.assertTrue(cube.has_lazy_data()) + self.assertArrayEqual(data.T, cube.data) + + if __name__ == '__main__': tests.main() From 08de76fa14033d948b27a4425035783d662bb846 Mon Sep 17 00:00:00 2001 From: cpelley Date: Fri, 20 May 2016 11:15:26 +0100 Subject: [PATCH 2/4] DOC: Whatsnew --- .../newfeature_2016-May-20_lazy_cube_transpose.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt diff --git a/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt b/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt new file mode 100644 index 0000000000..f368a52054 --- /dev/null +++ b/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt @@ -0,0 +1 @@ +* The transpose method of a Cube now results in a transposed view of the original data rather than a transposed copy. From c1ca711c71d3911ec5f31db25a3969573904f1e4 Mon Sep 17 00:00:00 2001 From: Carwyn Pelley Date: Fri, 10 Jun 2016 09:52:24 +0100 Subject: [PATCH 3/4] DOC: Updated whatsnew wording --- .../newfeature_2016-May-20_lazy_cube_transpose.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt b/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt index f368a52054..79e95711bd 100644 --- a/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt +++ b/docs/iris/src/whatsnew/contributions_1.10/newfeature_2016-May-20_lazy_cube_transpose.txt @@ -1 +1 @@ -* The transpose method of a Cube now results in a transposed view of the original data rather than a transposed copy. +* The transpose method of a Cube now results in a lazy transposed view of the original rather than realising the data then transposing it. From 5eef7324cf9922f435ce60aade431c0f8408427c Mon Sep 17 00:00:00 2001 From: Carwyn Pelley Date: Tue, 9 Aug 2016 15:00:55 +0100 Subject: [PATCH 4/4] MAINT: Changes based on review --- lib/iris/cube.py | 9 ++++++--- lib/iris/tests/unit/cube/test_Cube.py | 8 ++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index d7d488c429..c1853896a5 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2802,11 +2802,14 @@ def transpose(self, new_order=None): """ if new_order is None: - new_order = np.arange(self.data.ndim)[::-1] - elif len(new_order) != self.data.ndim: + new_order = np.arange(self.ndim)[::-1] + elif len(new_order) != self.ndim: raise ValueError('Incorrect number of dimensions.') - self._my_data = self.lazy_data().transpose(new_order) + if self.has_lazy_data(): + self._my_data = self.lazy_data().transpose(new_order) + else: + self._my_data = self.data.transpose(new_order) dim_mapping = {src: dest for dest, src in enumerate(new_order)} diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index f7d48319b0..fb31d46950 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -1418,6 +1418,14 @@ def test_lazy_data(self): self.assertTrue(cube.has_lazy_data()) self.assertArrayEqual(data.T, cube.data) + def test_not_lazy_data(self): + data = np.arange(12).reshape(3, 4) + cube = Cube(data) + cube.transpose() + self.assertFalse(cube.has_lazy_data()) + self.assertIs(data.base, cube.data.base) + self.assertArrayEqual(data.T, cube.data) + if __name__ == '__main__': tests.main()