Show Different Reward Image after X Number of Trials

PsychoPy version: 2022.2.5

I am new to PsychoPy and am trying to convert an experiment that worked on a previous version to this new version. The task is to choose between two pictures after an audio cue. There are a total of 32 trials, but after every 8 trials, I would like a new image to be shown (as a reward). I would also like the code to count whether the chosen image was correct.

Below is an image of the loops I am working with. The participants are shown the two images in “testing” and then asked to choose one of the images in “choiceTest”. After 8 trials, I would like “unicorn1” to show, then have it go back to “testing” and “choiceTest” for another 8 trials before “unicorn2” is shown. This would continue until all 32 trials are complete.

Again, this task was coded on a previous version but unfortunately the code doesn’t work. I would like the new code to do exactly the same thing as the old code, so I pasted it below in case someone can “translate” it for me.

In “End Routine” of “choiceTest”:

if picleft.contains(mouse_2):
    continueRoutine = False
    thisExp.addData ('RT', t)
    thisExp.addData('clicked', pic1)
    if target == pic1 :
     score = 1
    else :
     score = 0
    thisExp.addData('score', score)
elif picright.contains(mouse_2):
    continueRoutine = False
    thisExp.addData ('RT', t)
    thisExp.addData('clicked', pic2)
    if target == pic2 :
     score = 1
    else :
     score = 0
    thisExp.addData('score', score)

In “Each Frame” of “unicorn1”:

if testingLoop.thisTrialN == 7:
        continueRoutine=False

Hi @jekuo,
This looks do-able, especially if it was working previously.
When the code doesn’t work, are you receiving an error message, crash, or other unexpected behaviour?
Will this study be run offline (PsychoPy) or online with Pavlovia (PsychoJS)?
What previous version of PsychoPy did you use when the code was working?

-shabkr

Thank you so much!

To be frank, I am taking over this project from lab members who are not available to help, so I don’t know much about the previous versions used. I think the study was originally made in 2020 - it was definitely a few years ago. I would like the study to eventually be run with Pavlovia.

I downloaded the previous study and tried to run it on this new version and it got stuck on a different code in an earlier routine (the study begins with a practice loop before going to the testing loop from my original post). Because of this, I rebuilt the study in the new version of PsychoPy and fixed the first code that didn’t work. Now, when I’ve copied the code to my new, rebuilt study, I get this error:

TypeError: only size-1 arrays can be converted to Python scalars

It doesn’t make sense to have code which includes continueRoutine = False in End Routine. Is that code supposed to be in Each Frame?

In the original experiment, the code was in End Routine. I moved the code to Each Frame and received the same error message:

TypeError: only size-1 arrays can be converted to Python scalars

It looks like part of the code is receiving a long array when it wants a single number, but is there a little more detail to the error message? Usually error messages start with a line such as:
File “C:\Program Files\PsychoPy\LONG_PATH”, line XXX, in FUNCTION
and this can help narrow down the specific error you are experiencing.

For your “Each Frame” of “unicorn1” code:

if testingLoop.thisTrialN == 7:
        continueRoutine=False

I would expect to see unicorn1 on every trial except trial 8 (“end routine early if the trial is number 8”). I might switch it to != 7 depending on the behaviour once the error messages are fixed.
-shabkr

I changed the “unicorn1” code and will see if it works once the earlier issue is resolved. Here is the full error message I get before it reaches “unicorn1”:

File “/Users/benitezlab/Desktop/WW_1_kids/WW_1_kids_lastrun.py”, line 1953, in
if picLeft2.contains(mouse2):
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/visual/basevisual.py”, line 894, in contains
return pointInPolygon(xy[0], xy[1], poly=poly)
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/psychopy/visual/helpers.py”, line 64, in pointInPolygon
return mplPath(poly).contains_point([x, y])
File “/Applications/PsychoPy.app/Contents/Resources/lib/python3.8/matplotlib/path.py”, line 547, in contains_point
return _path.point_in_path(point[0], point[1], radius, self, transform)
TypeError: only size-1 arrays can be converted to Python scalars

Thanks for the longer error message, that does have some helpful info.

It looks like something related to the if-statement might be the culprit. As a first step, I would double check the names of your variables picLeft2 and mouse2 to make sure they match with the relevant component names in your routine.
(the elif line will likely need to be double checked as well). To further debug, you could also try making a very minimal experiment (as few routines/components as possible) that is able to produce the same error message.

Thank you so much!! Yes, this worked - I had to change the names of “mouse2” to the correct component and had to change “target” to “correct”, which matches my excel spreadsheet. I’ve pasted the final code at the bottom of this reply in case it would be helpful for anyone else reading the thread.

However, I have run into another issue - now when I run the experiment, it gets to “choiceTest” (the routine that has the participant use their mouse to click on one of the images) and doesn’t let me click the stimuli. Instead, if I hover over the image, it chooses it. I haven’t altered the component, and it did work previously. Right now the component properties are:

Any insight into fixing this would be helpful!


Fixed Code:

if picLeft2.contains(clickchoice):
    continueRoutine = False
    thisExp.addData ('RT', t)
    thisExp.addData('clicked', image1)
    if correct == image1 :
     score = 1
    else :
     score = 0
    thisExp.addData('score', score)
elif picRight2.contains(clickchoice):
    continueRoutine = False
    thisExp.addData ('RT', t)
    thisExp.addData('clicked', image2)
    if correct == image2 :
     score = 1
    else :
     score = 0
    thisExp.addData('score', score)

The above behaviour is likely related to the code pieces mentioned by wakecarter, the behaviour you want will determine where the code should go.

  • If you want the routine to end when the mouse hovers over the image, you want the code in Each Frame and include the line continueRoutine = False in the relevant if-statements. This sounds like the current behaviour.
  • If you want the routine to end when the mouse is clicked, you want the code in End Routine (because the Mouse component End routine on press : valid click will end the routine for you). The continueRoutine = False lines shouldn’t be needed.

Hope that helps,
-shabkr

Yes, that worked. Thank you so much for all your help!!

2 Likes