Browse Source

Add parameters for different bad wifi handling policy

Xi Ruoyao 5 years ago
parent
commit
e78a51772a
1 changed files with 40 additions and 11 deletions
  1. 40 11
      wifiloc.py

+ 40 - 11
wifiloc.py

@@ -67,7 +67,7 @@ def best_match(known_points, new_point, similarity):
             best = pt
     return best, max_
 
-def remove_bad_wifi(pts, crit = 500):
+def select_bad_wifi(pts, crit):
     from collections import namedtuple
     Loc = namedtuple('Loc', ['latitude', 'longitude'])
     mac_loc = {}
@@ -77,13 +77,6 @@ def remove_bad_wifi(pts, crit = 500):
             if not mac in mac_loc:
                 mac_loc[mac] = []
             loc = Loc(pts[i].latitude, pts[i].longitude)
-            """
-            for loc1 in mac_loc[mac]:
-                if dist(loc1, loc) > crit:
-                    print(loc1, loc, dist(loc1, loc))
-                    bad_mac.add(mac)
-                    break
-            """
             mac_loc[mac].append(loc)
         for mac in mac_loc:
             sum_la = 0.0
@@ -97,13 +90,32 @@ def remove_bad_wifi(pts, crit = 500):
                 if dist(cent, loc) > crit:
                     bad_mac.add(mac)
                     break
+    return bad_mac
+
+def remove_bad_wifi(pts, bad_mac):
     for i in range(len(pts)):
         for mac in bad_mac:
             if mac in pts[i].wifi_snr:
                 pts[i].wifi_snr.pop(mac)
+    return pts
+
+def remove_bad_wifi2(pts, bad_mac):
+    good_index = []
+    j = 0
+    for i in range(len(pts)):
+        bad = False
+        for mac in pts[i].wifi_snr:
+            if mac in bad_mac:
+                bad = True
+                break
+        if not bad:
+            pts[j] = pts[i]
+            j += 1
+    return pts[:j]
 
-def toplev(infile, sim, bad_wifi_crit, sim_crit, stat_cnt = 10,
-        stat_delta = 50):
+def toplev(infile, sim, sim_crit, bad_wifi_crit = 500, stat_cnt = 10,
+        stat_delta = 50, remove_bad_wifi_policy = 1,
+        skip_tests_with_bad_wifi = False):
     from itertools import groupby
     from random import shuffle
     data = read_data(infile)
@@ -122,13 +134,30 @@ def toplev(infile, sim, bad_wifi_crit, sim_crit, stat_cnt = 10,
         # shuffle(points)
         points.sort(key = lambda x:x.timestamp)
         train = points[:len(points)//2]
-        remove_bad_wifi(train, bad_wifi_crit)
+        if not remove_bad_wifi_policy in {0, 1, 2}:
+            raise Exception("unknown remove_bad_wifi_policy")
+        if remove_bad_wifi_policy > 0:
+            bad_wifi = select_bad_wifi(train, bad_wifi_crit)
+        else:
+            bad_wifi = {}
+        if remove_bad_wifi_policy == 1:
+            train = remove_bad_wifi(train, bad_wifi)
+        if remove_bad_wifi_policy == 2:
+            train = remove_bad_wifi2(train, bad_wifi)
         # print(len(train))
         verify = points[len(points)//2:]
         tot += len(verify)
         for pt in verify:
             if len(pt.wifi_snr) == 0:
                 continue
+            if skip_tests_with_bad_wifi:
+                have_bad_wifi = False
+                for mac in pt.wifi_snr:
+                    if mac in bad_wifi:
+                        have_bad_wifi = True
+                        break
+                if have_bad_wifi:
+                    continue
             pt1, sim1 = best_match(train, pt, sim)
             if sim1 is None or sim1 < sim_crit:
                 # print('no match, sim =', sim(pt1, pt))