-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·178 lines (145 loc) · 5.58 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python2
import numpy as np
import h5py
import cPickle
from scipy import signal as dsp
import multiprocessing as mp
from time import time as WALLCLOCK
import signal
import twisted.internet.protocol as tip
import twisted.internet.reactor as tir
#### globals
MICROVOLTS_PER_COUNT = np.float16(0.195)
lowcut = 300.
highcut = 9500.
fs = 3e4
order = 5
FILTERB, FILTERA = dsp.butter(order, [lowcut*2./fs, highcut*2./fs], btype='band')
#### helper functions
def filterSlab(slab):
"""
NOTE: this upcasts to np.float64
"""
return dsp.lfilter(FILTERB, FILTERA, slab, axis=1)
def calculateActivity(slab, wpipe):
nchan = slab.shape[0]
slab = (np.array(slab, dtype='float')-2**15)*MICROVOLTS_PER_COUNT
filteredSlab = filterSlab(slab)
threshold = (-4.5*np.median(np.abs(filteredSlab), axis=1)/0.6745) # from J.P. Kinney
activity = np.zeros(nchan)
for i in range(nchan):
activity[i] = np.sum((filteredSlab[i,:] < threshold[i]))
wpipe.send(activity)
class PipedProcess(mp.Process):
def __init__(self, rpipe, **kwargs):
mp.Process.__init__(self, **kwargs)
self.rpipe = rpipe
def chanMap_linearToWillow(channels_linear, probeMap_dict):
willowChans = []
for linChan in channels_linear:
shank = linChan // 204
row = linChan % 102
col = linChan & 0x1
willowChans.append(probeMap_dict[shank, row, col])
return willowChans
def MPGenerator(dataSlab, nproc):
nchan = dataSlab.shape[0]
nchanPerProc = nchan // nproc
splitter = np.arange(1,nproc) * nchanPerProc
for subSlab in np.split(dataSlab, splitter):
yield subSlab
#### main twisted code
CACHE_RAW = None
CACHE_TIME = None
class AspenProtocol(tip.Protocol):
def __init__(self):
self.activity = np.zeros(1020, dtype=np.float16)
self.chans1020_linear = np.arange(1020, dtype=int)
self.chans1020_willow = np.array(chanMap_linearToWillow(self.chans1020_linear, probeMap_dict),
dtype=np.uint16)
def dataReceived(self, payload):
print 'payload = ', payload
msg = payload.split(',')
if msg[0] == 'nsamples':
nsamples = (FILE['channel_data'].shape[0] / 1024)
nsamples_payload = np.array(nsamples, dtype=np.uint64).tostring()
self.transport.write(nsamples_payload)
elif msg[0] == 'dataSlab':
dataCoords = msg[1:]
tir.callInThread(self.handleDataRequest, dataCoords)
#self.handleDataRequest(dataCoords)
elif msg[0] == 'activity':
time = int(msg[1])
tir.callInThread(self.handleActivityRequest, time)
#self.handleActivityRequest(time)
else:
print 'unexpected keyword:', msg[0]
def handleDataRequest(self, dataCoords):
global CACHE_RAW, CACHE_TIME
# send channel list
chan = int(dataCoords[0])
time = int(dataCoords[1])
filt = (dataCoords[2] == 'True')
channels_linear = range(chan, chan+12)
channels_willow = np.array(chanMap_linearToWillow(channels_linear, probeMap_dict),
dtype=np.uint16)
self.transport.write(channels_willow.tostring())
# IO (first, attempt to retrieve raw data from cache)
if time == CACHE_TIME:
print 'cache hit!'
data = CACHE_RAW
else:
print 'cache miss! ', time, CACHE_TIME
data = np.array(FILE['channel_data'][(1024 * time):(1024 * (time + 60000))],
dtype=np.uint16).reshape((-1, 1024)).transpose()
# slab selection and conversion
dataSlab = (np.array(data[channels_willow,:], dtype=np.float16)-np.float16(2**15))*MICROVOLTS_PER_COUNT
# filtering
if filt:
dataSlab = np.array(filterSlab(dataSlab), dtype=np.float16)
self.transport.write(dataSlab.tostring())
# save to cache
CACHE_RAW = data
CACHE_TIME = time
def handleActivityRequest(self, time):
# IO (first, attempt to retrieve raw data from cache)
if time == CACHE_TIME:
print 'cache hit!'
data = CACHE_RAW
else:
print 'cache miss! ', time, CACHE_TIME
data = np.array(FILE['channel_data'][(1024 * time):(1024 * (time + 60000))],
dtype=np.uint16).reshape((-1, 1024)).transpose()
dataSlab = data[self.chans1020_willow,:] # re-ordering
# (multi) processing
procs = []
for subSlab in MPGenerator(dataSlab, NCPU):
rpipe, wpipe = mp.Pipe()
procs.append(PipedProcess(rpipe, target=calculateActivity,
args=(subSlab, wpipe)))
for pp in procs: pp.start()
cursor = 0
for i, pp in enumerate(procs):
pp.join()
activitySlice = pp.rpipe.recv()
nchan_slice = activitySlice.shape[0]
self.activity[cursor:(cursor+nchan_slice)] = activitySlice
cursor += nchan_slice
self.transport.write(self.activity.tostring())
class AspenFactory(tip.ServerFactory):
def buildProtocol(self, addr):
return AspenProtocol()
if __name__ == '__main__':
import sys
if len(sys.argv)>1:
FILE = h5py.File(sys.argv[1])
else:
print 'Usage: ./server.py <path/to/datafile.h5>'
sys.exit(1)
probeMap_dict = cPickle.load(open('probeMap_1020_level2_20160402.p', 'rb'))
NCPU = mp.cpu_count()
PORT = 8000
print '\nNCPU = %d' % NCPU
print 'Serving on Port %d\n' % PORT
tir.listenTCP(PORT, AspenFactory())
tir.run()