Hello Everyone, I am trying to measure reaction time from onset of an image stimulus untill user press ‘return’. And later on this reaction time , should be saved in a file with the response of the stimulus.I have tried to start clock after stimulus is shown and to collect reaction time I am using function getTime(), when user press ‘return’ ; but it gives me implausible millisecond reaction time like 0.000617261801381; Here is the code, can please anyone help.
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division
from psychopy import gui, visual, core, data, event, logging, sound
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)
import numpy as np # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle
import os # handy system and path functions
import sys # to get file system encoding
from threading import Timer
from random import shuffle
import codecs, os
import time,subprocess
from re import match
import sys
from psychopy.core import getTime
reload(sys)
sys.setdefaultencoding('utf8')
PATH = 'C:\\Users\\VR 6-Diff\\OneDrive\\creativity\\Programming_creativity_slides\\__Programming\\__Programming\\5KombinierenVonObjekten' #please enter path of your system
OUTPATH = '{0:s}\\results\\'.format(PATH) # output path for storing the results
sans = ['Gill Sans MT', 'Arial','Helvetica','Verdana'] #use the first font found on this list
exp_name = '5Kombinieren Von Objekten'
exp_info = {'Versuchspersonnummer': '', 'Versuchspersonnummer (repeat)': ''}
dlg = gui.DlgFromDict(dictionary=exp_info, title=exp_name)
if dlg.OK is False:
core.quit() # user pressed cancel
while exp_info['Versuchspersonnummer'] != exp_info['Versuchspersonnummer (repeat)']:
dlg = gui.DlgFromDict(dictionary=exp_info, title='Please insert matching number in both fields')
if dlg.OK is False:
core.quit() # user pressed cancel
exp_info['date'] = data.getDateStr() # add a simple timestamp
exp_info['exp_name'] = exp_name
output_file = OUTPATH + exp_info['exp_name'] + '_{0:02d}.txt'.format(int(exp_info['Versuchspersonnummer']))
if os.path.isfile(output_file):
print 'Error: File already exists. Restart experiment and choose subject number which is not used yet.'
exit(1)
myWin = visual.Window(size =[1366,768], monitor= "testmonitor",color = (230,230,230),allowGUI = False, fullscr = True, # please change your screen size here.
winType='pyglet',colorSpace = 'rgb255', units = 'deg')
def saveThisResponse(captured_string):
outfile = OUTPATH + exp_info['exp_name']+ '_{0:02d}.txt'.format(int(exp_info['Versuchspersonnummer']))
f = open(outfile, 'a') #open our results file in append mode so we don't overwrite anything
f.write(captured_string) #write the string they typed
f.write('_typed at %s' %time.asctime()) #write a timestamp (very course)
f.write( '_Reaction Time is' +'_{:f}'.format(Reaction_time))
#f.write('_ reaction time is %5.4f milliseconds %f'%Rt)
f.write('\n') # write a line ending
f.close() #close and "save" the output file
def updateTheResponse(captured_string):
CapturedResponseString.setText(captured_string)
CapturedResponseString.draw()
ResponseInstuction.draw()
myWin.flip()
scale =0.6
files_instr = [f for f in os.listdir('{0:s}/instr_pics/'.format(PATH))if match(r'KO_instr_[1-4]+.png', f)]
for file in files_instr:
instr_screen = visual.ImageStim(myWin, image='{0:s}/instr_pics/{1:s}'.format(PATH,file),pos =(0.1,-0.1))
instr_screen.size*= scale
instr_screen.draw()#draw
myWin.flip()#show
event.waitKeys()#click
ready = visual.ImageStim(myWin, image = '{0:s}/Ready&Thanks_pic/Ready.png'.format(PATH),pos =(0.2,-0.2))
ready.size*= scale
ready.draw()
myWin.flip()
event.waitKeys()
scale2 = 0.6
files = [f for f in os.listdir('{0:s}/stim_pics/'.format(PATH))if match(r'KOitem_[0-9]+.png', f)]
for file in files:
stimulus = visual.ImageStim(myWin, image='{0:s}/stim_pics/{1:s}'.format(PATH,file),size=(25,10),units = 'deg',pos =(0,5))
#stimulus.size*= scale2
ResponseInstuction = visual.ImageStim(myWin, image = '{0:s}/Ready&Thanks_pic/Type_instr.png'.format(PATH),pos = (0,-2),size=(15,5))
CapturedResponseString = visual.TextStim(myWin,
units='norm',height = 0.1,
pos=(0,-0.5), text='',
font=sans, bold = True,
alignHoriz = 'center',alignVert='center',
color='Black')
captured_string = '' #empty for now..
stimulus.draw() #draw stimulus1 screen
ResponseInstuction.draw() # draw instruction
myWin.flip() # show instruction
# until the participant hits the return key
clock = core.Clock()
subject_response_finished = 0 # only changes when they hit return
while clock.getTime () <= 30 and subject_response_finished == 0 :
stimulus.draw()
RT = core.Clock()# start clock here when stimulus is shown
for key in event.getKeys():
if key in ['escape']:
myWin.close()
core.quit()
elif key in ['return'] :
myWin.flip()
rt = RT.getTime()
Reaction_time = rt*10000
print 'time is %.4f '%(Reaction_time)
print captured_string #show in debug window
saveThisResponse(captured_string) #write to file
captured_string = '' #reset to zero length
subject_response_finished = 1 #allows the next trial to start
RT.reset()
clock.reset()
elif key in ['delete','backspace']:
captured_string = captured_string[:-1] #delete last character
updateTheResponse(captured_string)
elif key in ['space']:
captured_string = captured_string+' '
updateTheResponse(captured_string)
elif key in ['period']:
captured_string = captured_string+'.'
updateTheResponse(captured_string)
elif key in ['comma']:
captured_string = captured_string+','
updateTheResponse(captured_string)
elif key in ['lshift','rshift']:
pass #do nothing when some keys are pressed
else:
captured_string = captured_string+key
updateTheResponse(captured_string)
event.clearEvents(eventType='keyboard')
thanks_screen = visual.ImageStim(myWin, image='{0:s}/Ready&Thanks_pic/Thanks.PNG'.format(PATH))
thanks_screen.draw()
myWin.flip()
event.waitKeys()
myWin.close()