@@ -114,41 +114,34 @@ def hexLat2W(nrows=5, ncols=5, **kwargs):
114114 return W (w , ** kwargs )
115115
116116
117- def lat2W (nrows = 5 , ncols = 5 , rook = True , id_type = 'int' , ** kwargs ):
117+ def lat2W (nrows = 5 , ncols = 5 , rook = True , torus = False , id_type = 'int' , ** kwargs ):
118118 """
119119 Create a W object for a regular lattice.
120-
121120 Parameters
122121 ----------
123-
124122 nrows : int
125123 number of rows
126124 ncols : int
127125 number of columns
128126 rook : boolean
129127 type of contiguity. Default is rook. For queen, rook =False
128+ torus : boolean
129+ generates weights for circles, spheres. Default is set to False
130130 id_type : string
131131 string defining the type of IDs to use in the final W object;
132132 options are 'int' (0, 1, 2 ...; default), 'float' (0.0,
133133 1.0, 2.0, ...) and 'string' ('id0', 'id1', 'id2', ...)
134134 **kwargs : keyword arguments
135135 optional arguments for :class:`pysal.weights.W`
136-
137-
138136 Returns
139137 -------
140-
141138 w : W
142139 instance of spatial weights class W
143-
144140 Notes
145141 -----
146-
147142 Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.
148-
149143 Examples
150144 --------
151-
152145 >>> from libpysal.weights import lat2W
153146 >>> w9 = lat2W(3,3)
154147 >>> "%.3f"%w9.pct_nonzero
@@ -161,11 +154,11 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int', **kwargs):
161154 n = nrows * ncols
162155 r1 = nrows - 1
163156 c1 = ncols - 1
157+ lat_pts = range (n )
164158 rid = [i // ncols for i in range (n )] #must be floor!
165159 cid = [i % ncols for i in range (n )]
166160 w = {}
167- r = below = 0
168- for i in range (n - 1 ):
161+ for i in range (n ):
169162 if rid [i ] < r1 :
170163 below = rid [i ] + 1
171164 r = below * ncols + cid [i ]
@@ -187,6 +180,38 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int', **kwargs):
187180 r = (rid [i ] + 1 ) * ncols - 1 + cid [i ]
188181 w [i ] = w .get (i , []) + [r ]
189182 w [r ] = w .get (r , []) + [i ]
183+ if torus :
184+ if rid [i ] == r1 and r1 not in [0 , 1 ]:
185+ below = rid [i ] + 1
186+ r = lat_pts [(below * ncols + cid [i ]) - n ]
187+ w [i ] = w .get (i , []) + [r ]
188+ w [r ] = w .get (r , []) + [i ]
189+ if cid [i ] == c1 and c1 not in [0 , 1 ]:
190+ c = lat_pts [(rid [i ] * ncols ) - n ]
191+ w [i ] = w .get (i , []) + [c ]
192+ w [c ] = w .get (c , []) + [i ]
193+ if not rook :
194+ if r1 not in [0 , 1 ]:
195+ # southeast bishop
196+ if cid [i ] < c1 and rid [i ] == r1 :
197+ r = lat_pts [((rid [i ] + 1 ) * ncols + 1 + cid [i ]) - n ]
198+ w [i ] = w .get (i , []) + [r ]
199+ w [r ] = w .get (r , []) + [i ]
200+ # southwest bishop
201+ if cid [i ] > 0 and rid [i ] == r1 :
202+ r = lat_pts [((rid [i ] + 1 ) * ncols - 1 + cid [i ]) - n ]
203+ w [i ] = w .get (i , []) + [r ]
204+ w [r ] = w .get (r , []) + [i ]
205+ if c1 not in [0 , 1 ]:
206+ if cid [i ] == c1 :
207+ r = lat_pts [i + 1 - n ]
208+ w [i ] = w .get (i , []) + [r ]
209+ w [r ] = w .get (r , []) + [i ]
210+ if cid [i ] == 0 :
211+ below = rid [i ] + 2
212+ r = lat_pts [(below * ncols - 1 ) - n ]
213+ w [i ] = w .get (i , []) + [r ]
214+ w [r ] = w .get (r , []) + [i ]
190215
191216 neighbors = {}
192217 weights = {}
@@ -211,6 +236,7 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int', **kwargs):
211236 return W (w , weights , ids = ids , id_order = ids [:], ** kwargs )
212237
213238
239+
214240def block_weights (regimes , ids = None , sparse = False , ** kwargs ):
215241 """
216242 Construct spatial weights for regime neighbors.
0 commit comments