Skip to content

Commit

Permalink
Catch age parsing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Apr 25, 2024
1 parent 5bac582 commit 997b39c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
19 changes: 11 additions & 8 deletions bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,14 +531,17 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N

# Collect personal data for the participants.tsv file
if dataformat == 'DICOM': # PAR does not contain personal info
personals = {}
age = datasource.attributes('PatientAge') # A string of characters with one of the following formats: nnnD, nnnW, nnnM, nnnY
if age.endswith('D'): age = float(age.rstrip('D')) / 365.2524
elif age.endswith('W'): age = float(age.rstrip('W')) / 52.1775
elif age.endswith('M'): age = float(age.rstrip('M')) / 12
elif age.endswith('Y'): age = float(age.rstrip('Y'))
if age and options.get('anon','y') in ('y','yes'):
age = int(float(age))
age = datasource.attributes('PatientAge') # A string of characters with one of the following formats: nnnD, nnnW, nnnM, nnnY
try:
if age.endswith('D'): age = float(age.rstrip('D')) / 365.2524
elif age.endswith('W'): age = float(age.rstrip('W')) / 52.1775
elif age.endswith('M'): age = float(age.rstrip('M')) / 12
elif age.endswith('Y'): age = float(age.rstrip('Y'))
if age and options.get('anon','y') in ('y','yes'):
age = int(float(age))
except Exception as exc:
LOGGER.warning(f"Could not parse age from: {datasource.path}\n{exc}")
personals = {}
personals['age'] = str(age)
personals['sex'] = datasource.attributes('PatientSex')
personals['size'] = datasource.attributes('PatientSize')
Expand Down
21 changes: 12 additions & 9 deletions bidscoin/plugins/spec2nii2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,18 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
try:
age = dateutil.parser.parse(datasource.attributes('rhr_rh_scan_date')) - dateutil.parser.parse(datasource.attributes('rhe_dateofbirth'))
age = str(age.days) + 'D'
except dateutil.parser.ParserError as dateerror:
except dateutil.parser.ParserError as exc:
pass
if age.endswith('D'): age = float(age.rstrip('D')) / 365.2524
elif age.endswith('W'): age = float(age.rstrip('W')) / 52.1775
elif age.endswith('M'): age = float(age.rstrip('M')) / 12
elif age.endswith('Y'): age = float(age.rstrip('Y'))
if age:
if options.get('anon',OPTIONS['anon']) in ('y','yes'):
age = int(float(age))
personals['age'] = str(age)
try:
if age.endswith('D'): age = float(age.rstrip('D')) / 365.2524
elif age.endswith('W'): age = float(age.rstrip('W')) / 52.1775
elif age.endswith('M'): age = float(age.rstrip('M')) / 12
elif age.endswith('Y'): age = float(age.rstrip('Y'))
if age:
if options.get('anon',OPTIONS['anon']) in ('y','yes'):
age = int(float(age))
personals['age'] = str(age)
except Exception as exc:
LOGGER.warning(f"Could not parse age from: {datasource.path}\n{exc}")

return personals

0 comments on commit 997b39c

Please sign in to comment.