Showing different trials to a returning participant
Today’s tip is a little different. With the help of ChatGPT, I have written a solution which would allow participants to press escape during a local experiment and then be shown different trials when they restart. This will only work locally, not online.
Put the following code into the Begin Experiment tab of a Python code component.
The code counts the number of rows in a conditions file and then looks for values of trials.thisIndex in data files starting the with current participant number, creating useRows as a list of indices that haven’t yet been seen.
import pandas as pd
from glob import glob
# Set variables
conditions_file = "conditions.xlsx" # Can be .xlsx or .csv
loop_name = "trials"
# Determine file type and read data
if conditions_file.endswith('.xlsx'):
df_conditions = pd.read_excel(conditions_file)
elif conditions_file.endswith('.csv'):
df_conditions = pd.read_csv(conditions_file)
else:
raise ValueError("Unsupported file format. Use .xlsx or .csv.")
# Get the total number of rows
total_rows = len(df_conditions)
# Get all CSV files starting with participant ID
csv_files = glob(os.path.join("data", f"{expInfo['participant']}*.csv"))
# Extract trials.thisIndex values
trial_indices = set() # Using a set for fast lookup
for file in csv_files:
try:
df = pd.read_csv(file)
if loop_name + '.thisIndex' in df.columns:
trial_indices.update(df[loop_name + '.thisIndex'].dropna().astype(int))
except Exception as e:
print(f"Error reading {file}: {e}")
# Generate the full range of expected indices
all_indices = set(range(total_rows))
# Find missing indices
useRows = sorted(all_indices - trial_indices)
# Output results
print('useRows', useRows)
Then set the selected rows of the loop as $useRows.