-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBKMap.py
More file actions
377 lines (310 loc) · 12.3 KB
/
Copy pathBKMap.py
File metadata and controls
377 lines (310 loc) · 12.3 KB
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
from tkinter import Canvas, Tk
import numpy as np
import rasterio
from matplotlib import cm
from PIL import Image, ImageTk
from Logger import Logger
import time
from Log_to_csv_mvp import log_to_csv
import csv
from collections import defaultdict
from statistics import mean
import math # Needed for Euclidean distance (square root)
class BKMap(Tk):
def __init__(self, filename):
Tk.__init__(self)
self.interface()
self.logger = Logger(filename)
self.log = {}
self.scores = {}
self.points = []
self.filename = filename
self.title = 'Map of Bouwkunde'
self.h = 1080
self.w = 1920
self.currentPos = (0, 0)
self.resizable(0,0)
self.bind("q", self.exit)
self.bind("l", self.logging)
self.bind("p", self.printLogs)
self.bind("x", self.deletePoint)
self.bind("d", self.dictToWNT)
self.bind("s", self.saveLogs)
self.isLogging = False
self.output_info()
self.setTexts()
self.actualPointIndex = None
self.openImage()
self.setCanvas()
self.setDisplay()
self.protocol('WM_DELETE_WINDOW', self.quitProgram)
self.isRunning = True
self.draw()
def interface(self):
print("\n" + "=" * 170)
print(" WI-FI FINGERPRINTING")
print(" TREASURE MAP")
print("=" * 170)
# =======================
# KNOBS FOR TREASURE HUNT
# =======================
# MINIMUM RELIABILITY: as a fraction
MIN_RELIABILITY_PERCENT = input("Minimum reliability percentage (default 0): ")
if MIN_RELIABILITY_PERCENT == "":
MIN_RELIABILITY_PERCENT = 0.00
else:
MIN_RELIABILITY_PERCENT = float(MIN_RELIABILITY_PERCENT) / 100.0
# Missing-signal penalty (dBm)
MISSING_PENALTY_RSS = input("Missing penalty RSS (default: -100): ")
if MISSING_PENALTY_RSS == "":
MISSING_PENALTY_RSS = -100
else:
MISSING_PENALTY_RSS = int(MISSING_PENALTY_RSS)
# Scoring algorithm (M or E)
SCORING_ALGORITHM = input("Choose scoring algorithm, (default) M=Manhattan or E=Euclidean: ")
if SCORING_ALGORITHM == "":
SCORING_ALGORITHM = "M"
else:
SCORING_ALGORITHM = SCORING_ALGORITHM.strip().upper()
# Refreshing time
REFRESHING_TIME = input("Refreshing time in seconds (default 2): ")
if REFRESHING_TIME == "":
REFRESHING_TIME = 2
else:
REFRESHING_TIME = float(REFRESHING_TIME)
# Difference tolerance
TOLL = input("Tolerance (default 3): ")
if TOLL == "":
TOLL = 3
else:
TOLL = float(TOLL)
self.min_rel_per = MIN_RELIABILITY_PERCENT
self.mis_pen_rss = MISSING_PENALTY_RSS
self.scor_alg = SCORING_ALGORITHM
self.ref_time = REFRESHING_TIME
self.toll = TOLL
# =========
# LOAD DATA
# =========
self.target_files = {
"Location_1" : "location_1.csv",
"Location_2" : "location_2.csv",
"Location_3" : "location_3.csv",
"Location_4" : "location_4.csv",
}
self.test_files = {
"Test_Location_1" : "known_locations/known_1.csv",
"Test_Location_2" : "known_locations/known_2.csv",
"Test_Location_3" : "known_locations/known_3.csv",
"Test_Location_4" : "known_locations/known_4.csv"
}
def setDisplay(self):
self.bind("<Motion>", self.displayCoords)
self.bind("<ButtonRelease-1>", self.onLeftMouseClick)
self.bind("<ButtonRelease-3>", self.onRightMouseClick)
def setTexts(self):
self.logtxt = "Not logging :("
self.coordtxt = "0, 0"
def output_info(self):
print(
f"=====Use=====\n\
Press on the map to add location. \n\
Press 'L' to start or stop logging\n\
Press 'Q' to quit the program \n\
Press 'P' to print logs\n\
Rightclick on point to select and 'X' to remove\n\
Press 'S' to save dict as a txt file\n\
Press 'D' to write points to WKT with scores\n\
============= "
)
def setCanvas(self):
self.canvas = Canvas(self, bg = 'white', width = self.w, height=self.h)
self.canvas.pack()
self.canvas.create_image(0,0,image=self.img, anchor= 'nw')
def openImage(self):
img = Image.open('BKPlan.jpg')
self.w = img.width
self.h = img.height
self.img = ImageTk.PhotoImage(img)
def displayCoords(self, event):
if (
(event.x > 0)
and (event.x < self.w)
and (event.y > 0)
and (event.y < self.h)
):
wx, wy = (event.x, event.y)
self.coordtxt = f'{wx}, {wy}'
self.canvas.itemconfig(self.coordstext, text=self.coordtxt)
def onLeftMouseClick(self, event):
self.points.append([event.x, event.y, 'red'])
if self.isLogging:
self.logger.endLog()
self.log[self.currentPos] = self.logger.getLogs()
self.logger.startLog()
self.currentPos = (event.x, event.y)
def onRightMouseClick(self, event):
toleranceRadius = 6
for i, pt in enumerate(self.points):
if abs(pt[0] - event.x) <= toleranceRadius and abs(pt[1] - event.y) <= toleranceRadius:
if self.actualPointIndex is not None:
self.points[self.actualPointIndex][2] = 'red'
self.points[i][2] = 'green'
self.actualPointIndex = i
def drawPoint(self, x, y, col):
radius = 3
self.canvas.create_oval(x - radius, y - radius, x + radius, y + radius, fill=col)
def logging(self, event):
if self.isLogging:
self.isLogging = False
self.logger.endLog()
try:
self.log[self.currentPos]
except:
self.log[self.currentPos] = self.logger.getLogs()
else:
self.log[self.currentPos] += self.logger.getLogs()
self.getScores(self.currentPos)
self.logtxt = 'Not logging :('
else:
self.isLogging = True
self.logger.startLog()
self.logtxt = 'Logging! :D'
self.canvas.itemconfig(self.loggingText, text = self.logtxt)
def draw(self):
while self.isRunning:
self.canvas.delete('all')
self.canvas.create_image(0,0,image=self.img, anchor= 'nw')
for pt in self.points:
self.drawPoint(pt[0], pt[1], pt[2])
self.coordstext = self.canvas.create_text(
self.w, self.h, fill="black", anchor="se", text= self.coordtxt
)
self.loggingText = self.canvas.create_text(
self.w//2, 0, fill="black", anchor="n", text = self.logtxt
)
self.canvas.update()
def printLogs(self, event):
for key in self.log.keys():
print(f'{key}: {self.log[key][:5]}')
def deletePoint(self, event):
if self.isLogging:
return
del self.points[self.actualPointIndex]
try:
keys = list(self.log)
key = keys[self.actualPointIndex]
self.log.pop(key)
except:
print('Point had no logs')
self.actualPointIndex = None
def getScores(self, key):
x,y = key[0], key[1]
fileName = f"Live_log_{x}_{y}.txt"
with open(fileName, 'w') as f:
for line in self.log[key]:
f.write(f'{line}')
csvFile = log_to_csv(fileName, 'eduroam', key)
self.scores[key] = self.compute_score(csvFile)
def exit(self, event):
print('Bye!')
self.isRunning = False
self.destroy()
time.sleep(1)
def quitProgram(self):
print('Bye!')
self.isRunning = False
self.destroy()
time.sleep(1)
def process_csv_fingerprint(self, filepath):
mac_rss_values = defaultdict(list) # values are lists (become rss_list)
total_rows = 0
with open(filepath, 'r', newline='') as file:
reader = csv.reader(file)
# Handle potential different headers or skip first row
header = next(reader, None)
# quick checks
for row in reader:
if len(row) < 4: continue # check valid row length
ssid = row[2].strip()
if "eduroam" not in ssid.lower(): # check if "eduroam" is listed
continue
total_rows += 1
mac = row[1].strip() # collect MAC
try:
rss = int(row[3].strip()) # collect RSS
mac_rss_values[mac].append(rss) # (key <- mac): (value <- rss_list)
except ValueError:
continue
if total_rows == 0:
print(" [WARNING] No valid Eduroam data found in file.")
return None, 0, 0
# FILTERING
final_fingerprint = {}
dropped_macs = 0
for mac, rss_list in mac_rss_values.items():
# Frequency Check: How often did this MAC appear?
observation_count = len(rss_list)
reliability = observation_count / total_rows # as proportion
if reliability >= self.min_rel_per:
# stable signal -> Add Average RSS to fingerprint
avg_rss = round(mean(rss_list))
final_fingerprint[mac] = avg_rss
else:
# It's noise
dropped_macs += 1
# print(f" [DONE] Kept {len(final_fingerprint)} stable APs. Dropped {dropped_macs} noisy signals.")
kept_count = len(final_fingerprint)
return final_fingerprint, kept_count, total_rows
def score(self, target_fp, live_fp):
final_score = 0
if self.scor_alg == "M": # Manhattan
for mac, target_rss in target_fp.items():
if mac in live_fp:
final_score += abs(target_rss - live_fp[mac])
else:
final_score += abs(target_rss - self.mis_pen_rss)
elif self.scor_alg == 'E':
sum_sq_diff = 0 # Euclidean
for mac, target_rss in target_fp.items():
if mac in live_fp:
diff = target_rss - live_fp[mac]
sum_sq_diff += (diff ** 2)
else:
diff = target_rss - self.mis_pen_rss
sum_sq_diff += (diff ** 2)
final_score = math.sqrt(sum_sq_diff)
# Penalize strong alien signals
for mac, live_rss in live_fp.items():
if mac not in target_fp and live_rss > -60:
final_score += 20
return final_score
def dictToWNT(self,event):
with open('WTKPoints.txt', 'w') as f:
for key, lst in self.scores.items():
f.write(f'{key[0]},{key[1]},{lst[0]},{lst[1]},{lst[2]},{lst[3]}\n')
def compute_score(self, live_observation):
targets = {}
for location, location_path in self.target_files.items():
fp, kept, total = self.process_csv_fingerprint(location_path)
if not fp:
print(f"Warning: no fingerprint for {location}, skipping.")
targets[location] = {"fp": math.inf, "kept": kept, "total": total}
targets[location] = {"fp": fp, "kept": kept, "total": total}
live_fp, live_kept, live_total = self.process_csv_fingerprint(live_observation)
if not live_fp:
print("Warning: no live fingerprint, skipping this cycle.")
score_lst = []
for location, target_info in targets.items():
target_fp = target_info["fp"]
live_score = self.score(target_fp, live_fp)
score_lst.append((location, live_score))
return score_lst
def saveLogs(self, event):
with open('AllLogs.txt', 'w') as f:
for key, item in self.log.items():
f.write(f'{key}: {item}\n')
def main():
Map = BKMap()
if __name__ == '__main__':
main()