Skip to content

Commit

Permalink
fixed sanitization syntax, added more robust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheChymera committed May 22, 2024
1 parent 06ce4cc commit 0034ce4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion nwb2bids/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
25 changes: 25 additions & 0 deletions nwb2bids/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
44 changes: 36 additions & 8 deletions nwb2bids/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0034ce4

Please sign in to comment.