@@ -114,41 +114,34 @@ def hexLat2W(nrows=5, ncols=5, **kwargs):
114
114
return W (w , ** kwargs )
115
115
116
116
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 ):
118
118
"""
119
119
Create a W object for a regular lattice.
120
-
121
120
Parameters
122
121
----------
123
-
124
122
nrows : int
125
123
number of rows
126
124
ncols : int
127
125
number of columns
128
126
rook : boolean
129
127
type of contiguity. Default is rook. For queen, rook =False
128
+ torus : boolean
129
+ generates weights for circles, spheres. Default is set to False
130
130
id_type : string
131
131
string defining the type of IDs to use in the final W object;
132
132
options are 'int' (0, 1, 2 ...; default), 'float' (0.0,
133
133
1.0, 2.0, ...) and 'string' ('id0', 'id1', 'id2', ...)
134
134
**kwargs : keyword arguments
135
135
optional arguments for :class:`pysal.weights.W`
136
-
137
-
138
136
Returns
139
137
-------
140
-
141
138
w : W
142
139
instance of spatial weights class W
143
-
144
140
Notes
145
141
-----
146
-
147
142
Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.
148
-
149
143
Examples
150
144
--------
151
-
152
145
>>> from libpysal.weights import lat2W
153
146
>>> w9 = lat2W(3,3)
154
147
>>> "%.3f"%w9.pct_nonzero
@@ -161,11 +154,11 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int', **kwargs):
161
154
n = nrows * ncols
162
155
r1 = nrows - 1
163
156
c1 = ncols - 1
157
+ lat_pts = range (n )
164
158
rid = [i // ncols for i in range (n )] #must be floor!
165
159
cid = [i % ncols for i in range (n )]
166
160
w = {}
167
- r = below = 0
168
- for i in range (n - 1 ):
161
+ for i in range (n ):
169
162
if rid [i ] < r1 :
170
163
below = rid [i ] + 1
171
164
r = below * ncols + cid [i ]
@@ -187,6 +180,38 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int', **kwargs):
187
180
r = (rid [i ] + 1 ) * ncols - 1 + cid [i ]
188
181
w [i ] = w .get (i , []) + [r ]
189
182
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 ]
190
215
191
216
neighbors = {}
192
217
weights = {}
@@ -211,6 +236,7 @@ def lat2W(nrows=5, ncols=5, rook=True, id_type='int', **kwargs):
211
236
return W (w , weights , ids = ids , id_order = ids [:], ** kwargs )
212
237
213
238
239
+
214
240
def block_weights (regimes , ids = None , sparse = False , ** kwargs ):
215
241
"""
216
242
Construct spatial weights for regime neighbors.
0 commit comments