-
Notifications
You must be signed in to change notification settings - Fork 3
/
3_indoor_localization.py
411 lines (257 loc) · 9.71 KB
/
3_indoor_localization.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Indoor Loacation using Bayes
# code for offline processing
# Spyder, Python 3.6 (Ancaconda2 version)
# By: Dennis Nunez Fernandez
#%%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import cm
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
import itertools
import operator
import glob
#%%
plt.close('all')
DATA = "DATA_001_House"
path_data = "data1_filtered_wifis/"+DATA+"/"
path_parts = "data2_divided_parts/"+DATA+"/"
numRegions = 0
for fullname in glob.glob(path_data+"*.csv"):
dfInRegion = pd.read_csv(fullname, sep=" ")
numAPs = len(dfInRegion.columns)
numRegions = numRegions+1
Regions = ["R%.3d" % i for i in range(1,numRegions+1)]
APs = ["W%.3d" % i for i in range(1,numAPs+1)]
#%%
# Generate tables to see important Regions and APs: df_counter, df_counters_w, df_counters_r
df_counter = pd.DataFrame(columns = APs, index =Regions)
#read recursively
for fullname in glob.glob(path_data+"*.csv"):
print(fullname)
#read dataframe of a region
dfInRegion = pd.read_csv(fullname, sep=" ")
for x in range(8,101):
if -x ==-100:
dfInRegion = dfInRegion.replace(-x, 0)
dfInRegion = dfInRegion.replace(-x, 1)
df_count = dfInRegion.sum()/len(dfInRegion)
df_count1 = df_count.tolist()
df_count0 = dfInRegion.columns.tolist()
df_counter.loc[fullname[len(fullname)-8:len(fullname)-4]] = df_count1
df_counter_ = df_counter.copy()
df_counter_[df_counter_<=0.6]=0
df_counter_[df_counter_>0.6]=1
df_countt = df_counter_.sum()/len(df_counter_)
df_countt1 = df_countt.tolist()
df_countt0 = dfInRegion.columns.tolist()
df_counters_w = pd.DataFrame(columns = APs, index=['0'])
df_counters_w.loc['0'] = df_countt1
df_countt = df_counter_.sum(axis=1)/len(APs)
df_countt1 = df_countt.tolist()
df_countt0 = dfInRegion.columns.tolist()
df_counters_r = pd.DataFrame(columns = Regions, index=['0'])
df_counters_r.loc['0'] = df_countt1
#%%
# SELECT Regions and APs from df_counter, df_counters_w, df_counters_r
# SELECT Regions and APs from df_counter, df_counters_w, df_counters_r
# SELECT Regions and APs from df_counter, df_counters_w, df_counters_r
# SELECT Regions and APs from df_counter, df_counters_w, df_counters_r
Regions_ = [1,2,3,4]
APs_ = [2,8]
Regions = ["R%.3d" % i for i in Regions_]
APs = ["W%.3d" % i for i in APs_]
numRegions = len(Regions)
numAPs = len(APs)
#%%
# Import training datasets
# Folds
parts = ['part01','part02','part03','part04','part05','part06','part07','part08','part09','part10']
test_parts = ['part01','part02']
train_parts = [x for x in parts if x not in test_parts]
#%%
# SELECT TRAINING
Dtr = {}
for r in Regions:
Dtr[r] = pd.DataFrame()
for p in train_parts:
name = r+".csv"
Dtr[r+p] = pd.read_csv(path_parts+p+"/"+name, sep=" ")
Dtr[r+p] = Dtr[r+p][APs]
Dtr[r] = Dtr[r].append(Dtr[r+p], ignore_index=True)
print("Reading "+ path_parts+p+"/"+name)
# SELECT TESTING
Dte = {}
for r in Regions:
Dte[r] = pd.DataFrame()
for p in test_parts:
name = r+".csv"
Dte[r+p] = pd.read_csv(path_parts+p+"/"+name, sep=" ")
Dte[r+p] = Dte[r+p][APs]
Dte[r] = Dte[r].append(Dte[r+p], ignore_index=True)
print("Reading "+ path_parts+p+"/"+name)
#%%
#%%
# CLEAN THE TRAINING RSSI DATASETS BY USING GAUSSIAN CURVES
# Import training datasets
# Define function to calcule Gaussian histogram
def hist_gauss(values):
val = sorted(values[values!=-100.0])
x = range(-100, -10)
mu = np.mean(val)
sigma = np.std(val)
if np.isnan(mu) and np.isnan(sigma):
y = [0]*len(x)
elif mu==0 or sigma==0:
y = [0]*len(x)
else:
y = (1 / (np.sqrt(2 * np.pi * np.power(sigma, 2)))) * \
(np.power(np.e, -(np.power((x - mu), 2) / (2 * np.power(sigma, 2)))))
return y
# Calculate Gaussian Histograms
Dtr_g = {}
for r in Regions:
for w in APs:
print("Calculating Gaussian Histogram for "+r+"_"+w)
Dtr_g[r+"_"+w] = hist_gauss(Dtr[r][w])
# As instance, draw histograms at region R1
R_draw = Regions[0]
print("Ploting for " + R_draw+ "\n ...")
color = cm.rainbow(np.linspace(0,1,numAPs+1))
# Plot Histogram
for w in APs:
plt.hist(Dtr[R_draw][w][Dtr[R_draw][w]!=-100],bins=range(-100, -10),alpha=0.5,color=color[APs.index(w)],density=True,label=w+" histogram")
# Plot Gaussians
for w in APs:
plt.plot(range(-100, -10), Dtr_g[R_draw+"_"+w],color=color[APs.index(w)],label=w+" Gaussian histogram")
# Show
plt.title("APs histograms at "+"region " + R_draw)
plt.legend()
plt.show()
#%%
# PERFORM ANALYSIS OF BAYESIAN FILTERS OFFLINE
dict_params = {}
dict_w_post = {}
# Create tables for Wn
for w in APs:
dict_params[w] = [0]*90
for r in Regions:
dict_params[w] = np.vstack((dict_params[w],Dtr_g[r+"_"+w]))
dict_params[w] = dict_params[w][1:numRegions+1][:]
# W1 is composed by:
# R1 x W1_gauss
# R2 x W1_gauss
# R3 x W1_gauss
# R4 x W1_gauss
# Function for prediction using Bayes
def prediction_region(row):
bins = range(-100, -9)
for w in APs:
dict_params["value"+w] = row[w]
dict_params["indx"+w] = bins.index(dict_params["value"+w])
# we take uniform distribution as prior
for w in APs:
dict_params["prior"+w] = [0]*1
for r in Regions:
dict_params["prior"+w] = np.vstack((dict_params["prior"+w],[1.0/numRegions]))
dict_params["prior"+w] = dict_params["prior"+w][1:numRegions+1][:]
print("prior"+w+"\n", np.array(dict_params["prior"+w]).round(decimals=3))
accuracy = 0
while accuracy < 0.95:
print("\n")
for w in APs:
print ("Starting Bayes for "+w)
# Apply Bayes
dict_params["posterior"+w] = dict_params["prior"+w] * np.vstack(dict_params[w][:,dict_params["indx"+w]])
if np.sum(dict_params["posterior"+w])!= 0:
# Normalization
dict_params["posterior"+w+"_norm"] = np.asarray([float(i)/sum(dict_params["posterior"+w]) for i in dict_params["posterior"+w]])
# Max normalized value
dict_w_post["max_"+"posterior"+w+"_norm"] = np.amax(dict_params["posterior"+w+"_norm"])
# Predicted region
dict_params["posterior"+w+"_pred"] = Regions[np.where(dict_params["posterior"+w+"_norm"]==dict_w_post["max_"+"posterior"+w+"_norm"])[0][0]]
else:
# Normalization
dict_params["posterior"+w+"_norm"] = [0]*len(dict_params["posterior"+w])
# Max normalized value
dict_w_post["max_"+"posterior"+w+"_norm"] = 0
# Predicted region
dict_params["posterior"+w+"_pred"] = 0
print("posterior"+w+"_norm",": \n", np.array(dict_params["posterior"+w+"_norm"]).round(decimals=3))
# Select the highest accuracy after the first iteration
# be careful when two regions have the same accuracy
max_W = max(dict_w_post.items(), key=operator.itemgetter(1))[0][13:17]
# assign values
for w in APs:
dict_params["prior"+w] = dict_params["posterior"+max_W+"_norm"]
accuracy = dict_w_post["max_"+"posterior"+max_W+"_norm"]
prediction = int(dict_params["posterior"+max_W+"_pred"][1:4])
print("\nContinue with ",max_W)
print("Pred:", "", dict_params["posterior"+max_W+"_pred"], ", Acc:", np.array(accuracy).round(decimals=3))
return prediction, accuracy
#%%
#%%
# Evaluating for a single measurement
values = {}
for w in APs:
values[w] = -60
for w in APs:
print(w+" meassure:"+str(values[w])+"dBm")
print("\n \n")
pred, acc = prediction_region(values)
print("\n \n")
print("Predicted region:", pred)
print("Accuracy:", "%.2f" % (100*acc), "%")
#%%
#%%
## CONSTRUCTION OF CONFUSION MATRIX
# Read testing dataset
for r in Regions:
# assign true values
d = {'true': int(r[1:4])*np.ones(len(Dte[r]), dtype=int)}
df = pd.DataFrame(data=d)
Dte[r] = Dte[r].join(df)
acc = Dte[Regions[0]]
Regionss = list(Regions)
Regionss.remove(Regionss[0])
for r in Regionss:
acc = acc.append(Dte[r])
test_R = acc.reset_index(drop=True)
# Shuffle all testing dataset
test_R = test_R.sample(frac=1).reset_index(drop=True)
# True values of testing dataset
R_true = test_R["true"].tolist()
# Predicted values of testing dataset
R_predicted = test_R.apply(prediction_region, axis=1)
R_predicted = [row[0] for row in R_predicted]
# Set labels
labels = Regions
# Calculation of Confusion Matrix
cm = confusion_matrix(R_true, R_predicted)
# Normalize Confusion Matrix
cm = cm / cm.astype(np.float).sum(axis=1)
# Plot Confusion Matrix
fig = plt.figure(figsize = (numRegions,numRegions))
ax = fig.add_subplot(111)
cax = ax.matshow(cm)
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, float("{0:.2f}".format(round(cm[i, j],2))),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
i = range(len(labels))
ax.set(xticks=i, xticklabels=labels, yticks=i, yticklabels=labels)
plt.xlabel('Predicted')
plt.ylabel('True')
#plt.savefig('confusion_matrix.png', format='png')
plt.show()
# Show accuracy
acc = 100*accuracy_score(R_true, R_predicted)
print("\n\n")
print("========================")
print("Overall accuracy: "+ "%.2f" % acc+"%")
print("========================")
#%%
#%%