Skip to content

Commit

Permalink
Sanitizing BIDS values
Browse files Browse the repository at this point in the history
  • Loading branch information
TheChymera committed May 21, 2024
1 parent 05ae185 commit 06ce4cc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions nwb2bids/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import csv
import json
import shutil
import re

# The star is required by clize to know to typeset it as `--no-copy` instead of `no-copy`.
def reposit(in_dir, out_dir, *, no_copy=False):
Expand Down Expand Up @@ -143,6 +144,10 @@ def reposit(in_dir, out_dir, *, no_copy=False):
shutil.copyfile(nwb_file, bids_path)


def sanitize_bids_value(in_string, pattern=r"[^a-zA-Z0-9]", replacement="X"):
out_string = re.sub(in_string, pattern, replacement)
return out_string


def extract_metadata(filepath: str) -> dict:

Expand Down Expand Up @@ -171,15 +176,15 @@ def extract_metadata(filepath: str) -> dict:
"InstitutionName": nwbfile.institution,
},
"subject": {
"subject_keyvalue": "sub-" + subject.subject_id,
"subject_keyvalue": "sub-" + sanitize_bids_value(subject.subject_id),
"species": subject.species,
"strain": subject.strain,
"birthday": subject.date_of_birth,
"age": subject.age,
"sex": subject.sex,
},
"session": {
"session_keyvalue": "ses-" + nwbfile.session_id if nwbfile.session_id else "",
"session_keyvalue": "ses-" + sanitize_bids_value(nwbfile.session_id) if nwbfile.session_id else "",
"number_of_trials": len(nwbfile.trials) if nwbfile.trials else None,
"comments": nwbfile.session_description,
},
Expand Down
2 changes: 1 addition & 1 deletion nwb2bids/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def nwb_testdata(
nwbfile.add_acquisition(time_series)

subject = Subject(
subject_id="1234",
subject_id="12_34",
sex="male",
)
nwbfile.subject = subject
Expand Down

0 comments on commit 06ce4cc

Please sign in to comment.