Parcourir la source

distribution visualizer (ugly)

Florian il y a 7 ans
Parent
commit
83be0c481b
4 fichiers modifiés avec 71 ajouts et 3 suppressions
  1. BIN
      input/map.png
  2. 1 1
      plan_gen/plan_gen.py
  3. 2 2
      plan_gen/plan_gen_cli.py
  4. 68 0
      plan_gen/plan_visualizer.py

BIN
input/map.png


+ 1 - 1
plan_gen/plan_gen.py

@@ -72,9 +72,9 @@ def make_clusters(nb_clusters, nodes):
 
 def make_densities(nb_clusters, centers=None, radius=None):
     ''' make a list of gaussian probability densities '''
-    densities = np.zeros((nb_clusters, nb_clusters))
     if centers is None:
         return make_gaussian(nb_clusters, radius=nb_clusters/2)
+    densities = np.zeros((nb_clusters, nb_clusters))
     for n, c in enumerate(centers):
         densities += make_gaussian(nb_clusters, center=c, radius=radius[n])
     return densities

+ 2 - 2
plan_gen/plan_gen_cli.py

@@ -7,8 +7,8 @@ import numpy as np
 import plan_gen.plan_gen as pg
 
 # TODO: make these constants as user params
-D_RADIUS = [10, 5]
-D_CENTERS = [(10, 5), (5,5)]
+D_CENTERS = [(27, 40), (32, 8)]
+D_RADIUS = [10, 10]
 
 if __name__ == '__main__':
 

+ 68 - 0
plan_gen/plan_visualizer.py

@@ -0,0 +1,68 @@
+#!/usr/bin/env python3
+''' quick plan_gen visualizer '''
+
+import sys
+import numpy as np
+import lxml.etree as etree
+import matplotlib
+
+def get_nodes(input):
+    tree = etree.parse(input)
+    return [node for node in tree.xpath("/network/nodes/node")]
+
+def get_persons(input):
+    tree = etree.parse(input)
+    return [p for p in tree.xpath("/plans/person")]
+
+if __name__ == '__main__':
+    matplotlib.use('TkAgg')
+    import matplotlib.pyplot as plt
+
+    NB_CLUSTERS = 50
+
+    # get data
+    NODES = get_nodes(sys.argv[1])
+    PERSONS = get_persons(sys.argv[2])
+    PERSONS_XY = ['{}|{}'.format(
+                  p.find('plan/act').get('x'),
+                  p.find('plan/act').get('y')) for p in PERSONS]
+    P_XY_UNIQUE, P_XY_COUNTS = np.unique(PERSONS_XY, return_counts=True)
+    
+    # plot init
+    FIG = plt.figure()
+    AX = FIG.add_subplot(111)
+
+    # plot nodes
+    NODES_X = [float(n.get('x')) for n in NODES]
+    NODES_Y = [float(n.get('y')) for n in NODES]
+    MIN_X, MAX_X = min(NODES_X), max(NODES_X)
+    MIN_Y, MAX_Y = min(NODES_Y), max(NODES_Y)
+    AX.scatter(NODES_X, NODES_Y,
+                marker='.', c='grey', linewidth=0.5, s=10, label='node')
+
+    # plot persons
+    PERSONS_X = [float(coord.split('|')[0]) for coord in P_XY_UNIQUE]
+    PERSONS_Y = [float(coord.split('|')[1]) for coord in P_XY_UNIQUE]
+    SC = AX.scatter(PERSONS_X, PERSONS_Y,
+                     alpha=0.75, s=P_XY_COUNTS*10,
+                     c=P_XY_COUNTS, cmap='YlOrRd', label='number of agents')
+
+    # plot map
+    img = plt.imread('input/map.png')
+    implot = plt.imshow(img, alpha=1, extent=[MIN_X, MAX_X, MIN_Y, MAX_Y])
+
+    # final plot
+    X_TICKS = np.arange(MIN_X, MAX_X, (MAX_X - MIN_X) / NB_CLUSTERS)
+    Y_TICKS = np.arange(MIN_Y, MAX_Y, (MAX_Y - MIN_Y) / NB_CLUSTERS)
+    TICK_LABELS = list(range(NB_CLUSTERS))
+    AX.set_title('Agents by nodes distribution (total: {} agents)'.format(len(PERSONS)))
+    AX.set_xlim(MIN_X, MAX_X)
+    AX.set_ylim(MIN_Y, MAX_Y)
+    AX.set_xticks(X_TICKS)
+    AX.set_yticks(Y_TICKS)
+    AX.set_xticklabels(TICK_LABELS)
+    AX.set_yticklabels(TICK_LABELS)
+    AX.grid(True)
+    plt.colorbar(SC)
+    plt.legend()
+    plt.show()