23
23
'shimbel' , 'remap_ids' , 'full2W' , 'full' , 'WSP2W' ,
24
24
'insert_diagonal' , 'get_ids' , 'get_points_array_from_shapefile' ,
25
25
'min_threshold_distance' , 'lat2SW' , 'w_local_cluster' ,
26
- 'higher_order_sp' , 'hexLat2W' , 'attach_islands' ,
26
+ 'higher_order_sp' , 'hexLat2W' , 'torusW' , ' attach_islands' ,
27
27
'nonplanar_neighbors' , 'fuzzy_contiguity' ]
28
28
29
29
@@ -113,35 +113,121 @@ def hexLat2W(nrows=5, ncols=5, **kwargs):
113
113
114
114
return W (w , ** kwargs )
115
115
116
+ def torusW (nrows = 5 , ncols = 5 , rook = True , ** kwargs ):
117
+ """
118
+ Create a W object for a torus lattice.
119
+
120
+ Parameters
121
+ ----------
122
+ nrows : int
123
+ number of rows
124
+ ncols : int
125
+ number of columns
126
+ rook : boolean
127
+ type of contiguity. Default is rook. For queen, rook = False
128
+ **kwargs : keyword arguments
129
+ optional arguments for :class:`pysal.weights.W`
130
+
131
+ Returns
132
+ -------
133
+ w : W
134
+ instance of spatial weights class W
135
+
136
+ Notes
137
+ -----
138
+ Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.
139
+
140
+ Examples
141
+ --------
142
+ >>> from libpysal.weights import lat2W, torusW
143
+ >>> w = lat2W(3,3)
144
+ >>> w.neighbors[0]
145
+ [3, 1]
146
+ >>> w.neighbors[3]
147
+ [0, 6, 4]
148
+ >>> wt = torusW(3,3)
149
+ >>> wt.neighbors[0]
150
+ [3, 1, 2, 6]
151
+ >>> wh.neighbors[3]
152
+ [0, 6, 4, 5]
153
+ """
154
+
155
+ n = nrows * ncols
156
+ r1 = nrows - 1
157
+ c1 = ncols - 1
158
+ lat_pts = range (n )
159
+ rid = [i // ncols for i in range (n )] #must be floor!
160
+ cid = [i % ncols for i in range (n )]
161
+
162
+ w = lat2W (nrows , ncols , rook ).neighbors
163
+ for i in range (n ):
164
+ if rid [i ] == r1 and r1 not in [0 , 1 ]:
165
+ below = rid [i ] + 1
166
+ r = lat_pts [(below * ncols + cid [i ]) - n ]
167
+ w [i ] = w .get (i , []) + [r ]
168
+ w [r ] = w .get (r , []) + [i ]
169
+ if cid [i ] == c1 and c1 not in [0 , 1 ]:
170
+ c = lat_pts [(rid [i ] * ncols ) - n ]
171
+ w [i ] = w .get (i , []) + [c ]
172
+ w [c ] = w .get (c , []) + [i ]
173
+ if not rook :
174
+ if r1 not in [0 , 1 ]:
175
+ if cid [i ] < c1 and rid [i ] == r1 :
176
+ r = lat_pts [((rid [i ] + 1 ) * ncols + 1 + cid [i ]) - n ]
177
+ w [i ] = w .get (i , []) + [r ]
178
+ w [r ] = w .get (r , []) + [i ]
179
+ if cid [i ] > 0 and rid [i ] == r1 :
180
+ r = lat_pts [((rid [i ] + 1 ) * ncols - 1 + cid [i ]) - n ]
181
+ w [i ] = w .get (i , []) + [r ]
182
+ w [r ] = w .get (r , []) + [i ]
183
+ if c1 not in [0 , 1 ]:
184
+ if cid [i ] == c1 :
185
+ r = lat_pts [i + 1 - n ]
186
+ w [i ] = w .get (i , []) + [r ]
187
+ w [r ] = w .get (r , []) + [i ]
188
+ if cid [i ] == 0 :
189
+ below = rid [i ] + 2
190
+ r = lat_pts [(below * ncols - 1 ) - n ]
191
+ w [i ] = w .get (i , []) + [r ]
192
+ w [r ] = w .get (r , []) + [i ]
193
+
194
+ return W (w , ** kwargs )
116
195
117
- def lat2W (nrows = 5 , ncols = 5 , rook = True , torus = False , id_type = 'int' , ** kwargs ):
196
+ def lat2W (nrows = 5 , ncols = 5 , rook = True , id_type = 'int' , ** kwargs ):
118
197
"""
119
198
Create a W object for a regular lattice.
199
+
120
200
Parameters
121
201
----------
202
+
122
203
nrows : int
123
204
number of rows
124
205
ncols : int
125
206
number of columns
126
207
rook : boolean
127
208
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
209
id_type : string
131
210
string defining the type of IDs to use in the final W object;
132
211
options are 'int' (0, 1, 2 ...; default), 'float' (0.0,
133
212
1.0, 2.0, ...) and 'string' ('id0', 'id1', 'id2', ...)
134
213
**kwargs : keyword arguments
135
214
optional arguments for :class:`pysal.weights.W`
215
+
216
+
136
217
Returns
137
218
-------
219
+
138
220
w : W
139
221
instance of spatial weights class W
222
+
140
223
Notes
141
224
-----
225
+
142
226
Observations are row ordered: first k observations are in row 0, next k in row 1, and so on.
227
+
143
228
Examples
144
229
--------
230
+
145
231
>>> from libpysal.weights import lat2W
146
232
>>> w9 = lat2W(3,3)
147
233
>>> "%.3f"%w9.pct_nonzero
@@ -150,24 +236,14 @@ def lat2W(nrows=5, ncols=5, rook=True, torus=False, id_type='int', **kwargs):
150
236
True
151
237
>>> w9[3] == {0: 1.0, 4: 1.0, 6: 1.0}
152
238
True
153
-
154
- >>> w9t = lat2W(3,3,torus=True)
155
- >>> "%.3f"%w9t.pct_nonzero
156
- '44.444'
157
- >>> w9t[0] == {1: 1.0, 2: 1.0, 3: 1.0, 6: 1.0}
158
- True
159
- >>> w9t[3] == {0: 1.0, 4: 1.0, 5: 1.0, 6: 1.0}
160
- True
161
239
"""
162
-
163
240
n = nrows * ncols
164
241
r1 = nrows - 1
165
242
c1 = ncols - 1
166
- lat_pts = range (n )
167
243
rid = [i // ncols for i in range (n )] #must be floor!
168
244
cid = [i % ncols for i in range (n )]
169
245
w = {}
170
- for i in range (n ):
246
+ for i in range (n - 1 ):
171
247
if rid [i ] < r1 :
172
248
below = rid [i ] + 1
173
249
r = below * ncols + cid [i ]
@@ -189,38 +265,6 @@ def lat2W(nrows=5, ncols=5, rook=True, torus=False, id_type='int', **kwargs):
189
265
r = (rid [i ] + 1 ) * ncols - 1 + cid [i ]
190
266
w [i ] = w .get (i , []) + [r ]
191
267
w [r ] = w .get (r , []) + [i ]
192
- if torus :
193
- if rid [i ] == r1 and r1 not in [0 , 1 ]:
194
- below = rid [i ] + 1
195
- r = lat_pts [(below * ncols + cid [i ]) - n ]
196
- w [i ] = w .get (i , []) + [r ]
197
- w [r ] = w .get (r , []) + [i ]
198
- if cid [i ] == c1 and c1 not in [0 , 1 ]:
199
- c = lat_pts [(rid [i ] * ncols ) - n ]
200
- w [i ] = w .get (i , []) + [c ]
201
- w [c ] = w .get (c , []) + [i ]
202
- if not rook :
203
- if r1 not in [0 , 1 ]:
204
- # southeast bishop
205
- if cid [i ] < c1 and rid [i ] == r1 :
206
- r = lat_pts [((rid [i ] + 1 ) * ncols + 1 + cid [i ]) - n ]
207
- w [i ] = w .get (i , []) + [r ]
208
- w [r ] = w .get (r , []) + [i ]
209
- # southwest bishop
210
- if cid [i ] > 0 and rid [i ] == r1 :
211
- r = lat_pts [((rid [i ] + 1 ) * ncols - 1 + cid [i ]) - n ]
212
- w [i ] = w .get (i , []) + [r ]
213
- w [r ] = w .get (r , []) + [i ]
214
- if c1 not in [0 , 1 ]:
215
- if cid [i ] == c1 :
216
- r = lat_pts [i + 1 - n ]
217
- w [i ] = w .get (i , []) + [r ]
218
- w [r ] = w .get (r , []) + [i ]
219
- if cid [i ] == 0 :
220
- below = rid [i ] + 2
221
- r = lat_pts [(below * ncols - 1 ) - n ]
222
- w [i ] = w .get (i , []) + [r ]
223
- w [r ] = w .get (r , []) + [i ]
224
268
225
269
neighbors = {}
226
270
weights = {}
@@ -245,7 +289,6 @@ def lat2W(nrows=5, ncols=5, rook=True, torus=False, id_type='int', **kwargs):
245
289
return W (w , weights , ids = ids , id_order = ids [:], ** kwargs )
246
290
247
291
248
-
249
292
def block_weights (regimes , ids = None , sparse = False , ** kwargs ):
250
293
"""
251
294
Construct spatial weights for regime neighbors.
@@ -1205,22 +1248,27 @@ def lat2SW(nrows=3, ncols=5, criterion="rook", row_st=False):
1205
1248
diagonals = []
1206
1249
offsets = []
1207
1250
if criterion == "rook" or criterion == "queen" :
1208
- d = np .ones ((1 , n ))
1209
- for i in range (ncols - 1 , n , ncols ):
1210
- d [0 , i ] = 0
1211
- diagonals .append (d )
1212
- offsets .append (- 1 )
1213
-
1214
- d = np .ones ((1 , n ))
1215
- diagonals .append (d )
1216
- offsets .append (- ncols )
1251
+ if ncols > 1 :
1252
+ d = np .ones ((1 , n ))
1253
+ for i in range (ncols - 1 , n , ncols ):
1254
+ d [0 , i ] = 0
1255
+ if criterion == "queen" and ncols == 2 :
1256
+ d = np .ones ((1 , n ))
1257
+ diagonals .append (d )
1258
+ offsets .append (- 1 )
1259
+
1260
+ if ncols >= 1 :
1261
+ d = np .ones ((1 , n ))
1262
+ diagonals .append (d )
1263
+ offsets .append (- ncols )
1217
1264
1218
1265
if criterion == "queen" or criterion == "bishop" :
1219
- d = np .ones ((1 , n ))
1220
- for i in range (0 , n , ncols ):
1221
- d [0 , i ] = 0
1222
- diagonals .append (d )
1223
- offsets .append (- (ncols - 1 ))
1266
+ if (criterion == "bishop" and ncols >= 2 ) or ncols > 2 :
1267
+ d = np .ones ((1 , n ))
1268
+ for i in range (0 , n , ncols ):
1269
+ d [0 , i ] = 0
1270
+ diagonals .append (d )
1271
+ offsets .append (- (ncols - 1 ))
1224
1272
1225
1273
d = np .ones ((1 , n ))
1226
1274
for i in range (ncols - 1 , n , ncols ):
0 commit comments