Randomly select some image files from the folder as stimuli

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): v2020.2.10

What are you trying to achieve?:
I hope to randomly select some images from a folder to appear on the screen as stimulation, so I added the following codes with Code Component, hoping to achieve it, but it kept reporting errors. I don’t know what to do, and I hope to get help.Thanks

What did you try to make it work?:
import os
import random

filenames= os.listdir(’./pics/’)
random.shuffle(filenames)
list_for_centers=filenames[:10]
target=[]

for item in list_for_centers:
target_name=’./pics/’+item
target.append(target_name)

print(target)

What specifically went wrong when you tried that?:
14.3494 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 836, in _createTexture
im = tex.copy().transpose(Image.FLIP_TOP_BOTTOM)
AttributeError: ‘list’ object has no attribute ‘transpose’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “C:\Users\31279\pythonProject\exp2_test_lastrun.py”, line 353, in
image.setImage(target)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\image.py”, line 305, 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 292, in image
wrapping=False)
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\basevisual.py”, line 841, in _createTexture
raise AttributeError(msg)
AttributeError: Couldn’t make sense of requested image.
Exception ignored in: <bound method TextStim.del of <psychopy.visual.text.TextStim object at 0x0000022A168304E0>>
Traceback (most recent call last):
File “C:\Program Files\PsychoPy3\lib\site-packages\psychopy\visual\text.py”, line 240, in del
File “C:\Program Files\PsychoPy3\lib\site-packages\pyglet\gl\lib.py”, line 97, in errcheck
ImportError: sys.meta_path is None, Python is likely shutting down

Experiment ended.

Include pasted full error message if possible. “That didn’t work” is not enough information.

Hi,

instead of using a complicated code component, you could just use a random loop and an excel file that contains all the images filenames. Then, have a simpler code component that keep tracts of the number of trials and end the loop after a certain number of images are presented (e.g.10) = random subset of images appear on screen.

tandy

Sorry, I don’t quite understand it, could you explain it in more detail? Thanks!!!:grinning:

Probably worth following @tandy’s advice. But here is the reason for your error: you are trying to set the image filename of a stimulus to be target. That variable is a list of file names, and the image stimulus doesn’t know what to do with a list object. You need to refer to a specific entry in that list in order to provide a valid input.

1 Like

:ok:! I got it!! Thanks! But actually I don’t know what @tandy means and how to do…:sob:

Add a code component that says something like this and place it into your image trial:

In the begin experiment tab:

myCounter = 0 #set the counter to 0

In the begin routine tab

myCounter = myCounter + 1
if myCounter >= 10:
	currentLoop.finished = True #after 10 images are presented stop the loop

tandy

Thank you soooo much!!! :sob:It works!! You saved my life :sob:

But there is a new problem now…I want the same images to appear twice, so I set “nReps$=2” in the loop properties. If loop finished, images can’t repeated anymore. What should I do?

Thank you again and again :grinning:

Ok so try this:

instead of having nReps$=2 in the trial loop, leave the repetitions as 1. Then create a second loop above the first one, set to sequential and with no condition file, set to repeat 2 times. Should work,

tandy

Is that what you mean? It seems doesn’t work… :sob:

What is not working?

Less elegant solution: just add 3 new routines (fixation - faces_alone, alone_rating) in the flow with a loop around them (so it’s 3 routine inside loop1, 3 routine inside loop2, each loop ends after 10 images).

tandy

It won’t repeat 2 times…

Maybe go back to the code component, I wrote new codes, but only showing 1 picture that loop will finish…How does it present all 10 pictures?

import os
import random

filenames= os.listdir('./pics/')
random.shuffle(filenames)
list_for_centers=filenames[:10]

for item in list_for_centers:
    target='./pics/'+item
    print(target)

Ok I think I did not understood what you wanted to do. So correct me if I’m wrong: you want to show 10 random images twice taken from a larger sample. So for example out of 100 images you want to show 10 at random (e.g. image 1, 56, 67, 44, 9, 34, 55, 90, 39, 34) and then (after a break/pause?) you want to present them again (1, 56, 67, 44, 9, 34, 55, 90, 39, 34) in the same or different order?

tandy

The reason the second loop won’t run is because this line:

myCounter = 0 #set the counter to 0

happens only once, at the beginning of the experiment. So on the second loop, the counter will still be 10 and hence the loop will finish immediately. So delete that line and instead put this in the “begin routine” tab:

if trials.thisN == 0:
    myCounter = 0 # set the counter to 0

so it gets reset at the start of each loop.

Yeah you are right. :smiley: But I don’t need a break.
I want to show 10 pictures at random (e.g. image 1, 56, 67, 44, 9, 34, 55, 90, 39, 34) and then present them again immediately (1, 56, 67, 44, 9, 34, 55, 90, 39, 34) in a different order. And this is the first block.

The second block is: picking 1 pictures (e.g. image 55) from the 10 (1, 56, 67, 44, 9, 34, 55, 90, 39, 34) as the target stimulus,then picking 4 pictures randomly (e.g. image 56, 9, 90, 44) from the remainder 9 (1, 56, 67, 44, 9, 34, 90, 39, 34) around the target. It still needs to present them again immediately.

I don’t know whether I spelt out? :disappointed_relieved: Thanks

Hey Michael,
Would you explain how to do that in coder?

Hi, from such a long thread, I’m not sure what the specific question is here. Could you provide some more detail (probably best to create a new topic for it)?

sure, thank you.