Redo all incorrect trials

Hi all,

I’m trying to create a script that will have the participant redo trials that they got incorrect. Essentially, the participant is first shown pictures in a specific location on the screen. Later, they must drag and drop the image in the correct location (coordinates cx,cy). If they get a trial wrong they’re shown the correct position, a sound is played and they have to repeat it, if they get it right then they don’t. they keep repeating trials they got incorrect until they were all right. Below is my code, I know it’s a bit messy.

TestIteration = 0

TestIteration += 1
# Set the excel sheet to use for this trial
TestExcelSheet = TestPhaseFilename + str(TestIteration).zfill(2) + '.csv'

# set up handler to look after randomisation of conditions etc
TestingPhase = data.TrialHandler(nReps=1, method='random',
    extraInfo=expInfo, originPath=-1,
    trialList=data.importConditions(IncorrectAnswers),
    seed=None, name='TestingPhase')
thisExp.addLoop(TestingPhase)  # add the loop to the experiment
myMouse.setVisible(1)
WrongAnswers = []
IncorrectAnswers = []

CorrectMsg = visual.TextStim(win = win, units = 'norm', height = 0.1,
            pos = (0,0), text = "Correct!", alignHoriz = 'center',
            alignVert = 'center', color = [0, 0, 0], wrapWidth=1)

ImagesToRedo = 0

for testImgii in TestingPhase:

    currentLoop = testImgii
    # Set some basics
    t = 0
    globalClock.reset()  # clock

    if testImgii != None:
        for paramName in testImgii:
            exec('{} = testImgii[paramName]'.format(paramName))

    ContinueStim = True

    # Set the objects for this trial
    DragImage.setImage(ImageN)
    bingoCircle.setPos((cx,cy))
    DragImage.setPos(0,0)
    CompoundSound = sound.Sound(SoundID)
    myMouse.setPos(0,0)

    # Instead of ContinueStim, can use nextEntry()
    while ContinueStim:
    # Draw the object to screen
        if bingoCircle.status == NOT_STARTED:
            bingoCircle.setAutoDraw(True)

        if DragImage.status == NOT_STARTED:
            DragImage.setAutoDraw(True)

        # Drag and drop image using mouse
        if DragImage.contain(myMouse) and myMouse.getPressed(0):
            DragImage.pos = myMouse.getPos()

            # When the mouse is released
            if not myMouse.getPressed(0):
                finalAnswer = myMouse.getPos()
                if bingoCircle.contains(DragImage):
                    # Correct answer!!
                    CorrectMsg.setAutoDraw()
                    ContinueStim = False
                else:
                    # Incorrect answer produces correct position and sound
                    DragImage.setPos((cx,cy))
                    CompoundSound.play()
                    ContinueStim = False
                    # Get the stimuli parameters and write to new excel file
                    WrongAnswers.append( TestingPhase._getLoopInfo(currentLoop) )
                    IncorrectAnswers.append({'ImageN' : ImageN, 'SoundID' : SoundID, 'cx' : cx, 'cy' : cy})
                    ImagesToRedo += 1

        # When this trial is done, record it
        if not ContinueStim:
            # Perhaps use event.xydist() to measure distances
            TestingPhase.addData('ImgTimeSpent', t)
            TestingPhase.addData('PositionAnswer', finalAnswer)
            # For the output below, set the distance to cm
            TestingPhase.addData('DistanceFromBingo', myMouse.mouseMoved(distance=True))
            TestingPhase.addData('TimeImgPressed', myMouse.mouseMoveTime())
            TestingPhase.addData('ImageWrong', DragImage)


        # Stop loop if 'esc' key is pressed
        if event.getKeys(keyList=["escape"]):
            core.quit()

        win.flip()

    routineTimer.reset()
    win.flip()

if bool(ImagesToRedo):
    NextMsg = 'There are still ', ImagesToRedo, ' images to correctly place'
else
    NextMsg = 'Congragulations! You got them all right! You''re done! Left-Click to exit'

I’m also trying to store time spent on each trial and number of times it to get an individual trial correct. I know this is a lot. Any help would be greatly appreciated.

Thank you and have a good day,

Noah

1 Like

Hi all,

I figured out what I posted above earlier. In case anyone asks the same questions, I’m posting the code I used to solve this.

Have a good day,

Noah

ImagesToRedo = 1
TestIteration = 0
IncorrectAnswers = data.importConditions('image+soundFiles.xlsx') #TestPhaseFilename + str(TestIteration).zfill(2) + '.xlsx'
CorrectMsg = visual.TextStim(win = win, units = 'norm', height = 0.1,
            pos = (0,0), text = "Correct!", alignHoriz = 'center',
            alignVert = 'center', color = [0, 0, 0], wrapWidth=1)

while ImagesToRedo:
    # Set the excel sheet to use for this trial
    TestIteration = TestIteration + 1
    # set up handler to look after randomisation of conditions etc
    TestingPhase = data.TrialHandler(nReps=1, method='random',
        extraInfo=expInfo, originPath=-1,
        trialList= IncorrectAnswers,
        seed=None, name='TestingPhase')
    ThisLoopFileName = ""
    # Set variables to be used in the testing phase
    thisExp.addLoop(TestingPhase)  # add the loop to the experiment
    IncorrectAnswers = []

    # Start the loop of TestingPhase
    for testImgii in TestingPhase:
        currentLoop = testImgii
        # Set some basics

        ContinueStim = True

        while ContinueStim:
        # Draw the object to screen

            if finalAnswerGiven and CORRECT:
                # Correct answer!!
                CorrectMsg.setAutoDraw(True)
                print("Hooray")
                CorrectOrMiss = 1
            elif finalAnswerGiven and not CORRECT:
                # Incorrect answer produces correct position and sound
                # Append the list of stims for next test loop
                IncorrectAnswers.append({'ImageN' : ImageN, 'SoundID' : SoundID, 'cx' : cx, 'cy' : cy})
                #IncorrectAnswers.append(dict(TestingPhase.trialList[TestingPhase.thisIndex]))
                print("Need to do again:")
                print(IncorrectAnswers)
                CorrectOrMiss = 0

            if finalAnswerGiven:
                TestingPhase.addData('ImgTimeSpent', t)
                TestingPhase.addData('PositionAnswer', finalAnswer)
                # For the output below, set the distance to cm
                #TestingPhase.addData('DistanceFromBingo', )
                TestingPhase.addData('TimeImgPressed', pressTimeClock.getTime())
                TestingPhase.addData('HitOrMoss', CorrectOrMiss)
                # DragImage.setOpacity(0)
            # Stop loop if 'esc' key is pressed
            if event.getKeys(keyList=["escape"]):
                core.quit()

            if ContinueStim:
                win.flip()

        routineTimer.reset()
    ImagesToRedo = len(IncorrectAnswers)
    if bool(ImagesToRedo):
        NextMsg = 'There are still ' + str(ImagesToRedo) + ' images to correctly place'
    else:
        NextMsg = 'Congragulations! You got them all right! You''re done! Left-Click to exit'

2 Likes