psychopy.org | Reference | Downloads | Github

Randomly displaying an image within a loop


#1

Hello,
I just realised there is an issue with my experiment: I have a loop which randomises trials, but in the middle of the trial I need to display an image so that participant are assigned to one of two conditions and perform a certain action accordingly.
Now, I just realised that this image is not displayed randomly - it is attached to the trial in the same row in excel, so even though psychopy can randomise trials within the loop, it does so across the whole row.
There have been a few similar posts on here but none of them offered solutions, so I’m asking for help. Here are my ideas so far, but I’m not sure how to implement them:

  1. create a new file for just that column - but I can’t tell psychopy to only choose 1 row and then not choose the same one later on
  2. Instead of using filename or variable path, combine the two, that is, when adding an image refer to “stimuli_list.xlsx/$image1” - but psychopy doesn’t seem to like this
  3. Code the routine using random() function - I don’t know whether I should refer to png files or stimulus image in psychopy or excel columns, either way not sure how to start the code.

Basically, any suggestions on how to randomise something other than location independently of the other variables specified in excel would be welcome, I’m happy to provide more information but didn’t want overload the post.

Thanks


#2

Hi,

I’m new to all this, so please keep that in mind when reading my suggestion.

You might want to try creating a code snippet in the trial that runs first. In the Begin Experiment you could define all your conditions instead of in the Excel file. Then in the Begin Routine section you could use the shuffle(X) function to shuffle the variable X. You can use shuffle(X) on each line to shuffle each of the variables in turn. This would randomise the order of those variables independently of each other. I’m not sure if that is what you’re looking for, but it sounds like it might be


#3

Thanks for your reply! I think that makes sense, but that’s exactly what I’m having trouble with - how do I define the conditions within code? I know how to randomise things like location, because then I’m referring to something within psychopy, like “[0.5, 0]”
but how do I refer to “image.png” in a way that psychopy will understand?


#4

Hi Agata,

I am not someone who codes in Python and am new to PsychoPy, so again please heed caution in taking any of my advice. But I think lists take strings, so imglist = ["image1.png", "image2.png"] should be an acceptable list to provide in the code snippet. Instead of referencing the excel file in the trials loop you would reference $imglist, I think. And then when you call the shuffle at the start of the routine, you’d shuffle(imglist).

If my thinking is correct, the below screenshots will create the image list at the start of the experiment, and then at the start of the routine (i.e., start of this trial) the image list will be shuffled. Then the image that is displayed will be the name that is in the first position of the variable imglist after shuffling, i.e. imglist[0]. Note that the code snippet happens before the image because it’s above the image in the order of the trial components.




#5

Thank you, that’s brilliant! I was wondering if there was a way to randomise it further, because right now I have 2 options, so if I shuffle on every trial, they’ll always alternate. What if I had 6 different images that I wanted to shuffle, and I wanted to ensure that each of them was only displayed once?
I removed [0] from the image referent hoping that it would make psychopy work through a shuffled list “row by row”, but that didn’t work (it didn’t understand what I was referring to)
I was just wondering if that could also be an option
Thanks


#6

imgList.pop() extracts the next remaining entry from the list.