def test_AZP_azp_connected_component__one_area(): adj = csr_matrix(np.array([[0]])) # adjacency matrix for a single node azp = AZP() obtained = azp._azp_connected_component(adj, initial_clustering=np.array([0]), attr=np.array([123])) desired = np.array([0]) assert obtained == desired
def azp(X, w, n_clusters=5, **kwargs): """AZP clustering algorithm. Parameters ---------- X : array-like n x k attribute data w : libpysal.weights.W instance spatial weights matrix n_clusters : int, optional, default: 5 The number of clusters to form. Returns ------- fitted cluster instance: region.p_regions.azp.AZP """ model = AZP() model.fit_from_w(attr=X.values, w=w, n_regions=n_clusters) return model
def fit_from_scipy_sparse_matrix( self, adj, attr, spatially_extensive_attr, threshold, max_it=10, objective_func=ObjectiveFunctionPairwise()): """ Solve the max-p-regions problem in a heuristic way (see [DAR2012]_). The resulting region labels are assigned to the instance's :attr:`labels_` attribute. Parameters ---------- adj : :class:`scipy.sparse.csr_matrix` Adjacency matrix representing the areas' contiguity relation. attr : :class:`numpy.ndarray` Array (number of areas x number of attributes) of areas' attributes relevant to clustering. spatially_extensive_attr : :class:`numpy.ndarray` Array (number of areas x number of attributes) of areas' attributes relevant to ensuring the threshold condition. threshold : numbers.Real or :class:`numpy.ndarray` The lower bound for a region's sum of spatially extensive attributes. The argument's type is numbers.Real if there is only one spatially extensive attribute per area, otherwise it is a one-dimensional array with as many entries as there are spatially extensive attributes per area. max_it : int, default: 10 The maximum number of partitions produced in the algorithm's construction phase. objective_func : :class:`region.objective_function.ObjectiveFunction`, default: ObjectiveFunctionPairwise() The objective function to use. """ print("f_f_SCIPY got:\n", attr, "\n", spatially_extensive_attr, "\n", threshold, sep="") weights = ps_api.WSP(adj).to_W() areas_dict = weights.neighbors self.metric = objective_func.metric best_partition = None best_obj_value = float("inf") feasible_partitions = [] partitions_before_enclaves_assignment = [] max_p = 0 # maximum number of regions # construction phase # print("constructing") for _ in range(max_it): # print(" ", _) partition, enclaves = self.grow_regions(adj, attr, spatially_extensive_attr, threshold) n_regions = len(partition) if n_regions > max_p: partitions_before_enclaves_assignment = [(partition, enclaves)] max_p = n_regions elif n_regions == max_p: partitions_before_enclaves_assignment.append( (partition, enclaves)) # print("\n" + "assigning enclaves") for partition, enclaves in partitions_before_enclaves_assignment: # print(" cleaning up in partition", partition) feasible_partitions.append( self.assign_enclaves(partition, enclaves, areas_dict, attr)) for partition in feasible_partitions: print(partition, "\n") # local search phase if self.local_search is None: self.local_search = AZP() self.local_search.allow_move_strategy = AllowMoveAZPMaxPRegions( spatially_extensive_attr, threshold, self.local_search.allow_move_strategy) for partition in feasible_partitions: self.local_search.fit_from_scipy_sparse_matrix( adj, attr, max_p, initial_labels=array_from_region_list(partition), objective_func=objective_func) partition = self.local_search.labels_ # print("optimized partition", partition) obj_value = objective_func(partition, attr) if obj_value < best_obj_value: best_obj_value = obj_value best_partition = partition self.labels_ = best_partition
def test_graph_str_multi_attr(): nx.set_node_attributes(graph, attr_str, attr_dict) cluster_object = AZP(random_state=0) cluster_object.fit_from_networkx(graph, double_attr_str, n_regions=2) result = region_list_from_array(cluster_object.labels_) compare_region_lists(result, optimal_clustering)
def test_dict_multi_attr(): cluster_object = AZP(random_state=0) cluster_object.fit_from_dict(neighbors_dict, double_attr_dict, n_regions=2) obtained = region_list_from_array(cluster_object.labels_) compare_region_lists(obtained, optimal_clustering)
def test_geodataframe_multi_attr(): cluster_object = AZP(random_state=0) cluster_object.fit_from_geodataframe(gdf, double_attr_str, n_regions=2) obtained = region_list_from_array(cluster_object.labels_) compare_region_lists(obtained, optimal_clustering)
def test_scipy_sparse_matrix_multi_attr(): cluster_object = AZP(random_state=0) cluster_object.fit_from_scipy_sparse_matrix(adj, double_attr, n_regions=2) obtained = region_list_from_array(cluster_object.labels_) compare_region_lists(obtained, optimal_clustering)
def test_w_basic(): cluster_object = AZP(random_state=0) cluster_object.fit_from_w(w, attr, n_regions=2) result = region_list_from_array(cluster_object.labels_) compare_region_lists(result, optimal_clustering)
def test_graph_dict_basic(): cluster_object = AZP(random_state=0) cluster_object.fit_from_networkx(graph, attr_dict, n_regions=2) result = region_list_from_array(cluster_object.labels_) compare_region_lists(result, optimal_clustering)
def test_dict(): value_dict = dataframe_to_dict(gdf, attr_str) cluster_object = AZP(random_state=0) cluster_object.fit_from_dict(neighbors_dict, value_dict, n_regions=2) result = region_list_from_array(cluster_object.labels_) compare_region_lists(result, optimal_clustering)
def test_geodataframe(): cluster_object = AZP(random_state=0) cluster_object.fit_from_geodataframe(gdf, attr_str, n_regions=2) result = region_list_from_array(cluster_object.labels_) compare_region_lists(result, optimal_clustering)