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..fd65e07 --- /dev/null +++ b/tests/test_clustree.py @@ -0,0 +1,55 @@ +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: {"weight": 0.5}, 2: {"weight": 0.5}}, + 1: {3: {"weight": 0.6}, 4: {"weight": 0.4}}, + 2: {6: {"weight": 1.0}}, + } + ), + ), + ( + 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: {"weight": 0.5}, 2: {"weight": 0.5}}, + 1: {3: {"weight": 0.5}}, + 2: {3: {"weight": 0.5}}, + } + ), + ), + ], + ids=["tree", "diamond"], +) +def test_clustree_weights(clusterings, expected): + import networkx.algorithms.isomorphism as iso + + result = gen_clustree(clusterings) + + assert nx.is_isomorphic( + expected, result, edge_match=iso.numerical_edge_match("weight", default=1) + )