Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jungheejung committed Aug 27, 2024
2 parents 6802c84 + 461c899 commit 04fd672
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 79 deletions.
87 changes: 47 additions & 40 deletions spacetop_prep/events/bidsify_fractional_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_task_name(bids_string, metadata_df):
str: The task name or an error message if the input is invalid.
"""
# Convert subject_id to integer by removing the 'sub-' prefix
fname = Path(bids_string).name
fname = Path(bids_string).stem
sub = extract_bids(fname, 'sub')
run = extract_bids(fname, 'run')
subject_number = int(sub.replace('sub-', ''))
Expand Down Expand Up @@ -124,34 +124,38 @@ def parse_args():

task_name = "tomsaxe"
filtered_saxe_flist = None
if args.bids_string:
if bids_string:
# Retrieve the task name from the bids_string using the provided metadata DataFrame
extract_task_name = get_task_name(args.bids_string, metadata_df)
extract_task_name = get_task_name(bids_string, metadata_df)
print(extract_task_name)
basename = Path(bids_string).stem
if 'tomsaxe' in extract_task_name:
# Extract the subject identifier from the bids_string
sub = extract_bids(Path(args.bids_string).name, 'sub')
saxe_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*.csv')) # Search for the CSV file corresponding to the bids_string within the specified directory

sub = extract_bids(basename, 'sub')
ses = extract_bids(basename, 'ses')
run = extract_bids(basename, 'run')
saxe_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*.csv')) # Search for the CSV file corresponding to the bids_string within the specified directory

if not saxe_flist:
# If no standard file is found, attempt to find a TEMP file as a fallback
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/**/*{args.bids_string}*TEMP*.csv'))
# If no standard file is found, attempt to find a TEMP file as a fallback
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/**/*{sub}*{ses}*task-fractional*{run}*TEMP*.csv'))

if temp_flist:
# Use the TEMP file if found
filtered_saxe_flist = temp_flist
else:
# If neither standard nor TEMP files are found, log a message and return an empty list
print(f'No behavior data file found for {args.bids_string}. Checked both standard and temporary filenames.')
print(f'No behavior data file found for {bids_string}. Checked both standard and temporary filenames.')
filtered_saxe_flist = None
else:
# If standard files are found, use them
filtered_saxe_flist = saxe_flist
else:
print(f'No behavior data file found for {args.bids_string}. Skipping to next task.')
print(f'No behavior data file found for {bids_string}. Skipping to next task.')
else:
# Get a list of all relevant files, excluding specific subjects
saxe_flist = list(Path(beh_inputdir).rglob(f'**/{task_name}*.csv'))
saxe_flist = list(Path(beh_inputdir).rglob(f'**/*{task_name}*.csv'))
filtered_saxe_flist = [file for file in saxe_flist if "sub-0001" not in str(file)]

if filtered_saxe_flist:
Expand All @@ -162,7 +166,7 @@ def parse_args():
sub_bids = extract_bids(saxe_fname, 'sub') # re.search(r'sub-\d+', saxe_fname).group(0)
ses_bids = extract_bids(saxe_fname, 'ses') # re.search(r'ses-\d+', saxe_fname).group(0)
run_bids = extract_bids(saxe_fname, 'run') # re.search(r'run-\d+', saxe_fname).group(0)
bids_name= f"{sub_bids} {ses_bids} {run_bids}"
bids_name= f"{sub_bids}_{ses_bids}_{run_bids}"
task_name = get_task_name(bids_name, metadata_df)
# task_name = re.search(r'run-\d+-(\w+)_beh', saxe_fname).group(1)
print(f"{sub_bids} {ses_bids} {run_bids} {task_name}")
Expand Down Expand Up @@ -289,18 +293,18 @@ def parse_args():
filtered_posner_flist = None
if args.bids_string:
# Retrieve the task name from the bids_string using the provided metadata DataFrame
task_name = get_task_name(args.bids_string, metadata_df)

if 'posner' in task_name:
# Extract the subject identifier from the bids_string
sub = extract_bids(Path(args.bids_string).name, 'sub')

extract_task_name = get_task_name(args.bids_string, metadata_df)
basename = Path(bids_string).stem
if 'posner' in extract_task_name:
sub = extract_bids(basename, 'sub')
ses = extract_bids(basename, 'ses')
run = extract_bids(basename, 'run')
# Search for the CSV file corresponding to the bids_string within the specified directory
posner_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*.csv'))
posner_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*.csv'))

if not posner_flist:
# If no standard file is found, attempt to find a TEMP file as a fallback
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*TEMP*.csv'))
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*TEMP*.csv'))

if temp_flist:
# Use the TEMP file if found
Expand All @@ -316,7 +320,7 @@ def parse_args():
print(f'No behavior data file found for {args.bids_string}. Skipping to next task.')
else:
# If no bids_string is provided, search for all relevant files excluding specific subjects
posner_flist = list(Path(beh_inputdir).rglob(f'**/{task_name}*.csv'))
posner_flist = list(Path(beh_inputdir).rglob(f'**/*{extract_task_name}*.csv'))

# Filter out files belonging to the excluded subject (e.g., "sub-0001")
filtered_posner_flist = [file for file in posner_flist if "sub-0001" not in str(file)]
Expand All @@ -328,7 +332,7 @@ def parse_args():
sub_bids = re.search(r'sub-\d+', posner_fname).group(0)
ses_bids = re.search(r'ses-\d+', posner_fname).group(0)
run_bids = re.search(r'run-\d+', posner_fname).group(0)
bids_name= f"{sub_bids} {ses_bids} {run_bids}"
bids_name= f"{sub_bids}_{ses_bids}_{run_bids}"
task_name = get_task_name(bids_name, metadata_df)
# task_name = re.search(r'run-\d+-(\w+)_beh', posner_fname).group(1)
print(f"{sub_bids} {ses_bids} {run_bids} {task_name}")
Expand Down Expand Up @@ -570,17 +574,18 @@ def parse_args():
if args.bids_string:
# Retrieve the task name from the bids_string using the provided metadata DataFrame
task_name = get_task_name(args.bids_string, metadata_df)

basename = Path(bids_string).stem
if 'memory' in task_name:
# Extract the subject identifier from the bids_string
sub = extract_bids(Path(args.bids_string).name, 'sub')
sub = extract_bids(basename, 'sub')
ses = extract_bids(basename, 'ses')
run = extract_bids(basename, 'run')

# Search for the CSV file corresponding to the bids_string within the specified directory
memory_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*.csv'))
memory_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*.csv'))

if not memory_flist:
# If no standard file is found, attempt to find a TEMP file as a fallback
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*TEMP*.csv'))
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*TEMP*.csv'))

if temp_flist:
# Use the TEMP file if found
Expand All @@ -596,7 +601,7 @@ def parse_args():
print(f'No behavior data file found for {args.bids_string}. Skipping to next task.')
else:
# If no bids_string is provided, search for all relevant files excluding specific subjects
memory_flist = list(Path(beh_inputdir).rglob(f'**/{task_name}*.csv'))
memory_flist = list(Path(beh_inputdir).rglob(f'**/*{task_name}*.csv'))

# Filter out files belonging to the excluded subject (e.g., "sub-0001")
filtered_memory_flist = [file for file in memory_flist if "sub-0001" not in str(file)]
Expand All @@ -608,7 +613,7 @@ def parse_args():
sub_bids = extract_bids(memory_fname, 'sub') ##re.search(r'sub-\d+', memory_fname).group(0)
ses_bids = extract_bids(memory_fname, 'ses') ##re.search(r'ses-\d+', memory_fname).group(0)
run_bids = extract_bids(memory_fname, 'run') ##re.search(r'run-\d+', memory_fname).group(0)
bids_name= f"{sub_bids} {ses_bids} {run_bids}"
bids_name= f"{sub_bids}_{ses_bids}_{run_bids}"
task_name = get_task_name(bids_name, metadata_df)
# task_name = re.search(r'run-\d+-(\w+)_beh', memory_fname).group(1)
print(f"{sub_bids} {ses_bids} {run_bids} {task_name}")
Expand Down Expand Up @@ -796,36 +801,38 @@ def parse_args():
"""
task_name = "tomspunt"
filtered_spunt_flist = None
if args.bids_string:
if bids_string:
# Retrieve the task name from the bids_string using the provided metadata DataFrame
task_name = get_task_name(args.bids_string, metadata_df)

if 'tomspunt' in task_name:
extract_task_name = get_task_name(bids_string, metadata_df)
print(f"extracted task is {extract_task_name}")
basename = Path(bids_string).stem
if 'tomspunt' in extract_task_name:
# Extract the subject identifier from the bids_string
sub = extract_bids(Path(args.bids_string).name, 'sub')

sub = extract_bids(basename, 'sub')
ses = extract_bids(basename, 'ses')
run = extract_bids(basename, 'run')
# Search for the CSV file corresponding to the bids_string within the specified directory
spunt_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*.csv'))
spunt_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*.csv'))

if not spunt_flist:
# If no standard file is found, attempt to find a TEMP file as a fallback
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{args.bids_string}*TEMP*.csv'))
temp_flist = list(Path(beh_inputdir).rglob(f'{sub}/task-fractional/*{sub}*{ses}*task-fractional*{run}*TEMP*.csv'))

if temp_flist:
# Use the TEMP file if found
filtered_spunt_flist = temp_flist
else:
# If neither standard nor TEMP files are found, log a message and return an empty list
print(f'No behavior data file found for {args.bids_string}. Checked both standard and temporary filenames.')
print(f'No behavior data file found for {bids_string}. Checked both standard and temporary filenames.')
filtered_spunt_flist = None
else:
# If standard files are found, use them
filtered_spunt_flist = spunt_flist
else:
print(f'No behavior data file found for {args.bids_string}. Skipping to next task.')
print(f'No behavior data file found for {bids_string}. Skipping to next task.')
else:
# If no bids_string is provided, search for all relevant files excluding specific subjects
spunt_flist = list(Path(beh_inputdir).rglob(f'**/{task_name}*.csv'))
spunt_flist = list(Path(beh_inputdir).rglob(f'**/*{task_name}*.csv'))

# Filter out files belonging to the excluded subject (e.g., "sub-0001")
filtered_spunt_flist = [file for file in spunt_flist if "sub-0001" not in str(file)]
Expand All @@ -836,7 +843,7 @@ def parse_args():
sub_bids = re.search(r'sub-\d+', spunt_fname).group(0)
ses_bids = re.search(r'ses-\d+', spunt_fname).group(0)
run_bids = re.search(r'run-\d+', spunt_fname).group(0)
bids_name= f"{sub_bids} {ses_bids} {run_bids}"
bids_name= f"{sub_bids}_{ses_bids}_{run_bids}"
task_name = get_task_name(bids_name, metadata_df)
# task_name = re.search(r'run-\d+-(\w+)_beh', spunt_fname).group(1)

Expand Down
Loading

0 comments on commit 04fd672

Please sign in to comment.