From 5f6820d7296f50fd942e9d873779c5a852226724 Mon Sep 17 00:00:00 2001 From: Nicholas Landry Date: Thu, 1 Jun 2023 15:03:10 -0400 Subject: [PATCH] tests for stats --- tests/conftest.py | 23 ++++++++++++ tests/stats/test_diedgestats.py | 62 ++++++++++++++++++++++++++++++--- tests/stats/test_dinodestats.py | 47 +++++++++++++++++++++++++ xgi/stats/diedgestats.py | 4 +-- 4 files changed, 130 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 5247f7bc1..4a7088ebe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -135,6 +135,11 @@ def bipartite_graph4(): return G +@pytest.fixture +def attr0(): + return {"color": "brown", "name": "camel"} + + @pytest.fixture def attr1(): return {"color": "red", "name": "horse"} @@ -227,3 +232,21 @@ def diedgelist2(): @pytest.fixture def diedgedict1(): return {0: ({1, 2, 3}, {4}), 1: ({5, 6}, {6, 7, 8})} + + +@pytest.fixture +def dihyperwithattrs(diedgelist2, attr0, attr1, attr2, attr3, attr4, attr5): + H = xgi.DiHypergraph() + H.add_nodes_from( + [ + (0, attr0), + (1, attr1), + (2, attr2), + (3, attr3), + (4, attr4), + (5, attr5), + ] + ) + H.add_edges_from(diedgelist2) + xgi.set_edge_attributes(H, {0: attr3, 1: attr4, 2: attr5}) + return H diff --git a/tests/stats/test_diedgestats.py b/tests/stats/test_diedgestats.py index 690de50c6..65397e3d8 100644 --- a/tests/stats/test_diedgestats.py +++ b/tests/stats/test_diedgestats.py @@ -108,17 +108,71 @@ def test_view_val(diedgelist1, diedgelist2): assert H.edges([1, 2]).order._val == {1: 2, 2: 3} +# test the pre-defined edgestat methods +def test_attrs(dihyperwithattrs): + c = dihyperwithattrs.edges.attrs("color") + assert c.asdict() == {0: "yellow", 1: "red", 2: "blue"} + c = dihyperwithattrs.edges.attrs("age", missing=100) + assert c.asdict() == {0: 100, 1: 20, 2: 2} + c = dihyperwithattrs.edges.attrs() + assert c.asdict() == { + 0: {"color": "yellow", "name": "zebra"}, + 1: {"color": "red", "name": "orangutan", "age": 20}, + 2: {"color": "blue", "name": "fish", "age": 2}, + } + with pytest.raises(ValueError): + dihyperwithattrs.edges.attrs(attr=100).asdict() + + +def test_order(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + assert H.edges.order.asdict() == {0: 2, 1: 2, 2: 3} + + assert H.edges.order(degree=1).asdict() == {0: 0, 1: -1, 2: 1} + + +def test_size(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + assert H.edges.size.asdict() == {0: 3, 1: 3, 2: 4} + + assert H.edges.size(degree=1).asdict() == {0: 1, 1: 0, 2: 2} + + +def test_tail_order(diedgelist1, diedgelist2): + H = xgi.DiHypergraph(diedgelist1) + assert H.edges.tail_order.asdict() == {0: 2, 1: 1} + + H = xgi.DiHypergraph(diedgelist2) + assert H.edges.tail_order.asdict() == {0: 1, 1: 1, 2: 2} + + assert H.edges.tail_order(degree=1).asdict() == {0: 0, 1: -1, 2: 0} + + def test_tail_size(diedgelist1, diedgelist2): H = xgi.DiHypergraph(diedgelist1) - H.edges.tail_size.asdict() == {0: 3, 1: 2} + assert H.edges.tail_size.asdict() == {0: 3, 1: 2} H = xgi.DiHypergraph(diedgelist2) - H.edges.tail_size.asdict() == {0: 2, 1: 2, 2: 3} + assert H.edges.tail_size.asdict() == {0: 2, 1: 2, 2: 3} + + assert H.edges.tail_size(degree=1).asdict() == {0: 1, 1: 0, 2: 1} + + +def test_head_order(diedgelist1, diedgelist2): + H = xgi.DiHypergraph(diedgelist1) + assert H.edges.head_order.asdict() == {0: 0, 1: 2} + + H = xgi.DiHypergraph(diedgelist2) + assert H.edges.head_order.asdict() == {0: 0, 1: 0, 2: 1} + + assert H.edges.head_order(degree=1).asdict() == {0: -1, 1: -1, 2: 0} def test_head_size(diedgelist1, diedgelist2): H = xgi.DiHypergraph(diedgelist1) - H.edges.head_size.asdict() == {0: 1, 1: 3} + assert H.edges.head_size.asdict() == {0: 1, 1: 3} H = xgi.DiHypergraph(diedgelist2) - H.edges.head_size.asdict() == {0: 1, 1: 1, 2: 2} + assert H.edges.head_size.asdict() == {0: 1, 1: 1, 2: 2} + + assert H.edges.head_size(degree=1).asdict() == {0: 0, 1: 0, 2: 1} diff --git a/tests/stats/test_dinodestats.py b/tests/stats/test_dinodestats.py index a58469a95..4d231196d 100644 --- a/tests/stats/test_dinodestats.py +++ b/tests/stats/test_dinodestats.py @@ -194,3 +194,50 @@ def test_moment(diedgelist2): assert round(deg.moment(2, center=True), 3) == 0.556 assert round(deg.moment(3, center=False), 3) == 7.667 assert round(deg.moment(3, center=True), 3) == 0.259 + + +# test pre-defined functions +def test_attrs(dihyperwithattrs): + c = dihyperwithattrs.nodes.attrs("color") + assert c.asdict() == { + 0: "brown", + 1: "red", + 2: "blue", + 3: "yellow", + 4: "red", + 5: "blue", + } + c = dihyperwithattrs.nodes.attrs("age", missing=100) + assert c.asdict() == {0: 100, 1: 100, 2: 100, 3: 100, 4: 20, 5: 2} + c = dihyperwithattrs.nodes.attrs() + assert c.asdict() == { + 0: {"color": "brown", "name": "camel"}, + 1: {"color": "red", "name": "horse"}, + 2: {"color": "blue", "name": "pony"}, + 3: {"color": "yellow", "name": "zebra"}, + 4: {"color": "red", "name": "orangutan", "age": 20}, + 5: {"color": "blue", "name": "fish", "age": 2}, + } + with pytest.raises(ValueError): + dihyperwithattrs.nodes.attrs(attr=100).asdict() + + +def test_degree(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + + assert H.nodes.degree.asdict() == {0: 1, 1: 2, 2: 3, 3: 1, 4: 2, 5: 1} + assert H.nodes.degree(order=2).asdict() == {0: 1, 1: 2, 2: 2, 3: 0, 4: 1, 5: 0} + + +def test_in_degree(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + + assert H.nodes.in_degree.asdict() == {0: 1, 1: 2, 2: 2, 3: 1, 4: 1, 5: 0} + assert H.nodes.in_degree(order=2).asdict() == {0: 1, 1: 2, 2: 1, 3: 0, 4: 0, 5: 0} + + +def test_out_degree(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + + assert H.nodes.out_degree.asdict() == {0: 0, 1: 0, 2: 1, 3: 0, 4: 2, 5: 1} + assert H.nodes.out_degree(order=2).asdict() == {0: 0, 1: 0, 2: 1, 3: 0, 4: 1, 5: 0} diff --git a/xgi/stats/diedgestats.py b/xgi/stats/diedgestats.py index 4dd2afb1c..024f39022 100644 --- a/xgi/stats/diedgestats.py +++ b/xgi/stats/diedgestats.py @@ -179,8 +179,8 @@ def size(net, bunch, degree=None): return {e: len(net._edge_in[e].union(net._edge_out[e])) for e in bunch} else: return { - e: len( - n + e: sum( + 1 for n in net._edge_in[e].union(net._edge_out[e]) if len(net._node_in[n].union(net._node_out[n])) == degree )