2
0

daily_routine.py 108 KB

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