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

combinedType(wordsubstituteset): flatten lists #84

Merged
merged 2 commits into from
Aug 17, 2023
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
19 changes: 16 additions & 3 deletions adtl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def get_combined_type(row: StrDict, rule: StrDict, ctx: Context = None):
return next(
filter(
lambda item: item is not None,
[get_value(row, r, ctx) for r in rules],
flatten([get_value(row, r, ctx) for r in rules]),
)
)
except StopIteration:
Expand All @@ -283,11 +283,11 @@ def get_combined_type(row: StrDict, rule: StrDict, ctx: Context = None):
"excludeWhen rule should be 'none', 'false-like', or a list of values"
)

values = [get_value(row, r, ctx) for r in rules]
values = flatten([get_value(row, r, ctx) for r in rules])
if combined_type == "set":
values = [*set(values)]
if excludeWhen is None:
return values
return list(values)
if excludeWhen == "none":
return [v for v in values if v is not None]
elif excludeWhen == "false-like":
Expand All @@ -298,6 +298,19 @@ def get_combined_type(row: StrDict, rule: StrDict, ctx: Context = None):
raise ValueError(f"Unknown {combined_type} in {rule}")


def flatten(xs):
"""
Flatten a list of lists +-/ non-list items
e.g.
[None, ['Dexamethasone']] -> [None, 'Dexamethasome']
"""
for x in xs:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
yield from flatten(x)
else:
yield x


def expand_refs(spec_fragment: StrDict, defs: StrDict) -> Union[StrDict, List[StrDict]]:
"Expand all references (ref) with definitions (defs)"

Expand Down
57 changes: 57 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1232,3 +1232,60 @@ def test_main_save_report():
"validation_errors": {},
}
Path("epoch-report.json").unlink()


@pytest.mark.parametrize(
"test_input,expected",
[
(
[None, ["Dexamethasone", "Fluticasone", "Methylprednisolone"]],
[None, "Dexamethasone", "Fluticasone", "Methylprednisolone"],
),
([12, ["13", "14"], [[15], ["sixteen"]]], [12, "13", "14", 15, "sixteen"]),
],
)
def test_flatten(test_input, expected):
assert list(parser.flatten(test_input)) == expected


@pytest.mark.parametrize(
"test_row, test_combination, expected",
[
(
{"corticost": "", "corticost_v2": "Dexa"},
"set",
[None, "Dexamethasone"],
),
({"corticost": "Decadron", "corticost_v2": "Dexa"}, "set", ["Dexamethasone"]),
(
{"corticost": "", "corticost_v2": "Cortisonal"},
"firstNonNull",
"Cortisonal",
),
],
)
def test_combinedtype_wordsubstituteset(test_row, test_combination, expected):
test_rule = {
"combinedType": test_combination,
"fields": [
{
"field": "corticost",
"apply": {
"function": "wordSubstituteSet",
"params": [
["Metil?corten", "Prednisone"],
["Decadron", "Dexamethasone"],
],
},
},
{
"field": "corticost_v2",
"apply": {
"function": "wordSubstituteSet",
"params": [["Cortisonal", "Cortisonal"], ["Dexa", "Dexamethasone"]],
},
},
],
}

assert parser.get_combined_type(test_row, test_rule) == unordered(expected)
Loading