-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
283 lines (234 loc) · 8.34 KB
/
validate.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# beacon.dat -- t_win=15
# beacon136.dat -- include=[1,3,6]
# beacon13.dat -- include=[1,3]
# beacon16.dat -- include=[1,6]
# beacon36.dat -- include=[3,6]
# f0beacon.dat -- score_threshold>=0, t_win=15
# f0.5beacon.dat -- score_threshold>=0.5, t_win=60
# f30beacon.dat -- score_threshold>=0, t_win=30
# f60beacon.dat -- score_threshold>=0, t_win=60
# f60beacon.e1.dat -- score_threshold>=0, t_win=60, exclude=[1]
from qraat.srv import util, signal, position
import scipy.stats
import numpy as np
import matplotlib.pyplot as pp
import pickle
suffix = 'dat'
conf_level=0.95
position.NORMALIZE_SPECTRUM=False
def band_dist(dep_id, sites, db_con):
cur = db_con.cursor()
band3 = {}
band10 = {}
for site_id in sites.keys():
cur.execute('''SELECT band3, band10
FROM qraat.est
WHERE deploymentID=%s
AND siteID=%s''', (dep_id, site_id))
band3[site_id] = []
band10[site_id] = []
for (bw3, bw10) in cur.fetchall():
band3[site_id].append(bw3)
band10[site_id].append(bw10)
N=100
fig = pp.gcf()
n, bins, patches = pp.hist(band3[site_id], N, histtype='stepfilled')
pp.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
pp.savefig('band3_dep%d_site%d.png' % (dep_id, site_id))
pp.clf()
fig = pp.gcf()
n, bins, patches = pp.hist(band10[site_id], N, histtype='stepfilled')
pp.setp(patches, 'facecolor', 'r', 'alpha', 0.75)
pp.savefig('band10_dep%d_site%d.png' % (dep_id, site_id))
pp.clf()
def process(sv, sites, center, params):
dep_id = params['dep_id']
t_start = params['t_start']
t_end = params['t_end']
t_step = params['t_step']
t_win = params['t_win']
t_chunk = params['t_chunk']
include = params['include']
exclude = params['exclude']
ct = 1; good = 0; total = 0
P = {} # site_id's --> position estimate
C = {} # site_id's --> confidence region estimate
for t in np.arange(t_start, t_end+t_chunk, t_chunk):
# Signal data
sig = signal.Signal(db_con, dep_id, t, t+t_chunk, include=include, exclude=exclude,
score_threshold=0.15)
print "chunk", ct, '(%d pulses)' % sig.get_count()
ct += 1
if sig.t_start == float("+inf"):
continue
# Compute positions
positions = position.WindowedPositionEstimator(sig, sites, site34, sv,
t_step, t_win, method=signal.Signal.Bartlet)
for pos in positions:
print pos.splines.keys(),
site_ids = tuple(set(pos.splines.keys()))
if P.get(site_ids) is None:
P[site_ids] = []
C[site_ids] = []
P[site_ids].append(pos.p)
if pos.p is not None:
try:
cov = position.BootstrapCovariance(pos, sites, max_resamples=500)
E = cov.conf(conf_level)
C[site_ids].append((E.angle, E.axes[0], E.axes[1]))
print "Ok"
good += 1
except position.SingularError:
C[site_ids].append(None)
print "non positive indefinite"
except position.PosDefError:
C[site_ids].append(None)
print "non positive indefinite (PosDefError)"
except position.BootstrapError:
C[site_ids].append(None)
print "samples ..."
else: C[site_ids].append(None)
total += 1
print 'good', good, "out of", total, "(t_win=%d, norm=%s)" % (
t_win, position.NORMALIZE_SPECTRUM)
return (P, C)
def plot_map(p_known, sites, fn):
fig = pp.gcf()
ax = fig.add_subplot(111)
ax.axis('equal')
ax.set_xlabel('easting (m)')
ax.set_ylabel('northing (m)')
offset = 20
for (id, p) in sites.iteritems():
pp.plot(p.imag, p.real, color='r', marker='o', ms=7)
pp.text(p.imag+offset, p.real+offset, id)
pp.plot(p_known.imag, p_known.real, color='w', marker='o', ms=7)
pp.grid()
pp.title("Receivers and transmitter")
pp.savefig("%s.png" % (fn), dpi=120)
pp.clf()
def mean(P, site_ids):
pos = np.array(filter(lambda x : x != None, P[site_ids]))
pos = pos[~np.isnan(pos)]
if len(pos) > 0:
return np.mean(pos)
else:
return None
def plot(P, fn, p_known=None):
for site_ids in P.keys():
p_mean = mean(P, site_ids)
if len(site_ids) > 1:
pos = np.array(filter(lambda p: p!=None, P[site_ids]))
#pos = pos[np.abs(pos - p_known) < 200]
fig = pp.gcf()
ax = fig.add_subplot(111)
ax.axis('equal')
ax.set_xlabel('easting (m)')
ax.set_ylabel('northing (m)')
pp.scatter(np.imag(pos), np.real(pos), alpha=0.3, facecolors='b', edgecolors='none', s=25)#, zorder=11)
if p_known is not None:
pp.plot(p_known.imag, p_known.real, color='w', marker='o', ms=7)
else:
pp.plot(p_mean.imag, p_mean.real, color='w', marker='o', ms=7)
pp.grid()
pp.title("sites=%s total estimates=%d" % (str(site_ids), len(pos)))
pp.tight_layout()
pp.savefig("%s-%s.png" % (fn, ''.join(map(lambda x: str(x), site_ids))), dpi=120, bbox_inches='tight')
pp.clf()
def count(C):
res = {}
for site_ids in C.keys():
res[site_ids] = len(filter(lambda ell: ell!=None, C[site_ids]))
return res
def correlation(P, C, p_known=None):
res = {}
for site_ids in P.keys():
val = []; dist = []
if len(site_ids) > 1 and len(P[site_ids]) >= 10:
p_mean = mean(P, site_ids)
for i in range(len(P[site_ids])):
if C[site_ids][i] is not None and C[site_ids][i][1] > 0 and C[site_ids][i][2]> 0:
angle, axis0, axis1 = C[site_ids][i]
E = position.Ellipse(P[site_ids][i], angle, [axis0, axis1])
val.append(E.area())
if p_known is None:
dist.append(np.abs(P[site_ids][i] - p_mean))
else:
dist.append(np.abs(P[site_ids][i] - p_known))
# First value is the correlation, the second is the p-value
# (probability of data asuuming they are uncorrelated)
res[site_ids] = scipy.stats.stats.pearsonr(val, dist)
else: res[site_ids] = None
return res
def coverage(P, C, p_known=None):
res = {}
for site_ids in P.keys():
val = []
if len(site_ids) > 1:
p_mean = mean(P, site_ids)
for i in range(len(P[site_ids])):
if C[site_ids][i] is not None and C[site_ids][i][1] > 0 and C[site_ids][i][2]> 0:
angle, axis0, axis1 = C[site_ids][i]
E = position.Ellipse(P[site_ids][i], angle, [axis0, axis1])
if p_known is None:
val.append(p_mean in E)
else: val.append(p_known in E)
if len(val) > 0:
res[site_ids] = (sum(val), len(val))
else:
res[site_ids] = None
else: res[site_ids] = None
return res
if __name__ == '__main__':
db_con = util.get_db('reader')
cal_id = 3
site34 = np.complex(4260910.87, 574296.45)
sv = signal.SteeringVectors(db_con, cal_id)
sites = util.get_sites(db_con)
(center, zone) = util.get_center(db_con)
# Becaon parameters
params = { 't_step' : 30,
't_win' : 60,
't_chunk' : 3600 / 4,
'dep_id' : 60,
't_start' : 1383098400.51432,
't_end' : 1383443999.351099,
'include' : [],
'exclude' : [] }
# Mary-Brook-walk-around params
#dep_id = 61
#t_start = 1396725598.548015
#t_end = 1396732325.777558
prefix = 'f30beacon'
fn = prefix + ''.join(map(lambda id: str(id), params['include']))
if len(params['exclude']) > 0:
fn += '.e' + ''.join(map(lambda id: str(id), params['exclude']))
print fn
#band_dist(params['dep_id'], sites, db_con)
#plot_map(site34, sites, 'beacon-map')
#P, C = process(sv, sites, center, params)
#pickle.dump((P, C), open(fn+'.'+suffix, 'w'))
(P, C) = pickle.load(open(fn+'.'+suffix, 'r'))
plot(P, fn, site34)
a = sorted(P.keys())
print "Count"
ct = count(C)
for site_ids in a:
print site_ids, '-->', ct[site_ids]
print "\nCorrelation" # of distance to true position and ellipse area
corr = correlation(P, C, site34)
for site_ids in a:
if corr[site_ids] == None:
print site_ids
else:
print site_ids, '--> %0.4f, p-val=%0.4f' % corr[site_ids]
print "\nCvg. probability"
cover = coverage(P, C)
for site_ids in a:
p = cover[site_ids]
if p == None:
print site_ids
elif p[0] == 0:
print site_ids, '%d/%d' % (p[0], p[1])
else:
print site_ids, '--> %%%.1f %d/%d' % ((float(100*p[0])/p[1],) + p)