Skip to content

Commit

Permalink
BUG: extract bids info from basename, not fullpath
Browse files Browse the repository at this point in the history
  • Loading branch information
jungheejung committed Aug 23, 2024
1 parent 2aa0d27 commit b82b670
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
17 changes: 12 additions & 5 deletions spacetop_prep/events/bidsify_alignvideos_ENH.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
# This script reads raw behavior data files from d_beh for task-alignvideo, extracts time stamps and design information, and stores them in new *events.tsv files accompanying BOLD files.
# For more information, please see README.md and the associated paper (Jung et al., 2024)
"""

def extract_bids(filename: str, key: str) -> str:
"""
Extracts BIDS information based on input "key" prefix.
If filename includes an extension, code will remove it.
"""
bids_info = [match for match in filename.split('_') if key in match][0]
bids_info_rmext = bids_info.split(os.extsep, 1)
return bids_info_rmext[0]
def add_rating_event(source_beh, t, t_run_start, rating_type, event_onset, event_rt, response_value_column):
"""
Adds a rating event to the new BIDS-compliant DataFrame.
Expand Down Expand Up @@ -167,10 +174,10 @@ def parse_args():
session_dict = {'ses-01': 4, 'ses-02': 4, 'ses-03': 3, 'ses-04': 2} # NOTE: different sessions have different numbers of runs

if bids_string:
# If bids_string is provided, process that specific file
sub = re.search(r'sub-\d+', bids_string).group(0)
ses = re.search(r'ses-\d+', bids_string).group(0)
run = re.search(r'run-\d+', bids_string).group(0)
fname = Path(bids_string).name
sub = extract_bids(fname, 'sub')
ses = extract_bids(fname, 'ses')
run = extract_bids(fname, 'run')
alignvideo_format_to_bids(sub, ses, run, task_name, beh_inputdir, bids_dir)
else:
# If no bids_string is provided, loop through the entire directory
Expand Down
7 changes: 4 additions & 3 deletions spacetop_prep/events/bidsify_faces_ENH.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ def main():
taskname = 'task-faces'

if bids_string:
sub = extract_bids(bids_string, 'sub')
ses = extract_bids(bids_string, 'ses')
run = extract_bids(bids_string, 'run')
fname = Path(bids_string).name
sub = extract_bids(fname, 'sub')
ses = extract_bids(fname, 'ses')
run = extract_bids(fname, 'run')
run_dict = {'run-01': 'age', 'run-02': 'sex', 'run-03': 'intensity'} if int(sub[-4:])%2 == 0 else {'run-01': 'intensity', 'run-02': 'sex', 'run-03': 'age'}
rating_type = run_dict[run]
faces_format2bids(sub, ses, taskname, run, rating_type, beh_inputdir, bids_dir)
Expand Down
15 changes: 9 additions & 6 deletions spacetop_prep/events/bidsify_fractional_ENH.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def parse_args():
# %%

if args.bids_string and 'tomsaxe' in extract_bids(args.bids_string, 'task'):
sub = extract_bids(args.bids_string, 'sub')
sub = extract_bids(Path(args.bids_string).name, 'sub')
saxe_flist = list(Path(beh_inputdir).rglob(f'{sub}/**/task-fractional/**/{args.bids_string}*.csv'))

if not saxe_flist:
Expand Down Expand Up @@ -236,8 +236,9 @@ def parse_args():
task_name = "posner"
# current_path = Path.cwd()
# main_dir = current_path.parent.parent
if args.bids_string and task_name in extract_bids(bids_string, 'task') :
sub = extract_bids(bids_string, 'sub')
if args.bids_string and task_name in extract_bids(bids_string, 'task'):
sub = extract_bids(Path(args.bids_string).name, 'sub')
# sub = extract_bids(bids_string, 'sub')
filtered_posner_flist = glob.glob(join(beh_inputdir, sub, '**','task-fractional', '**', f'*{bids_string}*.csv'), recursive=True)
else:
# scans_list = sorted(glob.glob('sub-*/**/*ses-04*scans*.tsv', recursive=True))
Expand Down Expand Up @@ -487,8 +488,9 @@ def parse_args():
# current_path = Path.cwd()
# memory_flist = sorted(glob.glob(join(beh_inputdir, '**', f'*{task_name}_beh.csv'), recursive=True))

if args.bids_string and task_name in extract_bids(bids_string, 'task') :
sub = extract_bids(bids_string, 'sub')
if args.bids_string and task_name in extract_bids(bids_string, 'task'):
sub = extract_bids(Path(args.bids_string).name, 'sub')
# sub = extract_bids(bids_string, 'sub')
filtered_memory_flist = glob.glob(join(beh_inputdir, sub, '**','task-fractional', '**', f'*{bids_string}*.csv'), recursive=True)
else:
# scans_list = sorted(glob.glob('sub-*/**/*ses-04*scans*.tsv', recursive=True))
Expand Down Expand Up @@ -688,7 +690,8 @@ def parse_args():
# spunt_flist = sorted(glob.glob(join(beh_inputdir, '**', f'*{task_name}_beh.csv'), recursive=True))

if args.bids_string and task_name in extract_bids(bids_string, 'task') :
sub = extract_bids(bids_string, 'sub')
sub = extract_bids(Path(args.bids_string).name, 'sub')
# sub = extract_bids(bids_string, 'sub')
filtered_spunt_flist = glob.glob(join(beh_inputdir, sub, '**','task-fractional', '**', f'*{bids_string}*.csv'), recursive=True)
else:
# scans_list = sorted(glob.glob('sub-*/**/*ses-04*scans*.tsv', recursive=True))
Expand Down
15 changes: 8 additions & 7 deletions spacetop_prep/events/bidsify_narratives_ENH.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,18 @@ def parse_args():
ses = 'ses-02'

if bids_string:
# If bids_string is provided, process that specific file
sub = extract_bids(bids_string, 'sub')
ses = extract_bids(bids_string, 'ses')
run = extract_bids(bids_string, 'run')
fname = Path(bids_string).name
sub = extract_bids(fname, 'sub')
ses = extract_bids(fname, 'ses')
run = extract_bids(fname, 'run')
narrative_format2bids(sub, ses, run, taskname, beh_inputdir, bids_dir)
else:
# If no bids_string is provided, loop through the entire directory
file_list = sorted(glob.glob(os.path.join(beh_inputdir, '**', f'sub-*task-narratives*_beh.csv'), recursive=True))
for fpath in file_list:
sub = extract_bids(bids_string, 'sub')
ses = extract_bids(bids_string, 'ses')
run = extract_bids(bids_string, 'run')
fname = Path(fpath).name
sub = extract_bids(fname, 'sub')
ses = extract_bids(fname, 'ses')
run = extract_bids(fname, 'run')
narrative_format2bids(sub, ses, run, taskname, beh_inputdir, bids_dir)

8 changes: 5 additions & 3 deletions spacetop_prep/events/bidsify_social_ENH.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,11 @@ def parse_args():
cognitive_logger = setup_logger('cognitive', 'task-cue_cognitive.log')

if args.bids_string and task_name in args.bids_string:
sub = extract_bids(bids_string, 'sub')
ses = extract_bids(bids_string, 'ses')
run = extract_bids(bids_string, 'run')
fname = Path(bids_string).name
sub = extract_bids(fname, 'sub')
ses = extract_bids(fname, 'ses')
run = extract_bids(fname, 'run')

# filtered_cognitive_flist = glob.glob(join(beh_inputdir, sub, '**','task-social', '**', f'*{bids_string}*.csv'), recursive=True)
filtered_cognitive_flist = glob.glob(str(Path(beh_inputdir) / sub / '**' / 'task-social' / '**' / f'*{args.bids_string}*.csv'), recursive=True)

Expand Down

0 comments on commit b82b670

Please sign in to comment.