TestSyn.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Based on https://www.neuron.yale.edu/phpBB/viewtopic.php?f=2&t=2750
  2. from neuron import *
  3. import numpy as np
  4. import sys
  5. import matplotlib.pyplot as plt
  6. duration = 500
  7. dt = 0.01
  8. def get_random_stim(rate):
  9. stimNc = h.NetStim()
  10. stimNc.noise = 1
  11. stimNc.start = 0
  12. stimNc.number = 1e9
  13. stimNc.interval = 1000.0 / rate
  14. return stimNc
  15. def get_timed_stim():
  16. stimNc = h.NetStim()
  17. stimNc.noise = 0
  18. stimNc.start = 100
  19. stimNc.number = 3
  20. stimNc.interval = 30
  21. return stimNc
  22. soma = h.Section()
  23. soma.L = 17.841242
  24. soma.diam = 17.841242
  25. soma.push()
  26. soma.insert("pas")
  27. soma.g_pas = 0.0003
  28. soma.e_pas = -70.0
  29. soma.Ra = 0.0354 * 1000
  30. """
  31. syn = h.ExpSyn(0.5, sec=soma)
  32. stim1 = h.IClamp(0.5, sec=soma)
  33. stim1.delay = 50.0
  34. stim1.dur = 5.0
  35. stim1.amp = 0.4
  36. stim2 = h.IClamp(0.5, sec=soma)
  37. stim2.delay = 140.0
  38. stim2.dur = 5.0
  39. stim2.amp = 0.4
  40. """
  41. def get_base_syn():
  42. syn = h.probAMPANMDASyn(0.5, sec=soma)
  43. return syn
  44. syn = get_base_syn()
  45. def run_sim(rate=10):
  46. print("Running simulation of %s with %s Hz input" % (duration, rate))
  47. # stimNc = get_random_stim(rate)
  48. stimNc = get_timed_stim()
  49. vec_nc = h.Vector()
  50. nc = h.NetCon(stimNc, syn)
  51. nc.weight[0] = 0.001
  52. nc.delay = 0
  53. nc.record(vec_nc)
  54. vec = {}
  55. all_states = ["A_AMPA", "A_NMDA", "B_AMPA", "B_NMDA"]
  56. gs = ["g_AMPA", "g_NMDA"]
  57. other = ["v", "t"]
  58. for var in (all_states + gs + other):
  59. vec[var] = h.Vector()
  60. if var != "v" and var != "t":
  61. exec("print('Recording: %s')" % var)
  62. exec("vec ['%s'].record(syn._ref_%s)" % (var, var))
  63. # record the membrane potentials and
  64. # synaptic currents
  65. vec["v"].record(soma(0.5)._ref_v)
  66. vec["t"].record(h._ref_t)
  67. # run the simulation
  68. h.load_file("stdrun.hoc")
  69. h.init()
  70. h.tstop = duration
  71. h.dt = dt
  72. h.run()
  73. spikes = []
  74. isis = []
  75. lastSpike = None
  76. for t in vec_nc:
  77. spikes.append(t)
  78. # print(t)
  79. if lastSpike:
  80. isis.append(t - lastSpike)
  81. lastSpike = t
  82. hz = 1000 / (h.tstop / len(spikes))
  83. print("nc: Spike times: %s" % ["%.3f" % t for t in vec_nc])
  84. print(
  85. "nc: Num spikes: %s; avg rate: %s Hz; avg isi: %s ms; std isi: %s ms"
  86. % (len(spikes), hz, np.average(isis), np.std(isis))
  87. )
  88. # assert abs((hz-rate)/rate)<0.01
  89. """
  90. scales = {
  91. "v": 0.001,
  92. "g": 1e-6,
  93. }
  94. for v in postLTP_vals:
  95. scales[v] = 1
  96. for var in scales:
  97. var_file = open("%s.dat" % var, "w")
  98. for i in range(len(vec["t"])):
  99. var_file.write("%s\t%s\n" % (vec["t"][i] / 1000, vec[var][i] * scales[var]))
  100. var_file.close()
  101. """
  102. for var in (all_states + gs + other):
  103. var_file = open("%s.dat" % var, "w")
  104. for i in range(len(vec["t"])):
  105. var_file.write("%s\t%s\n" % (vec["t"][i] / 1000, vec[var][i]))
  106. var_file.close()
  107. if not "-nogui" in sys.argv:
  108. # plot the results
  109. plt.figure()
  110. plt.title("Membrane potential")
  111. plt.plot(vec["t"], vec["v"])
  112. plt.figure()
  113. plt.title("Conductance")
  114. plt.plot(vec["t"], vec["g_AMPA"], label="g_AMPA")
  115. plt.plot(vec["t"], vec["g_NMDA"], label="g_NMDA")
  116. plt.legend()
  117. plt.figure()
  118. plt.title("States")
  119. for s in all_states:
  120. plt.plot(vec["t"], vec[s], label=s)
  121. plt.legend()
  122. run_sim()
  123. if not "-nogui" in sys.argv:
  124. plt.show()