def test_from_dataframe_with_transforms_and_fillna(dataframe): df = dataframe.df def key1(x): return x**2 def key2(x): return (x < 5) * x def key3(x): return (x > 0) * x h = Hypergraph.from_dataframe(df) assert "A" in h.edges["a"] assert "A" not in h.edges["b"] h = Hypergraph.from_dataframe(df, fillna=1) assert "A" in h.edges["b"] h = Hypergraph.from_dataframe(df, transforms=[key1, key2]) assert "A" in h.edges["c"] assert "C" not in h.edges["b"] h = Hypergraph.from_dataframe(df, transforms=[key2, key3]) assert "C" in h.edges["b"] h = Hypergraph.from_dataframe(df, transforms=[key3, key1], key=key2) assert "A" not in h.edges["a"] assert "B" in h.edges["b"] assert "C" not in h.edges["c"] assert "C" in h.edges["a"]
def test_s_component_subgraphs(): setsystem = [{1, 2, 3, 4}, {4, 5, 6}, {5, 6, 7}, {5, 6, 8}] h = Hypergraph(setsystem) assert {5, 4}.issubset( [len(g) for g in h.s_component_subgraphs(s=2, return_singletons=True)]) assert {3, 4}.issubset( [len(g) for g in h.s_component_subgraphs(s=3, return_singletons=True)])
def test_is_connected(): setsystem = [{1, 2, 3, 4}, {3, 4, 5, 6}, {5, 6, 7}, {5, 6, 8}] h = Hypergraph(setsystem) assert h.is_connected() == True assert h.is_connected(s=2) == False assert h.is_connected(s=2, edges=True) == True assert h.is_connected(s=3, edges=True) == False
def test_remove_singletons(): E = {1: {2, 3, 4, 5}, 6: {2, 5, 7, 8, 9}, 10: {11}, 12: {13}, 14: {7}} h = Hypergraph(E) assert h.shape == (9, 5) h1 = h.remove_singletons() assert h1.shape == (7, 3) assert h.shape == (9, 5)
def test_edge_diameter(seven_by_six): sbs = seven_by_six h = Hypergraph(sbs.edgedict) assert h.edge_diameter() == 3 with pytest.raises(Exception) as excinfo: h.edge_diameter(s=2) assert 'Hypergraph is not s-connected.' in str(excinfo.value)
def test_hypergraph_static(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edges, static=True) assert len(H.edges) == 6 assert len(H.nodes) == 7 assert H.get_id("E") == 3 assert list(H.get_linegraph(s=1)) == [0, 1, 2, 3, 4, 5]
def test_hypergraph_from_dict(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edgedict) assert len(H.edges) == 6 assert len(H.nodes) == 7 assert H.degree("A") == 3 assert H.size("R") == 2 assert H.order() == 7
def test_hypergraph_from_iterable_of_sets(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edges) assert len(H.edges) == 6 assert len(H.nodes) == 7 assert H.degree("A") == 3 assert H.number_of_edges() == 6 assert H.number_of_nodes() == 7
def test_singletons(): E = {1: {2, 3, 4, 5}, 6: {2, 5, 7, 8, 9}, 10: {11}, 12: {13}, 14: {7}} h = Hypergraph(E) assert h.shape == (9, 5) singles = h.singletons() assert len(singles) == 2 h.remove_edges(singles) assert h.shape == (7, 3)
def test_s_components(): setsystem = [{1, 2, 3, 4}, {4, 5, 6}, {5, 6, 7}, {5, 6, 8}] h = Hypergraph(setsystem) assert len(list(h.s_components())) == 1 assert len(list(h.s_components(s=2))) == 2 assert len(list(h.s_components(s=3))) == 4 assert len(list(h.s_components(s=3, edges=False))) == 7 assert len(list(h.s_components(s=4, edges=False))) == 8
def test_hypergraph_equality(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edgedict) G = Hypergraph(sbs.edgedict, 'G') assert not H == G sbs2 = seven_by_six K = Hypergraph(sbs2.edgedict) assert H == K del sbs2.edgedict['P'] J = Hypergraph(sbs2.edgedict) assert not H == J
def test_remove_node(): a, b, c, d = "a", "b", "c", "d" hbug = Hypergraph({0: [a, b], 1: [a, c], 2: [a, d]}) assert a in hbug.nodes assert a in hbug.edges[0] assert a in hbug.edges[1] assert a in hbug.edges[2] hbug.remove_node(a) assert a not in hbug.nodes assert a not in hbug.edges[0] assert a not in hbug.edges[1] assert a not in hbug.edges[2]
def test_remove_node(): a, b, c, d = 'a', 'b', 'c', 'd' hbug = Hypergraph({0: [a, b], 1: [a, c], 2: [a, d]}) assert a in hbug.nodes assert a in hbug.edges[0] assert a in hbug.edges[1] assert a in hbug.edges[2] hbug.remove_node(a) assert a not in hbug.nodes assert a not in hbug.edges[0] assert a not in hbug.edges[1] assert a not in hbug.edges[2]
def test_from_numpy_array(): M = np.array([[0, 1, 1, 0, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) h = Hypergraph.from_numpy_array(M) assert "v1" in h.edges["e0"] assert "e1" not in h.nodes["v2"].memberships with pytest.raises(Exception) as excinfo: h = Hypergraph.from_numpy_array(M, node_names=["A"]) assert "Number of node names does not match number of rows" in str( excinfo.value) node_names = ["A", "B", "C", "D"] edge_names = ["a", "b", "c", "d", "e"] h = Hypergraph.from_numpy_array(M, node_names, edge_names) assert "a" in h.edges assert "A" in h.nodes assert "B" in h.edges["a"]
def test_static_hypergraph_constructor_setsystem(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edgedict, static=True) assert isinstance(H.edges, StaticEntitySet) assert H.isstatic == True assert H.nwhy == False assert H.shape == (7, 6)
def test_from_numpy_array(): M = np.array([[0, 1, 1, 0, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0], [0, 0, 0, 0, 1]]) h = Hypergraph.from_numpy_array(M) assert 'v1' in h.edges['e0'] assert 'e1' not in h.nodes['v2'].memberships with pytest.raises(Exception) as excinfo: h = Hypergraph.from_numpy_array(M, node_names=['A']) assert 'Number of node names does not match number of rows' in str( excinfo.value) node_names = ['A', 'B', 'C', 'D'] edge_names = ['a', 'b', 'c', 'd', 'e'] h = Hypergraph.from_numpy_array(M, node_names, edge_names) assert 'a' in h.edges assert 'A' in h.nodes assert 'B' in h.edges['a']
def test_hypergraph_from_numpy_array(seven_by_six): sbs = seven_by_six H = Hypergraph.from_numpy_array(sbs.arr) assert len(H.nodes) == 6 assert len(H.edges) == 7 assert H.dim('e5') == 2 assert set(H.neighbors('v2')) == {'v0', 'v5'}
def test_from_dataframe_with_key(): M = np.array([[5, 0, 7, 2], [6, 8, 1, 1], [2, 5, 1, 9]]) index = ['A', 'B', 'C'] columns = ['a', 'b', 'c', 'd'] df = pd.DataFrame(M, index=index, columns=columns) h = Hypergraph.from_dataframe(df, key=lambda x: x > 4) assert 'A' in h.edges['a'] assert 'C' not in h.edges['a']
def test_from_dataframe_with_key(): M = np.array([[5, 0, 7, 2], [6, 8, 1, 1], [2, 5, 1, 9]]) index = ["A", "B", "C"] columns = ["a", "b", "c", "d"] df = pd.DataFrame(M, index=index, columns=columns) h = Hypergraph.from_dataframe(df, key=lambda x: x > 4) assert "A" in h.edges["a"] assert "C" not in h.edges["a"]
def test_from_numpy_array_with_key(): M = np.array([[5, 0, 7, 2], [6, 8, 1, 1], [2, 5, 1, 9]]) h = Hypergraph.from_numpy_array(M, node_names=['A', 'B', 'C'], edge_names=['a', 'b', 'c', 'd'], key=lambda x: x > 4) assert 'A' in h.edges['a'] assert 'C' not in h.edges['a']
def test_hypergraph_custom_attributes(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edges) assert isinstance(H.__str__(), str) assert isinstance(H.__repr__(), str) assert H.__contains__("A") assert H.__len__() == 7 nodes = [key for key in H.__iter__()] assert sorted(nodes) == ['A', 'C', 'E', 'K', 'T1', 'T2', 'V'] assert sorted(H.__getitem__("C")) == ['A', 'E', 'K']
def test_from_dataframe(): M = np.array([[1, 1, 0, 0], [0, 1, 1, 0], [1, 0, 1, 0]]) index = ['A', 'B', 'C'] columns = ['a', 'b', 'c', 'd'] df = pd.DataFrame(M, index=index, columns=columns) h = Hypergraph.from_dataframe(df) assert 'b' in h.edges assert 'd' not in h.edges assert 'C' in h.edges['a']
def test_from_dataframe(): M = np.array([[1, 1, 0, 0], [0, 1, 1, 0], [1, 0, 1, 0]]) index = ["A", "B", "C"] columns = ["a", "b", "c", "d"] df = pd.DataFrame(M, index=index, columns=columns) h = Hypergraph.from_dataframe(df) assert "b" in h.edges assert "d" not in h.edges assert "C" in h.edges["a"]
def test_from_bipartite(): g = nx.complete_bipartite_graph(2, 3) left, right = nx.bipartite.sets(g) h = Hypergraph.from_bipartite(g) assert left.issubset(h.nodes) assert right.issubset(h.edges) with pytest.raises(Exception) as excinfo: h.edge_diameter(s=4) assert "Hypergraph is not s-connected." in str(excinfo.value)
def test_from_numpy_array_with_key(): M = np.array([[5, 0, 7, 2], [6, 8, 1, 1], [2, 5, 1, 9]]) h = Hypergraph.from_numpy_array( M, node_names=["A", "B", "C"], edge_names=["a", "b", "c", "d"], key=lambda x: x > 4, ) assert "A" in h.edges["a"] assert "C" not in h.edges["a"]
def test_static_hypergraph_constructor_setsystem_nwhy(seven_by_six): sbs = seven_by_six edict = sbs.edgedict H = Hypergraph(edict, use_nwhy=True) assert isinstance(H.edges, StaticEntitySet) assert H.isstatic == True if nwhy_available: assert H.nwhy == True assert isinstance(H.g, nwhy.NWHypergraph) else: assert H.nwhy == False
def test_edge_diameter(seven_by_six): sbs = seven_by_six h = Hypergraph(sbs.edgedict) assert h.edge_diameter() == 3 assert h.edge_diameters()[2] == [{'I', 'L', 'O', 'P', 'R', 'S'}] with pytest.raises(Exception) as excinfo: h.edge_diameter(s=2) assert "Hypergraph is not s-connected." in str(excinfo.value)
def test_add_node_to_edge(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edgedict) assert H.shape == (7, 6) # add node not already in hypergraph to edge # alreadyin hypergraph node = Entity('B') edge = H.edges['P'] H.add_node_to_edge(node, edge) assert H.shape == (8, 6) # add edge with nodes already in hypergraph H.add_edge(Entity('Z', ['A', 'B'])) assert H.shape == (8, 7) # add edge not in hypergraph with nodes not in hypergraph H.add_edge(Entity('Y', ['M', 'N'])) assert H.shape == (10, 8)
def test_add_node_to_edge(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edgedict) assert H.shape == (7, 6) # add node not already in hypergraph to edge # alreadyin hypergraph node = Entity("B") edge = H.edges["P"] H.add_node_to_edge(node, edge) assert H.shape == (8, 6) # add edge with nodes already in hypergraph H.add_edge(Entity("Z", ["A", "B"])) assert H.shape == (8, 7) # add edge not in hypergraph with nodes not in hypergraph H.add_edge(Entity("Y", ["M", "N"])) assert H.shape == (10, 8)
def test_hypergraph_from_dict(seven_by_six): sbs = seven_by_six H = Hypergraph(sbs.edgedict) assert len(H.edges) == 6 assert len(H.nodes) == 7 assert H.degree('A') == 3 assert H.degree('A', edges=['P', 'I', 'R']) == 2 assert H.size('R') == 2 assert H.size('R', nodes=['E', 'K']) == 1