psychopy.org | Reference | Downloads | Github

Import, open and read an Excel file containing images and words (mixted file)

Hi everyone!
I allow myself to ask for help for my code that doesn’t work, probably because of the functions I use to open and read my particular Excel file.I think it’s just a small mistake easy to solve, but I haven’t been able to find the answer despite my reseach.

The problem:
In my code, I have to import, open and read an Excel file containing images to display and other variables that are not images (words to display). To import / open / read this file, I have used the .xlrd function to read the xlsx file and the .open function like this:

import xlrd, random
from psychopy import visual

in_file = 'essai_image_psycho/essai_image_1.xlsx'
inbook = xlrd.open_workbook(in_file)
insheet = inbook.sheet_by_index(0)

But, when I run my experiment, Psychopy sends me this error message:

Traceback (most recent call last):
  File "C:\Users\roman\Desktop\essai_image_psycho\psycho_essai_1_images_lastrun.py", line 142, in <module>
    inbook = xlrd.open_workbook(in_file)
  File "C:\Program Files\PsychoPy3\lib\site-packages\xlrd\__init__.py", line 116, in open_workbook
    with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'essai_image_psycho/essai_image_1.xlsx'
##### Experiment ended. #####

I precise that my Excel file and the folder are correctly named in my code and that my Excel file is stored in the same folder as my experiment file (.psyexp).

=> Do I use the right functions to open and read my file?
=> As my file contains images and words, Do I need to use a particular method to open and read my images in one hand, and my words in other hand?

Here’s my experiment if it helps:
psycho_essai_1_images.psyexp (16.2 KB)

If someone can help me, I would be very grateful! :grinning:
Thank you!
Romane

The issue is simply that the path you are supplying does not match the actual location of the file. If the Excel file is indeed in the same folder as your .psyexp file, then you should only need to provide the file name, yet you are currently also specifying a sub-folder (essai_image_psycho/).

Have you considered using a TrialHandler class to run your loop of trials? Not only will it read in your Excel file of image names and other conditions, it will handle randomisation as well as automatically saving your data at the end of the experiment, matched up with your condition information. You can also use an ExperimentHandler, especially if you have multiple sets of trials:

https://www.psychopy.org/api/data.html

1 Like

Hi Michael ! :wave: :grinning:

Thank you very much for your response! I have changed the path for my file and this problem seems to be solved now, thanks to you!
But, unfortunately, other errors appear in my code (probably hidden by the first). I think these errors come from my code for images. I really tried to fix it!! But I failed every time…
I’m really sorry to ask for your help again, but maybe you could have some ideas ?

The (new) problem:
When I run my code, the window closes and Psychopy sends me this long error message:

38.4492     ERROR     Couldn't make sense of requested image.
Traceback (most recent call last):
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\basevisual.py", line 832, in _createTexture
    im = tex.copy().transpose(Image.FLIP_TOP_BOTTOM)
AttributeError: 'ImageStim' object has no attribute 'copy'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\roman\Desktop\essai_image_psycho\psycho_essai_1_images_lastrun.py", line 321, in <module>
    image_lateralisation.setImage(imageLat[imageLat_order[cur_imageLat]])
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\image.py", line 302, in setImage
    setAttribute(self, 'image', value, log)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\attributetools.py", line 141, in setAttribute
    setattr(self, attrib, value)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\tools\attributetools.py", line 32, in __set__
    newValue = self.func(obj, value)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\image.py", line 289, in image
    wrapping=False)
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\basevisual.py", line 837, in _createTexture
    raise AttributeError(msg)
AttributeError: Couldn't make sense of requested image.
##### Experiment ended. #####

I precise that this code works perfectly when I don’t use images (but only words). So, the logic of the method seems to be great and the problem may come from my code for images itself. Moreover, I tried to use the TrialHandler function to import and load my data, but without any results. Thus, here I display the first version of my code and I join the two versions at the end. Here comes the monster:

import xlrd, random
from psychopy import visual

random.seed()

# 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_mot = 0
cur_imageLat = 0

#I create a window to display images
win = visual.Window(size =[1366, 768], fullscr = True, monitor = 'testMonitor', screen = 0, units = 'height')

# I load and read the file
in_file = 'essai_image_1.xlsx'
inbook = xlrd.open_workbook(in_file)
insheet = inbook.sheet_by_index(0)

# I create empty lists for each variables 
imageLat = [] #this will contain images
valence_image = []
mot = []
valence_mot = []

# I loop the file to fill lists precising where are each values
for rowx in range(1,num_items+1):
    row = insheet.row_values(rowx)
    imageLat.append(visual.ImageStim(win = win, name = 'image_Lateralisation', size = [0.67, 0.35], image = row[0], mask = None, ori = 0, opacity = 1, flipHoriz = False, flipVert = False))
    valence_image.append(row[1])
    mot.append(row[2])
    valence_mot.append(row[3])

# I create two combined lists and fill them
mot_order = [] #combined list to randomize the lists 'mot' and 'valence_mot' together
imageLat_order = [] #combined list to randomize 'imageLat' and 'valence_image' together

for i in range(num_items):
    mot_order.append(i)
    imageLat_order.append(i)
    
# I randomize separatly each combined list to have two different random orders
random.shuffle(mot_order)
random.shuffle(imageLat_order)


#I try to draw my current image at the begining of the Test Routine (it must be wrong)
for trial in range(num_items):
    cur_imageLat = imageLat[imageLat_order[trial]]
    stim.setImage(cur_imageLat)
    cur_imageLat.draw()

# I log all data that I need
thixExp.addData('image_lat', imageLat[imageLat_order[cur_imageLat]])
thisExp.addData('valence_image', valence_image[imageLat_order[cur_imageLat]])
thisExp.addData('mot', mot[mot_order[cur_mot]])
thisExp.addData('valence_mot', valence_mot[mot_order[cur_mot]])

# I increment 'mot' and 'imageLat' counters to update the counters at each trial
cur_imageLat = cur_imageLat+1
cur_mot = cur_mot+1

I really don’t know where the problem is. If you, or someone else has any suggestions, I would be very very grateful!

The two versions of my experiment:
first code: psycho_essai_1_images.psyexp (16.1 KB)
second code with TrialHandler: psycho_essai_2_images.psyexp (14.4 KB)

Romane.

Hi (again)!
I have fixed the problem now!!
I have changed my code to go back to my very first version (the simplest) and it works perfectly!! If someone wants to know the answer, he/she/they cans read my (own) response to my own topic, I post the entire code (and method) in it: response

Thanks again for your help @Michael !! :grinning:
Romane.