-
Notifications
You must be signed in to change notification settings - Fork 316
Tests for indexing meshes #7218
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
trexfeathers
merged 9 commits into
SciTools:FEATURE_index_set
from
pt331:tests_for_indexing_meshes
Jul 24, 2026
+248
−23
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bbc9535
Barebones tests
pt331 20baa58
assertion fix and allowing for single value index
pt331 7bd1809
fixed tests/stock/mesh topology_dimension
pt331 83815c0
Update as_mesh to deal with locations not existing
pt331 175832d
Copied as_mesh changes from unit tests branch
pt331 8a33405
Applied equality changes offered by @trexfeathers
pt331 f8fc84b
Remove cube_mesh_node from test_subset_indexing_new_mesh test
pt331 7f11908
updating to full tests - test_subset_indexing_mesh_index_set fails now
pt331 a2dc89a
Fixed looking at the wrong part of cube and _MeshIndexSet test
pt331 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
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
165 changes: 165 additions & 0 deletions
165
lib/iris/tests/integration/mesh/test_indexing_meshes.py
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,165 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the BSD license. | ||
| # See LICENSE in the root of the repository for full licensing details. | ||
|
|
||
| import pytest | ||
|
|
||
| from iris.coords import AuxCoord, Coord | ||
| from iris.cube import Cube | ||
| from iris.experimental import mesh_coord_indexing | ||
| from iris.loading import load_cube | ||
| from iris.mesh.components import Mesh, MeshCoord, _MeshIndexSet | ||
| from iris.tests import _shared_utils | ||
| from iris.tests.stock.mesh import sample_mesh, sample_mesh_cube | ||
|
|
||
| # using a cube with a mesh from file and building one from scratch (an example for each location): | ||
| # Index the cube to get back the auxcoord, meshxy, meshindexset (by using the setting) | ||
| # Modifications to the original mesh and check and if they are reflected | ||
| # test by looking at meshcoord and comparing | ||
| # something with as_mesh | ||
| # check that a change is not reflected | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cube_mesh_from_file(): | ||
| file_path = _shared_utils.get_data_path( | ||
| [ | ||
| "NetCDF", | ||
| "unstructured_grid", | ||
| "lfric_ngvat_2D_72t_face_half_levels_main_conv_rain.nc", | ||
| ] | ||
| ) | ||
| return (load_cube(file_path, "conv_rain"), "face") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cube_mesh_node(): | ||
| location = "node" | ||
| mesh = sample_mesh(n_nodes=15, n_edges=3, n_faces=3) # Cannot have a node only mesh | ||
| return (sample_mesh_cube(location=location, mesh=mesh), location) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cube_mesh_edge(): | ||
| location = "edge" | ||
| mesh = sample_mesh(n_nodes=15, n_edges=3, n_faces=0) | ||
| return (sample_mesh_cube(location=location, mesh=mesh), location) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def cube_mesh_face(): | ||
| location = "face" | ||
| mesh = sample_mesh(n_nodes=15, n_edges=0, n_faces=3) | ||
| return (sample_mesh_cube(location=location, mesh=mesh), location) | ||
|
|
||
|
|
||
| def change_and_assert_no_change_in_right(left: Coord, right: Coord, value: int): | ||
| left.points[0] = value | ||
| assert right.points[0] != value | ||
|
|
||
|
|
||
| def change_and_assert_change_in_right(left: Coord, right: Coord, value: int): | ||
| left.points[0] = value | ||
| assert right.points[0] == value | ||
|
|
||
|
|
||
| def assert_change_raises_exception(mesh_coord: MeshCoord): | ||
| assert mesh_coord.points | ||
| with pytest.raises(Exception, match="test"): | ||
| mesh_coord.points[0] = 0 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "fixture", | ||
| ["cube_mesh_from_file", "cube_mesh_node", "cube_mesh_edge", "cube_mesh_face"], | ||
| ) | ||
| def test_subset_indexing_auxcoord(fixture, request): | ||
| (cube, _) = request.getfixturevalue(fixture) | ||
| with mesh_coord_indexing.SETTING.context(mesh_coord_indexing.Options.AUX_COORD): | ||
| indexed_cube = cube[0, 0:1] | ||
|
|
||
| indexed_lat = indexed_cube.coord(standard_name="latitude") | ||
| indexed_lon = indexed_cube.coord(standard_name="longitude") | ||
| # The mesh's lat/lon should be represented as AuxCoord | ||
| assert isinstance(indexed_lat, AuxCoord) | ||
| assert isinstance(indexed_lon, AuxCoord) | ||
| # And no mesh in cube | ||
| assert indexed_cube.mesh is None | ||
|
|
||
| original_lat = cube.coord(standard_name="latitude") | ||
| original_lon = cube.coord(standard_name="longitude") | ||
|
pt331 marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Any changes to the indexed cube's mesh should not be reflected in the original | ||
| value = 9999 | ||
| change_and_assert_no_change_in_right(indexed_lat, original_lat, value) | ||
| change_and_assert_no_change_in_right(indexed_lon, original_lon, value) | ||
| # and vice versa | ||
| value = -9999 | ||
| change_and_assert_no_change_in_right(original_lat, indexed_lat, value) | ||
| change_and_assert_no_change_in_right(original_lon, indexed_lon, value) | ||
|
|
||
|
|
||
| # Excluding "cube_mesh_node" from this test because it is an invalid thing to do | ||
| # Test for the raised exception exists in unit tests | ||
| @pytest.mark.parametrize( | ||
| "fixture", | ||
| ["cube_mesh_from_file", "cube_mesh_edge", "cube_mesh_face"], | ||
| ) | ||
| def test_subset_indexing_new_mesh(fixture, request): | ||
| cube: Cube | ||
| (cube, _) = request.getfixturevalue(fixture) | ||
|
|
||
| with mesh_coord_indexing.SETTING.context(mesh_coord_indexing.Options.NEW_MESH): | ||
| indexed_cube = cube[0, 0:1] | ||
| # The mesh's lat/lon should be represented as MeshCoord with a Mesh as the mesh | ||
| indexed_lat = indexed_cube.coord(standard_name="latitude") | ||
| indexed_lon = indexed_cube.coord(standard_name="longitude") | ||
| assert isinstance(indexed_lat, MeshCoord) | ||
| assert isinstance(indexed_lon, MeshCoord) | ||
| assert isinstance(indexed_lat.mesh, Mesh) | ||
| assert isinstance(indexed_lon.mesh, Mesh) | ||
|
pt331 marked this conversation as resolved.
Outdated
|
||
|
|
||
| original_lat = cube.coord(standard_name="latitude") | ||
| original_lon = cube.coord(standard_name="longitude") | ||
|
pt331 marked this conversation as resolved.
Outdated
|
||
| # Any changes to the indexed cube's mesh should not be reflected in the original | ||
|
pt331 marked this conversation as resolved.
|
||
| value = 9999 | ||
| change_and_assert_no_change_in_right(indexed_lat, original_lat, value) | ||
| change_and_assert_no_change_in_right(indexed_lon, original_lon, value) | ||
| # and vice versa | ||
| value = -9999 | ||
| change_and_assert_no_change_in_right(original_lat, indexed_lat, value) | ||
| change_and_assert_no_change_in_right(original_lon, indexed_lon, value) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "fixture", | ||
| ["cube_mesh_from_file", "cube_mesh_node", "cube_mesh_edge", "cube_mesh_face"], | ||
| ) | ||
| def test_subset_indexing_mesh_index_set(fixture, request): | ||
| (cube, _) = request.getfixturevalue(fixture) | ||
| with mesh_coord_indexing.SETTING.context( | ||
| mesh_coord_indexing.Options.MESH_INDEX_SET | ||
| ): | ||
| indexed_cube = cube[0, 0:1] | ||
| # The mesh's lat/lon should be represented as MeshCoord, | ||
| # with a _MeshIndexSet as the mesh | ||
| indexed_lat = indexed_cube.coord(standard_name="latitude") | ||
| indexed_lon = indexed_cube.coord(standard_name="longitude") | ||
| assert isinstance(indexed_lat, MeshCoord) | ||
| assert isinstance(indexed_lon, MeshCoord) | ||
| assert isinstance(indexed_lat.mesh, _MeshIndexSet) | ||
| assert isinstance(indexed_lon.mesh, _MeshIndexSet) | ||
|
|
||
| # You cannot change the values of a _MeshIndexSet | ||
| # Not raising exception for some reason | ||
| assert_change_raises_exception(indexed_lat) | ||
| assert_change_raises_exception(indexed_lon) | ||
|
pt331 marked this conversation as resolved.
Outdated
|
||
|
|
||
| original_lat = cube.coord(standard_name="latitude") | ||
| original_lon = cube.coord(standard_name="longitude") | ||
|
pt331 marked this conversation as resolved.
Outdated
|
||
| # Changing the original mesh is reflected in the _MeshIndexSet | ||
| # Change not reflected for some reason | ||
| value = 9999 | ||
| change_and_assert_change_in_right(original_lat, indexed_lat, value) | ||
| change_and_assert_change_in_right(original_lon, indexed_lon, value) | ||
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
Oops, something went wrong.
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.