Skip to content

Commit

Permalink
Merge pull request #405 from monarch-initiative/improve-reporting-of-…
Browse files Browse the repository at this point in the history
…temporal-element-parse-errors

Improve reporting of errors encountered during parsing temporal elements
  • Loading branch information
ielis authored Jan 20, 2025
2 parents 45b5ee1 + 2af2766 commit 90be023
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/gpsea/model/_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ def from_iso8601_period(
Create `Age` from ISO8601 duration.
A `value` with **weeks** or days is parsed into a gestational age, while a `value` with years, months or days
is parsed into a postnatal age. An error is raised if a value for weeks and months (or years) is included
at the same time.
is parsed into a postnatal age.
An error is raised if a value for weeks and months (or years) is included
at the same time or if the `value` is not a valid ISO8601 string.
Examples
Expand Down
24 changes: 18 additions & 6 deletions src/gpsea/preprocessing/_phenopacket.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,10 +913,16 @@ def parse_onset_element(
case = time_element.WhichOneof("element")
if case == "age":
age = time_element.age
return Age.from_iso8601_period(value=age.iso8601duration)
try:
return Age.from_iso8601_period(value=age.iso8601duration)
except ValueError as ve:
notepad.add_error(message=ve.args[0])
elif case == "gestational_age":
age = time_element.gestational_age
return Age.gestational(weeks=age.weeks, days=age.days)
try:
return Age.gestational(weeks=age.weeks, days=age.days)
except ValueError as ve:
notepad.add_error(message=ve.args[0])
elif case == "ontology_class":
if term_onset_parser is None:
return None
Expand All @@ -927,7 +933,7 @@ def parse_onset_element(
)
else:
notepad.add_warning(f"`time_element` is in currently unsupported format `{case}`")
return None
return None


def parse_age_element(
Expand All @@ -941,13 +947,19 @@ def parse_age_element(
case = time_element.WhichOneof("element")
if case == "gestational_age":
age = time_element.gestational_age
return Age.gestational(weeks=age.weeks, days=age.days)
try:
return Age.gestational(weeks=age.weeks, days=age.days)
except ValueError as ve:
notepad.add_error(message=ve.args[0])
elif case == "age":
age = time_element.age
return Age.from_iso8601_period(value=age.iso8601duration)
try:
return Age.from_iso8601_period(value=age.iso8601duration)
except ValueError as ve:
notepad.add_error(message=ve.args[0])
else:
notepad.add_warning(
f"{case} of the {field} field cannot be parsed into age",
"Consider formatting the age as ISO8601 duration (e.g., \"P31Y2M\" for 31 years and 2 months)"
)
return None
return None

0 comments on commit 90be023

Please sign in to comment.