psychopy.org | Reference | Downloads | Github

Ending a loop after 5 correct answers (and not after a x% of correct answers)


#1

Hi!

I’m creating an experiment with a training phase in which participants have to guess if an image stimulus belongs to a particular category or not ((y)es/(n)o keyboard responses). After each response, a feedback is provided (correct/incorrect) in order for them to learn the rule that is behind such categorization. I’ve created this within a loop and it works fine. What I want to introduce now has to do with the ending of this loop: I want it to end once the participant has correctly answered 5 times for each stimulus (i.e., once he has learned the categorization rule).
I’ve been reading a lot of related topics but all I’ve found has to do with %s of correct answers, and I don’t know how to “translate” this information into my specific case.

Thank you very much!


#2

Hi @Lepton. You could create a list which keeps track of how many answers are correct in a row. In a code component add the following to the relevant tabs:

Begin Experiment

correctResp = []

Each Frame

keys = event.getKeys()

if len(keys):
    if conditionalForCorrectResponse:  # If response is correct, append a 1
        correctResp.append(1)
    else conditionalForIncorrectResponse:  # If response is incorrect, append a -1
        correctResp.append(-1)
    if sum(correctResp[-5:])==5:  # If last five responses are correct, end routine
        continueRoutine = False
        trials.nReps = trials.thisN  #  Note, you may need to set nReps to current nTrial, otherwise reps continue
    if 'q' in keys:
        core.quit()

#3

Hi @dvbridges. Thank you!

I introduced the code component in “Training”. I tried what you suggested but after 5 correct responses, reps continue, as you indicated in:

trials.nReps = trials.thisN # Note, you may need to set nReps to current nTrial, otherwise reps continue

What kind of value should I indicate in the loop’s “nReps $” in order for the trial to end after 5 correct answers?

Thank you and sorry, this is my very first time with Psychopy


#4

@Lepton, try replacing trials with trialsTraining, which is the name of your loop.


#5

@dvbridges, yes, sorry, I didn’t ask my question properly. This is already done. I mean the nReps$ in the Properties window of the loop (picture). I read somewhere to try with a high, random number (such as 999) so to let the rule apply, but it didn’t work.

nReps|690x142

This is how I adapted the code:

keys = event.getKeys()

if len(respTraining.keys):
if respTraining.corr: # If response is correct, append a 1
correctResp.append(1)
else: # If response is incorrect, append a -1
correctResp.append(-1)
if sum(correctResp[-5:])==5: # If last five responses are correct, end routine
continueRoutine = False
trialsTraining.nReps = trialsTraining.thisN # Note, you may need to set nReps to current nTrial, otherwise reps continue
if ‘q’ in keys:
core.quit()

Thank you for your help!


#6


#7

@Lepton, are you using a conditions file to control your stimuli and have you defined a correct answer column? It looks like you are, so we can change the code component to do the following:

Begin Experiment

correctResp = []

End Routine

if respTraining.corr:
    correctResp.append(1)
else:
    correctResp.append(-1)
if sum(correctResp[-5:])==5:
    continueRoutine = False
    trialsTraining.nReps = 0

You can set nReps to a high number, and the number of trials will be nReps * trials in the conditions file. When you set nReps to zero, no more repetitions of your trial blocks will occur.


#8

@dvbridges, yes I am using an excel and a correct answer is assigned. I also have a Feedback routine within the loop.
I tried to add this new code component you suggest at the bottom of the “Training” routine, but the test ends after 5 correct answers in total (=sum), while I want it to end after 5 correct answers for each item. Is it possible? I tried to add the code in “Each Frame” instead of in “End routine”, but then what prevails is the nReps in the “Properties” window of the loop.

By saying “You can set nReps to a high number, and the number of trials will be nReps * trials in the conditions file. When you set nReps to zero, no more repetitions of your trial blocks will occur.”, do you mean I should add a column in the conditions file with the number of repetitions, such as in the picture?

nReps_trialsTraining

nRepsProperties

Thank you very much


#9

Hi @Lepton, sorry I did not mean literally define the nReps variable like this. Your total number of trials will equal to nReps * trials, so if nReps == 10 and trials==10 then you will have 100 trials in total. So, if you just use 999 as nReps, you will most probably have enough trials.

To get N correct responses for each item, you will need to change the code, and add another column in your conditions file, something to identify each item. So, create a new column called picID, and add some unique identifier for each stim, e.g., A, B, and C for your 3 old stim that you are using in your training.

Next, add the following to relevant tabs in the code component (note, I am only using 2 correct for condition, just to shorten the demo):

Begin Experiment

from collections import defaultdict
correctResp = defaultdict(list)

Begin Routine

checkList = []

End Routine

# First, add a response to the dictionary for item identified using picID
if respTraining.corr:
    correctResp[picID].append(1)
else:
    correctResp[picID].append(0)
# Loop through the dict to determine correct items per stim
for key, val in correctResp.items():
    if sum(val[-2:])==2:
        checkList.append(True)
# if all stim have 2 correct consecutive responses, finish
if np.all(checkList) and len(checkList) == len(trialsTraining.trialList):
    continueRoutine = False
    trialsTraining.nReps = 0 

#10

Hi @dvbridges. I’ve been trying but I obtain the following error: Type error: string indices must be integers in correctResp[picID].append(1)
So I guess I should change the value of [picID] for another value, but there’s something that I am missing here; should I add a response value indicating a “True” meaning, such as “1”? The correct answer for each stimulus is either ‘y’ or ‘n’, and is determined by the parameter $corrAnsLep in the conditions file.
Sorry and thank you for your help


#11

@Lepton, would you be able to post the full traceback or error? Do you mean, should you add a response to the data file? Also, here is my working example - see how they compare

conditionalEnd.psyexp (7.7 KB)
condition.xlsx (8.9 KB)


#12

Hi @dvbridges. It finally works!! The error message does not appear anymore (although all I’ve done is downloading and opening your files… quite strange) and everything works fine. Thank you very, very much for your help and your patience.