psychopy.org | Reference | Downloads | Github

Keeping a running count of score and skip the loop conditionally

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

OS (e.g. Win10):
PsychoPy version (e.g. 1.83.x):
**Standard Standalone
What are you trying to achieve?:
Dear all,
I am trying to set up an experiment in which I have two separate loops. Each loop has a ‘trial’ in its routine component (if I am saying it correctly!). On each trial there is an auditory signal presented and participant has to select (using mouse click) either ‘picture A’ or ‘picture B’. I have set up the ‘corrAns’ in my conditions file and added under each frame aspect of code component some codes like the one below, so it scores.
###########################
for stimulus in [image_3,image_4]:

    # check if the mouse is pressed within the current one:
    if mouse.isPressedIn(stimulus):

        # Yes, so store the reaction time in the data:
        thisExp.addData('RT', t)

        # check if the stimulus' image filename matches the correct answer:
        if stimulus.image == eval(corrAns):
             thisExp.addData('score', 1)
        else:
             thisExp.addData('score', 0)
             continueRoutine = False

#############################################
This part of the experiment is basic and it works fine.
I have 16 trials in a loop (let’s say loop 1, i.e., the condition file from which loop 1 reads has 16 rows-one row per trial ). I have set the loop type to ‘Sequential’ so it goes in order I mentioned in condition file. The order is as below
Loop 1
Items Trial Running item ‘score’
1 Trial 1 Trial 2 If ‘0’ terminate the loop
2 Trial 3 Trial 4 If ‘0’ terminate the loop
3 Trial 5 Trial 6 If ‘0’ terminate the loop
4 Trial 7 Trial 8 If ‘0’ terminate the loop
5 Trial 9 Trial 10 If ‘0’ terminate the loop
6 Trial 11 Trial 12 If ‘0’ terminate the loop
7 Trial 13 Trial 14 If ‘0’ terminate the loop
8 Trial 15 Trial 16 If ‘0’ terminate the loop

…There will be instructions, followed by next loop.
The problem: how do I keep a running count after each item (i.e., two trials), so, after every item is completed, if the score is ‘0’ for that item (that is got ‘both’ the trials within the item wrong, note the max score can be 2), I can break the loop, i.e., the assumption is the participant is not going to do the trials in the next item for sure (as the items are increasingly difficult as it goes down).
I know I can start the score count by adding ‘score = 0’ at the ‘begin routine’ aspect of code component and start the count. Keeping the running count and checking the score after every item is completed seems the tricky bit.
Any help will be appreciated.

Thanks

I’m a bit confused (yes, you do have the terminology a bit backwards: Components (e.g. an image, a sound, etc) go together to make up a Routine, and a Trial consists of one or more routines).

Can we see a screenshot of the flow panel of your experiment, to help understand it?

Dear Michael

Sorry for not being clear. You are right about the trial and routines, and I should have used terms like ‘Sounds’ ‘Images’ instead. I am giving it a go again in more detail here.

I got ‘sounds’ and ‘images’ as routines in a trial. Trial ends after clicking one of the images, and the responses are scored. There is nothing tricky in this bit and I got it working perfectly.

See my .xlsx condition file and snap shot of data. As you can see that every item has two trials within it. I want the software to do a ‘running count’ [I think it can be made by checking the score after every item is completed (i,., 2 trials within an item is completed)] and check the ‘running count’ against the criteria. The criteria is if the ‘running count of an item’ is less than 1 (i.e., ‘0’) I want the loop to end, without presenting the remaining items, so the next aspects of the task can be run (as you can see in the screen shot that I have some instructions and another similar loop that follows). Note that I do not want to see the running count on the screen, I am happy for the machine to do it covertly.

I believe it could be done using the code below…

#####################

for stimulus in [image_3,image_4]:

    # check if the mouse is pressed within the current one:
    if mouse_3.isPressedIn(stimulus):

        # Yes, so store the reaction time in the data:
        thisExp.addData('RT', t)

        # check if the stimulus' image filename matches the correct answer:
        if stimulus.image == eval(corrAns):
             thisExp.addData('score', 1)
             RunScore = RunScore + 1 #needs to be running total? 
        else:
             thisExp.addData('score', 0)

        # end the trial once done here:
        continueRoutine = False 
        break

#######################
I inserted a ‘RunScore = 0’ at the Begin Routine and in the Each Frame as well, in an attempt to keep a running count, but i couldn’t figure out how to make use of this and stop the loop. At the moment ‘RunScore’ is not doing anything.

FYI: The program at the moment runs perfectly.

BACKWARD.xlsx (10.6 KB)

e525c5b0cbe8b50eb07fc8916e405.PNG" width=“649” height=“492”>

Thanks a lot for your time again

Yours
kuppu

I’m still a little confused by the description :wink: ‘Routines’ are the things you have labelled as play and trial. Routines contain components (like the things you have labelled sound_4 and text_8). Trials are just a conceptual thing, generally made up of one or more routines, such as the series of two routines play and trial. Using the same terminology helps communicate much faster (and you should specify what you mean by custom terms like ‘item’). These are all things you’ll understand implicitly, but for someone looking in from the outside, it can be a challenge.

But anyway, I think one of the problems is that you keep you keep zeroing your running score. e.g. if you put RunScore = 0 in the Each frame tab, then typically at 60 times a second, you are ensuring your running score stays at 0. Similarly, if you set it to zero at the beginning of a routine, it can never exceed 1.

I think what you want is only to set it to zero at the beginning of the loop. So in your Begin routine tab, insert something like this:

if trials_2.thisN == 0: # only at the start of the loop
    RunScore = 0 # initialise the counter

But I don’t see in the code you’ve posted so far what you actually do with that running score variable, in terms of making a decision.

Hi Michael

Thanks

I tried adding this, and it gives an error as “Name Trial_2 is not defined”: see the pic below.

Yes, you are right!. I didn’t have much idea about how to use the Runscore to end the loop. My inclination is to to check the Runscore variable after every item (that is two trials within it) has been completed and if it is ‘0’, terminate the loop? else continue to next item?
I have little idea about how to use it.
Looking forward to hearing from you.

I guess that error happens because you use the same routines in the second loop, which has a different name (trials instead of trials_2). You need a way to refer to the correct name of the loop. This problem has a workaround here:

https://groups.google.com/forum/#!topic/psychopy-users/gbnt6UxdLmg

But I think in the mean time Jon has added a way to refer to the enclosing loop generically rather than by its specific name but for the life of me, I can’t remember how that is done. perhaps someone else can pipe in here.

It is very easy to terminate a loop.

e.g. put something like this at the appropriate point:

if RunScore == 0:
    name_of_loop.finished = True

But once again, you’ll need to find a way to to refer to the enclosing loop other than by using its name directly.

Dear Michael
Thanks for the link to address the Loop issue. This part seems alright at the moment. See below…

I added something like this in ‘BeginRoutine’ after seeing the link.

I added, the code after ‘continueRoutine=False’ . See below…

Now, the task starts but after the first sound is presented, it freezes (it doesn’t take my mouse click). I see from the response sheet that it does not record the mouse response for the only trial that was presented. I think this is just a small problem.psychopy_data_2017_Feb_12_1118.csv (455 Bytes)

The section which I feel will need some thinking is the ‘RunScore==0’. My understanding when I used the term ‘RunScore==0’ is that it will keep a count of ‘Score’ variable after every ‘Item’ (i.e., two trials).
First obvious problem is I do not see in our code any link between these three variables ‘ITEM-SCORE-RUNSCORE’?. That is, somewhere we need to mention that ‘Runscore’ need to be checked after every two trials (1,2-check…3,4-check…5,6-check…7,8-check and so on) and execute the end loop command.

Could you suggest more directions on that?

Thanks for your time again.

Note the indenting on this:

if RunScore == 0:
    name_of_loop.finished = True

It’s important you use consistent indenting (the usual standard is four spaces = one indent). You are currently using 5, 5, and 1 spaces in that one code snippet. This can trip Python up.

On what the score is, I’m not really sure I understand your task and the fact that there are three things (item, score, runscore). There seems to be some ambiguity about what a trial is and hence it’s difficult to give code to count across two of them. You should give a very precise description of what needs to happen and when, using the actual names of your routines and loops.

Dear Michael
Thanks for your suggestion on indenting. I tried implementing and here I am giving it a try to be lot clearer than before.
Trial:

  1. Two ‘images’ appear on screen
  2. A ‘sound’ is presented
  3. One of the’ images’ needs to be clicked
  4. Trial ends. Accuracy is 'Score’d (‘1’ for correct, ‘0’ for incorrect)

[Item:two consecutive trials. See my condition file attached. Trial 1 & 2 comprises Item 1, Trials 3 & 4 comprises Item 2 and so on. Since it is not implemented in trial set up here, I am going to be stop using this term in this explanation and in any future explanations-this seems to only cause confusions. You can see what I mean from the condition file below.
BACKWARD.xlsx (10.6 KB)]

Score:
‘0’ or ‘1’ depending on the accuracy of response.

Runscore:
It is just version of ‘Score’ variable that needs to be calculated after (Moving window kind) set of 2 trials. Like below

  1. after trial 2 (summing score of trial 1 & 2 )
  2. after trial 4 (summing score of trial 3 & 4)
    and so on.
    Note-Max run score at any point is ‘2’.

What I want?

‘RunScore’ be checked after every two trials mentioned above and if it is ‘0’ the loop should be terminated without playing next set of trials. For example, lets say after presenting the trials 1 and 2, the Runscore is 2 (total score will be 2 as it increases linearly), the next trial will play. But, lets say after trial 6, the run score for trial 5 and 6 is 0 (the total score may be 4 at this point which we are not worries about), we will terminate the loop.

This is how my code looks at the moment.

We are making progress, I know I am struggling to project my requirements. I could design a whole task with your help last time, I am positive I could get enough help for this as well.

Looking forward to hearing from you.

Dear all

I have set up two loops with identical routines. However, with different conditions.
With some inputs from here, I have told the psychopy about the loops, by pasting this in ‘Begin Routine’ of the code component.
################
if trials_2.finished:
thisLoop = trials
else:
thisLoop = trials_2
#################
The condition file for the loop 1 has columns such as below…

itemnumber trialnumber
ITEM_1 TRIAL_1
ITEM _1 TRIAL_2
ITEM_2 TRIAL_1
ITEM_2 TRIAL_2
ITEM_3 TRIAL_1
ITEM_3 TRIAL_2
ITEM_4 TRIAL_1
ITEM_4 TRIAL_2
ITEM_5 TRIAL_1
ITEM_5 TRIAL_2
ITEM_6 TRIAL_1
ITEM_6 TRIAL_2
ITEM_7 TRIAL_1
ITEM_7 TRIAL_2
ITEM_8 TRIAL_1
ITEM_8 TRIAL_2

As usual one row per trial and depends on response from the participant it will be scored as ‘1’ or ‘0’. I had started a counter such as ‘score=0’ at the ‘Begin Experiment’ of code component with the hope that I will be able to achieve the following.
I want to stop playing the rest of the rows of the condition file if an ‘Item’ score is ‘0’ (i.e., score of Trial+Trial2) and move to next loop.

My code component at the moment looks like this
####################################
for stimulus in [image_3,image_4]:

# check if the mouse is pressed within the current one:
if mouse_3.isPressedIn(stimulus):

    # Yes, so store the reaction time in the data:
    thisExp.addData('RT', t)

    # check if the stimulus' image filename matches the correct answer:
    if stimulus.image == eval(corrAns):
         thisExp.addData('score', 1)
         score = score + 1
    else:
         thisExp.addData('score', 0)

    # end the trial once done here:
    continueRoutine = False

    # stop any further checking:
    break

#####################################################I am getting no where with this,
Any help is appreciated …
Thanks in advance