Skip to content

Commit

Permalink
Add combinedType min and max, fixes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
abhidg committed May 17, 2023
1 parent 4a561b3 commit bb5b1e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
15 changes: 4 additions & 11 deletions adtl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,11 @@ def get_combined_type(row: StrDict, rule: StrDict, ctx: Context = None):
rules.append({"field": match, **r})
else:
rules.append(r)
if combined_type == "all":
if combined_type in ["all", "any", "min", "max"]:
values = [get_value(row, r, ctx) for r in rules]
if all(v is None for v in values):
return None
else:
return all(values)
elif combined_type == "any":
values = [get_value(row, r, ctx) for r in rules]
if all(v is None for v in values):
return None
else:
return any(values)
values = [v for v in values if v not in [None, ""]]
# normally calling eval() is a bad idea, but here values are restricted, so okay
return eval(combined_type)(values) if values else None
elif combined_type == "firstNonNull":
try:
return next(
Expand Down
2 changes: 2 additions & 0 deletions docs/specification.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ Accepted values for `combinedType` are:

* *any* - Whether any of the fields are non-null (truthy)
* *all* - Whether all of the fields are non-null (truthy)
* *min* - Minimum of non-null fields
* *max* - Minimum of non-null fields
* *firstNonNull* - First in the list of fields that has a non-null value
* *list* - List of various fields
* *set* - List of various fields, with duplicates removed
Expand Down
17 changes: 17 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@
"source_unit": {"field": "age_unit", "values": {"1": "months", "2": "years"}},
}

ROW_DATE_BOTH_PRESENT = {"admission_date": "2020-05-05", "enrolment_date": "2020-05-19"}
ROW_DATE_ONLY_ONE = {"admission_date": "2020-05-05", "enrolment_date": ""}
RULE_COMBINED_TYPE_MIN = { # earliest date
"combinedType": "min",
"fields": [{"field": "admission_date"}, {"field": "enrolment_date"}],
}
RULE_COMBINED_TYPE_MAX = { # latest date
"combinedType": "max",
"fields": [{"field": "admission_date"}, {"field": "enrolment_date"}],
}

RULE_COMBINED_TYPE_ANY = {"combinedType": "any", "fields": LIVER_DISEASE}
RULE_COMBINED_TYPE_ALL = {"combinedType": "all", "fields": LIVER_DISEASE}
RULE_COMBINED_FIRST_NON_NULL = {
Expand Down Expand Up @@ -313,6 +324,12 @@
((ROW_CONDITIONAL, RULE_CONDITIONAL_FAIL), None),
((ROW_UNIT_MONTH, RULE_UNIT), 1.5),
((ROW_UNIT_YEAR, RULE_UNIT), 18),
((ROW_DATE_BOTH_PRESENT, RULE_COMBINED_TYPE_MIN), "2020-05-05"),
((ROW_DATE_BOTH_PRESENT, RULE_COMBINED_TYPE_MAX), "2020-05-19"),
((ROW_DATE_ONLY_ONE, RULE_COMBINED_TYPE_MIN), "2020-05-05"),
((ROW_DATE_ONLY_ONE, RULE_COMBINED_TYPE_MAX), "2020-05-05"),
(({"admission_date": "", "enrolment_date": ""}, RULE_COMBINED_TYPE_MIN), None),
(({"admission_date": "", "enrolment_date": ""}, RULE_COMBINED_TYPE_MAX), None),
(({"outcome_date": "02/05/2022"}, RULE_DATE_MDY), "05/02/2022"),
(({"outcome_date": "02/05/2022"}, RULE_DATE_ISO), "2022-05-02"),
(
Expand Down

0 comments on commit bb5b1e1

Please sign in to comment.