diff --git a/nwb2bids/base.py b/nwb2bids/base.py index 50ed87e..6f3666d 100644 --- a/nwb2bids/base.py +++ b/nwb2bids/base.py @@ -145,7 +145,7 @@ def reposit(in_dir, out_dir, *, no_copy=False): def sanitize_bids_value(in_string, pattern=r"[^a-zA-Z0-9]", replacement="X"): - out_string = re.sub(in_string, pattern, replacement) + out_string = re.sub(pattern, replacement, in_string) return out_string diff --git a/nwb2bids/conftest.py b/nwb2bids/conftest.py index 962988b..81bec59 100644 --- a/nwb2bids/conftest.py +++ b/nwb2bids/conftest.py @@ -30,3 +30,28 @@ def nwb_testdata( io.write(nwbfile) return filename + +@pytest.fixture(scope="session") +def nwb_testdata_nosessionid( + tmp_path_factory, + ): + from pynwb.testing.mock.file import mock_NWBFile + from pynwb.file import Subject + + nwbfile = mock_NWBFile( + session_start_time = datetime(2024, 3, 9, 22, 30, 3, tzinfo=tz.gettz("US/Eastern")), + session_id = "", + ) + time_series = pynwb.TimeSeries(name="Test", data=numpy.array([], dtype="uint8"), unit="n.a.", rate=1.0) + nwbfile.add_acquisition(time_series) + + subject = Subject( + subject_id="12_34", + sex="male", + ) + nwbfile.subject = subject + filename = tmp_path_factory.mktemp("test_nwb2bids") / "testfile.nwb" + with pynwb.NWBHDF5IO(path=filename, mode="w") as io: + io.write(nwbfile) + + return filename diff --git a/nwb2bids/test/test_base.py b/nwb2bids/test/test_base.py index 9bfd3a7..df27af4 100644 --- a/nwb2bids/test/test_base.py +++ b/nwb2bids/test/test_base.py @@ -2,24 +2,52 @@ import os def test_reposit(nwb_testdata, tmp_path): + tmp_path = str(tmp_path) nwb_testdir = os.path.dirname(nwb_testdata) base.reposit(nwb_testdir, tmp_path) # This should be done by invoking the validator once BEP032 is in the BIDS schema: for root, dirs, files in os.walk(tmp_path): if root == tmp_path: - assert dirs == ['sub-1234'] + assert dirs == ['sub-12X34'] assert set(files) == set(['participants.json', 'participants.tsv']) - if root == os.path.join(tmp_path, 'sub-1234'): + elif root == os.path.join(tmp_path, 'sub-12X34'): assert dirs == ['ses-20240309'] assert set(files) == set(['sessions.json', 'sessions.tsv']) - if root == os.path.join(tmp_path, 'sub-1234', 'ses-20240309'): + elif root == os.path.join(tmp_path, 'sub-12X34', 'ses-20240309'): assert dirs == ['ephys'] assert files == [] - if root == os.path.join(tmp_path, 'sub-1234', 'ses-20240309', 'ephys'): + elif root == os.path.join(tmp_path, 'sub-12X34', 'ses-20240309', 'ephys'): assert dirs == [] assert set(files) == set([ - 'sub-1234_contacts.tsv', - 'sub-1234_channels.tsv', - 'sub-1234_probes.tsv', - 'sub-1234_ses-20240309_ephys.nwb', + 'sub-12X34_contacts.tsv', + 'sub-12X34_channels.tsv', + 'sub-12X34_probes.tsv', + 'sub-12X34_ses-20240309_ephys.nwb', ]) + else: + print(root, files, dirs) + raise + +def test_reposit_nosessionid(nwb_testdata_nosessionid, tmp_path): + tmp_path = str(tmp_path) + nwb_testdir = os.path.dirname(nwb_testdata_nosessionid) + base.reposit(nwb_testdir, tmp_path) + # This should be done by invoking the validator once BEP032 is in the BIDS schema: + for root, dirs, files in os.walk(tmp_path): + if root == tmp_path: + assert dirs == ['sub-12X34'] + assert set(files) == set(['participants.json', 'participants.tsv']) + elif root == os.path.join(tmp_path, 'sub-12X34'): + assert dirs == ['ephys'] + assert set(files) == set(['sessions.json', 'sessions.tsv']) + elif root == os.path.join(tmp_path, 'sub-12X34', 'ephys'): + assert dirs == [] + assert set(files) == set([ + 'sub-12X34_channels.tsv', + 'sub-12X34__ephys.nwb', + 'sub-12X34_contacts.tsv', + 'sub-12X34_probes.tsv', + ]) + else: + print(root, files, dirs) + raise