def test_pons_asinorum(self): g0 = Graph.with_features( Equation.make_table(range(1, 20), range(1, 20), [plus, minus, times])) g1 = Graph.augment(PrefixedGraph(1, g0), PrefixedGraph(2, g0)) # print('QQ') # qq = list((x1, x1.with_prefix(2)) # for x1 in g1.query(WithPrefix(1, OfClass(After)))) # print(type(qq), type(qq[0]), len(qq[0])) # pts(qq) g = g1.add_edges( EnumEdges( Hops.from_pairs(*( (p_after, PrefixedNode(2, Before(p_after.unprefixed().x))) for p_after in g1.query(WithPrefix(1, OfClass(After))))))) #pts(g.query(WithPrefix(1, OfClass(After)))) #pts(g.query(WithPrefix(2, OfClass(Before)))) #pts(g.successors_of(PrefixedNode(1, After(10)))) #print() #pts(g.successors_of(PrefixedNode(1, After(63)))) self.assertTrue( g.find_hop(PrefixedNode(1, After(10)), PrefixedNode(2, Before(10)))) self.assertFalse( g.find_hop(PrefixedNode(1, After(63)), PrefixedNode(2, Before(63))))
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'), [])
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))), [])
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)
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'])
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)