Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run(self):
self.running = True
self.clock.reset()
last_onset = 0.000
# wait until next event requested, and simulate a key press
for onset, key in self.responses:
core.wait(float(onset) - last_onset)
if type(key) == int:
# avoid cryptic error if int
key = "{}".format(key)[0]
if type(key) == type(""):
event._onPygletKey(symbol=key, modifiers=0, emulated=True)
else:
logging.error('ResponseEmulator: only keyboard events '
'are supported')
last_onset = onset
if self.stopflag:
break
self.running = False
return self
t = time.localtime()
minPos = numpy.floor(t[4]) * 360 / 60 # NB floor will round down
minute.ori = minPos
minute.draw()
hourPos = (t[3]) * 360 / 12 # this one can be smooth
hour.ori = hourPos
hour.draw()
secPos = numpy.floor(t[5]) * 360 / 60 # NB floor will round down
second.ori = secPos
second.draw()
win.flip()
event.clearEvents('mouse') # only really needed for pygame windows
win.close()
core.quit()
# keep track of start time/frame for later
polygon.tStart = t
polygon.frameNStart = frameN # exact frame index
polygon.setAutoDraw(True)
# 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 selectComponents:
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 "select"-------
for thisComponent in selectComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# store data for trials (TrialHandler)
if len(mouse.x): trials.addData('mouse.x', mouse.x[0])
if len(mouse.y): trials.addData('mouse.y', mouse.y[0])
if len(mouse.leftButton): trials.addData('mouse.leftButton', mouse.leftButton[0])
if len(mouse.midButton): trials.addData('mouse.midButton', mouse.midButton[0])
if len(mouse.rightButton): trials.addData('mouse.rightButton', mouse.rightButton[0])
button_y_inner_margin = labelSize
button_y_outer_margin = labelSize + borderThickness
button_x_range = (0 - button_width / 2 + pos[0], 0 + button_width / 2 + pos[0])
self.win = win
self.borderThickness = borderThickness
self.labelSize = labelSize
self.pos = pos
self.labelText = labelText
self.textColor = textColor
self.borderColor = borderColor
self.buttonColor = buttonColor
self.buttonEnabled = buttonEnabled
self._dragging = False
self.mouse = event.Mouse()
self.buttonSelected = False
self.buttonItems = []
self.buttonBorder = BaseShapeStim(self.win, fillColor=self.borderColor, vertices=(
(button_x_range[0] - button_x_outer_margin, -button_y_outer_margin + self.pos[1]),
(button_x_range[0] - button_x_outer_margin, button_y_outer_margin + self.pos[1]),
(button_x_range[1] + button_x_outer_margin, button_y_outer_margin + self.pos[1]),
(button_x_range[1] + button_x_outer_margin, -button_y_outer_margin + self.pos[1])))
self.buttonInner = BaseShapeStim(self.win, fillColor=self.buttonColor, vertices=(
(button_x_range[0] - button_x_inner_margin, -button_y_inner_margin + self.pos[1]),
(button_x_range[0] - button_x_inner_margin, button_y_inner_margin + self.pos[1]),
(button_x_range[1] + button_x_inner_margin, button_y_inner_margin + self.pos[1]),
(button_x_range[1] + button_x_inner_margin, -button_y_inner_margin + self.pos[1])))
self.buttonInnerText = TextStim(self.win, text=self.labelText, color=self.textColor, pos=self.pos,
height=self.labelSize)
self.buttonItems.append(self.buttonBorder)
button is released.
:Note:
CustomMouse is a new feature, and subject to change.
`setPos()` does not work yet. `getRel()` returns `[0,0]`
and `mouseMoved()` always
returns `False`. `clickReset()` may not be working.
"""
# what local vars are defined (these are the init params) for use by
# __repr__
self._initParams = dir()
self._initParams.remove('self')
super(CustomMouse, self).__init__(name=name, autoLog=False)
self.autoLog = False # set properly at end of init
self.win = win
self.mouse = event.Mouse(win=self.win)
# maybe inheriting from Mouse would be easier? its not that simple
self.getRel = self.mouse.getRel
self.getWheelRel = self.mouse.getWheelRel
self.mouseMoved = self.mouse.mouseMoved # FAILS
self.mouseMoveTime = self.mouse.mouseMoveTime
self.getPressed = self.mouse.getPressed
self.clickReset = self.mouse.clickReset # ???
self._pix2windowUnits = self.mouse._pix2windowUnits
self._windowUnits2pix = self.mouse._windowUnits2pix
# the graphic to use as the 'mouse' icon (pointer)
if pointer:
self.setPointer(pointer)
else:
# self.pointer = TextStim(win, text='+')
RT_clock: False or psychopy.core.Clock
A clock used as a reference for measuring response time
fakeKey: None or (key pressed, response time)
This is used for simulating key presses in order to test that
the experiment is working.
"""
allKeys = []
event.clearEvents() # key presses might be stored from before
while len(allKeys) == 0: # if the participant did not respond earlier
if fakeKey is not None:
if RT_clock.getTime() > fakeKey[1]:
allKeys = [fakeKey]
else:
allKeys = event.getKeys(
keyList = self.comp.validResponses.keys(),
timeStamped = RT_clock)
self.last_keypress()
return allKeys
text=text,autoLog=False,wrapWidth=display_resolution[0]*0.8)
textstim.draw()
etime=core.getTime()*1000.0
textstim_init_dur=etime-stime
# Start the draw duration tests, for text change and no text change conditions.
stim_draw_orders=[[textstim,textbox],[textbox,textstim]]
#stim_draw_orders=[[textstim,textbox],]
for stim1, stim2 in stim_draw_orders:
stim1_txt_change_draw_times.clear()
stim2_txt_change_draw_times.clear()
stim1_no_change_draw_times.clear()
stim2_no_change_draw_times.clear()
demo_start=window.flip()
event.clearEvents()
fcount=0
stim1_type=stim1.__class__.__name__+u' '
stim2_type=stim2.__class__.__name__+u' '
while True:
# For the textBox and TextStim resource, change the text every
# chng_txt_each_flips, and record the time it takes to update the text
# and redraw() each resource type.
#
# Make sure timing of stim is for the time taken for that stim alone. ;)
gl.glFlush()
gl.glFinish()
if fcount==0 or fcount%chng_txt_each_flips==0:
t=getRandomString(text_length)
beach = visual.ImageStim(win, image='beach.jpg', flipHoriz=True, pos=(0, 4.50), units='deg')
faceRGB = visual.ImageStim(win, image='face.jpg', mask=None,
pos=(50, -50), size=None, # will be the size of the original image in pixels
units='pix', interpolate=True, autoLog=False)
print("original image size:", faceRGB.size)
faceALPHA = visual.GratingStim(win, pos=(-0.7, -0.2),
tex="sin", mask="face.jpg", color=[1.0, 1.0, -1.0],
size=(0.5, 0.5), units="norm", autoLog=False)
message = visual.TextStim(win, pos=(-0.95, -0.95),
text='[Esc] to quit', color='white',
anchorVert='bottom', anchorHoriz='left')
trialClock = core.Clock()
t = lastFPSupdate = 0
win.recordFrameIntervals = True
while not event.getKeys():
t = trialClock.getTime()
# Images can be manipulated on the fly
faceRGB.ori += 1 # advance ori by 1 degree
faceRGB.draw()
faceALPHA.phase += 0.01 # advance phase by 1/100th of a cycle
faceALPHA.draw()
beach.draw()
# update fps once per second
if t - lastFPSupdate > 1.0:
lastFPS = win.fps()
lastFPSupdate = t
message.text = "%ifps, [Esc] to quit" % lastFPS
message.draw()
win.flip()
def clearEvents(self, eventType=None):
if havePTB:
for buffer in self._buffers.values():
buffer._evts.clear()
buffer._keys.clear()
buffer._keysStillDown.clear()
else:
event.clearEvents(eventType)
# Draw bubbles of increasing radius at random positions
for radius in range(n_bubbles):
bubble.setRadius(radius/2.)
bubble.setPos(((rnd.random()-.5) * scrsize[0],
(rnd.random()-.5) * scrsize[1] ))
bubble.draw()
# Show the new screen we've drawn
win.flip()
# For the duration of 'changetime',
# Listen for a spacebar or escape press
change_clock.reset()
while change_clock.getTime() <= changetime:
keys = event.getKeys(keyList=['space','escape'])
if len(keys) > 0:
break
# Analyze the keypress
if keys:
if 'escape' in keys:
# Escape press = quit the experiment
break
else:
# Spacebar press = correct change detection; register response time
acc = 1
rt = rt_clock.getTime()
else:
# No press = failed change detection; maximal response time
acc = 0