-
Notifications
You must be signed in to change notification settings - Fork 0
Renaming variables and dimensions #102
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
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e4b8eab
Added rename var/dim option
joshuatorrance f6a6313
Fixed existing tests
joshuatorrance b15ced1
Added tests for renaming vars & dims
joshuatorrance e1fa200
Updated readme
joshuatorrance 90b0c04
Naming no longer a cmdline option, now it's in the metadata yaml files.
joshuatorrance 32ff7e1
Typos
joshuatorrance 40d64e2
Merge branch 'master' into 101-change-var-dim-names
joshuatorrance 9497ebf
Removed rogue comma
joshuatorrance 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 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 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,132 @@ | ||
| #!/usr/bin/env python | ||
|
|
||
| """ | ||
| Copyright 2025 ACCESS-NRI | ||
|
|
||
| author: Joshua Torrance <[email protected]> | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"), | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| import pytest | ||
| import netCDF4 | ||
| import yaml | ||
|
|
||
| from common import runcmd, make_nc | ||
|
|
||
| def get_var_names(ncfile): | ||
| with netCDF4.Dataset(ncfile, 'r') as ds: | ||
| return ds.variables.keys() | ||
|
|
||
| def get_dim_names(ncfile): | ||
| with netCDF4.Dataset(ncfile, 'r') as ds: | ||
| return ds.dimensions.keys() | ||
|
|
||
| def get_names(ncfile, var_or_dim): | ||
| if var_or_dim == "variable": | ||
| return get_var_names(ncfile) | ||
| else: | ||
| return get_dim_names(ncfile) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "name_tuples", | ||
| [ | ||
| [("Times", "time")], | ||
| [("Times", "times")], | ||
| [("Times", "quiteadifferentname99")], | ||
| [("Times", "_underscore")], | ||
| [("Times", "time"), ("temp", "temperature")], | ||
| ] | ||
| ) | ||
| def test_rename_vars(tmp_path, make_nc, name_tuples): | ||
| """ | ||
| Rename a list of variables given as a list of (old_name, new_name) | ||
| """ | ||
| d = { | ||
| "rename": { | ||
| "variables": {k: v for k, v in name_tuples}, | ||
| } | ||
| } | ||
|
|
||
| meta_path = tmp_path / "meta.yaml" | ||
| with open(meta_path, "w") as f: | ||
| yaml.dump(d, f) | ||
|
|
||
| runcmd(f"addmeta -m {meta_path} {make_nc}") | ||
|
|
||
| var_names = get_var_names(make_nc) | ||
|
|
||
| for old_name, new_name in name_tuples: | ||
| assert old_name not in var_names | ||
| assert new_name in var_names | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "name_tuples", | ||
| [ | ||
| [("x", "ex")], | ||
| [("y", "thisisaverylongdimname")], | ||
| [("Times", "times")], | ||
| [("x", "_underscore")], | ||
| [("x", "ex"), ("Times", "time"), ("y", "why")], | ||
| ] | ||
| ) | ||
| def test_rename_dims(tmp_path, make_nc, name_tuples): | ||
| """ | ||
| Rename a list of dims given as a list of (old_name, new_name) | ||
| """ | ||
| d = { | ||
| "rename": { | ||
| "dimensions": {k: v for k, v in name_tuples}, | ||
| } | ||
| } | ||
|
|
||
| meta_path = tmp_path / "meta.yaml" | ||
| with open(meta_path, "w") as f: | ||
| yaml.dump(d, f) | ||
|
|
||
| runcmd(f"addmeta -m {meta_path} {make_nc}") | ||
|
|
||
| dim_names = get_dim_names(make_nc) | ||
|
|
||
| for old_name, new_name in name_tuples: | ||
| assert old_name not in dim_names | ||
| assert new_name in dim_names | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "var_or_dim", ["variable", "dimension"] | ||
| ) | ||
| def test_rename_var_dim_missing(tmp_path, make_nc, var_or_dim): | ||
| """ | ||
| Confirm that trying to rename a variable or dimension that doesn't exist | ||
| does not fail. | ||
| """ | ||
| old_name = "thisdoesntexist" | ||
| new_name = "newname" | ||
| d = { | ||
| "rename": { | ||
| f"{var_or_dim}s": { | ||
| old_name: new_name | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| meta_path = tmp_path / "meta.yaml" | ||
| with open(meta_path, "w") as f: | ||
| yaml.dump(d, f) | ||
|
|
||
| runcmd(f"addmeta -m {meta_path} {make_nc}") | ||
|
|
||
| names = get_names(make_nc, var_or_dim) | ||
|
|
||
| assert old_name not in names | ||
| assert new_name not in names |
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.