Skip to content

Commit

Permalink
Merge pull request #500 from dbic/bf-participant-nas
Browse files Browse the repository at this point in the history
ENH+BF: consistent  n/a for age/sex, also handle ?M for months
  • Loading branch information
yarikoptic authored Mar 29, 2021
2 parents 6b80704 + ad75d50 commit 711d403
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
35 changes: 33 additions & 2 deletions heudiconv/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,36 @@ class BIDSError(Exception):
BIDS_VERSION = "1.4.1"


def maybe_na(val):
"""Return 'n/a' if non-None value represented as str is not empty
Primarily for the consistent use of lower case 'n/a' so 'N/A' and 'NA'
are also treated as 'n/a'
"""
if val is not None:
val = str(val)
val = val.strip()
return 'n/a' if (not val or val in ('N/A', 'NA')) else val


def treat_age(age):
"""Age might encounter 'Y' suffix or be a float"""
age = str(age)
if age.endswith('M'):
age = age.rstrip('M')
age = float(age) / 12
age = ('%.2f' if age != int(age) else '%d') % age
else:
age = age.rstrip('Y')
if age:
# strip all leading 0s but allow to scan a newborn (age 0Y)
age = '0' if not age.lstrip('0') else age.lstrip('0')
if age.startswith('.'):
# we had float point value, let's prepend 0
age = '0' + age
return age


def populate_bids_templates(path, defaults={}):
"""Premake BIDS text files with templates"""

Expand Down Expand Up @@ -278,12 +308,13 @@ def add_participant_record(studydir, subject, age, sex):
"control group)")])),
]),
sort_keys=False)

# Add a new participant
with open(participants_tsv, 'a') as f:
f.write(
'\t'.join(map(str, [participant_id,
age.lstrip('0').rstrip('Y') if age else 'N/A',
sex if sex else 'n/a',
maybe_na(treat_age(age)),
maybe_na(sex),
'control'])) + '\n')


Expand Down
26 changes: 26 additions & 0 deletions heudiconv/tests/test_bids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""Test functions in heudiconv.bids module.
"""

from heudiconv.bids import (
maybe_na,
treat_age,
)


def test_maybe_na():
for na in '', ' ', None, 'n/a', 'N/A', 'NA':
assert maybe_na(na) == 'n/a'
for notna in 0, 1, False, True, 'value':
assert maybe_na(notna) == str(notna)


def test_treat_age():
assert treat_age(0) == '0'
assert treat_age('0') == '0'
assert treat_age('0000') == '0'
assert treat_age('0000Y') == '0'
assert treat_age('000.1Y') == '0.1'
assert treat_age('1M') == '0.08'
assert treat_age('12M') == '1'
assert treat_age('0000.1') == '0.1'
assert treat_age(0000.1) == '0.1'

0 comments on commit 711d403

Please sign in to comment.