votes up 2

r and c must be positive

Package:
scipy
github stars 8546
Exception Class:
ValueError

Raise code

def count_blocks(A,blocksize):
    """For a given blocksize=(r,c) count the number of occupied
    blocks in a sparse matrix A
    """
    r,c = blocksize
    if r < 1 or c < 1:
        raise ValueError('r and c must be positive')

    if isspmatrix_csr(A):
        M,N = A.shape
        return csr_count_blocks(M,N,r,c,A.indptr,A.indices)
    elif isspmatrix_csc(A):
        return count_blocks(A.T,(c,r))
    else:

Ways to fix

votes up 2 votes down

the variable r and c, which specify the blocksize argument should be positive integers.

Here is how this exception happens.

from numpy import array, kron, diag
from scipy.sparse import csc_matrix
from scipy.sparse import spfuncs
X = kron([[1,1,0],[0,0,1],[1,0,1]],[[1,1]])
Y = csc_matrix(X)
spfuncs.count_blocks(X, (-1, -2))

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-28-bb35aca7c841> in <module>()  4 X = kron([[1,1,0],[0,0,1],[1,0,1]],[[1,1]])  5 Y = csc_matrix(X) ----> 6 spfuncs.count_blocks(X, (-1, -2)) 
/usr/local/lib/python3.7/dist-packages/scipy/sparse/spfuncs.py in count_blocks(A, blocksize)  90 r,c = blocksize  91 if r < 1 or c < 1: ---> 92 raise ValueError('r and c must be positive')  93   94 if isspmatrix_csr(A): 
ValueError: r and c must be positive

Fixed version of the code:

from numpy import array, kron, diag
from scipy.sparse import csc_matrix
from scipy.sparse import spfuncs
X = kron([[1,1,0],[0,0,1],[1,0,1]],[[1,1]])
Y = csc_matrix(X)
spfuncs.count_blocks(X, (1, 2))
Dec 17, 2021 kellemnegasi answer
kellemnegasi 22.6k

Add a possible fix

Please authorize to post fix