def to_graph_decomposition(H):
    """Returns a DirectedHypergraph object that has the same nodes (and
    corresponding attributes) as the given hypergraph, except that for all
    hyperedges in the given hypergraph, each node in the tail of the hyperedge
    is pairwise connected to each node in the head of the hyperedge in the
    new hypergraph.
    Said another way, each of the original hyperedges are decomposed in the
    new hypergraph into fully-connected bipartite components.

    :param H: the hypergraph to decompose into a graph.
    :returns: DirectedHypergraph -- the decomposed hypergraph.
    :raises: TypeError -- Transformation only applicable to
            directed hypergraphs

    """
    if not isinstance(H, DirectedHypergraph):
        raise TypeError("Transformation only applicable to \
                        directed hypergraphs")

    G = DirectedHypergraph()

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

    edges = [([tail_node], [head_node])
             for hyperedge_id in H.hyperedge_id_iterator()
             for tail_node in H.get_hyperedge_tail(hyperedge_id)
             for head_node in H.get_hyperedge_head(hyperedge_id)]
    G.add_hyperedges(edges)

    return G
def to_graph_decomposition(H):
    """Returns a DirectedHypergraph object that has the same nodes (and
    corresponding attributes) as the given hypergraph, except that for all
    hyperedges in the given hypergraph, each node in the tail of the hyperedge
    is pairwise connected to each node in the head of the hyperedge in the
    new hypergraph.
    Said another way, each of the original hyperedges are decomposed in the
    new hypergraph into fully-connected bipartite components.

    :param H: the hypergraph to decompose into a graph.
    :returns: DirectedHypergraph -- the decomposed hypergraph.
    :raises: TypeError -- Transformation only applicable to
            directed hypergraphs

    """
    if not isinstance(H, DirectedHypergraph):
        raise TypeError("Transformation only applicable to \
                        directed hypergraphs")

    G = DirectedHypergraph()

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

    edges = [([tail_node], [head_node])
             for hyperedge_id in H.hyperedge_id_iterator()
             for tail_node in H.get_hyperedge_tail(hyperedge_id)
             for head_node in H.get_hyperedge_head(hyperedge_id)]
    G.add_hyperedges(edges)

    return G
Beispiel #3
0
def get_hypertree_from_predecessors(H,
                                    Pv,
                                    source_node,
                                    node_weights=None,
                                    attr_name="weight"):
    """Gives the hypertree (i.e., the subhypergraph formed from the union of
    the set of paths from an execution of, e.g., the SBT algorithm) defined by
    Pv beginning at a source node. Returns a dictionary mapping each node to
    the ID of the hyperedge that preceeded it in the path (i.e., a Pv vector).
    Assigns the node weights (if provided) as attributes of the nodes (e.g.,
    the rank of that node in a specific instance of the SBT algorithm, or the
    cardinality of that node in a B-Visit traversal, etc.).

    :note: The IDs of the hyperedges in the subhypergraph returned may be
        different than those in the original hypergraph (even though the
        tail and head sets are identical).

    :param H: the hypergraph which the path algorithm was executed on.
    :param Pv: dictionary mapping each node to the ID of the hyperedge that
            preceeded it in the path.
    :param source_node: the root of the executed path algorithm.
    :param node_weights: [optional] dictionary mapping each node to some weight
                        measure.
    :param attr_name: key into the nodes' attribute dictionaries for their
                    weight values (if node_weights is provided).
    :returns: DirectedHypergraph -- subhypergraph induced by the path
            algorithm specified by the predecessor vector (Pv) from a
            source node.
    :raises: TypeError -- Algorithm only applicable to directed hypergraphs

    """
    if not isinstance(H, DirectedHypergraph):
        raise TypeError("Algorithm only applicable to directed hypergraphs")

    sub_H = DirectedHypergraph()

    # If node weights are not provided, simply collect all the nodes that are
    # will be in the hypertree
    if node_weights is None:
        nodes = [node for node in Pv.keys() if Pv[node] is not None]
        nodes.append(source_node)
    # If node weights are provided, collect all the nodes that will be in the
    # tree and pair them with their corresponding weights
    else:
        nodes = [(node, {
            attr_name: node_weights[node]
        }) for node in Pv.keys() if Pv[node] is not None]
        nodes.append((source_node, {attr_name: node_weights[source_node]}))
    # Add the collected elements to the hypergraph
    sub_H.add_nodes(nodes)

    # Add all hyperedges, specified by Pv, to the hypergraph
    hyperedges = [(H.get_hyperedge_tail(hyperedge_id),
                   H.get_hyperedge_head(hyperedge_id),
                   H.get_hyperedge_attributes(hyperedge_id))
                  for hyperedge_id in Pv.values() if hyperedge_id is not None]
    sub_H.add_hyperedges(hyperedges)

    return sub_H
Beispiel #4
0
def get_hypertree_from_predecessors(H, Pv, source_node,
                                    node_weights=None, attr_name="weight"):
    """Gives the hypertree (i.e., the subhypergraph formed from the union of
    the set of paths from an execution of, e.g., the SBT algorithm) defined by
    Pv beginning at a source node. Returns a dictionary mapping each node to
    the ID of the hyperedge that preceeded it in the path (i.e., a Pv vector).
    Assigns the node weights (if provided) as attributes of the nodes (e.g.,
    the rank of that node in a specific instance of the SBT algorithm, or the
    cardinality of that node in a B-Visit traversal, etc.).

    :note: The IDs of the hyperedges in the subhypergraph returned may be
        different than those in the original hypergraph (even though the
        tail and head sets are identical).

    :param H: the hypergraph which the path algorithm was executed on.
    :param Pv: dictionary mapping each node to the ID of the hyperedge that
            preceeded it in the path.
    :param source_node: the root of the executed path algorithm.
    :param node_weights: [optional] dictionary mapping each node to some weight
                        measure.
    :param attr_name: key into the nodes' attribute dictionaries for their
                    weight values (if node_weights is provided).
    :returns: DirectedHypergraph -- subhypergraph induced by the path
            algorithm specified by the predecessor vector (Pv) from a
            source node.
    :raises: TypeError -- Algorithm only applicable to directed hypergraphs

    """
    if not isinstance(H, DirectedHypergraph):
        raise TypeError("Algorithm only applicable to directed hypergraphs")

    sub_H = DirectedHypergraph()

    # If node weights are not provided, simply collect all the nodes that are
    # will be in the hypertree
    if node_weights is None:
        nodes = [node for node in Pv.keys() if Pv[node] is not None]
        nodes.append(source_node)
    # If node weights are provided, collect all the nodes that will be in the
    # tree and pair them with their corresponding weights
    else:
        nodes = [(node, {attr_name: node_weights[node]})
                 for node in Pv.keys() if Pv[node] is not None]
        nodes.append((source_node, {attr_name: node_weights[source_node]}))
    # Add the collected elements to the hypergraph
    sub_H.add_nodes(nodes)

    # Add all hyperedges, specified by Pv, to the hypergraph
    hyperedges = [(H.get_hyperedge_tail(hyperedge_id),
                   H.get_hyperedge_head(hyperedge_id),
                   H.get_hyperedge_attributes(hyperedge_id))
                  for hyperedge_id in Pv.values() if hyperedge_id is not None]
    sub_H.add_hyperedges(hyperedges)

    return sub_H
def test_get_hyperedge_weight():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

    H = DirectedHypergraph()
    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 == 6
    assert weight_e2 == 1
def test_get_predecessors():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    hyperedges = [(tail1, head1), (tail2, head2), (tail3, head3), (tail3, "F")]

    H = DirectedHypergraph()
    hyperedge_names = H.add_hyperedges(hyperedges)

    assert 'e1' in H.get_predecessors(head1)
    assert 'e2' in H.get_predecessors(head2)
    assert 'e3' in H.get_predecessors(head3)
    assert 'e4' in H.get_predecessors("F")

    assert H.get_predecessors([node_a]) == set()
def test_get_hyperedge_weight():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

    H = DirectedHypergraph()
    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 == 6
    assert weight_e2 == 1
def test_get_predecessors():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    hyperedges = [(tail1, head1), (tail2, head2), (tail3, head3), (tail3, "F")]

    H = DirectedHypergraph()
    hyperedge_names = H.add_hyperedges(hyperedges)

    assert 'e1' in H.get_predecessors(head1)
    assert 'e2' in H.get_predecessors(head2)
    assert 'e3' in H.get_predecessors(head3)
    assert 'e4' in H.get_predecessors("F")

    assert H.get_predecessors([node_a]) == set()
def test_get_symmetric_image():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    hyperedges = [(tail1, head1), (tail2, head2), (tail3, head3)]

    H = DirectedHypergraph()
    hyperedge_names = H.add_hyperedges(hyperedges)

    sym_H = H.get_symmetric_image()

    sym_H._check_consistency()

    assert sym_H._node_attributes == H._node_attributes

    assert sym_H._hyperedge_attributes["e1"]["tail"] == head1
    assert sym_H._hyperedge_attributes["e1"]["head"] == tail1
    assert sym_H._hyperedge_attributes["e1"]["__frozen_tail"] == frozen_head1
    assert sym_H._hyperedge_attributes["e1"]["__frozen_head"] == frozen_tail1
    assert sym_H._hyperedge_attributes["e2"]["tail"] == head2
    assert sym_H._hyperedge_attributes["e2"]["head"] == tail2
    assert sym_H._hyperedge_attributes["e2"]["__frozen_tail"] == frozen_head2
    assert sym_H._hyperedge_attributes["e2"]["__frozen_head"] == frozen_tail2
    assert sym_H._hyperedge_attributes["e3"]["tail"] == head3
    assert sym_H._hyperedge_attributes["e3"]["head"] == tail3
    assert sym_H._hyperedge_attributes["e3"]["__frozen_tail"] == frozen_head3
    assert sym_H._hyperedge_attributes["e3"]["__frozen_head"] == frozen_tail3

    assert sym_H._forward_star[node_a] == set(["e2"])
    assert sym_H._forward_star[node_b] == set()
    assert sym_H._forward_star[node_c] == set(["e1"])
    assert sym_H._forward_star[node_d] == set(["e1", "e2"])
    assert sym_H._forward_star[node_e] == set(["e3"])

    assert sym_H._backward_star[node_a] == set(["e1"])
    assert sym_H._backward_star[node_b] == set(["e1", "e2"])
    assert sym_H._backward_star[node_c] == set(["e2"])
    assert sym_H._backward_star[node_d] == set(["e3"])
    assert sym_H._backward_star[node_e] == set()
def test_get_symmetric_image():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    hyperedges = [(tail1, head1), (tail2, head2), (tail3, head3)]

    H = DirectedHypergraph()
    hyperedge_names = H.add_hyperedges(hyperedges)

    sym_H = H.get_symmetric_image()

    sym_H._check_consistency()

    assert sym_H._node_attributes == H._node_attributes

    assert sym_H._hyperedge_attributes["e1"]["tail"] == head1
    assert sym_H._hyperedge_attributes["e1"]["head"] == tail1
    assert sym_H._hyperedge_attributes["e1"]["__frozen_tail"] == frozen_head1
    assert sym_H._hyperedge_attributes["e1"]["__frozen_head"] == frozen_tail1
    assert sym_H._hyperedge_attributes["e2"]["tail"] == head2
    assert sym_H._hyperedge_attributes["e2"]["head"] == tail2
    assert sym_H._hyperedge_attributes["e2"]["__frozen_tail"] == frozen_head2
    assert sym_H._hyperedge_attributes["e2"]["__frozen_head"] == frozen_tail2
    assert sym_H._hyperedge_attributes["e3"]["tail"] == head3
    assert sym_H._hyperedge_attributes["e3"]["head"] == tail3
    assert sym_H._hyperedge_attributes["e3"]["__frozen_tail"] == frozen_head3
    assert sym_H._hyperedge_attributes["e3"]["__frozen_head"] == frozen_tail3

    assert sym_H._forward_star[node_a] == set(["e2"])
    assert sym_H._forward_star[node_b] == set()
    assert sym_H._forward_star[node_c] == set(["e1"])
    assert sym_H._forward_star[node_d] == set(["e1", "e2"])
    assert sym_H._forward_star[node_e] == set(["e3"])

    assert sym_H._backward_star[node_a] == set(["e1"])
    assert sym_H._backward_star[node_b] == set(["e1", "e2"])
    assert sym_H._backward_star[node_c] == set(["e2"])
    assert sym_H._backward_star[node_d] == set(["e3"])
    assert sym_H._backward_star[node_e] == set()
def test_remove_nodes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    tail4 = set([node_c])
    head4 = set([node_e])
    frozen_tail4 = frozenset(tail4)
    frozen_head4 = frozenset(head4)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib),
                  (tail2, head2),
                  (tail3, head3),
                  (tail4, head4)]

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

    # Test that everything that needed to be removed was removed
    assert node_a not in H._node_attributes
    assert node_a not in H._forward_star
    assert node_a not in H._backward_star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_tail1 not in H._successors
    assert frozen_head1 not in H._predecessors
    assert frozen_head2 not in H._predecessors
    assert frozen_tail2 not in H._successors

    assert node_d not in H._node_attributes
    assert node_d not in H._forward_star
    assert node_d not in H._backward_star
    assert "e3" not in H._hyperedge_attributes
    assert "e3" not in H._predecessors[frozen_head3]
    assert frozen_tail3 not in H._predecessors[frozen_head3]
def test_remove_nodes():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    tail4 = set([node_c])
    head4 = set([node_e])
    frozen_tail4 = frozenset(tail4)
    frozen_head4 = frozenset(head4)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3),
                  (tail4, head4)]

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

    # Test that everything that needed to be removed was removed
    assert node_a not in H._node_attributes
    assert node_a not in H._forward_star
    assert node_a not in H._backward_star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_tail1 not in H._successors
    assert frozen_head1 not in H._predecessors
    assert frozen_head2 not in H._predecessors
    assert frozen_tail2 not in H._successors

    assert node_d not in H._node_attributes
    assert node_d not in H._forward_star
    assert node_d not in H._backward_star
    assert "e3" not in H._hyperedge_attributes
    assert "e3" not in H._predecessors[frozen_head3]
    assert frozen_tail3 not in H._predecessors[frozen_head3]
def test_remove_hyperedge():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3)]

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

    assert 'e1' not in H._hyperedge_attributes
    assert frozen_tail1 not in H._successors
    assert frozen_head1 not in H._predecessors
    assert 'e1' not in H._forward_star[node_a]
    assert 'e1' not in H._forward_star[node_b]
    assert 'e1' not in H._backward_star[node_c]
    assert 'e1' not in H._backward_star[node_d]

    try:
        H.remove_hyperedge('e1')
        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'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3)]

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

    assert 'e1' not in H._hyperedge_attributes
    assert frozen_tail1 not in H._successors
    assert frozen_head1 not in H._predecessors
    assert 'e1' not in H._forward_star[node_a]
    assert 'e1' not in H._forward_star[node_b]
    assert 'e1' not in H._backward_star[node_c]
    assert 'e1' not in H._backward_star[node_d]

    try:
        H.remove_hyperedge('e1')
        assert False
    except ValueError:
        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'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    assert H.get_hyperedge_attribute('e1', 'weight') == 6
    assert H.get_hyperedge_attribute('e1', 'color') == 'black'
    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_attribute():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    assert H.get_hyperedge_attribute('e1', 'weight') == 6
    assert H.get_hyperedge_attribute('e1', 'color') == 'black'
    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_add_hyperedges():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

    H = DirectedHypergraph()
    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']['tail'] == tail1
    assert H._hyperedge_attributes['e1']['head'] == head1
    assert H._hyperedge_attributes['e1']['weight'] == 6
    assert H._hyperedge_attributes['e1']['color'] == 'black'
    assert H._hyperedge_attributes['e1']['sink'] is False

    assert H._hyperedge_attributes['e2']['tail'] == tail2
    assert H._hyperedge_attributes['e2']['head'] == head2
    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()
    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'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

    H = DirectedHypergraph()
    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']['tail'] == tail1
    assert H._hyperedge_attributes['e1']['head'] == head1
    assert H._hyperedge_attributes['e1']['weight'] == 6
    assert H._hyperedge_attributes['e1']['color'] == 'black'
    assert H._hyperedge_attributes['e1']['sink'] is False

    assert H._hyperedge_attributes['e2']['tail'] == tail2
    assert H._hyperedge_attributes['e2']['head'] == head2
    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()
    for hyperedge_id in H.hyperedge_id_iterator():
        assert hyperedge_id in hyperedge_names
def test_get_hyperedge_id():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3)]

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

    assert H.get_hyperedge_id(tail1, head1) == 'e1'
    assert H.get_hyperedge_id(tail2, head2) == 'e2'
    assert H.get_hyperedge_id(tail3, head3) == 'e3'

    try:
        H.get_hyperedge_id(tail1, head2)
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_copy():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

    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._backward_star == H._backward_star
    assert new_H._forward_star == H._forward_star

    assert new_H._successors == H._successors
    assert new_H._predecessors == H._predecessors
def test_copy():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

    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._backward_star == H._backward_star
    assert new_H._forward_star == H._forward_star

    assert new_H._successors == H._successors
    assert new_H._predecessors == H._predecessors
def test_get_hyperedge_id():
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    node_d = 'D'
    node_e = 'E'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3)]

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

    assert H.get_hyperedge_id(tail1, head1) == 'e1'
    assert H.get_hyperedge_id(tail2, head2) == 'e2'
    assert H.get_hyperedge_id(tail3, head3) == 'e3'

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

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    hyperedges = [(tail1, head1), (tail2, head2), (tail3, head3)]

    H = DirectedHypergraph()
    hyperedge_names = H.add_hyperedges(hyperedges)

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

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

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    hyperedges = [(tail1, head1), (tail2, head2), (tail3, head3)]

    H = DirectedHypergraph()
    hyperedge_names = H.add_hyperedges(hyperedges)

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

    # Try requesting an invalid node
    try:
        H.get_backward_star("F")
        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'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    H.write("test_directed_read_and_write.txt")

    # Try reading the hypergraph that was just written into a new hypergraph
    new_H = DirectedHypergraph()
    new_H.read("test_directed_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_tail = new_H.get_hyperedge_tail(new_hyperedge_id)
        new_hyperedge_head = new_H.get_hyperedge_head(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_tail = H.get_hyperedge_tail(hyperedge_id)
            hyperedge_head = H.get_hyperedge_head(hyperedge_id)
            hyperedge_weight = H.get_hyperedge_weight(hyperedge_id)

            if new_hyperedge_tail == hyperedge_tail and \
               new_hyperedge_head == hyperedge_head and \
               new_hyperedge_weight == hyperedge_weight:
                found_matching_hyperedge = True
                continue

        assert found_matching_hyperedge

    remove("test_directed_read_and_write.txt")

    # Try reading an invalid hypergraph file
    invalid_H = DirectedHypergraph()
    try:
        invalid_H.read("tests/data/invalid_directed_hypergraph.txt")
        assert False
    except IOError:
        pass
    except BaseException as e:
        assert False, e
def test_check_node_consistency():
    # make test hypergraph
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    # This should not fail
    H._check_consistency()

    # Check 5.1
    new_H = H.copy()
    new_H._forward_star["X"] = {}
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.2
    new_H = H.copy()
    new_H._backward_star["X"] = {}
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.3.1
    new_H = H.copy()
    new_H.add_hyperedge("X", "Y")
    del new_H._node_attributes["X"]
    del new_H._forward_star["X"]
    del new_H._forward_star["Y"]
    del new_H._backward_star["X"]
    del new_H._backward_star["Y"]
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.3.2
    new_H = H.copy()
    new_H.add_hyperedge("X", "Y")
    del new_H._node_attributes["Y"]
    del new_H._forward_star["X"]
    del new_H._forward_star["Y"]
    del new_H._backward_star["X"]
    del new_H._backward_star["Y"]
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.4
    new_H = H.copy()
    new_H._predecessors[frozenset("X")] = {}
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.5
    new_H = H.copy()
    new_H._successors[frozenset("X")] = {}
    new_H._predecessors[frozenset("X")] = {}
    try:
        new_H._check_node_consistency()
        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'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3)]

    H = DirectedHypergraph()
    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._forward_star
    assert node_a not in H._backward_star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_tail1 not in H._successors
    assert frozen_head1 not in H._predecessors
    assert frozen_head2 not in H._predecessors
    assert frozen_tail2 not in H._successors

    # Test that everything that wasn't supposed to be removed wasn't removed
    assert "e3" in H._hyperedge_attributes
    assert frozen_tail3 in H._successors
    assert frozen_head3 in H._predecessors

    # Remove another node
    H.remove_node(node_e)
    assert node_e not in H._node_attributes
    assert node_e not in H._forward_star
    assert node_e not in H._backward_star
    assert "e3" not in H._hyperedge_attributes
    assert frozen_head3 not in H._predecessors
    assert frozen_tail3 not in H._successors

    try:
        H.remove_node(node_a)
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_check_hyperedge_attributes_consistency():
    # make test hypergraph
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    # This should not fail
    H._check_consistency()

    # The following consistency checks should fail
    # Check 1.1
    new_H = H.copy()
    del new_H._hyperedge_attributes["e1"]["weight"]
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.2
    new_H = H.copy()
    new_H._hyperedge_attributes["e1"]["tail"] = head1
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.3
    new_H = H.copy()
    new_H._hyperedge_attributes["e1"]["head"] = tail1
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.4
    new_H = H.copy()
    del new_H._successors[frozen_tail1]
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.5
    new_H = H.copy()
    del new_H._predecessors[frozen_head1]
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.6
    new_H = H.copy()
    new_H._forward_star["A"].pop()
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.7
    new_H = H.copy()
    new_H._backward_star["C"].pop()
    try:
        new_H._check_consistency()
        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'

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    tail3 = set([node_d])
    head3 = set([node_e])
    frozen_tail3 = frozenset(tail3)
    frozen_head3 = frozenset(head3)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2), (tail3, head3)]

    H = DirectedHypergraph()
    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._forward_star
    assert node_a not in H._backward_star
    assert "e1" not in H._hyperedge_attributes
    assert "e2" not in H._hyperedge_attributes
    assert frozen_tail1 not in H._successors
    assert frozen_head1 not in H._predecessors
    assert frozen_head2 not in H._predecessors
    assert frozen_tail2 not in H._successors

    # Test that everything that wasn't supposed to be removed wasn't removed
    assert "e3" in H._hyperedge_attributes
    assert frozen_tail3 in H._successors
    assert frozen_head3 in H._predecessors

    # Remove another node
    H.remove_node(node_e)
    assert node_e not in H._node_attributes
    assert node_e not in H._forward_star
    assert node_e not in H._backward_star
    assert "e3" not in H._hyperedge_attributes
    assert frozen_head3 not in H._predecessors
    assert frozen_tail3 not in H._successors

    try:
        H.remove_node(node_a)
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
def test_check_node_consistency():
    # make test hypergraph
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    # This should not fail
    H._check_consistency()

    # Check 5.1
    new_H = H.copy()
    new_H._forward_star["X"] = {}
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.2
    new_H = H.copy()
    new_H._backward_star["X"] = {}
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.3.1
    new_H = H.copy()
    new_H.add_hyperedge("X", "Y")
    del new_H._node_attributes["X"]
    del new_H._forward_star["X"]
    del new_H._forward_star["Y"]
    del new_H._backward_star["X"]
    del new_H._backward_star["Y"]
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.3.2
    new_H = H.copy()
    new_H.add_hyperedge("X", "Y")
    del new_H._node_attributes["Y"]
    del new_H._forward_star["X"]
    del new_H._forward_star["Y"]
    del new_H._backward_star["X"]
    del new_H._backward_star["Y"]
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.4
    new_H = H.copy()
    new_H._predecessors[frozenset("X")] = {}
    try:
        new_H._check_node_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 5.5
    new_H = H.copy()
    new_H._successors[frozenset("X")] = {}
    new_H._predecessors[frozenset("X")] = {}
    try:
        new_H._check_node_consistency()
        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'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    H.write("test_directed_read_and_write.txt")

    # Try reading the hypergraph that was just written into a new hypergraph
    new_H = DirectedHypergraph()
    new_H.read("test_directed_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_tail = new_H.get_hyperedge_tail(new_hyperedge_id)
        new_hyperedge_head = new_H.get_hyperedge_head(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_tail = H.get_hyperedge_tail(hyperedge_id)
            hyperedge_head = H.get_hyperedge_head(hyperedge_id)
            hyperedge_weight = H.get_hyperedge_weight(hyperedge_id)

            if new_hyperedge_tail == hyperedge_tail and \
               new_hyperedge_head == hyperedge_head and \
               new_hyperedge_weight == hyperedge_weight:
                found_matching_hyperedge = True
                continue

        assert found_matching_hyperedge

    remove("test_directed_read_and_write.txt")

    # Try reading an invalid hypergraph file
    invalid_H = DirectedHypergraph()
    try:
        invalid_H.read("tests/data/invalid_directed_hypergraph.txt")
        assert False
    except IOError:
        pass
    except BaseException as e:
        assert False, e
def test_check_hyperedge_attributes_consistency():
    # make test hypergraph
    node_a = 'A'
    node_b = 'B'
    node_c = 'C'
    attrib_c = {'alt_name': 1337}
    common_attrib = {'common': True, 'source': False}

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

    node_d = 'D'

    H = DirectedHypergraph()
    H.add_nodes(node_list, common_attrib)

    tail1 = set([node_a, node_b])
    head1 = set([node_c, node_d])
    frozen_tail1 = frozenset(tail1)
    frozen_head1 = frozenset(head1)

    tail2 = set([node_b, node_c])
    head2 = set([node_d, node_a])
    frozen_tail2 = frozenset(tail2)
    frozen_head2 = frozenset(head2)

    attrib = {'weight': 6, 'color': 'black'}
    common_attrib = {'sink': False}

    hyperedges = [(tail1, head1, attrib), (tail2, head2)]

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

    # This should not fail
    H._check_consistency()

    # The following consistency checks should fail
    # Check 1.1
    new_H = H.copy()
    del new_H._hyperedge_attributes["e1"]["weight"]
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.2
    new_H = H.copy()
    new_H._hyperedge_attributes["e1"]["tail"] = head1
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.3
    new_H = H.copy()
    new_H._hyperedge_attributes["e1"]["head"] = tail1
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.4
    new_H = H.copy()
    del new_H._successors[frozen_tail1]
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.5
    new_H = H.copy()
    del new_H._predecessors[frozen_head1]
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.6
    new_H = H.copy()
    new_H._forward_star["A"].pop()
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e

    # Check 1.7
    new_H = H.copy()
    new_H._backward_star["C"].pop()
    try:
        new_H._check_consistency()
        assert False
    except ValueError:
        pass
    except BaseException as e:
        assert False, e
Beispiel #33
0
    'name': " ",
    'phero': 0.6
}), (['B'], ['C'], {
    'name': " ",
    'phero': 0.0
}), (['D', 'E'], ['F'], {
    'name': " ",
    'phero': 0.0
}), (['E', 'H'], ['F'], {
    'name': " ",
    'phero': 0.0
}), (['G'], ['H'], {
    'name': " ",
    'phero': 0.0
}), (['H'], ['F'], {
    'name': " ",
    'phero': 0.0
}), (['C'], ['F'], {
    'name': " ",
    'phero': 0.0
})]
H.add_hyperedges(hyperedges)

#edges = H.get_forward_star('A')
# for edge in edges:
#     printHyperedge(edge, H)
# print("Choosing hyperedge.....")
# printHyperedge(pheroChoice(edges, H), H)

aco_algorithm(['A'], H, 5, 5, 0.77)
# Add nodes 's' and 't' individually with arbitrary attributes
H.add_node('A', sink=False, source=True, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('B', sink=False, source=False, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('C', sink=False, source=False, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('D', sink=False, source=False, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('E', sink=False, source=False, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('G', sink=False, source=False, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('H', sink=False, source=False, cost=0.245, avail=0.99, qual=0.7, time=0.78)
H.add_node('F', source=False, sink=True, cost=0.1, avail=0.98, qual=0.4, time=0.45)

hyperedges = [(['A'], ['B'], {'name' : " ", 'phero': 0.3}),
              (['A'], ['D','E'], {'name' : " ", 'phero': 0.1}),
              (['A'], ['G'], {'name' : " ", 'phero': 0.6}),
              (['B'], ['C'], {'name' : " ", 'phero': 0.0}),
              (['D','E'], ['F'], {'name' : " ", 'phero': 0.0}),
              (['E','H'], ['F'], {'name' : " ", 'phero': 0.0}),
              (['G'], ['H'], {'name' : " ", 'phero': 0.0}),
              (['H'], ['F'], {'name' : " ", 'phero': 0.0}),
              (['C'], ['F'], {'name' : " ", 'phero': 0.0})  
        ]
H.add_hyperedges(hyperedges)

#edges = H.get_forward_star('A')
# for edge in edges:
#     printHyperedge(edge, H)
# print("Choosing hyperedge.....")
# printHyperedge(pheroChoice(edges, H), H)

aco_algorithm(['A'], H, 5, 5, 0.77)