From 0ce1e079344a79717c60394c2a1932436e45936b Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Thu, 11 Feb 2021 18:34:23 +1100 Subject: [PATCH 1/2] Fix gen_clustree --- constclust/clustree.py | 4 ++-- tests/test_clustree.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_clustree.py diff --git a/constclust/clustree.py b/constclust/clustree.py index 99f9309..e8c338c 100644 --- a/constclust/clustree.py +++ b/constclust/clustree.py @@ -74,8 +74,8 @@ def gen_clustree(cluster_df): current_nodes = grouped[level] next_nodes = grouped[level + 1] for current_node, next_node in product(current_nodes, next_nodes): - current_contents = g.node[current_node]["contents"] - next_contents = g.node[next_node]["contents"] + current_contents = g.nodes[current_node]["contents"] + next_contents = g.nodes[next_node]["contents"] intersect = np.intersect1d( current_contents, next_contents, assume_unique=True ) diff --git a/tests/test_clustree.py b/tests/test_clustree.py new file mode 100644 index 0000000..6a034d0 --- /dev/null +++ b/tests/test_clustree.py @@ -0,0 +1,39 @@ +import numpy as np +import pandas as pd +import networkx as nx + +from constclust.clustree import gen_clustree + +import pytest + + +@pytest.mark.parametrize( + "clusterings,expected", + [ + ( + pd.DataFrame( + { + "a": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "b": [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], + "c": [0, 0, 0, 1, 1, 2, 2, 2, 2, 2], + } + ), + nx.DiGraph({0: [1, 2], 1: [3, 4], 2: [6]}), + ), + ( + pd.DataFrame( + { + "a": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + "b": [1, 1, 1, 1, 1, 0, 0, 0, 0, 0], + "c": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + } + ), + nx.DiGraph({0: [1, 2], 1: [3], 2: [3]}), + ), + ], + ids=["tree", "diamond"], +) +def test_clustree_generation_isomorphic(clusterings, expected): + result = gen_clustree(clusterings) + + assert nx.is_isomorphic(expected, result) From 3db3e3a0d5b53a4b2c97eaad5262b6d471dafef5 Mon Sep 17 00:00:00 2001 From: Isaac Virshup Date: Thu, 11 Feb 2021 18:49:01 +1100 Subject: [PATCH 2/2] Add weights to test --- tests/test_clustree.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/test_clustree.py b/tests/test_clustree.py index 6a034d0..fd65e07 100644 --- a/tests/test_clustree.py +++ b/tests/test_clustree.py @@ -18,7 +18,13 @@ "c": [0, 0, 0, 1, 1, 2, 2, 2, 2, 2], } ), - nx.DiGraph({0: [1, 2], 1: [3, 4], 2: [6]}), + nx.DiGraph( + { + 0: {1: {"weight": 0.5}, 2: {"weight": 0.5}}, + 1: {3: {"weight": 0.6}, 4: {"weight": 0.4}}, + 2: {6: {"weight": 1.0}}, + } + ), ), ( pd.DataFrame( @@ -28,12 +34,22 @@ "c": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], } ), - nx.DiGraph({0: [1, 2], 1: [3], 2: [3]}), + nx.DiGraph( + { + 0: {1: {"weight": 0.5}, 2: {"weight": 0.5}}, + 1: {3: {"weight": 0.5}}, + 2: {3: {"weight": 0.5}}, + } + ), ), ], ids=["tree", "diamond"], ) -def test_clustree_generation_isomorphic(clusterings, expected): +def test_clustree_weights(clusterings, expected): + import networkx.algorithms.isomorphism as iso + result = gen_clustree(clusterings) - assert nx.is_isomorphic(expected, result) + assert nx.is_isomorphic( + expected, result, edge_match=iso.numerical_edge_match("weight", default=1) + )