forked from Shikhargupta/Spiking-Neural-Network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspike_train.py
63 lines (57 loc) · 1.72 KB
/
spike_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
########################### README ############################################
# This file is used to generate spike train from potential map. There are two
# methods to do so. One is deterministic where we calculate the spike frequency
# directly proportional to the potential of that pixel and construct a train
# with equally spaced spikes. Other one is stochastic where we calculate the
# probability of the pixel to fire a spike and construct a spike train
# accordingly
###############################################################################
import numpy as np
from numpy import interp
from neuron import neuron
import random
from recep_field import rf
import imageio
import math
from sklearn.preprocessing import normalize
# Builds a probabilistic spike train
def encode_stochastic(img):
T = 200
train = []
pot1 = normalize(img, norm='l2')
for l in range(28):
for m in range(28):
temp = np.random.uniform(size=(T+1))
temp = (temp < pot1[l][m])
train.append(temp)
return train
def encode_deterministic(pot):
#defining time frame of 1s with steps of 5ms
T = 200;
#initializing spike train
train = []
for l in range(28):
for m in range(28):
temp = np.zeros([(T+1),])
#calculating firing rate proportional to the membrane potential
freq = interp(pot[l][m], [-2,5], [1,20])
# print freq
if freq>0:
freq1 = math.ceil(T/freq)
#generating spikes according to the firing rate
k = freq1
while k<(T+1):
temp[int(k)] = 1
k = k + freq1
train.append(temp)
# print sum(temp)
return train
if __name__ == '__main__':
m = []
n = []
img = imageio.imread("training_images/1.png")
# pot = rf(img)
# train = encode_deterministic(pot)
# print train
# print img
encode_stochastic(img)