polychronizationNetwork.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. # polychronizationNetwork.py
  2. #
  3. # This file is part of the SpiNNaker Izhikevich polychronization model implementation.
  4. #
  5. # Copyright (C) 2018, Author: G. Trensch
  6. #
  7. # The SpiNNaker Izhikevich polychronization model implementation is free software:
  8. # you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # It is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this application. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  22. # = SpiNNaker Izhikevich polychronization network implementation w/o STDP.
  23. # = Simulation of selected network states created with the C model implementation.
  24. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  25. import spynnaker8 as sim
  26. from pyNN.utility.plotting import Figure, Panel
  27. import pylab
  28. import os
  29. import matplotlib.pyplot as plt
  30. import numpy as np
  31. import time
  32. import seaborn
  33. SPIKEPLOT = True
  34. start_time = time.time()
  35. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  36. # = Import the network state that was generated with the C model
  37. # = Parameterize according to the simulated network and state
  38. # =
  39. # = source target weight delay
  40. # = exc_exc_connections = [ (nnn, nnn, ww.wwww, d ), () ... ]
  41. # = exc_inh_connections = [ (), () ... ]
  42. # = inh_exc_connections = [ (), () ... ]
  43. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  44. EXTERNAL_CURRENT_FILE_NAME = 'randomNetworkInput.dat' # random input
  45. from pythonConWeightDelay_after1h import * # 1st state: connections, weights, and delays
  46. FIRINGS_FILE_NAME = 'firings_after1h.dat' # network acivity data output file
  47. # from pythonConWeightDelay_after2h import * # 2nd state
  48. # FIRINGS_FILE_NAME = 'firings_after2h.dat' #
  49. # from pythonConWeightDelay_after3h import * # 3rd state
  50. # FIRINGS_FILE_NAME = 'firings_after3h.dat' #
  51. # from pythonConWeightDelay_after4h import * # 4th state
  52. # FIRINGS_FILE_NAME = 'firings_after4h.dat' #
  53. # from pythonConWeightDelay_after5h import * # 5th state
  54. # FIRINGS_FILE_NAME = 'firings_after5h.dat' #
  55. # population sizes have to correspond to the imported network state
  56. NUM_EXCITATORY_NEURONS = 800
  57. NUM_INHIBITORY_NEURONS = 200
  58. NUM_NEURONS = NUM_EXCITATORY_NEURONS + NUM_INHIBITORY_NEURONS
  59. SYNAPSES_PER_NEURON = 100
  60. CURRENT_INJECTION_VALUE = 20.0
  61. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  62. # = Simulation parameters
  63. # = The simulation is carried out in 60 cycles with 1000 milliseconds simulation time each
  64. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  65. SIM_TIME_TOTAL = 1000 * 60 # [ms]
  66. SIM_TIME_SINGLE = 1000 # [ms]
  67. SIM_CYCLES = SIM_TIME_TOTAL / SIM_TIME_SINGLE
  68. SIM_TIME_STEP = 1.0 # [ms] (1ms = real time)
  69. SIM_MIN_DELAY = 1.0
  70. SIM_MAX_DELAY = 144.0
  71. SIM_NEURONS_PER_CORE = 70
  72. sim.setup( timestep = SIM_TIME_STEP, min_delay= SIM_MIN_DELAY, max_delay = SIM_MAX_DELAY )
  73. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  74. # = Neuron model and Izhikevich neuron parameters
  75. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  76. NEURON_MODEL = sim.Izhikevich
  77. # Regular spiking type Izhikevich neuron
  78. NEURON_PARAMS_EXCITATORY = { 'a': 0.02
  79. , 'b': 0.2
  80. , 'c': -65.0
  81. , 'd': 8.0
  82. , 'v_init': -65.0
  83. , 'u_init': -65.0 * 0.2 # according to the polychronization model
  84. , 'i_offset': 0.0
  85. }
  86. # Fast spiking type Izhikevich neuron
  87. NEURON_PARAMS_INHIBITORY = { 'a': 0.1
  88. , 'b': 0.2
  89. , 'c': -65.0
  90. , 'd': 2.0
  91. , 'v_init': -65.0
  92. , 'u_init': -65.0 * 0.2 # according to the polychronization model
  93. , 'i_offset': 0.0
  94. }
  95. sim.set_number_of_neurons_per_core( NEURON_MODEL, SIM_NEURONS_PER_CORE )
  96. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  97. # = Populations: create the two populations of the polychronization model
  98. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  99. pop_exc = sim.Population( NUM_EXCITATORY_NEURONS, NEURON_MODEL(**NEURON_PARAMS_EXCITATORY) )
  100. pop_inh = sim.Population( NUM_INHIBITORY_NEURONS, NEURON_MODEL(**NEURON_PARAMS_INHIBITORY) )
  101. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  102. # = Populations: create two spike source arrays for emulating external current and two empty lists,
  103. # = filled at begin of every cycle with the spike sequence, that is, the random input data
  104. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  105. spikes_inp_exc = list()
  106. for i in range( NUM_EXCITATORY_NEURONS ):
  107. spikes_inp_exc.append( [] )
  108. pop_inp_exc = sim.Population( NUM_EXCITATORY_NEURONS, sim.SpikeSourceArray( spike_times = spikes_inp_exc ) )
  109. spikes_inp_inh = list()
  110. for i in range( NUM_INHIBITORY_NEURONS ):
  111. spikes_inp_inh.append( [] )
  112. pop_inp_inh = sim.Population( NUM_INHIBITORY_NEURONS, sim.SpikeSourceArray( spike_times = spikes_inp_inh ) )
  113. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  114. # = Projections: set up the poylchronization network
  115. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  116. proj_exc_exc = sim.Projection( pop_exc, pop_exc
  117. , sim.FromListConnector( exc_exc_connections )
  118. , sim.StaticSynapse()
  119. , receptor_type = "excitatory" )
  120. proj_exc_inh = sim.Projection( pop_exc, pop_inh
  121. , sim.FromListConnector( exc_inh_connections )
  122. , sim.StaticSynapse()
  123. , receptor_type = "excitatory" )
  124. proj_inh_exc = sim.Projection( pop_inh, pop_exc
  125. , sim.FromListConnector( inh_exc_connections )
  126. , sim.StaticSynapse()
  127. , receptor_type = "inhibitory" )
  128. proj_inp_exc = sim.Projection( pop_inp_exc, pop_exc
  129. , sim.OneToOneConnector()
  130. , sim.StaticSynapse(weight = CURRENT_INJECTION_VALUE)
  131. , receptor_type = "excitatory" )
  132. proj_inp_inh = sim.Projection( pop_inp_inh, pop_inh
  133. , sim.OneToOneConnector()
  134. , sim.StaticSynapse(weight = CURRENT_INJECTION_VALUE)
  135. , receptor_type = "excitatory" )
  136. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  137. # = Set up recording
  138. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  139. pop_exc.record('spikes')
  140. pop_inh.record('spikes')
  141. pop_inp_exc.record('spikes')
  142. pop_inp_inh.record('spikes')
  143. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  144. # = Open files
  145. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  146. inputCurrentFile = open( EXTERNAL_CURRENT_FILE_NAME, 'r' )
  147. if os.path.isfile( FIRINGS_FILE_NAME ):
  148. os.remove( FIRINGS_FILE_NAME )
  149. outputSpikesFile = open( FIRINGS_FILE_NAME, 'a' )
  150. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  151. # = Set spike plot properties
  152. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  153. if SPIKEPLOT:
  154. seaborn.set()
  155. seaborn.despine()
  156. seaborn.set_style("white")
  157. seaborn.axes_style("darkgrid")
  158. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  159. # = Run simulation
  160. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  161. for cycle in range( SIM_CYCLES ):
  162. simStartTime = SIM_TIME_SINGLE * cycle
  163. spikes_inp_exc = list()
  164. for i in range(NUM_EXCITATORY_NEURONS):
  165. spikes_inp_exc.append([])
  166. spikes_inp_inh = list()
  167. for i in range(NUM_INHIBITORY_NEURONS):
  168. spikes_inp_inh.append([])
  169. # load random input date from file for this cycle
  170. for tickCount in range( SIM_TIME_SINGLE ):
  171. inputDataForThisTick = inputCurrentFile.readline().split()
  172. inputNeuronForThisTick = int(inputDataForThisTick[2])
  173. if( inputNeuronForThisTick < NUM_EXCITATORY_NEURONS ):
  174. spikes_inp_exc[inputNeuronForThisTick].append( tickCount + simStartTime )
  175. else:
  176. inputNeuronForThisTick = inputNeuronForThisTick - NUM_EXCITATORY_NEURONS
  177. spikes_inp_inh[inputNeuronForThisTick].append( tickCount + simStartTime )
  178. # set spike source array with random input data fo this cycle
  179. pop_inp_exc.set( spike_times = spikes_inp_exc )
  180. pop_inp_inh.set( spike_times = spikes_inp_inh )
  181. sim.run( SIM_TIME_SINGLE )
  182. # retrieve input spikes from populations, i.e., from spike source arrays
  183. spikesInpToExcPop = pop_inp_exc.get_data()
  184. spikesInpToInhPop = pop_inp_inh.get_data()
  185. # retrieve output spikes from populations and clear segment
  186. spikesExcPop = pop_exc.get_data('spikes', clear = True)
  187. spikesInhPop = pop_inh.get_data('spikes', clear = True)
  188. simEndTime = sim.get_current_time()
  189. print( simStartTime, simEndTime )
  190. if SPIKEPLOT:
  191. Figure( Panel(spikesInpToInhPop.segments[0].spiketrains
  192. , xticks = True, yticks = True, markersize = 2.0, xlim = (simStartTime, simEndTime))
  193. , Panel(spikesInpToExcPop.segments[0].spiketrains
  194. , xticks = True, yticks = True, markersize = 2.0, xlim = (simStartTime, simEndTime))
  195. , Panel(spikesInhPop.segments[0].spiketrains
  196. , xticks = True, yticks = True, markersize = 2.0, xlim = (simStartTime, simEndTime))
  197. , Panel(spikesExcPop.segments[0].spiketrains
  198. , xticks = True, yticks = True, markersize = 2.0, xlim = (simStartTime, simEndTime), xlabel = "Time [ms]")
  199. , title="Plochronization Network", annotations="Simulated with {}".format(sim.name())
  200. )
  201. plt.pause(0.1)
  202. # write firings to file
  203. firings = list()
  204. for i in range( SIM_TIME_SINGLE ):
  205. firings.append([])
  206. for neuron in range( NUM_EXCITATORY_NEURONS ):
  207. for spikeTimeIdx in range( len(spikesExcPop.segments[0].spiketrains[neuron]) ):
  208. firingTime = int(spikesExcPop.segments[0].spiketrains[neuron][spikeTimeIdx]) - simStartTime
  209. firings[firingTime].append( neuron )
  210. for neuron in range( NUM_INHIBITORY_NEURONS ):
  211. for spikeTimeIdx in range( len(spikesInhPop.segments[0].spiketrains[neuron]) ):
  212. firingTime = int(spikesInhPop.segments[0].spiketrains[neuron][spikeTimeIdx]) - simStartTime
  213. firings[firingTime].append( neuron + NUM_EXCITATORY_NEURONS )
  214. for i in range( SIM_TIME_SINGLE ):
  215. simCycle = str( '{:06d}'.format(cycle) )
  216. tickInCycle = str('{:06d}'.format(i))
  217. for n in firings[i]:
  218. neuronId = str('{:03d}'.format(n))
  219. line = ' '.join([simCycle, tickInCycle, neuronId, "\n"])
  220. outputSpikesFile.writelines( line )
  221. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  222. # = End simulation, close files, show spike plot
  223. # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
  224. sim.end()
  225. inputCurrentFile.close()
  226. outputSpikesFile.close()
  227. print("Simulation time: %s seconds" % (time.time() - start_time))
  228. if SPIKEPLOT:
  229. pylab.show()