Skip to content

Commit

Permalink
Merge pull request #149 from seattleflu/invalid-birth-date
Browse files Browse the repository at this point in the history
etl redcap-det uw-reopening: Handle invalid birth date
  • Loading branch information
kairstenfay authored Oct 3, 2020
2 parents 05e9518 + 5bdd272 commit 90a7f94
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions lib/seattleflu/id3c/cli/command/etl/redcap_det_uw_reopening.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def redcap_det_uw_reopening(*, db: DatabaseSession, cache: TTLCache, det: dict,
return None

patient_entry, patient_reference = create_patient(enrollment)
birthdate = datetime.strptime(enrollment['core_birthdate'], '%Y-%m-%d')
birthdate = parse_birth_date(enrollment)

if not patient_entry:
LOG.warning("Skipping record with insufficient information to construct patient")
Expand Down Expand Up @@ -253,6 +253,17 @@ def redcap_det_uw_reopening(*, db: DatabaseSession, cache: TTLCache, det: dict,
)


def parse_birth_date(record: dict) -> Optional[datetime]:
""" Returns a participant's birth date from a given *record* as a datetime
object if it can be parsed. Otherwise, emits a warning and returns None. """
try:
birth_date = datetime.strptime(record['core_birthdate'], '%Y-%m-%d')
except ValueError:
LOG.warning("Invalid `core_birthdate`.")
birth_date = None

return birth_date

def create_site_reference(record: dict, collection_method: CollectionMethod, event_type: EventType) -> Optional[Dict[str,dict]]:
"""
Create a Location reference for site of the sample collection encounter based
Expand Down Expand Up @@ -1147,8 +1158,13 @@ def create_computed_questionnaire_response(record: dict, patient_reference: dict
For example, a computed question captures the participant's age
on the date of the encounter.
"""

record['age'] = relativedelta(encounter_date, birthdate).years
# A birthdate of None will return a falsy relativedelta() object
delta = relativedelta(encounter_date, birthdate)
if not delta:
age = None
else:
age = delta.years
record['age'] = age

integer_questions = [
'age'
Expand Down

0 comments on commit 90a7f94

Please sign in to comment.