DLInt1SynCurrent.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 = "fortyMSPulse"
  26. showBefore = 300 * units.ms
  27. showAfter = 50 * units.ms
  28. # simStepSize = 0.1 * units.ms
  29. # simDuration = 1500 * units.ms
  30. # # inputParsName = 'oneSecondPulse'
  31. # # inputParsName = 'pulseTrainInt20Dur10'
  32. # inputParsName = 'pulseTrainInt20Dur16'
  33. # # inputParsName = 'pulseTrainInt33Dur10'
  34. # # inputParsName = 'pulseTrainInt33Dur16'
  35. # showBefore = 500 * units.ms
  36. # showAfter = 500 * units.ms
  37. DLInt1ModelProps = "DLInt1Aynur"
  38. DLInt1PropsDict = getattr(AdExpPars, DLInt1ModelProps)
  39. dlint1 = VSNeuron(**AdExp, inits=DLInt1PropsDict, name='dlint1')
  40. dlint1.recordMembraneV()
  41. dlint1.recordSpikes()
  42. DLInt1SynapsePropsE = 'DLInt1_syn_try2_e'
  43. # DLInt1SynapsePropsE = ""
  44. DLInt1SynapsePropsI = 'DLInt1_syn_try2_i'
  45. # DLInt1SynapsePropsI = ""
  46. DLInt1SynapseProps = "-".join((DLInt1SynapsePropsE, DLInt1SynapsePropsI))
  47. opDir = os.path.join(homeFolder, DLInt1ModelProps, DLInt1SynapseProps, inputParsName)
  48. opFile = os.path.join(opDir, 'SynCurrentTraces.png')
  49. OPNixFile = os.path.join(opDir, 'simResWithSynCurrents.h5')
  50. if os.path.isfile(opFile):
  51. ch = input('Results already exist at {}. Delete?(y/n):'.format(opFile))
  52. if ch == 'y':
  53. os.remove(opFile)
  54. if os.path.isfile(OPNixFile):
  55. os.remove(OPNixFile)
  56. else:
  57. sys.exit('User Abort!')
  58. elif not os.path.isdir(opDir):
  59. os.makedirs(opDir)
  60. inputPars = getattr(inputParsList, inputParsName)
  61. JO = JOSpikes265(nOutputs=1, simSettleTime=simSettleTime, **inputPars)
  62. if DLInt1SynapsePropsE:
  63. synPropsE = getattr(synapsePropsList, DLInt1SynapsePropsE)
  64. dlint1.addSynapse(synName="ExiJO", sourceNG=JO.JOSGG, **exp2Syn,
  65. synParsInits=synPropsE,
  66. synStateInits=exp2SynStateInits,
  67. sourceInd=0, destInd=0
  68. )
  69. if DLInt1SynapsePropsI:
  70. synPropsI = getattr(synapsePropsList, DLInt1SynapsePropsI)
  71. dlint1.addSynapse(synName="InhJO", sourceNG=JO.JOSGG, **exp2Syn,
  72. synParsInits=synPropsI,
  73. synStateInits=exp2SynStateInits,
  74. sourceInd=0, destInd=0
  75. )
  76. net = Network()
  77. net.add(JO.JOSGG)
  78. dlint1.addToNetwork(net)
  79. if DLInt1SynapsePropsE:
  80. gEMonitor = StateMonitor(dlint1.incomingSynapses["ExiJO"], "g_ExiJO", record=True)
  81. net.add(gEMonitor)
  82. if DLInt1SynapsePropsI:
  83. gIMonitor = StateMonitor(dlint1.incomingSynapses["InhJO"], "g_InhJO", record=True)
  84. net.add(gIMonitor)
  85. defaultclock.dt = simStepSize
  86. totalSimDur = simDuration + simSettleTime
  87. net.run(totalSimDur, report='text')
  88. simT, memV = dlint1.getMemVTrace()
  89. spikeTimes = dlint1.getSpikes()
  90. dlint1MemVAS = AnalogSignal(signal=memV / units.mV,
  91. sampling_period=(simStepSize / units.ms) * qu.ms,
  92. t_start=0 * qu.mV,
  93. units="mV",
  94. name="DLInt1 MemV")
  95. dlint1SpikesQU = (spikeTimes / units.ms) * qu.ms
  96. joSpikesQU = (JO.spikeTimes / units.ms) * qu.ms
  97. sineInput = getSineInput(simDur=simDuration, simStepSize=simStepSize,
  98. sinPulseDurs=inputPars['sinPulseDurs'],
  99. sinPulseStarts=inputPars['sinPulseStarts'],
  100. freq=265 * units.Hz, simSettleTime=simSettleTime)
  101. inputAS = AnalogSignal(signal=sineInput,
  102. sampling_period=(simStepSize / units.ms) * qu.ms,
  103. t_start=0 * qu.mV,
  104. units="um",
  105. name="Input Vibration Signal")
  106. fig, axs = plt.subplots(nrows=3, figsize=(10, 6.25), sharex='col')
  107. axs[0].plot(simT / units.ms, memV / units.mV)
  108. axs[0].set_ylabel('DLInt1\nMembrane\nPotential\n(mV)')
  109. spikesY = memV.min() + 1.05 * (memV.max() - memV.min())
  110. axs[0].plot(spikeTimes / units.ms, [spikesY / units.mV] * spikeTimes.shape[0], 'k^')
  111. axs[0].set_xlim([(simSettleTime - showBefore) / units.ms,
  112. (totalSimDur + showAfter) / units.ms])
  113. if DLInt1SynapsePropsE:
  114. gSynE = gEMonitor.g_ExiJO[0]
  115. iSynE = -gSynE * (memV - synPropsE['Esyn'])
  116. axs[1].plot(simT / units.ms,
  117. iSynE / units.nA, 'r-', label=r'$I_{synE}$')
  118. iSynEAS = AnalogSignal(signal=iSynE / units.nA,
  119. sampling_period=(simStepSize / units.ms) * qu.ms,
  120. t_start=0 * qu.mV,
  121. units="nA",
  122. name="DL-Int-1 input EPSC")
  123. if DLInt1SynapsePropsI:
  124. gSynI = gIMonitor.g_InhJO[0]
  125. iSynI = -gSynI * (memV - synPropsI['Esyn'])
  126. axs[1].plot(simT / units.ms,
  127. iSynI / units.nA, 'g-', label=r'$I_{synI}$')
  128. iSynIAS = AnalogSignal(signal=iSynI / units.nA,
  129. sampling_period=(simStepSize / units.ms) * qu.ms,
  130. t_start=0 * qu.mV,
  131. units="nA",
  132. name="DL-Int-1 input IPSC")
  133. axs[1].legend(loc='center right')
  134. axs[1].set_ylabel("Synaptic\ncurrents\n(nA)")
  135. axs[2].plot(simT / units.ms, sineInput,
  136. color=[130. / 255, 72. / 255, 7. / 255], ls='-', marker='None',
  137. label='Vibration Input')
  138. axs[2].plot(JO.spikeTimes / units.ms, [sineInput.max() * 1.05] * len(JO.spikeTimes), 'k^',
  139. label='JO Spikes')
  140. axs[2].legend(loc='upper right')
  141. axs[2].set_xlabel('time (ms)')
  142. axs[2].set_ylabel('Input')
  143. fig.tight_layout()
  144. fig.canvas.draw()
  145. fig.savefig(opFile, dpi=150)
  146. nixFile = nixio.File.open(OPNixFile, mode=nixio.FileMode.ReadWrite)
  147. neuronModels = nixFile.create_section("Neuron Models", "Model Parameters")
  148. DLInt1PropsSec = neuronModels.create_section("DL-Int-1", "AdExp")
  149. for propName, propVal in DLInt1PropsDict.items():
  150. addBrianQuantity2Section(DLInt1PropsSec, propName, propVal)
  151. inputSec = nixFile.create_section("Input Parameters", "Sinusoidal Pulses")
  152. for parName, parVal in inputPars.items():
  153. addBrianQuantity2Section(inputSec, parName, parVal)
  154. addBrianQuantity2Section(inputSec, "simSettleTime", simSettleTime)
  155. brianSimSettingsSec = nixFile.create_section("Simulation Parameters", "Brian Simulation")
  156. addBrianQuantity2Section(brianSimSettingsSec, "simStepSize", simStepSize)
  157. addBrianQuantity2Section(brianSimSettingsSec, "totalSimDuration", totalSimDur)
  158. brianSimSettingsSec.create_property("method", nixio.Value("euler"))
  159. synPropsSec = nixFile.create_section("Synapse Models", "Model Parameters")
  160. if DLInt1SynapsePropsE:
  161. JODLInt1SynESec = synPropsSec.create_section("JODLInt1Exi", "DoubleExpSyn")
  162. JODLInt1SynEDict = getattr(synapsePropsList, DLInt1SynapsePropsE)
  163. for propName, propVal in JODLInt1SynEDict.items():
  164. addBrianQuantity2Section(JODLInt1SynESec, propName, propVal)
  165. JODLInt1SynESec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  166. JODLInt1SynESec.create_property("PostSynaptic Neuron", nixio.Value("DLInt1"))
  167. if DLInt1SynapsePropsI:
  168. JODLInt1SynISec = synPropsSec.create_section("JODLInt1Inh", "DoubleExpSyn")
  169. JODLInt1SynIDict = getattr(synapsePropsList, DLInt1SynapsePropsI)
  170. for propName, propVal in JODLInt1SynIDict.items():
  171. addBrianQuantity2Section(JODLInt1SynISec, propName, propVal)
  172. JODLInt1SynISec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  173. JODLInt1SynISec.create_property("PostSynaptic Neuron", nixio.Value("DLInt1"))
  174. blk = nixFile.create_block("Simulation Traces", "Brian Output")
  175. DLInt1DA = addAnalogSignal2Block(blk, dlint1MemVAS)
  176. inputDA = addAnalogSignal2Block(blk, inputAS)
  177. if DLInt1SynapsePropsE:
  178. epscAS = addAnalogSignal2Block(blk, iSynEAS)
  179. if DLInt1SynapsePropsI:
  180. ipscAS = addAnalogSignal2Block(blk, iSynIAS)
  181. addMultiTag("DLInt1 Spikes", type="Spikes", positions=dlint1SpikesQU,
  182. blk=blk, refs=[DLInt1DA])
  183. addMultiTag("JO Spikes", type="Spikes", positions=joSpikesQU,
  184. blk=blk, refs=[inputDA])
  185. nixFile.close()