示例#1
0
 def test_enum_hops(self):
     g = Graph(nodes=EnumNodes(['x', 'y', 'z']),
               edges=EnumEdges(Hops.from_dict(x={'y', 'z'})))
     self.assertTrue(g.has_node('y'))
     self.assertFalse(g.has_node('a'))
     self.assertCountEqual(g.predecessors_of('y'), ['x'])
     self.assertCountEqual(g.predecessors_of('x'), [])
示例#2
0
 def test_graph_propagator_incoming(self):
     g = Graph(nodes=EnumNodes(['a', 'b', 'o']),
               edges=EnumEdges(Hops.from_pairs(('a', 'b'), ('b', 'o'))))
     p = GraphPropagatorIncoming(positive_feedback_rate=0.2,
                                 alpha=0.9,
                                 max_total=1.0,
                                 noise=0.0)
     in_d = dict(a=1.0, b=0.5, o=0.0)
     out_d = p.propagate(g, in_d)
     self.assertAlmostEqual(out_d['a'], 0.569072143062159)
     self.assertAlmostEqual(out_d['b'], 0.319848331226608)
     self.assertAlmostEqual(out_d['o'], 0.11107952571123292)
示例#3
0
    def test_augment(self):
        g1 = AlphabetGraph()
        g2 = Graph(nodes=EnumNodes(range(1, 4)),
                   edges=EnumEdges(
                       Hops.from_pairs((1, 'a'), (2, 'b'), (3, 'c'))))
        g = Graph.augment(g2, g1)

        self.assertTrue(g2.has_node(1))
        self.assertFalse(g2.has_node('a'))
        self.assertFalse(g2.find_hop(1, 'a'))
        self.assertCountEqual(g2.successors_of(2), [])
        self.assertCountEqual(g2.predecessors_of('c'), [])

        self.assertTrue(g.has_node(1))
        self.assertTrue(g.has_node('a'))
        self.assertTrue(g.find_hop(1, 'a'))
        self.assertCountEqual(g.successors_of(2), ['b'])
        self.assertCountEqual(g.predecessors_of('b'), [2, 'a'])
示例#4
0
    def test_doubled_graph(self):
        g0 = Graph(nodes=EnumNodes(['a', 'b', 'o']),
                   edges=EnumEdges(Hops.from_pairs(('a', 'b'), ('b', 'o'))))
        g = Graph.augment(PrefixedGraph(1, g0), PrefixedGraph(2, g0))

        self.assertTrue(g.has_node(PrefixedNode(1, 'a')))
        self.assertTrue(g.has_node(PrefixedNode(2, 'a')))
        self.assertFalse(g.has_node(PrefixedNode(3, 'a')))
        self.assertFalse(g.has_node('a'))
        self.assertEqual(
            g.find_hop(PrefixedNode(1, 'a'), PrefixedNode(1, 'b')),
            Hop(PrefixedNode(1, 'a'), PrefixedNode(1, 'b'), 1.0))
        self.assertEqual(
            g.find_hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'b')),
            Hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'b'), 1.0))
        self.assertEqual(
            g.find_hop(PrefixedNode(1, 'a'), PrefixedNode(2, 'b')), None)
        self.assertEqual(g.find_hop('a', 'b'), None)

        # .add_edges() from one PrefixedGraph to the other

        g2 = g.add_edges(
            EnumEdges(
                Hops.from_pairs((PrefixedNode(1, 'a'), PrefixedNode(2, 'a')))))
        self.assertEqual(
            g2.find_hop(PrefixedNode(1, 'a'), PrefixedNode(2, 'a')),
            Hop(PrefixedNode(1, 'a'), PrefixedNode(2, 'a'), 1.0))

        self.assertCountEqual(g2.query(OfClass(str)), [
            PrefixedNode(1, 'a'),
            PrefixedNode(1, 'b'),
            PrefixedNode(1, 'o'),
            PrefixedNode(2, 'a'),
            PrefixedNode(2, 'b'),
            PrefixedNode(2, 'o')
        ])
        self.assertCountEqual(
            g2.query(WithPrefix(2, OfClass(str))),
            [PrefixedNode(2, 'a'),
             PrefixedNode(2, 'b'),
             PrefixedNode(2, 'o')])
        self.assertCountEqual(g0.query(WithPrefix(2, OfClass(str))), [])
示例#5
0
 def test_enum_graph(self):
     g = Graph(nodes=EnumNodes(['x', 'y', 'z']), edges=EnumEdges())
     self.assertTrue(g.has_node('y'))
     self.assertFalse(g.has_node('a'))
     self.assertCountEqual(g.predecessors_of('y'), [])
示例#6
0
    def test_prefix(self):
        g0 = Graph(nodes=EnumNodes(['a', 'b', 'o']),
                   edges=EnumEdges(Hops.from_pairs(('a', 'b'), ('b', 'o'))))
        g = PrefixedGraph(2, g0)

        # methods that go to Nodes

        self.assertFalse(g.has_node('a'))
        self.assertTrue(g.has_node(PrefixedNode(2, 'a')))
        self.assertFalse(g.has_node(PrefixedNode(1, 'a')))

        self.assertCountEqual(
            g.query(OfClass(str)),
            [PrefixedNode(2, 'a'),
             PrefixedNode(2, 'b'),
             PrefixedNode(2, 'o')])

        # methods that go to Edges

        # .hops_from_node()
        self.assertCountEqual(g.hops_from_node('a'), [])
        self.assertCountEqual(g.hops_from_node(PrefixedNode(
            2, 'a')), [Hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'b'), 1.0)])
        self.assertCountEqual(g.hops_from_node(PrefixedNode(1, 'a')), [])

        # .hops_to_node()
        self.assertCountEqual(g.hops_to_node('b'), [])
        self.assertCountEqual(g.hops_to_node(PrefixedNode(
            2, 'b')), [Hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'b'), 1.0)])
        self.assertCountEqual(g.hops_to_node(PrefixedNode(1, 'b')), [])

        # .find_hop()
        self.assertIsNone(g.find_hop('a', 'b'))
        self.assertEqual(
            g.find_hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'b')),
            Hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'b'), 1.0))
        self.assertEqual(
            g.find_hop(PrefixedNode(1, 'a'), PrefixedNode(2, 'b')), None)
        self.assertEqual(
            g.find_hop(PrefixedNode(2, 'a'), PrefixedNode(1, 'b')), None)

        # .degree_out() and .degree_in()
        self.assertEqual(g.degree_out('a'), 0)
        self.assertEqual(g.degree_out(PrefixedNode(1, 'a')), 0)
        self.assertEqual(g.degree_out(PrefixedNode(2, 'a')), 1)
        self.assertEqual(g.degree_in('b'), 0)
        self.assertEqual(g.degree_in(PrefixedNode(1, 'b')), 0)
        self.assertEqual(g.degree_in(PrefixedNode(2, 'b')), 1)

        # .successors_of() and .predecessors_of()
        self.assertCountEqual(g.successors_of('a'), [])
        self.assertCountEqual(g.successors_of(PrefixedNode(1, 'a')), [])
        self.assertCountEqual(g.successors_of(PrefixedNode(2, 'a')),
                              [PrefixedNode(2, 'b')])
        self.assertCountEqual(g.predecessors_of(PrefixedNode(2, 'b')),
                              [PrefixedNode(2, 'a')])

        # .hop_weight()
        self.assertEqual(
            g.hop_weight(PrefixedNode(2, 'a'), PrefixedNode(2, 'b')), 1.0)
        self.assertEqual(
            g.hop_weight(PrefixedNode(1, 'a'), PrefixedNode(2, 'b')), 0.0)
        self.assertEqual(
            g.hop_weight(PrefixedNode(2, 'a'), PrefixedNode(1, 'b')), 0.0)

        # .add_edges()

        g2 = g.add_edges(
            EnumEdges(
                Hops.from_pairs((PrefixedNode(2, 'a'), PrefixedNode(2, 'o')))))
        self.assertEqual(
            g2.find_hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'o')),
            Hop(PrefixedNode(2, 'a'), PrefixedNode(2, 'o'), 1.0))
        self.assertEqual(g2.find_hop('a', 'o'), None)