Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Ensure age from sessions file is not overriden #291

Merged
merged 1 commit into from
Jun 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions nibabies/utils/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,15 @@ def parse_bids_for_age_months(
age = _get_age_from_tsv(sessions_tsv, level='session', key=f'ses-{session_id}')

participants_tsv = Path(bids_root) / 'participants.tsv'
if participants_tsv.exists():
if participants_tsv.exists() and age is None:
age = _get_age_from_tsv(participants_tsv, level='participant', key=f'sub-{subject_id}')

return age


def _get_age_from_tsv(bids_tsv: Path, level: Literal['session', 'participant'], key: str):
def _get_age_from_tsv(
bids_tsv: Path, level: Literal['session', 'participant'], key: str
) -> Optional[int]:
import pandas as pd

df = pd.read_csv(str(bids_tsv), sep='\t')
Expand Down Expand Up @@ -328,7 +330,6 @@ def _get_age_from_tsv(bids_tsv: Path, level: Literal['session', 'participant'],
def _verify_age_json(bids_json: Path) -> bool:
try:
data = json.loads(bids_json.read_text())
assert data['age']['Units'] == 'months'
return data['age']['Units'].lower() == 'months'
except Exception:
return False
return True