psychopy.org | Reference | Downloads | Github

How to compare inputText to CorAns

Hi, I am trying to set up a basic digit-span task where participants are show a picture of a digit, one at a time. They then are prompted to type all digits that they saw. Right now, I am using the code component below in builder to give the participants the ability to see what they are typing on the screen. This code displays anything the participants type, and the routine is ended when enter is pressed.

My issue has been getting Psychopy to output the corrAns based on the final string of text entered (see attached conditions file). The final inputText string has to be compared, rather than recorded key presses (e.g., [‘4’, ‘5’, ‘RETURN’] ), because participants have the ability to use backspace in case they mistype something. With backspace enabled, endless amounts of correct correct responses, such as [‘4’, ‘6’, ‘BACKSPACE’, ‘5’, ‘RETURN’], are possible. Thus, the only way to compare to corrAns would be to compare corrAns with the final string of text (inputText) submitted. However, keyboard_resp_13.corr outputs a 0 regardless of whether the string of numbers submitted matches the corrAns.

Code Component:

Begin Experiment

inputText = “”

Begin Routine

theseKeys=""
shift_flag = False

Each Frame

n= len(theseKeys)
i = 0
while i < n:

if theseKeys[i] == 'return':
    # pressing RETURN means time to stop
    continueRoutine = False
    break

elif theseKeys[i] == 'backspace':
    inputText = inputText[:-1]  # lose the final character
    i = i + 1

elif theseKeys[i] == 'space':
    inputText += ' '
    i = i + 1

elif theseKeys[i] in ['lshift', 'rshift']:
    shift_flag = True
    i = i + 1

else:
    if len(theseKeys[i]) == 1:
        # we only have 1 char so should be a normal key, 
        # otherwise it might be 'ctrl' or similar so ignore it
        if shift_flag:
            inputText += chr( ord(theseKeys[i]) - ord(' '))
            shift_flag = False
        else:
            inputText += theseKeys[i]

    i = i + 1

End Routine

let’s store the final text string into the results finle…

thisExp.addData(‘inputText’, inputText)
inputText=""

Conditions excell file:
3digits_1.xlsx (9.1 KB)

Key_res properties:

Example output data:
_Trial_2017_Nov_16_1434.csv (788 Bytes)

Sorry, it seems as though the code I tried to post did not show up correctly. Here is another the code formatted better.

Begin Experiment*
inputText = ""

Begin Routine*
theseKeys=""
shift_flag = False

Each Frame*
n= len(theseKeys)
i = 0
while i < n:

    if theseKeys[i] == 'return':
        continueRoutine = False
        break

    elif theseKeys[i] == 'backspace':
        inputText = inputText[:-1]  
        i = i + 1

    elif theseKeys[i] == 'space':
        inputText += ' '
        i = i + 1

    elif theseKeys[i] in ['lshift', 'rshift']:
        shift_flag = True
        i = i + 1

    else:
        if len(theseKeys[i]) == 1:
            if shift_flag:
                inputText += chr( ord(theseKeys[i]) - ord(' '))
                shift_flag = False
            else:
                inputText += theseKeys[i]

        i = i + 1

End Routine*
thisExp.addData('inputText', inputText)
inputText=""
1 Like

All you have to do is only store the keypresses you want:

e.g. replace your “Every frame” code with something like this:

for key in event.getKeys():

    if key == 'return':
        thisExp.addData('response', theseKeys) # store the response in the data file
        continueRoutine = False
        break

    elif key == 'escape':
        core.quit() # exit the experiment

    elif key == 'backspace':
        theseKeys = theseKeys[:-1] 

    elif key in '0123456789':
        theseKeys = theseKeys + key

i.e. no return or backspace characters are stored (they just result in an action), and all other keys that are not digits are ignored.

You can actually just delete the keyboard component, as you are now doing the checking in code.

Thank you for your response Michael.

I replaced the every-frame code with the above code and removed the keyboard component.

The code runs with no error, but does not display the text on screen as digits are entered anymore. There also is no longer something like a key_resp.corr type variable in the data output, where a 0 is outputted if the response does not match corrAns and a 1 is outputted if the response does match corrAns.

As kind of a side note, my goal for data output is to have Psychopy output the key_resp.corr type variable explained above(i.e, outputting a 0 or 1 based on corrAns), and then have it automatically sum the responses of multiple loops (i.e., the correct responses will be summed since they’re given a value of 1). Thus, any insight on a code for how to sum or even average multiple output under a certain variable would also be helpful.

You need to actually tell your text stimulus to display your theseKeys variable.

Yes, you are responsible for that now, since there is no keyboard component trying to do it automatically. e.g. something like editing the code above:

if key == 'return':
     # store the response in the data file:
    thisExp.addData('response', theseKeys)

     # test if it was correct:
    if theseKeys == corrAns:
        thisExp.addData('response_correct', True)
    else:
        thisExp.addData('response_correct', False)

    # end this trial:
    continueRoutine = False
    break