daily_routine.py 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This experiment was created using PsychoPy3 Experiment Builder (v2023.2.3),
  5. on 一月 17, 2024, at 17:36
  6. If you publish work using this script the most relevant publication is:
  7. Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019)
  8. PsychoPy2: Experiments in behavior made easy Behav Res 51: 195.
  9. https://doi.org/10.3758/s13428-018-01193-y
  10. """
  11. # --- Import packages ---
  12. from psychopy import locale_setup
  13. from psychopy import prefs
  14. from psychopy import plugins
  15. plugins.activatePlugins()
  16. prefs.hardware['audioLib'] = 'ptb'
  17. prefs.hardware['audioLatencyMode'] = '3'
  18. from psychopy import sound, gui, visual, core, data, event, logging, clock, colors, layout
  19. from psychopy.tools import environmenttools
  20. from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
  21. STOPPED, FINISHED, PRESSED, RELEASED, FOREVER, priority)
  22. import numpy as np # whole numpy lib is available, prepend 'np.'
  23. from numpy import (sin, cos, tan, log, log10, pi, average,
  24. sqrt, std, deg2rad, rad2deg, linspace, asarray)
  25. from numpy.random import random, randint, normal, shuffle, choice as randchoice
  26. import os # handy system and path functions
  27. import sys # to get file system encoding
  28. import psychopy.iohub as io
  29. from psychopy.hardware import keyboard
  30. # Run 'Before Experiment' code from config
  31. import argparse
  32. from device.trigger_box import TriggerNeuracle
  33. # get train params
  34. def parse_args():
  35. parser = argparse.ArgumentParser(
  36. description='Daily routine'
  37. )
  38. parser.add_argument(
  39. '--side',
  40. dest='side',
  41. help='left hand or right hand',
  42. type=str
  43. )
  44. parser.add_argument(
  45. '--trial-num',
  46. dest='trial_num',
  47. help='total trial number',
  48. type=int
  49. )
  50. parser.add_argument(
  51. '--major-side-num',
  52. dest='major_side_num',
  53. help='trial number of major side',
  54. type=int
  55. )
  56. parser.add_argument(
  57. '--trigger-port',
  58. dest='trigger_port',
  59. help='Triggerbox serial port',
  60. type=str
  61. )
  62. return parser.parse_args()
  63. args = parse_args()
  64. # connect to trigger box
  65. trigger = TriggerNeuracle(port=args.trigger_port)
  66. # Run 'Before Experiment' code from assign_L_R_trails
  67. import random
  68. trials_list = []
  69. for i in range(args.trial_num):
  70. trials_list.append(i+1)
  71. random.shuffle(trials_list)
  72. elbow_opposite = trials_list[:(args.trial_num-args.major_side_num)]
  73. print("elbow_opposite:",sorted(elbow_opposite))
  74. random.shuffle(trials_list)
  75. hand_opposite = trials_list[:(args.trial_num-args.major_side_num)]
  76. print("hand_opposite:",sorted(hand_opposite))
  77. # --- Setup global variables (available in all functions) ---
  78. # Ensure that relative paths start from the same directory as this script
  79. _thisDir = os.path.dirname(os.path.abspath(__file__))
  80. # Store info about the experiment session
  81. psychopyVersion = '2023.2.3'
  82. expName = 'daily_rountine' # from the Builder filename that created this script
  83. expInfo = {
  84. 'participant': f"{randint(0, 999999):06.0f}",
  85. 'session': '001',
  86. 'date': data.getDateStr(), # add a simple timestamp
  87. 'expName': expName,
  88. 'psychopyVersion': psychopyVersion,
  89. }
  90. def showExpInfoDlg(expInfo):
  91. """
  92. Show participant info dialog.
  93. Parameters
  94. ==========
  95. expInfo : dict
  96. Information about this experiment, created by the `setupExpInfo` function.
  97. Returns
  98. ==========
  99. dict
  100. Information about this experiment.
  101. """
  102. # temporarily remove keys which the dialog doesn't need to show
  103. poppedKeys = {
  104. 'date': expInfo.pop('date', data.getDateStr()),
  105. 'expName': expInfo.pop('expName', expName),
  106. 'psychopyVersion': expInfo.pop('psychopyVersion', psychopyVersion),
  107. }
  108. # show participant info dialog
  109. dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
  110. if dlg.OK == False:
  111. core.quit() # user pressed cancel
  112. # restore hidden keys
  113. expInfo.update(poppedKeys)
  114. # return expInfo
  115. return expInfo
  116. def setupData(expInfo, dataDir=None):
  117. """
  118. Make an ExperimentHandler to handle trials and saving.
  119. Parameters
  120. ==========
  121. expInfo : dict
  122. Information about this experiment, created by the `setupExpInfo` function.
  123. dataDir : Path, str or None
  124. Folder to save the data to, leave as None to create a folder in the current directory.
  125. Returns
  126. ==========
  127. psychopy.data.ExperimentHandler
  128. Handler object for this experiment, contains the data to save and information about
  129. where to save it to.
  130. """
  131. # data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
  132. if dataDir is None:
  133. dataDir = _thisDir
  134. filename = u'data/%s_%s_%s' % (expInfo['participant'], expName, expInfo['date'])
  135. # make sure filename is relative to dataDir
  136. if os.path.isabs(filename):
  137. dataDir = os.path.commonprefix([dataDir, filename])
  138. filename = os.path.relpath(filename, dataDir)
  139. # an ExperimentHandler isn't essential but helps with data saving
  140. thisExp = data.ExperimentHandler(
  141. name=expName, version='',
  142. extraInfo=expInfo, runtimeInfo=None,
  143. originPath='C:\\Users\\asena\\Desktop\\kraken\\backend\\daily_routine.py',
  144. savePickle=True, saveWideText=True,
  145. dataFileName=dataDir + os.sep + filename, sortColumns='time'
  146. )
  147. thisExp.setPriority('thisRow.t', priority.CRITICAL)
  148. thisExp.setPriority('expName', priority.LOW)
  149. # return experiment handler
  150. return thisExp
  151. def setupLogging(filename):
  152. """
  153. Setup a log file and tell it what level to log at.
  154. Parameters
  155. ==========
  156. filename : str or pathlib.Path
  157. Filename to save log file and data files as, doesn't need an extension.
  158. Returns
  159. ==========
  160. psychopy.logging.LogFile
  161. Text stream to receive inputs from the logging system.
  162. """
  163. # this outputs to the screen, not a file
  164. logging.console.setLevel(logging.EXP)
  165. # save a log file for detail verbose info
  166. logFile = logging.LogFile(filename+'.log', level=logging.EXP)
  167. return logFile
  168. def setupWindow(expInfo=None, win=None):
  169. """
  170. Setup the Window
  171. Parameters
  172. ==========
  173. expInfo : dict
  174. Information about this experiment, created by the `setupExpInfo` function.
  175. win : psychopy.visual.Window
  176. Window to setup - leave as None to create a new window.
  177. Returns
  178. ==========
  179. psychopy.visual.Window
  180. Window in which to run this experiment.
  181. """
  182. if win is None:
  183. # if not given a window to setup, make one
  184. win = visual.Window(
  185. size=[1707, 1067], fullscr=True, screen=0,
  186. winType='pyglet', allowStencil=False,
  187. monitor='testMonitor', color=[1.0000, 1.0000, 1.0000], colorSpace='rgb',
  188. backgroundImage='', backgroundFit='none',
  189. blendMode='avg', useFBO=True,
  190. units='height'
  191. )
  192. if expInfo is not None:
  193. # store frame rate of monitor if we can measure it
  194. expInfo['frameRate'] = win.getActualFrameRate()
  195. else:
  196. # if we have a window, just set the attributes which are safe to set
  197. win.color = [1.0000, 1.0000, 1.0000]
  198. win.colorSpace = 'rgb'
  199. win.backgroundImage = ''
  200. win.backgroundFit = 'none'
  201. win.units = 'height'
  202. win.mouseVisible = False
  203. win.hideMessage()
  204. return win
  205. def setupInputs(expInfo, thisExp, win):
  206. """
  207. Setup whatever inputs are available (mouse, keyboard, eyetracker, etc.)
  208. Parameters
  209. ==========
  210. expInfo : dict
  211. Information about this experiment, created by the `setupExpInfo` function.
  212. thisExp : psychopy.data.ExperimentHandler
  213. Handler object for this experiment, contains the data to save and information about
  214. where to save it to.
  215. win : psychopy.visual.Window
  216. Window in which to run this experiment.
  217. Returns
  218. ==========
  219. dict
  220. Dictionary of input devices by name.
  221. """
  222. # --- Setup input devices ---
  223. inputs = {}
  224. ioConfig = {}
  225. # Setup iohub keyboard
  226. ioConfig['Keyboard'] = dict(use_keymap='psychopy')
  227. ioSession = '1'
  228. if 'session' in expInfo:
  229. ioSession = str(expInfo['session'])
  230. ioServer = io.launchHubServer(window=win, **ioConfig)
  231. eyetracker = None
  232. # create a default keyboard (e.g. to check for escape)
  233. defaultKeyboard = keyboard.Keyboard(backend='iohub')
  234. # return inputs dict
  235. return {
  236. 'ioServer': ioServer,
  237. 'defaultKeyboard': defaultKeyboard,
  238. 'eyetracker': eyetracker,
  239. }
  240. def pauseExperiment(thisExp, inputs=None, win=None, timers=[], playbackComponents=[]):
  241. """
  242. Pause this experiment, preventing the flow from advancing to the next routine until resumed.
  243. Parameters
  244. ==========
  245. thisExp : psychopy.data.ExperimentHandler
  246. Handler object for this experiment, contains the data to save and information about
  247. where to save it to.
  248. inputs : dict
  249. Dictionary of input devices by name.
  250. win : psychopy.visual.Window
  251. Window for this experiment.
  252. timers : list, tuple
  253. List of timers to reset once pausing is finished.
  254. playbackComponents : list, tuple
  255. List of any components with a `pause` method which need to be paused.
  256. """
  257. # if we are not paused, do nothing
  258. if thisExp.status != PAUSED:
  259. return
  260. # pause any playback components
  261. for comp in playbackComponents:
  262. comp.pause()
  263. # prevent components from auto-drawing
  264. win.stashAutoDraw()
  265. # run a while loop while we wait to unpause
  266. while thisExp.status == PAUSED:
  267. # make sure we have a keyboard
  268. if inputs is None:
  269. inputs = {
  270. 'defaultKeyboard': keyboard.Keyboard(backend='ioHub')
  271. }
  272. # check for quit (typically the Esc key)
  273. if inputs['defaultKeyboard'].getKeys(keyList=['escape']):
  274. endExperiment(thisExp, win=win, inputs=inputs)
  275. # flip the screen
  276. win.flip()
  277. # if stop was requested while paused, quit
  278. if thisExp.status == FINISHED:
  279. endExperiment(thisExp, inputs=inputs, win=win)
  280. # resume any playback components
  281. for comp in playbackComponents:
  282. comp.play()
  283. # restore auto-drawn components
  284. win.retrieveAutoDraw()
  285. # reset any timers
  286. for timer in timers:
  287. timer.reset()
  288. def run(expInfo, thisExp, win, inputs, globalClock=None, thisSession=None):
  289. """
  290. Run the experiment flow.
  291. Parameters
  292. ==========
  293. expInfo : dict
  294. Information about this experiment, created by the `setupExpInfo` function.
  295. thisExp : psychopy.data.ExperimentHandler
  296. Handler object for this experiment, contains the data to save and information about
  297. where to save it to.
  298. psychopy.visual.Window
  299. Window in which to run this experiment.
  300. inputs : dict
  301. Dictionary of input devices by name.
  302. globalClock : psychopy.core.clock.Clock or None
  303. Clock to get global time from - supply None to make a new one.
  304. thisSession : psychopy.session.Session or None
  305. Handle of the Session object this experiment is being run from, if any.
  306. """
  307. # mark experiment as started
  308. thisExp.status = STARTED
  309. # make sure variables created by exec are available globally
  310. exec = environmenttools.setExecEnvironment(globals())
  311. # get device handles from dict of input devices
  312. ioServer = inputs['ioServer']
  313. defaultKeyboard = inputs['defaultKeyboard']
  314. eyetracker = inputs['eyetracker']
  315. # make sure we're running in the directory for this experiment
  316. os.chdir(_thisDir)
  317. # get filename from ExperimentHandler for convenience
  318. filename = thisExp.dataFileName
  319. frameTolerance = 0.001 # how close to onset before 'same' frame
  320. endExpNow = False # flag for 'escape' or other condition => quit the exp
  321. # get frame duration from frame rate in expInfo
  322. if 'frameRate' in expInfo and expInfo['frameRate'] is not None:
  323. frameDur = 1.0 / round(expInfo['frameRate'])
  324. else:
  325. frameDur = 1.0 / 60.0 # could not measure, so guess
  326. # Start Code - component code to be run after the window creation
  327. # --- Initialize components for Routine "prepare_0" ---
  328. text_4 = visual.TextStim(win=win, name='text_4',
  329. text='第一个任务即将开始\n请您放松身体,保持安静\n\n任务时长:2分钟\n\n如果准备好了,请按空格键',
  330. font='Open Sans',
  331. pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
  332. color=[-1.0000, -1.0000, -1.0000], colorSpace='rgb', opacity=None,
  333. languageStyle='LTR',
  334. depth=0.0);
  335. key_resp_4 = keyboard.Keyboard()
  336. sound_1 = sound.Sound('static/audios/准备1_静息.wav', secs=-1, stereo=True, hamming=True,
  337. name='sound_1')
  338. sound_1.setVolume(1.0)
  339. # --- Initialize components for Routine "resting" ---
  340. image_3 = visual.ImageStim(
  341. win=win,
  342. name='image_3', units='pix',
  343. image='static/images/daily_resting.png', mask=None, anchor='center',
  344. ori=0.0, pos=(0, 0), size=(662, 500),
  345. color=[1,1,1], colorSpace='rgb', opacity=None,
  346. flipHoriz=False, flipVert=False,
  347. texRes=128.0, interpolate=True, depth=0.0)
  348. sound_7 = sound.Sound('static/audios/任务1_静息.wav', secs=-1, stereo=True, hamming=True,
  349. name='sound_7')
  350. sound_7.setVolume(1.0)
  351. sound_16 = sound.Sound('static/audios/ding.wav', secs=1.0, stereo=True, hamming=True,
  352. name='sound_16')
  353. sound_16.setVolume(1.0)
  354. # --- Initialize components for Routine "prepare_1" ---
  355. elbow_prepare = visual.TextStim(win=win, name='elbow_prepare',
  356. text=None,
  357. font='Open Sans',
  358. pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
  359. color=[-1.0000, -1.0000, -1.0000], colorSpace='rgb', opacity=None,
  360. languageStyle='LTR',
  361. depth=0.0);
  362. key_resp = keyboard.Keyboard()
  363. sound_2 = sound.Sound('static/audios/准备2_肘部.wav', secs=-1, stereo=True, hamming=True,
  364. name='sound_2')
  365. sound_2.setVolume(1.0)
  366. # --- Initialize components for Routine "task_1" ---
  367. elbow_img = visual.ImageStim(
  368. win=win,
  369. name='elbow_img',
  370. image=None, mask=None, anchor='center',
  371. ori=0.0, pos=(0, 0), size=(0.56,0.5),
  372. color=[1.0000, 1.0000, 1.0000], colorSpace='rgb', opacity=None,
  373. flipHoriz=False, flipVert=False,
  374. texRes=128.0, interpolate=True, depth=0.0)
  375. sound_6 = sound.Sound('static/audios/任务2_肘部.wav', secs=-1, stereo=True, hamming=True,
  376. name='sound_6')
  377. sound_6.setVolume(1.0)
  378. # --- Initialize components for Routine "rest_1" ---
  379. image_2 = visual.ImageStim(
  380. win=win,
  381. name='image_2',
  382. image='static/images/rest.png', mask=None, anchor='center',
  383. ori=0.0, pos=(0, 0), size=(0.56,0.5),
  384. color=[1.0000, 1.0000, 1.0000], colorSpace='rgb', opacity=None,
  385. flipHoriz=False, flipVert=False,
  386. texRes=128.0, interpolate=True, depth=0.0)
  387. sound_8 = sound.Sound('static/audios/ding.wav', secs=1.5, stereo=True, hamming=True,
  388. name='sound_8')
  389. sound_8.setVolume(1.0)
  390. sound_9 = sound.Sound('static/audios/放松.wav', secs=-1, stereo=True, hamming=True,
  391. name='sound_9')
  392. sound_9.setVolume(1.0)
  393. # --- Initialize components for Routine "prepare_2" ---
  394. passive_prepare = visual.TextStim(win=win, name='passive_prepare',
  395. text=None,
  396. font='Open Sans',
  397. pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
  398. color=[-1.0000, -1.0000, -1.0000], colorSpace='rgb', opacity=None,
  399. languageStyle='LTR',
  400. depth=0.0);
  401. key_resp_2 = keyboard.Keyboard()
  402. sound_3 = sound.Sound('static/audios/准备3_被动.wav', secs=-1, stereo=True, hamming=True,
  403. name='sound_3')
  404. sound_3.setVolume(1.0)
  405. # --- Initialize components for Routine "task_2" ---
  406. passive_img = visual.ImageStim(
  407. win=win,
  408. name='passive_img',
  409. image=None, mask=None, anchor='center',
  410. ori=0.0, pos=(0, 0), size=(0.56,0.5),
  411. color=[1,1,1], colorSpace='rgb', opacity=None,
  412. flipHoriz=False, flipVert=False,
  413. texRes=128.0, interpolate=True, depth=0.0)
  414. sound_14 = sound.Sound('static/audios/任务3_被动.wav', secs=-1, stereo=True, hamming=True,
  415. name='sound_14')
  416. sound_14.setVolume(1.0)
  417. # --- Initialize components for Routine "rest_2" ---
  418. image_6 = visual.ImageStim(
  419. win=win,
  420. name='image_6',
  421. image='static/images/rest.png', mask=None, anchor='center',
  422. ori=0.0, pos=(0, 0), size=(0.56,0.5),
  423. color=[1,1,1], colorSpace='rgb', opacity=None,
  424. flipHoriz=False, flipVert=False,
  425. texRes=128.0, interpolate=True, depth=0.0)
  426. sound_10 = sound.Sound('static/audios/ding.wav', secs=1.5, stereo=True, hamming=True,
  427. name='sound_10')
  428. sound_10.setVolume(1.0)
  429. sound_11 = sound.Sound('static/audios/放松.wav', secs=-1, stereo=True, hamming=True,
  430. name='sound_11')
  431. sound_11.setVolume(1.0)
  432. # --- Initialize components for Routine "prepare_3" ---
  433. active_prepare = visual.TextStim(win=win, name='active_prepare',
  434. text=None,
  435. font='Open Sans',
  436. pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
  437. color=[-1.0000, -1.0000, -1.0000], colorSpace='rgb', opacity=None,
  438. languageStyle='LTR',
  439. depth=0.0);
  440. key_resp_3 = keyboard.Keyboard()
  441. sound_4 = sound.Sound('static/audios/准备4_主动.wav', secs=-1, stereo=True, hamming=True,
  442. name='sound_4')
  443. sound_4.setVolume(1.0)
  444. # --- Initialize components for Routine "task_3" ---
  445. active_img = visual.ImageStim(
  446. win=win,
  447. name='active_img',
  448. image=None, mask=None, anchor='center',
  449. ori=0.0, pos=(0, 0), size=(0.56,0.5),
  450. color=[1,1,1], colorSpace='rgb', opacity=None,
  451. flipHoriz=False, flipVert=False,
  452. texRes=128.0, interpolate=True, depth=0.0)
  453. sound_15 = sound.Sound('static/audios/任务4_主动.wav', secs=-1, stereo=True, hamming=True,
  454. name='sound_15')
  455. sound_15.setVolume(1.0)
  456. # --- Initialize components for Routine "rest_3" ---
  457. image_7 = visual.ImageStim(
  458. win=win,
  459. name='image_7',
  460. image='static/images/rest.png', mask=None, anchor='center',
  461. ori=0.0, pos=(0, 0), size=(0.56,0.5),
  462. color=[1,1,1], colorSpace='rgb', opacity=None,
  463. flipHoriz=False, flipVert=False,
  464. texRes=128.0, interpolate=True, depth=0.0)
  465. sound_12 = sound.Sound('static/audios/ding.wav', secs=1.5, stereo=True, hamming=True,
  466. name='sound_12')
  467. sound_12.setVolume(1.0)
  468. sound_13 = sound.Sound('static/audios/放松.wav', secs=-1, stereo=True, hamming=True,
  469. name='sound_13')
  470. sound_13.setVolume(1.0)
  471. # --- Initialize components for Routine "done" ---
  472. end = visual.TextStim(win=win, name='end',
  473. text='恭喜您完成了所有任务!\n非常感谢您的配合!',
  474. font='Open Sans',
  475. pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
  476. color=[-1.0000, -1.0000, -1.0000], colorSpace='rgb', opacity=None,
  477. languageStyle='LTR',
  478. depth=0.0);
  479. sound_5 = sound.Sound('static/audios/结束.wav', secs=-1, stereo=True, hamming=True,
  480. name='sound_5')
  481. sound_5.setVolume(1.0)
  482. # create some handy timers
  483. if globalClock is None:
  484. globalClock = core.Clock() # to track the time since experiment started
  485. if ioServer is not None:
  486. ioServer.syncClock(globalClock)
  487. logging.setDefaultClock(globalClock)
  488. routineTimer = core.Clock() # to track time remaining of each (possibly non-slip) routine
  489. win.flip() # flip window to reset last flip timer
  490. # store the exact time the global clock started
  491. expInfo['expStart'] = data.getDateStr(format='%Y-%m-%d %Hh%M.%S.%f %z', fractionalSecondDigits=6)
  492. # --- Prepare to start Routine "prepare_0" ---
  493. continueRoutine = True
  494. # update component parameters for each repeat
  495. thisExp.addData('prepare_0.started', globalClock.getTime())
  496. key_resp_4.keys = []
  497. key_resp_4.rt = []
  498. _key_resp_4_allKeys = []
  499. sound_1.setSound('static/audios/准备1_静息.wav', hamming=True)
  500. sound_1.setVolume(1.0, log=False)
  501. sound_1.seek(0)
  502. # keep track of which components have finished
  503. prepare_0Components = [text_4, key_resp_4, sound_1]
  504. for thisComponent in prepare_0Components:
  505. thisComponent.tStart = None
  506. thisComponent.tStop = None
  507. thisComponent.tStartRefresh = None
  508. thisComponent.tStopRefresh = None
  509. if hasattr(thisComponent, 'status'):
  510. thisComponent.status = NOT_STARTED
  511. # reset timers
  512. t = 0
  513. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  514. frameN = -1
  515. # --- Run Routine "prepare_0" ---
  516. routineForceEnded = not continueRoutine
  517. while continueRoutine:
  518. # get current time
  519. t = routineTimer.getTime()
  520. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  521. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  522. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  523. # update/draw components on each frame
  524. # *text_4* updates
  525. # if text_4 is starting this frame...
  526. if text_4.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
  527. # keep track of start time/frame for later
  528. text_4.frameNStart = frameN # exact frame index
  529. text_4.tStart = t # local t and not account for scr refresh
  530. text_4.tStartRefresh = tThisFlipGlobal # on global time
  531. win.timeOnFlip(text_4, 'tStartRefresh') # time at next scr refresh
  532. # add timestamp to datafile
  533. thisExp.timestampOnFlip(win, 'text_4.started')
  534. # update status
  535. text_4.status = STARTED
  536. text_4.setAutoDraw(True)
  537. # if text_4 is active this frame...
  538. if text_4.status == STARTED:
  539. # update params
  540. pass
  541. # *key_resp_4* updates
  542. waitOnFlip = False
  543. # if key_resp_4 is starting this frame...
  544. if key_resp_4.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
  545. # keep track of start time/frame for later
  546. key_resp_4.frameNStart = frameN # exact frame index
  547. key_resp_4.tStart = t # local t and not account for scr refresh
  548. key_resp_4.tStartRefresh = tThisFlipGlobal # on global time
  549. win.timeOnFlip(key_resp_4, 'tStartRefresh') # time at next scr refresh
  550. # add timestamp to datafile
  551. thisExp.timestampOnFlip(win, 'key_resp_4.started')
  552. # update status
  553. key_resp_4.status = STARTED
  554. # keyboard checking is just starting
  555. waitOnFlip = True
  556. win.callOnFlip(key_resp_4.clock.reset) # t=0 on next screen flip
  557. win.callOnFlip(key_resp_4.clearEvents, eventType='keyboard') # clear events on next screen flip
  558. if key_resp_4.status == STARTED and not waitOnFlip:
  559. theseKeys = key_resp_4.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)
  560. _key_resp_4_allKeys.extend(theseKeys)
  561. if len(_key_resp_4_allKeys):
  562. key_resp_4.keys = _key_resp_4_allKeys[-1].name # just the last key pressed
  563. key_resp_4.rt = _key_resp_4_allKeys[-1].rt
  564. key_resp_4.duration = _key_resp_4_allKeys[-1].duration
  565. # a response ends the routine
  566. continueRoutine = False
  567. # if sound_1 is starting this frame...
  568. if sound_1.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  569. # keep track of start time/frame for later
  570. sound_1.frameNStart = frameN # exact frame index
  571. sound_1.tStart = t # local t and not account for scr refresh
  572. sound_1.tStartRefresh = tThisFlipGlobal # on global time
  573. # add timestamp to datafile
  574. thisExp.addData('sound_1.started', tThisFlipGlobal)
  575. # update status
  576. sound_1.status = STARTED
  577. sound_1.play(when=win) # sync with win flip
  578. # update sound_1 status according to whether it's playing
  579. if sound_1.isPlaying:
  580. sound_1.status = STARTED
  581. elif sound_1.isFinished:
  582. sound_1.status = FINISHED
  583. # check for quit (typically the Esc key)
  584. if defaultKeyboard.getKeys(keyList=["escape"]):
  585. thisExp.status = FINISHED
  586. if thisExp.status == FINISHED or endExpNow:
  587. endExperiment(thisExp, inputs=inputs, win=win)
  588. return
  589. # check if all components have finished
  590. if not continueRoutine: # a component has requested a forced-end of Routine
  591. routineForceEnded = True
  592. break
  593. continueRoutine = False # will revert to True if at least one component still running
  594. for thisComponent in prepare_0Components:
  595. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  596. continueRoutine = True
  597. break # at least one component has not yet finished
  598. # refresh the screen
  599. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  600. win.flip()
  601. # --- Ending Routine "prepare_0" ---
  602. for thisComponent in prepare_0Components:
  603. if hasattr(thisComponent, "setAutoDraw"):
  604. thisComponent.setAutoDraw(False)
  605. thisExp.addData('prepare_0.stopped', globalClock.getTime())
  606. # check responses
  607. if key_resp_4.keys in ['', [], None]: # No response was made
  608. key_resp_4.keys = None
  609. thisExp.addData('key_resp_4.keys',key_resp_4.keys)
  610. if key_resp_4.keys != None: # we had a response
  611. thisExp.addData('key_resp_4.rt', key_resp_4.rt)
  612. thisExp.addData('key_resp_4.duration', key_resp_4.duration)
  613. thisExp.nextEntry()
  614. sound_1.pause() # ensure sound has stopped at end of Routine
  615. # the Routine "prepare_0" was not non-slip safe, so reset the non-slip timer
  616. routineTimer.reset()
  617. # --- Prepare to start Routine "resting" ---
  618. continueRoutine = True
  619. # update component parameters for each repeat
  620. thisExp.addData('resting.started', globalClock.getTime())
  621. # Run 'Begin Routine' code from code_1
  622. i=0
  623. sound_7.setSound('static/audios/任务1_静息.wav', secs=5, hamming=True)
  624. sound_7.setVolume(1.0, log=False)
  625. sound_7.seek(0)
  626. sound_16.setSound('static/audios/ding.wav', secs=1.0, hamming=True)
  627. sound_16.setVolume(1.0, log=False)
  628. sound_16.seek(0)
  629. # keep track of which components have finished
  630. restingComponents = [image_3, sound_7, sound_16]
  631. for thisComponent in restingComponents:
  632. thisComponent.tStart = None
  633. thisComponent.tStop = None
  634. thisComponent.tStartRefresh = None
  635. thisComponent.tStopRefresh = None
  636. if hasattr(thisComponent, 'status'):
  637. thisComponent.status = NOT_STARTED
  638. # reset timers
  639. t = 0
  640. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  641. frameN = -1
  642. # --- Run Routine "resting" ---
  643. routineForceEnded = not continueRoutine
  644. while continueRoutine and routineTimer.getTime() < 121.0:
  645. # get current time
  646. t = routineTimer.getTime()
  647. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  648. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  649. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  650. # update/draw components on each frame
  651. # *image_3* updates
  652. # if image_3 is starting this frame...
  653. if image_3.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  654. # keep track of start time/frame for later
  655. image_3.frameNStart = frameN # exact frame index
  656. image_3.tStart = t # local t and not account for scr refresh
  657. image_3.tStartRefresh = tThisFlipGlobal # on global time
  658. win.timeOnFlip(image_3, 'tStartRefresh') # time at next scr refresh
  659. # add timestamp to datafile
  660. thisExp.timestampOnFlip(win, 'image_3.started')
  661. # update status
  662. image_3.status = STARTED
  663. image_3.setAutoDraw(True)
  664. # if image_3 is active this frame...
  665. if image_3.status == STARTED:
  666. # update params
  667. pass
  668. # if image_3 is stopping this frame...
  669. if image_3.status == STARTED:
  670. # is it time to stop? (based on global clock, using actual start)
  671. if tThisFlipGlobal > image_3.tStartRefresh + 120-frameTolerance:
  672. # keep track of stop time/frame for later
  673. image_3.tStop = t # not accounting for scr refresh
  674. image_3.frameNStop = frameN # exact frame index
  675. # add timestamp to datafile
  676. thisExp.timestampOnFlip(win, 'image_3.stopped')
  677. # update status
  678. image_3.status = FINISHED
  679. image_3.setAutoDraw(False)
  680. # Run 'Each Frame' code from code_1
  681. if i== 59:
  682. win.callOnFlip(trigger.send_trigger,0x01)
  683. i+=1
  684. else:
  685. i+=1
  686. # if sound_7 is starting this frame...
  687. if sound_7.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  688. # keep track of start time/frame for later
  689. sound_7.frameNStart = frameN # exact frame index
  690. sound_7.tStart = t # local t and not account for scr refresh
  691. sound_7.tStartRefresh = tThisFlipGlobal # on global time
  692. # add timestamp to datafile
  693. thisExp.addData('sound_7.started', tThisFlipGlobal)
  694. # update status
  695. sound_7.status = STARTED
  696. sound_7.play(when=win) # sync with win flip
  697. # if sound_7 is stopping this frame...
  698. if sound_7.status == STARTED:
  699. # is it time to stop? (based on global clock, using actual start)
  700. if tThisFlipGlobal > sound_7.tStartRefresh + 5-frameTolerance:
  701. # keep track of stop time/frame for later
  702. sound_7.tStop = t # not accounting for scr refresh
  703. sound_7.frameNStop = frameN # exact frame index
  704. # add timestamp to datafile
  705. thisExp.timestampOnFlip(win, 'sound_7.stopped')
  706. # update status
  707. sound_7.status = FINISHED
  708. sound_7.stop()
  709. # update sound_7 status according to whether it's playing
  710. if sound_7.isPlaying:
  711. sound_7.status = STARTED
  712. elif sound_7.isFinished:
  713. sound_7.status = FINISHED
  714. # if sound_16 is starting this frame...
  715. if sound_16.status == NOT_STARTED and tThisFlip >= 120-frameTolerance:
  716. # keep track of start time/frame for later
  717. sound_16.frameNStart = frameN # exact frame index
  718. sound_16.tStart = t # local t and not account for scr refresh
  719. sound_16.tStartRefresh = tThisFlipGlobal # on global time
  720. # add timestamp to datafile
  721. thisExp.addData('sound_16.started', tThisFlipGlobal)
  722. # update status
  723. sound_16.status = STARTED
  724. sound_16.play(when=win) # sync with win flip
  725. # if sound_16 is stopping this frame...
  726. if sound_16.status == STARTED:
  727. # is it time to stop? (based on global clock, using actual start)
  728. if tThisFlipGlobal > sound_16.tStartRefresh + 1.0-frameTolerance:
  729. # keep track of stop time/frame for later
  730. sound_16.tStop = t # not accounting for scr refresh
  731. sound_16.frameNStop = frameN # exact frame index
  732. # add timestamp to datafile
  733. thisExp.timestampOnFlip(win, 'sound_16.stopped')
  734. # update status
  735. sound_16.status = FINISHED
  736. sound_16.stop()
  737. # update sound_16 status according to whether it's playing
  738. if sound_16.isPlaying:
  739. sound_16.status = STARTED
  740. elif sound_16.isFinished:
  741. sound_16.status = FINISHED
  742. # check for quit (typically the Esc key)
  743. if defaultKeyboard.getKeys(keyList=["escape"]):
  744. thisExp.status = FINISHED
  745. if thisExp.status == FINISHED or endExpNow:
  746. endExperiment(thisExp, inputs=inputs, win=win)
  747. return
  748. # check if all components have finished
  749. if not continueRoutine: # a component has requested a forced-end of Routine
  750. routineForceEnded = True
  751. break
  752. continueRoutine = False # will revert to True if at least one component still running
  753. for thisComponent in restingComponents:
  754. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  755. continueRoutine = True
  756. break # at least one component has not yet finished
  757. # refresh the screen
  758. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  759. win.flip()
  760. # --- Ending Routine "resting" ---
  761. for thisComponent in restingComponents:
  762. if hasattr(thisComponent, "setAutoDraw"):
  763. thisComponent.setAutoDraw(False)
  764. thisExp.addData('resting.stopped', globalClock.getTime())
  765. sound_7.pause() # ensure sound has stopped at end of Routine
  766. sound_16.pause() # ensure sound has stopped at end of Routine
  767. # using non-slip timing so subtract the expected duration of this Routine (unless ended on request)
  768. if routineForceEnded:
  769. routineTimer.reset()
  770. else:
  771. routineTimer.addTime(-121.000000)
  772. # --- Prepare to start Routine "prepare_1" ---
  773. continueRoutine = True
  774. # update component parameters for each repeat
  775. thisExp.addData('prepare_1.started', globalClock.getTime())
  776. key_resp.keys = []
  777. key_resp.rt = []
  778. _key_resp_allKeys = []
  779. sound_2.setSound('static/audios/准备2_肘部.wav', hamming=True)
  780. sound_2.setVolume(1.0, log=False)
  781. sound_2.seek(0)
  782. # Run 'Begin Routine' code from code
  783. prepare_text1 = "下一个部分是:肘部运动\n\n这个任务需要您根据屏幕提示\n尝试弯曲肘部,然后放下\n您需要完成这个动作"+str(args.trial_num)+"次\n\n如果准备好了,请按空格键"
  784. elbow_prepare.text = prepare_text1
  785. # keep track of which components have finished
  786. prepare_1Components = [elbow_prepare, key_resp, sound_2]
  787. for thisComponent in prepare_1Components:
  788. thisComponent.tStart = None
  789. thisComponent.tStop = None
  790. thisComponent.tStartRefresh = None
  791. thisComponent.tStopRefresh = None
  792. if hasattr(thisComponent, 'status'):
  793. thisComponent.status = NOT_STARTED
  794. # reset timers
  795. t = 0
  796. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  797. frameN = -1
  798. # --- Run Routine "prepare_1" ---
  799. routineForceEnded = not continueRoutine
  800. while continueRoutine:
  801. # get current time
  802. t = routineTimer.getTime()
  803. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  804. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  805. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  806. # update/draw components on each frame
  807. # *elbow_prepare* updates
  808. # if elbow_prepare is starting this frame...
  809. if elbow_prepare.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  810. # keep track of start time/frame for later
  811. elbow_prepare.frameNStart = frameN # exact frame index
  812. elbow_prepare.tStart = t # local t and not account for scr refresh
  813. elbow_prepare.tStartRefresh = tThisFlipGlobal # on global time
  814. win.timeOnFlip(elbow_prepare, 'tStartRefresh') # time at next scr refresh
  815. # add timestamp to datafile
  816. thisExp.timestampOnFlip(win, 'elbow_prepare.started')
  817. # update status
  818. elbow_prepare.status = STARTED
  819. elbow_prepare.setAutoDraw(True)
  820. # if elbow_prepare is active this frame...
  821. if elbow_prepare.status == STARTED:
  822. # update params
  823. pass
  824. # *key_resp* updates
  825. waitOnFlip = False
  826. # if key_resp is starting this frame...
  827. if key_resp.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  828. # keep track of start time/frame for later
  829. key_resp.frameNStart = frameN # exact frame index
  830. key_resp.tStart = t # local t and not account for scr refresh
  831. key_resp.tStartRefresh = tThisFlipGlobal # on global time
  832. win.timeOnFlip(key_resp, 'tStartRefresh') # time at next scr refresh
  833. # add timestamp to datafile
  834. thisExp.timestampOnFlip(win, 'key_resp.started')
  835. # update status
  836. key_resp.status = STARTED
  837. # keyboard checking is just starting
  838. waitOnFlip = True
  839. win.callOnFlip(key_resp.clock.reset) # t=0 on next screen flip
  840. win.callOnFlip(key_resp.clearEvents, eventType='keyboard') # clear events on next screen flip
  841. if key_resp.status == STARTED and not waitOnFlip:
  842. theseKeys = key_resp.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)
  843. _key_resp_allKeys.extend(theseKeys)
  844. if len(_key_resp_allKeys):
  845. key_resp.keys = _key_resp_allKeys[-1].name # just the last key pressed
  846. key_resp.rt = _key_resp_allKeys[-1].rt
  847. key_resp.duration = _key_resp_allKeys[-1].duration
  848. # a response ends the routine
  849. continueRoutine = False
  850. # if sound_2 is starting this frame...
  851. if sound_2.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  852. # keep track of start time/frame for later
  853. sound_2.frameNStart = frameN # exact frame index
  854. sound_2.tStart = t # local t and not account for scr refresh
  855. sound_2.tStartRefresh = tThisFlipGlobal # on global time
  856. # add timestamp to datafile
  857. thisExp.addData('sound_2.started', tThisFlipGlobal)
  858. # update status
  859. sound_2.status = STARTED
  860. sound_2.play(when=win) # sync with win flip
  861. # update sound_2 status according to whether it's playing
  862. if sound_2.isPlaying:
  863. sound_2.status = STARTED
  864. elif sound_2.isFinished:
  865. sound_2.status = FINISHED
  866. # check for quit (typically the Esc key)
  867. if defaultKeyboard.getKeys(keyList=["escape"]):
  868. thisExp.status = FINISHED
  869. if thisExp.status == FINISHED or endExpNow:
  870. endExperiment(thisExp, inputs=inputs, win=win)
  871. return
  872. # check if all components have finished
  873. if not continueRoutine: # a component has requested a forced-end of Routine
  874. routineForceEnded = True
  875. break
  876. continueRoutine = False # will revert to True if at least one component still running
  877. for thisComponent in prepare_1Components:
  878. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  879. continueRoutine = True
  880. break # at least one component has not yet finished
  881. # refresh the screen
  882. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  883. win.flip()
  884. # --- Ending Routine "prepare_1" ---
  885. for thisComponent in prepare_1Components:
  886. if hasattr(thisComponent, "setAutoDraw"):
  887. thisComponent.setAutoDraw(False)
  888. thisExp.addData('prepare_1.stopped', globalClock.getTime())
  889. # check responses
  890. if key_resp.keys in ['', [], None]: # No response was made
  891. key_resp.keys = None
  892. thisExp.addData('key_resp.keys',key_resp.keys)
  893. if key_resp.keys != None: # we had a response
  894. thisExp.addData('key_resp.rt', key_resp.rt)
  895. thisExp.addData('key_resp.duration', key_resp.duration)
  896. thisExp.nextEntry()
  897. sound_2.pause() # ensure sound has stopped at end of Routine
  898. # the Routine "prepare_1" was not non-slip safe, so reset the non-slip timer
  899. routineTimer.reset()
  900. # set up handler to look after randomisation of conditions etc
  901. trials = data.TrialHandler(nReps=args.trial_num, method='sequential',
  902. extraInfo=expInfo, originPath=-1,
  903. trialList=[None],
  904. seed=None, name='trials')
  905. thisExp.addLoop(trials) # add the loop to the experiment
  906. thisTrial = trials.trialList[0] # so we can initialise stimuli with some values
  907. # abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
  908. if thisTrial != None:
  909. for paramName in thisTrial:
  910. globals()[paramName] = thisTrial[paramName]
  911. for thisTrial in trials:
  912. currentLoop = trials
  913. thisExp.timestampOnFlip(win, 'thisRow.t')
  914. # pause experiment here if requested
  915. if thisExp.status == PAUSED:
  916. pauseExperiment(
  917. thisExp=thisExp,
  918. inputs=inputs,
  919. win=win,
  920. timers=[routineTimer],
  921. playbackComponents=[]
  922. )
  923. # abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
  924. if thisTrial != None:
  925. for paramName in thisTrial:
  926. globals()[paramName] = thisTrial[paramName]
  927. # --- Prepare to start Routine "task_1" ---
  928. continueRoutine = True
  929. # update component parameters for each repeat
  930. thisExp.addData('task_1.started', globalClock.getTime())
  931. # Run 'Begin Routine' code from code_2
  932. i=0
  933. cnt1 = trials.thisN+1
  934. if args.side == "right":
  935. if cnt1 in elbow_opposite:
  936. elbow_img.image = "static/images/daily_elbow_left.png"
  937. trigger_elbow = 0x08
  938. trigger_rest = 0x09
  939. else:
  940. elbow_img.image = "static/images/daily_elbow_right.png"
  941. trigger_elbow = 0x02
  942. trigger_rest = 0x03
  943. else:
  944. if cnt1 in elbow_opposite:
  945. elbow_img.image = "static/images/daily_elbow_right.png"
  946. trigger_elbow = 0x02
  947. trigger_rest = 0x03
  948. else:
  949. elbow_img.image = "static/images/daily_elbow_left.png"
  950. trigger_elbow = 0x08
  951. trigger_rest = 0x09
  952. sound_6.setSound('static/audios/任务2_肘部.wav', hamming=True)
  953. sound_6.setVolume(1.0, log=False)
  954. sound_6.seek(0)
  955. # keep track of which components have finished
  956. task_1Components = [elbow_img, sound_6]
  957. for thisComponent in task_1Components:
  958. thisComponent.tStart = None
  959. thisComponent.tStop = None
  960. thisComponent.tStartRefresh = None
  961. thisComponent.tStopRefresh = None
  962. if hasattr(thisComponent, 'status'):
  963. thisComponent.status = NOT_STARTED
  964. # reset timers
  965. t = 0
  966. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  967. frameN = -1
  968. # --- Run Routine "task_1" ---
  969. routineForceEnded = not continueRoutine
  970. while continueRoutine:
  971. # get current time
  972. t = routineTimer.getTime()
  973. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  974. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  975. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  976. # update/draw components on each frame
  977. # *elbow_img* updates
  978. # if elbow_img is starting this frame...
  979. if elbow_img.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  980. # keep track of start time/frame for later
  981. elbow_img.frameNStart = frameN # exact frame index
  982. elbow_img.tStart = t # local t and not account for scr refresh
  983. elbow_img.tStartRefresh = tThisFlipGlobal # on global time
  984. win.timeOnFlip(elbow_img, 'tStartRefresh') # time at next scr refresh
  985. # add timestamp to datafile
  986. thisExp.timestampOnFlip(win, 'elbow_img.started')
  987. # update status
  988. elbow_img.status = STARTED
  989. elbow_img.setAutoDraw(True)
  990. # if elbow_img is active this frame...
  991. if elbow_img.status == STARTED:
  992. # update params
  993. pass
  994. # if elbow_img is stopping this frame...
  995. if elbow_img.status == STARTED:
  996. # is it time to stop? (based on global clock, using actual start)
  997. if tThisFlipGlobal > elbow_img.tStartRefresh + 5-frameTolerance:
  998. # keep track of stop time/frame for later
  999. elbow_img.tStop = t # not accounting for scr refresh
  1000. elbow_img.frameNStop = frameN # exact frame index
  1001. # add timestamp to datafile
  1002. thisExp.timestampOnFlip(win, 'elbow_img.stopped')
  1003. # update status
  1004. elbow_img.status = FINISHED
  1005. elbow_img.setAutoDraw(False)
  1006. # Run 'Each Frame' code from code_2
  1007. if i== 59:
  1008. win.callOnFlip(trigger.send_trigger,trigger_elbow)
  1009. i+=1
  1010. else:
  1011. i+=1
  1012. # if sound_6 is starting this frame...
  1013. if sound_6.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1014. # keep track of start time/frame for later
  1015. sound_6.frameNStart = frameN # exact frame index
  1016. sound_6.tStart = t # local t and not account for scr refresh
  1017. sound_6.tStartRefresh = tThisFlipGlobal # on global time
  1018. # add timestamp to datafile
  1019. thisExp.addData('sound_6.started', tThisFlipGlobal)
  1020. # update status
  1021. sound_6.status = STARTED
  1022. sound_6.play(when=win) # sync with win flip
  1023. # update sound_6 status according to whether it's playing
  1024. if sound_6.isPlaying:
  1025. sound_6.status = STARTED
  1026. elif sound_6.isFinished:
  1027. sound_6.status = FINISHED
  1028. # check for quit (typically the Esc key)
  1029. if defaultKeyboard.getKeys(keyList=["escape"]):
  1030. thisExp.status = FINISHED
  1031. if thisExp.status == FINISHED or endExpNow:
  1032. endExperiment(thisExp, inputs=inputs, win=win)
  1033. return
  1034. # check if all components have finished
  1035. if not continueRoutine: # a component has requested a forced-end of Routine
  1036. routineForceEnded = True
  1037. break
  1038. continueRoutine = False # will revert to True if at least one component still running
  1039. for thisComponent in task_1Components:
  1040. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1041. continueRoutine = True
  1042. break # at least one component has not yet finished
  1043. # refresh the screen
  1044. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1045. win.flip()
  1046. # --- Ending Routine "task_1" ---
  1047. for thisComponent in task_1Components:
  1048. if hasattr(thisComponent, "setAutoDraw"):
  1049. thisComponent.setAutoDraw(False)
  1050. thisExp.addData('task_1.stopped', globalClock.getTime())
  1051. sound_6.pause() # ensure sound has stopped at end of Routine
  1052. # the Routine "task_1" was not non-slip safe, so reset the non-slip timer
  1053. routineTimer.reset()
  1054. # --- Prepare to start Routine "rest_1" ---
  1055. continueRoutine = True
  1056. # update component parameters for each repeat
  1057. thisExp.addData('rest_1.started', globalClock.getTime())
  1058. # Run 'Begin Routine' code from code_3
  1059. win.callOnFlip(trigger.send_trigger,trigger_rest)
  1060. sound_8.setSound('static/audios/ding.wav', secs=1.5, hamming=True)
  1061. sound_8.setVolume(1.0, log=False)
  1062. sound_8.seek(0)
  1063. sound_9.setSound('static/audios/放松.wav', secs=3, hamming=True)
  1064. sound_9.setVolume(1.0, log=False)
  1065. sound_9.seek(0)
  1066. # keep track of which components have finished
  1067. rest_1Components = [image_2, sound_8, sound_9]
  1068. for thisComponent in rest_1Components:
  1069. thisComponent.tStart = None
  1070. thisComponent.tStop = None
  1071. thisComponent.tStartRefresh = None
  1072. thisComponent.tStopRefresh = None
  1073. if hasattr(thisComponent, 'status'):
  1074. thisComponent.status = NOT_STARTED
  1075. # reset timers
  1076. t = 0
  1077. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1078. frameN = -1
  1079. # --- Run Routine "rest_1" ---
  1080. routineForceEnded = not continueRoutine
  1081. while continueRoutine and routineTimer.getTime() < 5.0:
  1082. # get current time
  1083. t = routineTimer.getTime()
  1084. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1085. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1086. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1087. # update/draw components on each frame
  1088. # *image_2* updates
  1089. # if image_2 is starting this frame...
  1090. if image_2.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
  1091. # keep track of start time/frame for later
  1092. image_2.frameNStart = frameN # exact frame index
  1093. image_2.tStart = t # local t and not account for scr refresh
  1094. image_2.tStartRefresh = tThisFlipGlobal # on global time
  1095. win.timeOnFlip(image_2, 'tStartRefresh') # time at next scr refresh
  1096. # add timestamp to datafile
  1097. thisExp.timestampOnFlip(win, 'image_2.started')
  1098. # update status
  1099. image_2.status = STARTED
  1100. image_2.setAutoDraw(True)
  1101. # if image_2 is active this frame...
  1102. if image_2.status == STARTED:
  1103. # update params
  1104. pass
  1105. # if image_2 is stopping this frame...
  1106. if image_2.status == STARTED:
  1107. # is it time to stop? (based on global clock, using actual start)
  1108. if tThisFlipGlobal > image_2.tStartRefresh + 5-frameTolerance:
  1109. # keep track of stop time/frame for later
  1110. image_2.tStop = t # not accounting for scr refresh
  1111. image_2.frameNStop = frameN # exact frame index
  1112. # add timestamp to datafile
  1113. thisExp.timestampOnFlip(win, 'image_2.stopped')
  1114. # update status
  1115. image_2.status = FINISHED
  1116. image_2.setAutoDraw(False)
  1117. # if sound_8 is starting this frame...
  1118. if sound_8.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
  1119. # keep track of start time/frame for later
  1120. sound_8.frameNStart = frameN # exact frame index
  1121. sound_8.tStart = t # local t and not account for scr refresh
  1122. sound_8.tStartRefresh = tThisFlipGlobal # on global time
  1123. # add timestamp to datafile
  1124. thisExp.addData('sound_8.started', tThisFlipGlobal)
  1125. # update status
  1126. sound_8.status = STARTED
  1127. sound_8.play(when=win) # sync with win flip
  1128. # if sound_8 is stopping this frame...
  1129. if sound_8.status == STARTED:
  1130. # is it time to stop? (based on global clock, using actual start)
  1131. if tThisFlipGlobal > sound_8.tStartRefresh + 1.5-frameTolerance:
  1132. # keep track of stop time/frame for later
  1133. sound_8.tStop = t # not accounting for scr refresh
  1134. sound_8.frameNStop = frameN # exact frame index
  1135. # add timestamp to datafile
  1136. thisExp.timestampOnFlip(win, 'sound_8.stopped')
  1137. # update status
  1138. sound_8.status = FINISHED
  1139. sound_8.stop()
  1140. # update sound_8 status according to whether it's playing
  1141. if sound_8.isPlaying:
  1142. sound_8.status = STARTED
  1143. elif sound_8.isFinished:
  1144. sound_8.status = FINISHED
  1145. # if sound_9 is starting this frame...
  1146. if sound_9.status == NOT_STARTED and tThisFlip >= 1.2-frameTolerance:
  1147. # keep track of start time/frame for later
  1148. sound_9.frameNStart = frameN # exact frame index
  1149. sound_9.tStart = t # local t and not account for scr refresh
  1150. sound_9.tStartRefresh = tThisFlipGlobal # on global time
  1151. # add timestamp to datafile
  1152. thisExp.addData('sound_9.started', tThisFlipGlobal)
  1153. # update status
  1154. sound_9.status = STARTED
  1155. sound_9.play(when=win) # sync with win flip
  1156. # if sound_9 is stopping this frame...
  1157. if sound_9.status == STARTED:
  1158. # is it time to stop? (based on global clock, using actual start)
  1159. if tThisFlipGlobal > sound_9.tStartRefresh + 3-frameTolerance:
  1160. # keep track of stop time/frame for later
  1161. sound_9.tStop = t # not accounting for scr refresh
  1162. sound_9.frameNStop = frameN # exact frame index
  1163. # add timestamp to datafile
  1164. thisExp.timestampOnFlip(win, 'sound_9.stopped')
  1165. # update status
  1166. sound_9.status = FINISHED
  1167. sound_9.stop()
  1168. # update sound_9 status according to whether it's playing
  1169. if sound_9.isPlaying:
  1170. sound_9.status = STARTED
  1171. elif sound_9.isFinished:
  1172. sound_9.status = FINISHED
  1173. # check for quit (typically the Esc key)
  1174. if defaultKeyboard.getKeys(keyList=["escape"]):
  1175. thisExp.status = FINISHED
  1176. if thisExp.status == FINISHED or endExpNow:
  1177. endExperiment(thisExp, inputs=inputs, win=win)
  1178. return
  1179. # check if all components have finished
  1180. if not continueRoutine: # a component has requested a forced-end of Routine
  1181. routineForceEnded = True
  1182. break
  1183. continueRoutine = False # will revert to True if at least one component still running
  1184. for thisComponent in rest_1Components:
  1185. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1186. continueRoutine = True
  1187. break # at least one component has not yet finished
  1188. # refresh the screen
  1189. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1190. win.flip()
  1191. # --- Ending Routine "rest_1" ---
  1192. for thisComponent in rest_1Components:
  1193. if hasattr(thisComponent, "setAutoDraw"):
  1194. thisComponent.setAutoDraw(False)
  1195. thisExp.addData('rest_1.stopped', globalClock.getTime())
  1196. sound_8.pause() # ensure sound has stopped at end of Routine
  1197. sound_9.pause() # ensure sound has stopped at end of Routine
  1198. # using non-slip timing so subtract the expected duration of this Routine (unless ended on request)
  1199. if routineForceEnded:
  1200. routineTimer.reset()
  1201. else:
  1202. routineTimer.addTime(-5.000000)
  1203. thisExp.nextEntry()
  1204. if thisSession is not None:
  1205. # if running in a Session with a Liaison client, send data up to now
  1206. thisSession.sendExperimentData()
  1207. # completed args.trial_num repeats of 'trials'
  1208. # --- Prepare to start Routine "prepare_2" ---
  1209. continueRoutine = True
  1210. # update component parameters for each repeat
  1211. thisExp.addData('prepare_2.started', globalClock.getTime())
  1212. key_resp_2.keys = []
  1213. key_resp_2.rt = []
  1214. _key_resp_2_allKeys = []
  1215. sound_3.setSound('static/audios/准备3_被动.wav', hamming=True)
  1216. sound_3.setVolume(1.0, log=False)
  1217. sound_3.seek(0)
  1218. # Run 'Begin Routine' code from code_8
  1219. prepare_text2 = "下一个部分是:感受手部运动\n\n请您感受手部握拳然后保持的过程\n您需要完成这个动作"+str(args.trial_num)+"次,每次5秒\n\n如果准备好了,请按空格键"
  1220. passive_prepare.text = prepare_text2
  1221. # keep track of which components have finished
  1222. prepare_2Components = [passive_prepare, key_resp_2, sound_3]
  1223. for thisComponent in prepare_2Components:
  1224. thisComponent.tStart = None
  1225. thisComponent.tStop = None
  1226. thisComponent.tStartRefresh = None
  1227. thisComponent.tStopRefresh = None
  1228. if hasattr(thisComponent, 'status'):
  1229. thisComponent.status = NOT_STARTED
  1230. # reset timers
  1231. t = 0
  1232. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1233. frameN = -1
  1234. # --- Run Routine "prepare_2" ---
  1235. routineForceEnded = not continueRoutine
  1236. while continueRoutine:
  1237. # get current time
  1238. t = routineTimer.getTime()
  1239. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1240. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1241. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1242. # update/draw components on each frame
  1243. # *passive_prepare* updates
  1244. # if passive_prepare is starting this frame...
  1245. if passive_prepare.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  1246. # keep track of start time/frame for later
  1247. passive_prepare.frameNStart = frameN # exact frame index
  1248. passive_prepare.tStart = t # local t and not account for scr refresh
  1249. passive_prepare.tStartRefresh = tThisFlipGlobal # on global time
  1250. win.timeOnFlip(passive_prepare, 'tStartRefresh') # time at next scr refresh
  1251. # add timestamp to datafile
  1252. thisExp.timestampOnFlip(win, 'passive_prepare.started')
  1253. # update status
  1254. passive_prepare.status = STARTED
  1255. passive_prepare.setAutoDraw(True)
  1256. # if passive_prepare is active this frame...
  1257. if passive_prepare.status == STARTED:
  1258. # update params
  1259. pass
  1260. # *key_resp_2* updates
  1261. waitOnFlip = False
  1262. # if key_resp_2 is starting this frame...
  1263. if key_resp_2.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  1264. # keep track of start time/frame for later
  1265. key_resp_2.frameNStart = frameN # exact frame index
  1266. key_resp_2.tStart = t # local t and not account for scr refresh
  1267. key_resp_2.tStartRefresh = tThisFlipGlobal # on global time
  1268. win.timeOnFlip(key_resp_2, 'tStartRefresh') # time at next scr refresh
  1269. # add timestamp to datafile
  1270. thisExp.timestampOnFlip(win, 'key_resp_2.started')
  1271. # update status
  1272. key_resp_2.status = STARTED
  1273. # keyboard checking is just starting
  1274. waitOnFlip = True
  1275. win.callOnFlip(key_resp_2.clock.reset) # t=0 on next screen flip
  1276. win.callOnFlip(key_resp_2.clearEvents, eventType='keyboard') # clear events on next screen flip
  1277. if key_resp_2.status == STARTED and not waitOnFlip:
  1278. theseKeys = key_resp_2.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)
  1279. _key_resp_2_allKeys.extend(theseKeys)
  1280. if len(_key_resp_2_allKeys):
  1281. key_resp_2.keys = _key_resp_2_allKeys[-1].name # just the last key pressed
  1282. key_resp_2.rt = _key_resp_2_allKeys[-1].rt
  1283. key_resp_2.duration = _key_resp_2_allKeys[-1].duration
  1284. # a response ends the routine
  1285. continueRoutine = False
  1286. # if sound_3 is starting this frame...
  1287. if sound_3.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1288. # keep track of start time/frame for later
  1289. sound_3.frameNStart = frameN # exact frame index
  1290. sound_3.tStart = t # local t and not account for scr refresh
  1291. sound_3.tStartRefresh = tThisFlipGlobal # on global time
  1292. # add timestamp to datafile
  1293. thisExp.addData('sound_3.started', tThisFlipGlobal)
  1294. # update status
  1295. sound_3.status = STARTED
  1296. sound_3.play(when=win) # sync with win flip
  1297. # update sound_3 status according to whether it's playing
  1298. if sound_3.isPlaying:
  1299. sound_3.status = STARTED
  1300. elif sound_3.isFinished:
  1301. sound_3.status = FINISHED
  1302. # check for quit (typically the Esc key)
  1303. if defaultKeyboard.getKeys(keyList=["escape"]):
  1304. thisExp.status = FINISHED
  1305. if thisExp.status == FINISHED or endExpNow:
  1306. endExperiment(thisExp, inputs=inputs, win=win)
  1307. return
  1308. # check if all components have finished
  1309. if not continueRoutine: # a component has requested a forced-end of Routine
  1310. routineForceEnded = True
  1311. break
  1312. continueRoutine = False # will revert to True if at least one component still running
  1313. for thisComponent in prepare_2Components:
  1314. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1315. continueRoutine = True
  1316. break # at least one component has not yet finished
  1317. # refresh the screen
  1318. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1319. win.flip()
  1320. # --- Ending Routine "prepare_2" ---
  1321. for thisComponent in prepare_2Components:
  1322. if hasattr(thisComponent, "setAutoDraw"):
  1323. thisComponent.setAutoDraw(False)
  1324. thisExp.addData('prepare_2.stopped', globalClock.getTime())
  1325. # check responses
  1326. if key_resp_2.keys in ['', [], None]: # No response was made
  1327. key_resp_2.keys = None
  1328. thisExp.addData('key_resp_2.keys',key_resp_2.keys)
  1329. if key_resp_2.keys != None: # we had a response
  1330. thisExp.addData('key_resp_2.rt', key_resp_2.rt)
  1331. thisExp.addData('key_resp_2.duration', key_resp_2.duration)
  1332. thisExp.nextEntry()
  1333. sound_3.pause() # ensure sound has stopped at end of Routine
  1334. # the Routine "prepare_2" was not non-slip safe, so reset the non-slip timer
  1335. routineTimer.reset()
  1336. # set up handler to look after randomisation of conditions etc
  1337. trials_2 = data.TrialHandler(nReps=args.trial_num, method='random',
  1338. extraInfo=expInfo, originPath=-1,
  1339. trialList=[None],
  1340. seed=None, name='trials_2')
  1341. thisExp.addLoop(trials_2) # add the loop to the experiment
  1342. thisTrial_2 = trials_2.trialList[0] # so we can initialise stimuli with some values
  1343. # abbreviate parameter names if possible (e.g. rgb = thisTrial_2.rgb)
  1344. if thisTrial_2 != None:
  1345. for paramName in thisTrial_2:
  1346. globals()[paramName] = thisTrial_2[paramName]
  1347. for thisTrial_2 in trials_2:
  1348. currentLoop = trials_2
  1349. thisExp.timestampOnFlip(win, 'thisRow.t')
  1350. # pause experiment here if requested
  1351. if thisExp.status == PAUSED:
  1352. pauseExperiment(
  1353. thisExp=thisExp,
  1354. inputs=inputs,
  1355. win=win,
  1356. timers=[routineTimer],
  1357. playbackComponents=[]
  1358. )
  1359. # abbreviate parameter names if possible (e.g. rgb = thisTrial_2.rgb)
  1360. if thisTrial_2 != None:
  1361. for paramName in thisTrial_2:
  1362. globals()[paramName] = thisTrial_2[paramName]
  1363. # --- Prepare to start Routine "task_2" ---
  1364. continueRoutine = True
  1365. # update component parameters for each repeat
  1366. thisExp.addData('task_2.started', globalClock.getTime())
  1367. # Run 'Begin Routine' code from code_4
  1368. i=0
  1369. if args.side == "right":
  1370. passive_img.image = "static/images/daily_passive_right.png"
  1371. else:
  1372. passive_img.image = "static/images/daily_passive_left.png"
  1373. sound_14.setSound('static/audios/任务3_被动.wav', hamming=True)
  1374. sound_14.setVolume(1.0, log=False)
  1375. sound_14.seek(0)
  1376. # keep track of which components have finished
  1377. task_2Components = [passive_img, sound_14]
  1378. for thisComponent in task_2Components:
  1379. thisComponent.tStart = None
  1380. thisComponent.tStop = None
  1381. thisComponent.tStartRefresh = None
  1382. thisComponent.tStopRefresh = None
  1383. if hasattr(thisComponent, 'status'):
  1384. thisComponent.status = NOT_STARTED
  1385. # reset timers
  1386. t = 0
  1387. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1388. frameN = -1
  1389. # --- Run Routine "task_2" ---
  1390. routineForceEnded = not continueRoutine
  1391. while continueRoutine:
  1392. # get current time
  1393. t = routineTimer.getTime()
  1394. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1395. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1396. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1397. # update/draw components on each frame
  1398. # *passive_img* updates
  1399. # if passive_img is starting this frame...
  1400. if passive_img.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1401. # keep track of start time/frame for later
  1402. passive_img.frameNStart = frameN # exact frame index
  1403. passive_img.tStart = t # local t and not account for scr refresh
  1404. passive_img.tStartRefresh = tThisFlipGlobal # on global time
  1405. win.timeOnFlip(passive_img, 'tStartRefresh') # time at next scr refresh
  1406. # add timestamp to datafile
  1407. thisExp.timestampOnFlip(win, 'passive_img.started')
  1408. # update status
  1409. passive_img.status = STARTED
  1410. passive_img.setAutoDraw(True)
  1411. # if passive_img is active this frame...
  1412. if passive_img.status == STARTED:
  1413. # update params
  1414. pass
  1415. # if passive_img is stopping this frame...
  1416. if passive_img.status == STARTED:
  1417. # is it time to stop? (based on global clock, using actual start)
  1418. if tThisFlipGlobal > passive_img.tStartRefresh + 5-frameTolerance:
  1419. # keep track of stop time/frame for later
  1420. passive_img.tStop = t # not accounting for scr refresh
  1421. passive_img.frameNStop = frameN # exact frame index
  1422. # add timestamp to datafile
  1423. thisExp.timestampOnFlip(win, 'passive_img.stopped')
  1424. # update status
  1425. passive_img.status = FINISHED
  1426. passive_img.setAutoDraw(False)
  1427. # Run 'Each Frame' code from code_4
  1428. if i== 59:
  1429. win.callOnFlip(trigger.send_trigger,0x04)
  1430. i+=1
  1431. else:
  1432. i+=1
  1433. # if sound_14 is starting this frame...
  1434. if sound_14.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1435. # keep track of start time/frame for later
  1436. sound_14.frameNStart = frameN # exact frame index
  1437. sound_14.tStart = t # local t and not account for scr refresh
  1438. sound_14.tStartRefresh = tThisFlipGlobal # on global time
  1439. # add timestamp to datafile
  1440. thisExp.addData('sound_14.started', tThisFlipGlobal)
  1441. # update status
  1442. sound_14.status = STARTED
  1443. sound_14.play(when=win) # sync with win flip
  1444. # update sound_14 status according to whether it's playing
  1445. if sound_14.isPlaying:
  1446. sound_14.status = STARTED
  1447. elif sound_14.isFinished:
  1448. sound_14.status = FINISHED
  1449. # check for quit (typically the Esc key)
  1450. if defaultKeyboard.getKeys(keyList=["escape"]):
  1451. thisExp.status = FINISHED
  1452. if thisExp.status == FINISHED or endExpNow:
  1453. endExperiment(thisExp, inputs=inputs, win=win)
  1454. return
  1455. # check if all components have finished
  1456. if not continueRoutine: # a component has requested a forced-end of Routine
  1457. routineForceEnded = True
  1458. break
  1459. continueRoutine = False # will revert to True if at least one component still running
  1460. for thisComponent in task_2Components:
  1461. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1462. continueRoutine = True
  1463. break # at least one component has not yet finished
  1464. # refresh the screen
  1465. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1466. win.flip()
  1467. # --- Ending Routine "task_2" ---
  1468. for thisComponent in task_2Components:
  1469. if hasattr(thisComponent, "setAutoDraw"):
  1470. thisComponent.setAutoDraw(False)
  1471. thisExp.addData('task_2.stopped', globalClock.getTime())
  1472. sound_14.pause() # ensure sound has stopped at end of Routine
  1473. # the Routine "task_2" was not non-slip safe, so reset the non-slip timer
  1474. routineTimer.reset()
  1475. # --- Prepare to start Routine "rest_2" ---
  1476. continueRoutine = True
  1477. # update component parameters for each repeat
  1478. thisExp.addData('rest_2.started', globalClock.getTime())
  1479. # Run 'Begin Routine' code from code_5
  1480. win.callOnFlip(trigger.send_trigger,0x05)
  1481. sound_10.setSound('static/audios/ding.wav', secs=1.5, hamming=True)
  1482. sound_10.setVolume(1.0, log=False)
  1483. sound_10.seek(0)
  1484. sound_11.setSound('static/audios/放松.wav', secs=3, hamming=True)
  1485. sound_11.setVolume(1.0, log=False)
  1486. sound_11.seek(0)
  1487. # keep track of which components have finished
  1488. rest_2Components = [image_6, sound_10, sound_11]
  1489. for thisComponent in rest_2Components:
  1490. thisComponent.tStart = None
  1491. thisComponent.tStop = None
  1492. thisComponent.tStartRefresh = None
  1493. thisComponent.tStopRefresh = None
  1494. if hasattr(thisComponent, 'status'):
  1495. thisComponent.status = NOT_STARTED
  1496. # reset timers
  1497. t = 0
  1498. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1499. frameN = -1
  1500. # --- Run Routine "rest_2" ---
  1501. routineForceEnded = not continueRoutine
  1502. while continueRoutine and routineTimer.getTime() < 5.0:
  1503. # get current time
  1504. t = routineTimer.getTime()
  1505. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1506. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1507. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1508. # update/draw components on each frame
  1509. # *image_6* updates
  1510. # if image_6 is starting this frame...
  1511. if image_6.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
  1512. # keep track of start time/frame for later
  1513. image_6.frameNStart = frameN # exact frame index
  1514. image_6.tStart = t # local t and not account for scr refresh
  1515. image_6.tStartRefresh = tThisFlipGlobal # on global time
  1516. win.timeOnFlip(image_6, 'tStartRefresh') # time at next scr refresh
  1517. # add timestamp to datafile
  1518. thisExp.timestampOnFlip(win, 'image_6.started')
  1519. # update status
  1520. image_6.status = STARTED
  1521. image_6.setAutoDraw(True)
  1522. # if image_6 is active this frame...
  1523. if image_6.status == STARTED:
  1524. # update params
  1525. pass
  1526. # if image_6 is stopping this frame...
  1527. if image_6.status == STARTED:
  1528. # is it time to stop? (based on global clock, using actual start)
  1529. if tThisFlipGlobal > image_6.tStartRefresh + 5-frameTolerance:
  1530. # keep track of stop time/frame for later
  1531. image_6.tStop = t # not accounting for scr refresh
  1532. image_6.frameNStop = frameN # exact frame index
  1533. # add timestamp to datafile
  1534. thisExp.timestampOnFlip(win, 'image_6.stopped')
  1535. # update status
  1536. image_6.status = FINISHED
  1537. image_6.setAutoDraw(False)
  1538. # Run 'Each Frame' code from code_5
  1539. # if sound_10 is starting this frame...
  1540. if sound_10.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
  1541. # keep track of start time/frame for later
  1542. sound_10.frameNStart = frameN # exact frame index
  1543. sound_10.tStart = t # local t and not account for scr refresh
  1544. sound_10.tStartRefresh = tThisFlipGlobal # on global time
  1545. # add timestamp to datafile
  1546. thisExp.addData('sound_10.started', tThisFlipGlobal)
  1547. # update status
  1548. sound_10.status = STARTED
  1549. sound_10.play(when=win) # sync with win flip
  1550. # if sound_10 is stopping this frame...
  1551. if sound_10.status == STARTED:
  1552. # is it time to stop? (based on global clock, using actual start)
  1553. if tThisFlipGlobal > sound_10.tStartRefresh + 1.5-frameTolerance:
  1554. # keep track of stop time/frame for later
  1555. sound_10.tStop = t # not accounting for scr refresh
  1556. sound_10.frameNStop = frameN # exact frame index
  1557. # add timestamp to datafile
  1558. thisExp.timestampOnFlip(win, 'sound_10.stopped')
  1559. # update status
  1560. sound_10.status = FINISHED
  1561. sound_10.stop()
  1562. # update sound_10 status according to whether it's playing
  1563. if sound_10.isPlaying:
  1564. sound_10.status = STARTED
  1565. elif sound_10.isFinished:
  1566. sound_10.status = FINISHED
  1567. # if sound_11 is starting this frame...
  1568. if sound_11.status == NOT_STARTED and tThisFlip >= 1.2-frameTolerance:
  1569. # keep track of start time/frame for later
  1570. sound_11.frameNStart = frameN # exact frame index
  1571. sound_11.tStart = t # local t and not account for scr refresh
  1572. sound_11.tStartRefresh = tThisFlipGlobal # on global time
  1573. # add timestamp to datafile
  1574. thisExp.addData('sound_11.started', tThisFlipGlobal)
  1575. # update status
  1576. sound_11.status = STARTED
  1577. sound_11.play(when=win) # sync with win flip
  1578. # if sound_11 is stopping this frame...
  1579. if sound_11.status == STARTED:
  1580. # is it time to stop? (based on global clock, using actual start)
  1581. if tThisFlipGlobal > sound_11.tStartRefresh + 3-frameTolerance:
  1582. # keep track of stop time/frame for later
  1583. sound_11.tStop = t # not accounting for scr refresh
  1584. sound_11.frameNStop = frameN # exact frame index
  1585. # add timestamp to datafile
  1586. thisExp.timestampOnFlip(win, 'sound_11.stopped')
  1587. # update status
  1588. sound_11.status = FINISHED
  1589. sound_11.stop()
  1590. # update sound_11 status according to whether it's playing
  1591. if sound_11.isPlaying:
  1592. sound_11.status = STARTED
  1593. elif sound_11.isFinished:
  1594. sound_11.status = FINISHED
  1595. # check for quit (typically the Esc key)
  1596. if defaultKeyboard.getKeys(keyList=["escape"]):
  1597. thisExp.status = FINISHED
  1598. if thisExp.status == FINISHED or endExpNow:
  1599. endExperiment(thisExp, inputs=inputs, win=win)
  1600. return
  1601. # check if all components have finished
  1602. if not continueRoutine: # a component has requested a forced-end of Routine
  1603. routineForceEnded = True
  1604. break
  1605. continueRoutine = False # will revert to True if at least one component still running
  1606. for thisComponent in rest_2Components:
  1607. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1608. continueRoutine = True
  1609. break # at least one component has not yet finished
  1610. # refresh the screen
  1611. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1612. win.flip()
  1613. # --- Ending Routine "rest_2" ---
  1614. for thisComponent in rest_2Components:
  1615. if hasattr(thisComponent, "setAutoDraw"):
  1616. thisComponent.setAutoDraw(False)
  1617. thisExp.addData('rest_2.stopped', globalClock.getTime())
  1618. sound_10.pause() # ensure sound has stopped at end of Routine
  1619. sound_11.pause() # ensure sound has stopped at end of Routine
  1620. # using non-slip timing so subtract the expected duration of this Routine (unless ended on request)
  1621. if routineForceEnded:
  1622. routineTimer.reset()
  1623. else:
  1624. routineTimer.addTime(-5.000000)
  1625. thisExp.nextEntry()
  1626. if thisSession is not None:
  1627. # if running in a Session with a Liaison client, send data up to now
  1628. thisSession.sendExperimentData()
  1629. # completed args.trial_num repeats of 'trials_2'
  1630. # --- Prepare to start Routine "prepare_3" ---
  1631. continueRoutine = True
  1632. # update component parameters for each repeat
  1633. thisExp.addData('prepare_3.started', globalClock.getTime())
  1634. key_resp_3.keys = []
  1635. key_resp_3.rt = []
  1636. _key_resp_3_allKeys = []
  1637. sound_4.setSound('static/audios/准备4_主动.wav', hamming=True)
  1638. sound_4.setVolume(1.0, log=False)
  1639. sound_4.seek(0)
  1640. # Run 'Begin Routine' code from code_9
  1641. prepare_text3 = "最后一个部分:尝试握拳运动\n\n请您根据屏幕提示\n尝试握拳,然后尽量保持握拳的姿势\n您需要完成这个动作"+str(args.trial_num)+"次,每次5秒\n\n如果准备好了,请按空格键"
  1642. active_prepare.text = prepare_text3
  1643. # keep track of which components have finished
  1644. prepare_3Components = [active_prepare, key_resp_3, sound_4]
  1645. for thisComponent in prepare_3Components:
  1646. thisComponent.tStart = None
  1647. thisComponent.tStop = None
  1648. thisComponent.tStartRefresh = None
  1649. thisComponent.tStopRefresh = None
  1650. if hasattr(thisComponent, 'status'):
  1651. thisComponent.status = NOT_STARTED
  1652. # reset timers
  1653. t = 0
  1654. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1655. frameN = -1
  1656. # --- Run Routine "prepare_3" ---
  1657. routineForceEnded = not continueRoutine
  1658. while continueRoutine:
  1659. # get current time
  1660. t = routineTimer.getTime()
  1661. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1662. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1663. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1664. # update/draw components on each frame
  1665. # *active_prepare* updates
  1666. # if active_prepare is starting this frame...
  1667. if active_prepare.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  1668. # keep track of start time/frame for later
  1669. active_prepare.frameNStart = frameN # exact frame index
  1670. active_prepare.tStart = t # local t and not account for scr refresh
  1671. active_prepare.tStartRefresh = tThisFlipGlobal # on global time
  1672. win.timeOnFlip(active_prepare, 'tStartRefresh') # time at next scr refresh
  1673. # add timestamp to datafile
  1674. thisExp.timestampOnFlip(win, 'active_prepare.started')
  1675. # update status
  1676. active_prepare.status = STARTED
  1677. active_prepare.setAutoDraw(True)
  1678. # if active_prepare is active this frame...
  1679. if active_prepare.status == STARTED:
  1680. # update params
  1681. pass
  1682. # *key_resp_3* updates
  1683. waitOnFlip = False
  1684. # if key_resp_3 is starting this frame...
  1685. if key_resp_3.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  1686. # keep track of start time/frame for later
  1687. key_resp_3.frameNStart = frameN # exact frame index
  1688. key_resp_3.tStart = t # local t and not account for scr refresh
  1689. key_resp_3.tStartRefresh = tThisFlipGlobal # on global time
  1690. win.timeOnFlip(key_resp_3, 'tStartRefresh') # time at next scr refresh
  1691. # add timestamp to datafile
  1692. thisExp.timestampOnFlip(win, 'key_resp_3.started')
  1693. # update status
  1694. key_resp_3.status = STARTED
  1695. # keyboard checking is just starting
  1696. waitOnFlip = True
  1697. win.callOnFlip(key_resp_3.clock.reset) # t=0 on next screen flip
  1698. win.callOnFlip(key_resp_3.clearEvents, eventType='keyboard') # clear events on next screen flip
  1699. if key_resp_3.status == STARTED and not waitOnFlip:
  1700. theseKeys = key_resp_3.getKeys(keyList=['space'], ignoreKeys=["escape"], waitRelease=False)
  1701. _key_resp_3_allKeys.extend(theseKeys)
  1702. if len(_key_resp_3_allKeys):
  1703. key_resp_3.keys = _key_resp_3_allKeys[-1].name # just the last key pressed
  1704. key_resp_3.rt = _key_resp_3_allKeys[-1].rt
  1705. key_resp_3.duration = _key_resp_3_allKeys[-1].duration
  1706. # a response ends the routine
  1707. continueRoutine = False
  1708. # if sound_4 is starting this frame...
  1709. if sound_4.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1710. # keep track of start time/frame for later
  1711. sound_4.frameNStart = frameN # exact frame index
  1712. sound_4.tStart = t # local t and not account for scr refresh
  1713. sound_4.tStartRefresh = tThisFlipGlobal # on global time
  1714. # add timestamp to datafile
  1715. thisExp.addData('sound_4.started', tThisFlipGlobal)
  1716. # update status
  1717. sound_4.status = STARTED
  1718. sound_4.play(when=win) # sync with win flip
  1719. # update sound_4 status according to whether it's playing
  1720. if sound_4.isPlaying:
  1721. sound_4.status = STARTED
  1722. elif sound_4.isFinished:
  1723. sound_4.status = FINISHED
  1724. # check for quit (typically the Esc key)
  1725. if defaultKeyboard.getKeys(keyList=["escape"]):
  1726. thisExp.status = FINISHED
  1727. if thisExp.status == FINISHED or endExpNow:
  1728. endExperiment(thisExp, inputs=inputs, win=win)
  1729. return
  1730. # check if all components have finished
  1731. if not continueRoutine: # a component has requested a forced-end of Routine
  1732. routineForceEnded = True
  1733. break
  1734. continueRoutine = False # will revert to True if at least one component still running
  1735. for thisComponent in prepare_3Components:
  1736. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1737. continueRoutine = True
  1738. break # at least one component has not yet finished
  1739. # refresh the screen
  1740. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1741. win.flip()
  1742. # --- Ending Routine "prepare_3" ---
  1743. for thisComponent in prepare_3Components:
  1744. if hasattr(thisComponent, "setAutoDraw"):
  1745. thisComponent.setAutoDraw(False)
  1746. thisExp.addData('prepare_3.stopped', globalClock.getTime())
  1747. # check responses
  1748. if key_resp_3.keys in ['', [], None]: # No response was made
  1749. key_resp_3.keys = None
  1750. thisExp.addData('key_resp_3.keys',key_resp_3.keys)
  1751. if key_resp_3.keys != None: # we had a response
  1752. thisExp.addData('key_resp_3.rt', key_resp_3.rt)
  1753. thisExp.addData('key_resp_3.duration', key_resp_3.duration)
  1754. thisExp.nextEntry()
  1755. sound_4.pause() # ensure sound has stopped at end of Routine
  1756. # the Routine "prepare_3" was not non-slip safe, so reset the non-slip timer
  1757. routineTimer.reset()
  1758. # set up handler to look after randomisation of conditions etc
  1759. trials_3 = data.TrialHandler(nReps=args.trial_num, method='sequential',
  1760. extraInfo=expInfo, originPath=-1,
  1761. trialList=[None],
  1762. seed=None, name='trials_3')
  1763. thisExp.addLoop(trials_3) # add the loop to the experiment
  1764. thisTrial_3 = trials_3.trialList[0] # so we can initialise stimuli with some values
  1765. # abbreviate parameter names if possible (e.g. rgb = thisTrial_3.rgb)
  1766. if thisTrial_3 != None:
  1767. for paramName in thisTrial_3:
  1768. globals()[paramName] = thisTrial_3[paramName]
  1769. for thisTrial_3 in trials_3:
  1770. currentLoop = trials_3
  1771. thisExp.timestampOnFlip(win, 'thisRow.t')
  1772. # pause experiment here if requested
  1773. if thisExp.status == PAUSED:
  1774. pauseExperiment(
  1775. thisExp=thisExp,
  1776. inputs=inputs,
  1777. win=win,
  1778. timers=[routineTimer],
  1779. playbackComponents=[]
  1780. )
  1781. # abbreviate parameter names if possible (e.g. rgb = thisTrial_3.rgb)
  1782. if thisTrial_3 != None:
  1783. for paramName in thisTrial_3:
  1784. globals()[paramName] = thisTrial_3[paramName]
  1785. # --- Prepare to start Routine "task_3" ---
  1786. continueRoutine = True
  1787. # update component parameters for each repeat
  1788. thisExp.addData('task_3.started', globalClock.getTime())
  1789. # Run 'Begin Routine' code from code_6
  1790. i=0
  1791. cnt2 = trials_3.thisN+1
  1792. if args.side == "right":
  1793. if cnt2 in hand_opposite:
  1794. active_img.image = "static/images/daily_active_left.png"
  1795. trigger_active = 0x0A
  1796. trigger_rest = 0x0B
  1797. else:
  1798. active_img.image = "static/images/daily_active_right.png"
  1799. trigger_active = 0x06
  1800. trigger_rest = 0x07
  1801. else:
  1802. if cnt2 in hand_opposite:
  1803. active_img.image = "static/images/daily_active_right.png"
  1804. trigger_active = 0x06
  1805. trigger_rest = 0x07
  1806. else:
  1807. active_img.image = "static/images/daily_active_left.png"
  1808. trigger_active = 0x0A
  1809. trigger_rest = 0x0B
  1810. sound_15.setSound('static/audios/任务4_主动.wav', hamming=True)
  1811. sound_15.setVolume(1.0, log=False)
  1812. sound_15.seek(0)
  1813. # keep track of which components have finished
  1814. task_3Components = [active_img, sound_15]
  1815. for thisComponent in task_3Components:
  1816. thisComponent.tStart = None
  1817. thisComponent.tStop = None
  1818. thisComponent.tStartRefresh = None
  1819. thisComponent.tStopRefresh = None
  1820. if hasattr(thisComponent, 'status'):
  1821. thisComponent.status = NOT_STARTED
  1822. # reset timers
  1823. t = 0
  1824. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1825. frameN = -1
  1826. # --- Run Routine "task_3" ---
  1827. routineForceEnded = not continueRoutine
  1828. while continueRoutine:
  1829. # get current time
  1830. t = routineTimer.getTime()
  1831. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1832. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1833. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1834. # update/draw components on each frame
  1835. # *active_img* updates
  1836. # if active_img is starting this frame...
  1837. if active_img.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1838. # keep track of start time/frame for later
  1839. active_img.frameNStart = frameN # exact frame index
  1840. active_img.tStart = t # local t and not account for scr refresh
  1841. active_img.tStartRefresh = tThisFlipGlobal # on global time
  1842. win.timeOnFlip(active_img, 'tStartRefresh') # time at next scr refresh
  1843. # add timestamp to datafile
  1844. thisExp.timestampOnFlip(win, 'active_img.started')
  1845. # update status
  1846. active_img.status = STARTED
  1847. active_img.setAutoDraw(True)
  1848. # if active_img is active this frame...
  1849. if active_img.status == STARTED:
  1850. # update params
  1851. pass
  1852. # if active_img is stopping this frame...
  1853. if active_img.status == STARTED:
  1854. # is it time to stop? (based on global clock, using actual start)
  1855. if tThisFlipGlobal > active_img.tStartRefresh + 5-frameTolerance:
  1856. # keep track of stop time/frame for later
  1857. active_img.tStop = t # not accounting for scr refresh
  1858. active_img.frameNStop = frameN # exact frame index
  1859. # add timestamp to datafile
  1860. thisExp.timestampOnFlip(win, 'active_img.stopped')
  1861. # update status
  1862. active_img.status = FINISHED
  1863. active_img.setAutoDraw(False)
  1864. # Run 'Each Frame' code from code_6
  1865. if i== 59:
  1866. win.callOnFlip(trigger.send_trigger,trigger_active)
  1867. i+=1
  1868. else:
  1869. i+=1
  1870. # if sound_15 is starting this frame...
  1871. if sound_15.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  1872. # keep track of start time/frame for later
  1873. sound_15.frameNStart = frameN # exact frame index
  1874. sound_15.tStart = t # local t and not account for scr refresh
  1875. sound_15.tStartRefresh = tThisFlipGlobal # on global time
  1876. # add timestamp to datafile
  1877. thisExp.addData('sound_15.started', tThisFlipGlobal)
  1878. # update status
  1879. sound_15.status = STARTED
  1880. sound_15.play(when=win) # sync with win flip
  1881. # update sound_15 status according to whether it's playing
  1882. if sound_15.isPlaying:
  1883. sound_15.status = STARTED
  1884. elif sound_15.isFinished:
  1885. sound_15.status = FINISHED
  1886. # check for quit (typically the Esc key)
  1887. if defaultKeyboard.getKeys(keyList=["escape"]):
  1888. thisExp.status = FINISHED
  1889. if thisExp.status == FINISHED or endExpNow:
  1890. endExperiment(thisExp, inputs=inputs, win=win)
  1891. return
  1892. # check if all components have finished
  1893. if not continueRoutine: # a component has requested a forced-end of Routine
  1894. routineForceEnded = True
  1895. break
  1896. continueRoutine = False # will revert to True if at least one component still running
  1897. for thisComponent in task_3Components:
  1898. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  1899. continueRoutine = True
  1900. break # at least one component has not yet finished
  1901. # refresh the screen
  1902. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  1903. win.flip()
  1904. # --- Ending Routine "task_3" ---
  1905. for thisComponent in task_3Components:
  1906. if hasattr(thisComponent, "setAutoDraw"):
  1907. thisComponent.setAutoDraw(False)
  1908. thisExp.addData('task_3.stopped', globalClock.getTime())
  1909. sound_15.pause() # ensure sound has stopped at end of Routine
  1910. # the Routine "task_3" was not non-slip safe, so reset the non-slip timer
  1911. routineTimer.reset()
  1912. # --- Prepare to start Routine "rest_3" ---
  1913. continueRoutine = True
  1914. # update component parameters for each repeat
  1915. thisExp.addData('rest_3.started', globalClock.getTime())
  1916. # Run 'Begin Routine' code from code_7
  1917. win.callOnFlip(trigger.send_trigger,trigger_rest)
  1918. sound_12.setSound('static/audios/ding.wav', secs=1.5, hamming=True)
  1919. sound_12.setVolume(1.0, log=False)
  1920. sound_12.seek(0)
  1921. sound_13.setSound('static/audios/放松.wav', secs=3, hamming=True)
  1922. sound_13.setVolume(1.0, log=False)
  1923. sound_13.seek(0)
  1924. # keep track of which components have finished
  1925. rest_3Components = [image_7, sound_12, sound_13]
  1926. for thisComponent in rest_3Components:
  1927. thisComponent.tStart = None
  1928. thisComponent.tStop = None
  1929. thisComponent.tStartRefresh = None
  1930. thisComponent.tStopRefresh = None
  1931. if hasattr(thisComponent, 'status'):
  1932. thisComponent.status = NOT_STARTED
  1933. # reset timers
  1934. t = 0
  1935. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  1936. frameN = -1
  1937. # --- Run Routine "rest_3" ---
  1938. routineForceEnded = not continueRoutine
  1939. while continueRoutine and routineTimer.getTime() < 5.0:
  1940. # get current time
  1941. t = routineTimer.getTime()
  1942. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  1943. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  1944. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  1945. # update/draw components on each frame
  1946. # *image_7* updates
  1947. # if image_7 is starting this frame...
  1948. if image_7.status == NOT_STARTED and tThisFlip >= 0-frameTolerance:
  1949. # keep track of start time/frame for later
  1950. image_7.frameNStart = frameN # exact frame index
  1951. image_7.tStart = t # local t and not account for scr refresh
  1952. image_7.tStartRefresh = tThisFlipGlobal # on global time
  1953. win.timeOnFlip(image_7, 'tStartRefresh') # time at next scr refresh
  1954. # add timestamp to datafile
  1955. thisExp.timestampOnFlip(win, 'image_7.started')
  1956. # update status
  1957. image_7.status = STARTED
  1958. image_7.setAutoDraw(True)
  1959. # if image_7 is active this frame...
  1960. if image_7.status == STARTED:
  1961. # update params
  1962. pass
  1963. # if image_7 is stopping this frame...
  1964. if image_7.status == STARTED:
  1965. # is it time to stop? (based on global clock, using actual start)
  1966. if tThisFlipGlobal > image_7.tStartRefresh + 5-frameTolerance:
  1967. # keep track of stop time/frame for later
  1968. image_7.tStop = t # not accounting for scr refresh
  1969. image_7.frameNStop = frameN # exact frame index
  1970. # add timestamp to datafile
  1971. thisExp.timestampOnFlip(win, 'image_7.stopped')
  1972. # update status
  1973. image_7.status = FINISHED
  1974. image_7.setAutoDraw(False)
  1975. # if sound_12 is starting this frame...
  1976. if sound_12.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
  1977. # keep track of start time/frame for later
  1978. sound_12.frameNStart = frameN # exact frame index
  1979. sound_12.tStart = t # local t and not account for scr refresh
  1980. sound_12.tStartRefresh = tThisFlipGlobal # on global time
  1981. # add timestamp to datafile
  1982. thisExp.addData('sound_12.started', tThisFlipGlobal)
  1983. # update status
  1984. sound_12.status = STARTED
  1985. sound_12.play(when=win) # sync with win flip
  1986. # if sound_12 is stopping this frame...
  1987. if sound_12.status == STARTED:
  1988. # is it time to stop? (based on global clock, using actual start)
  1989. if tThisFlipGlobal > sound_12.tStartRefresh + 1.5-frameTolerance:
  1990. # keep track of stop time/frame for later
  1991. sound_12.tStop = t # not accounting for scr refresh
  1992. sound_12.frameNStop = frameN # exact frame index
  1993. # add timestamp to datafile
  1994. thisExp.timestampOnFlip(win, 'sound_12.stopped')
  1995. # update status
  1996. sound_12.status = FINISHED
  1997. sound_12.stop()
  1998. # update sound_12 status according to whether it's playing
  1999. if sound_12.isPlaying:
  2000. sound_12.status = STARTED
  2001. elif sound_12.isFinished:
  2002. sound_12.status = FINISHED
  2003. # if sound_13 is starting this frame...
  2004. if sound_13.status == NOT_STARTED and tThisFlip >= 1.2-frameTolerance:
  2005. # keep track of start time/frame for later
  2006. sound_13.frameNStart = frameN # exact frame index
  2007. sound_13.tStart = t # local t and not account for scr refresh
  2008. sound_13.tStartRefresh = tThisFlipGlobal # on global time
  2009. # add timestamp to datafile
  2010. thisExp.addData('sound_13.started', tThisFlipGlobal)
  2011. # update status
  2012. sound_13.status = STARTED
  2013. sound_13.play(when=win) # sync with win flip
  2014. # if sound_13 is stopping this frame...
  2015. if sound_13.status == STARTED:
  2016. # is it time to stop? (based on global clock, using actual start)
  2017. if tThisFlipGlobal > sound_13.tStartRefresh + 3-frameTolerance:
  2018. # keep track of stop time/frame for later
  2019. sound_13.tStop = t # not accounting for scr refresh
  2020. sound_13.frameNStop = frameN # exact frame index
  2021. # add timestamp to datafile
  2022. thisExp.timestampOnFlip(win, 'sound_13.stopped')
  2023. # update status
  2024. sound_13.status = FINISHED
  2025. sound_13.stop()
  2026. # update sound_13 status according to whether it's playing
  2027. if sound_13.isPlaying:
  2028. sound_13.status = STARTED
  2029. elif sound_13.isFinished:
  2030. sound_13.status = FINISHED
  2031. # check for quit (typically the Esc key)
  2032. if defaultKeyboard.getKeys(keyList=["escape"]):
  2033. thisExp.status = FINISHED
  2034. if thisExp.status == FINISHED or endExpNow:
  2035. endExperiment(thisExp, inputs=inputs, win=win)
  2036. return
  2037. # check if all components have finished
  2038. if not continueRoutine: # a component has requested a forced-end of Routine
  2039. routineForceEnded = True
  2040. break
  2041. continueRoutine = False # will revert to True if at least one component still running
  2042. for thisComponent in rest_3Components:
  2043. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  2044. continueRoutine = True
  2045. break # at least one component has not yet finished
  2046. # refresh the screen
  2047. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  2048. win.flip()
  2049. # --- Ending Routine "rest_3" ---
  2050. for thisComponent in rest_3Components:
  2051. if hasattr(thisComponent, "setAutoDraw"):
  2052. thisComponent.setAutoDraw(False)
  2053. thisExp.addData('rest_3.stopped', globalClock.getTime())
  2054. sound_12.pause() # ensure sound has stopped at end of Routine
  2055. sound_13.pause() # ensure sound has stopped at end of Routine
  2056. # using non-slip timing so subtract the expected duration of this Routine (unless ended on request)
  2057. if routineForceEnded:
  2058. routineTimer.reset()
  2059. else:
  2060. routineTimer.addTime(-5.000000)
  2061. thisExp.nextEntry()
  2062. if thisSession is not None:
  2063. # if running in a Session with a Liaison client, send data up to now
  2064. thisSession.sendExperimentData()
  2065. # completed args.trial_num repeats of 'trials_3'
  2066. # --- Prepare to start Routine "done" ---
  2067. continueRoutine = True
  2068. # update component parameters for each repeat
  2069. thisExp.addData('done.started', globalClock.getTime())
  2070. sound_5.setSound('static/audios/结束.wav', hamming=True)
  2071. sound_5.setVolume(1.0, log=False)
  2072. sound_5.seek(0)
  2073. # keep track of which components have finished
  2074. doneComponents = [end, sound_5]
  2075. for thisComponent in doneComponents:
  2076. thisComponent.tStart = None
  2077. thisComponent.tStop = None
  2078. thisComponent.tStartRefresh = None
  2079. thisComponent.tStopRefresh = None
  2080. if hasattr(thisComponent, 'status'):
  2081. thisComponent.status = NOT_STARTED
  2082. # reset timers
  2083. t = 0
  2084. _timeToFirstFrame = win.getFutureFlipTime(clock="now")
  2085. frameN = -1
  2086. # --- Run Routine "done" ---
  2087. routineForceEnded = not continueRoutine
  2088. while continueRoutine:
  2089. # get current time
  2090. t = routineTimer.getTime()
  2091. tThisFlip = win.getFutureFlipTime(clock=routineTimer)
  2092. tThisFlipGlobal = win.getFutureFlipTime(clock=None)
  2093. frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
  2094. # update/draw components on each frame
  2095. # *end* updates
  2096. # if end is starting this frame...
  2097. if end.status == NOT_STARTED and tThisFlip >= 0.5-frameTolerance:
  2098. # keep track of start time/frame for later
  2099. end.frameNStart = frameN # exact frame index
  2100. end.tStart = t # local t and not account for scr refresh
  2101. end.tStartRefresh = tThisFlipGlobal # on global time
  2102. win.timeOnFlip(end, 'tStartRefresh') # time at next scr refresh
  2103. # add timestamp to datafile
  2104. thisExp.timestampOnFlip(win, 'end.started')
  2105. # update status
  2106. end.status = STARTED
  2107. end.setAutoDraw(True)
  2108. # if end is active this frame...
  2109. if end.status == STARTED:
  2110. # update params
  2111. pass
  2112. # if end is stopping this frame...
  2113. if end.status == STARTED:
  2114. # is it time to stop? (based on global clock, using actual start)
  2115. if tThisFlipGlobal > end.tStartRefresh + 8-frameTolerance:
  2116. # keep track of stop time/frame for later
  2117. end.tStop = t # not accounting for scr refresh
  2118. end.frameNStop = frameN # exact frame index
  2119. # add timestamp to datafile
  2120. thisExp.timestampOnFlip(win, 'end.stopped')
  2121. # update status
  2122. end.status = FINISHED
  2123. end.setAutoDraw(False)
  2124. # if sound_5 is starting this frame...
  2125. if sound_5.status == NOT_STARTED and tThisFlip >= 1-frameTolerance:
  2126. # keep track of start time/frame for later
  2127. sound_5.frameNStart = frameN # exact frame index
  2128. sound_5.tStart = t # local t and not account for scr refresh
  2129. sound_5.tStartRefresh = tThisFlipGlobal # on global time
  2130. # add timestamp to datafile
  2131. thisExp.addData('sound_5.started', tThisFlipGlobal)
  2132. # update status
  2133. sound_5.status = STARTED
  2134. sound_5.play(when=win) # sync with win flip
  2135. # update sound_5 status according to whether it's playing
  2136. if sound_5.isPlaying:
  2137. sound_5.status = STARTED
  2138. elif sound_5.isFinished:
  2139. sound_5.status = FINISHED
  2140. # check for quit (typically the Esc key)
  2141. if defaultKeyboard.getKeys(keyList=["escape"]):
  2142. thisExp.status = FINISHED
  2143. if thisExp.status == FINISHED or endExpNow:
  2144. endExperiment(thisExp, inputs=inputs, win=win)
  2145. return
  2146. # check if all components have finished
  2147. if not continueRoutine: # a component has requested a forced-end of Routine
  2148. routineForceEnded = True
  2149. break
  2150. continueRoutine = False # will revert to True if at least one component still running
  2151. for thisComponent in doneComponents:
  2152. if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
  2153. continueRoutine = True
  2154. break # at least one component has not yet finished
  2155. # refresh the screen
  2156. if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
  2157. win.flip()
  2158. # --- Ending Routine "done" ---
  2159. for thisComponent in doneComponents:
  2160. if hasattr(thisComponent, "setAutoDraw"):
  2161. thisComponent.setAutoDraw(False)
  2162. thisExp.addData('done.stopped', globalClock.getTime())
  2163. sound_5.pause() # ensure sound has stopped at end of Routine
  2164. # the Routine "done" was not non-slip safe, so reset the non-slip timer
  2165. routineTimer.reset()
  2166. # mark experiment as finished
  2167. endExperiment(thisExp, win=win, inputs=inputs)
  2168. def saveData(thisExp):
  2169. """
  2170. Save data from this experiment
  2171. Parameters
  2172. ==========
  2173. thisExp : psychopy.data.ExperimentHandler
  2174. Handler object for this experiment, contains the data to save and information about
  2175. where to save it to.
  2176. """
  2177. filename = thisExp.dataFileName
  2178. # these shouldn't be strictly necessary (should auto-save)
  2179. thisExp.saveAsWideText(filename + '.csv', delim='auto')
  2180. thisExp.saveAsPickle(filename)
  2181. def endExperiment(thisExp, inputs=None, win=None):
  2182. """
  2183. End this experiment, performing final shut down operations.
  2184. This function does NOT close the window or end the Python process - use `quit` for this.
  2185. Parameters
  2186. ==========
  2187. thisExp : psychopy.data.ExperimentHandler
  2188. Handler object for this experiment, contains the data to save and information about
  2189. where to save it to.
  2190. inputs : dict
  2191. Dictionary of input devices by name.
  2192. win : psychopy.visual.Window
  2193. Window for this experiment.
  2194. """
  2195. if win is not None:
  2196. # remove autodraw from all current components
  2197. win.clearAutoDraw()
  2198. # Flip one final time so any remaining win.callOnFlip()
  2199. # and win.timeOnFlip() tasks get executed
  2200. win.flip()
  2201. # mark experiment handler as finished
  2202. thisExp.status = FINISHED
  2203. # shut down eyetracker, if there is one
  2204. if inputs is not None:
  2205. if 'eyetracker' in inputs and inputs['eyetracker'] is not None:
  2206. inputs['eyetracker'].setConnectionState(False)
  2207. logging.flush()
  2208. def quit(thisExp, win=None, inputs=None, thisSession=None):
  2209. """
  2210. Fully quit, closing the window and ending the Python process.
  2211. Parameters
  2212. ==========
  2213. win : psychopy.visual.Window
  2214. Window to close.
  2215. inputs : dict
  2216. Dictionary of input devices by name.
  2217. thisSession : psychopy.session.Session or None
  2218. Handle of the Session object this experiment is being run from, if any.
  2219. """
  2220. thisExp.abort() # or data files will save again on exit
  2221. # make sure everything is closed down
  2222. if win is not None:
  2223. # Flip one final time so any remaining win.callOnFlip()
  2224. # and win.timeOnFlip() tasks get executed before quitting
  2225. win.flip()
  2226. win.close()
  2227. if inputs is not None:
  2228. if 'eyetracker' in inputs and inputs['eyetracker'] is not None:
  2229. inputs['eyetracker'].setConnectionState(False)
  2230. logging.flush()
  2231. if thisSession is not None:
  2232. thisSession.stop()
  2233. # terminate Python process
  2234. core.quit()
  2235. # if running this experiment as a script...
  2236. if __name__ == '__main__':
  2237. # call all functions in order
  2238. expInfo = showExpInfoDlg(expInfo=expInfo)
  2239. thisExp = setupData(expInfo=expInfo)
  2240. logFile = setupLogging(filename=thisExp.dataFileName)
  2241. win = setupWindow(expInfo=expInfo)
  2242. inputs = setupInputs(expInfo=expInfo, thisExp=thisExp, win=win)
  2243. run(
  2244. expInfo=expInfo,
  2245. thisExp=thisExp,
  2246. win=win,
  2247. inputs=inputs
  2248. )
  2249. saveData(thisExp=thisExp)
  2250. quit(thisExp=thisExp, win=win, inputs=inputs)