Esempio n. 1
0
def test_get_hyperedge_weight():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    weight_e1 = H.get_hyperedge_weight('e1')
    weight_e2 = H.get_hyperedge_weight('e2')
    assert weight_e1 == 2
    assert weight_e2 == 2
def from_networkx_graph(nx_graph):
    """Returns an UndirectedHypergraph object that is the graph equivalent of
    the given NetworkX Graph object.

    :param nx_graph: the NetworkX undirected graph object to transform.
    :returns: UndirectedHypergraph -- H object equivalent to the
            NetworkX undirected graph.
    :raises: TypeError -- Transformation only applicable to undirected
            NetworkX graphs

    """
    import networkx as nx

    if not isinstance(nx_graph, nx.Graph):
        raise TypeError("Transformation only applicable to undirected \
                        NetworkX graphs")

    G = UndirectedHypergraph()

    for node in nx_graph.nodes_iter():
        G.add_node(node, copy.copy(nx_graph.node[node]))

    for edge in nx_graph.edges_iter():
        G.add_hyperedge([edge[0], edge[1]],
                        copy.copy(nx_graph[edge[0]][edge[1]]))

    return G
Esempio n. 3
0
def test_remove_hyperedges():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_hyperedges(['e1', 'e3'])

    assert 'e1' not in H._hyperedge_attributes
    assert 'e1' not in H._star[node_a]
    assert 'e1' not in H._star[node_b]
    assert 'e1' not in H._star[node_c]
    assert frozen_nodes1 not in H._node_set_to_hyperedge

    assert 'e3' not in H._hyperedge_attributes
    assert 'e3' not in H._star[node_d]
    assert 'e3' not in H._star[node_e]
    assert frozen_nodes3 not in H._node_set_to_hyperedge
def test_remove_hyperedges():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_hyperedges(['e1', 'e3'])

    assert 'e1' not in H._hyperedge_attributes
    assert 'e1' not in H._star[node_a]
    assert 'e1' not in H._star[node_b]
    assert 'e1' not in H._star[node_c]
    assert frozen_nodes1 not in H._node_set_to_hyperedge

    assert 'e3' not in H._hyperedge_attributes
    assert 'e3' not in H._star[node_d]
    assert 'e3' not in H._star[node_e]
    assert frozen_nodes3 not in H._node_set_to_hyperedge
def test_add_nodes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}
    common_attrib = {'common': True, 'source': False}

    node_list = [node_a, (node_b, {'source': False}),
                 (node_c, attrib_c), (node_d, attrib_d)]

    # Test adding unadded nodes with various attribute settings
    H = UndirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    assert node_a in H._node_attributes
    assert H._node_attributes[node_a] == common_attrib

    assert node_b in H._node_attributes
    assert H._node_attributes[node_b]['source'] is False

    assert node_c in H._node_attributes
    assert H._node_attributes[node_c]['alt_name'] == 1337

    assert node_d in H._node_attributes
    assert H._node_attributes[node_d]['label'] == 'black'
    assert H._node_attributes[node_d]['sink'] is True

    node_set = H.get_node_set()
    assert node_set == set(['A', 'B', 'C', 'D'])
    assert len(node_set) == len(node_list)
    for node in H.node_iterator():
        assert node in node_set
def test_get_hyperedge_weight():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    weight_e1 = H.get_hyperedge_weight('e1')
    weight_e2 = H.get_hyperedge_weight('e2')
    assert weight_e1 == 2
    assert weight_e2 == 2
def test_copy():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    H.add_node("A", root=True)
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    new_H = H.copy()

    assert new_H._node_attributes == H._node_attributes
    assert new_H._hyperedge_attributes == H._hyperedge_attributes
    assert new_H._star == H._star
    assert new_H._node_set_to_hyperedge == H._node_set_to_hyperedge
Esempio n. 8
0
def test_laplacian():
    '''
    To test the normalized cut, I wrote the same functions in Matlab
    and compared the result of it with the current library code. In
    this test, I calculated the delta matrix using Matlab and found
    the summation of each column of this matrix and compared that
    summation to the result of our code.
    The following is the matlab snippet that is used for this test:
    M = [1 0 1 0 0 0 0 0 0;1 1 0 0 1 0 0 0 0 ;1 0 1 0 0 0 0 0 0;\
    0 1 0 1 0 0 0 0 0;0 0 0 1 0 0 0 0 0;0 0 0 0 1 1 0 0 0;\
    0 0 0 0 0 1 0 0 1;0 0 0 0 0 1 1 1 0;0 0 0 0 0 0 1 0 0;\
    0 0 0 0 0 0 1 0 0;0 0 0 0 0 0 0 1 1;0 0 0 0 0 0 0 0 1];
    W = diag([9.1 10 1 1 3 2 4 3.5 4.1]);
    d_v = diag(H * diag(W));
    d_e = diag(sum(M));
    d_v_sqrt = sqrtm(d_v);
    d_v_sqrt_inv = inv(d_v_sqrt);
    d_e_inv = inv(d_e);
    M_trans = M';
    theta = d_v_sqrt_inv * H * W * d_e_inv * H_trans * d_v_sqrt_inv
    [n m] = size(M);
    I = eye(n);
    delta = I-theta
    '''
    H = UndirectedHypergraph()
    H.read('./tests/data/basic_undirected_hypergraph.txt')
    indices_to_nodes, nodes_to_indices = \
        umat.get_node_mapping(H)
    indices_to_hyperedge_id, hyperedge_id_to_indices = \
        umat.get_hyperedge_id_mapping(H)

    delta = partitioning._compute_normalized_laplacian(
        H, nodes_to_indices, hyperedge_id_to_indices)

    delta_column_sum = np.sum(delta.todense(), axis=0)
    delta_column_sum = np.squeeze(np.asarray(delta_column_sum))

    Matlab_output = {
        'v1': 0.0973,
        'v2': -0.3008,
        'v3': 0.0973,
        'v4': 0.0286,
        'v5': 0.3492,
        'v7': 0.2065,
        'v8': -0.0156,
        'v9': -0.2176,
        'v10': 0.1170,
        'v11': 0.1170,
        'v12': -0.0616,
        'v13': 0.1486
    }

    for key, value in Matlab_output.items():
        index = nodes_to_indices.get(key)
        assert fabs(delta_column_sum[index] - value) < 10e-4
Esempio n. 9
0
def test_to_networkx_graph():
    H = UndirectedHypergraph()
    H.read("tests/data/basic_undirected_hypergraph.txt")

    G = undirected_graph_transformations.to_networkx_graph(H)

    H_nodes = H.get_node_set()
    G_nodes = G.nodes.keys()

    assert H_nodes == set(G_nodes)

    H_nodes_attributes = [H.get_node_attributes(node) for node in H_nodes]
    for node in G_nodes:
        assert G.nodes[node] in H_nodes_attributes

    for hyperedge_id in H.hyperedge_id_iterator():
        hyperedge_nodes = H.get_hyperedge_nodes(hyperedge_id)
        for node_a in hyperedge_nodes:
            for node_b in hyperedge_nodes:
                if node_a != node_b:
                    assert G.has_edge(node_a, node_b)
                else:
                    if G.has_edge(node_a, node_b):
                        assert False

    # Try transforming an invalid undirected hypergraph
    try:
        undirected_graph_transformations.to_networkx_graph("invalid H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_stationary_distribution():
    H = UndirectedHypergraph()
    H.read('./tests/data/basic_undirected_hypergraph.txt')

    pi = partitioning.stationary_distribution(H)

    # Correctness tests go here
    assert sum(pi)-1.0 < 10e-4
    # Try partitioning an invalid undirected hypergraph
    try:
        pi = partitioning.stationary_distribution("H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 11
0
def test_stationary_distribution():
    H = UndirectedHypergraph()
    H.read('./tests/data/basic_undirected_hypergraph.txt')

    pi = partitioning.stationary_distribution(H)

    # Correctness tests go here
    assert sum(pi) - 1.0 < 10e-4
    # Try partitioning an invalid undirected hypergraph
    try:
        pi = partitioning.stationary_distribution("H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_laplacian():
    '''
    To test the normalized cut, I wrote the same functions in Matlab
    and compared the result of it with the current library code. In
    this test, I calculated the delta matrix using Matlab and found
    the summation of each column of this matrix and compared that
    summation to the result of our code.
    The following is the matlab snippet that is used for this test:
    M = [1 0 1 0 0 0 0 0 0;1 1 0 0 1 0 0 0 0 ;1 0 1 0 0 0 0 0 0;\
    0 1 0 1 0 0 0 0 0;0 0 0 1 0 0 0 0 0;0 0 0 0 1 1 0 0 0;\
    0 0 0 0 0 1 0 0 1;0 0 0 0 0 1 1 1 0;0 0 0 0 0 0 1 0 0;\
    0 0 0 0 0 0 1 0 0;0 0 0 0 0 0 0 1 1;0 0 0 0 0 0 0 0 1];
    W = diag([9.1 10 1 1 3 2 4 3.5 4.1]);
    d_v = diag(H * diag(W));
    d_e = diag(sum(M));
    d_v_sqrt = sqrtm(d_v);
    d_v_sqrt_inv = inv(d_v_sqrt);
    d_e_inv = inv(d_e);
    M_trans = M';
    theta = d_v_sqrt_inv * H * W * d_e_inv * H_trans * d_v_sqrt_inv
    [n m] = size(M);
    I = eye(n);
    delta = I-theta
    '''
    H = UndirectedHypergraph()
    H.read('./tests/data/basic_undirected_hypergraph.txt')
    indices_to_nodes, nodes_to_indices = \
        umat.get_node_mapping(H)
    indices_to_hyperedge_id, hyperedge_id_to_indices = \
        umat.get_hyperedge_id_mapping(H)

    delta = partitioning._compute_normalized_laplacian(H, nodes_to_indices,
                                                       hyperedge_id_to_indices)
    
    delta_column_sum = np.sum(delta.todense(), axis=0)
    delta_column_sum = np.squeeze(np.asarray(delta_column_sum))

    Matlab_output = {'v1': 0.0973, 'v2': -0.3008, 'v3': 0.0973,
                     'v4': 0.0286, 'v5': 0.3492, 'v7': 0.2065, 'v8': -0.0156,
                     'v9': -0.2176, 'v10': 0.1170, 'v11': 0.1170,
                     'v12': -0.0616, 'v13': 0.1486}

    for key, value in Matlab_output.items():
        index = nodes_to_indices.get(key)
        assert fabs(delta_column_sum[index]-value) < 10e-4
Esempio n. 13
0
def test_get_hyperedge_id():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert H.get_hyperedge_id(nodes1) == 'e1'
    assert H.get_hyperedge_id(nodes2) == 'e2'
    assert H.get_hyperedge_id(nodes3) == 'e3'

    try:
        H.get_hyperedge_id(set([node_a, node_b, node_c, node_d]))
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 14
0
def to_graph_decomposition(H):
    """Returns an UndirectedHypergraph object that has the same nodes (and
    corresponding attributes) as the given H, except that for all
    hyperedges in the given H, each node in the hyperedge is pairwise
    connected to every other node also in that hyperedge in the new H.
    Said another way, each of the original hyperedges are decomposed in the
    new H into cliques (aka the "2-section" or "clique graph").

    :param H: the H to decompose into a graph.
    :returns: UndirectedHypergraph -- the decomposed H.
    :raises: TypeError -- Transformation only applicable to
            undirected Hs

    """
    if not isinstance(H, UndirectedHypergraph):
        raise TypeError("Transformation only applicable to \
                        undirected Hs")

    G = UndirectedHypergraph()

    nodes = [(node, H.get_node_attributes(node_attributes))
             for node in G.node_iterator()]
    G.add_nodes(nodes)

    edges = [(node_a, node_b) for hyperedge_id in H.hyperedge_id_iterator()
             for node_a in H.get_hyperedge_nodes(hyperedge_id)
             for node_b in H.get_hyperedge_nodes(hyperedge_id)
             if node_a != node_b]

    G.add_hyperedges(edges)

    return G
Esempio n. 15
0
def test_get_hyperedge_attribute():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert H.get_hyperedge_attribute('e1', 'weight') == 1
    assert H.get_hyperedge_attribute('e1', 'color') == 'white'
    assert H.get_hyperedge_attribute('e1', 'sink') is False

    # Try requesting an invalid hyperedge
    try:
        H.get_hyperedge_attribute('e5', 'weight')
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Try requesting an invalid attribute
    try:
        H.get_hyperedge_attribute('e1', 'source')
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_to_networkx_graph():
    H = UndirectedHypergraph()
    H.read("tests/data/basic_undirected_hypergraph.txt")

    G = undirected_graph_transformations.to_networkx_graph(H)

    H_nodes = H.get_node_set()
    G_nodes = G.node.keys()

    assert H_nodes == set(G_nodes)

    H_nodes_attributes = [H.get_node_attributes(node) for node in H_nodes]
    for node in G_nodes:
        assert G.node[node] in H_nodes_attributes

    for hyperedge_id in H.hyperedge_id_iterator():
        hyperedge_nodes = H.get_hyperedge_nodes(hyperedge_id)
        for node_a in hyperedge_nodes:
            for node_b in hyperedge_nodes:
                if node_a != node_b:
                    assert G.has_edge(node_a, node_b)
                else:
                    if G.has_edge(node_a, node_b):
                        assert False

    # Try transforming an invalid undirected hypergraph
    try:
        undirected_graph_transformations.to_networkx_graph("invalid H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_normalized_hypergraph_cut():
    H = UndirectedHypergraph()
    H.read('./tests/data/basic_undirected_hypergraph.txt')
    S, T = partitioning.normalized_hypergraph_cut(H)

    # Correctness tests go here
    assert S
    assert T
    assert not S.intersection(T)

    # Try partitioning an invalid undirected hypergraph
    try:
        S, T = partitioning.normalized_hypergraph_cut("H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 18
0
def test_normalized_hypergraph_cut():
    H = UndirectedHypergraph()
    H.read('./tests/data/basic_undirected_hypergraph.txt')
    S, T = partitioning.normalized_hypergraph_cut(H)

    # Correctness tests go here
    assert S
    assert T
    assert not S.intersection(T)

    # Try partitioning an invalid undirected hypergraph
    try:
        S, T = partitioning.normalized_hypergraph_cut("H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 19
0
def test_to_graph_decomposition():
    H = UndirectedHypergraph()
    H.read("tests/data/basic_undirected_hypergraph.txt")

    G = undirected_graph_transformations.to_graph_decomposition(H)

    assert G.get_node_set() == H.get_node_set()

    for hyperedge_id in G.hyperedge_id_iterator():
        hyperedge_nodes = G.get_hyperedge_nodes(hyperedge_id)
        assert len(hyperedge_nodes) == 2
        assert G.has_hyperedge((hyperedge_nodes[0], hyperedge_nodes[1]))

    # Try posting an invalid undirected hypergraph
    try:
        undirected_graph_transformations.to_graph_decomposition("invalid H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_to_graph_decomposition():
    H = UndirectedHypergraph()
    H.read("tests/data/basic_undirected_hypergraph.txt")

    G = undirected_graph_transformations.to_graph_decomposition(H)

    assert G.get_node_set() == H.get_node_set()

    for hyperedge_id in G.hyperedge_id_iterator():
        hyperedge_nodes = G.get_hyperedge_nodes(hyperedge_id)
        assert len(hyperedge_nodes) == 2
        assert G.has_hyperedge((hyperedge_nodes[0], hyperedge_nodes[1]))

    # Try posting an invalid undirected hypergraph
    try:
        undirected_graph_transformations.to_graph_decomposition("invalid H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_get_hyperedge_attribute():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert H.get_hyperedge_attribute('e1', 'weight') == 1
    assert H.get_hyperedge_attribute('e1', 'color') == 'white'
    assert H.get_hyperedge_attribute('e1', 'sink') is False

    # Try requesting an invalid hyperedge
    try:
        H.get_hyperedge_attribute('e5', 'weight')
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Try requesting an invalid attribute
    try:
        H.get_hyperedge_attribute('e1', 'source')
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_get_hyperedge_id():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert H.get_hyperedge_id(nodes1) == 'e1'
    assert H.get_hyperedge_id(nodes2) == 'e2'
    assert H.get_hyperedge_id(nodes3) == 'e3'

    try:
        H.get_hyperedge_id(set([node_a, node_b, node_c, node_d]))
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 23
0
def from_networkx_graph(nx_graph):
    """Returns an UndirectedHypergraph object that is the graph equivalent of
    the given NetworkX Graph object.

    :param nx_graph: the NetworkX undirected graph object to transform.
    :returns: UndirectedHypergraph -- H object equivalent to the
            NetworkX undirected graph.
    :raises: TypeError -- Transformation only applicable to undirected
            NetworkX graphs

    """
    import networkx as nx

    if not isinstance(nx_graph, nx.Graph):
        raise TypeError("Transformation only applicable to undirected \
                        NetworkX graphs")

    G = UndirectedHypergraph()

    for node in nx_graph.nodes():
        G.add_node(node, copy.copy(nx_graph.nodes[node]))

    for edge in nx_graph.edges():
        G.add_hyperedge([edge[0], edge[1]],
                        copy.copy(nx_graph[edge[0]][edge[1]]))

    return G
def to_graph_decomposition(H):
    """Returns an UndirectedHypergraph object that has the same nodes (and
    corresponding attributes) as the given H, except that for all
    hyperedges in the given H, each node in the hyperedge is pairwise
    connected to every other node also in that hyperedge in the new H.
    Said another way, each of the original hyperedges are decomposed in the
    new H into cliques (aka the "2-section" or "clique graph").

    :param H: the H to decompose into a graph.
    :returns: UndirectedHypergraph -- the decomposed H.
    :raises: TypeError -- Transformation only applicable to
            undirected Hs

    """
    if not isinstance(H, UndirectedHypergraph):
        raise TypeError("Transformation only applicable to \
                        undirected Hs")

    G = UndirectedHypergraph()

    nodes = [(node, H.get_node_attributes(node_attributes))
             for node in G.node_iterator()]
    G.add_nodes(nodes)

    edges = [(node_a, node_b)
             for hyperedge_id in H.hyperedge_id_iterator()
             for node_a in H.get_hyperedge_nodes(hyperedge_id)
             for node_b in H.get_hyperedge_nodes(hyperedge_id)
             if node_a != node_b]

    G.add_hyperedges(edges)

    return G
Esempio n. 25
0
def test_add_hyperedge():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    attrib = {'weight': 6, 'color': 'black'}

    H = UndirectedHypergraph()
    H.add_node(node_a, label=1337)
    hyperedge_name = H.add_hyperedge(nodes1, attrib, weight=5)

    assert hyperedge_name == 'e1'

    # Test that all hyperedge attributes are correct
    assert H._hyperedge_attributes[hyperedge_name]['nodes'] == nodes1
    assert H._hyperedge_attributes[hyperedge_name]['__frozen_nodes'] == \
        frozen_nodes1
    assert H._hyperedge_attributes[hyperedge_name]['weight'] == 5
    assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'

    # Test that compose_hyperedge list contains the correct info
    assert frozen_nodes1 in H._node_set_to_hyperedge
    assert hyperedge_name == H._node_set_to_hyperedge[frozen_nodes1]

    # Test that the stars contain the correct info
    for node in frozen_nodes1:
        assert hyperedge_name in H._star[node]

    # Test that adding same hyperedge will only update attributes
    new_attrib = {'weight': 10}
    H.add_hyperedge(nodes1, new_attrib)
    assert H._hyperedge_attributes[hyperedge_name]['weight'] == 10
    assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'

    try:
        H.add_hyperedge(set())
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 26
0
def test_copy():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    H.add_node("A", root=True)
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    new_H = H.copy()

    assert new_H._node_attributes == H._node_attributes
    assert new_H._hyperedge_attributes == H._hyperedge_attributes
    assert new_H._star == H._star
    assert new_H._node_set_to_hyperedge == H._node_set_to_hyperedge
Esempio n. 27
0
def test_remove_nodes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_nodes([node_a, node_e])

    # Test that everything that needed to be removed was removed
    assert node_a not in H._node_attributes
    assert node_a not in H._star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_nodes1 not in H._node_set_to_hyperedge
    assert frozen_nodes2 not in H._node_set_to_hyperedge

    assert node_e not in H._node_attributes
    assert node_e not in H._star
    assert "e3" not in H._hyperedge_attributes
    assert frozen_nodes3 not in H._node_set_to_hyperedge
def test_remove_nodes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_nodes([node_a, node_e])

    # Test that everything that needed to be removed was removed
    assert node_a not in H._node_attributes
    assert node_a not in H._star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_nodes1 not in H._node_set_to_hyperedge
    assert frozen_nodes2 not in H._node_set_to_hyperedge

    assert node_e not in H._node_attributes
    assert node_e not in H._star
    assert "e3" not in H._hyperedge_attributes
    assert frozen_nodes3 not in H._node_set_to_hyperedge
Esempio n. 29
0
def test_add_hyperedges():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    common_attrib = {'sink': False}

    hyperedges = [nodes1, nodes2]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert 'e1' in hyperedge_names
    assert 'e2' in hyperedge_names

    assert H._hyperedge_attributes['e1']['nodes'] == nodes1
    assert H._hyperedge_attributes['e1']['weight'] == 1
    assert H._hyperedge_attributes['e1']['color'] == 'white'
    assert H._hyperedge_attributes['e1']['sink'] is False

    assert H._hyperedge_attributes['e2']['nodes'] == nodes2
    assert H._hyperedge_attributes['e2']['weight'] == 1
    assert H._hyperedge_attributes['e2']['color'] == 'white'
    assert H._hyperedge_attributes['e2']['sink'] is False

    assert set(hyperedge_names) == H.get_hyperedge_id_set()
    assert set(hyperedge_names) == H.get_hyperedge_id_set()
    for hyperedge_id in H.hyperedge_id_iterator():
        assert hyperedge_id in hyperedge_names
def test_add_hyperedges():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    common_attrib = {'sink': False}

    hyperedges = [nodes1, nodes2]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert 'e1' in hyperedge_names
    assert 'e2' in hyperedge_names

    assert H._hyperedge_attributes['e1']['nodes'] == nodes1
    assert H._hyperedge_attributes['e1']['weight'] == 1
    assert H._hyperedge_attributes['e1']['color'] == 'white'
    assert H._hyperedge_attributes['e1']['sink'] is False

    assert H._hyperedge_attributes['e2']['nodes'] == nodes2
    assert H._hyperedge_attributes['e2']['weight'] == 1
    assert H._hyperedge_attributes['e2']['color'] == 'white'
    assert H._hyperedge_attributes['e2']['sink'] is False

    assert set(hyperedge_names) == H.get_hyperedge_id_set()
    assert set(hyperedge_names) == H.get_hyperedge_id_set()
    for hyperedge_id in H.hyperedge_id_iterator():
        assert hyperedge_id in hyperedge_names
Esempio n. 31
0
def test_from_networkx_graph():
    H = UndirectedHypergraph()
    H.read("tests/data/basic_undirected_hypergraph.txt")

    nxG = undirected_graph_transformations.to_networkx_graph(H)

    G = undirected_graph_transformations.from_networkx_graph(nxG)

    nxG_nodes = nxG.nodes.keys()
    G_nodes = G.get_node_set()

    assert G_nodes == set(nxG_nodes)

    for edge in nxG.edges():
        assert G.has_hyperedge((edge[0], edge[1]))

    # Try transforming an invalid undirected hypergraph
    try:
        undirected_graph_transformations.from_networkx_graph("G")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_from_networkx_graph():
    H = UndirectedHypergraph()
    H.read("tests/data/basic_undirected_hypergraph.txt")

    nxG = undirected_graph_transformations.to_networkx_graph(H)

    G = undirected_graph_transformations.from_networkx_graph(nxG)

    nxG_nodes = nxG.node.keys()
    G_nodes = G.get_node_set()

    assert G_nodes == set(nxG_nodes)

    for edge in nxG.edges_iter():
        assert G.has_hyperedge((edge[0], edge[1]))

    # Try transforming an invalid undirected hypergraph
    try:
        undirected_graph_transformations.from_networkx_graph("G")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
def test_add_hyperedge():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    attrib = {'weight': 6, 'color': 'black'}

    H = UndirectedHypergraph()
    H.add_node(node_a, label=1337)
    hyperedge_name = H.add_hyperedge(nodes1, attrib, weight=5)

    assert hyperedge_name == 'e1'

    # Test that all hyperedge attributes are correct
    assert H._hyperedge_attributes[hyperedge_name]['nodes'] == nodes1
    assert H._hyperedge_attributes[hyperedge_name]['__frozen_nodes'] == \
        frozen_nodes1
    assert H._hyperedge_attributes[hyperedge_name]['weight'] == 5
    assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'

    # Test that compose_hyperedge list contains the correct info
    assert frozen_nodes1 in H._node_set_to_hyperedge
    assert hyperedge_name == H._node_set_to_hyperedge[frozen_nodes1]

    # Test that the stars contain the correct info
    for node in frozen_nodes1:
        assert hyperedge_name in H._star[node]

    # Test that adding same hyperedge will only update attributes
    new_attrib = {'weight': 10}
    H.add_hyperedge(nodes1, new_attrib)
    assert H._hyperedge_attributes[hyperedge_name]['weight'] == 10
    assert H._hyperedge_attributes[hyperedge_name]['color'] == 'black'

    try:
        H.add_hyperedge(set())
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 34
0
def test_remove_node():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_node(node_a)

    # Test that everything that needed to be removed was removed
    assert node_a not in H._node_attributes
    assert node_a not in H._star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_nodes1 not in H._node_set_to_hyperedge
    assert frozen_nodes2 not in H._node_set_to_hyperedge

    # Test that everything that wasn't supposed to be removed wasn't removed
    assert "e3" in H._hyperedge_attributes
    assert frozen_nodes3 in H._node_set_to_hyperedge

    try:
        H.remove_node(node_a)
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 35
0
def test_get_star():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert H.get_star(node_a) == set(['e1', 'e2'])
    assert H.get_star(node_b) == set(['e1'])
    assert H.get_star(node_c) == set(['e1'])
    assert H.get_star(node_d) == set(['e2', 'e3'])
    assert H.get_star(node_e) == set(['e3'])

    # Try requesting an invalid node
    try:
        H.get_star("F")
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_remove_node():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_node(node_a)

    # Test that everything that needed to be removed was removed
    assert node_a not in H._node_attributes
    assert node_a not in H._star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_nodes1 not in H._node_set_to_hyperedge
    assert frozen_nodes2 not in H._node_set_to_hyperedge

    # Test that everything that wasn't supposed to be removed wasn't removed
    assert "e3" in H._hyperedge_attributes
    assert frozen_nodes3 in H._node_set_to_hyperedge

    try:
        H.remove_node(node_a)
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 37
0
def test_remove_hyperedge():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_hyperedge('e1')

    assert 'e1' not in H._hyperedge_attributes
    assert 'e1' not in H._star[node_a]
    assert 'e1' not in H._star[node_b]
    assert 'e1' not in H._star[node_c]
    assert frozen_nodes1 not in H._node_set_to_hyperedge

    try:
        H.remove_hyperedge('e1')
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_get_star():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    assert H.get_star(node_a) == set(['e1', 'e2'])
    assert H.get_star(node_b) == set(['e1'])
    assert H.get_star(node_c) == set(['e1'])
    assert H.get_star(node_d) == set(['e2', 'e3'])
    assert H.get_star(node_e) == set(['e3'])

    # Try requesting an invalid node
    try:
        H.get_star("F")
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_remove_hyperedge():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')
    H.remove_hyperedge('e1')

    assert 'e1' not in H._hyperedge_attributes
    assert 'e1' not in H._star[node_a]
    assert 'e1' not in H._star[node_b]
    assert 'e1' not in H._star[node_c]
    assert frozen_nodes1 not in H._node_set_to_hyperedge

    try:
        H.remove_hyperedge('e1')
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 40
0
def test_add_nodes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}
    common_attrib = {'common': True, 'source': False}

    node_list = [
        node_a, (node_b, {
            'source': False
        }), (node_c, attrib_c), (node_d, attrib_d)
    ]

    # Test adding unadded nodes with various attribute settings
    H = UndirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    assert node_a in H._node_attributes
    assert H._node_attributes[node_a] == common_attrib

    assert node_b in H._node_attributes
    assert H._node_attributes[node_b]['source'] is False

    assert node_c in H._node_attributes
    assert H._node_attributes[node_c]['alt_name'] == 1337

    assert node_d in H._node_attributes
    assert H._node_attributes[node_d]['label'] == 'black'
    assert H._node_attributes[node_d]['sink'] is True

    node_set = H.get_node_set()
    assert node_set == set(['A', 'B', 'C', 'D'])
    assert len(node_set) == len(node_list)
    for node in H.node_iterator():
        assert node in node_set
def test_get_node_attributes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}

    # Test adding unadded nodes with various attribute settings
    H = UndirectedHypergraph()
    H.add_node(node_a)
    H.add_node(node_b, source=True)
    H.add_node(node_c, attrib_c)
    H.add_node(node_d, attrib_d, sink=False)

    attrs = H.get_node_attributes(node_b)
    assert attrs['source'] is True

    # Try requesting an invalid node
    try:
        H.get_node_attributes("E")
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_read_and_write():
    # Try writing the following hypergraph to a file
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    H.write("test_undirected_read_and_write.txt")

    # Try reading the hypergraph that was just written into a new hypergraph
    new_H = UndirectedHypergraph()
    new_H.read("test_undirected_read_and_write.txt")

    assert H._node_attributes.keys() == new_H._node_attributes.keys()

    for new_hyperedge_id in new_H.get_hyperedge_id_set():
        new_hyperedge_nodes = new_H.get_hyperedge_nodes(new_hyperedge_id)
        new_hyperedge_weight = new_H.get_hyperedge_weight(new_hyperedge_id)

        found_matching_hyperedge = False
        for hyperedge_id in H.get_hyperedge_id_set():
            hyperedge_nodes = H.get_hyperedge_nodes(hyperedge_id)
            hyperedge_weight = H.get_hyperedge_weight(hyperedge_id)

            if new_hyperedge_nodes == hyperedge_nodes and \
               new_hyperedge_weight == hyperedge_weight:
                found_matching_hyperedge = True
                continue

        assert found_matching_hyperedge

    remove("test_undirected_read_and_write.txt")

    # Try reading an invalid hypergraph file
    invalid_H = UndirectedHypergraph()
    try:
        invalid_H.read("tests/data/invalid_undirected_hypergraph.txt")
        assert False
    except IOError:
        pass
    except BaseException as e:
        assert False, e
def buildGraph():
    H = UndirectedHypergraph()
    node_delimiter = ','
    column_separator = ';'
    H.read('hypergraph.txt', node_delimiter, column_separator)
    return H
Esempio n. 44
0
df = pd.read_csv(
    "C:/Users/user/PycharmProjects/dataset/yelp-dataset/train_csv_version.csv",
    names=[
        'id', 'label', 'statement', 'subject', 'speaker', 'job', 'state',
        'party', 'barely_true_c', 'false_c', 'half_true_c', 'mostly_true_c',
        'pants_on_fire_c', 'venue'
    ])
df1 = pd.read_csv(
    "C:/Users/user/PycharmProjects/dataset/yelp-dataset/result.csv",
    names=['subject', 'edge'])

# df.info()
# print(df.head(10))

# Initialize an empty hypergraph
H = UndirectedHypergraph()

# NODE
node_list = df['id']
node_list1 = tuple(node_list)

# EDGE
node_subject = df1['edge']

# attribute_list = {"f1": df['speaker'],
#                   "f2": df['job'],
#                   "f3": df['state']}
# economy = df.loc[df['subject'] == 'economy']
# # list = economy['id']
hyperedge_list = (node_subject)
edge_list = list(zip(hyperedge_list, hyperedge_list.index))
Esempio n. 45
0
def test_read_and_write():
    # Try writing the following hypergraph to a file
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    nodes1 = set([node_a, node_b, node_c])
    frozen_nodes1 = frozenset(nodes1)

    nodes2 = set([node_a, node_d])
    frozen_nodes2 = frozenset(nodes2)

    nodes3 = set([node_d, node_e])
    frozen_nodes3 = frozenset(nodes3)

    common_attrib = {'weight': 2, 'sink': False}

    hyperedges = hyperedges = [nodes1, nodes2, nodes3]

    H = UndirectedHypergraph()
    hyperedge_names = \
        H.add_hyperedges(hyperedges, common_attrib, color='white')

    H.write("test_undirected_read_and_write.txt")

    # Try reading the hypergraph that was just written into a new hypergraph
    new_H = UndirectedHypergraph()
    new_H.read("test_undirected_read_and_write.txt")

    assert H._node_attributes.keys() == new_H._node_attributes.keys()

    for new_hyperedge_id in new_H.get_hyperedge_id_set():
        new_hyperedge_nodes = new_H.get_hyperedge_nodes(new_hyperedge_id)
        new_hyperedge_weight = new_H.get_hyperedge_weight(new_hyperedge_id)

        found_matching_hyperedge = False
        for hyperedge_id in H.get_hyperedge_id_set():
            hyperedge_nodes = H.get_hyperedge_nodes(hyperedge_id)
            hyperedge_weight = H.get_hyperedge_weight(hyperedge_id)

            if new_hyperedge_nodes == hyperedge_nodes and \
               new_hyperedge_weight == hyperedge_weight:
                found_matching_hyperedge = True
                continue

        assert found_matching_hyperedge

    remove("test_undirected_read_and_write.txt")

    # Try reading an invalid hypergraph file
    invalid_H = UndirectedHypergraph()
    try:
        invalid_H.read("tests/data/invalid_undirected_hypergraph.txt")
        assert False
    except IOError:
        pass
    except BaseException as e:
        assert False, e
def test_add_node():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}

    # Test adding unadded nodes with various attribute settings
    H = UndirectedHypergraph()
    H.add_node(node_a)
    H.add_node(node_b, source=True)
    H.add_node(node_c, attrib_c)
    H.add_node(node_d, attrib_d, sink=False)

    assert node_a in H._node_attributes
    assert H._node_attributes[node_a] == {}

    assert node_b in H._node_attributes
    assert H._node_attributes[node_b]['source'] is True

    assert node_c in H._node_attributes
    assert H._node_attributes[node_c]['alt_name'] == 1337

    assert node_d in H._node_attributes
    assert H._node_attributes[node_d]['label'] == 'black'
    assert H._node_attributes[node_d]['sink'] is False

    # Test adding a node that has already been added
    H.add_nodes(node_a, common=False)
    assert H._node_attributes[node_a]['common'] is False

    # Pass in bad (non-dict) attribute
    try:
        H.add_node(node_a, ["label", "black"])
        assert False
    except AttributeError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 47
0
def test_get_node_attributes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}

    # Test adding unadded nodes with various attribute settings
    H = UndirectedHypergraph()
    H.add_node(node_a)
    H.add_node(node_b, source=True)
    H.add_node(node_c, attrib_c)
    H.add_node(node_d, attrib_d, sink=False)

    attrs = H.get_node_attributes(node_b)
    assert attrs['source'] is True

    # Try requesting an invalid node
    try:
        H.get_node_attributes("E")
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Esempio n. 48
0
def test_add_node():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    node_d = 'D'
    attrib_d = {'label': 'black', 'sink': True}

    # Test adding unadded nodes with various attribute settings
    H = UndirectedHypergraph()
    H.add_node(node_a)
    H.add_node(node_b, source=True)
    H.add_node(node_c, attrib_c)
    H.add_node(node_d, attrib_d, sink=False)

    assert node_a in H._node_attributes
    assert H._node_attributes[node_a] == {}

    assert node_b in H._node_attributes
    assert H._node_attributes[node_b]['source'] is True

    assert node_c in H._node_attributes
    assert H._node_attributes[node_c]['alt_name'] == 1337

    assert node_d in H._node_attributes
    assert H._node_attributes[node_d]['label'] == 'black'
    assert H._node_attributes[node_d]['sink'] is False

    # Test adding a node that has already been added
    H.add_nodes(node_a, common=False)
    assert H._node_attributes[node_a]['common'] is False

    # Pass in bad (non-dict) attribute
    try:
        H.add_node(node_a, ["label", "black"])
        assert False
    except AttributeError:
        pass
    except BaseException as e:
        assert False, e