From ea8a8d8498e653353dca4909f804616e2635e110 Mon Sep 17 00:00:00 2001 From: Benjamin Perret Date: Mon, 18 Sep 2023 15:42:34 +0200 Subject: [PATCH 1/2] Fix bipartite graph matching with empty graph --- higra/algo/bipartite_graph.py | 10 ++++++++++ test/python/test_algo/test_bipartite_graph.py | 7 +++++++ test/python/test_image/test_graph_image.py | 11 +++++++++++ 3 files changed, 28 insertions(+) diff --git a/higra/algo/bipartite_graph.py b/higra/algo/bipartite_graph.py index 52a0aa17..cdb90ea2 100644 --- a/higra/algo/bipartite_graph.py +++ b/higra/algo/bipartite_graph.py @@ -129,12 +129,22 @@ def bipartite_graph_matching(graph, edge_weights): targets = graph[1] num_vertices = graph[2] num_edges = len(sources) + + if len(targets) != num_edges: + raise ValueError("sources and targets must have the same length") + except Exception as e: raise TypeError("graph must be an undirected graph or a tuple (sources, targets, num_vertices)", e) + if len(edge_weights) != num_edges: + raise ValueError("edge_weights must have the same length as the number of edges of the graph") + if not check: raise ValueError("graph must be bipartite") + if num_edges == 0: + return np.array([], dtype=np.int64) + # if edge weights dtype is floating point, emit a warning if np.issubdtype(edge_weights.dtype, np.floating): print("Warning: possible loss of precision, edge weights are casted to int64 for bipartite graph matching") diff --git a/test/python/test_algo/test_bipartite_graph.py b/test/python/test_algo/test_bipartite_graph.py index 481cdd13..8425dd21 100644 --- a/test/python/test_algo/test_bipartite_graph.py +++ b/test/python/test_algo/test_bipartite_graph.py @@ -58,6 +58,13 @@ def test_is_bipartite_graph_false(self): self.assertFalse(ans) self.assertTrue(len(color) == 0) + def test_bipartite_graph_matching_empty(self): + g = hg.UndirectedGraph(6) + weights = np.array([]) + + edges = hg.bipartite_graph_matching(g, weights) + self.assertTrue(len(edges) == 0) + def test_bipartite_graph_matching(self): g = hg.UndirectedGraph(6) g.add_edges(np.array([0, 0, 1, 1, 2, 1]), np.array([3, 4, 3, 5, 5, 4])) diff --git a/test/python/test_image/test_graph_image.py b/test/python/test_image/test_graph_image.py index 6b9630cc..ac9f5429 100644 --- a/test/python/test_image/test_graph_image.py +++ b/test/python/test_image/test_graph_image.py @@ -101,6 +101,17 @@ def test_get_nd_regular_graph(self): self.assertTrue(ref_edges == res_edges) + def test_match_pixels_image_2d_empty(self): + im1 = np.asarray([[1, 0, 0, 0], + [0, 0, 0, 0]]) + im2 = np.asarray([[0, 0, 0, 1], + [0, 0, 0, 0]]) + + sources, targets = hg.match_pixels_image_2d(im1, im2, 1.3, "absolute") + + self.assertTrue(len(sources) == 0) + self.assertTrue(len(targets) == 0) + def test_match_pixels_image_2d(self): im1 = np.asarray([[1, 0, 0, 1], [0, 0, 0, 1]]) From 77a8b0ec0abdbf6e08e8ee3d35abeef2d768574e Mon Sep 17 00:00:00 2001 From: Benjamin Perret Date: Mon, 18 Sep 2023 16:18:02 +0200 Subject: [PATCH 2/2] unfreeze sklearn version --- tools/azure-pipelines-linux-gcc.yml | 2 +- tools/azure-pipelines-mac.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/azure-pipelines-linux-gcc.yml b/tools/azure-pipelines-linux-gcc.yml index e412b874..38a64682 100644 --- a/tools/azure-pipelines-linux-gcc.yml +++ b/tools/azure-pipelines-linux-gcc.yml @@ -39,7 +39,7 @@ jobs: - script: | conda config --set always_yes yes --set changeps1 no conda update -q conda - conda install tbb-devel==2019.9 scikit-learn==1.0.1 -c conda-forge + conda install tbb-devel==2019.9 scikit-learn -c conda-forge displayName: Install python packages - script: | diff --git a/tools/azure-pipelines-mac.yml b/tools/azure-pipelines-mac.yml index 13799652..5a522ed4 100644 --- a/tools/azure-pipelines-mac.yml +++ b/tools/azure-pipelines-mac.yml @@ -31,7 +31,7 @@ jobs: - script: | conda config --set always_yes yes --set changeps1 no conda update -q conda - conda install cmake tbb-devel==2019.9 scikit-learn==1.0.1 -c conda-forge + conda install cmake tbb-devel==2019.9 scikit-learn -c conda-forge displayName: Install python packages - script: |