Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.65 KB | None | 0 0
  1. import os
  2. import traceback
  3. import nest
  4. from time import time
  5. from multiprocessing import cpu_count
  6. from NEST.functions import Functions
  7. from NEST.functions import Parameters
  8. from GRAS.tests_runner import convert_to_hdf5, plot_results
  9. from GRAS import plot_results as draw
  10.  
  11.  
  12. class V3(Functions):
  13. def __init__(self, parameters, iteration):
  14. """
  15. ToDo add info
  16. Args:
  17. parameters (Parameters):
  18. iteration (int):
  19. """
  20. nest.ResetKernel()
  21. nest.SetKernelStatus({'data_path': "dat",
  22. 'print_time': True,
  23. 'resolution': 0.025,
  24. 'overwrite_files': True,
  25. 'data_prefix': f"{iteration}_",
  26. 'total_num_virtual_procs': cpu_count(),
  27. 'rng_seeds': [int(time() * 10000 % 10000)] * cpu_count()})
  28.  
  29. super().__init__(parameters)
  30. self.P = parameters
  31.  
  32. # checking
  33.  
  34. self.init_network()
  35. self.simulate()
  36. self.resave()
  37.  
  38. def init_network(self):
  39. """
  40. TODO add info
  41. """
  42.  
  43. if self.P.air:
  44. self.P.toe = True
  45.  
  46. inh_coef = self.P.inh / 100
  47. quadru_coef = 0.5 if self.P.ped == 4 else 1
  48. sero_coef = 5.3 if self.P.ht5 else 1
  49. air_coef = 0.5 if self.P.air else 1
  50. toe_coef = 0.5 if self.P.toe else 1
  51.  
  52. neurons_in_ip = 196 # number of neurons in interneuronal pool
  53. neurons_in_moto_extensor = 210 # motoneurons number
  54. neurons_in_moto_flexor = 180
  55. neurons_in_aff_ip = 196 # number of neurons in interneuronal pool
  56. neurons_in_afferent = 120 # number of neurons in afferent
  57.  
  58. # groups of neurons
  59. EES = self.form_group("EES")
  60. E1 = self.form_group("E1")
  61. E2 = self.form_group("E2")
  62. E3 = self.form_group("E3")
  63. E4 = self.form_group("E4")
  64. E5 = self.form_group("E5")
  65.  
  66. CV1 = self.form_group("CV1")
  67. CV2 = self.form_group("CV2")
  68. CV3 = self.form_group("CV3")
  69. CV4 = self.form_group("CV4")
  70. CV5 = self.form_group("CV5")
  71. CD4 = self.form_group("CD4")
  72. CD5 = self.form_group("CD5")
  73.  
  74. OM1_0 = self.form_group("OM1_0")
  75. OM1_1 = self.form_group("OM1_1")
  76. OM1_2_E = self.form_group("OM1_2_E")
  77. OM1_2_F = self.form_group("OM1_2_F")
  78. OM1_3 = self.form_group("OM1_3")
  79.  
  80. OM2_0 = self.form_group("OM2_0")
  81. OM2_1 = self.form_group("OM2_1")
  82. OM2_2_E = self.form_group("OM2_2_E")
  83. OM2_2_F = self.form_group("OM2_2_F")
  84. OM2_3 = self.form_group("OM2_3")
  85.  
  86. OM3_0 = self.form_group("OM3_0")
  87. OM3_1 = self.form_group("OM3_1")
  88. OM3_2_E = self.form_group("OM3_2_E")
  89. OM3_2_F = self.form_group("OM3_2_F")
  90. OM3_3 = self.form_group("OM3_3")
  91.  
  92. OM4_0 = self.form_group("OM4_0")
  93. OM4_1 = self.form_group("OM4_1")
  94. OM4_2_E = self.form_group("OM4_2_E")
  95. OM4_2_F = self.form_group("OM4_2_F")
  96. OM4_3 = self.form_group("OM4_3")
  97.  
  98. OM5_0 = self.form_group("OM5_0")
  99. OM5_1 = self.form_group("OM5_1")
  100. OM5_2_E = self.form_group("OM5_2_E")
  101. OM5_2_F = self.form_group("OM5_2_F")
  102. OM5_3 = self.form_group("OM5_3")
  103.  
  104. MN_E = self.form_group("MN_E", neurons_in_moto_extensor, d_distr="bimodal")
  105. MN_F = self.form_group("MN_F", neurons_in_moto_flexor, d_distr="bimodal")
  106.  
  107. Ia_E_aff = self.form_group("Ia_E_aff", neurons_in_afferent)
  108. Ia_F_aff = self.form_group("Ia_F_aff", neurons_in_afferent)
  109.  
  110. R_E = self.form_group("R_E")
  111. R_F = self.form_group("R_F")
  112.  
  113. Ia_E_pool = self.form_group("Ia_E_pool", neurons_in_aff_ip)
  114. Ia_F_pool = self.form_group("Ia_F_pool", neurons_in_aff_ip)
  115.  
  116. eIP_E_0 = self.form_group("eIP_E_0", int(neurons_in_ip / 7))
  117. eIP_E_1 = self.form_group("eIP_E_1", int(neurons_in_ip / 7))
  118. eIP_F = self.form_group("eIP_F", neurons_in_ip)
  119.  
  120. iIP_E = self.form_group("iIP_E", neurons_in_ip)
  121. iIP_F = self.form_group("iIP_F", neurons_in_ip)
  122.  
  123. self.connect_spike_generator(EES, rate=self.P.EES)
  124. self.connect_noise_generator(CV1, rate=5000, t_end=self.P.skin_stim - 2)
  125. self.connect_noise_generator(CV2, rate=5000, t_start=self.P.skin_stim, t_end=2 * self.P.skin_stim - 2)
  126. self.connect_noise_generator(CV3, rate=5000, t_start=2 * self.P.skin_stim, t_end=3 * self.P.skin_stim - 2)
  127. self.connect_noise_generator(CV4, rate=5000, t_start=3 * self.P.skin_stim, t_end=5 * self.P.skin_stim - 2)
  128. self.connect_noise_generator(CV5, rate=5000, t_start=5 * self.P.skin_stim, t_end=6 * self.P.skin_stim - 2)
  129. self.connect_noise_generator(iIP_F, rate=3000, t_start=6 * self.P.skin_stim,
  130. t_end=6 * self.P.skin_stim + self.P.flexor_time - 5)
  131. self.connect_noise_generator(MN_E, rate=200, weight=90)
  132. self.connect_noise_generator(MN_F, rate=200, weight=125)
  133.  
  134. # connectomes
  135. self.connect_fixed_outdegree(EES, E1, 1, 370, no_distr=True)
  136. self.connect_fixed_outdegree(E1, E2, 1, 80, no_distr=True)
  137. self.connect_fixed_outdegree(E2, E3, 1, 80, no_distr=True)
  138. self.connect_fixed_outdegree(E3, E4, 1, 80, no_distr=True)
  139. self.connect_fixed_outdegree(E4, E5, 1, 80, no_distr=True)
  140.  
  141. self.connect_one_to_all(CV1, iIP_E, 0.5, 20)
  142. self.connect_one_to_all(CV2, iIP_E, 0.5, 20)
  143. self.connect_one_to_all(CV3, iIP_E, 0.5, 20)
  144. self.connect_one_to_all(CV4, iIP_E, 0.5, 20)
  145. self.connect_one_to_all(CV5, iIP_E, 0.5, 20)
  146.  
  147. def connect_to_ip(neuron, weight, delay):
  148. self.connect_fixed_outdegree(neuron, eIP_E_0, delay + 1.5, weight, neurons_in_ip)
  149. self.connect_fixed_outdegree(eIP_E_0, eIP_E_1, 1.2, 10, neurons_in_ip)
  150.  
  151. def connect_to_moto(weight):
  152. self.connect_fixed_outdegree(eIP_E_0, MN_E, 1.5, weight, neurons_in_moto_extensor)
  153. self.connect_fixed_outdegree(eIP_E_1, MN_E, 2.5, weight * 1.5, neurons_in_moto_extensor)
  154.  
  155. # extensor
  156.  
  157. coef2 = 2
  158.  
  159. connect_to_ip(OM1_2_E, 16.5 * coef2, 1)
  160. connect_to_ip(OM2_2_E, 16.5 * coef2, 1.75)
  161. connect_to_ip(OM3_2_E, 13 * coef2, 2.25)
  162. connect_to_ip(OM4_2_E, 16 * coef2, 2.8)
  163. connect_to_ip(OM5_2_E, 16 * coef2, 3.2)
  164.  
  165. connect_to_moto(23)
  166.  
  167. # flexor
  168. self.connect_fixed_outdegree(OM1_2_F, eIP_F, 1, 7, neurons_in_ip)
  169. self.connect_fixed_outdegree(OM2_2_F, eIP_F, 1, 7, neurons_in_ip)
  170. self.connect_fixed_outdegree(OM3_2_F, eIP_F, 1, 7, neurons_in_ip)
  171. self.connect_fixed_outdegree(OM4_2_F, eIP_F, 1, 7, neurons_in_ip)
  172. self.connect_fixed_outdegree(OM5_2_F, eIP_F, 1, 7, neurons_in_ip)
  173.  
  174. self.connect_fixed_outdegree(eIP_F, MN_F, 1, 3, neurons_in_moto_flexor)
  175.  
  176. # self.connect_fixed_outdegree(MN_F, R_F, 0.5, 2)
  177. # self.connect_fixed_outdegree(R_F, MN_F, 2, 0, neurons_in_moto_flexor)
  178.  
  179. self.connect_fixed_outdegree(Ia_F_aff, MN_F, 2, 0.5, neurons_in_moto_flexor)
  180. self.connect_fixed_outdegree(Ia_E_pool, MN_F, 1, -50, neurons_in_ip)
  181.  
  182. # OM 1
  183. # input from EES group 1
  184. self.connect_fixed_outdegree(E1, OM1_0, 3, 0.65)
  185. # input from sensory
  186. self.connect_one_to_all(CV1, OM1_0, 2.5, 0.9 * quadru_coef * sero_coef * toe_coef)
  187. self.connect_one_to_all(CV2, OM1_0, 2.5, 0.9 * quadru_coef * sero_coef * toe_coef)
  188. # [inhibition]
  189. self.connect_one_to_all(CV3, OM1_3, 2.5, 100)
  190. self.connect_one_to_all(CV4, OM1_3, 2.5, 100)
  191. self.connect_one_to_all(CV5, OM1_3, 2.5, 100)
  192. # inner connectomes
  193. self.connect_fixed_outdegree(OM1_0, OM1_1, 1.5, 50)
  194. self.connect_fixed_outdegree(OM1_1, OM1_2_E, 2, 7.5)
  195. self.connect_fixed_outdegree(OM1_1, OM1_2_F, 2.4, 8)
  196. self.connect_fixed_outdegree(OM1_1, OM1_3, 1.5, 3)
  197. self.connect_fixed_outdegree(OM1_2_E, OM1_1, 1.5, 6.8)
  198. self.connect_fixed_outdegree(OM1_2_F, OM1_1, 2.5, 30)
  199. self.connect_fixed_outdegree(OM1_2_E, OM1_3, 1.5, 8)
  200. self.connect_fixed_outdegree(OM1_2_F, OM1_3, 0.4, 8)
  201. self.connect_fixed_outdegree(OM1_3, OM1_1, 1.5, -2 * inh_coef)
  202. self.connect_fixed_outdegree(OM1_3, OM1_2_E, 1.5, -2 * inh_coef)
  203. self.connect_fixed_outdegree(OM1_3, OM1_2_F, 1, -1 * inh_coef)
  204. # output to OM2
  205. self.connect_fixed_outdegree(OM1_2_F, OM2_2_F, 1, 10)
  206.  
  207. # OM 2
  208. # input from EES group 2
  209. self.connect_fixed_outdegree(E2, OM2_0, 3, 0.47)
  210. # input from sensory [CV]
  211. self.connect_one_to_all(CV2, OM2_0, 2.5, 1 * quadru_coef * sero_coef * toe_coef)
  212. self.connect_one_to_all(CV3, OM2_0, 2.5, 1 * quadru_coef * sero_coef * toe_coef)
  213. # [inhibition]
  214. self.connect_one_to_all(CV4, OM2_3, 3, 80)
  215. self.connect_one_to_all(CV5, OM2_3, 3, 80)
  216. # # inner connectomes
  217. self.connect_fixed_outdegree(OM2_0, OM2_1, 1.5, 35)
  218. self.connect_fixed_outdegree(OM2_1, OM2_2_E, 2, 7.3)
  219. self.connect_fixed_outdegree(OM2_1, OM2_2_F, 2.4, 8)
  220. self.connect_fixed_outdegree(OM2_1, OM2_3, 1.5, 3)
  221. self.connect_fixed_outdegree(OM2_2_E, OM2_1, 1.5, 6.4)
  222. self.connect_fixed_outdegree(OM2_2_F, OM2_1, 2.5, 30)
  223. self.connect_fixed_outdegree(OM2_2_E, OM2_3, 1.5, 8)
  224. self.connect_fixed_outdegree(OM2_2_F, OM2_3, 0.4, 8)
  225. self.connect_fixed_outdegree(OM2_3, OM2_1, 1.5, -2 * inh_coef)
  226. self.connect_fixed_outdegree(OM2_3, OM2_2_E, 1.5, -2 * inh_coef)
  227. self.connect_fixed_outdegree(OM2_3, OM2_2_F, 1, -1 * inh_coef)
  228. # output to OM3
  229. self.connect_fixed_outdegree(OM2_2_F, OM3_2_F, 1, 10)
  230.  
  231. # OM 3
  232. # input from EES group 3
  233. self.connect_fixed_outdegree(E3, OM3_0, 3, 0.45)
  234. # input from sensory [CV]
  235. self.connect_one_to_all(CV3, OM3_0, 2.5, 0.96 * quadru_coef * sero_coef * toe_coef)
  236. self.connect_one_to_all(CV4, OM3_0, 2.5, 0.96 * quadru_coef * sero_coef * toe_coef)
  237. # [inhibition]
  238. self.connect_one_to_all(CV5, OM3_3, 3, 80)
  239. # input from sensory [CD]
  240. self.connect_one_to_all(CD4, OM3_0, 3, 11)
  241. # inner connectomes
  242. self.connect_fixed_outdegree(OM3_0, OM3_1, 1.5, 40)
  243. self.connect_fixed_outdegree(OM3_1, OM3_2_E, 2, 7.2)
  244. self.connect_fixed_outdegree(OM3_1, OM3_2_F, 2.4, 8)
  245. self.connect_fixed_outdegree(OM3_1, OM3_3, 1.5, 3)
  246. self.connect_fixed_outdegree(OM3_2_E, OM3_1, 1.5, 6.3)
  247. self.connect_fixed_outdegree(OM3_2_F, OM3_1, 2.5, 30)
  248. self.connect_fixed_outdegree(OM3_2_E, OM3_3, 1.5, 8)
  249. self.connect_fixed_outdegree(OM3_2_F, OM3_3, 0.4, 8)
  250. self.connect_fixed_outdegree(OM3_3, OM3_1, 1.5, -2 * inh_coef)
  251. self.connect_fixed_outdegree(OM3_3, OM3_2_E, 1.5, -2 * inh_coef)
  252. self.connect_fixed_outdegree(OM3_3, OM3_2_F, 1, -1 * inh_coef)
  253. # output to OM4
  254. self.connect_fixed_outdegree(OM3_2_F, OM4_2_F, 1, 10)
  255.  
  256. # OM 4
  257. # input from EES group 4
  258. self.connect_fixed_outdegree(E4, OM4_0, 3, 0.5)
  259. # input from sensory [CV]
  260. self.connect_one_to_all(CV4, OM4_0, 3.5, 0.96 * quadru_coef * sero_coef * air_coef)
  261. self.connect_one_to_all(CV5, OM4_0, 3.5, 0.96 * quadru_coef * sero_coef * air_coef)
  262. # input from sensory [CD]
  263. self.connect_one_to_all(CD4, OM4_0, 3, 11)
  264. self.connect_one_to_all(CD5, OM4_0, 3, 11)
  265. # inner connectomes
  266. self.connect_fixed_outdegree(OM4_0, OM4_1, 1.5, 40)
  267. self.connect_fixed_outdegree(OM4_1, OM4_2_E, 2, 6.7)
  268. self.connect_fixed_outdegree(OM4_1, OM4_2_F, 2.4, 8)
  269. self.connect_fixed_outdegree(OM4_1, OM4_3, 1.5, 3)
  270. self.connect_fixed_outdegree(OM4_2_E, OM4_1, 1.5, 5.5)
  271. self.connect_fixed_outdegree(OM4_2_F, OM4_1, 2.5, 30)
  272. self.connect_fixed_outdegree(OM4_2_E, OM4_3, 1.5, 8)
  273. self.connect_fixed_outdegree(OM4_2_F, OM4_3, 0.4, 8)
  274. self.connect_fixed_outdegree(OM4_3, OM4_1, 1.5, -2 * inh_coef)
  275. self.connect_fixed_outdegree(OM4_3, OM4_2_E, 1.5, -2 * inh_coef)
  276. self.connect_fixed_outdegree(OM4_3, OM4_2_F, 1, -1 * inh_coef)
  277. # output to OM5
  278. self.connect_fixed_outdegree(OM4_2_F, OM5_2_F, 1, 10)
  279.  
  280. # OM 5
  281. # input from EES group 5
  282. self.connect_fixed_outdegree(E5, OM5_0, 3.5, 0.46)
  283. # input from sensory [CV]
  284. self.connect_one_to_all(CV5, OM5_0, 3, 1 * quadru_coef * sero_coef * air_coef)
  285. # input from sensory [CD]
  286. self.connect_one_to_all(CD5, OM5_0, 3, 11)
  287. # inner connectomes
  288. self.connect_fixed_outdegree(OM5_0, OM5_1, 1.5, 40)
  289. self.connect_fixed_outdegree(OM5_1, OM5_2_E, 2, 5)
  290. self.connect_fixed_outdegree(OM5_1, OM5_2_F, 2.4, 8)
  291. self.connect_fixed_outdegree(OM5_1, OM5_3, 1.5, 3)
  292. self.connect_fixed_outdegree(OM5_2_E, OM5_1, 1.5, 3.5)
  293. self.connect_fixed_outdegree(OM5_2_F, OM5_1, 2.5, 30)
  294. self.connect_fixed_outdegree(OM5_2_E, OM5_3, 1.5, 8)
  295. self.connect_fixed_outdegree(OM5_2_F, OM5_3, 0.4, 8)
  296. self.connect_fixed_outdegree(OM5_3, OM5_1, 1.5, -3 * inh_coef)
  297. self.connect_fixed_outdegree(OM5_3, OM5_2_E, 1.5, -3 * inh_coef)
  298. self.connect_fixed_outdegree(OM5_3, OM5_2_F, 1, -1 * inh_coef)
  299.  
  300. # reflex arc
  301. self.connect_fixed_outdegree(iIP_E, eIP_F, 3.3, -14, neurons_in_ip)
  302. connect_to_ip(iIP_F, -40, 0.5)
  303. # self.connect_fixed_outdegree(iIP_F, eIP_E, 0.5, -40, neurons_in_ip)
  304.  
  305. self.connect_fixed_outdegree(iIP_E, OM1_2_F, 0.5, -3, neurons_in_ip)
  306. self.connect_fixed_outdegree(iIP_E, OM2_2_F, 0.5, -3, neurons_in_ip)
  307. self.connect_fixed_outdegree(iIP_E, OM3_2_F, 0.5, -3, neurons_in_ip)
  308. self.connect_fixed_outdegree(iIP_E, OM4_2_F, 0.5, -3, neurons_in_ip)
  309.  
  310. self.connect_fixed_outdegree(EES, Ia_E_aff, 1, 500)
  311. self.connect_fixed_outdegree(EES, Ia_F_aff, 1, 500)
  312.  
  313. self.connect_fixed_outdegree(iIP_E, Ia_E_pool, 1, 30, neurons_in_ip)
  314. self.connect_fixed_outdegree(iIP_F, Ia_F_pool, 1, 30, neurons_in_ip)
  315.  
  316. self.connect_fixed_outdegree(Ia_E_pool, Ia_F_pool, 1, -1, neurons_in_ip)
  317. self.connect_fixed_outdegree(Ia_F_pool, MN_E, 1, -10, neurons_in_ip)
  318. self.connect_fixed_outdegree(Ia_F_pool, Ia_E_pool, 1, -1, neurons_in_ip)
  319. self.connect_fixed_outdegree(Ia_E_aff, MN_E, 3.4, 26, neurons_in_moto_extensor)
  320.  
  321. self.connect_fixed_outdegree(R_E, R_F, 2, -1)
  322. self.connect_fixed_outdegree(R_F, R_E, 2, -1)
  323.  
  324.  
  325. if __name__ == "__main__":
  326. parameters = Parameters()
  327. parameters.tests = 10
  328. parameters.steps = 1
  329. parameters.cms = 21
  330. parameters.EES = 40
  331. parameters.inh = 100
  332. parameters.ped = 2
  333. parameters.ht5 = False
  334. parameters.air = False
  335. parameters.toe = False
  336. parameters.save_all = False
  337.  
  338. save_folder = f"{os.getcwd()}/dat"
  339.  
  340. for i in range(parameters.tests):
  341. while True:
  342. try:
  343. V3(parameters, iteration=i)
  344. break
  345. except Exception:
  346. print(traceback.format_exc())
  347. continue
  348.  
  349. convert_to_hdf5(save_folder)
  350. plot_results(save_folder, ees_hz=parameters.EES)
  351. draw.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement