Beispiel #1
0
 def test_block_weights(self):
     regimes = np.ones(25)
     regimes[range(10, 20)] = 2
     regimes[range(21, 25)] = 3
     regimes = np.array([
         1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 2., 2., 2., 2., 2., 2., 2.,
         2., 2., 2., 1., 3., 3., 3., 3.
     ])
     w = pysal.block_weights(regimes)
     ww0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
     self.assertEquals(w.weights[0], ww0)
     wn0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
     self.assertEquals(w.neighbors[0], wn0)
     regimes = ['n', 'n', 's', 's', 'e', 'e', 'w', 'w', 'e']
     n = len(regimes)
     w = pysal.block_weights(regimes)
     wn = {
         0: [1],
         1: [0],
         2: [3],
         3: [2],
         4: [5, 8],
         5: [4, 8],
         6: [7],
         7: [6],
         8: [4, 5]
     }
     self.assertEquals(w.neighbors, wn)
     ids = ['id-%i' % i for i in range(len(regimes))]
     w = pysal.block_weights(regimes, ids=np.array(ids))
     w0 = {'id-1': 1.0}
     self.assertEquals(w['id-0'], w0)
     w = pysal.block_weights(regimes, ids=ids)
     w0 = {'id-1': 1.0}
     self.assertEquals(w['id-0'], w0)
Beispiel #2
0
 def test_block_weights(self):
     regimes = np.ones(25)
     regimes[range(10, 20)] = 2
     regimes[range(21, 25)] = 3
     regimes = np.array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
                         2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 1., 3., 3.,
                         3., 3.])
     w = pysal.block_weights(regimes)
     ww0 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
     self.assertEquals(w.weights[0], ww0)
     wn0 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 20]
     self.assertEquals(w.neighbors[0], wn0)
     regimes = ['n', 'n', 's', 's', 'e', 'e', 'w', 'w', 'e']
     n = len(regimes)
     w = pysal.block_weights(regimes)
     wn = {0: [1], 1: [0], 2: [3], 3: [2], 4: [5, 8], 5: [4, 8],
           6: [7], 7: [6], 8: [4, 5]}
     self.assertEquals(w.neighbors, wn)
     ids = ['id-%i'%i for i in range(len(regimes))]
     w = pysal.block_weights(regimes, ids=np.array(ids))
     w0 = {'id-1': 1.0}
     self.assertEquals(w['id-0'], w0)
     w = pysal.block_weights(regimes, ids=ids)
     w0 = {'id-1': 1.0}
     self.assertEquals(w['id-0'], w0)
 def setUp(self):
     f = pysal.open(pysal.examples.get_path("mexico.csv"))
     vnames = ["pcgdp%d" % dec for dec in range(1940, 2010, 10)]
     y = np.transpose(np.array([f.by_col[v] for v in vnames]))
     self.y = y[:, 0]
     regimes = np.array(f.by_col('hanson98'))
     self.w = ps.block_weights(regimes)
Beispiel #4
0
 def setUp(self):
     f = pysal.open(pysal.examples.get_path("mexico.csv"))
     vnames = ["pcgdp%d" % dec for dec in range(1940, 2010, 10)]
     y = np.transpose(np.array([f.by_col[v] for v in vnames]))
     self.y = y[:, 0]
     regimes = np.array(f.by_col('hanson98'))
     self.w = ps.block_weights(regimes)
Beispiel #5
0
def rank(observations, w=None, regimes=None, method="tau"):
    originalObs = observations
    observations = observations.transpose()
    w = None
    a = None
    if regimes == None:  # generate regimes endogeneously.
        from GSA import Regionalization
        #TODO fix double transpose issue. fixed
        a = Regionalization.generateRegimes(w, originalObs)
        w = pysal.block_weights(a)
    else:  # regimes provided by user.
        w = pysal.block_weights(regimes)
    if method == "tau":
        result = [pysal.SpatialTau(observations[:, i], observations[:, i + 1], w, 100) for i in
                  range(0, len(w.id_order))]
        ret = [(r.tau_spatial, r.taus.mean(), r.tau_spatial_psim) for r in result]

    else:  # method == "theta"
        if regimes == None:
            result = pysal.Theta(regime=a, y=observations)
        else:
            result = pysal.Theta(regime=regimes, y=observations)
        ret = [(r.theta, r.pvalue_left, r.pvalue_right) for r in result]
    return ret
Beispiel #6
0
def bounded_world(r, c, nr, nc):
    '''
    Create W object for a bounded neighborhood topology based on grids (pixels
    and neighborhoods)

    NOTE: if r/nr or c/nc are not natural, the number of neighborhoods can be
    slightly inacurate
    ...

    Arguments
    ---------
    r       : int
              Number of pixels on the Y axis (rows)
    c       : int
              Number of pixels on the X axis (columns)
    nr      : int
              Number of neighborhoods on the Y axis (rows)
    nc      : int
              Number of neighborhoods on the X axis (columns)

    Returns
    -------
    W       : pysal.W
              Weights object
    ns      : ndarray
              Cardinalities for every observation to a neighborhood
    xys     : ndarray
              Nx2 array with coordinates of pixels
    '''
    x, y = np.indices((r, c))
    ir = int(np.round((r * 1.) / nr))
    ic = int(np.round((c * 1.) / nc))
    nrb = [i + ir for i in range(-ir, r, ir)]
    ncb = [i + ic for i in range(-ic, c, ic)]
    world = np.zeros((r, c), dtype=int)
    n = 0
    for i in range(len(nrb) - 1):
        for j in range(len(ncb) - 1):
            world[nrb[i]:nrb[i + 1], ncb[j]:ncb[j + 1]] = n
            n += 1
    world = world.flatten()
    w = ps.block_weights(world)
    return w, world, np.hstack((x.flatten()[:, None], y.flatten()[:, None]))
Beispiel #7
0
def bounded_world(r, c, nr, nc):
    '''
    Create W object for a bounded neighborhood topology based on grids (pixels
    and neighborhoods)

    NOTE: if r/nr or c/nc are not natural, the number of neighborhoods can be
    slightly inacurate
    ...

    Arguments
    ---------
    r       : int
              Number of pixels on the Y axis (rows)
    c       : int
              Number of pixels on the X axis (columns)
    nr      : int
              Number of neighborhoods on the Y axis (rows)
    nc      : int
              Number of neighborhoods on the X axis (columns)

    Returns
    -------
    W       : pysal.W
              Weights object
    ns      : ndarray
              Cardinalities for every observation to a neighborhood
    xys     : ndarray
              Nx2 array with coordinates of pixels
    '''
    x, y = np.indices((r, c))
    ir = int(np.round((r*1.)/nr))
    ic = int(np.round((c*1.)/nc))
    nrb = [i+ir for i in range(-ir, r, ir)]
    ncb = [i+ic for i in range(-ic, c, ic)]
    world = np.zeros((r, c), dtype=int)
    n = 0
    for i in range(len(nrb)-1):
        for j in range(len(ncb)-1):
            world[nrb[i]: nrb[i+1], ncb[j]:ncb[j+1]] = n
            n += 1
    world = world.flatten()
    w = ps.block_weights(world)
    return w, world, np.hstack((x.flatten()[:, None], y.flatten()[:, None]))