igraph / python-igraph

Python interface for igraph
GNU General Public License v2.0
1.28k stars 247 forks source link

Failed to use `scipy.sparse._csr.csr_matrix` to init a graph. #771

Open Vimos opened 3 months ago

Vimos commented 3 months ago

Describe the bug A clear and concise description of what the bug is.

Failed to use scipy.sparse._csr.csr_matrix to init a graph.

Traceback (most recent call last):
  File "/home/vimos/diffusion_for_train.py", line 78, in <module>
    x, y = diffusion_inverse_pairs(adj, seeds, args.diffusion_model)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vimos/diffusion_for_train.py", line 18, in diffusion_inverse_pairs
    G = Graph.Adjacency(adj_matrix)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vimos/anaconda3/lib/python3.11/site-packages/igraph/io/adjacency.py", line 54, in _construct_graph_from_adjacency
    return _graph_from_sparse_matrix(cls, matrix, mode=mode, loops=loops)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vimos/anaconda3/lib/python3.11/site-packages/igraph/sparse_matrix.py", line 104, in _graph_from_sparse_matrix
    edges.extend([(i, j)] * n)
                 ~~~~~~~~~^~~
TypeError: can't multiply sequence by non-int of type 'numpy.float32'

To reproduce Steps or minimal example code to reproduce the problem.

If you are confident that the issue is not in the Python interface but in the C core of igraph, please add it to the main igraph repo instead.

If you are unsure, feel free to add your issue here - we will transfer it to the main igraph repo if the root cause is in the C core of igraph.

Version information Which version of python-igraph are you using and where did you obtain it?

git+https://github.com/igraph/python-igraph@develop

ntamas commented 3 months ago

Try calling Graph.Adjacency(adj_matrix.astype(numpy.int32)) instead. Your matrix contains floating-point numbers; igraph tries to interpret the entries of the matrix as edge multiplicities, which are supposed to be integers. Alternatively, if you want a weighted matrix, use Graph.Weighted_Adjacency() instead.