Showing reward after every 10 trials based on the mean accuracy

Begin Experiment

accuracy = 0
candies = 0
candieImage = "reward/EmptyBox.png" 

End Routine

continueRoutine = True
print('RW_PPC.thisN',RW_PPC.thisN,', candies',candies, 'accuracy', accuracy)
if correct and RW_PPC.thisN > 2:
    accuracy += 1

if (RW_PPC.thisN - 3) % 10 == 9:
    if accuracy > 6:
        candies += 1
    if candies > 0:
        continueRoutine= False
    accuracy = 0
    candieImage = "reward/Candy1.png" + str(candies+1)

I said

You wrote

Please spot the different

In both the cases there is error.

In this case,

candieImage = "reward/Candy" + str(candies+1) + ".png"

the error looks like this

In this case,

candieImage = "reward/Candy1.png" + str(candies+1)

it looks like this

In this case,

candieImage = "reward/Candy1" + str(candies+1) + ".png"

Maybe the problem is not in the syntax, but something else which we might be missing.

Hi all

@wakecarter suggests using something called string concatenation to construct the image filename. In string concatenation you combine several strings to form a larger string. If the string concatenation results in a wrong name, PsychoPy won’t find the file.

Looking at your problem, the first part of your string is “reward/Candy”, the second part is the sum of the variable candies + 1 and the last part is “.png”. You need str(candies + 1) to convert the sum of the integer variable candies + 1 into a string. So you need to construct the string concatenation so that it produces the string results you want! And details matter!

If your intended string is “reward/Candy1.png”, where the number is the variable part, you need to construct the string in the following way, as already mentioned

candieImage = “reward/Candy” + str(Candy + 1) + “.png”

The first two error messages do not match the reported commands. The first error message suggests

candieImage = "reward/candy" + ".png" + str(candies+1)

The second error message suggests

candieImage = "reward/Candy1.png" + ".png" + str(candies+1)

Note the order of the arguments and the extra 1 in the second command. You print candieImage to the console to see if you construct the proper image filename.

print("image filename: ", candieImage)

Best wishes Jens

1 Like

Hii,
Thanks for explaining the issue so nicely. And I would like to apologize for such long delay in response, since I got busy with other stuffs and could not work on this experiment. I understood the part about string concatenation and how that will produce the name of the image expected to be called. However, I still have some issues in the experiment.

  1. About the image calling part, I wanted to call the image only after 10th, 20th, 30th trial and so on, and not after every trial. Currently the image is being called after every trial.
  2. About the calculation of accuracy, I wanted to exclude only the first trial, which is happening. The accuracy value should remain the same on an incorrect trial, and should increment on the next correct trial. What is happening is, on an incorrect trial, the accuracy value is incremented and it remains the same on the next trial even if that trial is correct.
    I have attached an excel sheet which explains the desired accuracy and reward (candies) value and the actual values that are coming.

Begin Experiment:

accuracy = 0
candies = 0
candieImage = "reward\EmptyBox.png"
skipReward = True

End Routine

print('RW_PPC.thisN',RW_PPC.thisN,'candies',candies, 'accuracy', accuracy)
if RW_PPC.thisN >= 0 and respMade == 1 and correct:
        accuracy += 1

if RW_PPC.thisN in [10, 20, 30, 40, 50, 60, 70, 80]:
    if accuracy >= 6:
        candies += 1
        skipReward = False
    accuracy = 0
if RW_PPC.thisN in [10, 20, 30, 40, 50, 60, 70, 80]:
    candieImage = "reward\Candy" + str(candies) + ".png"
    print('candieImage', candieImage)


trial structure for reward.xlsx (10.1 KB)

If possible please help me out with the above queries.

  1. At the moment you are setting skipReward = True in Begin Experiment but you aren’t resetting it to True after it’s False. Add an else condition or reset it in Begin Routine or End Routine before your if clause.
  1. This is the code that isn’t working for you.
if RW_PPC.thisN >= 0 and respMade == 1 and correct:
        accuracy += 1

How are respMade and correct being set and reset?

I added an else statement in the End Routine tab

print('RW_PPC.thisN',RW_PPC.thisN,'candies',candies, 'accuracy', accuracy)
if RW_PPC.thisN >= 0 and respMade == 1 and correct:
        accuracy += 1

if RW_PPC.thisN in [10, 20, 30, 40, 50, 60, 70, 80]:
    if accuracy >= 6:
        candies += 1
        skipReward = False
        accuracy = 0
        candieImage = "reward\Candy" + str(candies) + ".png"
        print('candieImage', candieImage)
    else:
        skipReward = True

The issue remained the same. The first reward is coming after the 10th trial, but after the 10th trial the reward image is coming after every trial.

I added respMade clause because the experiment was treating missed trials (trials with no response made) as correct only and incrementing the accuracy in that case as well.

Your else statement is too tabbed. However, looking at your code I think the best solution is a second else like:

if RW_PPC.thisN in [10, 20, 30, 40, 50, 60, 70, 80]:
    if accuracy >= 6:
        candies += 1
        skipReward = False
        accuracy = 0
        candieImage = "reward/Candy" + str(candies) + ".png"
        print('candieImage', candieImage)
    else:
        skipReward = True
else:
    skipReward = True

Note my change of \ to /

This doesn’t answer my question.

With this code now, the reward image is being displayed only after there is an increment in the candy value. If there is no increment in the candy value (i.e. the accuracy<6), there is no reward image (the previous reward image should repeat itself in this case, i.e., if it is one candy image previously, it should repeat that image). Also, along with this, the code is skipping the part where accuracy should be set to 0 after the 10th trial and start recounting from the 10th trial.

In target routine, where I take the responses on the target and response images are being displayed, I used respMade because if there is a no response on the first trial, there was some error coming up.
So below is the code which I put there to avoid this error and it worked there,
Begin Routine

stimuli = [RW_color1_image_PPC, RW_color2_image_PPC, RW_shape1_image_PPC, RW_shape2_image_PPC]
mouse = RW_PPC_resp
mouse.setPos(newPos=(0,0))
mouserec = mouse.getPos()
respMade = 0

Each Frame

mouseloc = mouse.getPos()
if RW_color1_image_PPC.contains(mouse):
    mouserec = mouseloc
    mousetime= core.getTime()
    correct = (RW_color1_image_PPC.image == RW_CorrAnsPPC_T)
    respMade = 1
    thisExp.addData('clicked_Resp', RW_color1_image_PPC.name)
    thisExp.addData('clicked_Resp_Time',mousetime)
    thisExp.addData('correct_response', correct)
    continueRoutine = False
elif RW_color2_image_PPC.contains(mouse):
    mouserec = mouseloc
    mousetime= core.getTime()
    correct = (RW_color2_image_PPC.image == RW_CorrAnsPPC_T)
    respMade = 1
    thisExp.addData('clicked_Resp', RW_color2_image_PPC.name)
    thisExp.addData('clicked_Resp_Time',mousetime)
    thisExp.addData('correct_response', correct)
    continueRoutine = False
elif RW_shape1_image_PPC.contains(mouse):
    mouserec = mouseloc
    mousetime= core.getTime()
    correct = (RW_shape1_image_PPC.image == RW_CorrAnsPPC_T)
    respMade = 1
    thisExp.addData('clicked_Resp', RW_shape1_image_PPC.name)
    thisExp.addData('clicked_Resp_Time',mousetime)
    thisExp.addData('correct_response', correct)
    continueRoutine = False
elif RW_shape2_image_PPC.contains(mouse):
    mouserec = mouseloc
    mousetime= core.getTime()
    correct = (RW_shape2_image_PPC.image == RW_CorrAnsPPC_T)
    respMade = 1
    thisExp.addData('clicked_Resp', RW_shape2_image_PPC.name)
    thisExp.addData('clicked_Resp_Time',mousetime)
    thisExp.addData('correct_response', correct)
    continueRoutine = False 

In the reward phase, I used respMade to avoid no response trials to be treated as correct.

This is the intended behaviour according to the tab alignments in your code. Please have a go at editing it so that only lines you what to execute when accuracy reaches 6 are tabbed to be executed in this if clause. Remove tabs from the others so they are executed every 10 trials regardless.

I wonder if clauses of the form
RW_color1_image_PPC.image == RW_CorrAnsPPC_T might not be working as intended. Personally I would put that kind of code in End Routine but I don’t think that’s the issue. Try printing you values for correct to the console.

I tried to do what you suggested here, and it seems that worked. Although, the reward image in this case is displayed after 11th trial is completed (which should be displayed after the 10th trial). Another thing is that if the reward image is to be repeated (i.e., the candy value remains the; accuracy<6), the reward image does not display.

print('RW_PPC.thisN', RW_PPC.thisN,'respMade', respMade, 'candies', candies, 'accuracy', accuracy, 'correct', correct, )
if RW_PPC.thisN >= 0 and respMade == 1 and correct:
        accuracy += 1

if RW_PPC.thisN in [10, 20, 30, 40, 50, 60, 70, 80]:
    if accuracy >= 6:
        candies += 1
        skipReward = False
    accuracy = 0
    candieImage = "reward/Candy" + str(candies) + ".png"
    print('candieImage', candieImage)
else:
    skipReward = True

I tried to print all values to make it clear what is going wrong in the code. Please have a look, I ran the ask for 50 trials.

pygame 2.1.0 (SDL 2.0.16, Python 3.8.10)
Hello from the pygame community. Contribute - pygame wiki
RW_PPC.thisN 0 respMade 1 candies 0 accuracy 0 correct True
RW_PPC.thisN 1 respMade 1 candies 0 accuracy 1 correct True
RW_PPC.thisN 2 respMade 1 candies 0 accuracy 2 correct True
RW_PPC.thisN 3 respMade 1 candies 0 accuracy 3 correct True
RW_PPC.thisN 4 respMade 1 candies 0 accuracy 4 correct True
RW_PPC.thisN 5 respMade 1 candies 0 accuracy 5 correct True
RW_PPC.thisN 6 respMade 0 candies 0 accuracy 6 correct True
RW_PPC.thisN 7 respMade 0 candies 0 accuracy 6 correct True
RW_PPC.thisN 8 respMade 0 candies 0 accuracy 6 correct True
RW_PPC.thisN 9 respMade 1 candies 0 accuracy 6 correct True
RW_PPC.thisN 10 respMade 1 candies 0 accuracy 7 correct True
candieImage reward/Candy1.png
RW_PPC.thisN 11 respMade 1 candies 1 accuracy 0 correct True
RW_PPC.thisN 12 respMade 1 candies 1 accuracy 1 correct True
RW_PPC.thisN 13 respMade 0 candies 1 accuracy 2 correct True
RW_PPC.thisN 14 respMade 1 candies 1 accuracy 2 correct True
RW_PPC.thisN 15 respMade 1 candies 1 accuracy 3 correct False
RW_PPC.thisN 16 respMade 1 candies 1 accuracy 3 correct False
RW_PPC.thisN 17 respMade 1 candies 1 accuracy 3 correct False
RW_PPC.thisN 18 respMade 1 candies 1 accuracy 3 correct False
RW_PPC.thisN 19 respMade 0 candies 1 accuracy 3 correct False
RW_PPC.thisN 20 respMade 0 candies 1 accuracy 3 correct False
candieImage reward/Candy1.png
RW_PPC.thisN 21 respMade 0 candies 1 accuracy 0 correct False
RW_PPC.thisN 22 respMade 1 candies 1 accuracy 0 correct True
RW_PPC.thisN 23 respMade 1 candies 1 accuracy 1 correct True
RW_PPC.thisN 24 respMade 1 candies 1 accuracy 2 correct True
RW_PPC.thisN 25 respMade 1 candies 1 accuracy 3 correct True
RW_PPC.thisN 26 respMade 1 candies 1 accuracy 4 correct True
RW_PPC.thisN 27 respMade 1 candies 1 accuracy 5 correct True
RW_PPC.thisN 28 respMade 1 candies 1 accuracy 6 correct True
RW_PPC.thisN 29 respMade 1 candies 1 accuracy 7 correct True
RW_PPC.thisN 30 respMade 1 candies 1 accuracy 8 correct True
candieImage reward/Candy2.png
RW_PPC.thisN 31 respMade 1 candies 2 accuracy 0 correct True
RW_PPC.thisN 32 respMade 1 candies 2 accuracy 1 correct True
RW_PPC.thisN 33 respMade 1 candies 2 accuracy 2 correct False
RW_PPC.thisN 34 respMade 1 candies 2 accuracy 2 correct True
RW_PPC.thisN 35 respMade 1 candies 2 accuracy 3 correct True
RW_PPC.thisN 36 respMade 1 candies 2 accuracy 4 correct True
RW_PPC.thisN 37 respMade 1 candies 2 accuracy 5 correct True
RW_PPC.thisN 38 respMade 1 candies 2 accuracy 6 correct True
RW_PPC.thisN 39 respMade 1 candies 2 accuracy 7 correct True
RW_PPC.thisN 40 respMade 1 candies 2 accuracy 8 correct True
candieImage reward/Candy3.png
RW_PPC.thisN 41 respMade 1 candies 3 accuracy 0 correct True
RW_PPC.thisN 42 respMade 1 candies 3 accuracy 1 correct False
RW_PPC.thisN 43 respMade 0 candies 3 accuracy 1 correct False
RW_PPC.thisN 44 respMade 0 candies 3 accuracy 1 correct False
RW_PPC.thisN 45 respMade 1 candies 3 accuracy 1 correct False
RW_PPC.thisN 46 respMade 1 candies 3 accuracy 1 correct True
RW_PPC.thisN 47 respMade 1 candies 3 accuracy 2 correct True
RW_PPC.thisN 48 respMade 0 candies 3 accuracy 3 correct True
RW_PPC.thisN 49 respMade 0 candies 3 accuracy 3 correct True
RW_PPC.thisN 50 respMade 1 candies 3 accuracy 3 correct True
candieImage reward/Candy3.png
RW_PPC.thisN 51 respMade 0 candies 3 accuracy 0 correct True
RW_PPC.thisN 52 respMade 0 candies 3 accuracy 0 correct True

When the candies value remained the same, there was no display of the reward image. As you will observe, even if the response was not made, it took it as correct, and still incremented the accuracy value. Same thing happened if there was an incorrect response.
Also, from the 11th, 21st, and so on trials, the accuracy value is always 0 irrespective of whether the trial was correct or not.