| Reference | Downloads | Github

How to randomize two different conditions?

Hi everyone,
Right now I have an experiment that has two conditions: map and text. The text condition can be seen in the picture in Trial2_Text1_2. What I want to do is to add another variable to text, making that condition has two “versions” of the text, but only one would be presented to the participant during the experiment randomly. For instance, one participants would see the one version of the text and another participant would see the second version of the text.
I tried to use blocks like it is explained here but the two texts were presented when I tried, not only one.
I’m very new to PsychoPy and I’m still trying to figure it out so I would be very thankful if anyone could help.

I’m not really sure of your needs here, but if you are just one to present one of the two rows of a conditions file as per the linked explanation on blocking, then you could put an expression like this in the “Selected rows” field of the relevant loop dialog box:

np.random.choice((0,1),size = 1)

i.e. if your conditions file has two rows (numbered 0 and 1), and you just want to randomly select one of them rather than cycle through both, that should do the trick.

Hi Michael,
Thank you for your reply.
I believe I didn’t explain well so I will try again.
On my text condition right now, I have 14 sentences(think of this as text 1) that are presented separated during the trial like in a PowerPoint. What I’m trying to do is to add other 14 different sentences(think of this as text 2) to be presented in the text condition. However, I want the text 1 and 2 to be randomly presented to participants. For instance, participant 1 would see only text1 while participant 2 would see only text2.
Does it make sense now?

OK, so you create two different conditions files, say text_1.csv and text_2.csv. All you need to do is control which condition file name is used in the loop that controls your trials.

There are a number of ways to do this. One is that you can add a field to the info dialog that appears at the start of each experiment. Let’s say you call it condition_file, and you type either 1 or 2 into that field at the start of each session. Then in the loop’s condition file field, you could put something like this:


That allows you to get balanced numbers in each condition, by manually specifying it for each subject. Or if you want it to be automatic and completely random, then put some thing like this in that field:

$np.random.choice(('text_1.csv','text_2.csv'), size = 1)

Hi Michael, not sure if I understood right but now the experiment it is not running.
I put the condition_file on the info dialog and then in the loop’s condition file field I added $np.random.choice((‘TextStimuli1.xlsx’,‘TextStimuli2.xlsx’), size = 1) and it is saying “no parameters set(conditionFile not found)” Should I give “directions” to where the excel files are kept?

  • Those are two different approaches. Just use one or the other.
  • That code expression assumes that the .xlsx files are in the same folder as your .psyexp Builder file. If they are in a sub-folder relative to that, then yes, you would need to specify that, as the program can’t guess where the files are. e.g. if they were in a sub-folder called “conditions”, then it would look like this:
$np.random.choice(('conditions/TextStimuli1.xlsx', 'conditions/TextStimuli2.xlsx'), size = 1)

I moved the excel files to the same folder as the builder file but still get the same error.

Do you get an error when trying to run, or just that warning “No parameters set”?
This warning is reasonable (and fine) because it’s just saying that a conditions file hasn’t been successfully loaded-- which is true because one hasn’t been chosen at this stage.

Confirm you get an error while running the experiment, and not just referring to this warning?

Hi Sawal,
The error it’s actually on the experiment. It shows the instructions but when it gets to the trials, the experiment closes.


$np.random.choice(('TextStimuli1.xlsx', 'TextStimuli2.xlsx'), size = 1)[0]

I think the previous suggestion might have been returning an array containing the filename, rather than just the bare filename.

Hi Michael,
Like I said, I moved the files to the same folder as the builder file and just now tried with this last suggestion you sent me and that is what I got.

Sorry, I had missed the $ at the start of that expression in the second post above. Add that, so you get the result of the expression, not the literal text.

Hi Michael,
I tried again and when it gets to the text, the experiment closes again. I’m not sure if that is to the problem that I’m having but I realized that in the text component I was using “$TextFile,” that is the variable from the first excel file text condition I had.

When I change the text component to $TextStimuli1, that is the variable from excel file TextStimuli1, that file runs correctly in the experiment and I can see the TextStimuli1.
When I use $TextStimuli2 , that is the variable from excel file TextStimuli2, it also runs that file correctly in the experiment.

However, when I try to add both $TextStimuli1 and $TextStimuli2 to the text component, the experiment does not starts. Is that related to anything? Or is there any expression I can use in the text component that will work for both?

Hi Michael, I’m trying out this solution, but after entering $f'text_{expInfo['condition_file']}.csv', the OK button at the bottom is not activated so I cannot save this change. How should I proceed?

Try $”text_”+expInfo[‘condition_file’]+”.csv”

1 Like

This works wonders, thanks!

Hi Michael,
thank you for your suggestion. When I run this code on builder it works perfectly,
However, once I upload to pavlovia, I get an error that says “Reference Error: np is not defined.”

I am not sure what this means (I am also unfamiliar with python). How should I proceed?