| Reference | Downloads | Github

Correct Answer comparison in looping response

Hello all,

I am trying to create an auditory digit span task, where participants will hear the stimulus (digits 1-9) being read out loud, and will respond accordingly. I am running PsychoPy 3 on Windows, using primarily the Builder with some Code Components in between.

The following are the constraints of the test:

  • There will be two types of assessment: forwards (FA) & backwards (BA).
  • The baseline number of stimuli per trial is 3 for FA and 2 for (BA). For example, a trial with 3 stimuli would be 4 - 8 - 1 being read aloud consecutively.
  • A response is correct if responded in both digits and order of presentation. For example, a 4 - 8 - 1 response for the trial above, in that exact order.
  • If a correct response is given, the next trial will increase by 1 digit (e.g. the next trial will have 4 stimuli, for the trial above).
  • If an incorrect response is given, the next trial will remain with the same amount of digits.
  • If two consecutive incorrect response is given, the next trial will decrease by 1 digit.
  • There will be a total of 14 trials each for both FA & BA.
  • By default, participants run through the forward assessment first, then backwards.

The following is what I’ve done so far:

  • I have created a loop for the stimulus presentation that is attached to an Excel conditions file (with the stimuli digits 1-9 in the corresponding rows linked to a .wav sound file, and a correct answer column corresponding to the sound file digits). This loop is in a random order (sampling without replacement from the .wav stimuli list), with the number of repeats (total stimulus per trial) dependent on the participant’s performance on the previous trial, starting with 3 on the first trial.

  • Following this is a pair of routines surrounded by a loop. The first routine is to intake a keyboard response constrained by key presses 1-9, and the second routine is to display a feedback to the participants for the key pressed. The loop is there to intake the responses corresponding to the total number of stimuli for that particular trial.

Here is my experiment flow (I have yet to add the adaptive staircase loop surrounding the Experiment_Trials & Response_Feedback routines):

The problems I am facing:

  1. The .csv data output file seems to only be correctly comparing the correct answers for the last response of the response loop, while all previous response being stored as incorrect. I have troubleshoot and confirmed that the corrAns variable works as intended, and the comparison of correct answer to participant response being done by the code also works. However, it only works for the last iteration of the loop, no matter how many times the iteration count is.

Here is the keyboard response properties for that routine:

Here is the Excel conditions file from which both the audio stimuli is being drawn upon & the correct answer list:


Here I have attached the code for the loop surrounding the above response routine. (Note: Response_Trial_Loop is the response loop, while defaultN is a variable I use to track the loop) (10.5 KB)

Finally, I have also attached a dummy data output file that better illustrates this problem.

001_Auditory Digit Span Task_2019_Jun_04_1236.csv (2.5 KB)

  1. I have yet to attempt to create the staircase loop because of the problem above, as it would depend on the correct/incorrect response from participants. In any case, I would appreciate it if I could be advised that the direction I am heading towards is correct.

Here is the PsychoPy experiment file as well.

Auditory Digit Span Task.psyexp (41.4 KB)

Thank you so much in advance!


can you attach the condition file?

I did, it is literally the screenshot above. I am attaching it anyway!

Stimulus.xlsx (8.4 KB)


Didn’t read closely your screenshot of condition file and assumed you created the stimuli in psychopy on the fly, so I asked for your condition file to run the exp myself, which is impossible without the external stimuli. my bad.
my understanding is that you current experiment flow is actually a “trial flow”, by trial I mean an experimental trial (referred later as eTrial) not a psychopy trial (referred laster as pTrial). You are trying loop through each digit in the stimuli pTrial and the loop through each key in the response pTrial .
I would recommend using a single pTrial for a eTrial. I would use your digit .wav files to create multiple stimulus .wav file each for an eTrial grouped by number of digits in matlab and present them in a single sound component (you can still use a code component to choose if to inc/dec number of digit based on subject response. I would use a single keyboard component to record response for every eTrial changing a few properties of the component.

In case you didn’t realized it: I am just a user not a developer. Be skeptical of my design

The issue here is what constitutes a “trial”. PsychoPy usually works on the idea that one row in the data file contains all the data pertaining to one trial. But the nature of your task is that one row in the data file is going to correspond to each response within a trial. Similarly, the correct answer variable must be specified at (and update for) the level of every individual response.

So check your posted .csv file. The first digit span is [1, 5, 7]. The corrAns column currently specified each of those as being the respective answer for that row of the file. But note that that cycle of presentation is completed before you start collecting responses. At that point, the value of corrAns is fixed at its last entry, which is why the only correct score you get is when you press 7 (and note that it would have been scored correct even if you typed that number out of order).

What you need to do is build up a list of correct answers during the presentation phase. And then during the response phase, you compare each answer to its corresponding entry in that list. Lets say you have a variable called correct_answers, which is [1, 5, 7]. Then in your keyboard component, the “Correct answer” field would be specified something like this:


i.e. you index the list of answers by the current loop iteration number, so that in this case, 1 would be the correct answer for the first iteration (iteration 0), and 7 would now only be scored correct in the last iteration (iteration 2).

How to build the list? Something like this in the “Begin routine” tab of the code component in the “ExperimentTrials” routine, to initialise the loop as empty, and then grow it on each iteration with the current value:

if Experiment_Trial_Loop.thisN == 0:
    correct_answers = []


Note that you could also now use that list to set the number of iterations of your response loop. i.e. you could put something like this in the nReps field of the loop:


instead of having to maintain some other variable (currently default_N).

You could probably also try de-selecting the “Is trials” option from the Experiment_Trial_Loop dialog, to stop it saving data. That would simplify the structure of your data file, as you aren’t really recording anything of interest there, just presenting stimuli. You would need to record the correct answer on each trial manually, though, then. Something like this in the “Begin routine” tab of the code component in the “Response_Trial_Routine”:

thisExp.addData('correct_response', correct_answers[Response_Trial_Loop.thisN])

Thank you very much, your explanation was clear and helped me understood the logic better.

I kept the “Is trials” option from the Experiment_Trial_Loop as it provided information on what stimuli was actually presented, in addition to the correct response.

All in all, it works as intended! Now, onto creating the Staircase loop to handle this behaviour.



1 Like