VHRanger / CSRGraph

A tiny library for large graphs
MIT License
111 stars 17 forks source link

ValueError: inconsistent shapes on sparse matrix generating process #21

Open garyhsu29 opened 1 year ago

garyhsu29 commented 1 year ago

Thanks for the great work on nodevectors & CSR graph! I encounter an issue while using nodevectors, but it turns out it's a problem on sparse matrix generating process. Error message:

ValueError Traceback (most recent call last)
Input In [34], in <cell line: 2>()
1 g2v = ProNE()
----> 2 g2v.fit(G)

File ~/miniforge3/envs/alphaA/lib/python3.8/site-packages/nodevectors/prone.py:82, in ProNE.fit(self, graph)
78 G = cg.csrgraph(graph)
79 features_matrix = self.pre_factorization(G.mat,
80 self.n_components,
81 self.exponent)
---> 82 vectors = ProNE.chebyshev_gaussian(
83 G.mat, features_matrix, self.n_components,
84 step=self.step, mu=self.mu, theta=self.theta)
85 self.model = dict(zip(G.nodes(), vectors))

File ~/miniforge3/envs/alphaA/lib/python3.8/site-packages/nodevectors/prone.py:154, in ProNE.chebyshev_gaussian(G, a, n_components, step, mu, theta)
151 return a
152 print(G.shape)
--> 154 A = sparse.eye(nnodes) + G
155 DA = preprocessing.normalize(A, norm='l1')
156 # L is graph laplacian

File ~/miniforge3/envs/alphaA/lib/python3.8/site-packages/scipy/sparse/base.py:414, in spmatrix.add(self, other)
412 elif isspmatrix(other):
413 if other.shape != self.shape:
--> 414 raise ValueError("inconsistent shapes")
415 return self._add_sparse(other)
416 elif isdense(other):

ValueError: inconsistent shapes
garyhsu29 commented 1 year ago

I think the symmetrical problem could be solved by editing https://github.com/VHRanger/CSRGraph/blob/db3caaa58fc84de36e4645e37b268e8f656f090a/csrgraph/methods.py#L33 to sparse.csr_matrix((weights, dst, new_src), shape = (new_src.shape[0] - 1, new_src.shape[0] - 1))