simSynCurrents.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. import os
  2. import sys
  3. import seaborn as sns
  4. from brian2 import defaultclock, units, StateMonitor
  5. from matplotlib import pyplot as plt
  6. from brian2.core.network import Network
  7. from dirDefs import homeFolder
  8. from models.neuronModels import VSNeuron, JOSpikes265, getSineInput
  9. from models.neurons import AdExp
  10. from models.synapses import exp2Syn, exp2SynStateInits
  11. from mplPars import mplPars
  12. from paramLists import synapsePropsList, inputParsList, AdExpPars
  13. from neo import AnalogSignal
  14. import nixio
  15. from neoNIXIO import addAnalogSignal2Block, addMultiTag
  16. import quantities as qu
  17. from brianUtils import addBrianQuantity2Section
  18. sns.set(style="whitegrid", rc=mplPars)
  19. simSettleTime = 600 * units.ms
  20. simStepSize = 0.1 * units.ms
  21. simDuration = 150 * units.ms
  22. # inputParsName = 'onePulse'
  23. # inputParsName = 'twoPulse'
  24. # inputParsName = 'threePulse'
  25. inputParsName = "thirtyMSPulse"
  26. # inputParsName = "fortyMSPulse"
  27. showBefore = 50 * units.ms
  28. showAfter = 50 * units.ms
  29. # simStepSize = 0.1 * units.ms
  30. # simDuration = 450 * units.ms
  31. # # inputParsName = "pTShortInt20Dur10"
  32. # # inputParsName = "pTShortInt20Dur16"
  33. # # inputParsName = "pTShortInt33Dur10"
  34. # # inputParsName = "pTShortInt33Dur16"
  35. # # inputParsName = "pTShortInt33Dur20"
  36. # # inputParsName = "pTShortInt50Dur10"
  37. # # inputParsName = "pTShortInt50Dur16"
  38. # # inputParsName = "pTShortInt50Dur20"
  39. # inputParsName = "pTShortInt100Dur10"
  40. # # inputParsName = "pTShortInt100Dur16"
  41. # # inputParsName = "pTShortInt100Dur20"
  42. #
  43. # showBefore = 100 * units.ms
  44. # showAfter = 100 * units.ms
  45. # simStepSize = 0.1 * units.ms
  46. # simDuration = 1500 * units.ms
  47. # # inputParsName = 'oneSecondPulse'
  48. # # inputParsName = 'pulseTrainInt20Dur10'
  49. # inputParsName = 'pulseTrainInt20Dur16'
  50. # # inputParsName = 'pulseTrainInt33Dur10'
  51. # # inputParsName = 'pulseTrainInt33Dur16'
  52. # showBefore = 500 * units.ms
  53. # showAfter = 500 * units.ms
  54. DLInt1ModelProps = "DLInt1Aynur"
  55. DLInt1SynapsePropsE = 'DLInt1_syn_try2_e'
  56. # DLInt1SynapsePropsE = ""
  57. DLInt1SynapsePropsI = 'DLInt1_syn_try2_i'
  58. # DLInt1SynapsePropsI = ""
  59. DLInt1SynapseProps = "".join((DLInt1SynapsePropsE, DLInt1SynapsePropsI))
  60. DLInt2ModelProps = "DLInt2Try2"
  61. DLInt2SynapseProps = 'DLInt2_syn_try2'
  62. DLInt1DLInt2SynProps = "DLInt1_DLInt2_try1"
  63. opDir = os.path.join(homeFolder, DLInt1ModelProps + DLInt2ModelProps,
  64. DLInt1SynapseProps + DLInt2SynapseProps + DLInt1DLInt2SynProps,
  65. inputParsName)
  66. opFileDLInt1 = os.path.join(opDir, 'SynCurrentTracesDLInt1.png')
  67. opFileDLInt2 = os.path.join(opDir, 'SynCurrentTracesDLInt2.png')
  68. OPNixFile = os.path.join(opDir, 'simResWithSynCurrents.h5')
  69. if os.path.isfile(OPNixFile):
  70. ch = input('Results already exist at {}. Delete?(y/n):'.format(OPNixFile))
  71. if ch == 'y':
  72. os.remove(OPNixFile)
  73. if os.path.isfile(opFileDLInt1):
  74. os.remove(opFileDLInt1)
  75. if os.path.isfile(opFileDLInt2):
  76. os.remove(opFileDLInt2)
  77. else:
  78. sys.exit('User Abort!')
  79. elif not os.path.isdir(opDir):
  80. os.makedirs(opDir)
  81. inputPars = getattr(inputParsList, inputParsName)
  82. net = Network()
  83. JO = JOSpikes265(nOutputs=1, simSettleTime=simSettleTime, **inputPars)
  84. net.add(JO.JOSGG)
  85. DLInt1PropsDict = getattr(AdExpPars, DLInt1ModelProps)
  86. dlint1 = VSNeuron(**AdExp, inits=DLInt1PropsDict, name='dlint1')
  87. dlint1.recordSpikes()
  88. dlint1.recordMembraneV()
  89. if DLInt1SynapsePropsE:
  90. synPropsEDLInt1 = getattr(synapsePropsList, DLInt1SynapsePropsE)
  91. dlint1.addSynapse(synName="ExiJO", sourceNG=JO.JOSGG, **exp2Syn,
  92. synParsInits=synPropsEDLInt1,
  93. synStateInits=exp2SynStateInits,
  94. sourceInd=0, destInd=0
  95. )
  96. if DLInt1SynapsePropsI:
  97. synPropsIDLInt1 = getattr(synapsePropsList, DLInt1SynapsePropsI)
  98. dlint1.addSynapse(synName="InhJO", sourceNG=JO.JOSGG, **exp2Syn,
  99. synParsInits=synPropsIDLInt1,
  100. synStateInits=exp2SynStateInits,
  101. sourceInd=0, destInd=0
  102. )
  103. dlint1.addToNetwork(net)
  104. if DLInt1SynapsePropsE:
  105. gEMonitorDLInt1 = StateMonitor(dlint1.incomingSynapses["ExiJO"], "g_ExiJO", record=True)
  106. net.add(gEMonitorDLInt1)
  107. if DLInt1SynapsePropsI:
  108. gIMonitorDLInt1 = StateMonitor(dlint1.incomingSynapses["InhJO"], "g_InhJO", record=True)
  109. net.add(gIMonitorDLInt1)
  110. DLInt2PropsDict = getattr(AdExpPars, DLInt2ModelProps)
  111. dlint2 = VSNeuron(**AdExp, inits=DLInt2PropsDict, name='dlint2')
  112. dlint2.recordMembraneV()
  113. dlint2.recordSpikes()
  114. if DLInt2SynapseProps:
  115. synPropsEDLInt2 = getattr(synapsePropsList, DLInt2SynapseProps)
  116. dlint2.addSynapse(synName="ExiJO", sourceNG=JO.JOSGG, **exp2Syn,
  117. synParsInits=synPropsEDLInt2,
  118. synStateInits=exp2SynStateInits,
  119. sourceInd=0, destInd=0
  120. )
  121. if DLInt1DLInt2SynProps:
  122. synPropsIDLInt2 = getattr(synapsePropsList, DLInt1DLInt2SynProps)
  123. dlint2.addSynapse(synName="DLInt1", sourceNG=dlint1.ng, **exp2Syn,
  124. synParsInits=synPropsIDLInt2,
  125. synStateInits=exp2SynStateInits,
  126. sourceInd=0, destInd=0
  127. )
  128. dlint2.addToNetwork(net)
  129. if DLInt2SynapseProps:
  130. gEMonitorDLInt2 = StateMonitor(dlint2.incomingSynapses["ExiJO"], "g_ExiJO", record=True)
  131. net.add(gEMonitorDLInt2)
  132. if DLInt1DLInt2SynProps:
  133. gIMonitorDLInt2 = StateMonitor(dlint2.incomingSynapses["DLInt1"], "g_DLInt1", record=True)
  134. net.add(gIMonitorDLInt2)
  135. defaultclock.dt = simStepSize
  136. totalSimDur = simDuration + simSettleTime
  137. net.run(totalSimDur, report='text')
  138. simT, DLInt2memV = dlint2.getMemVTrace()
  139. DLInt2spikeTimes = dlint2.getSpikes()
  140. dlint2MemVAS = AnalogSignal(signal=DLInt2memV / units.mV,
  141. sampling_period=(simStepSize / units.ms) * qu.ms,
  142. t_start=0 * qu.mV,
  143. units="mV",
  144. name="DLInt2 MemV")
  145. dlint2SpikesQU = (DLInt2spikeTimes / units.ms) * qu.ms
  146. simT, DLInt1memV = dlint1.getMemVTrace()
  147. DLInt1spikeTimes = dlint1.getSpikes()
  148. dlint1MemVAS = AnalogSignal(signal=DLInt1memV / units.mV,
  149. sampling_period=(simStepSize / units.ms) * qu.ms,
  150. t_start=0 * qu.mV,
  151. units="mV",
  152. name="DLInt1 MemV")
  153. dlint1SpikesQU = (DLInt1spikeTimes / units.ms) * qu.ms
  154. joSpikesQU = (JO.spikeTimes / units.ms) * qu.ms
  155. sineInput = getSineInput(simDur=simDuration, simStepSize=simStepSize,
  156. sinPulseDurs=inputPars['sinPulseDurs'],
  157. sinPulseStarts=inputPars['sinPulseStarts'],
  158. freq=265 * units.Hz, simSettleTime=simSettleTime)
  159. inputAS = AnalogSignal(signal=sineInput,
  160. sampling_period=(simStepSize / units.ms) * qu.ms,
  161. t_start=0 * qu.mV,
  162. units="um",
  163. name="Input Vibration Signal")
  164. fig, axs = plt.subplots(nrows=3, figsize=(10, 6.25), sharex='col')
  165. axs[0].plot(simT / units.ms, DLInt2memV / units.mV)
  166. spikesY = DLInt2memV.min() + 1.05 * (DLInt2memV.max() - DLInt2memV.min())
  167. axs[0].plot(DLInt2spikeTimes / units.ms, [spikesY / units.mV] * DLInt2spikeTimes.shape[0], 'k^')
  168. axs[0].set_ylabel('DLInt2\nMembrane\nPotential\n(mV)')
  169. axs[0].set_xlim([(simSettleTime - showBefore) / units.ms,
  170. (totalSimDur + showAfter) / units.ms])
  171. fig1, axs1 = plt.subplots(nrows=3, figsize=(10, 6.25), sharex='col')
  172. axs1[0].plot(simT / units.ms, DLInt1memV / units.mV)
  173. spikesY = DLInt1memV.min() + 1.05 * (DLInt1memV.max() - DLInt1memV.min())
  174. axs1[0].plot(DLInt1spikeTimes / units.ms, [spikesY / units.mV] * DLInt1spikeTimes.shape[0], 'k^')
  175. axs1[0].set_ylabel('DLInt1\nMembrane\nPotential\n(mV)')
  176. axs1[0].set_xlim([(simSettleTime - showBefore) / units.ms,
  177. (totalSimDur + showAfter) / units.ms])
  178. if DLInt2SynapseProps:
  179. gSynEDLInt2 = gEMonitorDLInt2.g_ExiJO[0]
  180. iSynEDLInt2 = -gSynEDLInt2 * (DLInt2memV - synPropsEDLInt2['Esyn'])
  181. axs[1].plot(simT / units.ms,
  182. iSynEDLInt2 / units.nA, 'm-', label=r'$I_{synE}$')
  183. iSynEASDLInt2 = AnalogSignal(signal=iSynEDLInt2 / units.nA,
  184. sampling_period=(simStepSize / units.ms) * qu.ms,
  185. t_start=0 * qu.mV,
  186. units="nA",
  187. name="DL-Int-2 input EPSC")
  188. if DLInt1DLInt2SynProps:
  189. gSynI = gIMonitorDLInt2.g_DLInt1[0]
  190. iSynI = -gSynI * (DLInt2memV - synPropsIDLInt2['Esyn'])
  191. axs[1].plot(simT / units.ms,
  192. iSynI / units.nA, 'c-', label=r'$I_{synI}$')
  193. iSynIASDLInt2 = AnalogSignal(signal=iSynI / units.nA,
  194. sampling_period=(simStepSize / units.ms) * qu.ms,
  195. t_start=0 * qu.mV,
  196. units="nA",
  197. name="DL-Int-2 input IPSC")
  198. axs[1].legend(loc='center right')
  199. axs[1].set_ylabel("Synaptic\ncurrents\n(nA)")
  200. axs[2].plot(simT / units.ms, sineInput,
  201. color=[130. / 255, 72. / 255, 7. / 255], ls='-', marker='None',
  202. label='Vibration Input')
  203. axs[2].plot(JO.spikeTimes / units.ms, [sineInput.max() * 1.05] * len(JO.spikeTimes), 'k^',
  204. label='JO Spikes')
  205. axs[2].legend(loc='center right')
  206. axs[2].set_xlabel('time (ms)')
  207. axs[2].set_ylabel('Input')
  208. fig.tight_layout()
  209. fig.canvas.draw()
  210. # plt.show()
  211. fig.savefig(opFileDLInt2, dpi=150)
  212. if DLInt1SynapsePropsE:
  213. gSynEDLInt1 = gEMonitorDLInt1.g_ExiJO[0]
  214. iSynEDLInt1 = -gSynEDLInt1 * (DLInt1memV - synPropsEDLInt1['Esyn'])
  215. axs1[1].plot(simT / units.ms,
  216. iSynEDLInt1 / units.nA, 'r-', label=r'$I_{synE}$')
  217. iSynEASDLInt1 = AnalogSignal(signal=iSynEDLInt1 / units.nA,
  218. sampling_period=(simStepSize / units.ms) * qu.ms,
  219. t_start=0 * qu.mV,
  220. units="nA",
  221. name="DL-Int-1 input EPSC")
  222. if DLInt1SynapsePropsI:
  223. gSynIDLInt1 = gIMonitorDLInt1.g_InhJO[0]
  224. iSynIDLInt1 = -gSynIDLInt1 * (DLInt1memV - synPropsIDLInt1['Esyn'])
  225. axs1[1].plot(simT / units.ms,
  226. iSynIDLInt1 / units.nA, 'g-', label=r'$I_{synI}$')
  227. iSynIASDLInt1 = AnalogSignal(signal=iSynIDLInt1 / units.nA,
  228. sampling_period=(simStepSize / units.ms) * qu.ms,
  229. t_start=0 * qu.mV,
  230. units="nA",
  231. name="DL-Int-1 input IPSC")
  232. axs1[1].legend(loc='center right')
  233. axs1[1].set_ylabel("Synaptic\ncurrents\n(nA)")
  234. axs1[2].plot(simT / units.ms, sineInput,
  235. color=[130. / 255, 72. / 255, 7. / 255], ls='-', marker='None',
  236. label='Vibration Input')
  237. axs1[2].plot(JO.spikeTimes / units.ms, [sineInput.max() * 1.05] * len(JO.spikeTimes), 'k^',
  238. label='JO Spikes')
  239. axs1[2].legend(loc='center right')
  240. axs1[2].set_xlabel('time (ms)')
  241. axs1[2].set_ylabel('Input')
  242. fig1.tight_layout()
  243. fig1.canvas.draw()
  244. # plt.show()
  245. fig1.savefig(opFileDLInt1, dpi=150)
  246. nixFile = nixio.File.open(OPNixFile, mode=nixio.FileMode.ReadWrite)
  247. neuronModels = nixFile.create_section("Neuron Models", "Model Parameters")
  248. DLInt1PropsSec = neuronModels.create_section("DL-Int-1", "AdExp")
  249. for propName, propVal in DLInt1PropsDict.items():
  250. addBrianQuantity2Section(DLInt1PropsSec, propName, propVal)
  251. DLInt2PropsSec = neuronModels.create_section("DL-Int-2", "AdExp")
  252. for propName, propVal in DLInt2PropsDict.items():
  253. addBrianQuantity2Section(DLInt2PropsSec, propName, propVal)
  254. inputSec = nixFile.create_section("Input Parameters", "Sinusoidal Pulses")
  255. for parName, parVal in inputPars.items():
  256. addBrianQuantity2Section(inputSec, parName, parVal)
  257. addBrianQuantity2Section(inputSec, "simSettleTime", simSettleTime)
  258. brianSimSettingsSec = nixFile.create_section("Simulation Parameters", "Brian Simulation")
  259. addBrianQuantity2Section(brianSimSettingsSec, "simStepSize", simStepSize)
  260. addBrianQuantity2Section(brianSimSettingsSec, "totalSimDuration", totalSimDur)
  261. brianSimSettingsSec.create_property("method", nixio.Value("euler"))
  262. synPropsSec = nixFile.create_section("Synapse Models", "Model Parameters")
  263. if DLInt1SynapsePropsE:
  264. JODLInt1SynESec = synPropsSec.create_section("JODLInt1Exi", "DoubleExpSyn")
  265. JODLInt1SynEDict = getattr(synapsePropsList, DLInt1SynapsePropsE)
  266. for propName, propVal in JODLInt1SynEDict.items():
  267. addBrianQuantity2Section(JODLInt1SynESec, propName, propVal)
  268. JODLInt1SynESec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  269. JODLInt1SynESec.create_property("PostSynaptic Neuron", nixio.Value("DLInt1"))
  270. if DLInt1SynapsePropsI:
  271. JODLInt1SynISec = synPropsSec.create_section("JODLInt1Inh", "DoubleExpSyn")
  272. JODLInt1SynIDict = getattr(synapsePropsList, DLInt1SynapsePropsI)
  273. for propName, propVal in JODLInt1SynIDict.items():
  274. addBrianQuantity2Section(JODLInt1SynISec, propName, propVal)
  275. JODLInt1SynISec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  276. JODLInt1SynISec.create_property("PostSynaptic Neuron", nixio.Value("DLInt1"))
  277. if DLInt2SynapseProps:
  278. JODLInt2SynESec = synPropsSec.create_section("JODLInt2Exi", "DoubleExpSyn")
  279. JODLInt2SynEDict = getattr(synapsePropsList, DLInt2SynapseProps)
  280. for propName, propVal in JODLInt2SynEDict.items():
  281. addBrianQuantity2Section(JODLInt2SynESec, propName, propVal)
  282. JODLInt2SynESec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  283. JODLInt2SynESec.create_property("PostSynaptic Neuron", nixio.Value("DLInt2"))
  284. if DLInt1DLInt2SynProps:
  285. DLInt1DLInt2SynSec = synPropsSec.create_section("DLInt1DLInt2Inh", "DoubleExpSyn")
  286. DLInt1DLInt2SynDict = getattr(synapsePropsList, DLInt1DLInt2SynProps)
  287. for propName, propVal in DLInt1DLInt2SynDict.items():
  288. addBrianQuantity2Section(DLInt1DLInt2SynSec, propName, propVal)
  289. DLInt1DLInt2SynSec.create_property("PreSynaptic Neuron", nixio.Value("DLInt1"))
  290. DLInt1DLInt2SynSec.create_property("PostSynaptic Neuron", nixio.Value("DLInt2"))
  291. blk = nixFile.create_block("Simulation Traces", "Brian Output")
  292. DLInt2DA = addAnalogSignal2Block(blk, dlint2MemVAS)
  293. DLInt1DA = addAnalogSignal2Block(blk, dlint1MemVAS)
  294. inputDA = addAnalogSignal2Block(blk, inputAS)
  295. if DLInt1SynapsePropsE:
  296. epscAS = addAnalogSignal2Block(blk, iSynEASDLInt1)
  297. if DLInt1SynapsePropsI:
  298. ipscAS = addAnalogSignal2Block(blk, iSynIASDLInt1)
  299. if DLInt2SynapseProps:
  300. epscAS = addAnalogSignal2Block(blk, iSynEASDLInt2)
  301. if DLInt1DLInt2SynProps:
  302. ipscAS = addAnalogSignal2Block(blk, iSynIASDLInt2)
  303. addMultiTag("DLInt2 Spikes", type="Spikes", positions=dlint2SpikesQU,
  304. blk=blk, refs=[DLInt2DA])
  305. addMultiTag("DLInt1 Spikes", type="Spikes", positions=dlint1SpikesQU,
  306. blk=blk, refs=[DLInt2DA])
  307. addMultiTag("JO Spikes", type="Spikes", positions=joSpikesQU,
  308. blk=blk, refs=[inputDA])
  309. nixFile.close()