Skip to content

Commit

Permalink
Make slices_over tests go faster (#5973)
Browse files Browse the repository at this point in the history
* Make slices_over tests go faster

* url to slices_over

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>

* update comments

---------

Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com>
  • Loading branch information
rcomer and trexfeathers authored May 30, 2024
1 parent 3986950 commit 18ff0f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
10 changes: 6 additions & 4 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ This document explains the changes made to Iris for this release
#. `@pp-mo`_ reworked benchmark peak-memory measurement to use the
`tracemalloc <https://docs.python.org/3.12/library/tracemalloc.html>`_
package.
(:pull: `5948`)
(:pull:`5948`)

#. `@pp-mo`_ added a benchmark 'trialrun' sub-command, to quickly test
benchmarks during development. (:pull: `5957`)
benchmarks during development. (:pull:`5957`)

#. `@pp-mo`_ moved several memory-measurement benchmarks from 'on-demand' to
the standard set, in hopes that use of 'tracemalloc' (:pull: `5948`) makes
the standard set, in hopes that use of 'tracemalloc' (:pull:`5948`) makes
the results consistent enough to monitor for performance changes.
(:pull: `5959`)
(:pull:`5959`)

#. `@rcomer`_ made some :meth:`~iris.cube.Cube.slices_over` tests go faster (:pull:`5973`)


.. comment
Expand Down
14 changes: 7 additions & 7 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,10 +1051,10 @@ def test_all_permutations(self):
@tests.skip_data
class Test_slices_over(tests.IrisTest):
def setUp(self):
self.cube = stock.realistic_4d()
self.cube = stock.realistic_4d()[:, :7, :10, :10]
# Define expected iterators for 1D and 2D test cases.
self.exp_iter_1d = range(len(self.cube.coord("model_level_number").points))
self.exp_iter_2d = np.ndindex(6, 70, 1, 1)
self.exp_iter_2d = np.ndindex(6, 7, 1, 1)
# Define maximum number of interactions for particularly long
# (and so time-consuming) iterators.
self.long_iterator_max = 5
Expand Down Expand Up @@ -1090,7 +1090,7 @@ def test_1d_slice_nonexistent_dimension_given(self):
_ = self.cube.slices_over(self.cube.ndim + 1)

def test_2d_slice_coord_given(self):
# Slicing over these two dimensions returns 420 2D cubes, so only check
# Slicing over these two dimensions returns 42 2D cubes, so only check
# cubes up to `self.long_iterator_max` to keep test runtime sensible.
res = self.cube.slices_over(
[self.cube.coord("time"), self.cube.coord("model_level_number")]
Expand All @@ -1109,7 +1109,7 @@ def test_2d_slice_nonexistent_coord_given(self):
)

def test_2d_slice_coord_name_given(self):
# Slicing over these two dimensions returns 420 2D cubes, so only check
# Slicing over these two dimensions returns 42 2D cubes, so only check
# cubes up to `self.long_iterator_max` to keep test runtime sensible.
res = self.cube.slices_over(["time", "model_level_number"])
for ct in range(self.long_iterator_max):
Expand All @@ -1124,7 +1124,7 @@ def test_2d_slice_nonexistent_coord_name_given(self):
_ = self.cube.slices_over(["time", "wibble"])

def test_2d_slice_dimension_given(self):
# Slicing over these two dimensions returns 420 2D cubes, so only check
# Slicing over these two dimensions returns 42 2D cubes, so only check
# cubes up to `self.long_iterator_max` to keep test runtime sensible.
res = self.cube.slices_over([0, 1])
for ct in range(self.long_iterator_max):
Expand All @@ -1150,11 +1150,11 @@ def test_2d_slice_nonexistent_dimension_given(self):
_ = self.cube.slices_over([0, self.cube.ndim + 1])

def test_multidim_slice_coord_given(self):
# Slicing over surface altitude returns 100x100 2D cubes, so only check
# Slicing over surface altitude returns 10x10 2D cubes, so only check
# cubes up to `self.long_iterator_max` to keep test runtime sensible.
res = self.cube.slices_over("surface_altitude")
# Define special ndindex iterator for the different dims sliced over.
nditer = np.ndindex(1, 1, 100, 100)
nditer = np.ndindex(1, 1, 10, 10)
for ct in range(self.long_iterator_max):
indices = list(next(nditer))
# Replace the dimensions not iterated over with spanning slices.
Expand Down

0 comments on commit 18ff0f8

Please sign in to comment.