SpikeNet 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
SpikeNet/__init__.py ADDED
@@ -0,0 +1,811 @@
1
+ import os
2
+ import sys
3
+ import random
4
+
5
+ __version__ = "1.0.0"
6
+
7
+ _neurons = []
8
+ _sinapce = []
9
+
10
+ user_script_path = os.path.abspath(sys.argv[0])
11
+ user_script_dir = os.path.dirname(user_script_path)
12
+
13
+
14
+ def LoadBrain(file_path):
15
+ global _neurons
16
+ global _sinapce
17
+ with open(file_path, "r", encoding="utf-8") as f:
18
+ file_content = f.read()
19
+ if "Neurons" in file_content and "NeuronsEnd" in file_content:
20
+ _neurons.append(file_content.split("Neurons", 1)[1].split("NeuronsEnd", 1)[0])
21
+ if "Sinapce" in file_content and "SinapceEnd" in file_content:
22
+ _sinapce.append(file_content.split("Sinapce", 1)[1].split("SinapceEnd", 1)[0])
23
+ return
24
+
25
+
26
+ def LoadBrainText(Braintxt):
27
+ global _neurons
28
+ global _sinapce
29
+ file_content = Braintxt
30
+ if "Neurons" in file_content and "NeuronsEnd" in file_content:
31
+ _neurons.append(file_content.split("Neurons", 1)[1].split("NeuronsEnd", 1)[0])
32
+ if "Sinapce" in file_content and "SinapceEnd" in file_content:
33
+ _sinapce.append(file_content.split("Sinapce", 1)[1].split("SinapceEnd", 1)[0])
34
+ return
35
+
36
+
37
+ def CreateABrainTF(file_path_neurons, file_path_sinapce, file_name="Brain.brain"):
38
+ with open(file_path_neurons, "r", encoding="utf-8") as f:
39
+ Neurons_Connect = f.read()
40
+ with open(file_path_sinapce, "r", encoding="utf-8") as f:
41
+ Sinapce_Connect = f.read()
42
+ BrainTxt = ""
43
+ BrainTxt += "Neurons\n"
44
+ BrainTxt += Neurons_Connect
45
+ BrainTxt += "\nNeuronsEnd"
46
+ BrainTxt += "\nSinapce\n"
47
+ BrainTxt += Sinapce_Connect
48
+ BrainTxt += "\nSinapceEnd"
49
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
50
+ f.write(BrainTxt)
51
+ return BrainTxt
52
+
53
+
54
+ def CreateABrainTT(TextNeurons, TextSinapce, file_name="Brain.brain"):
55
+ BrainTxt = ""
56
+ BrainTxt += "Neurons\n"
57
+ BrainTxt += TextNeurons
58
+ BrainTxt += "\nNeuronsEnd"
59
+ BrainTxt += "\nSinapce\n"
60
+ BrainTxt += TextSinapce
61
+ BrainTxt += "\nSinapceEnd"
62
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
63
+ f.write(BrainTxt)
64
+ return BrainTxt
65
+
66
+
67
+ def CreateBrainFeedForward(N_SENS=16, N_MOT=4, LAYERS="100,50,20", INHIB=0.2, file_name="Brain.brain"):
68
+ NewNeuron = ""
69
+ NewSynapse = ""
70
+ nid = 0
71
+ syn_id = 0
72
+
73
+ layer_sizes = [int(x) for x in LAYERS.split(",")]
74
+
75
+ sensory_start = nid
76
+ for i in range(N_SENS):
77
+ NewNeuron += f"{nid},excitatory,sensory,0\n"
78
+ nid += 1
79
+ sensory_end = nid
80
+
81
+ layer_ranges = []
82
+ for L_idx, size in enumerate(layer_sizes):
83
+ start = nid
84
+ for i in range(size):
85
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
86
+ NewNeuron += f"{nid},{kind},hidden{L_idx+1},{L_idx+1}\n"
87
+ nid += 1
88
+ layer_ranges.append((start, nid))
89
+
90
+ motor_start = nid
91
+ for i in range(N_MOT):
92
+ NewNeuron += f"{nid},excitatory,motor,{len(layer_sizes)+1}\n"
93
+ nid += 1
94
+ motor_end = nid
95
+
96
+ all_layers = [(sensory_start, sensory_end)] + layer_ranges + [(motor_start, motor_end)]
97
+
98
+ neuron_types = {}
99
+ for line in NewNeuron.strip().split("\n"):
100
+ parts = line.split(",")
101
+ neuron_types[int(parts[0])] = parts[1]
102
+
103
+ for L_idx in range(len(all_layers) - 1):
104
+ pre_start, pre_end = all_layers[L_idx]
105
+ post_start, post_end = all_layers[L_idx + 1]
106
+
107
+ for pre in range(pre_start, pre_end):
108
+ for post in range(post_start, post_end):
109
+ if neuron_types[pre] == "inhibitory":
110
+ weight = -abs(random.gauss(0.5, 0.15))
111
+ stype = "inhibitory"
112
+ else:
113
+ weight = abs(random.gauss(0.6, 0.2))
114
+ stype = "excitatory"
115
+
116
+ delay = random.uniform(1.0, 3.0)
117
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
118
+ syn_id += 1
119
+
120
+ BrainTxt = ""
121
+ BrainTxt += "Neurons\n"
122
+ BrainTxt += NewNeuron
123
+ BrainTxt += "\nNeuronsEnd"
124
+ BrainTxt += "\nSinapce\n"
125
+ BrainTxt += NewSynapse
126
+ BrainTxt += "\nSinapceEnd"
127
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
128
+ f.write(BrainTxt)
129
+ return BrainTxt
130
+
131
+
132
+ def CreateBrainFeedForwardFeedBack(N_SENS=16, N_MOT=4, LAYERS="100,50,20", INHIB=0.2, FB_STRENGTH=0.5, file_name="Brain.brain"):
133
+ NewNeuron = ""
134
+ NewSynapse = ""
135
+ nid = 0
136
+ syn_id = 0
137
+
138
+ layer_sizes = [int(x) for x in LAYERS.split(",")]
139
+
140
+ sensory_start = nid
141
+ for i in range(N_SENS):
142
+ NewNeuron += f"{nid},excitatory,sensory,0\n"
143
+ nid += 1
144
+ sensory_end = nid
145
+
146
+ layer_ranges = []
147
+ for L_idx, size in enumerate(layer_sizes):
148
+ start = nid
149
+ for i in range(size):
150
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
151
+ NewNeuron += f"{nid},{kind},hidden{L_idx+1},{L_idx+1}\n"
152
+ nid += 1
153
+ layer_ranges.append((start, nid))
154
+
155
+ motor_start = nid
156
+ for i in range(N_MOT):
157
+ NewNeuron += f"{nid},excitatory,motor,{len(layer_sizes)+1}\n"
158
+ nid += 1
159
+ motor_end = nid
160
+
161
+ all_layers = [(sensory_start, sensory_end)] + layer_ranges + [(motor_start, motor_end)]
162
+
163
+ neuron_types = {}
164
+ for line in NewNeuron.strip().split("\n"):
165
+ parts = line.split(",")
166
+ neuron_types[int(parts[0])] = parts[1]
167
+
168
+ for L_idx in range(len(all_layers) - 1):
169
+ pre_start, pre_end = all_layers[L_idx]
170
+ post_start, post_end = all_layers[L_idx + 1]
171
+
172
+ for pre in range(pre_start, pre_end):
173
+ for post in range(post_start, post_end):
174
+ if neuron_types[pre] == "inhibitory":
175
+ weight = -abs(random.gauss(0.5, 0.15))
176
+ stype = "inhibitory"
177
+ else:
178
+ weight = abs(random.gauss(0.6, 0.2))
179
+ stype = "excitatory"
180
+
181
+ delay = random.uniform(1.0, 3.0)
182
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
183
+ syn_id += 1
184
+
185
+ for L_idx in range(len(all_layers) - 1, 0, -1):
186
+ pre_start, pre_end = all_layers[L_idx]
187
+ post_start, post_end = all_layers[L_idx - 1]
188
+
189
+ for pre in range(pre_start, pre_end):
190
+ for post in range(post_start, post_end):
191
+ if neuron_types[pre] == "inhibitory":
192
+ weight = -abs(random.gauss(0.5, 0.15)) * FB_STRENGTH
193
+ stype = "inhibitory"
194
+ else:
195
+ weight = abs(random.gauss(0.6, 0.2)) * FB_STRENGTH
196
+ stype = "excitatory"
197
+
198
+ delay = random.uniform(2.0, 5.0)
199
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
200
+ syn_id += 1
201
+
202
+ BrainTxt = ""
203
+ BrainTxt += "Neurons\n"
204
+ BrainTxt += NewNeuron
205
+ BrainTxt += "\nNeuronsEnd"
206
+ BrainTxt += "\nSinapce\n"
207
+ BrainTxt += NewSynapse
208
+ BrainTxt += "\nSinapceEnd"
209
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
210
+ f.write(BrainTxt)
211
+ return BrainTxt
212
+
213
+
214
+ def CreateBrainRecurrent(N_SENS=16, N_MOT=4, LAYERS="100,50,20", INHIB=0.2, REC_STRENGTH=0.3, REC_DENSITY=0.3, file_name="Brain.brain"):
215
+ NewNeuron = ""
216
+ NewSynapse = ""
217
+ nid = 0
218
+ syn_id = 0
219
+
220
+ layer_sizes = [int(x) for x in LAYERS.split(",")]
221
+
222
+ sensory_start = nid
223
+ for i in range(N_SENS):
224
+ NewNeuron += f"{nid},excitatory,sensory,0\n"
225
+ nid += 1
226
+ sensory_end = nid
227
+
228
+ layer_ranges = []
229
+ for L_idx, size in enumerate(layer_sizes):
230
+ start = nid
231
+ for i in range(size):
232
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
233
+ NewNeuron += f"{nid},{kind},hidden{L_idx+1},{L_idx+1}\n"
234
+ nid += 1
235
+ layer_ranges.append((start, nid))
236
+
237
+ motor_start = nid
238
+ for i in range(N_MOT):
239
+ NewNeuron += f"{nid},excitatory,motor,{len(layer_sizes)+1}\n"
240
+ nid += 1
241
+ motor_end = nid
242
+
243
+ all_layers = [(sensory_start, sensory_end)] + layer_ranges + [(motor_start, motor_end)]
244
+
245
+ neuron_types = {}
246
+ for line in NewNeuron.strip().split("\n"):
247
+ parts = line.split(",")
248
+ neuron_types[int(parts[0])] = parts[1]
249
+
250
+ for L_idx in range(len(all_layers) - 1):
251
+ pre_start, pre_end = all_layers[L_idx]
252
+ post_start, post_end = all_layers[L_idx + 1]
253
+
254
+ for pre in range(pre_start, pre_end):
255
+ for post in range(post_start, post_end):
256
+ if neuron_types[pre] == "inhibitory":
257
+ weight = -abs(random.gauss(0.5, 0.15))
258
+ stype = "inhibitory"
259
+ else:
260
+ weight = abs(random.gauss(0.6, 0.2))
261
+ stype = "excitatory"
262
+
263
+ delay = random.uniform(1.0, 3.0)
264
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
265
+ syn_id += 1
266
+
267
+ for L_idx, (start, end) in enumerate(layer_ranges):
268
+ n = end - start
269
+ n_rec = int(n * n * REC_DENSITY)
270
+
271
+ for _ in range(n_rec):
272
+ pre = random.randint(start, end - 1)
273
+ post = random.randint(start, end - 1)
274
+ if pre == post:
275
+ continue
276
+
277
+ if neuron_types[pre] == "inhibitory":
278
+ weight = -abs(random.gauss(0.5, 0.15)) * REC_STRENGTH
279
+ stype = "inhibitory"
280
+ else:
281
+ weight = abs(random.gauss(0.6, 0.2)) * REC_STRENGTH
282
+ stype = "excitatory"
283
+
284
+ delay = random.uniform(2.0, 4.0)
285
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
286
+ syn_id += 1
287
+
288
+ BrainTxt = ""
289
+ BrainTxt += "Neurons\n"
290
+ BrainTxt += NewNeuron
291
+ BrainTxt += "\nNeuronsEnd"
292
+ BrainTxt += "\nSinapce\n"
293
+ BrainTxt += NewSynapse
294
+ BrainTxt += "\nSinapceEnd"
295
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
296
+ f.write(BrainTxt)
297
+ return BrainTxt
298
+
299
+
300
+ def CreateBrainColumnar(N_SENS=16, N_MOT=4, N_COLUMNS=10, LAYERS_PER_COL="17,17,17,17,16,16", INHIB=0.25, LATERAL=0.05, LATERAL_STRENGTH=0.4, file_name="Brain.brain"):
301
+ NewNeuron = ""
302
+ NewSynapse = ""
303
+ nid = 0
304
+ syn_id = 0
305
+
306
+ layer_sizes = [int(x) for x in LAYERS_PER_COL.split(",")]
307
+ neurons_per_col = sum(layer_sizes)
308
+
309
+ sensory_start = nid
310
+ for i in range(N_SENS):
311
+ NewNeuron += f"{nid},excitatory,sensory,0,0\n"
312
+ nid += 1
313
+ sensory_end = nid
314
+
315
+ column_layers = []
316
+ for c in range(N_COLUMNS):
317
+ col_layers = []
318
+ for L_idx, size in enumerate(layer_sizes):
319
+ start = nid
320
+ for i in range(size):
321
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
322
+ NewNeuron += f"{nid},{kind},col{c}_L{L_idx+1},{L_idx+1},{c}\n"
323
+ nid += 1
324
+ col_layers.append((start, nid))
325
+ column_layers.append(col_layers)
326
+
327
+ motor_start = nid
328
+ for i in range(N_MOT):
329
+ NewNeuron += f"{nid},excitatory,motor,{len(layer_sizes)+1},-1\n"
330
+ nid += 1
331
+ motor_end = nid
332
+
333
+ neuron_types = {}
334
+ for line in NewNeuron.strip().split("\n"):
335
+ parts = line.split(",")
336
+ neuron_types[int(parts[0])] = parts[1]
337
+
338
+ for c in range(N_COLUMNS):
339
+ for L_idx in range(len(layer_sizes) - 1):
340
+ pre_start, pre_end = column_layers[c][L_idx]
341
+ post_start, post_end = column_layers[c][L_idx + 1]
342
+
343
+ for pre in range(pre_start, pre_end):
344
+ for post in range(post_start, post_end):
345
+ if neuron_types[pre] == "inhibitory":
346
+ weight = -abs(random.gauss(0.5, 0.15))
347
+ stype = "inhibitory"
348
+ else:
349
+ weight = abs(random.gauss(0.6, 0.2))
350
+ stype = "excitatory"
351
+
352
+ delay = random.uniform(1.0, 3.0)
353
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
354
+ syn_id += 1
355
+
356
+ layer1_ids = []
357
+ for c in range(N_COLUMNS):
358
+ layer1_ids.extend(range(column_layers[c][0][0], column_layers[c][0][1]))
359
+
360
+ for s in range(sensory_start, sensory_end):
361
+ for t in layer1_ids:
362
+ if random.random() < 0.3:
363
+ weight = abs(random.gauss(0.6, 0.2))
364
+ delay = random.uniform(1.0, 2.5)
365
+ NewSynapse += f"{syn_id},{s},{t},{weight:.4f},{delay:.2f},excitatory\n"
366
+ syn_id += 1
367
+
368
+ layer6_ids = []
369
+ for c in range(N_COLUMNS):
370
+ layer6_ids.extend(range(column_layers[c][-1][0], column_layers[c][-1][1]))
371
+
372
+ for s in layer6_ids:
373
+ for m in range(motor_start, motor_end):
374
+ if random.random() < 0.6:
375
+ if neuron_types[s] == "inhibitory":
376
+ weight = -abs(random.gauss(0.4, 0.1))
377
+ stype = "inhibitory"
378
+ else:
379
+ weight = abs(random.gauss(0.7, 0.2))
380
+ stype = "excitatory"
381
+ delay = random.uniform(1.0, 3.0)
382
+ NewSynapse += f"{syn_id},{s},{m},{weight:.4f},{delay:.2f},{stype}\n"
383
+ syn_id += 1
384
+
385
+ n_lateral = int(N_COLUMNS * (N_COLUMNS - 1) * neurons_per_col * neurons_per_col * LATERAL * 0.001)
386
+
387
+ for _ in range(n_lateral):
388
+ c1 = random.randint(0, N_COLUMNS - 1)
389
+ c2 = random.randint(0, N_COLUMNS - 1)
390
+ if c1 == c2:
391
+ continue
392
+
393
+ L1 = random.randint(0, len(layer_sizes) - 1)
394
+ L2 = random.randint(0, len(layer_sizes) - 1)
395
+
396
+ pre_start, pre_end = column_layers[c1][L1]
397
+ post_start, post_end = column_layers[c2][L2]
398
+
399
+ pre = random.randint(pre_start, pre_end - 1)
400
+ post = random.randint(post_start, post_end - 1)
401
+
402
+ if neuron_types[pre] == "inhibitory":
403
+ weight = -abs(random.gauss(0.4, 0.1)) * LATERAL_STRENGTH
404
+ stype = "inhibitory"
405
+ else:
406
+ weight = abs(random.gauss(0.5, 0.15)) * LATERAL_STRENGTH
407
+ stype = "excitatory"
408
+
409
+ delay = random.uniform(2.0, 5.0)
410
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
411
+ syn_id += 1
412
+
413
+ BrainTxt = ""
414
+ BrainTxt += "Neurons\n"
415
+ BrainTxt += NewNeuron
416
+ BrainTxt += "\nNeuronsEnd"
417
+ BrainTxt += "\nSinapce\n"
418
+ BrainTxt += NewSynapse
419
+ BrainTxt += "\nSinapceEnd"
420
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
421
+ f.write(BrainTxt)
422
+ return BrainTxt
423
+
424
+
425
+ def CreateBrainModular(N_SENS=16, N_MOT=4, N_MODULES=4, NEURONS_PER_MODULE=100, MODULE_LAYERS=2, INHIB=0.2, MODULE_CONNECT=0.3, MOD_STRENGTH=0.4, file_name="Brain.brain"):
426
+ NewNeuron = ""
427
+ NewSynapse = ""
428
+ nid = 0
429
+ syn_id = 0
430
+
431
+ sensory_start = nid
432
+ for i in range(N_SENS):
433
+ NewNeuron += f"{nid},excitatory,sensory,0,0,-1\n"
434
+ nid += 1
435
+ sensory_end = nid
436
+
437
+ per_layer = NEURONS_PER_MODULE // MODULE_LAYERS
438
+ module_layers = []
439
+ for m in range(N_MODULES):
440
+ ml = []
441
+ for L_idx in range(MODULE_LAYERS):
442
+ start = nid
443
+ for i in range(per_layer):
444
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
445
+ NewNeuron += f"{nid},{kind},mod{m}_L{L_idx+1},{L_idx+1},{m}\n"
446
+ nid += 1
447
+ ml.append((start, nid))
448
+ module_layers.append(ml)
449
+
450
+ motor_start = nid
451
+ for i in range(N_MOT):
452
+ NewNeuron += f"{nid},excitatory,motor,{MODULE_LAYERS+1},-1,-1\n"
453
+ nid += 1
454
+ motor_end = nid
455
+
456
+ neuron_types = {}
457
+ for line in NewNeuron.strip().split("\n"):
458
+ parts = line.split(",")
459
+ neuron_types[int(parts[0])] = parts[1]
460
+
461
+ for m in range(N_MODULES):
462
+ for L_idx in range(MODULE_LAYERS - 1):
463
+ pre_start, pre_end = module_layers[m][L_idx]
464
+ post_start, post_end = module_layers[m][L_idx + 1]
465
+
466
+ for pre in range(pre_start, pre_end):
467
+ for post in range(post_start, post_end):
468
+ if neuron_types[pre] == "inhibitory":
469
+ weight = -abs(random.gauss(0.5, 0.15))
470
+ stype = "inhibitory"
471
+ else:
472
+ weight = abs(random.gauss(0.6, 0.2))
473
+ stype = "excitatory"
474
+ delay = random.uniform(1.0, 3.0)
475
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
476
+ syn_id += 1
477
+
478
+ for m in range(N_MODULES):
479
+ m_first_start, m_first_end = module_layers[m][0]
480
+ for s in range(sensory_start, sensory_end):
481
+ for t in range(m_first_start, m_first_end):
482
+ if random.random() < 0.3:
483
+ weight = abs(random.gauss(0.6, 0.2))
484
+ delay = random.uniform(1.0, 2.5)
485
+ NewSynapse += f"{syn_id},{s},{t},{weight:.4f},{delay:.2f},excitatory\n"
486
+ syn_id += 1
487
+
488
+ for m in range(N_MODULES):
489
+ m_last_start, m_last_end = module_layers[m][-1]
490
+ for s in range(m_last_start, m_last_end):
491
+ for t in range(motor_start, motor_end):
492
+ if random.random() < 0.4:
493
+ if neuron_types[s] == "inhibitory":
494
+ weight = -abs(random.gauss(0.4, 0.1))
495
+ stype = "inhibitory"
496
+ else:
497
+ weight = abs(random.gauss(0.7, 0.2))
498
+ stype = "excitatory"
499
+ delay = random.uniform(1.0, 3.0)
500
+ NewSynapse += f"{syn_id},{s},{t},{weight:.4f},{delay:.2f},{stype}\n"
501
+ syn_id += 1
502
+
503
+ n_cross = int(N_MODULES * (N_MODULES - 1) * NEURONS_PER_MODULE * MODULE_CONNECT * 0.05)
504
+
505
+ for _ in range(n_cross):
506
+ m1 = random.randint(0, N_MODULES - 1)
507
+ m2 = random.randint(0, N_MODULES - 1)
508
+ if m1 == m2:
509
+ continue
510
+
511
+ L1 = random.randint(0, MODULE_LAYERS - 1)
512
+ L2 = random.randint(0, MODULE_LAYERS - 1)
513
+
514
+ pre_start, pre_end = module_layers[m1][L1]
515
+ post_start, post_end = module_layers[m2][L2]
516
+
517
+ pre = random.randint(pre_start, pre_end - 1)
518
+ post = random.randint(post_start, post_end - 1)
519
+
520
+ if neuron_types[pre] == "inhibitory":
521
+ weight = -abs(random.gauss(0.4, 0.1)) * MOD_STRENGTH
522
+ stype = "inhibitory"
523
+ else:
524
+ weight = abs(random.gauss(0.5, 0.15)) * MOD_STRENGTH
525
+ stype = "excitatory"
526
+
527
+ delay = random.uniform(2.0, 5.0)
528
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
529
+ syn_id += 1
530
+
531
+ BrainTxt = ""
532
+ BrainTxt += "Neurons\n"
533
+ BrainTxt += NewNeuron
534
+ BrainTxt += "\nNeuronsEnd"
535
+ BrainTxt += "\nSinapce\n"
536
+ BrainTxt += NewSynapse
537
+ BrainTxt += "\nSinapceEnd"
538
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
539
+ f.write(BrainTxt)
540
+ return BrainTxt
541
+
542
+
543
+ def CreateBrainTopographic(N_SENS=16, N_MOT=4, GRID1="10x10", GRID2="8x8", INHIB=0.2, LOCAL_R=2, DENSITY=0.6, file_name="Brain.brain"):
544
+ NewNeuron = ""
545
+ NewSynapse = ""
546
+ nid = 0
547
+ syn_id = 0
548
+
549
+ grid1_w, grid1_h = [int(x) for x in GRID1.split("x")]
550
+ grid2_w, grid2_h = [int(x) for x in GRID2.split("x")]
551
+ layer1_size = grid1_w * grid1_h
552
+ layer2_size = grid2_w * grid2_h
553
+
554
+ sensory_start = nid
555
+ for i in range(N_SENS):
556
+ NewNeuron += f"{nid},excitatory,sensory,0,-1,-1\n"
557
+ nid += 1
558
+ sensory_end = nid
559
+
560
+ layer1_start = nid
561
+ layer1_positions = []
562
+ for y in range(grid1_h):
563
+ for x in range(grid1_w):
564
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
565
+ NewNeuron += f"{nid},{kind},layer1,1,{x},{y}\n"
566
+ layer1_positions.append((nid, x, y))
567
+ nid += 1
568
+ layer1_end = nid
569
+
570
+ layer2_start = nid
571
+ layer2_positions = []
572
+ for y in range(grid2_h):
573
+ for x in range(grid2_w):
574
+ kind = "inhibitory" if random.random() < INHIB else "excitatory"
575
+ NewNeuron += f"{nid},{kind},layer2,2,{x},{y}\n"
576
+ layer2_positions.append((nid, x, y))
577
+ nid += 1
578
+ layer2_end = nid
579
+
580
+ motor_start = nid
581
+ for i in range(N_MOT):
582
+ NewNeuron += f"{nid},excitatory,motor,3,-1,-1\n"
583
+ nid += 1
584
+ motor_end = nid
585
+
586
+ neuron_types = {}
587
+ for line in NewNeuron.strip().split("\n"):
588
+ parts = line.split(",")
589
+ neuron_types[int(parts[0])] = parts[1]
590
+
591
+ for s in range(sensory_start, sensory_end):
592
+ for t, tx, ty in layer1_positions:
593
+ if random.random() < 0.4:
594
+ weight = abs(random.gauss(0.6, 0.2))
595
+ delay = random.uniform(1.0, 2.5)
596
+ NewSynapse += f"{syn_id},{s},{t},{weight:.4f},{delay:.2f},excitatory\n"
597
+ syn_id += 1
598
+
599
+ for pre_id, px, py in layer1_positions:
600
+ for post_id, qx, qy in layer1_positions:
601
+ if pre_id == post_id:
602
+ continue
603
+ dist = abs(px - qx) + abs(py - qy)
604
+ if dist <= LOCAL_R:
605
+ if random.random() < DENSITY * (1.0 - dist / (LOCAL_R + 1)):
606
+ if neuron_types[pre_id] == "inhibitory":
607
+ weight = -abs(random.gauss(0.5, 0.15))
608
+ stype = "inhibitory"
609
+ else:
610
+ weight = abs(random.gauss(0.5, 0.15))
611
+ stype = "excitatory"
612
+ delay = random.uniform(1.0, 3.0)
613
+ NewSynapse += f"{syn_id},{pre_id},{post_id},{weight:.4f},{delay:.2f},{stype}\n"
614
+ syn_id += 1
615
+
616
+ for pre_id, px, py in layer1_positions:
617
+ for post_id, qx, qy in layer2_positions:
618
+ if abs(px - qx) <= 1 and abs(py - qy) <= 1:
619
+ if random.random() < 0.5:
620
+ if neuron_types[pre_id] == "inhibitory":
621
+ weight = -abs(random.gauss(0.5, 0.15))
622
+ stype = "inhibitory"
623
+ else:
624
+ weight = abs(random.gauss(0.6, 0.2))
625
+ stype = "excitatory"
626
+ delay = random.uniform(1.0, 3.0)
627
+ NewSynapse += f"{syn_id},{pre_id},{post_id},{weight:.4f},{delay:.2f},{stype}\n"
628
+ syn_id += 1
629
+
630
+ for pre_id, px, py in layer2_positions:
631
+ for post_id, qx, qy in layer2_positions:
632
+ if pre_id == post_id:
633
+ continue
634
+ dist = abs(px - qx) + abs(py - qy)
635
+ if dist <= LOCAL_R:
636
+ if random.random() < DENSITY * (1.0 - dist / (LOCAL_R + 1)):
637
+ if neuron_types[pre_id] == "inhibitory":
638
+ weight = -abs(random.gauss(0.5, 0.15))
639
+ stype = "inhibitory"
640
+ else:
641
+ weight = abs(random.gauss(0.5, 0.15))
642
+ stype = "excitatory"
643
+ delay = random.uniform(1.0, 3.0)
644
+ NewSynapse += f"{syn_id},{pre_id},{post_id},{weight:.4f},{delay:.2f},{stype}\n"
645
+ syn_id += 1
646
+
647
+ layer2_ids = [pid for pid, _, _ in layer2_positions]
648
+ for s in layer2_ids:
649
+ for m in range(motor_start, motor_end):
650
+ if random.random() < 0.3:
651
+ if neuron_types[s] == "inhibitory":
652
+ weight = -abs(random.gauss(0.4, 0.1))
653
+ stype = "inhibitory"
654
+ else:
655
+ weight = abs(random.gauss(0.7, 0.2))
656
+ stype = "excitatory"
657
+ delay = random.uniform(1.0, 3.0)
658
+ NewSynapse += f"{syn_id},{s},{m},{weight:.4f},{delay:.2f},{stype}\n"
659
+ syn_id += 1
660
+
661
+ n_far = int(layer1_size * layer2_size * 0.005)
662
+ for _ in range(n_far):
663
+ pre = random.choice(layer1_positions)[0]
664
+ post = random.choice(layer2_positions)[0]
665
+ if neuron_types[pre] == "inhibitory":
666
+ weight = -abs(random.gauss(0.3, 0.1))
667
+ stype = "inhibitory"
668
+ else:
669
+ weight = abs(random.gauss(0.4, 0.15))
670
+ stype = "excitatory"
671
+ delay = random.uniform(2.0, 5.0)
672
+ NewSynapse += f"{syn_id},{pre},{post},{weight:.4f},{delay:.2f},{stype}\n"
673
+ syn_id += 1
674
+
675
+ BrainTxt = ""
676
+ BrainTxt += "Neurons\n"
677
+ BrainTxt += NewNeuron
678
+ BrainTxt += "\nNeuronsEnd"
679
+ BrainTxt += "\nSinapce\n"
680
+ BrainTxt += NewSynapse
681
+ BrainTxt += "\nSinapceEnd"
682
+ with open(os.path.join(user_script_dir, file_name), "w", encoding="utf-8") as f:
683
+ f.write(BrainTxt)
684
+ return BrainTxt
685
+
686
+
687
+ def StartBrain(brain_index=0):
688
+ global _neurons
689
+ global _sinapce
690
+
691
+ n_text = _neurons[brain_index]
692
+ s_text = _sinapce[brain_index]
693
+
694
+ neurons = []
695
+ for line in n_text.strip().split("\n"):
696
+ if not line:
697
+ continue
698
+ parts = line.split(",")
699
+ if not parts[0].isdigit():
700
+ continue
701
+ neurons.append({
702
+ "id": int(parts[0]),
703
+ "type": parts[1],
704
+ "layer": parts[2],
705
+ })
706
+
707
+ synapses = []
708
+ for line in s_text.strip().split("\n"):
709
+ if not line:
710
+ continue
711
+ parts = line.split(",")
712
+ if not parts[1].isdigit():
713
+ continue
714
+ synapses.append({
715
+ "pre": int(parts[1]),
716
+ "post": int(parts[2]),
717
+ "weight": float(parts[3]),
718
+ "delay": float(parts[4]),
719
+ })
720
+
721
+ N = len(neurons)
722
+
723
+ state = {
724
+ "neurons": neurons,
725
+ "synapses": synapses,
726
+ "V": [-65.0] * N,
727
+ "refract": [0] * N,
728
+ "spike_cnt": [0.0] * N,
729
+ "delay_buffer": [],
730
+ "delay_head": 0,
731
+ "sensor_ids": [],
732
+ "motor_ids": [],
733
+ }
734
+
735
+ for n in neurons:
736
+ if n["layer"] == "sensory":
737
+ state["sensor_ids"].append(n["id"])
738
+ if n["layer"] == "motor":
739
+ state["motor_ids"].append(n["id"])
740
+
741
+ max_delay = int(max(s["delay"] for s in synapses))
742
+ state["delay_buffer"] = [[0.0] * len(synapses) for _ in range(max_delay + 1)]
743
+
744
+ return state
745
+
746
+
747
+ def StepBrain(state, sensors):
748
+ N = len(state["neurons"])
749
+ synapses = state["synapses"]
750
+ n_syn = len(synapses)
751
+ buf_len = len(state["delay_buffer"])
752
+
753
+ I = [0.0] * N
754
+
755
+ for i, sid in enumerate(state["sensor_ids"]):
756
+ if i < len(sensors):
757
+ I[sid] += sensors[i] * 10.0
758
+
759
+ delivered = state["delay_buffer"][state["delay_head"]]
760
+ for k in range(n_syn):
761
+ val = delivered[k]
762
+ if val != 0.0:
763
+ I[synapses[k]["post"]] += val
764
+ state["delay_buffer"][state["delay_head"]] = [0.0] * n_syn
765
+
766
+ for i in range(N):
767
+ if state["refract"][i] > 0:
768
+ state["refract"][i] -= 1
769
+ continue
770
+ dV = 1.0 / 10.0 * (-(state["V"][i] - (-65.0)) + I[i])
771
+ state["V"][i] += dV
772
+
773
+ fired = []
774
+ for i in range(N):
775
+ if state["refract"][i] <= 0 and state["V"][i] >= -55.0:
776
+ fired.append(i)
777
+
778
+ for nid in fired:
779
+ state["V"][nid] = -75.0
780
+ state["refract"][nid] = 2
781
+ state["spike_cnt"][nid] += 1
782
+ for k in range(n_syn):
783
+ if synapses[k]["pre"] == nid:
784
+ slot = (state["delay_head"] + int(synapses[k]["delay"])) % buf_len
785
+ state["delay_buffer"][slot][k] += synapses[k]["weight"]
786
+
787
+ state["delay_head"] = (state["delay_head"] + 1) % buf_len
788
+
789
+ motors = []
790
+ for mid in state["motor_ids"]:
791
+ rate = state["spike_cnt"][mid] / 50.0
792
+ motors.append(min(1.0, rate))
793
+ state["spike_cnt"][mid] *= 0.99
794
+
795
+ return motors
796
+
797
+
798
+ def DeleteBrain(index):
799
+ global _neurons
800
+ global _sinapce
801
+ _neurons.pop(index)
802
+ _sinapce.pop(index)
803
+ return
804
+
805
+
806
+ def DeleteAllBrain():
807
+ global _neurons
808
+ global _sinapce
809
+ _neurons = []
810
+ _sinapce = []
811
+ return
@@ -0,0 +1,31 @@
1
+ Metadata-Version: 2.4
2
+ Name: SpikeNet
3
+ Version: 1.0.0
4
+ Summary: Spiking neural network library for games
5
+ Author: Danic_Programmer
6
+ License: MIT
7
+ Keywords: neural-network,spiking,snn,brain,lif
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: license-file
15
+
16
+ # SpikeNet
17
+
18
+
19
+
20
+ Спайковая нейросеть для игр. 6 архитектур, LIF-нейроны, `.brain` формат.
21
+
22
+
23
+
24
+ \## Установка
25
+
26
+
27
+
28
+ ```bash
29
+
30
+ pip install SpikeNet
31
+
@@ -0,0 +1,6 @@
1
+ SpikeNet/__init__.py,sha256=ewZwp1E91wxnOhvNggldf8iQrdiJOdbVmp5MLow6i3s,28518
2
+ spikenet-1.0.0.dist-info/licenses/LICENSE,sha256=yuBFTU_dAboogTpcde6N0dTiZJ3uL8mFjQVlbgpxY0c,1092
3
+ spikenet-1.0.0.dist-info/METADATA,sha256=eUt-6dcUoYP-UzYtHkalv2-dXx7S-fbMm_dO8thxEl4,666
4
+ spikenet-1.0.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
5
+ spikenet-1.0.0.dist-info/top_level.txt,sha256=gOfTsZytGjL1mJfmuiwhM48Sy1RuZRfYCNFlwIimiIo,9
6
+ spikenet-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Danic_Programmer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ SpikeNet