Skip to content

Commit

Permalink
Fix bids mappings for derivatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominika Zemanovicova committed Jan 18, 2024
1 parent f847d1c commit c408fc9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
6 changes: 5 additions & 1 deletion bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -2196,12 +2196,16 @@ def add_bids_mappings(bids_mappings: List[BidsMapping], session: Path, bidsfolde
else:
target_subject = bidsses.name
target_session = None
if target.relative_to(bidsfolder).parts[0] == "derivatives":
target_outfolder = bidsfolder
else:
target_outfolder = bidsses
new_entry = {
"subject": target_subject,
"session": target_session,
'SeriesDescription': bids_mapping.run.get("attributes", {}).get("SeriesDescription"),
'source': bids_mapping.source.relative_to(session.parent),
'BIDS_mapping': target.relative_to(bidsses),
'BIDS_mapping': target.relative_to(target_outfolder),
}
entries.append(new_entry)
df_mappings = pd.DataFrame(entries)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,42 @@ def test_add_bids_mappings__session(tmp_path):
assert result_df.equals(expected_df)


def test_add_bids_mappings__derivatives(tmp_path):
"""Test creating 'bids_mappings.tsv' with derivatives."""

sessionfolder = tmp_path / 'source' / 'sub-01'
bidsfolder = tmp_path / 'bids'
bidsses = tmp_path / 'bids' / 'sub-01'
out = bidsfolder / "code" / "bidscoin" / "bids_mappings.tsv"

bids_mappings = [
BidsMapping(
sessionfolder / 'fmap_source',
{bidsfolder / 'derivatives' / 'SIEMENS' / 'fmap' / 'sub-01_TB1RFM.nii.gz'},
'fmap',
{'bids': {'run': '<<>>'}, 'attributes': {'SeriesDescription': 't1'}}
)
]
expected_df = pd.DataFrame(
{
"subject": ["sub-01"],
"session": ['NaN'],
"SeriesDescription": ["t1"],
"source": [str(Path("sub-01") / "fmap_source")],
"BIDS_mapping": [str(Path("derivatives") / "SIEMENS" / "fmap" / "sub-01_TB1RFM.nii.gz")]
}
)

# Run the function
bids.add_bids_mappings(bids_mappings, sessionfolder, bidsfolder, bidsses)

# Check the results
assert out.is_file() is True
result_df = pd.read_csv(out, sep='\t')
result_df['session'].fillna('NaN', inplace=True)
assert result_df.equals(expected_df)


def test_drop_session_from_bids_mappings__session_dropped(tmp_path):

out = tmp_path / 'bids' / "code" / "bidscoin" / "bids_mappings.tsv"
Expand Down

0 comments on commit c408fc9

Please sign in to comment.