TestSynUDF.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. """
  29. syn = h.ExpSyn(0.5, sec=soma)
  30. stim1 = h.IClamp(0.5, sec=soma)
  31. stim1.delay = 50.0
  32. stim1.dur = 5.0
  33. stim1.amp = 0.4
  34. stim2 = h.IClamp(0.5, sec=soma)
  35. stim2.delay = 140.0
  36. stim2.dur = 5.0
  37. stim2.amp = 0.4
  38. """
  39. def get_base_syn():
  40. syn = h.probUDFSyn(0.5, sec=soma)
  41. return syn
  42. syn = get_base_syn()
  43. def run_sim(rate=10):
  44. print("Running simulation of %s with %s Hz input" % (duration, rate))
  45. # stimNc = get_random_stim(rate)
  46. stimNc = get_timed_stim()
  47. vec_nc = h.Vector()
  48. nc = h.NetCon(stimNc, syn)
  49. nc.weight[0] = 0.001
  50. nc.delay = 0
  51. nc.record(vec_nc)
  52. vec = {}
  53. all_states = ["A", "B"]
  54. gs = ["g"]
  55. other = ["v", "t"]
  56. for var in (all_states + gs + other):
  57. vec[var] = h.Vector()
  58. if var != "v" and var != "t":
  59. exec("print('Recording: %s')" % var)
  60. exec("vec ['%s'].record(syn._ref_%s)" % (var, var))
  61. # record the membrane potentials and
  62. # synaptic currents
  63. vec["v"].record(soma(0.5)._ref_v)
  64. vec["t"].record(h._ref_t)
  65. # run the simulation
  66. h.load_file("stdrun.hoc")
  67. h.init()
  68. h.tstop = duration
  69. h.dt = dt
  70. h.run()
  71. spikes = []
  72. isis = []
  73. lastSpike = None
  74. for t in vec_nc:
  75. spikes.append(t)
  76. # print(t)
  77. if lastSpike:
  78. isis.append(t - lastSpike)
  79. lastSpike = t
  80. hz = 1000 / (h.tstop / len(spikes))
  81. print("nc: Spike times: %s" % ["%.3f" % t for t in vec_nc])
  82. print(
  83. "nc: Num spikes: %s; avg rate: %s Hz; avg isi: %s ms; std isi: %s ms"
  84. % (len(spikes), hz, np.average(isis), np.std(isis))
  85. )
  86. # assert abs((hz-rate)/rate)<0.01
  87. """
  88. scales = {
  89. "v": 0.001,
  90. "g": 1e-6,
  91. }
  92. for v in postLTP_vals:
  93. scales[v] = 1
  94. for var in scales:
  95. var_file = open("%s.dat" % var, "w")
  96. for i in range(len(vec["t"])):
  97. var_file.write("%s\t%s\n" % (vec["t"][i] / 1000, vec[var][i] * scales[var]))
  98. var_file.close()
  99. """
  100. for var in (all_states + gs + other):
  101. var_file = open("%s.dat" % var, "w")
  102. for i in range(len(vec["t"])):
  103. var_file.write("%s\t%s\n" % (vec["t"][i] / 1000, vec[var][i]))
  104. var_file.close()
  105. if not "-nogui" in sys.argv:
  106. # plot the results
  107. plt.figure()
  108. plt.title("Membrane potential")
  109. plt.plot(vec["t"], vec["v"])
  110. plt.figure()
  111. plt.title("Conductance")
  112. plt.plot(vec["t"], vec["g"], label="g")
  113. plt.legend()
  114. plt.figure()
  115. plt.title("States")
  116. for s in all_states:
  117. plt.plot(vec["t"], vec[s], label=s)
  118. plt.legend()
  119. run_sim()
  120. if not "-nogui" in sys.argv:
  121. plt.show()