示例#1
0
    def test_Kernel(self):
        kw = pysal.Kernel(self.points)
        self.assertEqual(kw.weights[0],
                         [1.0, 0.50000004999999503, 0.44098306152674649])
        kw15 = pysal.Kernel(self.points, bandwidth=15.0)
        self.assertEqual(kw15[0], {
            0: 1.0,
            1: 0.33333333333333337,
            3: 0.2546440075000701
        })
        self.assertEqual(kw15.bandwidth[0], 15.)
        self.assertEqual(kw15.bandwidth[-1], 15.)
        bw = [25.0, 15.0, 25.0, 16.0, 14.5, 25.0]
        kwa = pysal.Kernel(self.points, bandwidth=bw)
        self.assertEqual(kwa.weights[0], [
            1.0, 0.59999999999999998, 0.55278640450004202, 0.10557280900008403
        ])
        self.assertEqual(kwa.neighbors[0], [0, 1, 3, 4])
        self.assertEqual(kwa.bandwidth[0], 25.)
        self.assertEqual(kwa.bandwidth[1], 15.)
        self.assertEqual(kwa.bandwidth[2], 25.)
        self.assertEqual(kwa.bandwidth[3], 16.)
        self.assertEqual(kwa.bandwidth[4], 14.5)
        self.assertEqual(kwa.bandwidth[5], 25.)
        kwea = pysal.Kernel(self.points, fixed=False)
        self.assertEqual(kwea.weights[0],
                         [1.0, 0.10557289844279438, 9.9999990066379496e-08])
        l = kwea.bandwidth.tolist()
        self.assertEqual(
            l,
            [[11.180341005532938], [11.180341005532938], [20.000002000000002],
             [11.180341005532938], [14.142137037944515], [18.027758180095585]])
        kweag = pysal.Kernel(self.points, fixed=False, function='gaussian')
        self.assertEqual(
            kweag.weights[0],
            [0.3989422804014327, 0.26741902915776961, 0.24197074871621341])
        l = kweag.bandwidth.tolist()
        self.assertEqual(
            l,
            [[11.180341005532938], [11.180341005532938], [20.000002000000002],
             [11.180341005532938], [14.142137037944515], [18.027758180095585]])

        kw = pysal.kernelW_from_shapefile(self.polyShp, idVariable='POLYID')
        self.assertEqual(
            set(kw.weights[1]),
            set([
                0.0070787731484506233, 0.2052478782400463, 0.23051223027663237,
                1.0
            ]))
        kwa = pysal.adaptive_kernelW_from_shapefile(self.polyShp)
        self.assertEqual(kwa.weights[0],
                         [1.0, 0.03178906767736345, 9.9999990066379496e-08])
示例#2
0
def main():
    """ Reports the spatial autocorrelation for each target variable. """
    ap = argparse.ArgumentParser()
    ap.add_argument(
        '--coords',
        default='/data/twcounty/stats2/counties/counties.top100.bounding.txt',
        help='tsv file with counties, lat/long, and bounding boxes')
    ap.add_argument(
        '--dbf',
        default=
        '/data/twcounty/stats2/counties/census_polygons/gz_2010_us_050_00_500k.dbf',
        help='county dbf files')
    ap.add_argument(
        '--input',
        default='/data/twcounty/features/counties.norm=none.liwc.pkl',
        help='pickled county feature Data object')
    ap.add_argument('--json',
                    default='/data/twcounty/json',
                    help='directory of jsons per county')
    ap.add_argument(
        '--shapes',
        default=
        '/data/twcounty/stats2/counties/census_polygons/gz_2010_us_050_00_500k.shp',
        help='county shape files')
    ap.add_argument('--targets',
                    default='/data/twcounty/targets.tsv',
                    help='targets per county')
    ap.add_argument('--tweets',
                    default='/data/twcounty/tweets.tsv',
                    help='tweets')
    args = ap.parse_args()

    coords = read_coords(args.coords)
    overlaps = get_overlaps(coords)
    print_tweets_in_overlaps(overlaps, args.json, args.tweets)
    shapes = pysal.open(args.shapes)
    print 'read', len(shapes), 'counties from shape file'
    dbf = pysal.open(args.dbf)
    counties = data.read(args.input).features
    targets, target_alpha = read_targets(args.targets, counties)
    for t in targets:
        didx, d = [(i, dd) for i, dd in enumerate(dbf)
                   if dd[1] + dd[2] == t][0]
        targets[t]['dbf'] = d
        targets[t]['dbfi'] = didx
        shape = shapes[didx]
        targets[t]['shape'] = shape

    fips = sorted(targets)
    print '\t'.join(['mi.I  ', 'mi.EI  ', 'p_norm ', 'outcome'])
    weights = pysal.Kernel(
        np.array([targets[f]['shape'].centroid for f in fips]))
    for outcome in target_alpha:
        y = np.array([float(targets[t][outcome]) for t in fips])
        y = y / np.sum(y)
        mi = pysal.Moran(y, weights)
        print '%.5f\t%.5f\t%.5f\t%s' % (mi.I, mi.EI, mi.p_norm, outcome)
示例#3
0
def make_distance_weights():
    """ Compute a distance weight matrix based on the geographical coordinates
    of each county. """
    coords = twexpt.read_coords(
        '/data/twcounty/stats2/counties/counties.top100.bounding.txt')
    coords = [coords[cty] for cty in sorted(coords)]
    weights = pysal.Kernel(np.array(coords))
    for i in range(len(weights.weights)):
        idx = weights.neighbors[i].index(i)
        del weights.weights[i][idx]
        del weights.neighbors[i][idx]
    weights.transform = 'r'
    return weights
示例#4
0
def gpd_kernel(gpdf,
               bandwidth=None,
               fixed=True,
               k=2,
               diagonal=False,
               function='triangular',
               eps=1.0000001,
               ids=None):
    """
    Kernel weights
    https://github.com/pysal/pysal/blob/master/pysal/weights/Distance.py
    """
    points = gpd_points(gpdf)
    kw = pysal.Kernel(points,
                      bandwidth=bandwidth,
                      fixed=fixed,
                      k=k,
                      diagonal=diagonal,
                      function=function,
                      eps=eps,
                      ids=ids)
    return kw