Skip to content

Cube dim validation utility#2327

Open
maxwhitemet wants to merge 3 commits intometoppv:masterfrom
maxwhitemet:cube_dim_validation_utility
Open

Cube dim validation utility#2327
maxwhitemet wants to merge 3 commits intometoppv:masterfrom
maxwhitemet:cube_dim_validation_utility

Conversation

@maxwhitemet
Copy link
Contributor

@maxwhitemet maxwhitemet commented Mar 10, 2026

Addresses #1099

Description
This PR adds, and tests, a utility to validate cube dimensions.

Testing:

  • Ran tests and they passed OK
  • Added new tests for the new feature(s)

@maxwhitemet maxwhitemet mentioned this pull request Mar 10, 2026
2 tasks
Copy link
Contributor

@gavinevans gavinevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @maxwhitemet 👍

I've added a few comments.

Comment on lines +72 to +74
ValueError:
If an invalid mode is specified, if required dimensions are missing, or if
forbidden dimensions are present.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would usually list the different ValueErrors that can be raised separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted.


"""
if mode not in ("exact", "minimum"):
raise ValueError("mode must be 'exact' or 'minimum'")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also add the mode that was provided to the exception message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Comment on lines +87 to +89
Resolve dimension tokens to actual dimension coordinate names. 'x' and 'y' are
treated as axis tokens to resolve, while all other entries are treated as
explicit dimension coordinate names.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps label would be better than token here, and is the word used in e.g. https://scitools-iris.readthedocs.io/en/stable/generated/api/iris.util.html#iris.util.guess_coord_axis?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted.

def test_validate_cube_dimensions_raises(
cube_3d, required_dimensions, forbidden_dimensions, mode, error_message
):
with pytest.raises(ValueError, match=error_message):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is missing a docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

def test_validate_cube_dimensions_passes(
cube_3d, required_dimensions, forbidden_dimensions, mode
):
validate_cube_dimensions(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is missing a docstring.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


@pytest.fixture
def cube_3d():
data = np.ones((2, 1, 1), dtype=np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short docstring would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Copy link
Contributor Author

@maxwhitemet maxwhitemet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review @gavinevans. I have implemented all feedback.

Comment on lines +72 to +74
ValueError:
If an invalid mode is specified, if required dimensions are missing, or if
forbidden dimensions are present.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted.

Comment on lines +87 to +89
Resolve dimension tokens to actual dimension coordinate names. 'x' and 'y' are
treated as axis tokens to resolve, while all other entries are treated as
explicit dimension coordinate names.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted.


@pytest.fixture
def cube_3d():
data = np.ones((2, 1, 1), dtype=np.float32)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

def test_validate_cube_dimensions_passes(
cube_3d, required_dimensions, forbidden_dimensions, mode
):
validate_cube_dimensions(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

def test_validate_cube_dimensions_raises(
cube_3d, required_dimensions, forbidden_dimensions, mode, error_message
):
with pytest.raises(ValueError, match=error_message):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants