Esempio n. 1
0
    def test_Decomposition(self):
        s_map = gpd.read_file(
            load_example("Sacramento1").get_path("sacramentot2.shp"))
        index1 = Dissim(s_map, 'HISP', 'TOT_POP')
        index2 = Dissim(s_map, 'BLACK', 'TOT_POP')
        res = DecomposeSegregation(index1,
                                   index2,
                                   counterfactual_approach="composition")
        np.testing.assert_almost_equal(res.c_a, -0.16138819842911295)
        np.testing.assert_almost_equal(res.c_s, -0.005104643275796905)
        res.plot(plot_type='cdfs')
        res.plot(plot_type='maps')

        res = DecomposeSegregation(index1,
                                   index2,
                                   counterfactual_approach="share")
        np.testing.assert_almost_equal(res.c_a, -0.1543828579279878)
        np.testing.assert_almost_equal(res.c_s, -0.012109983776922045)
        res.plot(plot_type='cdfs')
        res.plot(plot_type='maps')

        res = DecomposeSegregation(index1,
                                   index2,
                                   counterfactual_approach="dual_composition")
        np.testing.assert_almost_equal(res.c_a, -0.16159526946235048)
        np.testing.assert_almost_equal(res.c_s, -0.004897572242559378)
        res.plot(plot_type='cdfs')
        res.plot(plot_type='maps')
Esempio n. 2
0
    def test_Inference(self):
        s_map = gpd.read_file(
            load_example("Sacramento1").get_path("sacramentot2.shp"))
        index1 = Dissim(s_map, 'HISP_', 'TOT_POP')
        index2 = Dissim(s_map, 'BLACK_', 'TOT_POP')

        groups_list = ['WHITE_', 'BLACK_', 'ASIAN_', 'HISP_']
        m_index = MultiDissim(s_map, groups_list)

        m_index_1 = MultiDissim(s_map[0:200], groups_list)
        m_index_2 = MultiDissim(s_map[200:], groups_list)

        # Single Value Tests #
        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="systematic",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.01603886544282861,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="bootstrap",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.31992467511262773,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="evenness",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.01596295861644252,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="permutation",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.32184656076566864,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="systematic_permutation",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.01603886544282861,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="even_permutation",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.01619436868061094,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(m_index,
                              null_approach="bootstrap",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.4143544081847027,
                                       decimal=3)

        np.random.seed(123)
        res = SingleValueTest(m_index,
                              null_approach="evenness",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       0.01633979237418177,
                                       decimal=3)

        # Two Value Tests #
        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="random_label",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.0031386146371949076,
                                       decimal=3)

        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="counterfactual_composition",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.005032145622504718,
                                       decimal=3)

        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="counterfactual_share",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.034350440515125,
                                       decimal=3)

        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="counterfactual_dual_composition",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.004771386292706747,
                                       decimal=3)

        np.random.seed(123)
        res = TwoValueTest(m_index_1,
                           m_index_2,
                           null_approach="random_label",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.0024327144012562685,
                                       decimal=3)
Esempio n. 3
0
 def test_Dissim(self):
     s_map = gpd.read_file(
         load_example("Sacramento1").get_path("sacramentot2.shp"))
     df = s_map[['geometry', 'HISP_', 'TOT_POP']]
     index = Dissim(df, 'HISP_', 'TOT_POP')
     np.testing.assert_almost_equal(index.statistic, 0.32184656076566864)
Esempio n. 4
0
def segregationMisc(data_df,zipPolygon):
    caseWeekly_unstack=data_df['CasesWeekly'].unstack(level=0)
    caseWeekly_columnsReplace_dic={key:"case_"+str(key) for key in caseWeekly_unstack.columns}
    caseWeekly_unstack.rename(columns=caseWeekly_columnsReplace_dic,inplace=True)
    
    testWeekly_unstack=data_df['TestsWeekly'].unstack(level=0)
    testWeekly_columnsReplace_dic={key:"test_"+str(key) for key in testWeekly_unstack.columns}
    testWeekly_unstack.rename(columns=testWeekly_columnsReplace_dic,inplace=True)
    
    zip_codes= gpd.read_file(zipPolygon)
    data_df_zipGPD=zip_codes.merge(caseWeekly_unstack,left_on='zip', right_on=caseWeekly_unstack.index)
    data_df_zipGPD=data_df_zipGPD.merge(testWeekly_unstack,left_on='zip', right_on=testWeekly_unstack.index)
    # print(data_df_zipGPD)
    W=ps.lib.weights.Queen(data_df_zipGPD.geometry)
    W.transform = 'R'
    
    # weeks=idx_weekNumber=data_df.index.get_level_values('Week Number')
    # weeks=np.unique(weeks)
    columnsName=list(caseWeekly_columnsReplace_dic.values())+list(testWeekly_columnsReplace_dic.values())
    caseColumnsaName=list(caseWeekly_columnsReplace_dic.values())
    testColumnsName=list(testWeekly_columnsReplace_dic.values())
    
    # valArray=data_df_zipGPD[weeks].to_numpy()
    # valArray_fillNan=bfill(valArray).T
    # valArray_fillNan[np.isnan(valArray_fillNan)]=0  
    # # print(valArray_fillNan,valArray_fillNan.shape)  
    data_df_zipGPD_bfill=data_df_zipGPD.fillna(method='bfill',axis='columns')    
    data_df_zipGPD_bfill[columnsName]=data_df_zipGPD_bfill[columnsName].fillna(0)
    data_df_zipGPD_bfill[columnsName]=data_df_zipGPD_bfill[columnsName].astype('int32') #'int32'  float
    
    
    #A-aspatial indexes
    gdf=data_df_zipGPD_bfill[['case_19','test_19','case_15','test_15','geometry']]
    gdf=gdf[~(gdf==0).any(axis=1)]    
    
    #01-Dissimilarity
    from segregation.aspatial import Dissim
    index = Dissim(gdf, 'case_19','test_19')
    # print(type(index))
    print("dissimilarity:",index.statistic)
            
    #02-Gini
    from segregation.aspatial import GiniSeg
    index = GiniSeg(gdf, 'case_19','test_19')
    # type(index)
    print("Gini:",index.statistic)
    
    #03-Entropy
    from segregation.aspatial import Entropy
    index = Entropy(gdf, 'case_19','test_19')
    # type(index)
    print("Entropy:",index.statistic)
    
    #04-Atkinson
    from segregation.aspatial import Atkinson
    index = Atkinson(gdf, 'case_19','test_19', b = 0.5)
    # type(index)
    print("Atkinson:",index.statistic)
            
    
    #05-Concentration Profile
    from segregation.aspatial import ConProf
    index = ConProf(gdf, 'case_19','test_19')
    # type(index)
    print("Concentration Profile:",index.statistic)
    index.plot()
    
    #06-Isolation
    from segregation.aspatial import Isolation
    index = Isolation(gdf, 'case_19','test_19')
    # type(index)
    print("Isolation:",index.statistic)
    
    #07-Exposure
    from segregation.aspatial import Exposure
    index = Exposure(gdf, 'case_19','test_19')
    # type(index)
    print("Exposure:",index.statistic)
    
    #08-Correlation Ratio
    from segregation.aspatial import CorrelationR
    index = CorrelationR(gdf, 'case_19','test_19')
    # type(index)
    print("Correlation Ratio:",index.statistic)
    
    #09-Modified Dissimilarity
    from segregation.aspatial import ModifiedDissim
    index = ModifiedDissim(gdf, 'case_19','test_19', iterations = 500)
    # type(index)   
    print("Modified Dissimilarity:",index.statistic)
    
    #10-Modified Gini 
    from segregation.aspatial import ModifiedGiniSeg
    index = ModifiedGiniSeg(gdf, 'case_19','test_19', iterations = 500)
    # type(index)
    print("Modified Gini :",index.statistic)
    
    # #11-Bias-Corrected Dissimilarity
    # #FloatingPointError: invalid value encountered in true_divide
    # from segregation.aspatial import BiasCorrectedDissim
    # index = BiasCorrectedDissim(gdf, 'case_19','test_19', B = 500)
    # # type(index)
    # print("Bias-Corrected Dissimilarity:",index.statistic)
    
    #12-Density-Corrected Dissimilarity
    from segregation.aspatial import DensityCorrectedDissim
    index = DensityCorrectedDissim(gdf, 'case_19','test_19', xtol = 1e-5)
    # type(index)
    print("Density-Corrected Dissimilarity:",index.statistic)
    
    #13-Minimum-Maximum Index (MM)
    from segregation.aspatial import MinMax
    index = MinMax(gdf, 'case_19','test_19')
    # type(index)
    print("Minimum-Maximum Index (MM):",index.statistic)
    
    
    #B-Decomposition framework of the PySAL segregation module shapley值法 ref:于伟,张鹏._我国高校生均经费支出省际差异的再分析——基于shapley值分解的方法
    gdf["subtotal"]=gdf.case_19+gdf.case_15
    G_19=GiniSeg(gdf, 'case_19','subtotal')
    gdf_15=gdf[gdf.case_15<gdf.test_15]
    G_15=GiniSeg(gdf_15, 'case_15','subtotal')
    print("G19_gini:",G_19.statistic)
    print("G15_gini:",G_15.statistic)
    print("G_19-G_15:",G_19.statistic-G_15.statistic)
    
    # help(DecomposeSegregation)
    #14-Composition Approach (default)
    #Sergio J. Rey, Renan Cortes, and Elijah Knaap.Comparative Spatial Segregation Analytics[J].5.31.2019
    # help(DecomposeSegregation) #Decompose segregation differences into spatial and attribute components.
    #Shapley decomposition 
    DS_composition = DecomposeSegregation(G_15,G_19)
    print("Shapley's Spatial Component of the decomposition:",DS_composition.c_s) #Shapley's Spatial Component of the decomposition
    print("Shapley's Attribute Component of the decomposition:",DS_composition.c_a) #Shapley's Attribute Component of the decomposition
    plt.figure(figsize=(10,10))
    DS_composition.plot(plot_type = 'cdfs')
    # plt.figure(figsize=(50,50))
    DS_composition.plot(plot_type = 'maps')
    
    
    #15-Share Approach ref: Rey, S. et al "Comparative Spatial Segregation Analytics". 
    DS_share = DecomposeSegregation(G_19, G_15, counterfactual_approach = 'share')
    plt.figure(figsize=(10,10))
    DS_share.plot(plot_type = 'cdfs')
    plt.figure()
    DS_share.plot(plot_type = 'maps')

    #16-Dual Composition Approach
    DS_dual = DecomposeSegregation(G_19, G_15, counterfactual_approach = 'dual_composition')
    plt.figure(figsize=(10,10))
    DS_dual.plot(plot_type = 'cdfs')
    plt.figure()
    DS_dual.plot(plot_type = 'maps')
    
    #17-Inspecting a different index: Relative Concentration
    from segregation.spatial import RelativeConcentration
    RCO_19 = RelativeConcentration(gdf, 'case_19','subtotal')
    RCO_15= RelativeConcentration(gdf_15, 'case_15','subtotal')    
    print("RCO_19.statistic - RCO_15.statistic",RCO_19.statistic - RCO_15.statistic)
    RCO_DS_composition = DecomposeSegregation(RCO_19, RCO_15)
    print("RCO_DS_composition.c_s:",RCO_DS_composition.c_s)
    print("RCO_DS_composition.c_a:",RCO_DS_composition.c_a)
    
    
    #C-inference wrappers
    # single value
    #18-dissimilarity
    D= Dissim(gdf, 'case_19','subtotal')
    # print(type(index))
    print("dissimilarity:",D.statistic)
    #evenness
    infer_D_eve = SingleValueTest(D, iterations_under_null = 1000, null_approach = "evenness", two_tailed = True)
    plt.figure()
    infer_D_eve.plot()
    print("infer_D_eve.est_sim.mean:",infer_D_eve.est_sim.mean())
    print("infer_D_eve.p_value:",infer_D_eve.p_value)
    #systematic
    infer_D_sys = SingleValueTest(D, iterations_under_null = 5000, null_approach = "systematic", two_tailed = True)
    plt.figure()
    infer_D_sys.plot()
    
    # print("^"*50)
    #ValueError: 'Some estimates resulted in NaN or infinite values for estimations under null hypothesis.
    #19-relative concentration
    # RCO = RelativeConcentration(gdf, 'case_19','subtotal')
    # RCO=RCO_15
    # print(RCO)
    #permutation
    # infer_RCO_per = SingleValueTest(RCO, iterations_under_null = 1000, null_approach = "permutation", two_tailed = True)
    # plt.figure()
    # infer_RCO_per.plot()
    # print("infer_RCO_per.p_value:",infer_RCO_per.p_value)
    # #even_permutation
    # infer_RCO_eve_per = SingleValueTest(RCO, iterations_under_null = 1000, null_approach = "even_permutation", two_tailed = True)
    # plt.figure()
    # infer_RCO_eve_per.plot()
    
    #20-relative centralization
    #ValueError: It not possible to determine the center distance for, at least, one unit. This is probably due to the magnitude of the number of the centroids. We recommend to reproject the geopandas DataFrame.
    # print(gdf.crs)
    # RCE = RelativeCentralization(gdf.to_crs(communityArea_CRS), 'case_19','test_19')
    # infer_RCE_per = SingleValueTest(RCE, iterations_under_null = 1000, null_approach = "permutation", two_tailed = True)
    # plt.figure()
    # infer_RCE_per.plot()
    
    #D-comparative inference
    #21-compararive dissimilarity
    D_19=Dissim(gdf, 'case_19','subtotal')
    D_15=Dissim(gdf_15, 'case_15','subtotal')
    print("D_19-D_15:",D_19.statistic-D_15.statistic)
    
    compare_D_fit = TwoValueTest(D_19, D_15, iterations_under_null = 1000, null_approach = "random_label")
    plt.figure()
    compare_D_fit.plot()
    print("compare_D_fit.p_value:",compare_D_fit.p_value)
    
    #22-comparative Gini
    G_19=GiniSeg(gdf, 'case_19','subtotal')
    gdf_15=gdf[gdf.case_15<gdf.test_15]
    G_15=GiniSeg(gdf_15, 'case_15','subtotal')    
    compare_G_fit = TwoValueTest(G_19, G_15, iterations_under_null = 1000, null_approach = "random_label")
    plt.figure()
    compare_G_fit.plot()
    
    #23-comparative spatial dissimilarity
    SD_19 = SpatialDissim(gdf, 'case_19','subtotal')
    SD_15 = SpatialDissim(gdf_15, 'case_15','subtotal')
    compare_SD_fit = TwoValueTest(SD_19, SD_15, iterations_under_null = 500, null_approach = "counterfactual_composition")
    plt.figure()
    compare_SD_fit.plot()
Esempio n. 5
0
    def test_Inference(self):
        s_map = gpd.read_file(libpysal.examples.get_path("sacramentot2.shp"))
        index1 = Dissim(s_map, 'HISP_', 'TOT_POP')
        index2 = Dissim(s_map, 'BLACK_', 'TOT_POP')

        # Single Value Tests #
        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="systematic",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), 0.01603886544282861)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="bootstrap",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), 0.31992467511262773)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="evenness",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), 0.01596295861644252)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="permutation",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), 0.32184656076566864)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="systematic_permutation",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), 0.01603886544282861)

        np.random.seed(123)
        res = SingleValueTest(index1,
                              null_approach="even_permutation",
                              iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), 0.01619436868061094)

        # Two Value Tests #
        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="random_label",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.0013532591859387643)

        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="counterfactual_composition",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.005032145622504718)

        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="counterfactual_share",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(), -0.034350440515125)

        np.random.seed(123)
        res = TwoValueTest(index1,
                           index2,
                           null_approach="counterfactual_dual_composition",
                           iterations_under_null=50)
        np.testing.assert_almost_equal(res.est_sim.mean(),
                                       -0.004771386292706747)