psychopy.org | Reference | Downloads | Github

Randomly pick up an image from a big pool of images all belonging to a condition

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

OS (e.g. Win10): win 10
PsychoPy version (e.g. 1.84.x): 2020 1.3
Standard Standalone? (y/n) If not then what?:Y
What are you trying to achieve?:
I am attempting to randomly pick up an image from a list of several images. This is quite simple if you have a long csv with all the image names. However 1) each list of images represent an experimental condition, I would like, depending on the condition, to pick up a random image from a pool.

What did you try to make it work?:
I tried to use an inner loop which calls as condition $imagelist. The outer loop recalls a conditions excel file. In this file, as part of the conditions I have a variable imagelist where I recall the .csv containing the large pools of images. I manage to call the right conditions and that’s nice! However I would like the inner loop to get images randomly from the long image list!!

What specifically went wrong when you tried that?:
I tried to then set the inner loop to take randomly rows 0:2 but instead of taking 3 random rows it only takes the first three of each image list.

Is there a way to do that from the builder entirely?
Thanks a lot

The issue is that selecting a subset of rows occurs before the randomisation gets applied. You can work around this with a snippet of custom code, by letting Builder randomise all the rows, but only allow three of them to be presented.

So set the inner loop to random and don’t select a subset of rows. Then insert a code component (from the “custom” component panel) and in its “End routine” tab, insert some Python code something like this:

# end after three trials have been completed:
if the_name_of_your_inner_loop.thisN == 2:
    the_name_of_your_inner_loop.finished = True

Hi Micheal,

Thanks for your kind and prompt reply! It now works indeed :D!
However, this was aimed to be pushed to Pavlovia. The code you suggested me translates well in JSPsych and I can pilot the experiment on Pavlovia, but interestingly it does not seem to manage to get out of the loop !
Do you, by any chance, have any hints on why this happens?

Many thanks for your help,
Marco

Within the conditional statement try just doing

trials.finished=True

or

trials.finished=true

for JS.
They updated the way the end loops. See Loop.finished=true No longer working for details.

Thanks @Bobby_Thomas

1 Like

Hi Bobby and Micheal,

Thanks for your help,
As suggested I changed the code snippet to indicate trials.finished = true.
The script runs on Pavlovia but still it does not seem to manage to get out of the loop.
I don’t know if I may am missing something. I attach here screenshots of the structure of my task and of the code snippets I inserted as suggested.

screen_3

So the idea is that the primeloop recalls a variable $ImgList that is contained into the .csvs files recalled from the block.xlsx file associated to the block loop. Each csv files correspond to an experimental condition and includes the list of images relevant for that conditions. What I want to do is to be able to pull a single image from each image pool. So what I tried to do was entering once in the prime loop every time then exit from it, and controlling the no. of trials from the blockloop.
This logic seem to fit what I want to do but probably there is a better way to do it?
In any case, I can’t get the reason why once of Pavlovia cannot get out of the loop!

Note that when offline, Micheal initial solution worked :slight_smile: !

Many thanks again for your kind help

I’m wondering if this is an ordering issue. I don’t know much about how a lot of the ordering of loop operations goes on behind the scenes, but one possible cause of your problem would be if in PsychoJS the primeloop.thisRepN is being incremented before your custom code snippet runs.
If you want to always end the primeloop after 1 run, why not try making the conditional statement more inclusive, so try doing

if primeloop.thisN >= 0:
    trials.finished=True

and it should translate to

if ((primeloop.thisRepN >= 0)){
    trials.finished=true;
}

That might not solve it, but it follows the approach of trying the easy fixes first that don’t require any major configuration changes of your task, and if that doesn’t work we can always try more complex edits later.

1 Like

Hi Bobby!!
You had absolutely the right intuition!!! :slight_smile:
This way it works well!!! Indeed the loop must be incremented beforehand by 1!

Ok, I have the last strange issue that I could not easily solve.
My experiment runs well, however, I have a little nuisance with the data output.
Obviously it collects the response anytime it enters in the inner loop, and attach it to the output. (at the moment the inner loop the box “Is trial” is checked (as it should be I suppose?).
However the data output also shows an additional row which refer to the condition that has been picked from the block list referred from the outer loop. Is there a way to have this on the same Row ?

Below an example of how the data look like!

As you can see the first row refers to the response collected while the second row refers to the “blockloop” and indicates the corresponding condition.

I have seen there is another post about this:

But none of the solutions proposed there seemed to work for my case?

Thanks again for your help

Glad that worked!
Unfortunately I know nothing about how PsychoPy saves data, and how to reformat that. I have always just taken the data in the format they come and then done reformatting on my own in excel or R, or some other software. Personally I find that easier, but if it is a big enough issue for you it might be worth making a separate post on the forums.
-Bobby

Very Fair,

Yes is certainly something which you can workaround later in data pre-processing. It looks a bit odd! I will try to ask in a separate post then,
Thanks again a lot for the help, now there are just little minor issues to figure out but I’ve got a working script :).

Cheers,
Marco

1 Like