Exemplo n.º 1
0
 def test_lat2W(self):
     w9 = pysal.lat2W(3, 3)
     self.assertEquals(w9.pct_nonzero, 29.62962962962963)
     self.assertEquals(w9[0], {1: 1.0, 3: 1.0})
     self.assertEquals(w9[3], {0: 1.0, 4: 1.0, 6: 1.0})
     self.assertEquals(pysal.lat2W(1, 1), None)
     self.assertEquals(pysal.lat2W(1, -2), None)
Exemplo n.º 2
0
 def setUp(self):
     db=pysal.open(pysal.examples.get_path("columbus.dbf"),"r")
     y = np.array(db.by_col("CRIME"))
     self.y = np.reshape(y, (49,1))
     X = []
     X.append(db.by_col("HOVAL"))
     X.append(db.by_col("INC"))
     self.X = np.array(X).T
     X2 = []
     X2.append(db.by_col("INC"))
     self.X2 = np.array(X2).T
     yd = []
     yd.append(db.by_col("HOVAL"))
     self.yd = np.array(yd).T
     q = []
     q.append(db.by_col("DISCBD"))
     self.q = np.array(q).T
     self.w = pysal.queen_from_shapefile(pysal.examples.get_path("columbus.shp"))
     self.w.transform = 'r'
     self.r_var = 'NSA'
     self.regimes = db.by_col(self.r_var)
     #Artficial:
     n = 256
     self.n2 = n/2
     self.x_a1 = np.random.uniform(-10,10,(n,1))
     self.x_a2 = np.random.uniform(1,5,(n,1))
     self.q_a = self.x_a2 + np.random.normal(0,1,(n,1))
     self.x_a = np.hstack((self.x_a1,self.x_a2))
     self.y_a = np.dot(np.hstack((np.ones((n,1)),self.x_a)),np.array([[1],[0.5],[2]])) + np.random.normal(0,1,(n,1))
     latt = int(np.sqrt(n))
     self.w_a = pysal.lat2W(latt,latt)
     self.w_a.transform='r'
     self.regi_a = [0]*(n/2) + [1]*(n/2)
     self.w_a1 = pysal.lat2W(latt/2,latt)
     self.w_a1.transform='r'
 def setUp(self):
     db=pysal.open(pysal.examples.get_path("columbus.dbf"),"r")
     y = np.array(db.by_col("CRIME"))
     self.y = np.reshape(y, (49,1))
     X = []
     X.append(db.by_col("HOVAL"))
     X.append(db.by_col("INC"))
     self.X = np.array(X).T
     X2 = []
     X2.append(db.by_col("INC"))
     self.X2 = np.array(X2).T
     yd = []
     yd.append(db.by_col("HOVAL"))
     self.yd = np.array(yd).T
     q = []
     q.append(db.by_col("DISCBD"))
     self.q = np.array(q).T
     self.w = pysal.queen_from_shapefile(pysal.examples.get_path("columbus.shp"))
     self.w.transform = 'r'
     self.r_var = 'NSA'
     self.regimes = db.by_col(self.r_var)
     #Artficial:
     n = 256
     self.n2 = n/2
     self.x_a1 = np.random.uniform(-10,10,(n,1))
     self.x_a2 = np.random.uniform(1,5,(n,1))
     self.q_a = self.x_a2 + np.random.normal(0,1,(n,1))
     self.x_a = np.hstack((self.x_a1,self.x_a2))
     self.y_a = np.dot(np.hstack((np.ones((n,1)),self.x_a)),np.array([[1],[0.5],[2]])) + np.random.normal(0,1,(n,1))
     latt = int(np.sqrt(n))
     self.w_a = pysal.lat2W(latt,latt)
     self.w_a.transform='r'
     self.regi_a = [0]*(n/2) + [1]*(n/2)
     self.w_a1 = pysal.lat2W(latt/2,latt)
     self.w_a1.transform='r'
Exemplo n.º 4
0
 def setUp(self):
     db = pysal.open(pysal.examples.get_path("baltim.dbf"), 'r')
     self.ds_name = "baltim.dbf"
     self.y_name = "PRICE"
     self.y = np.array(db.by_col(self.y_name)).T
     self.y.shape = (len(self.y), 1)
     self.x_names = ["NROOM", "AGE", "SQFT"]
     self.x = np.array([db.by_col(var) for var in self.x_names]).T
     ww = pysal.open(pysal.examples.get_path("baltim_q.gal"))
     self.w = ww.read()
     ww.close()
     self.w_name = "baltim_q.gal"
     self.w.transform = 'r'
     self.regimes = db.by_col("CITCOU")
     #Artficial:
     n = 256
     self.n2 = n / 2
     self.x_a1 = np.random.uniform(-10, 10, (n, 1))
     self.x_a2 = np.random.uniform(1, 5, (n, 1))
     self.q_a = self.x_a2 + np.random.normal(0, 1, (n, 1))
     self.x_a = np.hstack((self.x_a1, self.x_a2))
     self.y_a = np.dot(np.hstack((np.ones(
         (n, 1)), self.x_a)), np.array(
             [[1], [0.5], [2]])) + np.random.normal(0, 1, (n, 1))
     latt = int(np.sqrt(n))
     self.w_a = pysal.lat2W(latt, latt)
     self.w_a.transform = 'r'
     self.regi_a = [0] * (n / 2) + [1] * (n / 2)
     self.w_a1 = pysal.lat2W(latt / 2, latt)
     self.w_a1.transform = 'r'
Exemplo n.º 5
0
 def setUp(self):
     db =  pysal.open(pysal.examples.get_path("baltim.dbf"),'r')
     self.ds_name = "baltim.dbf"
     self.y_name = "PRICE"
     self.y = np.array(db.by_col(self.y_name)).T
     self.y.shape = (len(self.y),1)
     self.x_names = ["NROOM","AGE","SQFT"]
     self.x = np.array([db.by_col(var) for var in self.x_names]).T
     ww = pysal.open(pysal.examples.get_path("baltim_q.gal"))
     self.w = ww.read()
     ww.close()
     self.w_name = "baltim_q.gal"
     self.w.transform = 'r'
     self.regimes = db.by_col("CITCOU")
     #Artficial:
     n = 256
     self.n2 = n/2
     self.x_a1 = np.random.uniform(-10,10,(n,1))
     self.x_a2 = np.random.uniform(1,5,(n,1))
     self.q_a = self.x_a2 + np.random.normal(0,1,(n,1))
     self.x_a = np.hstack((self.x_a1,self.x_a2))
     self.y_a = np.dot(np.hstack((np.ones((n,1)),self.x_a)),np.array([[1],[0.5],[2]])) + np.random.normal(0,1,(n,1))
     latt = int(np.sqrt(n))
     self.w_a = pysal.lat2W(latt,latt)
     self.w_a.transform='r'
     self.regi_a = [0]*(n/2) + [1]*(n/2)
     self.w_a1 = pysal.lat2W(latt/2,latt)
     self.w_a1.transform='r'
Exemplo n.º 6
0
 def test_w_difference(self):
     """Unit test"""
     w1 = pysal.lat2W(4, 4, rook=False)
     w2 = pysal.lat2W(4, 4, rook=True)
     w3 = pysal.weights.Wsets.w_difference(w1, w2, constrained=False)
     self.assertNotEqual(w1[0], w3[0])
     self.assertEqual(set(w1.neighbors[15]), set([10, 11, 14]))
     self.assertEqual(set(w2.neighbors[15]), set([11, 14]))
     self.assertEqual(set(w3.neighbors[15]), set([10]))
Exemplo n.º 7
0
 def test_w_difference(self):
     """Unit test"""
     w1 = pysal.lat2W(4, 4, rook=False)
     w2 = pysal.lat2W(4, 4, rook=True)
     w3 = pysal.weights.Wsets.w_difference(w1, w2, constrained=False)
     self.assertNotEqual(w1[0], w3[0])
     self.assertEqual(set(w1.neighbors[15]), set([10, 11, 14]))
     self.assertEqual(set(w2.neighbors[15]), set([11, 14]))
     self.assertEqual(set(w3.neighbors[15]), set([10]))
Exemplo n.º 8
0
 def test_w_union(self):
     """Unit test"""
     w1 = pysal.lat2W(4, 4)
     w2 = pysal.lat2W(6, 4)
     w3 = pysal.weights.Wsets.w_union(w1, w2)
     self.assertEqual(w1[0], w3[0])
     self.assertEqual(set(w1.neighbors[15]), set([11, 14]))
     self.assertEqual(set(w2.neighbors[15]), set([11, 14, 19]))
     self.assertEqual(set(w3.neighbors[15]), set([19, 11, 14]))
Exemplo n.º 9
0
 def test_w_union(self):
     """Unit test"""
     w1 = pysal.lat2W(4, 4)
     w2 = pysal.lat2W(6, 4)
     w3 = pysal.weights.Wsets.w_union(w1, w2)
     self.assertEqual(w1[0], w3[0])
     self.assertEqual(set(w1.neighbors[15]), set([11, 14]))
     self.assertEqual(set(w2.neighbors[15]), set([11, 14, 19]))
     self.assertEqual(set(w3.neighbors[15]), set([19, 11, 14]))
Exemplo n.º 10
0
 def test_higher_order(self):
     w10 = pysal.lat2W(10, 10)
     w10_2 = pysal.higher_order(w10, 2)
     w10_20 = {2: 1.0, 11: 1.0, 20: 1.0}
     self.assertEquals(w10_20, w10_2[0])
     w5 = pysal.lat2W()
     w50 = {1: 1.0, 5: 1.0}
     self.assertEquals(w50, w5[0])
     w51 = {0: 1.0, 2: 1.0, 6: 1.0}
     self.assertEquals(w51, w5[1])
     w5_2 = pysal.higher_order(w5, 2)
     w5_20 = {2: 1.0, 10: 1.0, 6: 1.0}
     self.assertEquals(w5_20, w5_2[0])
Exemplo n.º 11
0
 def test_higher_order(self):
     w10 = pysal.lat2W(10, 10)
     w10_2 = pysal.higher_order(w10, 2)
     w10_20 = {2: 1.0, 11: 1.0, 20: 1.0}
     self.assertEquals(w10_20, w10_2[0])
     w5 = pysal.lat2W()
     w50 = {1: 1.0, 5: 1.0}
     self.assertEquals(w50, w5[0])
     w51 = {0: 1.0, 2: 1.0, 6: 1.0}
     self.assertEquals(w51, w5[1])
     w5_2 = pysal.higher_order(w5, 2)
     w5_20 = {2: 1.0, 10: 1.0, 6: 1.0}
     self.assertEquals(w5_20, w5_2[0])
Exemplo n.º 12
0
    def __init__( self, x, w=None ):
        """
        x : numpy array, either rectangular spatial array that will be
        raveled for pysal, or array that is already 1D. If x is 1D,
        then we must have that len(x)=k**2 in order to create the
        square weight matrix.

        w : if x is 1D, must provide w, a weights matrix indicating
        the spatial correlation between neighboring data points. Eg.,
        w_ij = 1 if grid element i is a neighbor of j, 0 otherwise.
        """
        
        if x.ndim > 1:
            # for ricker computations assume x is square
            n = x.shape[0]
            self.x = x.ravel()
               
            # create contiguous weight matrix ('rook' format, eg. NWSE
            # neighbors)
            self.w = pysal.lat2W( n, n )

        elif x.ndim == 1 and w is None:
            print "Must provide wieghts 'w'."
            return
        else:
            self.x = x
            n = int( np.sqrt( len( x ) ) )
            self.w = w

        # create the Moran object
        self.moran = pysal.Moran( self.x, self.w )
Exemplo n.º 13
0
    def setUp(self):
        from pysal import rook_from_shapefile
        self.w = rook_from_shapefile(pysal.examples.get_path('10740.shp'))

        self.neighbors = {
            0: [3, 1],
            1: [0, 4, 2],
            2: [1, 5],
            3: [0, 6, 4],
            4: [1, 3, 7, 5],
            5: [2, 4, 8],
            6: [3, 7],
            7: [4, 6, 8],
            8: [5, 7]
        }
        self.weights = {
            0: [1, 1],
            1: [1, 1, 1],
            2: [1, 1],
            3: [1, 1, 1],
            4: [1, 1, 1, 1],
            5: [1, 1, 1],
            6: [1, 1],
            7: [1, 1, 1],
            8: [1, 1]
        }

        self.w3x3 = pysal.lat2W(3, 3)
Exemplo n.º 14
0
 def test___iter__(self):
     w = pysal.lat2W(3, 3)
     res = {}
     for i, wi in enumerate(w):
         res[i] = wi
     self.assertEqual(res[0], (0, {1: 1.0, 3: 1.0}))
     self.assertEqual(res[8], (8, {5: 1.0, 7: 1.0}))
Exemplo n.º 15
0
    def setUp(self):
        from pysal import rook_from_shapefile
        self.w = rook_from_shapefile(pysal.examples.get_path('10740.shp'))
        wsp = self.w.to_WSP()
        self.w = wsp.to_W()

        self.neighbors = {
            0: [3, 1],
            1: [0, 4, 2],
            2: [1, 5],
            3: [0, 6, 4],
            4: [1, 3, 7, 5],
            5: [2, 4, 8],
            6: [3, 7],
            7: [4, 6, 8],
            8: [5, 7]
        }
        self.weights = {
            0: [1, 1],
            1: [1, 1, 1],
            2: [1, 1],
            3: [1, 1, 1],
            4: [1, 1, 1, 1],
            5: [1, 1, 1],
            6: [1, 1],
            7: [1, 1, 1],
            8: [1, 1]
        }

        self.w3x3 = pysal.lat2W(3, 3)
        w3x3 = pysal.weights.WSP(self.w3x3.sparse, self.w3x3.id_order)
        self.w3x3 = pysal.weights.WSP2W(w3x3)
Exemplo n.º 16
0
 def test_Maxp_LISA(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones(w.n)
     mpl = pysal.region.Maxp_LISA(w, z, p, floor=3, floor_variable=p)
     self.assertEquals(mpl.p, 31)
     self.assertEquals(mpl.regions[0], [99, 89, 98, 97])
Exemplo n.º 17
0
 def test_Maxp_LISA(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones(w.n)
     mpl = pysal.region.Maxp_LISA(w, z, p, floor=3, floor_variable=p)
     self.assertEquals(mpl.p, 31)
     self.assertEquals(mpl.regions[0], [99, 89, 98, 97])
Exemplo n.º 18
0
 def test_shimbel(self):
     w5 = pysal.lat2W()
     w5_shimbel = pysal.shimbel(w5)
     w5_shimbel024 = 8
     self.assertEquals(w5_shimbel024, w5_shimbel[0][24])
     w5_shimbel004 = [-1, 1, 2, 3]
     self.assertEquals(w5_shimbel004, w5_shimbel[0][0:4])
Exemplo n.º 19
0
 def test_shimbel(self):
     w5 = pysal.lat2W()
     w5_shimbel = pysal.shimbel(w5)
     w5_shimbel024 = 8
     self.assertEquals(w5_shimbel024, w5_shimbel[0][24])
     w5_shimbel004 = [-1, 1, 2, 3]
     self.assertEquals(w5_shimbel004, w5_shimbel[0][0:4])
Exemplo n.º 20
0
 def test___iter__(self):
     w = pysal.lat2W(3, 3)
     res = {}
     for i, wi in enumerate(w):
         res[i] = wi
     self.assertEqual(res[0], {1: 1.0, 3: 1.0})
     self.assertEqual(res[8], {5: 1.0, 7: 1.0})
 def test_Maxp_LISA(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones(w.n)
     np.random.seed(111)
     mpl = pysal.region.Maxp_LISA(w, z, p, floor=3, floor_variable=p)
     self.assertEqual(mpl.p, 30)
     self.assertEqual(mpl.regions[0], [99, 89, 79, 88, 78])
Exemplo n.º 22
0
 def test_w_subset(self):
     """Unit test"""
     w1 = pysal.lat2W(6, 4)
     ids = range(16)
     w2 = pysal.weights.Wsets.w_subset(w1, ids)
     self.assertEqual(w1[0], w2[0])
     self.assertEqual(set(w1.neighbors[15]), set([11, 14, 19]))
     self.assertEqual(set(w2.neighbors[15]), set([11, 14]))
Exemplo n.º 23
0
 def test_w_subset(self):
     """Unit test"""
     w1 = pysal.lat2W(6, 4)
     ids = range(16)
     w2 = pysal.weights.Wsets.w_subset(w1, ids)
     self.assertEqual(w1[0], w2[0])
     self.assertEqual(set(w1.neighbors[15]), set([11, 14, 19]))
     self.assertEqual(set(w2.neighbors[15]), set([11, 14]))
Exemplo n.º 24
0
    def get_weight_matrix(self, array, rook=False, shpfile=None):
        """Return the spatial weight matrix based on pysal functionalities

        Keyword arguments:
            array    Numpy array with inventory values.
            rook    Boolean to select spatial weights matrix as rook or
                    queen case.
            shpfile    Name of file used to setup weight matrix.
        """
        # Get case name.
        if rook:
            case = 'rook'
        else:
            case = 'queen'
        # Get grid dimension.
        dim = array.shape
        if self.sptype == 'vector':
            try:
                # Create weights based on shapefile topology using defined key.
                if shpfile is None:
                    shpfile = self.invfile
                # Differentiat between rook and queen's case.
                if rook:
                    w = pysal.rook_from_shapefile(shpfile, self.invcol)
                else:
                    w = pysal.queen_from_shapefile(shpfile, self.invcol)
            except:
                msg = "Couldn't build spatial weight matrix for vector "
                "inventory <%s>" % (self.name)
                raise RuntimeError(msg)

            # Match weight index to inventory array index.
            w.id_order = list(self.inv_index)

            logger.info("Weight matrix in %s's case successfully calculated "
                        "for vector dataset" % case)
        elif self.sptype == 'raster':
            try:
                # Construct weight matrix in input grid size.
                w = pysal.lat2W(*dim, rook=rook)
            except:
                msg = "Couldn't build spatial weight matrix for raster "
                "inventory <%s>" % (self.name)
                raise RuntimeError(msg)

            logger.info("Weight matrix in %s's case successfully calculated "
                        "for raster dataset" % case)

        # Print imported raster summary.
        print("[ WEIGHT NUMBER ] = ", w.n)
        print("[ MIN NEIGHBOR ] = ", w.min_neighbors)
        print("[ MAX NEIGHBOR ] = ", w.max_neighbors)
        print("[ ISLANDS ] = ", *w.islands)
        print("[ HISTOGRAM ] = ", *w.histogram)

        self._Inventory__modmtime()

        return(w)
Exemplo n.º 25
0
 def test_asymmetries(self):
     w = pysal.lat2W(3, 3)
     w.transform = 'r'
     result = w.asymmetry()
     self.assertEqual(result,
                      [(0, 1), (0, 3), (1, 0), (1, 2), (1, 4), (2, 1),
                       (2, 5), (3, 0), (3, 4), (3, 6), (4, 1), (4, 3),
                       (4, 5), (4, 7), (5, 2), (5, 4), (5, 8), (6, 3),
                       (6, 7), (7, 4), (7, 6), (7, 8), (8, 5), (8, 7)])
Exemplo n.º 26
0
 def test_asymmetries(self):
     w = pysal.lat2W(3, 3)
     w.transform = 'r'
     result = w.asymmetry()
     self.assertEqual(result, [(0, 1), (0, 3), (1, 0), (1, 2), (1, 4),
                               (2, 1), (2, 5), (3, 0), (3, 4), (3, 6),
                               (4, 1), (4, 3), (4, 5), (4, 7), (5, 2),
                               (5, 4), (5, 8), (6, 3), (6, 7), (7, 4),
                               (7, 6), (7, 8), (8, 5), (8, 7)])
Exemplo n.º 27
0
 def test_Maxp(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones((w.n, 1), float)
     floor = 3
     solution = pysal.region.Maxp(
         w, z, floor, floor_variable=p, initial=100)
     self.assertEquals(solution.p, 29)
     self.assertEquals(solution.regions[0], [4, 14, 5, 24, 3, 25, 15, 23])
Exemplo n.º 28
0
def convert_to_pysal(data):
    """
    Pysal expects a distance matrix, and data formatted in a numpy array.
    This functions takes a data grid and returns those things.
    """
    w = pysal.lat2W(len(data[0]), len(data))
    data = np.array(data)
    data = np.reshape(data, (len(data)*len(data[0]), 1))
    return w, data
Exemplo n.º 29
0
 def test_Maxp(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones((w.n, 1), float)
     floor = 3
     np.random.seed(111)
     solution = pysal.region.Maxp(
         w, z, floor, floor_variable=p, initial=100)
     self.assertEquals(solution.p, 28)
     self.assertEquals(solution.regions[0], [51, 61, 71])
Exemplo n.º 30
0
    def setUp(self):
        from pysal import rook_from_shapefile
        self.w = rook_from_shapefile(pysal.examples.get_path('10740.shp'))

        self.neighbors = {0: [3, 1], 1: [0, 4, 2], 2: [1, 5], 3: [0, 6, 4], 4: [1, 3,
                                                                                7, 5], 5: [2, 4, 8], 6: [3, 7], 7: [4, 6, 8], 8: [5, 7]}
        self.weights = {0: [1, 1], 1: [1, 1, 1], 2: [1, 1], 3: [1, 1, 1], 4: [1, 1,
                                                                              1, 1], 5: [1, 1, 1], 6: [1, 1], 7: [1, 1, 1], 8: [1, 1]}

        self.w3x3 = pysal.lat2W(3, 3)
Exemplo n.º 31
0
 def test_Maxp(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones((w.n, 1), float)
     floor = 3
     solution = pysal.region.Maxp(w,
                                  z,
                                  floor,
                                  floor_variable=p,
                                  initial=100)
     self.assertEquals(solution.p, 29)
     self.assertEquals(solution.regions[0], [4, 14, 5, 24, 3, 25, 15, 23])
Exemplo n.º 32
0
 def setUp(self):
     self.neighbors = {'c': ['b'], 'b': ['c', 'a'], 'a': ['b']}
     self.weights = {'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
     self.id_order = ['a', 'b', 'c']
     self.weights = {'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
     self.w = pysal.W(self.neighbors, self.weights, self.id_order)
     self.y = np.array([0, 1, 2])
     self.wlat = pysal.lat2W(3, 3)
     self.ycat = ['a','b','a','b','c','b','c','b','c']
     self.ycat2 = ['a', 'c', 'c', 'd', 'b', 'a', 'd', 'd', 'c']
     self.ym = np.vstack((self.ycat,self.ycat2)).T
     self.random_seed = 503
Exemplo n.º 33
0
 def test_remap_ids(self):
     w = pysal.lat2W(3, 2)
     wid_order = [0, 1, 2, 3, 4, 5]
     self.assertEquals(wid_order, w.id_order)
     wneighbors0 = [2, 1]
     self.assertEquals(wneighbors0, w.neighbors[0])
     old_to_new = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
     w_new = pysal.remap_ids(w, old_to_new)
     w_newid_order = ['a', 'b', 'c', 'd', 'e', 'f']
     self.assertEquals(w_newid_order, w_new.id_order)
     w_newdneighborsa = ['c', 'b']
     self.assertEquals(w_newdneighborsa, w_new.neighbors['a'])
Exemplo n.º 34
0
 def test_order(self):
     w = pysal.lat2W(3, 3)
     o = {0: [-1, 1, 2, 1, 2, 3, 2, 3, 0],
          1: [1, -1, 1, 2, 1, 2, 3, 2, 3],
          2: [2, 1, -1, 3, 2, 1, 0, 3, 2],
          3: [1, 2, 3, -1, 1, 2, 1, 2, 3],
          4: [2, 1, 2, 1, -1, 1, 2, 1, 2],
          5: [3, 2, 1, 2, 1, -1, 3, 2, 1],
          6: [2, 3, 0, 1, 2, 3, -1, 1, 2],
          7: [3, 2, 3, 2, 1, 2, 1, -1, 1],
          8: [0, 3, 2, 3, 2, 1, 2, 1, -1]}
     self.assertEquals(pysal.order(w), o)
Exemplo n.º 35
0
 def test_variance(self):
     y = np.arange(1, 10)
     w = pysal.lat2W(3, 3)
     mi = pysal.Moran(y, w, transformation='B')
     np.testing.assert_allclose(mi.VI_rand,
                                0.059687500000000004,
                                atol=ATOL,
                                rtol=RTOL)
     np.testing.assert_allclose(mi.VI_norm,
                                0.053125000000000006,
                                atol=ATOL,
                                rtol=RTOL)
Exemplo n.º 36
0
 def test_remap_ids(self):
     w = pysal.lat2W(3, 2)
     wid_order = [0, 1, 2, 3, 4, 5]
     self.assertEquals(wid_order, w.id_order)
     wneighbors0 = [2, 1]
     self.assertEquals(wneighbors0, w.neighbors[0])
     old_to_new = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f'}
     w_new = pysal.remap_ids(w, old_to_new)
     w_newid_order = ['a', 'b', 'c', 'd', 'e', 'f']
     self.assertEquals(w_newid_order, w_new.id_order)
     w_newdneighborsa = ['c', 'b']
     self.assertEquals(w_newdneighborsa, w_new.neighbors['a'])
Exemplo n.º 37
0
 def test_order(self):
     w = pysal.lat2W(3, 3)
     o = {0: [-1, 1, 2, 1, 2, 3, 2, 3, 0],
          1: [1, -1, 1, 2, 1, 2, 3, 2, 3],
          2: [2, 1, -1, 3, 2, 1, 0, 3, 2],
          3: [1, 2, 3, -1, 1, 2, 1, 2, 3],
          4: [2, 1, 2, 1, -1, 1, 2, 1, 2],
          5: [3, 2, 1, 2, 1, -1, 3, 2, 1],
          6: [2, 3, 0, 1, 2, 3, -1, 1, 2],
          7: [3, 2, 3, 2, 1, 2, 1, -1, 1],
          8: [0, 3, 2, 3, 2, 1, 2, 1, -1]}
     self.assertEquals(pysal.order(w), o)
Exemplo n.º 38
0
 def setUp(self):
     self.neighbors = {'c': ['b'], 'b': ['c', 'a'], 'a': ['b']}
     self.weights = {'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
     self.id_order = ['a', 'b', 'c']
     self.weights = {'c': [1.0], 'b': [1.0, 1.0], 'a': [1.0]}
     self.w = pysal.W(self.neighbors, self.weights, self.id_order)
     self.y = np.array([0, 1, 2])
     self.wlat = pysal.lat2W(3, 3)
     self.ycat = ['a', 'b', 'a', 'b', 'c', 'b', 'c', 'b', 'c']
     self.ycat2 = ['a', 'c', 'c', 'd', 'b', 'a', 'd', 'd', 'c']
     self.ym = np.vstack((self.ycat, self.ycat2)).T
     self.random_seed = 503
 def test_Maxp(self):
     w = pysal.lat2W(10, 10)
     z = np.random.random_sample((w.n, 2))
     p = np.ones((w.n, 1), float)
     floor = 3
     np.random.seed(111)
     solution = pysal.region.Maxp(w,
                                  z,
                                  floor,
                                  floor_variable=p,
                                  initial=100)
     self.assertEqual(solution.p, 28)
     self.assertEqual(solution.regions[0], [51, 61, 71])
Exemplo n.º 40
0
 def test_cardinalities(self):
     w = pysal.lat2W(3, 3)
     self.assertEqual(w.cardinalities, {
         0: 2,
         1: 3,
         2: 2,
         3: 3,
         4: 4,
         5: 3,
         6: 2,
         7: 3,
         8: 2
     })
Exemplo n.º 41
0
    def setUp(self):
        from pysal import rook_from_shapefile
        self.w = rook_from_shapefile(pysal.examples.get_path('10740.shp'))
        wsp = self.w.to_WSP()
        self.w = wsp.to_W()

        self.neighbors = {0: [3, 1], 1: [0, 4, 2], 2: [1, 5], 3: [0, 6, 4],
                          4: [1, 3, 7, 5], 5: [2, 4, 8], 6: [3, 7],
                          7: [4, 6, 8], 8: [5, 7]}
        self.weights = {0: [1, 1], 1: [1, 1, 1], 2: [1, 1], 3: [1, 1, 1],
                        4: [1, 1, 1, 1], 5: [1, 1, 1], 6: [1, 1], 7: [1, 1, 1],
                        8: [1, 1]}

        self.w3x3 = pysal.lat2W(3, 3)
        w3x3 = pysal.weights.WSP(self.w3x3.sparse, self.w3x3.id_order)
        self.w3x3 = pysal.weights.WSP2W(w3x3)
Exemplo n.º 42
0
 def test_lag_spatial(self):
     yl = pysal.lag_spatial(self.w, self.y)
     np.testing.assert_array_almost_equal(yl, [1., 2., 1.])
     self.w.id_order = ['b', 'c', 'a']
     y = np.array([1, 2, 0])
     yl = pysal.lag_spatial(self.w, y)
     np.testing.assert_array_almost_equal(yl, [2., 1., 1.])
     w = pysal.lat2W(3, 3)
     y = np.arange(9)
     yl = pysal.lag_spatial(w, y)
     ylc = np.array([4., 6., 6., 10., 16., 14., 10., 18., 12.])
     np.testing.assert_array_almost_equal(yl, ylc)
     w.transform = 'r'
     yl = pysal.lag_spatial(w, y)
     ylc = np.array([2., 2., 3., 3.33333333, 4., 4.66666667, 5., 6., 6.])
     np.testing.assert_array_almost_equal(yl, ylc)
Exemplo n.º 43
0
def _lat2Network(k):
    """helper function to create a network from a square lattice.
    
    Used for testing purposes 
    """
    lat = ps.lat2W(k+1,k+1) 
    k1 = k+1
    nodes = {}
    edges = []
    for node in lat.id_order:
        for neighbor in lat[node]:
            edges.append((node,neighbor))
        nodes[node] = ( node/k1, node%k1 )

    res = {"nodes": nodes, "edges": edges}

    return res
Exemplo n.º 44
0
Arquivo: util.py Projeto: xrzhou/pysal
def lat2Network(k):
    """helper function to create a network from a square lattice.

    Used for testing purposes
    """
    lat = ps.lat2W(k + 1, k + 1)
    k1 = k + 1
    nodes = {}
    edges = []
    for node in lat.id_order:
        for neighbor in lat[node]:
            edges.append((node, neighbor))
        nodes[node] = (node / k1, node % k1)

    res = {"nodes": nodes, "edges": edges}

    return res
Exemplo n.º 45
0
 def test_lag_spatial(self):
     yl = pysal.lag_spatial(self.w, self.y)
     np.testing.assert_array_almost_equal(yl, [1., 2., 1.])
     self.w.id_order = ['b', 'c', 'a']
     y = np.array([1, 2, 0])
     yl = pysal.lag_spatial(self.w, y)
     np.testing.assert_array_almost_equal(yl, [2., 1., 1.])
     w = pysal.lat2W(3, 3)
     y = np.arange(9)
     yl = pysal.lag_spatial(w, y)
     ylc = np.array([4., 6., 6., 10., 16., 14., 10., 18., 12.])
     np.testing.assert_array_almost_equal(yl, ylc)
     w.transform = 'r'
     yl = pysal.lag_spatial(w, y)
     ylc = np.array(
         [2., 2., 3., 3.33333333, 4.,
          4.66666667, 5., 6., 6.])
     np.testing.assert_array_almost_equal(yl, ylc)
Exemplo n.º 46
0
def create_mtx(x):
    """
    creates adjacency matrix
    :param x: size of matrix
    :return: adjacency matrix
    """
    X = np.random.rand(x, x)

    # optional way to create adjacency matrix
    # adj_mtx, _ = embed_mesh(X)

    w = pysal.lat2W(x, x)
    adj_mtx = np.array(w.full()[0])

    coords = []
    for i in range(x):
        for j in range(x):
            coords.append([i, j, 0])

    coords = np.array(coords)
    return adj_mtx, coords
Exemplo n.º 47
0
def test(cores=None):
    """
    """
    #Test data
    w = ps.lat2W(10, 10)
    random_int = RandomState(123456789)
    attribute = random_int.random_sample((w.n, 2))

    #mp Boilerplate
    if cores == None:
        cores = mp.cpu_count()
    numifs = 20

    #Locking solution space
    solution_lock = mp.Lock()
    csoln_space = mp.Array(ctypes.c_int32,
                           numifs * (w.n + 1),
                           lock=solution_lock)
    soln_space = np.frombuffer(csoln_space.get_obj(), dtype=np.int32)
    soln_space[:] = 0
    soln_space.shape = (-1, w.n + 1)
    initshared_soln(csoln_space)

    jobs = []
    for i in xrange(cores):
        p = IFS(attribute, w, lock=solution_lock, pid=i)
        jobs.append(p)
        p.start()
    for j in jobs:
        j.join()

    for i in range(numifs):
        checkcontiguity(soln_space[i], w)
    """
    for i in range(numifs):
        print soln_space[i][1:].reshape(-1,10)
        print
    """
    print "Generated solution space with {} regions per solution".format(
        soln_space[:, 0])
Exemplo n.º 48
0
def test(cores=None):
    """
    """
    #Test data
    w = ps.lat2W(10, 10)
    random_int = RandomState(123456789)
    attribute = random_int.random_sample((w.n, 2))

    #mp Boilerplate
    if cores == None:
        cores = mp.cpu_count()
    numifs = 20

    #Locking solution space
    solution_lock = mp.Lock()
    csoln_space = mp.Array(ctypes.c_int32, numifs * (w.n + 1), lock=solution_lock)
    soln_space = np.frombuffer(csoln_space.get_obj(), dtype=np.int32)
    soln_space[:] = 0
    soln_space.shape = (-1, w.n + 1)
    initshared_soln(csoln_space)

    jobs = []
    for i in xrange(cores):
        p = IFS(attribute, w, lock=solution_lock, pid=i)
        jobs.append(p)
        p.start()
    for j in jobs:
        j.join()

    for i in range(numifs):
        checkcontiguity(soln_space[i], w)

    """
    for i in range(numifs):
        print soln_space[i][1:].reshape(-1,10)
        print
    """
    print "Generated solution space with {} regions per solution".format(soln_space[:,0])
Exemplo n.º 49
0
 def setUp(self):
     from pysal import rook_from_shapefile
     self.w = pysal.open(pysal.examples.get_path("sids2.gal")).read()
     self.wsp = pysal.weights.WSP(self.w.sparse, self.w.id_order)
     w3x3 = pysal.lat2W(3, 3)
     self.w3x3 = pysal.weights.WSP(w3x3.sparse)
Exemplo n.º 50
0
 def setUp(self):
     self.w = pysal.lat2W(4, 4)
     self.y = np.ones(16)
     self.y[0:8] = 0
Exemplo n.º 51
0
def estMorans(frame):
    workingFrame = copy.deepcopy(frame)
    x, y, z = zip(*workingFrame)
    w = ps.lat2W(int(max(x) + 1.0), int(max(y) + 1.0), rook=False)
    lm = ps.Moran(z, w)
    return (lm.I)
Exemplo n.º 52
0
    offsets = np.array(offsets)
    m = sparse.dia_matrix((data, offsets), shape=(n, n), dtype=np.int8)
    m = m + m.T
    if row_st:
        m = sparse.spdiags(1. / m.sum(1).T, 0, *m.shape) * m
    return m


def write_gal(file, k=10):
    f = open(file, 'w')
    n = k * k
    f.write("0 %d" % n)
    for i in xrange(n):
        row = i / k
        col = i % k
        neighs = [i - i, i + 1, i - k, i + k]
        neighs = [j for j in neighs if j >= 0 and j < n]
        f.write("\n%d %d\n" % (i, len(neighs)))
        f.write(" ".join(map(str, neighs)))
    f.close()

if __name__ == "__main__":
    from pysal import lat2W

    assert (lat2W(5, 5).sparse.todense() == lat2SW(5, 5).todense()).all()
    assert (lat2W(5, 3).sparse.todense() == lat2SW(5, 3).todense()).all()
    assert (lat2W(5, 3, rook=False).sparse.todense() == lat2SW(5, 3,
                                                               'queen').todense()).all()
    assert (lat2W(50, 50, rook=False).sparse.todense() == lat2SW(50,
                                                                 50, 'queen').todense()).all()
Exemplo n.º 53
0
def hexLat2W(nrows=5, ncols=5):
    """
    Create a W object for a hexagonal lattice.

    Parameters
    ----------

    nrows   : int
              number of rows
    ncols   : int
              number of columns

    Returns
    -------

    w : W
        instance of spatial weights class W

    Notes
    -----

    Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.

    Construction is based on shifting every other column of a regular lattice
    down 1/2 of a cell.

    Examples
    --------

    >>> import pysal as ps
    >>> w = ps.lat2W()
    >>> w.neighbors[1]
    [0, 6, 2]
    >>> w.neighbors[21]
    [16, 20, 22]
    >>> wh = ps.hexLat2W()
    >>> wh.neighbors[1]
    [0, 6, 2, 5, 7]
    >>> wh.neighbors[21]
    [16, 20, 22]
    >>>
    """

    if nrows == 1 or ncols == 1:
        print "Hexagon lattice requires at least 2 rows and columns"
        print "Returning a linear contiguity structure"
        return lat2W(nrows, ncols)

    n = nrows * ncols
    rid = [i / ncols for i in xrange(n)]
    cid = [i % ncols for i in xrange(n)]
    r1 = nrows - 1
    c1 = ncols - 1

    w = lat2W(nrows, ncols).neighbors
    for i in xrange(n):
        odd = cid[i] % 2
        if odd:
            if rid[i] < r1:  # odd col index above last row
                # new sw neighbor
                if cid[i] > 0:
                    j = i + ncols - 1
                    w[i] = w.get(i, []) + [j]
                # new se neighbor
                if cid[i] < c1:
                    j = i + ncols + 1
                    w[i] = w.get(i, []) + [j]

        else:  # even col
            # nw
            jnw = [i - ncols - 1]
            # ne
            jne = [i - ncols + 1]
            if rid[i] > 0:
                w[i]
                if cid[i] == 0:
                    w[i] = w.get(i, []) + jne
                elif cid[i] == c1:
                    w[i] = w.get(i, []) + jnw
                else:
                    w[i] = w.get(i, []) + jne
                    w[i] = w.get(i, []) + jnw


    return pysal.weights.W(w)
Exemplo n.º 54
0
def hexLat2W(nrows=5, ncols=5):
    """
    Create a W object for a hexagonal lattice.

    Parameters
    ----------

    nrows   : int
              number of rows
    ncols   : int
              number of columns

    Returns
    -------

    w : W
        instance of spatial weights class W

    Notes
    -----

    Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.

    Construction is based on shifting every other column of a regular lattice
    down 1/2 of a cell.

    Examples
    --------

    >>> import pysal as ps
    >>> w = ps.lat2W()
    >>> w.neighbors[1]
    [0, 6, 2]
    >>> w.neighbors[21]
    [16, 20, 22]
    >>> wh = ps.hexLat2W()
    >>> wh.neighbors[1]
    [0, 6, 2, 5, 7]
    >>> wh.neighbors[21]
    [16, 20, 22]
    >>>
    """

    if nrows == 1 or ncols == 1:
        print "Hexagon lattice requires at least 2 rows and columns"
        print "Returning a linear contiguity structure"
        return lat2W(nrows, ncols)

    n = nrows * ncols
    rid = [i / ncols for i in xrange(n)]
    cid = [i % ncols for i in xrange(n)]
    r1 = nrows - 1
    c1 = ncols - 1

    w = lat2W(nrows, ncols).neighbors
    for i in xrange(n):
        odd = cid[i] % 2
        if odd:
            if rid[i] < r1:  # odd col index above last row
                # new sw neighbor
                if cid[i] > 0:
                    j = i + ncols - 1
                    w[i] = w.get(i, []) + [j]
                # new se neighbor
                if cid[i] < c1:
                    j = i + ncols + 1
                    w[i] = w.get(i, []) + [j]

        else:  # even col
            # nw
            jnw = [i - ncols - 1]
            # ne
            jne = [i - ncols + 1]
            if rid[i] > 0:
                w[i]
                if cid[i] == 0:
                    w[i] = w.get(i, []) + jne
                elif cid[i] == c1:
                    w[i] = w.get(i, []) + jnw
                else:
                    w[i] = w.get(i, []) + jne
                    w[i] = w.get(i, []) + jnw

    return pysal.weights.W(w)
Exemplo n.º 55
0
[0.1111111,0.1111111,0.1111111,0,0.1111111,0.1111111,0.1111111,0,0,0.1111111,0,0.1111111,0.1111111,0]]

yy=[
    [1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
[1,2,3,4,5,6,7,8,9,10,11,12,13,14],
]
npy=np.arange(196)
npy.shape=(14,14)

weight1=np.array(weight1)
y1=pysal.lag_spatial(weight1,npy)

print(y1)
'''
w = pysal.lat2W(5, 5)
w2 = pysal.higher_order(w, 2)
print(w)
print(w2)
Exemplo n.º 56
0
 def test_cardinalities(self):
     w = pysal.lat2W(3, 3)
     self.assertEqual(w.cardinalities, {0: 2, 1: 3, 2: 2, 3: 3, 4: 4, 5: 3,
                                        6: 2, 7: 3, 8: 2})
Exemplo n.º 57
0
 def test_asymmetry(self):
     w = pysal.lat2W(3, 3)
     self.assertEqual(w.asymmetry(), [])
     w.transform = 'r'
     self.assertFalse(w.asymmetry() == [])
Exemplo n.º 58
0
 def test_n(self):
     w = pysal.lat2W()
     self.assertEqual(w.n, 25)
Exemplo n.º 59
0
    m = sparse.dia_matrix((data, offsets), shape=(n, n), dtype=np.int8)
    m = m + m.T
    if row_st:
        m = sparse.spdiags(1. / m.sum(1).T, 0, *m.shape) * m
    return m


def write_gal(file, k=10):
    f = open(file, 'w')
    n = k * k
    f.write("0 %d" % n)
    for i in xrange(n):
        row = i / k
        col = i % k
        neighs = [i - i, i + 1, i - k, i + k]
        neighs = [j for j in neighs if j >= 0 and j < n]
        f.write("\n%d %d\n" % (i, len(neighs)))
        f.write(" ".join(map(str, neighs)))
    f.close()


if __name__ == "__main__":
    from pysal import lat2W

    assert (lat2W(5, 5).sparse.todense() == lat2SW(5, 5).todense()).all()
    assert (lat2W(5, 3).sparse.todense() == lat2SW(5, 3).todense()).all()
    assert (lat2W(5, 3, rook=False).sparse.todense() == lat2SW(
        5, 3, 'queen').todense()).all()
    assert (lat2W(50, 50, rook=False).sparse.todense() == lat2SW(
        50, 50, 'queen').todense()).all()
Exemplo n.º 60
0
 def test_set_transform(self):
     w = pysal.lat2W(2, 2)
     self.assertEqual(w.transform, 'O')
     self.assertEquals(w.weights[0], [1.0, 1.0])
     w.transform = 'r'
     self.assertEquals(w.weights[0], [0.5, 0.5])