Alternating Faster/Slower IOIs Chosen Randomly Across Trials

OS (MacOS Sequoia 15.3.1):
PsychoPy version (2024.2.4):

What are you trying to achieve?:

Hello,

I am creating a task in which participants tap along to a metronome at a certain bpm for each trial using an excel file. Everything works well but I want to have each trial alternate between faster and slower BPM conditions such that trials are presented at faster IOI, slower IOI, faster IOI, slower IOI, etc.

I need to write an algorithm that will:

  1. pick a random IOI for the first trial
  2. pick a random direction (faster or slower) for the second trial
  3. for whichever direction it picks, it find all possible remaining IOIs in that direction, then pick one at random (and remove it from the list)
  4. Also need to switch direction if the fastest or slowest IOI is picked
  5. repeat until all IOIs are gone from the list.

What did you try to make it work?:

Here is some code that I have tried so far:

import pandas as pd
import random

def ioi_selection_algorithm("/Users/cm/Desktop/Experiment_1a/Exp_1a_conditions.xlsx", sheet_name=0):
  
    # Read IOI data from Excel file
    df = pd.read_excel("/Users/cm/Desktop/Experiment_1a/Exp_1a_conditions.xlsx", sheet_name=0)
    
    # Extract the IOI column (in 'BPM')
    ioi_values = sorted(df['BPM'].unique())
    print(f"Available IOIs (sorted): {ioi_values}")
    
    # Create a sequence of all IOIs for a single trial
    sequence = []
    available_iois = ioi_values.copy()
    
    # Step 1: Pick a random IOI to start
    first_ioi = random.choice(available_iois)
    sequence.append(first_ioi)
    available_iois.remove(first_ioi)
    print(f"Starting with IOI: {first_ioi}")
    
    # Step 2: Choose a random direction
    direction = random.choice(["faster", "slower"])
    print(f"Initial direction: {direction}")
    
    # Step 3-5: Continue selecting IOIs until all are used
    while available_iois:
        # Get potential IOIs in the current direction
        if direction == "faster":
            potential_iois = [ioi for ioi in available_iois if ioi < sequence[-1]]
            
            # If no faster IOIs are available, switch direction
            if not potential_iois:
                direction = "slower"
                print("Switching direction to: slower (no faster IOIs available)")
                potential_iois = [ioi for ioi in available_iois if ioi > sequence[-1]]
        else:  # direction is "slower"
            potential_iois = [ioi for ioi in available_iois if ioi > sequence[-1]]
            
            # If no slower IOIs are available, switch direction
            if not potential_iois:
                direction = "faster"
                print("Switching direction to: faster (no slower IOIs available)")
                potential_iois = [ioi for ioi in available_iois if ioi < sequence[-1]]
        
        # Select a random IOI from the potential list
        if potential_iois:
            next_ioi = random.choice(potential_iois)
            sequence.append(next_ioi)
            available_iois.remove(next_ioi)
            print(f"Selected {next_ioi} (direction: {direction})")
        else:
            # This should only happen if we've used all IOIs
            break
    
    print(f"Final sequence for trial: {sequence}")
    return sequence

# Example usage
if __name__ == "__main__":
    # Replace with Excel file path
    file_path = "Exp_1a_conditions.xlsx"
    trial_sequence = ioi_selection_algorithm(file_path)
    

What specifically went wrong when you tried that?:

Before I added this code, my experiment worked perfectly. Now that I added it, it crashes when the trials start. When I ran this, it gave me two error messages. First, it said that “multiple dropped frames have occurred” and “stopping key buffers but this could be dangerous if other keyboards rely on the same”

I’m new to coding and am not sure about the best way to go about this so I would greatly appreciate any feedback!

Those don’t sound like the critical error message.

ChatGPT isn’t very good at working with Builder.