Beispiel #1
0
    def test_knnW(self):
        kd = pysal.cg.kdtree.KDTree(np.array(self.points), distance_metric='euclidean')
        wnn2 = pysal.knnW(kd, 2)
        self.assertEqual(wnn2.neighbors[0], [1,3])

        pts = [i.centroid for i in pysal.open(self.polyShp)]
        kd = pysal.cg.kdtree.KDTree(pts)
        wnn4 = pysal.knnW(kd, 4)
        self.assertEqual(wnn4.neighbors[0], [2,1,3,7])
        self.assertEqual(wnn4.neighbors[7], [3,6,12,11])
Beispiel #2
0
    def test_knnW(self):
        kd = pysal.cg.kdtree.KDTree(np.array(self.points),
                                    distance_metric='euclidean')
        wnn2 = pysal.knnW(kd, 2)
        self.assertEqual(wnn2.neighbors[0], [1, 3])

        pts = [i.centroid for i in pysal.open(self.polyShp)]
        kd = pysal.cg.kdtree.KDTree(pts)
        wnn4 = pysal.knnW(kd, 4)
        self.assertEqual(wnn4.neighbors[0], [2, 1, 3, 7])
        self.assertEqual(wnn4.neighbors[7], [3, 6, 12, 11])
def missing_zones(dset,rents,k):

    #Zones dataset
    z=dset.zones
    z['zone_id']=z.index
    rents=pd.merge(z, rents, left_on='zone_id', right_index=True, how='outer')
    rents['ind']=np.arange(len(rents.index))

    # Construct nearest neighbors
    geo=pd.DataFrame(z['zonecentroid_x'])
    geo['zonecentroid_y']=z['zonecentroid_y']
    geo=np.array(geo)
    n=len(geo)

    w=pysal.knnW(geo, k=k)
    wneigh=np.zeros((n,k))
    for i in range(0,n):
        wneigh[i,:]=w.neighbors[i]


    ind=np.array(rents[np.isnan(rents['zonal_rent'])].ind)

    rents.loc[np.isnan(rents['zonal_rent']),'zonal_rent']=rents.ind.apply(filling, args=(rents, 'zonal_rent', wneigh))

    return pd.DataFrame(rents['zonal_rent'], index=rents.index)
Beispiel #4
0
def missing_zones(dset, rents, k):

    #Zones dataset
    z = dset.zones
    z['zone_id'] = z.index
    rents = pd.merge(z,
                     rents,
                     left_on='zone_id',
                     right_index=True,
                     how='outer')
    rents['ind'] = np.arange(len(rents.index))

    # Construct nearest neighbors
    geo = pd.DataFrame(z['zonecentroid_x'])
    geo['zonecentroid_y'] = z['zonecentroid_y']
    geo = np.array(geo)
    n = len(geo)

    w = pysal.knnW(geo, k=k)
    wneigh = np.zeros((n, k))
    for i in range(0, n):
        wneigh[i, :] = w.neighbors[i]

    ind = np.array(rents[np.isnan(rents['zonal_rent'])].ind)

    rents.loc[np.isnan(rents['zonal_rent']),
              'zonal_rent'] = rents.ind.apply(filling,
                                              args=(rents, 'zonal_rent',
                                                    wneigh))

    return pd.DataFrame(rents['zonal_rent'], index=rents.index)
Beispiel #5
0
 def setUp(self):
     sids = pysal.open(pysal.examples.get_path('sids2.shp'), 'r')
     self.sids = sids
     self.d = np.array([i.centroid for i in sids])
     self.w = knnW(self.d, k=5)
     if not self.w.id_order_set:
         self.w.id_order = self.w.id_order
     sids_db = pysal.open(pysal.examples.get_path('sids2.dbf'), 'r')
     self.b, self.e = np.array(sids_db[:, 8]), np.array(sids_db[:, 9])
Beispiel #6
0
    def test_knnW_arc(self):
        pts = [x.centroid for x in pysal.open(self.arcShp)]
        dist = pysal.cg.sphere.arcdist  # default radius is Earth KM
        full = np.matrix([[dist(pts[i], pts[j]) for j in xrange(
            len(pts))] for i in xrange(len(pts))])

        kd = pysal.cg.kdtree.KDTree(pts, distance_metric='Arc',
                                    radius=pysal.cg.sphere.RADIUS_EARTH_KM)
        w = pysal.knnW(kd, 4)
        self.assertTrue((full.argsort()[:, 1:5] == np.array(
            [w.neighbors[x] for x in range(len(pts))])).all())
Beispiel #7
0
 def test_knnW(self):
     x = np.indices((5, 5))
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW(data, k=2)
     wnn4 = pysal.knnW(data, k=4)
     wnn4.neighbors[0]
     self.assertEqual(wnn4.neighbors[0], [1, 5, 6, 2])
     self.assertEqual(wnn2.neighbors[5], [0, 6])
     self.assertEqual(wnn2.pct_nonzero, 0.080000000000000002)
     wnn3e = pysal.knnW(data, p=2, k=3)
     self.assertEqual(wnn3e.neighbors[0], [1, 5, 6])
     wc = pysal.knnW_from_shapefile(self.polyShp)
     self.assertEqual(wc.pct_nonzero, 0.040816326530612242)
     self.assertEqual(wc.neighbors[0], [2, 1])
     wc3 = pysal.knnW_from_shapefile(self.polyShp, k=3, idVariable="POLYID")
     self.assertEqual(wc3.weights[1], [1, 1, 1])
     self.assertEqual(wc3.neighbors[1], [3, 2, 4])
Beispiel #8
0
    def test_knnW_arc(self):
        pts = [x.centroid for x in pysal.open(self.arcShp)]
        dist = pysal.cg.sphere.arcdist  # default radius is Earth KM
        full = np.matrix([[dist(pts[i], pts[j]) for j in xrange(
            len(pts))] for i in xrange(len(pts))])

        kd = pysal.cg.kdtree.KDTree(pts, distance_metric='Arc',
                                    radius=pysal.cg.sphere.RADIUS_EARTH_KM)
        w = pysal.knnW(kd, 4)
        self.assertEqual(set(w.neighbors[4]), set([1,3,9,12]))
        self.assertEqual(set(w.neighbors[40]), set([31,38,45,49]))
Beispiel #9
0
 def test_knnW(self):
     x = np.indices((5, 5))
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW(data, k=2)
     wnn4 = pysal.knnW(data, k=4)
     wnn4.neighbors[0]
     self.assertEqual(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     self.assertEqual(set(wnn2.neighbors[5]), set([0, 6]))
     self.assertEqual(wnn2.pct_nonzero, 8.0)
     wnn3e = pysal.knnW(data, p=2, k=3)
     self.assertEqual(set(wnn3e.neighbors[0]), set([1, 5, 6]))
     wc = pysal.knnW_from_shapefile(self.polyShp)
     self.assertEqual(wc.pct_nonzero, 4.081632653061225)
     self.assertEqual(set(wc.neighbors[0]), set([2, 1]))
     wc3 = pysal.knnW_from_shapefile(self.polyShp, k=3)
     self.assertEqual(wc3.weights[1], [1, 1, 1])
     self.assertEqual(set(wc3.neighbors[1]), set([0, 3, 7]))
Beispiel #10
0
 def test_knnW_from_array(self):
     import numpy as np
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW_from_array(data, k=2)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     self.assertEquals(set(wnn4.neighbors[5]), set([0, 6, 10, 1]))
     self.assertEquals(set(wnn2.neighbors[0]), set([1, 5]))
     self.assertEquals(set(wnn2.neighbors[5]), set([0, 6]))
     self.assertAlmostEquals(wnn2.pct_nonzero, 0.080000000000000002)
     self.assertAlmostEquals(wnn4.pct_nonzero, 0.16)
     wnn4 = pysal.knnW_from_array(data, k=4)
     self.assertEquals(set(wnn4.neighbors[0]), set([1, 5, 6, 2]))
     wnn3e = pysal.knnW(data, p=2, k=3)
     self.assertEquals(set(wnn3e.neighbors[0]),set([1, 5, 6]))
     wnn3m = pysal.knnW(data, p=1, k=3)
     self.assertEquals(set(wnn3m.neighbors[0]), set([1, 5, 2]))
Beispiel #11
0
 def test_knnW(self):
     x = np.indices((5, 5))
     x, y = np.indices((5, 5))
     x.shape = (25, 1)
     y.shape = (25, 1)
     data = np.hstack([x, y])
     wnn2 = pysal.knnW(data, k=2)
     wnn4 = pysal.knnW(data, k=4)
     wnn4.neighbors[0]
     self.assertEqual(wnn4.neighbors[0], [1, 5, 6, 2])
     self.assertEqual(wnn2.neighbors[5], [0, 6])
     self.assertEqual(wnn2.pct_nonzero, 0.080000000000000002)
     wnn3e = pysal.knnW(data, p=2, k=3)
     self.assertEqual(wnn3e.neighbors[0], [1, 5, 6])
     wc = pysal.knnW_from_shapefile(self.polyShp)
     self.assertEqual(wc.pct_nonzero, 0.040816326530612242)
     self.assertEqual(wc.neighbors[0], [2, 1])
     wc3 = pysal.knnW_from_shapefile(self.polyShp, k=3, idVariable="POLYID")
     self.assertEqual(wc3.weights[1], [1, 1, 1])
     self.assertEqual(wc3.neighbors[1], [3, 2, 4])
Beispiel #12
0
    def test_knnW_arc(self):
        pts = [x.centroid for x in pysal.open(self.arcShp)]
        dist = pysal.cg.sphere.arcdist  # default radius is Earth KM
        full = np.matrix([[dist(pts[i], pts[j]) for j in xrange(
            len(pts))] for i in xrange(len(pts))])

        kd = pysal.cg.kdtree.KDTree(pts, distance_metric='Arc',
                                    radius=pysal.cg.sphere.RADIUS_EARTH_KM)
        w = pysal.knnW(kd, 4)
        self.assertEqual(set(w.neighbors[4]), set([1,3,9,12]))
        self.assertEqual(set(w.neighbors[40]), set([31,38,45,49]))
Beispiel #13
0
    def test_knnW_arc(self):
        pts = [x.centroid for x in pysal.open(self.arcShp)]
        dist = pysal.cg.sphere.arcdist  # default radius is Earth KM
        full = np.matrix([[dist(pts[i], pts[j]) for j in xrange(len(pts))]
                          for i in xrange(len(pts))])

        kd = pysal.cg.kdtree.KDTree(pts,
                                    distance_metric='Arc',
                                    radius=pysal.cg.sphere.RADIUS_EARTH_KM)
        w = pysal.knnW(kd, 4)
        self.assertTrue((full.argsort()[:, 1:5] == np.array(
            [w.neighbors[x] for x in range(len(pts))])).all())
Beispiel #14
0
def data_zone_census( zones):

    data_census=pd.read_csv(os.path.join(misc.data_dir(),'census_zone.csv'))
    #del data_census['median_value']
    data=pd.merge(zones, data_census, on='zone_id', how='inner')


    #Income using census block group dat
    data['median_income']=data['median_income'].astype(float)
    data['ln_inc']=np.log(data['median_income'])

    # Asked price (census)
    #data['median_value']=data['median_value'].apply(float)
    data['ln_price']=np.log(data['median_value'])

    # Race composition
    data['all races']=data['White alone'].apply(float)+ data['Black or African American alone'].apply(float)\
                     + data['American Indian and Alaska Native alone'].apply(float)+ data['Asian alone'].apply(float)\
                +data['Native Hawaiian and Other Pacific Islander alone'].apply(float)+ data['Some other race alone'].apply(float)\
                +data['two races or more'].apply(float)
    data['percent_white']=np.log(data['White alone']/data['all races'])
    data['percent_black']=data['Black or African American alone']/data['all races']
    data['percent_black2']=data['Black or African American alone']/data['all races']**2
    data['ln_residential_sqft_mean2']=data['ln_residential_sqft_mean']**2


    # Creating max  and min income of neighbors ( can important have implications in terms of gentrification)
    geo=pd.DataFrame(data['zonecentroid_x'])
    geo['zonecentroid_y']=data['zonecentroid_y']
    geo=np.array(geo)
    w=pysal.knnW(geo, k=10)

    n=len(geo)
    neigh_income_max=np.zeros(n)
    neigh_income_min=np.zeros(n)

    for i in range(0, n-1):
        arr=w.neighbors[i]
        zone=np.zeros(n)
        for j in arr:
         zone[j]=1

        data['neigh']=zone
        neigh_income_max[i]=data[data['neigh']==1].median_income.max()
        neigh_income_min[i]=data[data['neigh']==1].median_income.min()

    data['ln_neigh_income_max']=np.log(neigh_income_max/data['median_income'])
    data['ln_neigh_income_min']=np.log(neigh_income_min/data['median_income'])

    data=data.set_index(data['zone_id'])
    return data
Beispiel #15
0
def gpd_knnW(gpdf, k=2, p=2, ids=None):
    """
    Constructs distance-based spatial weights from a geopandas object
    :param gpd: geopandas dataframe
    :param k: int number of nearest neighbors
    :param p: float
    :return weight

    https://github.com/pysal/pysal/blob/master/pysal/weights/Distance.py
    """
    points = gpd_points(gpdf)
    kd = pysal.cg.kdtree.KDTree(points)
    w = pysal.knnW(kd, k, p, ids)
    return w
Beispiel #16
0
def main():
    work_dir = r"D:\hcho_change\ROI\COI"
    os.chdir(work_dir)
    os.chdir("aod")

    files = os.listdir(os.getcwd())
    os.chdir("..")
    # name = files[-1]
    for name in files:
        os.chdir("aod")
        print name
        f = gdal.Open(name)
        cols = f.RasterXSize
        rows = f.RasterYSize
        gt = f.GetGeoTransform()
        proj = f.GetProjection()

        array = f.ReadAsArray(0, 0, cols, rows)
        #预处理
        array[np.where(~np.isfinite(array) == True)] = -1
        array[np.where(array < 0)] = np.float("nan")
        #------
        y, x = np.where(np.isfinite(array))

        lats = np.array([gt[3] + gt[5] * i for i in y])
        lons = np.array([gt[0] + gt[1] * i for i in x])
        data = array[y, x]

        coor = np.vstack((lons, lats)).T
        w = pysal.knnW(coor, k=3)
        mr = pysal.Moran_Local(data, w).p_sim
        array[np.where(np.isfinite(array))] = mr
        array[np.where(~np.isfinite(array))] = 0.

        dr = gdal.GetDriverByName("GTiff")
        os.chdir("..")
        os.chdir("out")
        name = name[0:-4] + "moran.tif"
        ds = dr.Create(name, cols, rows, 1, gdal.GDT_Float32)
        ds.SetGeoTransform(gt)
        ds.SetProjection(proj)
        ds.GetRasterBand(1).WriteArray(array)
        os.chdir("..")
        print name + " has done."
Beispiel #17
0
 def test_Headbanging_Median_Rate(self):
     sids_d = np.array([i.centroid for i in self.sids])
     sids_w = pysal.knnW(sids_d, k=5)
     if not sids_w.id_order_set:
         sids_w.id_order = sids_w.id_order
     s_ht = sm.Headbanging_Triples(sids_d, sids_w, k=5)
     sids_db = pysal.open(pysal.examples.get_path('sids2.dbf'), 'r')
     s_e, s_b = np.array(sids_db[:, 9]), np.array(sids_db[:, 8])
     sids_hb_r = sm.Headbanging_Median_Rate(s_e, s_b, s_ht)
     sids_hb_rr5 = np.array([0.00075586, 0.,
                             0.0008285, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_rr5, sids_hb_r.r[:5])
     sids_hb_r2 = sm.Headbanging_Median_Rate(s_e, s_b, s_ht, iteration=5)
     sids_hb_r2r5 = np.array([0.0008285, 0.00084331,
                              0.00086896, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_r2r5, sids_hb_r2.r[:5])
     sids_hb_r3 = sm.Headbanging_Median_Rate(s_e, s_b, s_ht, aw=s_b)
     sids_hb_r3r5 = np.array([0.00091659, 0.,
                              0.00156838, 0.0018315, 0.00498891])
     np.testing.assert_array_almost_equal(sids_hb_r3r5, sids_hb_r3.r[:5])