-
Notifications
You must be signed in to change notification settings - Fork 297
Chunk Control Tests #5583
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 24 commits into
SciTools:FEATURE_chunk_control
from
ESadek-MO:cc_tests
Nov 20, 2023
Merged
Chunk Control Tests #5583
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
78a62d8
converted tests to pytest, added neg_one, and incomplete from_file an…
ESadek-MO a54f424
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c6f115c
added from_file test
ESadek-MO 1bb45a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 82951d3
added mocking tests
ESadek-MO 32ebf09
Merge branch 'cc_tests' of github.com:ESadek-MO/iris into cc_tests
ESadek-MO de72114
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6d939a6
trial and error with mocks and patches, may or may not work
ESadek-MO eed93d4
Merge branch 'cc_tests' of github.com:ESadek-MO/iris into cc_tests
ESadek-MO 8d728ae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 36ba71e
converted Mock to patch in as_dask test
ESadek-MO 9aa899e
Merge branch 'cc_tests' of github.com:ESadek-MO/iris into cc_tests
ESadek-MO 0b63581
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 82a10b6
review comment changes
ESadek-MO 9137527
merge conflicts
ESadek-MO 4f80847
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fee7ed2
pre commit fixes
ESadek-MO c386032
merge conflicts
ESadek-MO 6727f7b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 139fd41
review comments, and added test in test__get_cf_var_data()
ESadek-MO bfe23b6
added in another test
ESadek-MO 5140274
Merge branch 'cc_tests' of github.com:ESadek-MO/iris into cc_tests
ESadek-MO fc8c78b
added tests and fixed review comments
ESadek-MO 929b03b
added AuxCoord test
ESadek-MO 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
This file was deleted.
Oops, something went wrong.
216 changes: 216 additions & 0 deletions
216
lib/iris/tests/unit/fileformats/netcdf/loader/test__chunk_control.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,216 @@ | ||
| # 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. | ||
| """Unit tests for :class:`iris.fileformats.netcdf.loader.ChunkControl`.""" | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests # isort:skip | ||
| from unittest.mock import ANY, patch | ||
|
|
||
| import dask | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| import iris | ||
| from iris.cube import CubeList | ||
| from iris.fileformats.netcdf import loader | ||
| from iris.fileformats.netcdf.loader import CHUNK_CONTROL | ||
| import iris.tests.stock as istk | ||
|
|
||
|
|
||
| @pytest.fixture() | ||
| def save_cubelist_with_sigma(tmp_filepath): | ||
| cube = istk.simple_4d_with_hybrid_height() | ||
| cube_varname = "my_var" | ||
| sigma_varname = "my_sigma" | ||
| cube.var_name = cube_varname | ||
| cube.coord("sigma").var_name = sigma_varname | ||
| cube.coord("sigma").guess_bounds() | ||
| iris.save(cube, tmp_filepath) | ||
| return cube_varname, sigma_varname | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def save_cube_with_chunksize(tmp_filepath): | ||
| cube = istk.simple_3d() | ||
| # adding an aux coord allows us to test that | ||
| # iris.fileformats.netcdf.loader._get_cf_var_data() | ||
| # will only throw an error if from_file mode is | ||
| # True when the entire cube has no specified chunking | ||
| aux = iris.coords.AuxCoord( | ||
| points=np.zeros((3, 4)), | ||
| long_name="random", | ||
| units="1", | ||
| ) | ||
| cube.add_aux_coord(aux, [1, 2]) | ||
| iris.save(cube, tmp_filepath, chunksizes=(1, 3, 4)) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def tmp_filepath(tmp_path_factory): | ||
| tmp_dir = tmp_path_factory.mktemp("data") | ||
| tmp_path = tmp_dir / "tmp.nc" | ||
| return str(tmp_path) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def remove_min_bytes(): | ||
| old_min_bytes = loader._LAZYVAR_MIN_BYTES | ||
| loader._LAZYVAR_MIN_BYTES = 0 | ||
| yield | ||
| loader._LAZYVAR_MIN_BYTES = old_min_bytes | ||
|
|
||
|
|
||
| def test_default(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, _ = save_cubelist_with_sigma | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| assert cube.lazy_data().chunksize == (3, 4, 5, 6) | ||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (4,) | ||
| assert sigma.lazy_bounds().chunksize == (4, 2) | ||
|
|
||
|
|
||
| def test_control_global(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, _ = save_cubelist_with_sigma | ||
| with CHUNK_CONTROL.set(model_level_number=2): | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| assert cube.lazy_data().chunksize == (3, 2, 5, 6) | ||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (2,) | ||
| assert sigma.lazy_bounds().chunksize == (2, 2) | ||
|
|
||
|
|
||
| def test_control_sigma_only(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, sigma_varname = save_cubelist_with_sigma | ||
| with CHUNK_CONTROL.set(sigma_varname, model_level_number=2): | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| assert cube.lazy_data().chunksize == (3, 4, 5, 6) | ||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (2,) | ||
| # N.B. this does not apply to bounds array | ||
| assert sigma.lazy_bounds().chunksize == (4, 2) | ||
|
|
||
|
|
||
| def test_control_cube_var(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, _ = save_cubelist_with_sigma | ||
| with CHUNK_CONTROL.set(cube_varname, model_level_number=2): | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| assert cube.lazy_data().chunksize == (3, 2, 5, 6) | ||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (2,) | ||
| assert sigma.lazy_bounds().chunksize == (2, 2) | ||
|
|
||
|
|
||
| def test_invalid_chunksize(tmp_filepath, save_cubelist_with_sigma): | ||
| with pytest.raises(ValueError): | ||
| with CHUNK_CONTROL.set(model_level_numer="2"): | ||
| CubeList(loader.load_cubes(tmp_filepath)) | ||
|
|
||
|
|
||
| def test_invalid_var_name(tmp_filepath, save_cubelist_with_sigma): | ||
| with pytest.raises(ValueError): | ||
| with CHUNK_CONTROL.set([1, 2], model_level_numer="2"): | ||
| CubeList(loader.load_cubes(tmp_filepath)) | ||
|
|
||
|
|
||
| def test_control_multiple(tmp_filepath, save_cubelist_with_sigma): | ||
trexfeathers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cube_varname, sigma_varname = save_cubelist_with_sigma | ||
| with CHUNK_CONTROL.set( | ||
| cube_varname, model_level_number=2 | ||
| ), CHUNK_CONTROL.set(sigma_varname, model_level_number=3): | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| assert cube.lazy_data().chunksize == (3, 2, 5, 6) | ||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (3,) | ||
| assert sigma.lazy_bounds().chunksize == (2, 2) | ||
|
|
||
|
|
||
| def test_neg_one(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, _ = save_cubelist_with_sigma | ||
| with dask.config.set({"array.chunk-size": "50B"}): | ||
| with CHUNK_CONTROL.set(model_level_number=-1): | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| # uses known good output | ||
| assert cube.lazy_data().chunksize == (1, 4, 1, 1) | ||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (4,) | ||
| assert sigma.lazy_bounds().chunksize == (4, 1) | ||
|
|
||
|
|
||
| def test_from_file(tmp_filepath, save_cube_with_chunksize): | ||
| with CHUNK_CONTROL.from_file(): | ||
| cube = next(loader.load_cubes(tmp_filepath)) | ||
| assert cube.shape == (2, 3, 4) | ||
| assert cube.lazy_data().chunksize == (1, 3, 4) | ||
|
|
||
|
|
||
| def test_no_chunks_from_file(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, _ = save_cubelist_with_sigma | ||
| with pytest.raises(KeyError): | ||
| with CHUNK_CONTROL.from_file(): | ||
| CubeList(loader.load_cubes(tmp_filepath)) | ||
|
|
||
|
|
||
| def test_as_dask(tmp_filepath, save_cubelist_with_sigma): | ||
trexfeathers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| This does not test return values, as we can't be sure | ||
| dask chunking behaviour won't change, or that it will differ | ||
| from our own chunking behaviour. | ||
| """ | ||
| message = "Mock called, rest of test unneeded" | ||
| with patch("iris.fileformats.netcdf.loader.as_lazy_data") as as_lazy_data: | ||
| as_lazy_data.side_effect = RuntimeError(message) | ||
| with CHUNK_CONTROL.as_dask(): | ||
| try: | ||
| CubeList(loader.load_cubes(tmp_filepath)) | ||
| except RuntimeError as e: | ||
| if str(e) != message: | ||
| raise e | ||
| as_lazy_data.assert_called_with(ANY, chunks=None, dask_chunking=True) | ||
|
|
||
|
|
||
| def test_pinned_optimisation(tmp_filepath, save_cubelist_with_sigma): | ||
| cube_varname, _ = save_cubelist_with_sigma | ||
| with dask.config.set({"array.chunk-size": "250B"}): | ||
| with CHUNK_CONTROL.set(model_level_number=2): | ||
| cubes = CubeList(loader.load_cubes(tmp_filepath)) | ||
| cube = cubes.extract_cube(cube_varname) | ||
| assert cube.shape == (3, 4, 5, 6) | ||
| # uses known good output | ||
| # known good output WITHOUT pinning: (1, 1, 5, 6) | ||
| assert cube.lazy_data().chunksize == (1, 2, 2, 6) | ||
trexfeathers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| sigma = cube.coord("sigma") | ||
| assert sigma.shape == (4,) | ||
| assert sigma.lazy_points().chunksize == (2,) | ||
| assert sigma.lazy_bounds().chunksize == (2, 2) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.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
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.