Skip to content

Instantly share code, notes, and snippets.

@cosacog
Last active July 17, 2018 05:17
Show Gist options
  • Save cosacog/14dc6114f07d106b24c4c8ca0ae11782 to your computer and use it in GitHub Desktop.
Save cosacog/14dc6114f07d106b24c4c8ca0ae11782 to your computer and use it in GitHub Desktop.
psychopy for single pulse TMS
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
## settings
prt_num = int('00001111',2) # port number(digit number) indicates port 8,7,6,5,4,3,2,1
t_range = [5, 7] # time range for TMS (sec)
t_init_wait = 3 # time for start TMS (sec)
n_rep_tms = 12 # TMS repetition frequency (times)
p_prt_address = '0xCFF8'
##
from psychopy import visual, core, event, logging, gui, parallel
import numpy as np
# wait time array
arr_t_wait = np.random.random_sample(n_rep_tms-1)*np.diff(np.array(t_range))[0]+t_range[0]
arr_t_wait_comp = np.r_[t_init_wait, arr_t_wait]
# window
myWin = visual.Window(size=(1024, 768), fullscr=False, screen=0, allowGUI=False, allowStencil=False,
monitor=u'crt_monitor', color=[0,0,0], colorSpace='rgb',
blendMode='avg')
# for test
#myWin = visual.Window([600,600], color='black',units='pix')
txt_setting = """Press <LtClick> for single stim
<Return> to start sequential stim
Time range: """ +str(t_range[0]) + '-' + str(t_range[1]) + 'sec\nTMS number of times: ' + str(n_rep_tms)
msg = visual.TextStim(myWin, text=txt_setting,units='pix',height= 32)
msg.draw()
myWin.flip()
# clock
trialClock = core.Clock()
# click for TMS
myMouse = event.Mouse()
parallel.setPortAddress(address=p_prt_address)
wasMouseDown = False
continueRoutine = True
while continueRoutine:
isMouseDown = myMouse.getPressed()[0] #
myMouse.clickReset()
if not isMouseDown and wasMouseDown:
print myMouse.getPos()
parallel.setData(int(prt_num))
# for test
# parallel.setData(int(255))
core.wait(0.01)
parallel.setData(int(0))
wasMouseDown = isMouseDown
theseKeys = event.getKeys(keyList=['return'])
if len(theseKeys) > 0: # at least one key was pressed
# key_resp_start.keys = theseKeys[-1] # just the last key pressed
#key_resp_start.rt = key_resp_start.clock.getTime()
# a response ends the routine
print(theseKeys)
continueRoutine = False
# press "escape" then quit
if event.getKeys(keyList=["escape"]):
core.quit()
# for-loop for sequential TMS
ii = 1
for t_wait in arr_t_wait_comp:
txt_trial = 'Trial '+ str(ii) + '/' + str(n_rep_tms)
msg = visual.TextStim(myWin, height=36,text=txt_trial,units='pix')
msg.draw()
myWin.flip()
# start clock
t = 0
trialClock.reset()
continueRoutine = True
while continueRoutine:
core.wait(0.2)
t = trialClock.getTime()
# print(t)
# press "escape" then quit
if event.getKeys(keyList=["escape"]):
core.quit()
if t >=t_wait:
# output trigger
parallel.setData(int(prt_num))
core.wait(0.01)
parallel.setData(int(0))
ii += 1
continueRoutine = False
myWin.close()
core.quit()
@clueless11
Copy link

How would one use this to run a repetitive protocol? I don’t understand t_range ?

Is t_init_wait just a one time initial waiting period to commence? How goes on set isi ... or inter trains intervals ...

Is n_rep_tms total # of stimulations?

Any further details at all would be appreciated

@cosacog
Copy link
Author

cosacog commented Jul 17, 2018

Thank you for your comment.

After run this script on Psychopy 2, press "Enter" to start repetitive protocol. I assume that single pulse by "left click" to confirm that the coil position is correct, then press "Enter" to start the protocol.

Is t_init_wait just a one time initial waiting period to commence?

Correct.

How goes on set isi

t_range = [5, 7] # time range for TMS (sec)

The number indicates the minimum ISI and maximum isi.

Is n_rep_tms total # of stimulations?

Correct.

I did not expect that this helps someone. I store it just for me.

I hope that helps. I will prepare the documents in my spare time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment