Pick items randomly when the condition file has 'R'

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win7):
PsychoPy version (e.g. 1.83.0):
**Standard Standalone
What are you trying to achieve?:
I have a very simple presentation of images in my experiment. I have set it up using condition file called ‘cond_train.xlsx’. Now I selected the ‘LoopType’ to be random so the rows are already randomly presented.

What I want is, when ever I have ‘R.bmp’ in the ‘cond_train.xlsx’, I want the program to pick any one of the 24 items (contains 24 images) in the folder ‘R’. Is it possible?

con_train.xlsx (9.7 KB)

Many thanks in advance

Kuppu

You could Definately do this by adding a code component to your loop in builder view and doing something like …

In the “begin experiment” tab insert

from psychopy import numpy 

ImageList=(INSERT LIST OF IMAGE PATHS/NAMES SEPERATED BY COMAS)

In the “begin routine” tab insert

If image=='R.bmp':
    ThisImage=numpy.random.choice(ImageList) 
else:
    ThisImage=image

Assuming that “image” is the name of the variable column that R.bmp would be in in your conditions file.

Then just set your image in the builder component to be $ThisImage

Thanks Rebecca

I Tried this gives an error unexpected character after line continuation character - a syntax error

The ‘loc_2’ is the header for the column in which I have R.bmp’s . It seems to have problem in locating the folder in which the pictures are. Folder ‘PicFolder’ has R1.bmp,R2.bmp,R3.bmp.

Could you help?

Thanks

If your images are stored in a folder called PicFolder/, that folder name needs to be in quotes with each filename. At the moment, you have it outside the quotes, which means that Python will try to interpret PicFolder as a variable (which doesn’t exist) and also get confused by the special character /

i.e.

imageList = ['PicFolder/R1.bmp', 'PicFolder/R2.bmp', 'PicFolder/R2.bmp']

Dear Michael

Thanks. I made the change and the error is

I think it is because it is looking for ‘R.bmp’ image, which is not there. It should rather go in to the condition file and look for ‘R.bmp’ and if it finds one it should then locate the folder and pick one of the three images from the folder randomly. Could you fix this problem?

I wanted to go beyond the problem, so I I just created a file R.bmp to temporarily get around this) and see what the program does. It now throws this error

Any suggestions will help.
Thanks for your time

Hi There,

So sorry for the delayed response to this. Did you get this sorted?

This last error is just because you need to import numpy at the start of your experiment. This can be done if you just put

from psychopy import numpy

in the “begin experiment” tab of your code component

Becca

Rebecca is on to it, however Builder does actually import numpy by default. It just does it like this, though:

import numpy as np

So you don’t need an extra import statement, but you should use np in place of numpy as necessary, e.g. np.random.choice()

1 Like

Dear Rebecca and Michael

Sorry for the delay in getting back. My problem was I wanted to select one of the images randomly from a folder named ‘rand’, if the condition file had ‘R’.

My condition file looks like this

cond_file

I have created this under ‘Begin Experiment’ of my code component as per suggestions

ImageList=['rand\R1.png','rand\R2.png']

and, under ‘Each Frame’

if item2=='R':#item2 is the column header in condition file 
    ThisImage=np.random.choice(ImageList)
else:
    ThisImage=item2

This gives me this error.

I think it is a simple problem but could not get my lame head around this

Any suggestions?

Thanks in advance

kuppu