diff --git a/bidscoin/bids.py b/bidscoin/bids.py index 14330067..8e9ad8fa 100644 --- a/bidscoin/bids.py +++ b/bidscoin/bids.py @@ -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) diff --git a/tests/test_bids.py b/tests/test_bids.py index 6855bd22..fda9614a 100644 --- a/tests/test_bids.py +++ b/tests/test_bids.py @@ -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"