question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug in scipy.sparse.csgraph.min_weight_full_bipartite_matching

See original GitHub issue

min_weight_full_bipartite_matching gets stuck when running, python is not responding to SIGINT, needed to use cltr+\ to send SIGTERM.

To reproduce:

import pickle
from scipy.sparse.csgraph import min_weight_full_bipartite_matching
t = ... # see comment bellow
min_weight_full_bipartite_matching(t)

The matrix is a coo matrix, which is representing the dense matrix below, all 0s are implicit (no 0 values in coo matrix):

[[0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001      0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.001     ]
 [0.         0.11687445 0.         0.         0.01319788 0.07509257 0.         0.         0.         0.74228317 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.81087935 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.8408466  0.         0.         0.         0.         0.01194389 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.82994211 0.         0.         0.         0.11468516 0.         0.         0.         0.11173505 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.18796507 0.         0.04002318 0.         0.         0.         0.         0.         0.75883335 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.71545464 0.         0.         0.         0.         0.         0.02748488 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.78470564 0.         0.         0.         0.         0.         0.         0.         0.14829198 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.10870609 0.         0.         0.         0.8918677  0.         0.         0.         0.06306644 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.63844085 0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.7442354  0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.09850549 0.         0.         0.18638258 0.2769244  0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.         0.         0.         0.         0.         0.         0.         0.73182464 0.         0.         0.46443561 0.38589284 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]
 [0.29510278 0.         0.         0.         0.         0.         0.         0.         0.09666032 0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.         0.        ]]

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rossbarcommented, Nov 7, 2022

From the documentation of scipy.sparse.csgraph, it expects a biadjacency_matrix

Thanks @Code-Gratefully , you are right: since the matrix was square I misinterpreted it as an adjacency matrix rather than a biadjacency matrix (which, as you point out, the csgraph.min_weight_full_bipartite_matching docs explicitly warn against) - my mistake!

If we instead interpret the input matrix correctly as a biadjacency matrix, then we hit a different exception in NetworkX:

>>> import networkx as nx
>>> import scipy as sp
>>> a = <copied from above>
>>> m = sp.sparse.csr_matrix(a)  # nx.bipartite.from_biadjacency_matrix expects a scipy.sparse array
>>> G = nx.bipartite.from_biadjacency_matrix(m)
>>> G.number_of_nodes()  # Expect 44 (sum(a.shape))
44
>>> top_nodes = list(range(a.shape[0]))  # Be explicit about node sets
>>> nx.bipartite.minimum_weight_full_matching(G, top_nodes=top_nodes)
Traceback (most recent call last)
   ...
ValueError: cost matrix is infeasible

Note that the NetworkX version just calls scipy.optimize.linear_sum_assignment on the dense biadjacency_matrix (with inf representing non-edges rather than 0), which is where the exception originates. Full traceback included below

Traceback
nx.bipartite.minimum_weight_full_matching(G, top_nodes=list(range(a.shape[0])))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [104], line 1
----> 1 nx.bipartite.minimum_weight_full_matching(G, top_nodes=list(range(a.shape[0])))

File ~/repos/networkx/networkx/algorithms/bipartite/matching.py:574, in minimum_weight_full_matching(G, top_nodes, weight) 572 weights = np.full(weights_sparse.shape, np.inf) 573 weights[weights_sparse.row, weights_sparse.col] = weights_sparse.data –> 574 left_matches = sp.optimize.linear_sum_assignment(weights) 575 d = {U[u]: V[v] for u, v in zip(*left_matches)} 576 # d will contain the matching from edges in left to right; we need to 577 # add the ones from right to left as well.

ValueError: cost matrix is infeasible

The next step would be to chase down why specifically the input is infeasible, then see if that’s something that csgraph.min_weight_full_bipartite_matching can check for to raise an exception rather than hang.

1reaction
Code-Gratefullycommented, Nov 2, 2022

Sure, how about this plain code:

import numpy as np

a = np.array([[0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.001     , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.001     ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.001     , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.001     , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.001     , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.001     , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.001     ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.001     , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.001     ],
        [0.        , 0.11687445, 0.        , 0.        , 0.01319788,
         0.07509257, 0.        , 0.        , 0.        , 0.74228317,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.81087935, 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.8408466 ,
         0.        , 0.        , 0.        , 0.        , 0.01194389,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.82994211, 0.        , 0.        , 0.        ,
         0.11468516, 0.        , 0.        , 0.        , 0.11173505,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.18796507, 0.        , 0.04002318, 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.75883335, 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.71545464, 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.02748488, 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.78470564, 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.14829198, 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.10870609, 0.        , 0.        , 0.        ,
         0.8918677 , 0.        , 0.        , 0.        , 0.06306644,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.63844085, 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.7442354 , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.09850549, 0.        , 0.        ,
         0.18638258, 0.2769244 , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.73182464, 0.        , 0.        ,
         0.46443561, 0.38589284, 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ],
        [0.29510278, 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.09666032, 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        , 0.        , 0.        , 0.        ,
         0.        , 0.        ]])

from scipy.sparse.csgraph import min_weight_full_bipartite_matching
from scipy.sparse.coo import coo_matrix
min_weight_full_bipartite_matching(coo_matrix(a))
Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.sparse.csgraph.min_weight_full_bipartite_matching
Returns the minimum weight full matching of a bipartite graph. New in version 1.6.0. Parameters. biadjacency_matrixsparse matrix. Biadjacency matrix of the ...
Read more >
BUG: Incorrect residual graph in scipy.sparse.csgraph ...
Describe your issue. Consider the reproducing example below. This seems to be the incorrect residual graph from the flow. 1-2 has a 5...
Read more >
what is wrong with importing modules in scipy , is it a bug?
This is an artifact of Python's importing, not of SciPy. Do from scipy import sparse [as sp]. or import scipy.sparse [as sp].
Read more >
scipy.sparse.csgraph.construct_dist_matrix
Parameters: grapharray_like or sparse. The N x N matrix representation of a directed or undirected graph. If dense, then non-edges are indicated by...
Read more >
Compressed sparse graph routines ... - CuPy
Compressed sparse graph routines ( cupyx.scipy.sparse.csgraph )#. Note. The csgraph module uses pylibcugraph as a backend. You need to install pylibcugraph ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found