Skip to content
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

Match variable string input #245

Merged
merged 6 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmip6_preprocessing/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import numpy as np
import xarray as xr

from cmip6_preprocessing.utils import _key_from_attrs, cmip6_dataset_id
from cmip6_preprocessing.utils import (
_key_from_attrs,
_maybe_make_list,
cmip6_dataset_id,
)


try:
Expand Down Expand Up @@ -604,6 +608,8 @@ def match_metrics(
# metrics should never match the variable
exact_attrs_wo_var = [ma for ma in exact_attrs if ma != "variable_id"]

match_variables = _maybe_make_list(match_variables)

# if match is set to exact check all these attributes
if match_attrs == "exact":
match_attrs = exact_attrs_wo_var
Expand Down
2 changes: 2 additions & 0 deletions docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ By `Tom Nicholas <https://github.com/TomNicholas>`_ and `Julius Busecke <https:/

Bugfixes
~~~~~~~~
- :py:func:`~cmip6_preprocessing.postprocessing.match_metrics` now accepts single variables as str input (:issue:`229`, :pull:`245`)

- :py:func:`~cmip6_preprocessing.postprocessing.concat_members` now returns a dataset with labelled `member_id` dimension (:issue:`196` , :pull:`197`). By `Julius Busecke <https://github.com/jbusecke>`_

- Fixes incompatibility with upstream changes in xarray>=0.19.0 (:issue:`173`, :pull:`174`). By `Julius Busecke <https://github.com/jbusecke>`_
Expand Down
21 changes: 21 additions & 0 deletions tests/test_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,27 @@ def test_match_metrics_print_statistics(capsys, metricname):
assert "No match found:" + str({metricname: 0}) in captured.out


def test_match_metrics_match_variable_str_input():
# give a dataset that has member_id as dim (indicator that it was aggregated).
metricname = "area"
attrs = {
"source_id": "a",
"grid_label": "a",
"experiment_id": "a",
"table_id": "a",
"variant_label": "a",
"version": "a",
}
ds = random_ds()
ds.attrs = attrs
ds_metric = random_ds().rename({"data": metricname})
ds_metric.attrs = attrs

ds_dict_parsed_list = match_metrics({"a": ds}, {"aa": ds_metric}, [metricname])
ds_dict_parsed_str = match_metrics({"a": ds}, {"aa": ds_metric}, metricname)
xr.testing.assert_equal(ds_dict_parsed_str["a"], ds_dict_parsed_list["a"])


@pytest.mark.parametrize("combine_func_kwargs", [{}, {"compat": "override"}])
def test_combine_datasets_merge(combine_func_kwargs):
attrs_a = {
Expand Down