diff --git a/tests/stats/test_core_stats_functions.py b/tests/stats/test_core_stats_functions.py index ec61ed5ae..e007c8a6e 100644 --- a/tests/stats/test_core_stats_functions.py +++ b/tests/stats/test_core_stats_functions.py @@ -93,7 +93,7 @@ def test_dihypergraph_view_val(diedgelist1, diedgelist2): assert H.edges([1, 2]).order._val == {1: 2, 2: 3} -def test_stats_are_views(edgelist1): +def test_hypergraph_stats_are_views(edgelist1): H = xgi.Hypergraph(edgelist1) ns = H.nodes.degree assert ns.asdict() == {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1} @@ -109,6 +109,20 @@ def test_stats_are_views(edgelist1): assert es.asdict() == {0: 2, 1: 0, 2: 1, 3: 2, 4: 2} +def test_dihypergraph_stats_are_views(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + es = H.edges.order + assert es.asdict() == {0: 2, 1: 2, 2: 3} + H.add_edge(([3, 4, 5], [6])) + assert es.asdict() == {0: 2, 1: 2, 2: 3, 3: 3} + + H = xgi.DiHypergraph(diedgelist2) + ns = H.nodes.degree + assert ns.asdict() == {0: 1, 1: 2, 2: 3, 4: 2, 3: 1, 5: 1} + H.add_node(10) + assert ns.asdict() == {0: 1, 1: 2, 2: 3, 4: 2, 3: 1, 5: 1, 10: 0} + + def test_hypergraph_user_defined(edgelist1): # Hypergraphs @@ -233,7 +247,7 @@ def test_dihypergraph_stats_items(diedgelist2): ### Numerical statistics -def test_filterby(edgelist1, edgelist8): +def test_hypergraph_filterby(edgelist1, edgelist8): H = xgi.Hypergraph(edgelist1) assert H.nodes.filterby("degree", 2).average_neighbor_degree.asdict() == {6: 1.0} assert H.nodes.filterby("average_neighbor_degree", 1.0).degree.asdict() == { @@ -252,6 +266,47 @@ def test_filterby(edgelist1, edgelist8): assert H.edges.filterby("size", 2).order.asdict() == {0: 1, 8: 1, 7: 1} +def test_dihypergraph_filterby(diedgelist1, diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + assert H.nodes.filterby("degree", 2).in_degree.asdict() == {1: 2, 4: 1} + assert H.nodes.filterby("degree", 2).out_degree.asdict() == {1: 0, 4: 2} + assert H.nodes.filterby("in_degree", 1).degree.asdict() == {0: 1, 3: 1, 4: 2} + assert H.nodes.filterby("out_degree", 1).degree.asdict() == {2: 3, 5: 1} + + assert H.edges.filterby("order", 2).size.asdict() == {0: 3, 1: 3} + assert H.edges.filterby("size", 4).order.asdict() == {2: 3} + + H = xgi.DiHypergraph(diedgelist1) + assert H.nodes.filterby("degree", 1).in_degree.asdict() == { + 1: 1, + 2: 1, + 3: 1, + 4: 0, + 5: 1, + 6: 1, + 7: 0, + 8: 0, + } + assert H.nodes.filterby("degree", 1).out_degree.asdict() == { + 1: 0, + 2: 0, + 3: 0, + 4: 1, + 5: 0, + 6: 1, + 7: 1, + 8: 1, + } + assert H.nodes.filterby("in_degree", 1).degree.asdict() == { + 1: 1, + 2: 1, + 3: 1, + 5: 1, + 6: 1, + } + assert H.nodes.filterby("out_degree", 1).degree.asdict() == {8: 1, 4: 1, 6: 1, 7: 1} + + def test_filterby_modes(edgelist1, edgelist8): H = xgi.Hypergraph(edgelist1) assert list(H.nodes.filterby("degree", 2)) == [6] @@ -274,7 +329,19 @@ def test_filterby_modes(edgelist1, edgelist8): assert set(H.nodes.filterby("degree", (2, 3), "between")) == {4, 5, 6} -def test_call_filterby(edgelist1, edgelist8): +def test_dihypergraph_filterby_modes(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + assert list(H.nodes.filterby("degree", 2)) == [1, 4] + assert list(H.nodes.filterby("degree", 2, "eq")) == [1, 4] + assert list(H.nodes.filterby("degree", 1, "neq")) == [1, 2, 4] + assert list(H.nodes.filterby("degree", 3, "geq")) == [2] + assert list(H.nodes.filterby("degree", 3, "gt")) == [] + assert list(H.nodes.filterby("degree", 0, "leq")) == [] + assert list(H.nodes.filterby("degree", 1, "lt")) == [] + assert set(H.nodes.filterby("degree", (1, 3), "between")) == set(H.nodes) + + +def test_hypergraph_call_filterby(edgelist1, edgelist8): H = xgi.Hypergraph(edgelist1) filtered = H.nodes([5, 6, 7, 8]).filterby("average_neighbor_degree", 2.0).degree @@ -299,7 +366,38 @@ def test_call_filterby(edgelist1, edgelist8): assert filtered.asdict() == {5: 3, 6: 3} -def test_filterby_with_nodestat(edgelist4, edgelist8): +def test_dihypergraph_call_filterby(diedgelist1, diedgelist2): + H = xgi.DiHypergraph(diedgelist1) + + filtered = H.nodes([4, 5, 6]).filterby("in_degree", 1).degree + assert filtered.asdict() == {5: 1, 6: 1} + + H = xgi.DiHypergraph(diedgelist2) + assert set(H.nodes([1, 2, 3]).filterby("degree", 2)) == {1} + assert H.edges([1, 2]).filterby("order", 2).size.asdict() == {1: 3} + + +def test_hypergraph_after_call_with_args(edgelist1): + H = xgi.Hypergraph(edgelist1) + degs = {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1} + assert H.nodes.degree.asdict() == degs + + # check when calling without arguments AFTER calling WITH arguments + H.nodes.degree(order=2).asdict() + assert H.nodes.degree.asdict() == degs + + +def test_dihypergraph_after_call_with_args(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + degs = {0: 1, 1: 2, 2: 3, 4: 2, 3: 1, 5: 1} + assert H.nodes.degree.asdict() == degs + + # check when calling without arguments AFTER calling WITH arguments + H.nodes.degree(order=2).asdict() + assert H.nodes.degree.asdict() == degs + + +def test_hypergraph_filterby_with_nodestat(edgelist4, edgelist8): H = xgi.Hypergraph(edgelist4) assert list(H.nodes.filterby(H.nodes.degree(order=2), 2)) == [3] @@ -307,6 +405,15 @@ def test_filterby_with_nodestat(edgelist4, edgelist8): assert list(H.nodes.filterby(H.nodes.degree(order=2), 2)) == [1, 4, 5] +def test_dihypergraph_filterby_with_nodestat(diedgelist1, diedgelist2): + H = xgi.DiHypergraph(diedgelist1) + assert list(H.edges.filterby(H.edges.order(degree=1), 3)) == [0, 1] + + H = xgi.DiHypergraph(diedgelist2) + assert list(H.edges.filterby(H.edges.order(degree=2), 1)) == [1] + assert list(H.nodes.filterby(H.nodes.degree(order=2), 1)) == [0, 4] + + def test_filterby_edgestat_with_nodestat(edgelist4, edgelist8): H = xgi.Hypergraph(edgelist4) assert list(H.edges.filterby(H.edges.order(degree=2), 2)) == [1] @@ -315,7 +422,7 @@ def test_filterby_edgestat_with_nodestat(edgelist4, edgelist8): assert list(H.edges.filterby(H.edges.order(degree=4), 1)) == [2, 3] -def test_aggregates(edgelist1, edgelist2, edgelist8): +def test_hypergraph_aggregates(edgelist1, edgelist2, edgelist8): H = xgi.Hypergraph(edgelist1) assert H.nodes.degree.max() == 2 assert H.nodes.degree.min() == 1 @@ -384,7 +491,32 @@ def test_aggregates(edgelist1, edgelist2, edgelist8): assert round(H.edges.order.var(), 3) == 0.765 -def test_moment(edgelist1, edgelist6): +def test_dihypergraph_aggregates(diedgelist1, diedgelist2): + H = xgi.DiHypergraph(diedgelist1) + assert H.edges.order.max() == 3 + assert H.edges.order.min() == 3 + assert H.edges.order.sum() == 6 + assert round(H.edges.order.mean(), 3) == 3 + assert round(H.edges.order.std(), 3) == 0 + assert round(H.edges.order.var(), 3) == 0 + + H = xgi.DiHypergraph(diedgelist2) + assert H.nodes.degree.max() == 3 + assert H.nodes.degree.min() == 1 + assert H.nodes.degree.sum() == 10 + assert round(H.nodes.degree.mean(), 3) == 1.667 + assert round(H.nodes.degree.std(), 3) == 0.745 + assert round(H.nodes.degree.var(), 3) == 0.556 + + assert H.edges.order.max() == 3 + assert H.edges.order.min() == 2 + assert H.edges.order.sum() == 7 + assert round(H.edges.order.mean(), 3) == 2.333 + assert round(H.edges.order.std(), 3) == 0.471 + assert round(H.edges.order.var(), 3) == 0.222 + + +def test_hypergraph_moment(edgelist1, edgelist6): H = xgi.Hypergraph(edgelist1) deg = H.nodes.degree assert round(deg.moment(), 3) == 1.375 @@ -402,6 +534,16 @@ def test_moment(edgelist1, edgelist6): assert round(deg.moment(3, center=True), 3) == 0.0 +def test_dihypergraph_moment(diedgelist2): + H = xgi.DiHypergraph(diedgelist2) + deg = H.nodes.degree + assert round(deg.moment(), 3) == 3.333 + assert round(deg.moment(2, center=False), 3) == 3.333 + 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 + + def test_hypergraph_single_id(edgelist1): H = xgi.Hypergraph(edgelist1) assert H.degree()[1] == 1 @@ -552,16 +694,6 @@ def test_missing_attrs(hyperwithattrs): } -def test_after_call_with_args(edgelist1): - H = xgi.Hypergraph(edgelist1) - degs = {1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 2, 7: 1, 8: 1} - assert H.nodes.degree.asdict() == degs - - # check when calling without arguments AFTER calling WITH arguments - H.nodes.degree(order=2).asdict() - assert H.nodes.degree.asdict() == degs - - ### Test multiple stats @@ -822,29 +954,3 @@ def test_multi_with_attrs(hyperwithattrs): 4: [2, "red"], 5: [2, "blue"], } - - -def test_aggregates(edgelist1, edgelist2, edgelist8): - H = xgi.Hypergraph(edgelist1) - assert H.edges.order.max() == 2 - assert H.edges.order.min() == 0 - assert H.edges.order.sum() == 5 - assert round(H.edges.order.mean(), 3) == 1.25 - assert round(H.edges.order.std(), 3) == 0.829 - assert round(H.edges.order.var(), 3) == 0.688 - - H = xgi.Hypergraph(edgelist2) - assert H.edges.order.max() == 2 - assert H.edges.order.min() == 1 - assert H.edges.order.sum() == 4 - assert round(H.edges.order.mean(), 3) == 1.333 - assert round(H.edges.order.std(), 3) == 0.471 - assert round(H.edges.order.var(), 3) == 0.222 - - H = xgi.Hypergraph(edgelist8) - assert H.edges.order.max() == 4 - assert H.edges.order.min() == 1 - assert H.edges.order.sum() == 17 - assert round(H.edges.order.mean(), 3) == 1.889 - assert round(H.edges.order.std(), 3) == 0.875 - assert round(H.edges.order.var(), 3) == 0.765 diff --git a/tests/stats/test_diedgestats.py b/tests/stats/test_diedgestats.py index 9bdf61ffe..9ea380daf 100644 --- a/tests/stats/test_diedgestats.py +++ b/tests/stats/test_diedgestats.py @@ -2,66 +2,18 @@ import xgi - -def test_filterby(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - assert H.edges.filterby("order", 2).size.asdict() == {0: 3, 1: 3} - assert H.edges.filterby("size", 4).order.asdict() == {2: 3} - - -def test_call_filterby(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - assert H.edges([1, 2]).filterby("order", 2).size.asdict() == {1: 3} - - -def test_filterby_with_nodestat(diedgelist1, diedgelist2): - H = xgi.DiHypergraph(diedgelist1) - assert list(H.edges.filterby(H.edges.order(degree=1), 3)) == [0, 1] - - H = xgi.DiHypergraph(diedgelist2) - assert list(H.edges.filterby(H.edges.order(degree=2), 1)) == [1] - - -def test_aggregates(diedgelist1, diedgelist2): - H = xgi.DiHypergraph(diedgelist1) - assert H.edges.order.max() == 3 - assert H.edges.order.min() == 3 - assert H.edges.order.sum() == 6 - assert round(H.edges.order.mean(), 3) == 3 - assert round(H.edges.order.std(), 3) == 0 - assert round(H.edges.order.var(), 3) == 0 - - H = xgi.DiHypergraph(diedgelist2) - assert H.edges.order.max() == 3 - assert H.edges.order.min() == 2 - assert H.edges.order.sum() == 7 - assert round(H.edges.order.mean(), 3) == 2.333 - assert round(H.edges.order.std(), 3) == 0.471 - assert round(H.edges.order.var(), 3) == 0.222 - - -def test_stats_are_views(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - es = H.edges.order - assert es.asdict() == {0: 2, 1: 2, 2: 3} - H.add_edge(([3, 4, 5], [6])) - assert es.asdict() == {0: 2, 1: 2, 2: 3, 3: 3} - - -### di-edge specific tests +### di-edge stat specific tests 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} @@ -71,7 +23,6 @@ def test_tail_order(diedgelist1, diedgelist2): 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} @@ -81,7 +32,6 @@ def test_tail_size(diedgelist1, diedgelist2): H = xgi.DiHypergraph(diedgelist2) 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} @@ -91,7 +41,6 @@ def test_head_order(diedgelist1, diedgelist2): 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} @@ -101,5 +50,4 @@ def test_head_size(diedgelist1, diedgelist2): H = xgi.DiHypergraph(diedgelist2) 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 f34688f9b..4a650ea9a 100644 --- a/tests/stats/test_dinodestats.py +++ b/tests/stats/test_dinodestats.py @@ -4,111 +4,7 @@ import xgi - -def test_filterby(diedgelist1, diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - assert H.nodes.filterby("degree", 2).in_degree.asdict() == {1: 2, 4: 1} - assert H.nodes.filterby("degree", 2).out_degree.asdict() == {1: 0, 4: 2} - assert H.nodes.filterby("in_degree", 1).degree.asdict() == {0: 1, 3: 1, 4: 2} - assert H.nodes.filterby("out_degree", 1).degree.asdict() == {2: 3, 5: 1} - - H = xgi.DiHypergraph(diedgelist1) - assert H.nodes.filterby("degree", 1).in_degree.asdict() == { - 1: 1, - 2: 1, - 3: 1, - 4: 0, - 5: 1, - 6: 1, - 7: 0, - 8: 0, - } - assert H.nodes.filterby("degree", 1).out_degree.asdict() == { - 1: 0, - 2: 0, - 3: 0, - 4: 1, - 5: 0, - 6: 1, - 7: 1, - 8: 1, - } - assert H.nodes.filterby("in_degree", 1).degree.asdict() == { - 1: 1, - 2: 1, - 3: 1, - 5: 1, - 6: 1, - } - assert H.nodes.filterby("out_degree", 1).degree.asdict() == {8: 1, 4: 1, 6: 1, 7: 1} - - -def test_filterby_with_nodestat(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - assert list(H.nodes.filterby(H.nodes.degree(order=2), 1)) == [0, 4] - - -def test_filterby_modes(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - assert list(H.nodes.filterby("degree", 2)) == [1, 4] - assert list(H.nodes.filterby("degree", 2, "eq")) == [1, 4] - assert list(H.nodes.filterby("degree", 1, "neq")) == [1, 2, 4] - assert list(H.nodes.filterby("degree", 3, "geq")) == [2] - assert list(H.nodes.filterby("degree", 3, "gt")) == [] - assert list(H.nodes.filterby("degree", 0, "leq")) == [] - assert list(H.nodes.filterby("degree", 1, "lt")) == [] - assert set(H.nodes.filterby("degree", (1, 3), "between")) == set(H.nodes) - - -def test_aggregates(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - assert H.nodes.degree.max() == 3 - assert H.nodes.degree.min() == 1 - assert H.nodes.degree.sum() == 10 - assert round(H.nodes.degree.mean(), 3) == 1.667 - assert round(H.nodes.degree.std(), 3) == 0.745 - assert round(H.nodes.degree.var(), 3) == 0.556 - - -def test_stats_are_views(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - ns = H.nodes.degree - assert ns.asdict() == {0: 1, 1: 2, 2: 3, 4: 2, 3: 1, 5: 1} - H.add_node(10) - assert ns.asdict() == {0: 1, 1: 2, 2: 3, 4: 2, 3: 1, 5: 1, 10: 0} - - -def test_after_call_with_args(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - degs = {0: 1, 1: 2, 2: 3, 4: 2, 3: 1, 5: 1} - assert H.nodes.degree.asdict() == degs - - # check when calling without arguments AFTER calling WITH arguments - H.nodes.degree(order=2).asdict() - assert H.nodes.degree.asdict() == degs - - -def test_moment(diedgelist2): - H = xgi.DiHypergraph(diedgelist2) - deg = H.nodes.degree - assert round(deg.moment(), 3) == 3.333 - assert round(deg.moment(2, center=False), 3) == 3.333 - 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 - - -def test_call_filterby(diedgelist1, diedgelist2): - H = xgi.DiHypergraph(diedgelist1) - - filtered = H.nodes([4, 5, 6]).filterby("in_degree", 1).degree - assert filtered.asdict() == {5: 1, 6: 1} - - H = xgi.DiHypergraph(diedgelist2) - assert set(H.nodes([1, 2, 3]).filterby("degree", 2)) == {1} - - -## di-node specific tests +## di-node stat specific tests def test_degree(diedgelist2): @@ -117,7 +13,6 @@ def test_degree(diedgelist2): assert H.degree() == degs assert H.degree(order=2) == {0: 1, 1: 2, 2: 2, 4: 1, 3: 0, 5: 0} assert H.nodes.degree.asdict() == degs - assert H.nodes.degree.aslist() == list(degs.values()) def test_in_degree(diedgelist2): @@ -126,7 +21,6 @@ def test_in_degree(diedgelist2): assert H.in_degree() == degs assert H.in_degree(order=2) == {0: 1, 1: 2, 2: 1, 4: 0, 3: 0, 5: 0} assert H.nodes.in_degree.asdict() == degs - assert H.nodes.in_degree.aslist() == list(degs.values()) def test_out_degree(diedgelist2): @@ -135,4 +29,3 @@ def test_out_degree(diedgelist2): assert H.out_degree() == degs assert H.out_degree(order=2) == {0: 0, 1: 0, 2: 1, 4: 1, 3: 0, 5: 0} assert H.nodes.out_degree.asdict() == degs - assert H.nodes.out_degree.aslist() == list(degs.values()) diff --git a/tests/stats/test_edgestats.py b/tests/stats/test_edgestats.py index 31c0c8b18..27941f8e0 100644 --- a/tests/stats/test_edgestats.py +++ b/tests/stats/test_edgestats.py @@ -2,3 +2,43 @@ import pytest import xgi + +### edge stat specific tests + + +def test_size(edgelist1, edgelist8): + H = xgi.Hypergraph(edgelist1) + sizes = {0: 3, 1: 1, 2: 2, 3: 3} + assert H.size() == sizes + assert H.size(degree=2) == {0: 0, 1: 0, 2: 1, 3: 1} + assert H.edges.size.asdict() == sizes + + H = xgi.Hypergraph(edgelist8) + sizes = {0: 2, 1: 3, 2: 3, 3: 5, 4: 3, 5: 3, 6: 3, 7: 2, 8: 2} + assert H.size() == sizes + assert H.size(degree=2) == {0: 0, 1: 0, 2: 0, 3: 0, 4: 1, 5: 1, 6: 0, 7: 1, 8: 1} + assert H.edges.size.asdict() == sizes + + +def test_order(edgelist1, edgelist8): + H = xgi.Hypergraph(edgelist1) + orders = {0: 2, 1: 0, 2: 1, 3: 2} + assert H.order() == orders + assert H.order(degree=2) == {0: -1, 1: -1, 2: 0, 3: 0} + assert H.edges.order().asdict() == orders + + H = xgi.Hypergraph(edgelist8) + orders = {0: 1, 1: 2, 2: 2, 3: 4, 4: 2, 5: 2, 6: 2, 7: 1, 8: 1} + assert H.order() == orders + assert H.order(degree=2) == { + 0: -1, + 1: -1, + 2: -1, + 3: -1, + 4: 0, + 5: 0, + 6: -1, + 7: 0, + 8: 0, + } + assert H.edges.order().asdict() == orders diff --git a/tests/stats/test_nodestats.py b/tests/stats/test_nodestats.py index 6ae089862..9194df127 100644 --- a/tests/stats/test_nodestats.py +++ b/tests/stats/test_nodestats.py @@ -4,6 +4,8 @@ import xgi +### node stat specific tests + def test_degree(edgelist1, edgelist8): H = xgi.Hypergraph(edgelist1) @@ -11,14 +13,12 @@ def test_degree(edgelist1, edgelist8): assert H.degree() == degs assert H.degree(order=2) == {1: 1, 2: 1, 3: 1, 4: 0, 5: 0, 6: 1, 7: 1, 8: 1} assert H.nodes.degree.asdict() == degs - assert H.nodes.degree.aslist() == list(degs.values()) H = xgi.Hypergraph(edgelist8) degs = {0: 6, 1: 5, 2: 4, 3: 4, 4: 3, 5: 2, 6: 2} assert H.degree() == degs assert H.degree(order=2) == {0: 3, 1: 2, 2: 3, 3: 3, 4: 2, 5: 2, 6: 0} assert H.nodes.degree.asdict() == degs - assert H.nodes.degree.aslist() == list(degs.values()) def test_average_neighbor_degree(edgelist1, edgelist8): @@ -26,13 +26,11 @@ def test_average_neighbor_degree(edgelist1, edgelist8): vals = {1: 1.0, 2: 1.0, 3: 1.0, 4: 0, 5: 2.0, 6: 1.0, 7: 1.5, 8: 1.5} assert H.average_neighbor_degree() == vals assert H.nodes.average_neighbor_degree().asdict() == vals - assert H.nodes.average_neighbor_degree().aslist() == list(vals.values()) H = xgi.Hypergraph(edgelist8) vals = {0: 3.6, 1: 3.5, 2: 4.0, 3: 4.0, 4: 4.2, 5: 4.0, 6: 5.5} assert H.average_neighbor_degree() == vals assert H.nodes.average_neighbor_degree().asdict() == vals - assert H.nodes.average_neighbor_degree().aslist() == list(vals.values()) def test_clustering_coefficient(): @@ -40,20 +38,18 @@ def test_clustering_coefficient(): H = xgi.Hypergraph() assert H.clustering_coefficient() == dict() - assert H.nodes.clustering_coefficient().aslist() == [] assert H.nodes.clustering_coefficient().asdict() == dict() # no edges H.add_nodes_from(range(3)) - assert H.nodes.clustering_coefficient().aslist() == [0, 0, 0] assert H.nodes.clustering_coefficient().asdict() == {0: 0, 1: 0, 2: 0} # edges edges = [[1, 2, 3], [2, 3, 4, 5], [3, 4, 5]] H = xgi.Hypergraph(edges) - assert np.allclose( - H.nodes.clustering_coefficient.aslist(), np.array([1, 2 / 3, 2 / 3, 1, 1]) - ) + val = H.nodes.clustering_coefficient.asdict() + true_val = {1: 1, 2: 2 / 3, 3: 2 / 3, 4: 1, 5: 1} + assert val == true_val def test_local_clustering_coefficient(): @@ -61,20 +57,18 @@ def test_local_clustering_coefficient(): H = xgi.Hypergraph() assert H.local_clustering_coefficient() == dict() - assert H.nodes.local_clustering_coefficient().aslist() == [] assert H.nodes.local_clustering_coefficient().asdict() == dict() # no edges H.add_nodes_from(range(3)) - assert H.nodes.local_clustering_coefficient().aslist() == [0, 0, 0] assert H.nodes.local_clustering_coefficient().asdict() == {0: 0, 1: 0, 2: 0} # edges edges = [[1, 2, 3], [2, 3, 4, 5], [3, 4, 5]] H = xgi.Hypergraph(edges) - assert np.allclose( - H.nodes.local_clustering_coefficient.aslist(), np.array([0, 0, 0.25, 0, 0]) - ) + val = H.nodes.local_clustering_coefficient.asdict() + true_val = {1: 0, 2: 0, 3: 0.25, 4: 0, 5: 0} + assert val == true_val def test_two_node_clustering_coefficient(): @@ -82,29 +76,24 @@ def test_two_node_clustering_coefficient(): H = xgi.Hypergraph() assert H.two_node_clustering_coefficient() == dict() - assert H.nodes.two_node_clustering_coefficient().aslist() == [] assert H.nodes.two_node_clustering_coefficient().asdict() == dict() # no edges H.add_nodes_from(range(3)) - assert H.nodes.two_node_clustering_coefficient().aslist() == [0, 0, 0] assert H.nodes.two_node_clustering_coefficient().asdict() == {0: 0, 1: 0, 2: 0} # edges edges = [[1, 2, 3], [2, 3, 4, 5], [3, 4, 5]] H = xgi.Hypergraph(edges) - assert np.allclose( - H.nodes.two_node_clustering_coefficient(kind="union").aslist(), - np.array( - [ - 0.41666666666666663, - 0.45833333333333326, - 0.5833333333333333, - 0.6666666666666666, - 0.6666666666666666, - ] - ), - ) + val = H.nodes.two_node_clustering_coefficient(kind="union").asdict() + true_val = { + 1: 0.41666666666666663, + 2: 0.45833333333333326, + 3: 0.5833333333333333, + 4: 0.6666666666666666, + 5: 0.6666666666666666, + } + assert val == true_val def test_attrs(hyperwithattrs, attr1, attr2, attr3, attr4, attr5): @@ -117,7 +106,6 @@ def test_attrs(hyperwithattrs, attr1, attr2, attr3, attr4, attr5): 5: attr5, } assert H.nodes.attrs.asdict() == attrs - assert H.nodes.attrs.aslist() == list(attrs.values()) assert H.nodes.attrs("color").asdict() == {n: H._node_attr[n]["color"] for n in H} filtered = H.nodes.filterby_attr("color", "blue").attrs diff --git a/xgi/stats/edgestats.py b/xgi/stats/edgestats.py index 9b034d63e..68e8ca392 100644 --- a/xgi/stats/edgestats.py +++ b/xgi/stats/edgestats.py @@ -177,8 +177,7 @@ def size(net, bunch, degree=None): return {e: len(net._edge[e]) for e in bunch} else: return { - e: len(n for n in net._edge[e] if len(net._node[n]) == degree) - for e in bunch + e: sum(len(net._node[n]) == degree for n in net._edge[e]) for e in bunch }