jacanterbury
jacanterbury

Reputation: 1505

Whats the best way to test my psychopy builder experiment? Can I automate responses

I'd like to automate the testing of my PsychoPy Builder experiment to cover a mix of correct/incorrect responses.

I can't find anything in the Help covering this area.

Does anyone have any suggestions?

Upvotes: 1

Views: 334

Answers (2)

jrgray
jrgray

Reputation: 415

There is built-in but not well documented support for keyboard-based testing like this, but not mouse: class psychopy.hardware.emulator.ResponseEmulator(threading.thread)

See http://www.psychopy.org/api/hardware/emulator.html and scroll down for ResponseEmulator. This is used in internal testing, and is not just for the fMRI simulator. Maybe it needs more visibility!

I think it would go something like this:

from psychopy.hardware.emulator import ResponseEmulator
simulated_responses = [(2.3, 'a'), (7.5, 'b')]
responder = ResponseEmulator(simulated_responses)
responder.start()

and you'd get a 'a' key happening at 2.3 sec after the .start(), then a 'b' 7.5 sec after the .start(), just as if a person pressed that key at that time (maybe not frame-accurate but very close).

Upvotes: 1

jacanterbury
jacanterbury

Reputation: 1505

For the record, with a bit of surfing + experimenting I came up with the following that fitted the bill for me. 1/ add a code block to import the following libraries:

import win32api 
import win32con 
import time

Then define the keycodes for the input you're looking for e.g.:

VK_CODE = {
'enter':0x0D,
'esc':0x1B,
'spacebar':0x20,
'pageup':0x21,
'pagedown':0x22,
'end':0x23,
'home':0x24,
'left':0x25,
'up':0x26,
'right':0x27,
'down':0x28,
'0':0x30,
'1':0x31,
'2':0x32,
'3':0x33,
'4':0x34,
'5':0x35,
'6':0x36,
'7':0x37,
'8':0x38,
'9':0x39,
'a':0x41,
'b':0x42,
'c':0x43,
'd':0x44,
'e':0x45,
'f':0x46,
'g':0x47,
'h':0x48,
'i':0x49,
'j':0x4A,
'k':0x4B,
'l':0x4C,
'm':0x4D,
'n':0x4E,
'o':0x4F,
'p':0x50,
'q':0x51,
'r':0x52,
's':0x53,
't':0x54,
'u':0x55,
'v':0x56,
'w':0x57,
'x':0x58,
'y':0x59,
'z':0x5A,
'numpad_0':0x60,
'numpad_1':0x61,
'numpad_2':0x62,
'numpad_3':0x63,
'numpad_4':0x64,
'numpad_5':0x65,
'numpad_6':0x66,
'numpad_7':0x67,
'numpad_8':0x68,
'numpad_9':0x69,
'multiply':0x6A,
'add':0x6B,
'separator':0x6C,
'subtract':0x6D,
'decimal':0x6E,
'divide':0x6F,
'f1':0x70,
'f2':0x71,
'f3':0x72,
'f4':0x73,
'f5':0x74,
'f6':0x75,
'f7':0x76,
'f8':0x77,
'f9':0x78,
'f10':0x79,
'f11':0x7A,
'f12':0x7B
}

then, in a code block somewhere within your trial loop, on the 'Begin Routine' tab add:

frame_counter = 0

and on the 'each frame' tab add this

frame_counter +=1

# usually at 60 frames per second , so below we wait for ~1 second 
# 'autoResp' below is the column name in your excel results file
# you can change this to whatever you want
#
# *IMPORTANT* Below, 
# -replace 'thisTrial' with the name you gave to your trial loop
# -'autoResp' is the column namein the csv file with the desired AUTOMATIC 
#       keyboard responses in

if frame_counter > 60:
    this_resp = VK_CODE[thisTrial['autoResp']]
    win32api.keybd_event( this_resp, 0, 0, 0)
    time.sleep(.05) # wait a while before doing the key_up ...
    win32api.keybd_event( this_resp,0 ,win32con.KEYEVENTF_KEYUP ,0) 
    frame_counter=0

See the code comments in the snippet above.

This then pulls in 'automated' keypresses from your csv file (column named 'autoResp' in this instance. Nb you can use this to test correct and incorrect scenarios

Upvotes: 0

Related Questions