How to present one picture randomly

Hello all,
I met a problem that I don’t think I can solve it by myself. I am asking for your help sincerely.
I have a stimulus set. One stimulus set contains many object categories, and each categories consist of four pictures. The question is that I don’t know how to randomly select one picture in each trial as well as randomly select the object categories at the same time. If it need code to cope with, please inform me in details.
Here’s how my folder of the stimulus set looks like:


Really need your help.Thanks!

Maybe I should simpllify my question:
How to randomly pick one picture from four pictures in each trial?

It sounds like you need a spreadsheet with one row per category and columns for Category, Pic1, Pic2,Pic3,Pic4

Then in a code component above your image component you could have the following in Begin Routine.

thisPic=None 
picChoose=randint(0,4)
if picChoose==0:
     thisPic=Pic1
elif picChoose ==1:
     thisPic=Pic2
elif picChoose ==2:
     thisPic=Pic3
elif picChoose ==3:
     thisPic=Pic4

Then put $thisPic in your image component

2 Likes

Thank you very much. Let me have a try. :smiley:

That works. Thanks sincerely!
However, I have another question about randomization. I have another stimulus set, which contains many object categories. The difference is that each object categories consists of eight object pictures. It is composed of 2 levels of exemplars, 2 levels of orientation, and 2 levels of materials(222=8). What’s more, the variable Feature(orientation, material) is a between-subject variable.That means, one group of subjects are tested with exemplar-orientation, the other group of subjects are tested with exemplar-material. The problem is that I don’t know how to present one single picture at a trial randomly.
Here’s how my folder of the stimulus set looks like:

Really need your help! Thanks! :innocent:

I’m sorry but this sounds like several questions.

Between subject variables are best set in the expInfo dialogue box (to ensure equal distribution of conditions). Random allocation in code is fine for some variables but can result in randomly having very different group sizes, especially in small studies.

Unless you want to commission someone to help you or do it for you then you should make your best attempt and show what you tried and what went wrong.

I think you are totally correct. I did’t have a clear thought about the problem.
Now let me make it clear.

In my condition file, there are eight pictures. I want the computer to randomly pick one from Pic1 to Pic4,or from Pic5 to Pic8(to control the Orientation variable). Then how to modifly the former codes to obtain the effect?

The following is my condition file:
CAD_Material.xlsx (9.6 KB)

Hoping for your advices. Thanks sincerely!

The easiest option would be to split them into two separate Excel files and then select columns 1-4 as above.

Hello wakecarter,

Thanks for your patient help.
I am not so good at codes. I have two stimulus sets. In the OE stimulus set, each object category has 4 object pictures; In the CAD stimulus set, each object category has 8 object pictures. I want to randomly pick pictures in the two stimulus set. I am going to create a code of nested loop. So based on the former code:
thisPic=None picChoose=randint(0,4) if picChoose==0: thisPic=Pic1 elif picChoose ==1: thisPic=Pic2 elif picChoose ==2: thisPic=Pic3 elif picChoose ==3: thisPic=Pic4
I make some adjustments as the following:

thisPic=None 
while stimulus_set == "OE":
    picChoose=randint(0,4)
    if picChoose==0:
        thisPic=Pic1
    elif picChoose ==1:
        thisPic=Pic2
    elif picChoose ==2:
        thisPic=Pic3
    elif picChoose ==3:
        thisPic=Pic4

while stimulus_set == "CAD":
    picChoose=randint(0,7)
    if picChoose==0:
        thisPic=Pic1
    elif picChoose ==1:
        thisPic=Pic2
    elif picChoose ==2:
        thisPic=Pic3
    elif picChoose ==3:
        thisPic=Pic4
    elif picChoose ==4:
        thisPic=Pic5
    elif picChoose ==5:
        thisPic=Pic6
    elif picChoose ==6:
        thisPic=Pic7
    elif picChoose ==7:
        thisPic=Pic8

I am not sure whether it is correct. Can you have a check for me?
Thanks sincerely.
Best wishes, Cheer.

I would change while to if in both locations but that otherwise looks okay.

1 Like

Hello wakecarter,
Sorry to bother you.
When I use the following codes in the Begin Routine, the pictures were all missed.

thisPic=None 
stimulus_set = None
while stimulus_set == "OE":
    picChoose=randint(0,4)
    if picChoose==0:
        thisPic=Pic1
    elif picChoose ==1:
        thisPic=Pic2
    elif picChoose ==2:
        thisPic=Pic3
    elif picChoose ==3:
        thisPic=Pic4

while stimulus_set == "CAD":
    picChoose=randint(0,7)
    if picChoose==0:
        thisPic=Pic1
    elif picChoose ==1:
        thisPic=Pic2
    elif picChoose ==2:
        thisPic=Pic3
    elif picChoose ==3:
        thisPic=Pic4
    elif picChoose ==4:
        thisPic=Pic5
    elif picChoose ==5:
        thisPic=Pic6
    elif picChoose ==6:
        thisPic=Pic7
    elif picChoose ==7:
        thisPic=Pic8

The error code:

Generating PsychoPy script...

############# Running: D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py ##############
75.0111     INFO     Loaded monitor calibration from ['2021_02_20 15:59']
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
##### Experiment ended. #####

Generating PsychoPy script...

############# Running: D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py ##############
135.2939     INFO     Loaded monitor calibration from ['2021_02_20 15:59']
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
##### Experiment ended. #####

Generating PsychoPy script...

############# Running: D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py ##############
294.3059     INFO     Loaded monitor calibration from ['2021_02_20 15:59']
Alert 4205:Python Syntax Error in 'Begin Routine' tab. See '    elif picChoose ==1:
' on line number 6 of the 'Begin Routine' tab.
	For further info see https://psychopy.org/alerts/4205.html
  File "D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py", line 1092
    elif picChoose ==1:
       ^
SyntaxError: invalid syntax
##### Experiment ended. #####

Generating PsychoPy script...

############# Running: D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py ##############
302.4314     INFO     Loaded monitor calibration from ['2021_02_20 15:59']
Alert 4205:Python Syntax Error in 'Begin Routine' tab. See '    elif picChoose ==1:
' on line number 6 of the 'Begin Routine' tab.
	For further info see https://psychopy.org/alerts/4205.html
  File "D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py", line 1092
    elif picChoose ==1:
       ^
SyntaxError: invalid syntax
##### Experiment ended. #####

Generating PsychoPy script...

############# Running: D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py ##############
308.3122     INFO     Loaded monitor calibration from ['2021_02_20 15:59']
Alert 4205:Python Syntax Error in 'Begin Routine' tab. See '    elif picChoose ==1:
' on line number 6 of the 'Begin Routine' tab.
	For further info see https://psychopy.org/alerts/4205.html
  File "D:\psychopy\RWO_LTM\Exp1 - copy_lastrun.py", line 1092
    elif picChoose ==1:
       ^
SyntaxError: invalid syntax
##### Experiment ended. #####

Could you give me some suggestions how to adjust the codes?
Thanks sincerely.

Note Wakefield’s earlier suggestion ^^^

But also note that you specify stimulus_set = None and then immediately check the value of stimulus_set: there is not much point in that, as you’ve just set it to None.

This sort of code could also be much more concise, like this:

if stimulus_set == 'OE':
    thisPic = randchoice([Pic1, Pic2, Pic3, Pic4])
elif stimulus_set == 'CAD':
    thisPic = randchoice([Pic1, Pic2, Pic3, Pic4, Pic5, Pic6, Pic7, Pic8])
else:
    print('Oops - logic error!')

randchoice is a function imported by Builder from the numpy library and made available to you, as follows:

from numpy.random import choice as randchoice
1 Like

Haaa! Obviously I misunderstood his advice before. Now the problem has been solved. Thanks!!!

Hello Michael,
Thanks for your codes! That also works, which is pretty consice! :grinning:

randchoice is much neater but won’t work online, which is why I avoid it.

I got it. Thanks for reminding! :star_struck: