-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
87 lines (68 loc) · 2.73 KB
/
preprocessing.py
File metadata and controls
87 lines (68 loc) · 2.73 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
#!/bin/python
"""
Preprocessing phase consists of:
* Takes a json file and creates another one with "r_" preceded;
* For every collection/location transform the array of fingerprints
into a single fingerprint;
* Reads _whitelist.json_ file and replaces the equivalences;
"""
import sys
import json
import math
def transform_rssi(rssi):
min_rssi = -100
positive = rssi - min_rssi
return pow(positive, math.e)/pow(-min_rssi, math.e)
json_file = sys.argv[1]
f = open(json_file)
data = json.load(f)
w = open('whitelist.json', 'r')
w_list = json.load(w)
collections = {}
# while 'collection'+str(coll_no) in data:
for c in data.keys():
collection = data[c]
# Transform array of fingerprints into a single fingerprint
fingerprints = collection['fingerprints']
if not fingerprints:
continue
# fingerprint = fingerprints[0]
fingerprint = {}
if 'timestamp' in fingerprints[0]:
fingerprint['timestamp'] = fingerprints[0]['timestamp']
fingerprint['wifi'] = {}
if 'ble' in fingerprints[0]:
fingerprint['ble'] = fingerprints[0]['ble']
if 'gps' in fingerprints[0]:
fingerprint['gps'] = fingerprints[0]['gps']
if 'telephony' in fingerprints[0]:
fingerprint['telephony'] = fingerprints[0]['telephony']
for i,f in enumerate(fingerprints):
if i==0:
continue
eq_mac = None
for mac in f["wifi"].keys():
for key in w_list:
if mac in w_list[key]:
eq_mac = key
if not eq_mac:
continue
# If new MAC, add it to the collection
if not eq_mac in fingerprint["wifi"]:
fingerprint["wifi"][eq_mac] = f["wifi"][mac]
fingerprint["wifi"][eq_mac]['rssi'] = [transform_rssi(int(f["wifi"][mac]['rssi']))]
else: # If existing MAC, add only the rssi value
# If rssi is a string, transform it to an 1 element array
if isinstance(fingerprint["wifi"][eq_mac]["rssi"], str):
fingerprint["wifi"][eq_mac]["rssi"] = [transform_rssi(int(f["wifi"][mac]["rssi"]))]
# If rssi is an array, add the rssi value to array
if isinstance(fingerprint["wifi"][eq_mac]["rssi"], list):
fingerprint["wifi"][eq_mac]["rssi"].append(transform_rssi(int(f["wifi"][mac]["rssi"])))
for mac in f["ble"].keys():
if not mac in fingerprint["ble"]:
fingerprint["ble"][mac] = f["ble"][mac]
collection["fingerprints"] = fingerprint
collections[c] = collection
with open("p_"+json_file, "w+") as outfile:
json.dump(collections, outfile, indent = 4)
outfile.close()