brainconn.utils.matrix.threshold_proportional¶
-
threshold_proportional
(W, p, copy=True)[source]¶ This function “thresholds” the connectivity matrix by preserving a proportion p (0<p<1) of the strongest weights. All other weights, and all weights on the main diagonal (self-self connections) are set to 0.
If copy is not set, this function will modify W in place.
Parameters: - W (
numpy.ndarray
) – weighted connectivity matrix - p (float) – proportional weight threshold (0<p<1)
- copy (bool) – if True, returns a copy of the matrix. Otherwise, modifies the matrix in place. Default value=True.
Returns: W – thresholded connectivity matrix
Return type: Notes
The proportion of elements set to 0 is a fraction of all elements in the matrix, whether or not they are already 0. That is, this function has the following behavior:
>> x = np.random.random((10,10)) >> x_25 = threshold_proportional(x, .25) >> np.size(np.where(x_25)) #note this double counts each nonzero element 46 >> x_125 = threshold_proportional(x, .125) >> np.size(np.where(x_125)) 22 >> x_test = threshold_proportional(x_25, .5) >> np.size(np.where(x_test)) 46
That is, the 50% thresholding of x_25 does nothing because >=50% of the elements in x_25 are aleady <=0. This behavior is the same as in BCT. Be careful with matrices that are both signed and sparse.
- W (