JODLInt1DLInt2.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import os
  2. import sys
  3. import seaborn as sns
  4. from brian2 import defaultclock, units
  5. from brian2.core.network import Network
  6. from brian2.units.fundamentalunits import Quantity
  7. from matplotlib import pyplot as plt
  8. from dirDefs import homeFolder
  9. from models.neuronModels import VSNeuron, JOSpikes265, getSineInput
  10. from mplPars import mplPars
  11. from paramLists import synapsePropsList, inputParsList, AdExpPars
  12. from models.synapses import exp2SynStateInits, exp2Syn
  13. from models.neurons import AdExp
  14. from neo import AnalogSignal, SpikeTrain
  15. import nixio
  16. from neoNIXIO import addAnalogSignal2Block, addMultiTag
  17. import quantities as qu
  18. from brianUtils import addBrianQuantity2Section
  19. def runJODLInt1DLInt2(simStepSize: Quantity, simDuration: Quantity, simSettleTime: Quantity,
  20. inputParsName: str, showBefore: Quantity, showAfter: Quantity,
  21. DLInt1ModelProps: str, DLInt2ModelProps: str,
  22. DLInt1SynapsePropsE: str, DLInt1SynapsePropsI: str,
  23. DLInt2SynapseProps: str, DLInt1DLInt2SynProps: str,
  24. askReplace=True):
  25. sns.set(style="whitegrid", rc=mplPars)
  26. DLInt1SynapseProps = "".join((DLInt1SynapsePropsE, DLInt1SynapsePropsI))
  27. opDir = os.path.join(homeFolder, DLInt1ModelProps + DLInt2ModelProps,
  28. DLInt1SynapseProps + DLInt2SynapseProps + DLInt1DLInt2SynProps,
  29. inputParsName)
  30. opFile = os.path.join(opDir, 'Traces.png')
  31. OPNixFile = os.path.join(opDir, 'SimResults.h5')
  32. if askReplace:
  33. if os.path.isfile(opFile):
  34. ch = input('Results already exist at {}. Delete?(y/n):'.format(opFile))
  35. if ch == 'y':
  36. os.remove(opFile)
  37. if os.path.isfile(OPNixFile):
  38. os.remove(OPNixFile)
  39. else:
  40. sys.exit('User Abort!')
  41. elif not os.path.isdir(opDir):
  42. os.makedirs(opDir)
  43. else:
  44. if os.path.isfile(opFile):
  45. os.remove(opFile)
  46. if os.path.isfile(OPNixFile):
  47. os.remove(OPNixFile)
  48. elif not os.path.isdir(opDir):
  49. os.makedirs(opDir)
  50. inputPars = getattr(inputParsList, inputParsName)
  51. net = Network()
  52. JO = JOSpikes265(nOutputs=1, simSettleTime=simSettleTime, **inputPars)
  53. net.add(JO.JOSGG)
  54. DLInt1PropsDict = getattr(AdExpPars, DLInt1ModelProps)
  55. dlint1 = VSNeuron(**AdExp, inits=DLInt1PropsDict, name='dlint1')
  56. dlint1.recordSpikes()
  57. dlint1.recordMembraneV()
  58. if DLInt1SynapsePropsE:
  59. dlint1.addSynapse(synName="ExiJO", sourceNG=JO.JOSGG, **exp2Syn,
  60. synParsInits=getattr(synapsePropsList, DLInt1SynapsePropsE),
  61. synStateInits=exp2SynStateInits,
  62. sourceInd=0, destInd=0
  63. )
  64. if DLInt1SynapsePropsI:
  65. dlint1.addSynapse(synName="InhJO", sourceNG=JO.JOSGG, **exp2Syn,
  66. synParsInits=getattr(synapsePropsList, DLInt1SynapsePropsI),
  67. synStateInits=exp2SynStateInits,
  68. sourceInd=0, destInd=0
  69. )
  70. dlint1.addToNetwork(net)
  71. DLInt2PropsDict = getattr(AdExpPars, DLInt2ModelProps)
  72. dlint2 = VSNeuron(**AdExp, inits=DLInt2PropsDict, name='dlint2')
  73. dlint2.recordMembraneV()
  74. dlint2.recordSpikes()
  75. if DLInt2SynapseProps:
  76. dlint2.addSynapse(synName="JOExi", sourceNG=JO.JOSGG, **exp2Syn,
  77. synParsInits=getattr(synapsePropsList, DLInt2SynapseProps),
  78. synStateInits=exp2SynStateInits,
  79. sourceInd=0, destInd=0
  80. )
  81. if DLInt1DLInt2SynProps:
  82. dlint2.addSynapse(synName="DLInt1", sourceNG=dlint1.ng, **exp2Syn,
  83. synParsInits=getattr(synapsePropsList, DLInt1DLInt2SynProps),
  84. synStateInits=exp2SynStateInits,
  85. sourceInd=0, destInd=0
  86. )
  87. dlint2.addToNetwork(net)
  88. defaultclock.dt = simStepSize
  89. totalSimDur = simDuration + simSettleTime
  90. net.run(totalSimDur, report='text')
  91. simT, DLInt1_memV = dlint1.getMemVTrace()
  92. DLInt1_spikeTimes = dlint1.getSpikes()
  93. fig, axs = plt.subplots(nrows=3, figsize=(10, 6.25), sharex='col')
  94. axs[0].plot(simT / units.ms, DLInt1_memV / units.mV)
  95. spikesY = DLInt1_memV.min() + 1.05 * (DLInt1_memV.max() - DLInt1_memV.min())
  96. axs[0].plot(DLInt1_spikeTimes / units.ms, [spikesY / units.mV] * DLInt1_spikeTimes.shape[0], 'k^')
  97. axs[0].set_ylabel('DLInt1 \nmemV (mV)')
  98. axs[0].set_xlim([(simSettleTime - showBefore) / units.ms,
  99. (totalSimDur + showAfter) / units.ms])
  100. simT, DLInt2_memV = dlint2.getMemVTrace()
  101. DLInt2_spikeTimes = dlint2.getSpikes()
  102. axs[1].plot(simT / units.ms, DLInt2_memV / units.mV)
  103. spikesY = DLInt2_memV.min() + 1.05 * (DLInt2_memV.max() - DLInt2_memV.min())
  104. axs[1].plot(DLInt2_spikeTimes / units.ms, [spikesY / units.mV] * DLInt2_spikeTimes.shape[0], 'k^')
  105. axs[1].set_ylabel('DLInt2 \nmemV (mV)')
  106. sineInput = getSineInput(simDur=simDuration, simStepSize=simStepSize,
  107. sinPulseDurs=inputPars['sinPulseDurs'],
  108. sinPulseStarts=inputPars['sinPulseStarts'],
  109. freq=265 * units.Hz, simSettleTime=simSettleTime)
  110. axs[2].plot(simT / units.ms, sineInput, 'r-', label='Vibration Input')
  111. axs[2].plot(JO.spikeTimes / units.ms, [sineInput.max() * 1.05] * len(JO.spikeTimes), 'k^',
  112. label='JO Spikes')
  113. axs[2].legend(loc='upper right')
  114. axs[2].set_xlabel('time (ms)')
  115. axs[2].set_ylabel('Vibration \nInput/JO\n Spikes')
  116. fig.tight_layout()
  117. fig.canvas.draw()
  118. fig.savefig(opFile, dpi=150)
  119. plt.close(fig.number)
  120. del fig
  121. dlint1MemVAS = AnalogSignal(signal=DLInt1_memV /units.mV,
  122. sampling_period=(simStepSize / units.ms) * qu.ms,
  123. t_start=0 * qu.mV,
  124. units="mV",
  125. name="DLInt1 MemV")
  126. dlint2MemVAS = AnalogSignal(signal=DLInt2_memV / units.mV,
  127. sampling_period=(simStepSize / units.ms) * qu.ms,
  128. t_start=0 * qu.mV,
  129. units="mV",
  130. name="DLInt2 MemV")
  131. inputAS = AnalogSignal(signal=sineInput,
  132. sampling_period=(simStepSize / units.ms) * qu.ms,
  133. t_start=0 * qu.mV,
  134. units="um",
  135. name="Input Vibration Signal")
  136. dlint1SpikesQU = (DLInt1_spikeTimes / units.ms) * qu.ms
  137. dlint2SpikesQU = (DLInt2_spikeTimes / units.ms) * qu.ms
  138. joSpikesQU = (JO.spikeTimes / units.ms) * qu.ms
  139. nixFile = nixio.File.open(OPNixFile, mode=nixio.FileMode.ReadWrite)
  140. neuronModels = nixFile.create_section("Neuron Models", "Model Parameters")
  141. DLInt1PropsSec = neuronModels.create_section("DL-Int-1", "AdExp")
  142. for propName, propVal in DLInt1PropsDict.items():
  143. addBrianQuantity2Section(DLInt1PropsSec, propName, propVal)
  144. DLInt2PropsSec = neuronModels.create_section("DL-Int-2", "AdExp")
  145. for propName, propVal in DLInt2PropsDict.items():
  146. addBrianQuantity2Section(DLInt2PropsSec, propName, propVal)
  147. inputSec = nixFile.create_section("Input Parameters", "Sinusoidal Pulses")
  148. for parName, parVal in inputPars.items():
  149. addBrianQuantity2Section(inputSec, parName, parVal)
  150. addBrianQuantity2Section(inputSec, "simSettleTime", simSettleTime)
  151. brianSimSettingsSec = nixFile.create_section("Simulation Parameters", "Brian Simulation")
  152. addBrianQuantity2Section(brianSimSettingsSec, "simStepSize", simStepSize)
  153. addBrianQuantity2Section(brianSimSettingsSec, "totalSimDuration", totalSimDur)
  154. brianSimSettingsSec.create_property("method", nixio.Value("euler"))
  155. synPropsSec = nixFile.create_section("Synapse Models", "Model Parameters")
  156. if DLInt1SynapsePropsE:
  157. JODLInt1SynESec = synPropsSec.create_section("JODLInt1Exi", "DoubleExpSyn")
  158. JODLInt1SynEDict = getattr(synapsePropsList, DLInt1SynapsePropsE)
  159. for propName, propVal in JODLInt1SynEDict.items():
  160. addBrianQuantity2Section(JODLInt1SynESec, propName, propVal)
  161. JODLInt1SynESec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  162. JODLInt1SynESec.create_property("PostSynaptic Neuron", nixio.Value("DLInt1"))
  163. if DLInt1SynapsePropsI:
  164. JODLInt1SynISec = synPropsSec.create_section("JODLInt1Inh", "DoubleExpSyn")
  165. JODLInt1SynIDict = getattr(synapsePropsList, DLInt1SynapsePropsI)
  166. for propName, propVal in JODLInt1SynIDict.items():
  167. addBrianQuantity2Section(JODLInt1SynISec, propName, propVal)
  168. JODLInt1SynISec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  169. JODLInt1SynISec.create_property("PostSynaptic Neuron", nixio.Value("DLInt1"))
  170. if DLInt2SynapseProps:
  171. JODLInt2SynESec = synPropsSec.create_section("JODLInt2Exi", "DoubleExpSyn")
  172. JODLInt2SynEDict = getattr(synapsePropsList, DLInt2SynapseProps)
  173. for propName, propVal in JODLInt2SynEDict.items():
  174. addBrianQuantity2Section(JODLInt2SynESec, propName, propVal)
  175. JODLInt2SynESec.create_property("PreSynaptic Neuron", nixio.Value("JO"))
  176. JODLInt2SynESec.create_property("PostSynaptic Neuron", nixio.Value("DLInt2"))
  177. if DLInt1DLInt2SynProps:
  178. DLInt1DLInt2SynSec = synPropsSec.create_section("DLInt1DLInt2Inh", "DoubleExpSyn")
  179. DLInt1DLInt2SynDict = getattr(synapsePropsList, DLInt1DLInt2SynProps)
  180. for propName, propVal in DLInt1DLInt2SynDict.items():
  181. addBrianQuantity2Section(DLInt1DLInt2SynSec, propName, propVal)
  182. DLInt1DLInt2SynSec.create_property("PreSynaptic Neuron", nixio.Value("DLInt1"))
  183. DLInt1DLInt2SynSec.create_property("PostSynaptic Neuron", nixio.Value("DLInt2"))
  184. blk = nixFile.create_block("Simulation Traces", "Brian Output")
  185. DLInt1DA = addAnalogSignal2Block(blk, dlint1MemVAS)
  186. DLInt2DA = addAnalogSignal2Block(blk, dlint2MemVAS)
  187. inputDA = addAnalogSignal2Block(blk, inputAS)
  188. addMultiTag("DLInt1 Spikes", type="Spikes", positions=dlint1SpikesQU,
  189. blk=blk, refs=[DLInt1DA])
  190. addMultiTag("DLInt2 Spikes", type="Spikes", positions=dlint2SpikesQU,
  191. blk=blk, refs=[DLInt2DA])
  192. addMultiTag("JO Spikes", type="Spikes", positions=joSpikesQU,
  193. blk=blk, refs=[inputDA])
  194. nixFile.close()