make_spike_extraction_test_data.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. def main():
  2. from brian2 import start_scope,mvolt,ms,NeuronGroup,StateMonitor,run
  3. import matplotlib.pyplot as plt
  4. import neo
  5. import quantities as pq
  6. start_scope()
  7. # Izhikevich neuron parameters.
  8. a = 0.02/ms
  9. b = 0.2/ms
  10. c = -65*mvolt
  11. d = 6*mvolt/ms
  12. I = 4*mvolt/ms
  13. # Standard Izhikevich neuron equations.
  14. eqs = '''
  15. dv/dt = 0.04*v**2/(ms*mvolt) + (5/ms)*v + 140*mvolt/ms - u + I : volt
  16. du/dt = a*((b*v) - u) : volt/second
  17. '''
  18. reset = '''
  19. v = c
  20. u += d
  21. '''
  22. # Setup and run simulation.
  23. G = NeuronGroup(1, eqs, threshold='v>30*mvolt', reset='v = -70*mvolt')
  24. G.v = -65*mvolt
  25. G.u = b*G.v
  26. M = StateMonitor(G, 'v', record=True)
  27. run(300*ms)
  28. # Store results in neo format.
  29. vm = neo.core.AnalogSignal(M.v[0], units=pq.V, sampling_period=0.1*pq.ms)
  30. # Plot results.
  31. plt.figure()
  32. plt.plot(vm.times*1000,vm*1000) # Plot mV and ms instead of V and s.
  33. plt.xlabel('Time (ms)')
  34. plt.ylabel('mv')
  35. # Save results.
  36. iom = neo.io.PyNNNumpyIO('spike_extraction_test_data')
  37. block = neo.core.Block()
  38. segment = neo.core.Segment()
  39. segment.analogsignals.append(vm)
  40. block.segments.append(segment)
  41. iom.write(block)
  42. # Load results.
  43. iom2 = neo.io.PyNNNumpyIO('spike_extraction_test_data.npz')
  44. data = iom2.read()
  45. vm = data[0].segments[0].analogsignals[0]
  46. # Plot results.
  47. # The two figures should match.
  48. plt.figure()
  49. plt.plot(vm.times*1000,vm*1000) # Plot mV and ms instead of V and s.
  50. plt.xlabel('Time (ms)')
  51. plt.ylabel('mv')
  52. if __name__ == '__main__':
  53. main()