The script PsychoPy has compiled is:
ââ"#!/usr/bin/env python2
-- coding: utf-8 --
âââ
This experiment was created using PsychoPy2 Experiment Builder (v1.84.2),
on November 29, 2016, at 09:49
If you publish work using this script please cite the PsychoPy publications:
Peirce, JW (2007) PsychoPy - Psychophysics software in Python.
Journal of Neuroscience Methods, 162(1-2), 8-13.
Peirce, JW (2009) Generating stimuli for neuroscience using PsychoPy.
Frontiers in Neuroinformatics, 2:10. doi: 10.3389/neuro.11.010.2008
"â"
from future import absolute_import, division
from psychopy import locale_setup, 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
Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(file)).decode(sys.getfilesystemencoding())
os.chdir(_thisDir)
Store info about the experiment session
expName = âExpt1â # from the Builder filename that created this script
expInfo = {âparticipantâ:ââ, âsessionâ:â001â}
dlg = gui.DlgFromDict(dictionary=expInfo, title=expName)
if dlg.OK == False:
core.quit() # user pressed cancel
expInfo[âdateâ] = data.getDateStr() # add a simple timestamp
expInfo[âexpNameâ] = expName
Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
filename = thisDir + os.sep + uâdata/%s%s_%sâ % (expInfo[âparticipantâ], expName, expInfo[âdateâ])
An ExperimentHandler isnât essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version=ââ,
extraInfo=expInfo, runtimeInfo=None,
originPath=None,
savePickle=True, saveWideText=True,
dataFileName=filename)
save a log file for detail verbose info
logFile = logging.LogFile(filename+â.logâ, level=logging.EXP)
logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file
endExpNow = False # flag for âescapeâ or other condition => quit the exp
Start Code - component code to be run before the window creation
Setup the Window
win = visual.Window(
size=(1600, 900), fullscr=True, screen=0,
allowGUI=False, allowStencil=False,
monitor=âtestMonitorâ, color=[0,0,0], colorSpace=ârgbâ,
blendMode=âavgâ, useFBO=True)
store frame rate of monitor if we can measure it
expInfo[âframeRateâ] = win.getActualFrameRate()
if expInfo[âframeRateâ] != None:
frameDur = 1.0 / round(expInfo[âframeRateâ])
else:
frameDur = 1.0 / 60.0 # could not measure, so guess
Initialize components for Routine âInstructions1â
Instructions1Clock = core.Clock()
text = visual.TextStim(win=win, name=âtextâ,
text=âAnswer the arithmetic problem by saying aloud your answer and pressing the spacebar at the same timeâ,
font=âArialâ,
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color=âwhiteâ, colorSpace=ârgbâ, opacity=1,
depth=0.0);
Initialize components for Routine âArithmetic_problemâ
Arithmetic_problemClock = core.Clock()
from numpy.random import random, randint, normal, shuffle
import random
locations = [-0.6,0.0,0.6]
rloca = random.choice(locations)
rlocb = random.choice(locations)
Fixation = visual.TextStim(win=win, name=âFixationâ,
text=âREADYâ,
font=âArialâ,
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color=âwhiteâ, colorSpace=ârgbâ, opacity=1,
depth=-1.0);
Arithmetic = visual.TextStim(win=win, name=âArithmeticâ,
text=âdefault textâ,
font=âArialâ,
pos=(0, 0), height=0.2, wrapWidth=None, ori=0,
color=âwhiteâ, colorSpace=ârgbâ, opacity=1,
depth=-2.0);
S_MU = visual.ImageStim(
win=win, name=âS_MUâ,
image=âC:\Users\User\Documents\Experiment1\square.pngâ, mask=None,
ori=0, pos=(0, 0.6), size=(0.25, 0.4),
color=1.0, colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-4.0)
S_ML = visual.ImageStim(
win=win, name=âS_MLâ,
image=âC:\Users\User\Documents\Experiment1\square.pngâ, mask=None,
ori=0, pos=(0, -0.6), size=(0.25, 0.4),
color=[1,1,1], colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-5.0)
S_LU = visual.ImageStim(
win=win, name=âS_LUâ,
image=âC:\Users\User\Documents\Experiment1\square.pngâ, mask=None,
ori=0, pos=(-0.6, 0.6), size=(0.25, 0.4),
color=[1,1,1], colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-6.0)
S_LL = visual.ImageStim(
win=win, name=âS_LLâ,
image=âC:\Users\User\Documents\Experiment1\square.pngâ, mask=None,
ori=0, pos=(-0.6, -0.6), size=(0.25, 0.4),
color=[1,1,1], colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-7.0)
S_RU = visual.ImageStim(
win=win, name=âS_RUâ,
image=âC:\Users\User\Documents\Experiment1\square.pngâ, mask=None,
ori=0, pos=(0.6,0.6), size=(0.25,0.4),
color=[1,1,1], colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-8.0)
S_RL = visual.ImageStim(
win=win, name=âS_RLâ,
image=âC:\Users\User\Documents\Experiment1\square.pngâ, mask=None,
ori=0, pos=(0.6,-0.6), size=(0.25,0.4),
color=[1,1,1], colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-9.0)
imagered = visual.ImageStim(
win=win, name=âimageredâ,
image=uâC:\Users\User\Documents\Experiment1\fillsquare.pngâ, mask=None,
ori=0, pos=(rloca,rlocb), size=(0.25, 0.4),
color=[1,1,1], colorSpace=ârgbâ, opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-10.0)
Initialize components for Routine âButton1â
Button1Clock = core.Clock()
Initialize components for Routine âButton2â
Button2Clock = core.Clock()
Initialize components for Routine âButton3â
Button3Clock = core.Clock()
Create some handy timers
globalClock = core.Clock() # to track the time since experiment started
routineTimer = core.CountdownTimer() # to track time remaining of each (non-slip) routine
------Prepare to start Routine âInstructions1â-------
t = 0
Instructions1Clock.reset() # clock
frameN = -1
continueRoutine = True
update component parameters for each repeat
End_instructions = event.BuilderKeyResponse()
keep track of which components have finished
Instructions1Components = [text, End_instructions]
for thisComponent in Instructions1Components:
if hasattr(thisComponent, âstatusâ):
thisComponent.status = NOT_STARTED
-------Start Routine âInstructions1â-------
while continueRoutine:
# get current time
t = Instructions1Clock.getTime()
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *text* updates
if t >= 0.0 and text.status == NOT_STARTED:
# keep track of start time/frame for later
text.tStart = t
text.frameNStart = frameN # exact frame index
text.setAutoDraw(True)
# *End_instructions* updates
if t >= 0.0 and End_instructions.status == NOT_STARTED:
# keep track of start time/frame for later
End_instructions.tStart = t
End_instructions.frameNStart = frameN # exact frame index
End_instructions.status = STARTED
# keyboard checking is just starting
win.callOnFlip(End_instructions.clock.reset) # t=0 on next screen flip
event.clearEvents(eventType='keyboard')
if End_instructions.status == STARTED:
theseKeys = event.getKeys()
# check for quit:
if "escape" in theseKeys:
endExpNow = True
if len(theseKeys) > 0: # at least one key was pressed
End_instructions.keys = theseKeys[-1] # just the last key pressed
End_instructions.rt = End_instructions.clock.getTime()
# a response ends the routine
continueRoutine = False
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in Instructions1Components:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# check for quit (the Esc key)
if endExpNow or event.getKeys(keyList=["escape"]):
core.quit()
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
-------Ending Routine âInstructions1â-------
for thisComponent in Instructions1Components:
if hasattr(thisComponent, âsetAutoDrawâ):
thisComponent.setAutoDraw(False)
check responses
if End_instructions.keys in [ââ, [], None]: # No response was made
End_instructions.keys=None
thisExp.addData(âEnd_instructions.keysâ,End_instructions.keys)
if End_instructions.keys != None: # we had a response
thisExp.addData(âEnd_instructions.rtâ, End_instructions.rt)
thisExp.nextEntry()
the Routine âInstructions1â was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
set up handler to look after randomisation of conditions etc
Primary_task = data.TrialHandler(nReps=1, method=âfullRandomâ,
extraInfo=expInfo, originPath=-1,
trialList=data.importConditions(âBatch1.xlsxâ),
seed=None, name=âPrimary_taskâ)
thisExp.addLoop(Primary_task) # add the loop to the experiment
thisPrimary_task = Primary_task.trialList[0] # so we can initialise stimuli with some values
abbreviate parameter names if possible (e.g. rgb = thisPrimary_task.rgb)
if thisPrimary_task != None:
for paramName in thisPrimary_task.keys():
exec(paramName + â= thisPrimary_task.â + paramName)
for thisPrimary_task in Primary_task:
currentLoop = Primary_task
# abbreviate parameter names if possible (e.g. rgb = thisPrimary_task.rgb)
if thisPrimary_task != None:
for paramName in thisPrimary_task.keys():
exec(paramName + â= thisPrimary_task.â + paramName)
# ------Prepare to start Routine "Arithmetic_problem"-------
t = 0
Arithmetic_problemClock.reset() # clock
frameN = -1
continueRoutine = True
# update component parameters for each repeat
Arithmetic.setText(Problem)
Reaction_time = event.BuilderKeyResponse()
# keep track of which components have finished
Arithmetic_problemComponents = [Fixation, Arithmetic, Reaction_time, S_MU, S_ML, S_LU, S_LL, S_RU, S_RL, imagered]
for thisComponent in Arithmetic_problemComponents:
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# -------Start Routine "Arithmetic_problem"-------
while continueRoutine:
# get current time
t = Arithmetic_problemClock.getTime()
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *Fixation* updates
if t >= 0.0 and Fixation.status == NOT_STARTED:
# keep track of start time/frame for later
Fixation.tStart = t
Fixation.frameNStart = frameN # exact frame index
Fixation.setAutoDraw(True)
frameRemains = 0.0 + 1- win.monitorFramePeriod * 0.75 # most of one frame period left
if Fixation.status == STARTED and t >= frameRemains:
Fixation.setAutoDraw(False)
# *Arithmetic* updates
if t >= 1 and Arithmetic.status == NOT_STARTED:
# keep track of start time/frame for later
Arithmetic.tStart = t
Arithmetic.frameNStart = frameN # exact frame index
Arithmetic.setAutoDraw(True)
# *Reaction_time* updates
if t >= 1 and Reaction_time.status == NOT_STARTED:
# keep track of start time/frame for later
Reaction_time.tStart = t
Reaction_time.frameNStart = frameN # exact frame index
Reaction_time.status = STARTED
# keyboard checking is just starting
win.callOnFlip(Reaction_time.clock.reset) # t=0 on next screen flip
event.clearEvents(eventType='keyboard')
if Reaction_time.status == STARTED:
theseKeys = event.getKeys(keyList=['space'])
# check for quit:
if "escape" in theseKeys:
endExpNow = True
if len(theseKeys) > 0: # at least one key was pressed
if Reaction_time.keys == []: # then this was the first keypress
Reaction_time.keys = theseKeys[0] # just the first key pressed
Reaction_time.rt = Reaction_time.clock.getTime()
# a response ends the routine
continueRoutine = False
# *S_MU* updates
if t >= 0.0 and S_MU.status == NOT_STARTED:
# keep track of start time/frame for later
S_MU.tStart = t
S_MU.frameNStart = frameN # exact frame index
S_MU.setAutoDraw(True)
if S_MU.status == STARTED: # only update if drawing
S_MU.setColor([1,1,1], colorSpace='rgb', log=False)
# *S_ML* updates
if t >= 0.0 and S_ML.status == NOT_STARTED:
# keep track of start time/frame for later
S_ML.tStart = t
S_ML.frameNStart = frameN # exact frame index
S_ML.setAutoDraw(True)
# *S_LU* updates
if t >= 0.0 and S_LU.status == NOT_STARTED:
# keep track of start time/frame for later
S_LU.tStart = t
S_LU.frameNStart = frameN # exact frame index
S_LU.setAutoDraw(True)
# *S_LL* updates
if t >= 0.0 and S_LL.status == NOT_STARTED:
# keep track of start time/frame for later
S_LL.tStart = t
S_LL.frameNStart = frameN # exact frame index
S_LL.setAutoDraw(True)
# *S_RU* updates
if t >= 0.0 and S_RU.status == NOT_STARTED:
# keep track of start time/frame for later
S_RU.tStart = t
S_RU.frameNStart = frameN # exact frame index
S_RU.setAutoDraw(True)
# *S_RL* updates
if t >= 0.0 and S_RL.status == NOT_STARTED:
# keep track of start time/frame for later
S_RL.tStart = t
S_RL.frameNStart = frameN # exact frame index
S_RL.setAutoDraw(True)
# *imagered* updates
if t >= 0.0 and imagered.status == NOT_STARTED:
# keep track of start time/frame for later
imagered.tStart = t
imagered.frameNStart = frameN # exact frame index
imagered.setAutoDraw(True)
frameRemains = 0.0 + 1.0- win.monitorFramePeriod * 0.75 # most of one frame period left
if imagered.status == STARTED and t >= frameRemains:
imagered.setAutoDraw(False)
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in Arithmetic_problemComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# check for quit (the Esc key)
if endExpNow or event.getKeys(keyList=["escape"]):
core.quit()
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Arithmetic_problem"-------
for thisComponent in Arithmetic_problemComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# check responses
if Reaction_time.keys in ['', [], None]: # No response was made
Reaction_time.keys=None
Primary_task.addData('Reaction_time.keys',Reaction_time.keys)
if Reaction_time.keys != None: # we had a response
Primary_task.addData('Reaction_time.rt', Reaction_time.rt)
# the Routine "Arithmetic_problem" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# ------Prepare to start Routine "Button1"-------
t = 0
Button1Clock.reset() # clock
frameN = -1
continueRoutine = True
# update component parameters for each repeat
Button_1 = event.BuilderKeyResponse()
# keep track of which components have finished
Button1Components = [Button_1]
for thisComponent in Button1Components:
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# -------Start Routine "Button1"-------
while continueRoutine:
# get current time
t = Button1Clock.getTime()
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *Button_1* updates
if t >= 0.0 and Button_1.status == NOT_STARTED:
# keep track of start time/frame for later
Button_1.tStart = t
Button_1.frameNStart = frameN # exact frame index
Button_1.status = STARTED
# keyboard checking is just starting
win.callOnFlip(Button_1.clock.reset) # t=0 on next screen flip
event.clearEvents(eventType='keyboard')
if Button_1.status == STARTED:
theseKeys = event.getKeys()
# check for quit:
if "escape" in theseKeys:
endExpNow = True
if len(theseKeys) > 0: # at least one key was pressed
Button_1.keys = theseKeys[-1] # just the last key pressed
Button_1.rt = Button_1.clock.getTime()
# a response ends the routine
continueRoutine = False
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in Button1Components:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# check for quit (the Esc key)
if endExpNow or event.getKeys(keyList=["escape"]):
core.quit()
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Button1"-------
for thisComponent in Button1Components:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# check responses
if Button_1.keys in ['', [], None]: # No response was made
Button_1.keys=None
Primary_task.addData('Button_1.keys',Button_1.keys)
if Button_1.keys != None: # we had a response
Primary_task.addData('Button_1.rt', Button_1.rt)
# the Routine "Button1" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# ------Prepare to start Routine "Button2"-------
t = 0
Button2Clock.reset() # clock
frameN = -1
continueRoutine = True
# update component parameters for each repeat
Button_2 = event.BuilderKeyResponse()
# keep track of which components have finished
Button2Components = [Button_2]
for thisComponent in Button2Components:
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# -------Start Routine "Button2"-------
while continueRoutine:
# get current time
t = Button2Clock.getTime()
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *Button_2* updates
if t >= 0.0 and Button_2.status == NOT_STARTED:
# keep track of start time/frame for later
Button_2.tStart = t
Button_2.frameNStart = frameN # exact frame index
Button_2.status = STARTED
# keyboard checking is just starting
win.callOnFlip(Button_2.clock.reset) # t=0 on next screen flip
event.clearEvents(eventType='keyboard')
if Button_2.status == STARTED:
theseKeys = event.getKeys()
# check for quit:
if "escape" in theseKeys:
endExpNow = True
if len(theseKeys) > 0: # at least one key was pressed
Button_2.keys = theseKeys[-1] # just the last key pressed
Button_2.rt = Button_2.clock.getTime()
# a response ends the routine
continueRoutine = False
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in Button2Components:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# check for quit (the Esc key)
if endExpNow or event.getKeys(keyList=["escape"]):
core.quit()
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Button2"-------
for thisComponent in Button2Components:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# check responses
if Button_2.keys in ['', [], None]: # No response was made
Button_2.keys=None
Primary_task.addData('Button_2.keys',Button_2.keys)
if Button_2.keys != None: # we had a response
Primary_task.addData('Button_2.rt', Button_2.rt)
# the Routine "Button2" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# ------Prepare to start Routine "Button3"-------
t = 0
Button3Clock.reset() # clock
frameN = -1
continueRoutine = True
# update component parameters for each repeat
Button_3 = event.BuilderKeyResponse()
# keep track of which components have finished
Button3Components = [Button_3]
for thisComponent in Button3Components:
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# -------Start Routine "Button3"-------
while continueRoutine:
# get current time
t = Button3Clock.getTime()
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *Button_3* updates
if t >= 0.0 and Button_3.status == NOT_STARTED:
# keep track of start time/frame for later
Button_3.tStart = t
Button_3.frameNStart = frameN # exact frame index
Button_3.status = STARTED
# keyboard checking is just starting
win.callOnFlip(Button_3.clock.reset) # t=0 on next screen flip
event.clearEvents(eventType='keyboard')
if Button_3.status == STARTED:
theseKeys = event.getKeys(keyList=['return'])
# check for quit:
if "escape" in theseKeys:
endExpNow = True
if len(theseKeys) > 0: # at least one key was pressed
Button_3.keys = theseKeys[-1] # just the last key pressed
Button_3.rt = Button_3.clock.getTime()
# a response ends the routine
continueRoutine = False
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in Button3Components:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# check for quit (the Esc key)
if endExpNow or event.getKeys(keyList=["escape"]):
core.quit()
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Button3"-------
for thisComponent in Button3Components:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# check responses
if Button_3.keys in ['', [], None]: # No response was made
Button_3.keys=None
Primary_task.addData('Button_3.keys',Button_3.keys)
if Button_3.keys != None: # we had a response
Primary_task.addData('Button_3.rt', Button_3.rt)
# the Routine "Button3" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
thisExp.nextEntry()
completed 1 repeats of âPrimary_taskâ
these shouldnât be strictly necessary (should auto-save)
thisExp.saveAsWideText(filename+â.csvâ)
thisExp.saveAsPickle(filename)
logging.flush()
make sure everything is closed down
thisExp.abort() # or data files will save again on exit
win.close()
core.quit()""""