pysal.lib.weights.
w_clip
(w1, w2, outSP=True, silence_warnings=False)[source]¶Clip a continuous W object (w1) with a different W object (w2) so only cells where w2 has a nonzero value remain with nonzero values in w1.
Checks on w1 and w2 are performed to make sure they conform to the appropriate format and, if not, they are converted.
Parameters: 


Returns: 

Examples
>>> from pysal.lib.weights import lat2W
First create a W object from a lattice using queen contiguity and rowstandardize it (note that these weights will stay when we clip the object, but they will not neccesarily represent a rowstandardization anymore):
>>> w1 = lat2W(3, 2, rook=False)
>>> w1.transform = 'R'
We will clip that geography assuming observations 0, 2, 3 and 4 belong to one group and 1, 5 belong to another group and we don’t want both groups to interact with each other in our weights (i.e. w_ij = 0 if i and j in different groups). For that, we use the following method:
>>> import pysal.lib
>>> w2 = pysal.lib.weights.util.block_weights(['r1', 'r2', 'r1', 'r1', 'r1', 'r2'])
To illustrate that w2 will only be considered as binary even when the object passed is not, we can rowstandardize it
>>> w2.transform = 'R'
The clipped object wc
will contain only the spatial queen
relationships that occur within one group (‘r1’ or ‘r2’) but will have
gotten rid of those that happen across groups
>>> wcs = pysal.lib.weights.set_operations.w_clip(w1, w2, outSP=True)
This will create a sparse object (recommended when n is large).
>>> wcs.sparse.toarray()
array([[0. , 0. , 0.33333333, 0.33333333, 0. ,
0. ],
[0. , 0. , 0. , 0. , 0. ,
0. ],
[0.2 , 0. , 0. , 0.2 , 0.2 ,
0. ],
[0.2 , 0. , 0.2 , 0. , 0.2 ,
0. ],
[0. , 0. , 0.33333333, 0.33333333, 0. ,
0. ],
[0. , 0. , 0. , 0. , 0. ,
0. ]])
If we wanted an original W object, we can control that with the argument
outSP
:
>>> wc = pysal.lib.weights.set_operations.w_clip(w1, w2, outSP=False)
WARNING: there are 2 disconnected observations Island ids: [1, 5] >>> wc.full()[0] array([[0. , 0. , 0.33333333, 0.33333333, 0. ,
 ],
 [0. , 0. , 0. , 0. , 0. ,
 ],
 [0.2 , 0. , 0. , 0.2 , 0.2 ,
 ],
 [0.2 , 0. , 0.2 , 0. , 0.2 ,
 ],
 [0. , 0. , 0.33333333, 0.33333333, 0. ,
 ],
 [0. , 0. , 0. , 0. , 0. ,
 ]])
You can check they are actually the same:
>>> wcs.sparse.toarray() == wc.full()[0]
array([[ True, True, True, True, True, True],
[ True, True, True, True, True, True],
[ True, True, True, True, True, True],
[ True, True, True, True, True, True],
[ True, True, True, True, True, True],
[ True, True, True, True, True, True]])