Parcourir la source

optimized cluster selection

Florian il y a 7 ans
Parent
commit
20a46fdf75
2 fichiers modifiés avec 11 ajouts et 2 suppressions
  1. 9 2
      plan_gen/plan_gen.py
  2. 2 0
      plan_gen/plan_gen_cli.py

+ 9 - 2
plan_gen/plan_gen.py

@@ -85,6 +85,15 @@ def make_densities(nb_clusters, centers=None, radius=None):
         densities += make_gaussian(nb_clusters, center=c, radius=radius[n])
     return densities
 
+def clean_densities(densities, clusters):
+    ''' clean density probability if a cluster is empty '''
+    densities = densities.flatten()
+    clusters = clusters.flatten()
+    for n, c in enumerate(clusters):
+        if c is None:
+            densities[n] = 0.0
+    return densities
+
 # random generators
 # ------------------
 
@@ -101,8 +110,6 @@ def rand_node_xy(nodes, clusters, densities):
     clusters = clusters.flatten()
     densities = densities.flatten()
     cluster = np.random.choice(clusters, p=densities/sum(densities))
-    while cluster is None:
-        cluster = np.random.choice(clusters, p=densities/sum(densities))
     node = cluster[np.random.randint(len(cluster))]
     return (node.get('x'), node.get('y'))
 

+ 2 - 0
plan_gen/plan_gen_cli.py

@@ -35,7 +35,9 @@ if __name__ == '__main__':
     NODES = pg.get_nodes(INPUT_NETWORK)
     CLUSTERS = pg.make_clusters(NB_CLUSTERS, NODES)
     H_DENSITIES = pg.make_densities(NB_CLUSTERS, H_CENTERS, H_RADIUS)
+    H_DENSITIES = pg.clean_densities(H_DENSITIES, CLUSTERS)
     W_DENSITIES = pg.make_densities(NB_CLUSTERS, W_CENTERS, W_RADIUS)
+    W_DENSITIES = pg.clean_densities(W_DENSITIES, CLUSTERS)
 
     # make xml
     SEEDS = np.random.random_integers(0, 1e9, NB_CORES)