def clustering(distribution, areal_units, classes=None): """ Return the clustering coefficient for the different classes Assume that the class `c` is overrepresented in `N_u` areal units, and that we obtain `N_c` clusters after aggregating the neighbourhings units. Parameter --------- distribution: nested dictionaries Number of people per class, per areal unit as given in the raw data (ungrouped). The dictionary must have the following formatting: > {areal_id: {class_id: number}} areal_units: dictionnary Dictionnary of areal unit ids with shapely polygon object representing the unit's geometry as values. classes: dictionary of lists When the original categories need to be aggregated into different classes. > {class: [categories belonging to this class]} This can be arbitrarily imposed, or computed with uncover_classes function of this package. Returns ------- clustering: dictionary Dictionary of classes names with clustering values. """ # Regroup into classes if specified. Otherwise return categories indicated # in the data if not classes: classes = return_categories(distribution) ## Get the number of neighbourhoods neigh = mb.neighbourhoods(distribution, areal_units, classes) num_neigh = {cl: len(neigh[cl]) for cl in classes} num_units = { cl: len([a for ne in neigh[cl] for a in ne]) for cl in classes } ## Compute clustering values clustering = {} for cl in classes: if num_units[cl] == 0: clustering[cl] = float('nan') elif num_units[cl] == 1: clustering[cl] = 1 else: clustering[cl] = _single_clustering(num_units[cl], num_neigh[cl]) clustering[cl] = ((num_neigh[cl] - num_units[cl]) / (1 - num_units[cl])) return clustering
def test_neighbourhoods_clust(self): """ Test the extraction of neighbourhoods """ city = clustered_city() units = grid() neigh = mb.neighbourhoods(city, units) neigh_answer = {"A": [[0, 1, 3, 6]], "B": [[2, 4, 5, 7, 8]]} assert len(neigh["A"]) == len(neigh_answer["A"]) assert len(neigh["B"]) == len(neigh_answer["B"])
def clustering(distribution, areal_units, classes=None): """ Return the clustering coefficient for the different classes Assume that the class `c` is overrepresented in `N_u` areal units, and that we obtain `N_c` clusters after aggregating the neighbourhings units. Parameter --------- distribution: nested dictionaries Number of people per class, per areal unit as given in the raw data (ungrouped). The dictionary must have the following formatting: > {areal_id: {class_id: number}} areal_units: dictionnary Dictionnary of areal unit ids with shapely polygon object representing the unit's geometry as values. classes: dictionary of lists When the original categories need to be aggregated into different classes. > {class: [categories belonging to this class]} This can be arbitrarily imposed, or computed with uncover_classes function of this package. Returns ------- clustering: dictionary Dictionary of classes names with clustering values. """ # Regroup into classes if specified. Otherwise return categories indicated # in the data if not classes: classes = return_categories(distribution) ## Get the number of neighbourhoods neigh = mb.neighbourhoods(distribution, areal_units, classes) num_neigh = {cl: len(neigh[cl]) for cl in classes} num_units = {cl: len([a for ne in neigh[cl] for a in ne]) for cl in classes} ## Compute clustering values clustering = {} for cl in classes: if num_units[cl] == 0: clustering[cl] = float('nan') elif num_units[cl] == 1: clustering[cl] = 1 else: clustering[cl] = _single_clustering(num_units[cl], num_neigh[cl]) clustering[cl] = ((num_neigh[cl] - num_units[cl]) / (1 - num_units[cl])) return clustering
def test_neighbourhoods_check(self): """ Test the extraction of neighbourhoods """ city = checkerboard_city() units = grid() neigh = mb.neighbourhoods(city, units) neigh_answer = {"A":[[0],[2],[4],[6],[8]], "B":[[1],[3],[5],[7]]} assert len(neigh["A"]) == len(neigh_answer["A"]) assert len(neigh["B"]) == len(neigh_answer["B"])
def test_neighbourhoods_clust(self): """ Test the extraction of neighbourhoods """ city = clustered_city() units = grid() neigh = mb.neighbourhoods(city, units) neigh_answer = {"A":[[0,1,3,6]], "B":[[2,4,5,7,8]]} assert len(neigh["A"]) == len(neigh_answer["A"]) assert len(neigh["B"]) == len(neigh_answer["B"])
def test_neighbourhoods_check(self): """ Test the extraction of neighbourhoods """ city = checkerboard_city() units = grid() neigh = mb.neighbourhoods(city, units) neigh_answer = { "A": [[0], [2], [4], [6], [8]], "B": [[1], [3], [5], [7]] } assert len(neigh["A"]) == len(neigh_answer["A"]) assert len(neigh["B"]) == len(neigh_answer["B"])