Эх сурвалжийг харах

better commute analysis robustness

Martin 6 жил өмнө
parent
commit
9ecd251db2
1 өөрчлөгдсөн 28 нэмэгдсэн , 8 устгасан
  1. 28 8
      commute.py

+ 28 - 8
commute.py

@@ -87,6 +87,8 @@ def directions_call(source, destination, mode):
     resp_data = resp.json()
 
     if not resp_data.get('routes'):
+        if 'error_message' in resp_data:
+            print('Error: %s' % resp_data['error_message'])
         return
 
     _record_api_response(resp_data)
@@ -212,17 +214,17 @@ def log_graph(graph, commutes):
         print('')
 
 def analyze_locations(commutes, num_locations):
-    print('Loading new directions data for %i locations...' % num_locations)
-
     with db.connect() as conn:
         rows = conn.select('''select p.lat, p.lon, price from properties p
-                                where lat is not null and lon is not null and property_type != "Site"''')
+                                where lat is not null and lon is not null and property_type != "Site" and price is not null''')
 
         random.shuffle(rows)
         to_load = []
+        durations = []
+        paths = []
 
         for lat, lon, price in rows:
-            if not (250000 < price < 550000):
+            if not (config.MIN_PRICE < price < config.MAX_PRICE):
                 continue
 
             closest = None
@@ -235,21 +237,35 @@ def analyze_locations(commutes, num_locations):
                     closest_dist = dist
             
             target_loc = (lat, lon)
+            closest_dist = geo_util.ggcd(target_loc, closest)
 
-            if (geo_util.ggcd(target_loc, closest) > 300
-                and any(geo_util.inside(target_loc, bbox) for bbox in config.MONITORED_BBOXES)):
+            if (closest_dist > 300 and any(geo_util.inside(target_loc, bbox) for bbox in config.MONITORED_BBOXES)):
                 to_load.append(target_loc)
+            elif closest_dist <= 300:
+                durations.append(commutes[closest]['duration'] + closest_dist)
+                paths.append(commutes[closest]['path'])
         
+
         print('\tRemaining to load: %i' % len(to_load))
 
+        durations.sort()
+        print('Durations (min): ' + ', '.join(['%.2f' % (durations[int(len(durations) * x)] / 60.) 
+                                               for x in [0.05, 0.25, 0.5, 0.75, 0.95]]))
+
+        random.shuffle(paths)
+        for p in paths[:10]:
+            print(p)
+
+        return
         random.shuffle(to_load)
 
         # target = (53.699593,-6.259772)
         # scores = {loc: geo_util.ggcd(target, loc) for loc in to_load}
         # to_load.sort(key=lambda loc: scores[loc])
 
+        print('Loading new directions data for %i locations...' % num_locations)
         for loc in to_load[:num_locations]:
-            print('\tLoading %s...' % str(loc))
+            # print('\tLoading %s...' % str(loc))
             directions_call(loc, config.WORKPLACE, TransitMethod.TRANSIT)
 
 def add_commute_data(properties, commutes):
@@ -289,8 +305,12 @@ def add_commute_data(properties, commutes):
             'commute_path': best_path,
         })
 
-if __name__ == '__main__':
+def run():
     while True:
         graph = load_graph()
         commutes = determine_fastest_paths(graph, config.WORKPLACE)
         analyze_locations(commutes, 20)
+
+
+if __name__ == '__main__':
+    run()