Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup method #161

Merged
merged 14 commits into from
Sep 5, 2022
28 changes: 14 additions & 14 deletions tests/classes/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def test_is_uniform(edgelist1, edgelist6, edgelist7):
def test_edge_neighborhood(edgelist3):
H = xgi.Hypergraph(edgelist3)
assert H.nodes.neighbors(3) == {1, 2, 4}
assert xgi.edge_neighborhood(H, 3) == [[1, 2], [4]]
assert xgi.edge_neighborhood(H, 3, include_self=True) == [[1, 2, 3], [3, 4]]
assert xgi.edge_neighborhood(H, 3) == [{1, 2}, {4}]
assert xgi.edge_neighborhood(H, 3, include_self=True) == [{1, 2, 3}, {3, 4}]
with pytest.raises(IDNotFound):
xgi.edge_neighborhood(H, 7)

Expand Down Expand Up @@ -310,13 +310,13 @@ def test_convert_labels_to_integers(hypergraph1, hypergraph2):
assert H1.edges[1]["label"] == "e2"
assert H1.edges[2]["label"] == "e3"

assert H1.edges.members(0) == [0, 1]
assert H1.edges.members(1) == [0, 1, 2]
assert H1.edges.members(2) == [2]
assert H1.edges.members(0) == {0, 1}
assert H1.edges.members(1) == {0, 1, 2}
assert H1.edges.members(2) == {2}

assert H1.nodes.memberships(0) == [0, 1]
assert H1.nodes.memberships(1) == [0, 1]
assert H1.nodes.memberships(2) == [1, 2]
assert H1.nodes.memberships(0) == {0, 1}
assert H1.nodes.memberships(1) == {0, 1}
assert H1.nodes.memberships(2) == {1, 2}

assert set(H2.nodes) == {0, 1, 2}
assert set(H2.edges) == {0, 1, 2}
Expand All @@ -329,13 +329,13 @@ def test_convert_labels_to_integers(hypergraph1, hypergraph2):
assert H2.edges[1]["label"] == "e2"
assert H2.edges[2]["label"] == "e3"

assert H2.edges.members(0) == [2, 0]
assert H2.edges.members(1) == [2, 1]
assert H2.edges.members(2) == [2, 0, 1]
assert H2.edges.members(0) == {0, 2}
assert H2.edges.members(1) == {1, 2}
assert H2.edges.members(2) == {0, 1, 2}

assert H2.nodes.memberships(0) == [0, 2]
assert H2.nodes.memberships(1) == [1, 2]
assert H2.nodes.memberships(2) == [0, 1, 2]
assert H2.nodes.memberships(0) == {0, 2}
assert H2.nodes.memberships(1) == {1, 2}
assert H2.nodes.memberships(2) == {0, 1, 2}

assert H3.nodes[0]["old_ids"] == "a"
assert H3.edges[0]["old_ids"] == "e1"
Expand Down
130 changes: 63 additions & 67 deletions tests/classes/test_hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ def test_constructor(edgelist5, dict5, incidence5, dataframe5):
H_hg = xgi.Hypergraph(H_list)

assert (
list(H_list.nodes)
== list(H_dict.nodes)
== list(H_mat.nodes)
== list(H_df.nodes)
== list(H_hg.nodes)
set(H_list.nodes)
== set(H_dict.nodes)
== set(H_mat.nodes)
== set(H_df.nodes)
== set(H_hg.nodes)
)
assert (
list(H_list.edges)
== list(H_dict.edges)
== list(H_mat.edges)
== list(H_df.edges)
== list(H_hg.edges)
set(H_list.edges)
== set(H_dict.edges)
== set(H_mat.edges)
== set(H_df.edges)
== set(H_hg.edges)
)
assert (
list(H_list.edges.members(0))
== list(H_dict.edges.members(0))
== list(H_mat.edges.members(0))
== list(H_df.edges.members(0))
== list(H_hg.edges.members(0))
H_list.edges.members(0)
== H_dict.edges.members(0)
== H_mat.edges.members(0)
== H_df.edges.members(0)
== H_hg.edges.members(0)
)

with pytest.raises(XGIError):
Expand Down Expand Up @@ -133,11 +133,11 @@ def test_hypergraph_attr(edgelist1):

def test_members(edgelist1):
H = xgi.Hypergraph(edgelist1)
assert H.nodes.memberships(1) == [0]
assert H.nodes.memberships(2) == [0]
assert H.nodes.memberships(3) == [0]
assert H.nodes.memberships(4) == [1]
assert H.nodes.memberships(6) == [2, 3]
assert H.nodes.memberships(1) == {0}
assert H.nodes.memberships(2) == {0}
assert H.nodes.memberships(3) == {0}
assert H.nodes.memberships(4) == {1}
assert H.nodes.memberships(6) == {2, 3}
with pytest.raises(IDNotFound):
H.nodes.memberships(0)
with pytest.raises(TypeError):
Expand All @@ -150,9 +150,9 @@ def test_add_edge():
H.add_edge(edge)
assert (1 in H) and (2 in H) and (3 in H)
assert 0 in H.edges
assert [1, 2, 3] in H.edges.members()
assert [1, 2, 3] == H.edges.members(0)
assert H.edges.members(dtype=dict) == {0: [1, 2, 3]}
assert {1, 2, 3} in H.edges.members()
assert {1, 2, 3} == H.edges.members(0)
assert H.edges.members(dtype=dict) == {0: {1, 2, 3}}

H = xgi.Hypergraph()
for edge in [[], set(), iter([])]:
Expand All @@ -165,19 +165,19 @@ def test_add_edge_with_id():
H.add_edge([1, 2, 3], id="myedge")
assert (1 in H) and (2 in H) and (3 in H)
assert "myedge" in H.edges
assert [1, 2, 3] in H.edges.members()
assert [1, 2, 3] == H.edges.members("myedge")
assert H.edges.members(dtype=dict) == {"myedge": [1, 2, 3]}
assert {1, 2, 3} in H.edges.members()
assert {1, 2, 3} == H.edges.members("myedge")
assert H.edges.members(dtype=dict) == {"myedge": {1, 2, 3}}


def test_add_edge_with_attr():
H = xgi.Hypergraph()
H.add_edge([1, 2, 3], color="red", place="peru")
assert (1 in H) and (2 in H) and (3 in H)
assert 0 in H.edges
assert [1, 2, 3] in H.edges.members()
assert [1, 2, 3] == H.edges.members(0)
assert H.edges.members(dtype=dict) == {0: [1, 2, 3]}
assert {1, 2, 3} in H.edges.members()
assert {1, 2, 3} == H.edges.members(0)
assert H.edges.members(dtype=dict) == {0: {1, 2, 3}}
assert H.edges[0] == {"color": "red", "place": "peru"}


Expand All @@ -187,62 +187,62 @@ def test_add_node_to_edge():
H.add_node_to_edge("fruits", "pear")
H.add_node_to_edge("veggies", "lettuce")
assert H.edges.members(dtype=dict) == {
"fruits": ["apple", "banana", "pear"],
"veggies": ["lettuce"],
"fruits": {"apple", "banana", "pear"},
"veggies": {"lettuce"},
}


def test_add_edges_from_iterable_of_members():
edges = [[0, 1], [1, 2], [2, 3, 4]]
edges = [{0, 1}, {1, 2}, {2, 3, 4}]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges.members()) == edges
assert H.edges.members() == edges

H1 = xgi.Hypergraph(edges)
with pytest.raises(XGIError):
xgi.Hypergraph(H1.edges)

H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges.members()) == edges
assert H.edges.members() == edges

edges = {frozenset([0, 1]), frozenset([1, 2]), frozenset([2, 3, 4])}
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges.members()) == [list(e) for e in edges]
assert H.edges.members() == [set(e) for e in edges]

edges = [[0, 1], frozenset([1, 2]), (2, 3, 4)]
edges = [[0, 1], {1, 2}, (2, 3, 4)]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges.members()) == [list(e) for e in edges]
assert H.edges.members() == [set(e) for e in edges]

edges = [["foo", "bar"], ["bar", "baz"], ["foo", "bar", "baz"]]
edges = [{"foo", "bar"}, {"bar", "baz"}, {"foo", "bar", "baz"}]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.nodes) == ["foo", "bar", "baz"]
assert list(H.edges.members()) == [list(e) for e in edges]
assert set(H.nodes) == {"foo", "bar", "baz"}
assert H.edges.members() == edges

edges = [["a", "b"], ["b", "c"], ["c", "d", "e"]]
edges = [{"a", "b"}, {"b", "c"}, {"c", "d", "e"}]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.nodes) == ["a", "b", "c", "d", "e"]
assert list(H.edges.members()) == [list(e) for e in edges]
assert set(H.nodes) == {"a", "b", "c", "d", "e"}
assert H.edges.members() == edges


def test_add_edges_from_format1():
edges = [([0, 1], 0), ([1, 2], 1), ([2, 3, 4], 2)]
edges = [({0, 1}, 0), ({1, 2}, 1), ({2, 3, 4}, 2)]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges) == [e[1] for e in edges]
assert H.edges.members(dtype=dict) == {e[1]: e[0] for e in edges}

edges = [([0, 1], "a"), ([1, 2], "b"), ([2, 3, 4], "foo")]
edges = [({0, 1}, "a"), ({1, 2}, "b"), ({2, 3, 4}, "foo")]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges) == [e[1] for e in edges]
assert H.edges.members(dtype=dict) == {e[1]: e[0] for e in edges}

edges = [([0, 1], "a"), ([1, 2], "b"), ([2, 3, 4], 100)]
edges = [({0, 1}, "a"), ({1, 2}, "b"), ({2, 3, 4}, 100)]
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges) == [e[1] for e in edges]
Expand All @@ -251,9 +251,9 @@ def test_add_edges_from_format1():

def test_add_edges_from_format2():
edges = [
([0, 1], {"color": "red"}),
([1, 2], {"age": 30}),
([2, 3, 4], {"color": "blue", "age": 40}),
({0, 1}, {"color": "red"}),
({1, 2}, {"age": 30}),
({2, 3, 4}, {"color": "blue", "age": 40}),
]
H = xgi.Hypergraph()
H.add_edges_from(edges)
Expand All @@ -265,9 +265,9 @@ def test_add_edges_from_format2():

def test_add_edges_from_format3():
edges = [
([0, 1], "one", {"color": "red"}),
([1, 2], "two", {"age": 30}),
([2, 3, 4], "three", {"color": "blue", "age": 40}),
({0, 1}, "one", {"color": "red"}),
({1, 2}, "two", {"age": 30}),
({2, 3, 4}, "three", {"color": "blue", "age": 40}),
]
H = xgi.Hypergraph()
H.add_edges_from(edges)
Expand All @@ -282,7 +282,7 @@ def test_add_edges_from_dict():
H = xgi.Hypergraph()
H.add_edges_from(edges)
assert list(H.edges) == ["one", "two", "three"]
assert H.edges.members() == list(edges.values())
assert H.edges.members() == [set(edges[e]) for e in edges]


def test_add_edges_from_attr_precedence():
Expand Down Expand Up @@ -374,56 +374,52 @@ def test_double_edge_swap(edgelist1):
H = xgi.Hypergraph(edgelist1)

with pytest.raises(XGIError):
H.double_edge_swap(5, 6, 2, 3, is_loopy=False)
H.double_edge_swap(5, 6, 2, 3)

H.double_edge_swap(1, 6, 0, 3)
assert H.edges.members() == [[6, 2, 3], [4], [5, 6], [1, 7, 8]]
assert H.edges.members() == [{2, 3, 6}, {4}, {5, 6}, {1, 7, 8}]

H.double_edge_swap(3, 4, 0, 1)
assert H.edges.members() == [[6, 2, 4], [3], [5, 6], [1, 7, 8]]
assert H.edges.members() == [{2, 4, 6}, {3}, {5, 6}, {1, 7, 8}]

with pytest.raises(IDNotFound):
H.double_edge_swap(10, 3, 0, 1)

with pytest.raises(XGIError):
H.double_edge_swap(8, 3, 0, 1)

# loopy swap
H.double_edge_swap(4, 6, 0, 2)
assert H.edges.members() == [[6, 2, 6], [3], [5, 4], [1, 7, 8]]


def test_duplicate_edges(edgelist1):
H = xgi.Hypergraph(edgelist1)
assert list(H.edges.duplicates()) == []

H.add_edge([1, 3, 2]) # same order as existing edge
assert list(H.edges.duplicates()) == [0, 4]
assert set(H.edges.duplicates()) == {4}

H.add_edge([1, 2, 3]) # different order, same members
assert list(H.edges.duplicates()) == [0, 4, 5]
assert set(H.edges.duplicates()) == {4, 5}

H = xgi.Hypergraph([[1, 2, 3, 3], [1, 2, 3]]) # repeated nodes
assert list(H.edges.duplicates()) == []
assert set(H.edges.duplicates()) == {1}

H = xgi.Hypergraph([[1, 2, 3, 3], [3, 1, 2, 3]]) # repeated nodes
assert list(H.edges.duplicates()) == [0, 1]
assert set(H.edges.duplicates()) == {1}


def test_duplicate_nodes(edgelist1):
H = xgi.Hypergraph(edgelist1)
assert list(H.nodes.duplicates()) == [1, 2, 3, 7, 8]
assert set(H.nodes.duplicates()) == {2, 3, 8}

H.add_edges_from([[1, 4], [2, 6, 7], [6, 8]])
assert list(H.nodes.duplicates()) == []
assert set(H.nodes.duplicates()) == set()

# this loop makes 1 and 2 belong to the same edges
for edgeid, members in H.edges.members(dtype=dict).items():
if 1 in members and 2 not in members:
H.add_node_to_edge(edgeid, 2)
if 1 not in members and 2 in members:
H.add_node_to_edge(edgeid, 1)
assert list(H.nodes.duplicates()) == [1, 2]
assert set(H.nodes.duplicates()) == {2}


def test_remove_node_weak(edgelist1):
Expand Down
30 changes: 15 additions & 15 deletions tests/classes/test_hypergraphviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@ def test_subhypergraph(edgelist1):
assert xgi.is_frozen(new_H)
assert not xgi.is_frozen(H)

assert list(H.nodes) == list(new_H.nodes)
assert list(H.edges) == list(new_H.edges)
assert set(H.nodes) == set(new_H.nodes)
assert set(H.edges) == set(new_H.edges)

new_H = xgi.classes.hypergraphviews.subhypergraph(H, nodes=[1, 2, 3, 4, 5])
assert list(new_H.nodes) == [1, 2, 3, 4, 5]
assert list(new_H.edges) == [0, 1]
assert set(new_H.nodes) == {1, 2, 3, 4, 5}
assert set(new_H.edges) == {0, 1}

new_H = xgi.classes.hypergraphviews.subhypergraph(H, edges=[1, 2])
assert list(new_H.nodes) == [1, 2, 3, 4, 5, 6, 7, 8]
assert list(new_H.edges) == [1, 2]
assert list(new_H.nodes.isolates(ignore_singletons=False)) == [1, 2, 3, 7, 8]
assert set(new_H.nodes) == {1, 2, 3, 4, 5, 6, 7, 8}
assert set(new_H.edges) == {1, 2}
assert set(new_H.nodes.isolates(ignore_singletons=False)) == {1, 2, 3, 7, 8}

new_H = xgi.classes.hypergraphviews.subhypergraph(
H, nodes=[1, 2, 3, 4, 5], edges=[1, 2]
)
assert list(new_H.nodes) == [1, 2, 3, 4, 5]
assert list(new_H.edges) == [1]
assert list(new_H.nodes.isolates(ignore_singletons=False)) == [1, 2, 3, 5]
assert set(new_H.nodes) == {1, 2, 3, 4, 5}
assert set(new_H.edges) == {1}
assert set(new_H.nodes.isolates(ignore_singletons=False)) == {1, 2, 3, 5}

new_H = xgi.classes.hypergraphviews.subhypergraph(H, nodes=[4], edges=[0, 1, 2])
assert list(new_H.nodes) == [4]
assert list(new_H.edges) == [1]
assert set(new_H.nodes) == {4}
assert set(new_H.edges) == {1}

new_H = xgi.classes.hypergraphviews.subhypergraph(H, nodes=[3, 4, 5, 6], edges=[2])
assert list(new_H.nodes) == [3, 4, 5, 6]
assert list(new_H.edges) == [2]
assert list(new_H.nodes.isolates(ignore_singletons=False)) == [3, 4]
assert set(new_H.nodes) == {3, 4, 5, 6}
assert set(new_H.edges) == {2}
assert set(new_H.nodes.isolates(ignore_singletons=False)) == {3, 4}
2 changes: 1 addition & 1 deletion tests/classes/test_iddict.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_iddict(edgelist1):
assert H.edges[0] == dict()

with pytest.raises(XGIError):
H._edge[None] = [1, 2, 3]
H._edge[None] = {1, 2, 3}

with pytest.raises(IDNotFound):
del H._node["test"]
Expand Down
Loading