Skip to content

Commit

Permalink
Match variable string input (#245)
Browse files Browse the repository at this point in the history
* Add failing test

* manual addition

* final test

* fix the test

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update whats-new.rst

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
jbusecke and pre-commit-ci[bot] authored Jul 1, 2022
1 parent 81b18b9 commit c3b8f21
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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

0 comments on commit c3b8f21

Please sign in to comment.