Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* The transpose method of a Cube now results in a lazy transposed view of the original rather than realising the data then transposing it.
12 changes: 6 additions & 6 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2802,14 +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.')

# 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()
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)}

Expand Down
17 changes: 17 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,5 +1410,22 @@ 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)

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()