2011/results/att11/spikevision: LCAbig2_UDP.py

File LCAbig2_UDP.py, 5.4 KB (added by sshapero, 7 years ago)

NEF Network that takes 48x48 UDP inputs from jAER, subsamples it to a 24x24, finds sparse representation. Includes code for handling UDP.

Line 
1import nef
2from numeric import *
3import socket, sys, time
4from struct import *
5import shelve
6
7fmt_string = '!iI'
8stride = 8
9y_offset = 8
10y_length = 8
11x_offset = 1
12x_length = 8
13
14HOST = "127.0.0.1"
15PORT = str(9000)
16BUFFSIZE = 2048
17ADDR = (HOST,PORT)
18
19
20class event:
21    def __init__(self,addr,time):
22        self.time = time
23        self.x = 127 - ((addr>>x_offset)&2**(x_length-1)-1)
24        self.y = (addr>>y_offset)&2**(y_length-1)-1
25        self.intensity = 1 - (addr & 1)
26    def __repr__(self):
27        return repr((self.intensity,self.x,self.y,self.time))
28
29# Use the below for command line inputs
30#host = sys.argv[1]
31#textport = sys.argv[2]
32
33host = HOST
34textport = PORT
35
36
37
38def sth(x):
39        th=.6
40        if x>th:
41                return (x-th)
42        elif x<-th:
43                return (x+th)
44        else:
45                return 0
46
47def sthn(x):
48        return [sth(y) for y in x]
49
50def zero(m,n):
51    # Create zero matrix
52    new_matrix = [[0 for row in range(n)] for col in range(m)]
53    return new_matrix
54
55def eye(n):
56    new_matrix = [[(row == col) for row in range(n)] for col in range(n)]
57    return new_matrix
58
59def multmin(matrix1):
60        # Matrix multiplication
61        new_matrix = zero(len(matrix1),len(matrix1))
62        for i in range(len(matrix1)):
63                for j in range(len(matrix1)):
64                        if(i != j):
65                                for k in range(len(matrix1[0])):
66                                        new_matrix[i][j] -= matrix1[i][k]*matrix1[j][k]
67
68        return new_matrix
69
70
71def transpose(matrix1):
72        new_matrix = zero(len(matrix1[0]),len(matrix1))
73        for i in range(len(matrix1)):
74                for j in range(len(matrix1[0])):
75                        new_matrix[j][i] = matrix1[i][j]
76
77        return new_matrix
78
79db=shelve.open('data_th')
80if (db.has_key('dict')):
81    PHI_pre = db['dict']
82    recur_pre = db['recur']
83else:
84    f = open('thdictionary.txt')
85
86    num_dict = 128*8
87    PHI_pre = []
88    for img in range(num_dict):
89        line = f.readline()
90        dictpre=[]
91        first = 0
92        for char in range(len(line)):
93                if (line[char] == ' '):
94                        last = char
95                        dictpre.append(round(float(line[first:last]),4))
96                        first = char+1
97        #print img     
98        PHI_pre.append(dictpre)
99    f.close()
100
101    f = open('threcur.txt')
102
103    recur_pre = []
104    for img in range(num_dict):
105        line = f.readline()
106        dictpre=[]
107        first = 0
108        for char in range(len(line)):
109                if (line[char] == ' '):
110                        last = char
111                        dictpre.append(round(float(line[first:last]),4))
112                        first = char+1
113               
114        recur_pre.append(dictpre)
115
116    f.close()
117   
118    db['dict']=PHI_pre
119    db['recur']=recur_pre
120
121db.close()
122       
123dict = array(PHI_pre)
124recur = array(recur_pre)
125
126
127numNodes = len(dict)
128numInputs = len(dict[0])
129
130
131
132class MyInput(nef.SimpleNode):
133        def origin_value(self):
134                out_pre = zero(1,576)
135                out = out_pre[0][:]
136                compress = 2
137                if(self.t_start < .11):
138                    logtime = 12
139                    self.oldevents = logtime*[[]]
140                else:
141                    try:
142                        data, addr = sock.recvfrom(BUFFSIZE)
143                    except socket.error:
144                        self.oldevents[1:]=self.oldevents[:-1]
145                        self.oldevents[0]=[]
146                    else:
147                        #print addr
148                        #print "sanity check:", addr[1]/4, (len(data)-4)/8, (len(data)-4)%8
149                        #if (((len(data)-4)%8 != 0)or ((len(data)-4)/8 ==0 )):
150                        #print "There is an error in the format of the packets sent"
151                        #sys.exit(1)
152                        tind = unpack("!i",data[0:4])
153                        # print tind
154                        event_list = []   
155                        for iter in range(4,len(data)-4,stride):
156                                message = unpack(fmt_string,data[iter:(iter+stride)])
157                                #print message
158                                event_list.append(event(message[0],message[1]))
159                               
160                                self.oldevents[1:]=self.oldevents[:-1]
161                        self.oldevents[0]=event_list
162                               
163                for evlist in self.oldevents:
164                    for events in evlist:
165                        x = int(events.x/compress)
166                        y = int(events.y/compress)
167                        outind = int(x+24*(24-y-1))
168                        #print x, y, outind
169                        if (x < 24 and y < 24 and x > -1 and y > -1):
170                                #print events.time
171                                out[outind] = 2*int(events.intensity)-1
172                return out
173
174
175lif = array(eye(numInputs))*0.5
176
177net=nef.Network('LCA',quick=True)
178#net.add_to(world)
179
180#input=net.make_input('input',clippre[15])
181myinput=MyInput('input')
182net.add(myinput)
183#infilter=net.make('infilter',1,numInputs,mode='direct')
184neuron=net.make_array('neurons',40,numNodes,intercept=(0,1))
185decoders=net.make('decoders',1,numInputs,mode='direct')
186neuron_value=net.make('neuron_value',1,numNodes,mode='direct')
187
188
189#net.connect(input,neuron,transform=dict)
190#net.connect(myinput.getOrigin('value'),infilter,pstc=.0005)
191#net.connect(infilter,infilter,transform=lif,pstc=.0005)
192net.connect(myinput.getOrigin('value'),neuron,transform=dict,pstc=.002)
193net.connect(neuron,neuron,transform=recur,func=sthn,pstc=.002)
194net.connect(neuron,decoders,transform=dict.T,func=sthn,pstc=.0005)
195net.connect(neuron,neuron_value,func=sthn,pstc=.0005)
196
197sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
198sock.bind((HOST,int(PORT) ))
199sock.settimeout(.001)
200
201net.view(play=0.1)
202
203#sim=net.network.simulator
204#sim.run(0,1,0.001)
205
206#print neuron_value.getOrigin('X').getValues().getValues()
207#print outs.getOrigin('X').getValues().getValues()