Skip to content

Commit

Permalink
556 pattern matching on var finds multiple options (#557)
Browse files Browse the repository at this point in the history
* Tests pass for find_target_col. Will let CI find out if any other tests fail

* Running black
  • Loading branch information
djinnome authored Mar 28, 2024
1 parent 0d7db4d commit a7da30f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
4 changes: 3 additions & 1 deletion pyciemss/integration_utils/result_processing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
from typing import Any, Dict, Iterable, List, Mapping, Optional, Union

import numpy as np
Expand Down Expand Up @@ -144,7 +145,8 @@ def find_target_col(var: str, options: List[str]):
# TODO: This "underscore-trailing-name matching" seems very fragile....
# It is done this way since you can intervene on params & states
# and that will match either.
options = [c for c in options if f"{var}_" in c]
pattern = re.compile(f"(?:^|_){var}_(state|param)")
options = [c for c in options if pattern.search(c)]
if len(options) == 0:
raise KeyError(f"No target column match found for '{var}'.")
if len(options) > 1:
Expand Down
33 changes: 27 additions & 6 deletions tests/integration_utils/test_result_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,36 @@ def test_get_times_for(intervention):

@pytest.mark.parametrize("name", ["underscored", "with_underscore", "I", "i"])
def test_find_target_col(name):
columns = [
"before_underscored",
"underscored_after",
"before_with_underscore_after",
"stuff_I_stuff",
good_columns = [
"before_underscored_param",
"underscored_after_state",
"sample_with_underscore_state",
"i_state",
"sampli_id_state",
"persistent_I_param",
]
result = result_processing.find_target_col(name, columns)
result = result_processing.find_target_col(name, good_columns)
assert name in result
multiple_match_columns = [
"i_state",
"persistent_i_param",
"before_underscored_param",
"underscored_param",
"with_underscore_param",
"not_with_underscore_state",
"With_I_param",
"I_state",
]
with pytest.raises(ValueError):
result_processing.find_target_col(name, multiple_match_columns)
no_match_columns = [
"stuff_I_stuff_state",
"sampli_state",
"before_with_underscore_after_param",
"underscored_after_state",
]
with pytest.raises(KeyError):
result_processing.find_target_col(name, no_match_columns)


@pytest.mark.parametrize("logging_step_size", [1, 5, 10, 12, 23])
Expand Down

0 comments on commit a7da30f

Please sign in to comment.