I have an experiment I made with Builder that runs well, but I need to be able to randomize two sets of image stimuli separately for each trial. I have read many of the previous posts on this subject and followed a tutorial online. I wrote some code but you will see below I am getting error messages.
The images are posters and faces presented sequentially – I simply want Poster A to not always be presented with Face A simply because they are in the same row -ie, randomize each column independently before starting the experiment.
I added the following code (appended below) as a routine at the start of the experiment. My excel file is called Conditionsfile.xlsx and the two columns are labeled poster_stim and face_stim in that file.
I keep getting the following error message: File “/Users/Username/Desktop/PsychopyRose/MacKenzie2/MacKenzieCode_lastrun.py”, line 601
trialloop = data.TrialHandler(nReps=$num_items, method=‘random’,
SyntaxError: invalid syntax
################ Experiment ended with exit code 1 [pid:29420] #################
In the trial loop gui of the Builder, I designated $num_items as the total number of rows in the excel sheet and removed the conditions file designation there (bc I designated it in the code below). So I expected the program to ignore the ‘random’ designation at the top of the trial loop gui. But it is clearly not. (for example, I changed that drop down to sequential, and the error message about method= changed from ‘random’ to ‘sequential’)
I am using the terms ‘posters’ and ‘faces’ in the code below as terms I am defining here - that is, I am purposely not using the names of my excel columns bc when I do I get a different error message : Alert 4705: Column name ‘poster_stim’ in conditions file already exists as a variable in this experiment (user).
So before I wrote the code I used the Gui boxes of Builder for the Images to designate the columns in the conditions file $poster_stim. I changed that to $posters[cur_poster] based on the code below.
CODE I ADDED:
Before Experiment Tab:
import random, xlrd
#randomize seed
random.seed()
#input file
in_file = 'Conditionsfile.xlsx'
#number of items to load
num_items = 9
#counters to hold the next stimulus reference
cur_poster = 0
cur_face = 0
Begin Experiment Tab:
#open the excel file
inbook = xlrd.open_workbook(in_file)
insheet = inbook.sheet_by_index(0)
#arrays to hold our stimuli
posters = [ ]
faces = []
#loop through the rows in the stimulus file
for rowx in range(1,num_items+1):
#read in an entire row
row = insheet.row_values(rowx)
#save the poster and face stim
posters.append(row[0])
faces.append(row[1])
random.shuffle(posters)
random.shuffle(faces)
End Routine Tab:
#log the current word and color to our data file
thisExp.addData('posters, posters[cur_poster])
thisExp.addData('faces, faces[cur_face])
#increment stimuli counters
cur_poster = cur_poster + 1
cur_face = cur_face + 1