Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.10 KB | None | 0 0
  1. def apply_points_cluster_mam_on_gdf_multiprocess(
  2.     network,
  3.     network_element_dict,
  4.     ign_parcel,
  5.     tigre_parcel,
  6.     size_of_range=config.MAM_GRID_SIZE,
  7.     step_of_range=config.MAM_GRID_STEP,
  8.     nb_evals=config.MAM_NB_EVALS,
  9.     how=config.MAM_OBJECTIVE_METHOD,
  10.     max_distance_for_clusters=config.MAM_MAX_DIST_CLUST,
  11.     point_buffer=config.MAM_POINT_BUFFER,
  12.     save_translation_file=False,
  13.     parcel_buffer=config.MERGE_PARCEL_BUFFER,
  14.     mam_buffer_aug_rate=config.MAM_BUFFER_AUG_RATE,
  15.     mam_aug_buffer_min_threshold=config.MAM_PARCEL_AREA_THRESOLD,
  16. ):
  17.     """ Apply the points cluster mam on a whole gdf with multiprocess
  18.    
  19.    Arguments:
  20.        network {GeoDataFrame} -- network gdf to move
  21.        chambers {GeoDataFrame} -- chambers gdf to move
  22.        size_of_range {float} -- size of the grid in each direction. The length of the
  23.            grid = size_of_range*2
  24.        step_of_range {float} -- step for moving on the grid.
  25.        nb_evals {int} -- number of evaluations for hyperopt
  26.        how {string} -- method used to evaluate the best move how. sum if how == 'sum',
  27.        ratio if anything else
  28.        save_translation_file {boolean} -- if True, will save the and X,Y delta for each
  29.            point clustered
  30.        parcel_buffer - in meter : the buffer used to merge parcels that are close together
  31.    Returns:
  32.        moved_gdf {GeoDataFrame} -- moved GeoDataFrame
  33.    """
  34.     to_move_gdf = network.copy()
  35.     to_move_gdf = to_move_gdf[to_move_gdf.id.isin([882714,
  36. 4766449,
  37. 4766450,
  38. 4766451,
  39. 810344
  40. ])]
  41.     assert len(to_move_gdf) > 0
  42.     to_move_gdf = explode_multilines(to_move_gdf)
  43.  
  44.     logger.info("Create list of unique points and their affected clusters")
  45.     clustered_points = create_unique_points_clusters(to_move_gdf, max_distance_for_clusters)
  46.     nb_clusters = len(clustered_points.cluster.unique())
  47.     nb_points = len(clustered_points)
  48.  
  49.     stats_nb_points = {"nb_clusters": nb_clusters, "nb_points": nb_points}
  50.     logger.info("There are {nb_clusters} clusters, from {nb_points} points".format(**stats_nb_points))
  51.    
  52.     unary_parcel = ign_parcel.buffer(parcel_buffer).unary_union
  53.     logger.info("Launch multi-process mam for network")
  54.     executor = concurrent.futures.ProcessPoolExecutor(config.MAM_MULTIPROCESS_NB_EXEC)
  55.     futures = [
  56.         executor.submit(
  57.             apply_points_cluster_mam,
  58.             ign_parcel,
  59.             tigre_parcel,
  60.             unary_parcel,
  61.             nb_cluster,
  62.             clustered_points,
  63.             size_of_range,
  64.             step_of_range,
  65.             nb_evals,
  66.             how,
  67.             point_buffer,
  68.             mam_buffer_aug_rate,
  69.             mam_aug_buffer_min_threshold,
  70.         )
  71.         for nb_cluster in range(nb_clusters)
  72.     ]
  73.     concurrent.futures.wait(futures),
  74.     futures_df = _get_futures_df(futures, save_translation_file=save_translation_file)
  75.     # we check that all the clusters has been computed
  76.     assert len(futures_df.index) == nb_clusters
  77.    
  78.     logger.info("Moving network elements...")
  79.     assignement_element_points_dict = {}
  80.     rejection_element_dict = {}
  81.     rejected_element_nearest_neighbor_dict = {}
  82.     for key, value in network_element_dict.items():
  83.         assignement_element_points, rejection_element_list = assign_points_to_cluster(
  84.             value, clustered_points
  85.         )
  86.         assignement_element_points_dict[key] = assignement_element_points
  87.         rejection_element_dict[key] = rejection_element_list
  88.         rejected_element_nearest_neighbor_dict[key] = handle_rejected_cases(
  89.             rejection_element_list, clustered_points
  90.         )
  91.     moved_network = _move_network(
  92.         to_move_gdf, clustered_points, futures_df, nb_clusters,
  93.     )
  94.  
  95.     moved_network_dict = {}
  96.     for key in assignement_element_points_dict.keys():
  97.         moved_network_dict[key] = _move_network_element(
  98.             network_element_dict[key],
  99.             assignement_element_points_dict[key],
  100.             futures_df,
  101.             nb_clusters,
  102.         )
  103.     return rejection_element_dict, moved_network, moved_network_dict, stats_nb_points
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement