-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgen_adj_mx_ag.py
More file actions
94 lines (78 loc) · 3.03 KB
/
gen_adj_mx_ag.py
File metadata and controls
94 lines (78 loc) · 3.03 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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import pandas as pd
import pickle
import tensorflow as tf
import Queue as Q
from math import sin, cos, sqrt, atan2, radians
DEBUG = True
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('sensor_ids_filename', 'data/coordinate.csv',
'File containing sensor ids separated by comma.')
flags.DEFINE_float('k', 2, 'The number of kNN neighbors which is a hypter parameter')
flags.DEFINE_string('output_pkl_filename', 'data/sensor_graph/adj_mat.pkl', 'Path of the output file.')
def get_distance(lat_1, lng_1, lat_2, lng_2):
# radius of earth in km
R = 6373.0
dlng = radians(lat_2) - radians(lat_1)
dlat = radians(lng_2) - radians(lng_1)
x = dlat*cos((lat_1+lat_2)*0.5)
d = R * sqrt(x**2 + dlng**2)
if DEBUG:
print (d)
return d
def kNN(sensor_id_list, lat_list, long_list, id, k=2):
kNN_PQ = Q.PriorityQueue(maxsize=k)
nearest_distance = np.inf
nearest_id = -1
for _id in sensor_id_list:
if _id == id:
continue
dis = -get_distance(lat_list[id-1], long_list[id-1], lat_list[_id-1], long_list[_id-1])
if not kNN_PQ.full():
kNN_PQ.put([dis, _id])
else:
temp_data = kNN_PQ.get()
temp_dis = temp_data[0]
temp_label = temp_data[1]
if dis > temp_dis:
temp_dis = dis
temp_label = _id
kNN_PQ.put([temp_dis, temp_label])
return kNN_PQ
def get_adjacency_matrix(sensor_id_list, sensor_name, lat_list, long_list, k=2):
"""
:sensor_id_list: list of sensor id
:lat_list: list of latitude of sensor
:long_list: list of longtitude of sensor
"""
sensor_num = len(sensor_id_list)
dist_mx = np.zeros((sensor_num, sensor_num), dtype=np.float32)
sensor_name2id = {}
# Construct the graph using kNN, add edge to k nearest neighors
for idx, id in enumerate(sensor_id_list):
if DEBUG:
print (sensor_name[idx], id-1)
sensor_name2id[sensor_name[idx]] = id-1
knn_pq = kNN(sensor_id_list, lat_list, long_list, id, k)
knn_list = knn_pq.queue
for nei in knn_list:
dist_mx[id - 1, nei[1] - 1] = 1
dist_mx[id - 1][id - 1] = 1
if DEBUG:
print (dist_mx)
return sensor_name2id, dist_mx
if __name__ == '__main__':
sensor_info = pd.read_csv(FLAGS.sensor_ids_filename, dtype={'senor_id':'int', 'senor_name':'str', 'Lat':'float', 'Long':'float'})
sensor_id_list = sensor_info['senor_id'].tolist()
sensor_name = sensor_info['sensor_name'].tolist()
lat_list = sensor_info['Lat'].tolist()
long_list = sensor_info['Long'].tolist()
if DEBUG:
print(sensor_id_list, lat_list, long_list)
sensor_name2id, adj_mx = get_adjacency_matrix(sensor_id_list, sensor_name, lat_list, long_list)
with open(FLAGS.output_pkl_filename, 'w') as f:
pickle.dump([sensor_name, sensor_name2id, adj_mx], f)