Import and display images and words from a mixted Excel file (images are not displayed)

Re!
I answer my own question! :joy:
In this previous message, I said that Psychopy sent me an error message explaining that the indices of list must be integers or slice, whereas mine was a list.
I think the problem was the way I filled out my combined lists:

# The incorrect manner:
mot_order.append(mot)
mot_order.append(valence_mot)
imageLat_order.append(imageLat)
imageLat_order.append(valence_image)

It wasn’t the right way to do it (at ALL)! So I have changed that using a Loop to fill out my lists and it works perfectly now! I have done this:

#the correct method:
for i in range(num_items):
imageLat_order.append(i)
mot_order.append(i)

So, now, I post the right code, hopping that will help some people! :wink:
If you want more details about the purpose of my experiment, you can read the begining of this topic (not the code in it, it was incorrect) : topic

the code:
In a first code component at the begining of the flow, in the ‘begin experiment’ field:

# I import packages to read xlsx files, randmize lists, manipulate images
import xlrd, random

# I precise random function to have one particular random order per subject
random.seed()

# I load my xlsx file containing images, words and 2 attributes
in_file = 'essai_image_1.xlsx'

# I define the number of items that I will use in my entire experiment
num_items = 6

# I put the reference to increment 'mot' and 'imageLat' counters through the experiment
cur_motStim = 0
cur_imageStim = 0

In the same Code component in the ‘begin routine’ field:

#I open and read the file that I load in the 'inbook' object
inbook = xlrd.open_workbook(in_file)
#As it's an excel file I precise the sheet to be selected
insheet = inbook.sheet_by_index(0)

#I create empty lists to stored my four variables
imageLat = []
valence_image = []
mot = []
valence_mot = []

#I fill out my lists with a loop
for rowx in range(1, num_items+1):
    row = insheet.row_values(rowx)#I define what is 'row' here
    imageLat.append(row[0])
    valence_image.append(row[1])
    mot.append(row[2])
    valence_mot.append(row[3])

#I create two empty lists that will be used to group together images+valence of images and words + valence of words 
mot_order = []
imageLat_order = []

#I fill out them with all values of all variables
for i in range(num_items):
    mot_order.append(i)
    imageLat_order.append(i)

#I randomize separatly each list to have one particular random order per list
random.shuffle(mot_order)
random.shuffle(imageLat_order)

In a second Code Component in the test Routine (with Image and Text components) in the ‘end routine’ field:

#I log my data (cur_imageStim and cur_motStim to log the four variables values corresponding to this current trial)
#to keep images + valence of images together in the output file, I log them from the same combined list 'imageLat_order'
#I do the same thing for words + valence of words (from the 'mot_order' combined list)
thisExp.addData('image_later', imageLat[imageLat_order[cur_imageStim]])
thisExp.addData('valence_image', valence_image[imageLat_order[cur_imageStim]])
thisExp.addData('mot', mot[mot_order[cur_motStim]])
thisExp.addData('valence_mot', valence_mot[mot_order[cur_motStim]])

#I increment the counter of trials
cur_imageStim = cur_imageStim+1
cur_motStim = cur_motStim+1

And to finish, I create a Text component and a Image component in the Test Routine to configure the display of images and words (size, positions…).
To indicate the image to be displayed I put $imageLat[$imageLat_order[$cur_imageStim]] in the Image component and in the Text component I put $mot[$mot_order[$cur_motStim]] to indicate the word to be displayed (Be careful to not forget a $ when you do this !!).
Don’t forget a Keyboard component if you want a response from your subjects!

I precise that this code is largely based on the Jason Ozubko’s tutorials on youtube (very good tutorials, go and see them, really!!).
I hope this response will help!
Romane

1 Like