Skip to content

Commit

Permalink
tests for stats
Browse files Browse the repository at this point in the history
  • Loading branch information
nwlandry committed Jun 1, 2023
1 parent 99a4c45 commit 5f6820d
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 6 deletions.
23 changes: 23 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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
62 changes: 58 additions & 4 deletions tests/stats/test_diedgestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
47 changes: 47 additions & 0 deletions tests/stats/test_dinodestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
4 changes: 2 additions & 2 deletions xgi/stats/diedgestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 5f6820d

Please sign in to comment.