Randomly pulling one stimulus from one condition per trial

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): 2020.2.6
Standard Standalone? (y/n) If not then what?: Y
What are you trying to achieve?:

My stimuli are presented text (number triads) in 6 conditions. Each condition has a different number of possible stimuli (fewer permutations of ordered triads: 1 2 3; more combinations unordered: 2 1 3; 2 3 1; etc.) I would like to have each block to have the same number of trials per condition, but each trial to be a randomly pulled stimulus from that condition.

What did you try to make it work?:

I’ve tried to co-opt the “block design” instructions described here: Block designs and counterbalancing — Workshops for PsychoPy 2020 2020

Currently, I have a “block” excel file with 1 parameter being the name of the “condition” excel files. I have 6 “condition” files containing the stimuli. I’ve built two loops around “trial”: a larger “choose_condition” that contains a (non-repeating) loop that I intend to randomly pull a line from the condition spreadsheet for presentation.

I believe I have followed the instructions on the above link exactly, but it doesn’t run, and I think it’s because the inner loop is not connecting to the condition files.

What specifically went wrong when you tried that?:

No error message. It fails on the trial stage (after instructions/deleting trial lets ending screen run).

My inner loop “Conditions” box does not set: “No parameters set (conditionsFile not found”).

I suspect the above error is the issue, but I don’t know.

Thank you for your time!


I’ve been working on this for a while, and using some other snippets of code from other topics I have gotten the script to generally run. This is what that snippet looks like, in case this helps anyone in the future. conditionsFile is the variable that the trial loop pulls, and CO/CU…LU are the excel files holding my stimuli, file is the label in the outer loop spreadsheet.

conditionsFile = ' '
    if file == 'CO.xlsx':
        conditionsFile = 'CO.xlsx'
    elif file == 'SO.xlsx':
        conditionsFile = 'SO.xlsx'
    elif file == 'LO.xlsx':
        conditionsFile = 'LO.xlsx'
    elif file == 'CU.xlsx':
        conditionsFile = 'CU.xlsx'
    elif file == 'SU.xlsx':
        conditionsFile = 'SU.xlsx'
        conditionsFile = 'LU.xlsx'

I have two issues that I’m running into now:

  1. The code component option in the builder won’t place this chunk in a location where it can function. I’ve manually inserted it at the start of the outer loop. My impression is that this means I’ll need to manually fix this every time I press the “compile to script” button. Is there a way to set it more securely?

  2. I want to select a single row from each condition row for each trial. Right now, it’s set up to pull a condition file, run through all rows within that file randomly, then move on to the next. I figure that a snippet of codealong these lines will do it, placed in the “selected row(s)” parameter of my inner loop:

np.random.randint(1,len('''condition file''')

Unfortunately, I’m new to Python, so parsing the code is still difficult for me, and I’m not sure where to put it within the loop or how to correctly identify the number of rows in the file to know the maximum possible row to select.

Thank you!