Ejemplo n.º 1
0
    def test_adaptive_bandwidth(self):
        w = d.Kernel(self.points, fixed=False)
        np.testing.assert_allclose(sorted(w[self.known_wi3].values()),
                                   sorted(self.known_w3),
                                   rtol=RTOL)
        bws = w.bandwidth.tolist()
        np.testing.assert_allclose(bws, self.known_w3_abws, rtol=RTOL)

        w = d.Kernel(self.points, fixed=False, function='gaussian')
        for k, v in w[self.known_wi4].items():
            np.testing.assert_allclose((k, v), (k, self.known_w4[k]),
                                       rtol=RTOL)
        bws = w.bandwidth.tolist()
        np.testing.assert_allclose(bws, self.known_w4_abws, rtol=RTOL)
Ejemplo n.º 2
0
    def test_fixed_bandwidth(self):
        w = d.Kernel(self.points, bandwidth=15.0)
        for k, v in w[self.known_wi1].items():
            np.testing.assert_allclose((k, v), (k, self.known_w1[k]))
        np.testing.assert_allclose(np.ones((w.n, 1)) * 15, w.bandwidth)

        w = d.Kernel(self.points, bandwidth=self.known_w2_bws)
        for k, v in w[self.known_wi2].items():
            np.testing.assert_allclose((k, v), (k, self.known_w2[k]),
                                       rtol=RTOL)
        for i in range(w.n):
            np.testing.assert_allclose(w.bandwidth[i],
                                       self.known_w2_bws[i],
                                       rtol=RTOL)
Ejemplo n.º 3
0
    def test_dense(self):
        w_rook = ps.weights.Rook.from_shapefile(
            ps.examples.get_path('lattice10x10.shp'))
        polys = ps.open(ps.examples.get_path('lattice10x10.shp'))
        centroids = [p.centroid for p in polys]
        w_db = d.DistanceBand(centroids, 1, build_sp=False)

        for k in w_db.id_order:
            np.testing.assert_equal(w_db[k], w_rook[k])
Ejemplo n.º 4
0
 def test_integers(self):
     """
     see issue #126
     """
     grid_integers = [
         tuple(map(int, poly.vertices[0])) for poly in self.grid_f
     ]
     self.grid_f.seek(0)
     grid_dbw = d.DistanceBand(grid_integers, 1)
     for k, v in grid_dbw:
         self.assertEquals(v, self.grid_rook_w[k])
Ejemplo n.º 5
0
 def test_arcdist(self):
     arc = ps.cg.sphere.arcdist
     kdt = KDTree(self.arc_points,
                  distance_metric='Arc',
                  radius=ps.cg.sphere.RADIUS_EARTH_KM)
     npoints = self.arc_points.shape[0]
     full = np.matrix([[
         arc(self.arc_points[i], self.arc_points[j])
         for j in xrange(npoints)
     ] for i in xrange(npoints)])
     maxdist = full.max()
     w = d.DistanceBand(kdt, maxdist, binary=False, alpha=1.0)
     np.testing.assert_allclose(w.sparse.todense(), full)
Ejemplo n.º 6
0
def jacquez(s_coords, t_coords, k, permutations=99):
    """
    Jacquez k nearest neighbors test for spatio-temporal interaction. [3]_

    Parameters
    ----------
    s_coords        : array
                      nx2 spatial coordinates

    t_coords        : array
                      nx1 temporal coordinates

    k               : int
                      the number of nearest neighbors to be searched

    permutations    : int
                      the number of permutations used to establish pseudo-
                      significance (default is 99)

    Returns
    -------
    jacquez_result  : dictionary
                      contains the statistic (stat) for the test and the
                      associated p-value (pvalue)
    stat            : float
                      value of the Jacquez k nearest neighbors test for the
                      dataset
    pvalue          : float
                      p-value associated with the statistic (normally
                      distributed with k-1 df)

    References
    ----------
    .. [3] G. Jacquez. 1996. A k nearest neighbour test for space-time
       interaction. Statistics in Medicine, 15(18):1935-1949.


    Examples
    --------
    >>> import numpy as np
    >>> import pysal

    Read in the example data and create an instance of SpaceTimeEvents.

    >>> path = pysal.examples.get_path("burkitt")
    >>> events = SpaceTimeEvents(path,'T')

    The Jacquez test counts the number of events that are k nearest
    neighbors in both time and space. The following runs the Jacquez test
    on the example data and reports the resulting statistic. In this case,
    there are 13 instances where events are nearest neighbors in both space
    and time.

    # turning off as kdtree changes from scipy < 0.12 return 13
    #>>> np.random.seed(100)
    #>>> result = jacquez(events.space, events.t ,k=3,permutations=99)
    #>>> print result['stat']
    #12

    The significance of this can be assessed by calling the p-
    value from the results dictionary, as shown below. Again, no
    space-time interaction is observed.

    #>>> result['pvalue'] < 0.01
    #False

    """
    time = t_coords
    space = s_coords
    n = len(time)

    # calculate the nearest neighbors in space and time separately
    knnt = Distance.knnW(time, k)
    knns = Distance.knnW(space, k)

    nnt = knnt.neighbors
    nns = knns.neighbors
    knn_sum = 0

    # determine which events are nearest neighbors in both space and time
    for i in range(n):
        t_neighbors = nnt[i]
        s_neighbors = nns[i]
        check = set(t_neighbors)
        inter = check.intersection(s_neighbors)
        count = len(inter)
        knn_sum += count

    stat = knn_sum

    # return the results (if no inference)
    if not permutations:
        return stat

    # loop for generating a random distribution to assess significance
    dist = []
    for p in range(permutations):
        j = 0
        trand = np.random.permutation(time)
        knnt = Distance.knnW(trand, k)
        nnt = knnt.neighbors
        for i in range(n):
            t_neighbors = nnt[i]
            s_neighbors = nns[i]
            check = set(t_neighbors)
            inter = check.intersection(s_neighbors)
            count = len(inter)
            j += count

        dist.append(j)

    # establish the pseudo significance of the observed statistic
    distribution = np.array(dist)
    greater = np.ma.masked_greater_equal(distribution, stat)
    count = np.ma.count_masked(greater)
    pvalue = (count + 1.0) / (permutations + 1.0)

    # report the results
    jacquez_result = {'stat': stat, 'pvalue': pvalue}
    return jacquez_result
Ejemplo n.º 7
0
def jacquez(s_coords, t_coords, k, permutations=99):
    """
    Jacquez k nearest neighbors test for spatio-temporal interaction. [3]_

    Parameters
    ----------
    s_coords        : array
                      nx2 spatial coordinates

    t_coords        : array
                      nx1 temporal coordinates

    k               : int
                      the number of nearest neighbors to be searched

    permutations    : int
                      the number of permutations used to establish pseudo-
                      significance (default is 99)

    Returns
    -------
    jacquez_result  : dictionary
                      contains the statistic (stat) for the test and the
                      associated p-value (pvalue)
    stat            : float
                      value of the Jacquez k nearest neighbors test for the
                      dataset
    pvalue          : float
                      p-value associated with the statistic (normally
                      distributed with k-1 df)

    References
    ----------
    .. [3] G. Jacquez. 1996. A k nearest neighbour test for space-time
       interaction. Statistics in Medicine, 15(18):1935-1949.


    Examples
    --------
    >>> import numpy as np
    >>> import pysal

    Read in the example data and create an instance of SpaceTimeEvents.

    >>> path = pysal.examples.get_path("burkitt")
    >>> events = SpaceTimeEvents(path,'T')

    The Jacquez test counts the number of events that are k nearest
    neighbors in both time and space. The following runs the Jacquez test
    on the example data and reports the resulting statistic. In this case,
    there are 13 instances where events are nearest neighbors in both space
    and time.

    # turning off as kdtree changes from scipy < 0.12 return 13
    #>>> np.random.seed(100)
    #>>> result = jacquez(events.space, events.t ,k=3,permutations=99)
    #>>> print result['stat']
    #12

    The significance of this can be assessed by calling the p-
    value from the results dictionary, as shown below. Again, no
    space-time interaction is observed.

    #>>> result['pvalue'] < 0.01
    #False

    """
    time = t_coords
    space = s_coords
    n = len(time)

    # calculate the nearest neighbors in space and time separately
    knnt = Distance.knnW(time, k)
    knns = Distance.knnW(space, k)

    nnt = knnt.neighbors
    nns = knns.neighbors
    knn_sum = 0

    # determine which events are nearest neighbors in both space and time
    for i in range(n):
        t_neighbors = nnt[i]
        s_neighbors = nns[i]
        check = set(t_neighbors)
        inter = check.intersection(s_neighbors)
        count = len(inter)
        knn_sum += count

    stat = knn_sum

    # return the results (if no inference)
    if not permutations:
        return stat

    # loop for generating a random distribution to assess significance
    dist = []
    for p in range(permutations):
        j = 0
        trand = np.random.permutation(time)
        knnt = Distance.knnW(trand, k)
        nnt = knnt.neighbors
        for i in range(n):
            t_neighbors = nnt[i]
            s_neighbors = nns[i]
            check = set(t_neighbors)
            inter = check.intersection(s_neighbors)
            count = len(inter)
            j += count

        dist.append(j)

    # establish the pseudo significance of the observed statistic
    distribution = np.array(dist)
    greater = np.ma.masked_greater_equal(distribution, stat)
    count = np.ma.count_masked(greater)
    pvalue = (count + 1.0) / (permutations + 1.0)

    # report the results
    jacquez_result = {'stat': stat, 'pvalue': pvalue}
    return jacquez_result
Ejemplo n.º 8
0
 def test_init(self):
     w = d.KNN(self.euclidean_kdt, k=2)
     self.assertEqual(w.neighbors[0], [1, 3])
Ejemplo n.º 9
0
 def test_init(self):
     w = d.Kernel(self.euclidean_kdt)
     for k, v in w[self.known_wi0].items():
         np.testing.assert_allclose(v, self.known_w0[k], rtol=RTOL)
Ejemplo n.º 10
0
 def test_init(self):
     w = d.DistanceBand(self.grid_kdt, 1)
     for k, v in w:
         self.assertEquals(v, self.grid_rook_w[k])
Ejemplo n.º 11
0
 def test_reweight(self):
     w = d.KNN(self.points, k=2)
     new_point = [(21, 21)]
     wnew = w.reweight(k=4, p=1, new_data=new_point, inplace=False)
     self.assertEqual(wnew[0], {1: 1.0, 3: 1.0, 4: 1.0, 6: 1.0})