当前位置: 首页>>代码示例>>Python>>正文


Python directed_hypergraph.DirectedHypergraph类代码示例

本文整理汇总了Python中halp.directed_hypergraph.DirectedHypergraph的典型用法代码示例。如果您正苦于以下问题:Python DirectedHypergraph类的具体用法?Python DirectedHypergraph怎么用?Python DirectedHypergraph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了DirectedHypergraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_from_networkx_digraph

def test_from_networkx_digraph():
    H = DirectedHypergraph()
    H.read("tests/data/basic_directed_hypergraph.txt")

    nxG = directed_graph_transformations.to_networkx_digraph(H)

    G = directed_graph_transformations.from_networkx_digraph(nxG)

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

    assert G_nodes == set(nxG_nodes)

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

    # Try transforming an invalid directed hypergraph
    try:
        directed_graph_transformations.from_networkx_digraph("G")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:Murali-group,项目名称:halp,代码行数:26,代码来源:test_directed_graph_transformations.py

示例2: test_get_successors

def test_get_successors():
    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_successors(tail1)
    assert 'e2' in H.get_successors(tail2)
    assert 'e3' in H.get_successors(tail3)
    assert 'e4' in H.get_successors(tail3)

    assert H.get_successors([node_a]) == set()
开发者ID:Murali-group,项目名称:halp,代码行数:33,代码来源:test_directed_hypergraph.py

示例3: from_networkx_digraph

def from_networkx_digraph(nx_digraph):
    """Returns a DirectedHypergraph object that is the graph equivalent of the
    given NetworkX DiGraph object.

    :param nx_digraph: the NetworkX directed graph object to transform.
    :returns: DirectedHypergraph -- hypergraph object equivalent to the
            NetworkX directed graph.
    :raises: TypeError -- Transformation only applicable to directed
            NetworkX graphs

    """
    import networkx as nx

    if not isinstance(nx_digraph, nx.DiGraph):
        raise TypeError("Transformation only applicable to directed \
                        NetworkX graphs")

    G = DirectedHypergraph()

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

    for edge in nx_digraph.edges_iter():
        tail_node = edge[0]
        head_node = edge[1]
        G.add_hyperedge(tail_node,
                        head_node,
                        copy.copy(nx_digraph[tail_node][head_node]))

    return G
开发者ID:Murali-group,项目名称:halp,代码行数:30,代码来源:directed_graph_transformations.py

示例4: test_raises_exception_if_k_not_integer

 def test_raises_exception_if_k_not_integer(self):
     H = DirectedHypergraph()
     source, destination = 1, 2
     H.add_nodes([source, destination])
     self.assertRaises(TypeError,
                       ksh.k_shortest_hyperpaths,
                       H, source, destination, 0.1)
开发者ID:Murali-group,项目名称:halp,代码行数:7,代码来源:test_k_shortest_hyperpaths.py

示例5: test_stationary_distribution

def test_stationary_distribution():
    H = DirectedHypergraph()
    H.read("./tests/data/basic_directed_hypergraph.txt")

    # Try random-walking a directed hypergraph with a node
    # with no outgoing hyperedges
    try:
        pi = rw.stationary_distribution(H)
        assert False
    except AssertionError:
        pass
    except BaseException as e:
        assert False, e

    # Try random-walking a valid directed hypergraph
    H.add_hyperedge(["u"], ["u"], weight=1)
    pi = rw.stationary_distribution(H)

    # Correctness tests go here

    # Try partitioning an invalid directed hypergraph
    try:
        pi = rw.stationary_distribution("H")
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:annaritz,项目名称:halp,代码行数:28,代码来源:test_directed_random_walk.py

示例6: test_raises_exception_if_H_not_B_hypegraph

 def test_raises_exception_if_H_not_B_hypegraph(self):
     H = DirectedHypergraph()
     H.add_nodes([1, 2, 3])
     H.add_hyperedge([1], [2, 3])
     source, destination = 1, 2
     self.assertRaises(TypeError,
                       ksh.k_shortest_hyperpaths, H, source, destination, 1)
开发者ID:Murali-group,项目名称:halp,代码行数:7,代码来源:test_k_shortest_hyperpaths.py

示例7: test_add_nodes

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 = DirectedHypergraph()
    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
开发者ID:Murali-group,项目名称:halp,代码行数:34,代码来源:test_directed_hypergraph.py

示例8: test_get_hyperedge_weight

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
开发者ID:Murali-group,项目名称:halp,代码行数:29,代码来源:test_directed_hypergraph.py

示例9: test_visit

def test_visit():
    H = DirectedHypergraph()
    H.read("tests/data/basic_directed_hypergraph.txt")

    visited_nodes, Pv, Pe = directed_paths.visit(H, 's')

    assert visited_nodes == set(['s', 'x', 'y', 'z', 'u', 't', 'a'])

    assert Pv['s'] is None
    assert (Pe['e1'], Pe['e2'], Pe['e3']) == ('s', 's', 's')
    assert Pv['x'] in ('e1', 'e2')
    assert Pv['y'] == 'e2'
    assert Pv['z'] == 'e3'
    assert Pe['e4'] in ('x', 'y', 'z')
    assert Pv['u'] == 'e4'
    assert Pv['t'] == 'e8'
    assert Pv['a'] == 'e7'
    assert Pe['e5'] == 'a'
    assert Pe['e6'] == 'x'
    assert Pe['e7'] == 't'
    assert Pe['e8'] == 's'
    assert Pv['b'] is None

    try:
        directed_paths.visit('s', 't')
        assert False
    except TypeError:
        pass
    except BaseException as e:
        assert False, e
开发者ID:Murali-group,项目名称:halp,代码行数:30,代码来源:test_directed_paths.py

示例10: test_hyperedge_cardinality_ratio_list

def test_hyperedge_cardinality_ratio_list():
    H = DirectedHypergraph()
    H.read("tests/data/basic_directed_hypergraph.txt")

    ratio_list = [0.5, 1.0, 1.0, 1.0, 2.0, 1.0, 1.5, 0.5]
    returned_list = directed_statistics.hyperedge_cardinality_ratio_list(H)

    assert sorted(ratio_list) == sorted(returned_list)
开发者ID:Murali-group,项目名称:halp,代码行数:8,代码来源:test_directed_statistics.py

示例11: test_returns_hyperpath_containing_source_if_source_equals_destination

 def test_returns_hyperpath_containing_source_if_source_equals_destination(
         self):
     s = '1'
     T = {s: None}
     H = DirectedHypergraph()
     H.add_node(s)
     path = directed_paths.get_hyperpath_from_predecessors(H, T, s, s)
     self.assertTrue(path.has_node(s))
开发者ID:Murali-group,项目名称:halp,代码行数:8,代码来源:test_directed_paths.py

示例12: test_get_symmetric_image

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()
开发者ID:Murali-group,项目名称:halp,代码行数:57,代码来源:test_directed_hypergraph.py

示例13: test_returns_hyperpath_with_single_node_if_source_equals_destination

 def test_returns_hyperpath_with_single_node_if_source_equals_destination(
         self):
     s = '1'
     T = {s: None}
     H = DirectedHypergraph()
     H.add_node(s)
     path = directed_paths.get_hyperpath_from_predecessors(H, T, s, s)
     self.assertEqual(len(path.get_node_set()), 1)
     self.assertEqual(len(path.get_hyperedge_id_set()), 0)
开发者ID:Murali-group,项目名称:halp,代码行数:9,代码来源:test_directed_paths.py

示例14: test_remove_nodes

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]
开发者ID:Murali-group,项目名称:halp,代码行数:57,代码来源:test_directed_hypergraph.py

示例15: test_raises_exception_if_all_nodes_have_predecessors

 def test_raises_exception_if_all_nodes_have_predecessors(self):
     s1, s2, s3 = 1, 2, 3
     H = DirectedHypergraph()
     H.add_nodes([s1, s2, s3])
     e1 = H.add_hyperedge([s1], [s2])
     T = {s1: e1, s2: e1, s3: e1}
     self.assertRaises(ValueError,
                       directed_paths.get_hyperpath_from_predecessors,
                       H, T, s1, s2)
开发者ID:Murali-group,项目名称:halp,代码行数:9,代码来源:test_directed_paths.py


注:本文中的halp.directed_hypergraph.DirectedHypergraph类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。