psychopy.org | Reference | Downloads | Github

Trialhandler with Excel file conditions


#1

Hello PsychoPy users!

I am working on Builder view (PsychoPy version 1.83.04) and am trying to import conditions from excel (stimuli sentences followed by pictures). I have created two types of code to import sentences but I think it’s wrong:
#OPTION 1:
stimuli = []
datafile = open(‘SIwritten.xlsx’, ‘Stimuli_sentences’)
reader = xlsx.reader(datafile)
for row in reader:
stimuli.append(stimulus)

for stimulus in stimuli:
stimulus.draw()
win.flip()

#OPTION 2:
wordstimuli = [‘SIwritten.xlsx’, ‘Stimuli_sentences’]
for thisStim in wordstimuli:
wordStims.append(visual.Textstim(win, thisStim))

#display stimuli
for thisStim in wordstimuli:
thisStim.draw()
win.flip()
event.waitKeys()

Do I need to create a trialHandler first or perhaps use
psychopy.data.importConditions(fileName, returnFieldNames=False, selection=’’)?

After each stimuli sentence I’d like to display an image (links are also in Excel file). Can I import them as conditions too? I’ve used this code but again, I don’t think it’s right:
image1 = [‘SIwritten2.xlsx’, ‘picture1’]
image2 = [‘SIwritten2.xlsx’, ‘picture2’]
for imageStim in image1:
imageStims.append(visual.ImageStim(win, imageStim, pos=(-3.0,0.0)))

for imageStim in image2:
imageStims.append(visual.ImageStim(win, imageStim, pos=(3.0,0.0)))

#to display image:
for imageStim in image1 and image2:
imageStim.draw()
win.flip()
event.waitKeys(keyList=[‘left’,‘right’])

Thank you kindly and sorry if this is not clear or has been cross-posted!


#2

Hello, could you please edit this so that the code appears as code? Without indentation it’s illegible. Put three backticks on the line above and the line below your code like this:

```python

# your code here

```

Also, it says you’re using the Builder, but you’re giving us only code. Are you using the coder?


#3

Hi!

Thanks for your reply. I have altered the code so it is readable and yes, you are right - I’m using the coder. Sorry, my mistake!

stimuli = []
datafile = open('SIwritten.xlsx', 'Stimuli_sentences')
reader = xlsx.reader(datafile)
for row in reader:
stimuli.append(stimulus)

for stimulus in stimuli:
stimulus.draw()
win.flip()

wordstimuli = ['SIwritten.xlsx', 'Stimuli_sentences']
for thisStim in wordstimuli:
wordStims.append(visual.Textstim(win, thisStim))

for thisStim in wordstimuli:
thisStim.draw()
win.flip()
event.waitKeys()

Thank you for your help!


#4

Here’s the code for the image stimuli from two columns in the same Excel file.

image1 = ['SIwritten2.xlsx', 'picture1']
image2 = ['SIwritten2.xlsx', 'picture2']
for imageStim in image1:
imageStims.append(visual.ImageStim(win, imageStim, pos=(-3.0,0.0)))

for imageStim in image2:
imageStims.append(visual.ImageStim(win, imageStim, pos=(3.0,0.0)))

for imageStim in image1 and image2:
imageStim.draw()
win.flip()
event.waitKeys(keyList=['left','right'])

I have also thought of using trialHandler. It works but it displays information from all columns, not just the Stimuli_sentences column, i.e. it does not loop through each row separately but groups all data.

trialList=data.importConditions('SIwritten.xlsx', 'Stimuli_sentences')
trials = data.TrialHandler(trialList,3, method='random', 
dataTypes=None, extraInfo=None, seed=None, originPath=None, 
autoLog=True)
 
for trials in trialList:
message = visual.TextStim(win, text=trials)
message.draw()
win.flip()
core.wait(3.0)

Thank you for your help!


#5

Hi. You’re unlikely to get an answer here until you pose a question. You’ve listed code but not said exactly what the problem is (e.g. “I think it’s wrong” is not sufficient for us to know what difficulties your are experiencing). For example, state precisely what you are attempting to do and what went wrong (such as error messages you received).

There are many problems with the code above, but how about you make it easier for us and break it down to specific issues of what you want to do and what goes wrong.


#6

When I run the code, it stops at the point in which I ask it to open the datafile. I get the following message:

datafile = open(‘SIwritten.xlsx’, ‘Stimuli_sentences’)
ValueError: mode string must begin with one of ‘r’, ‘w’, ‘a’ or ‘U’, not ‘Stimuli_sentences’

I would like to present each sentence from the column entitled ‘Stimuli_sentences’ followed by two pictures side by side from two columns ‘picture1’ and ‘picture2’. After picture presentation I would like to wait for participants to press either ‘left’ or ‘right’ key and depending on the key they press, for them to see a specific message, i.e. ‘correct’ or ‘incorrect’. For that purpose I include another column in my conditions file called ‘correct_answer’. Here’s that section of the code:

keys = event.getKeys(keyList=['left','right'])

#to read variable from a file:
datafile = open('correct_answer')
reader = xlsx.reader(datafile)


#set variables:
keyList = ['left', 'right']
result = [correct_answer]
message = []

#if/elif to display message on feedback:

if result=='left' and keyList=='left':
    message = visual.TextStim(win, text="Correct!")
elif result=='right'and keyList=='right':
    message = visual.TextStim(win, text="Correct!")
else:
    message = visual.TextStim(win, text="Incorrect!")
   

message.draw()
win.flip()
core.wait(2.0)

Hope that is clearer now! Thank you!


#7

It looks like you need to do some more research on standard Psychopy and python design patterns. You’re getting that error because you’re not using the open() command correctly. The first argument is the file you want to open, the second is, like the error message says, either ‘r’ for reading the file, ‘w’ for writing a file (as in erase if it exists and write a new one, which is not what you want here), ‘a’ for append, etc.

But I don’t think that’s going to work the way you want it to on an xlsx file, since you’ll need some type of program that can parse xlsx files. I personally would do what you mentioned earlier, use data.importConditions or use a TrialHandler, which do the heavy lifting for you.

But I’m still worried about how you would apparently be opening a new file at every trial? Normally you read a file in once and cycle through its data when presenting stimuli. I think your best course of action at this point would be to study and tinker with the demos, and / or tinker in the Builder and study the code it generates, before trying to reinvent the wheel without enough experience yet.

I hope it’s clear I mean this constructively, and not to discourage you, but I think you need to shed a little bit with Psychopy demos, documentation, python in general, and good ole’ Google.