@staticmethod 
    def modularityMatrix(W):
        if not scipy.sparse.isspmatrix_csr(W):
            raise ValueError("W is not a csr matrix")        
        
        d = numpy.array(W.sum(0)).ravel()
        m = W.getnnz()/2 
        
        B = W - numpy.outer(d, d)/(2*m)
        
        return B
Boiler-plate test set-up
dataset/ETHPy150Open charanpald/APGL/apgl/graph/GraphUtils.py/GraphUtils.modularityMatrix--crossover with--dataset/ETHPy150Open projectatomic/atomic-reactor/tests/plugins/test_import_image.py/prepare