Guest User

polygon_clickable_height

a guest
Sep 24th, 2019
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This experiment was created using PsychoPy3 Experiment Builder (v3.1.5),
  5. on Tue Sep 24 13:58:42 2019
  6. If you publish work using this script please cite the PsychoPy publications:
  7. Peirce, JW (2007) PsychoPy - Psychophysics software in Python.
  8. Journal of Neuroscience Methods, 162(1-2), 8-13.
  9. Peirce, JW (2009) Generating stimuli for neuroscience using PsychoPy.
  10. Frontiers in Neuroinformatics, 2:10. doi: 10.3389/neuro.11.010.2008
  11. """
  12.  
  13. from __future__ import absolute_import, division
  14. from psychopy import locale_setup, sound, gui, visual, core, data, event, logging, clock
  15. from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
  16. STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)
  17. import numpy as np # whole numpy lib is available, prepend 'np.'
  18. from numpy import (sin, cos, tan, log, log10, pi, average,
  19. sqrt, std, deg2rad, rad2deg, linspace, asarray)
  20. from numpy.random import random, randint, normal, shuffle
  21. import os # handy system and path functions
  22. import sys # to get file system encoding
  23.  
  24. from psychopy.hardware import keyboard
  25.  
  26. # Ensure that relative paths start from the same directory as this script
  27. _thisDir = os.path.dirname(os.path.abspath(__file__))
  28. os.chdir(_thisDir)
  29.  
  30. # Store info about the experiment session
  31. psychopyVersion = '3.1.5'
  32. expName = 'LOLTEMPREM' # from the Builder filename that created this script
  33. expInfo = {'participant': '', 'session': '001'}
  34. dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
  35. if dlg.OK == False:
  36. core.quit() # user pressed cancel
  37. expInfo['date'] = data.getDateStr() # add a simple timestamp
  38. expInfo['expName'] = expName
  39. expInfo['psychopyVersion'] = psychopyVersion
  40.  
  41. # Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
  42. filename = _thisDir + os.sep + u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])
  43.  
  44. # An ExperimentHandler isn't essential but helps with data saving
  45. thisExp = data.ExperimentHandler(name=expName, version='',
  46. extraInfo=expInfo, runtimeInfo=None,
  47. originPath='/Users/lowe/Documents/Programmering/Python/PsychoPy projects/My own practice projects/LOLTEMPREM.py',
  48. savePickle=True, saveWideText=True,
  49. dataFileName=filename)
  50. # save a log file for detail verbose info
  51. logFile = logging.LogFile(filename + '.log', level=logging.EXP)
  52. logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file
  53.  
  54. endExpNow = False # flag for 'escape' or other condition => quit the exp
  55.  
  56. # Start Code - component code to be run before the window creation
  57.  
  58. # Setup the Window
  59. win = visual.Window(
  60. size=[1280, 800], fullscr=True, screen=0,
  61. winType='pyglet', allowGUI=False, allowStencil=False,
  62. monitor='testMonitor', color=[0, 0, 0], colorSpace='rgb',
  63. blendMode='avg', useFBO=True,
  64. units='height')
  65. # store frame rate of monitor if we can measure it
  66. expInfo['frameRate'] = win.getActualFrameRate()
  67. if expInfo['frameRate'] != None:
  68. frameDur = 1.0 / round(expInfo['frameRate'])
  69. else:
  70. frameDur = 1.0 / 60.0 # could not measure, so guess
  71.  
  72. # create a default keyboard (e.g. to check for escape)
  73. defaultKeyboard = keyboard.Keyboard()
  74.  
  75. # Initialize components for Routine "trial"
  76. trialClock = core.Clock()
  77. polygon = visual.ShapeStim(
  78. win=win, name='polygon', units='deg',
  79. vertices=[[-(3, 3)[0] / 2.0, -(3, 3)[1] / 2.0], [+(3, 3)[0] / 2.0, -(3, 3)[1] / 2.0], [0, (3, 3)[1] / 2.0]],
  80. ori=0, pos=(0, 0),
  81. lineWidth=1, lineColor=[1, 1, 1], lineColorSpace='rgb',
  82. fillColor=[1, 1, 1], fillColorSpace='rgb',
  83. opacity=1, depth=0.0, interpolate=True)
  84. mouse = event.Mouse(win=win)
  85. x, y = [None, None]
  86. mouse.mouseClock = core.Clock()
  87.  
  88. # Create some handy timers
  89. globalClock = core.Clock() # to track the time since experiment started
  90. routineTimer = core.CountdownTimer() # to track time remaining of each (non-slip) routine
  91.  
  92. # ------Prepare to start Routine "trial"-------
  93. t = 0
  94. trialClock.reset() # clock
  95. frameN = -1
  96. continueRoutine = True
  97. # update component parameters for each repeat
  98. # setup some python lists for storing info about the mouse
  99. mouse.x = []
  100. mouse.y = []
  101. mouse.leftButton = []
  102. mouse.midButton = []
  103. mouse.rightButton = []
  104. mouse.time = []
  105. mouse.clicked_name = []
  106. gotValidClick = False # until a click is received
  107. # keep track of which components have finished
  108. trialComponents = [polygon, mouse]
  109. for thisComponent in trialComponents:
  110. thisComponent.tStart = None
  111. thisComponent.tStop = None
  112. thisComponent.tStartRefresh = None
  113. thisComponent.tStopRefresh = None
  114. if hasattr(thisComponent, 'status'):
  115. thisComponent.status = NOT_STARTED
  116.  
  117. # -------Start Routine "trial"-------
  118. while continueRoutine:
  119. # get current time
  120. t = trialClock.getTime()
  121. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  122. # update/draw components on each frame
  123.  
  124. # *polygon* updates
  125. if t >= 0.0 and polygon.status == NOT_STARTED:
  126. # keep track of start time/frame for later
  127. polygon.tStart = t # not accounting for scr refresh
  128. polygon.frameNStart = frameN # exact frame index
  129. win.timeOnFlip(polygon, 'tStartRefresh') # time at next scr refresh
  130. polygon.setAutoDraw(True)
  131. # *mouse* updates
  132. if t >= 0.0 and mouse.status == NOT_STARTED:
  133. # keep track of start time/frame for later
  134. mouse.tStart = t # not accounting for scr refresh
  135. mouse.frameNStart = frameN # exact frame index
  136. win.timeOnFlip(mouse, 'tStartRefresh') # time at next scr refresh
  137. mouse.status = STARTED
  138. mouse.mouseClock.reset()
  139. prevButtonState = mouse.getPressed() # if button is down already this ISN'T a new click
  140. if mouse.status == STARTED: # only update if started and not finished!
  141. buttons = mouse.getPressed()
  142. if buttons != prevButtonState: # button state changed?
  143. prevButtonState = buttons
  144. if sum(buttons) > 0: # state changed to a new click
  145. # check if the mouse was inside our 'clickable' objects
  146. gotValidClick = False
  147. for obj in [polygon]:
  148. if obj.contains(mouse):
  149. gotValidClick = True
  150. mouse.clicked_name.append(obj.name)
  151. x, y = mouse.getPos()
  152. mouse.x.append(x)
  153. mouse.y.append(y)
  154. buttons = mouse.getPressed()
  155. mouse.leftButton.append(buttons[0])
  156. mouse.midButton.append(buttons[1])
  157. mouse.rightButton.append(buttons[2])
  158. mouse.time.append(mouse.mouseClock.getTime())
  159. if gotValidClick: # abort routine on response
  160. continueRoutine = False
  161.  
  162. # check for quit (typically the Esc key)
  163. if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
  164. core.quit()
  165.  
  166. # check if all components have finished
  167. if not continueRoutine: # a component has requested a forced-end of Routine
  168. break
  169. continueRoutine = False # will revert to True if at least one component still running
  170. for thisComponent in trialComponents:
  171. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  172. continueRoutine = True
  173. break # at least one component has not yet finished
  174.  
  175. # refresh the screen
  176. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  177. win.flip()
  178.  
  179. # -------Ending Routine "trial"-------
  180. for thisComponent in trialComponents:
  181. if hasattr(thisComponent, "setAutoDraw"):
  182. thisComponent.setAutoDraw(False)
  183. thisExp.addData('polygon.started', polygon.tStartRefresh)
  184. thisExp.addData('polygon.stopped', polygon.tStopRefresh)
  185. # store data for thisExp (ExperimentHandler)
  186. if len(mouse.x): thisExp.addData('mouse.x', mouse.x[0])
  187. if len(mouse.y): thisExp.addData('mouse.y', mouse.y[0])
  188. if len(mouse.leftButton): thisExp.addData('mouse.leftButton', mouse.leftButton[0])
  189. if len(mouse.midButton): thisExp.addData('mouse.midButton', mouse.midButton[0])
  190. if len(mouse.rightButton): thisExp.addData('mouse.rightButton', mouse.rightButton[0])
  191. if len(mouse.time): thisExp.addData('mouse.time', mouse.time[0])
  192. if len(mouse.clicked_name): thisExp.addData('mouse.clicked_name', mouse.clicked_name[0])
  193. thisExp.addData('mouse.started', mouse.tStart)
  194. thisExp.addData('mouse.stopped', mouse.tStop)
  195. thisExp.nextEntry()
  196. # the Routine "trial" was not non-slip safe, so reset the non-slip timer
  197. routineTimer.reset()
  198.  
  199. # Flip one final time so any remaining win.callOnFlip()
  200. # and win.timeOnFlip() tasks get executed before quitting
  201. win.flip()
  202.  
  203. # these shouldn't be strictly necessary (should auto-save)
  204. thisExp.saveAsWideText(filename + '.csv')
  205. thisExp.saveAsPickle(filename)
  206. logging.flush()
  207. # make sure everything is closed down
  208. thisExp.abort() # or data files will save again on exit
  209. win.close()
  210. core.quit()
RAW Paste Data