Example #1
0
    def test_integrity(self):
        graph = Graph()
        initial_node_name = gen_name()
        graph.add_node(initial_node_name,
                       layer=0,
                       position=(0.5, 0.5),
                       label='E')

        [i1, i2] = P1().apply(graph, [initial_node_name])
        [i1_1, i1_2] = P2().apply(graph, [i1])
        [i2_1, i2_2] = P2().apply(graph, [i2])
        [i3_1, i3_2] = P2().apply(graph, [i1_1])

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i4_1, i4_2] = P2().apply(graph, [i1_2])

        self.check_graph_integrity(graph, i1, 'i')
        self.check_graph_integrity(graph, i2, 'i')
        self.check_graph_integrity(graph, i1_1, 'i')
        self.check_graph_integrity(graph, i1_2, 'i')
        self.check_graph_integrity(graph, i2_1, 'I')
        self.check_graph_integrity(graph, i2_2, 'I')
        self.check_graph_integrity(graph, i3_1, 'I')
        self.check_graph_integrity(graph, i3_2, 'I')
        self.check_graph_integrity(graph, i4_1, 'I')
        self.check_graph_integrity(graph, i4_2, 'I')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
Example #2
0
    def test_happy_path(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0.5, 0.5), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P1().apply(graph, [initial_node])

        nodes_data = graph.nodes(data=True)

        self.assertEqual(len(graph.nodes()), 7)
        self.assertEqual(len(graph.edges()), 13)

        # check the initial node
        initial_node_data = nodes_data[initial_node]
        self.assertEqual(initial_node_data['layer'], 0)
        self.assertEqual(initial_node_data['position'], (0.5, 0.5))
        self.assertEqual(initial_node_data['label'], 'e')

        # check other nodes
        vx_bl = get_node_at(graph, 1, (0, 0))
        vx_br = get_node_at(graph, 1, (1, 0))
        vx_tl = get_node_at(graph, 1, (0, 1))
        vx_tr = get_node_at(graph, 1, (1, 1))
        self.assertIsNotNone(vx_bl)
        self.assertIsNotNone(vx_br)
        self.assertIsNotNone(vx_tl)
        self.assertIsNotNone(vx_tr)
        self.assertEqual(nodes_data[vx_bl]['label'], 'E')
        self.assertEqual(nodes_data[vx_br]['label'], 'E')
        self.assertEqual(nodes_data[vx_tl]['label'], 'E')
        self.assertEqual(nodes_data[vx_tr]['label'], 'E')

        vx_i1 = get_node_at(graph, 1, (2 / 3, 1 / 3))
        vx_i2 = get_node_at(graph, 1, (1 / 3, 2 / 3))
        self.assertIsNotNone(vx_i1)
        self.assertIsNotNone(vx_i2)
        self.assertEqual(nodes_data[vx_i1]['label'], 'I')
        self.assertEqual(nodes_data[vx_i2]['label'], 'I')

        self.assertTrue(graph.has_edge(initial_node, vx_i1))
        self.assertTrue(graph.has_edge(initial_node, vx_i2))
        self.assertTrue(graph.has_edge(vx_tl, vx_tr))
        self.assertTrue(graph.has_edge(vx_tr, vx_br))
        self.assertTrue(graph.has_edge(vx_br, vx_bl))
        self.assertTrue(graph.has_edge(vx_bl, vx_tl))
        self.assertTrue(graph.has_edge(vx_bl, vx_tr))
        self.assertTrue(graph.has_edge(vx_i1, vx_bl))
        self.assertTrue(graph.has_edge(vx_i1, vx_br))
        self.assertTrue(graph.has_edge(vx_i1, vx_tr))
        self.assertTrue(graph.has_edge(vx_i2, vx_bl))
        self.assertTrue(graph.has_edge(vx_i2, vx_tl))
        self.assertTrue(graph.has_edge(vx_i2, vx_tr))

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
Example #3
0
    def test_different_position(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0, 0), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        P1().apply(graph, [initial_node],
                   positions=[
                       (0, 0),
                       (2, 1.5),
                       (1.5, 2),
                       (-0.5, 1.5),
                   ])

        # check other nodes
        vx_bl = get_node_at(graph, 1, (0, 0))
        vx_br = get_node_at(graph, 1, (2, 1.5))
        vx_tl = get_node_at(graph, 1, (1.5, 2))
        vx_tr = get_node_at(graph, 1, (-0.5, 1.5))
        self.assertIsNotNone(vx_bl)
        self.assertIsNotNone(vx_br)
        self.assertIsNotNone(vx_tl)
        self.assertIsNotNone(vx_tr)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
def derive_b():
    graph = Graph()
    initial_node_name = gen_name()
    graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    visualize_graph_3d(graph)
    pyplot.show()

    [i1, i2] = P1().apply(graph, [initial_node_name])

    visualize_graph_3d(graph)
    pyplot.show()

    [i1_1, i1_2] = P2().apply(graph, [i1], orientation=1)
    [i2_1] = P9().apply(graph, [i2])

    visualize_graph_3d(graph)
    pyplot.show()

    P10().apply(graph, [i1_1, i1_2, i2_1])

    visualize_graph_3d(graph)
    pyplot.show()

    return graph
Example #5
0
    def run(self, graph, p1_positions):
        assert len(graph.nodes()) == 1
        initial_node_name = list(graph.nodes())[0]
        self.visualize_if_enabled(graph)

        [i1, i2] = P1().apply(graph, [initial_node_name], positions=p1_positions)
        self.visualize_if_enabled(graph)

        [i1_] = P9().apply(graph, [i1])
        self.visualize_if_enabled(graph)

        [i2_] = P9().apply(graph, [i2])
        self.visualize_if_enabled(graph)

        [] = P12().apply(graph, [i1, i2, i1_, i2_])
        self.visualize_if_enabled(graph)

        if self.visualize:
            visualize_graph_layer(graph, 0)
            pyplot.show()

            visualize_graph_layer(graph, 1)
            pyplot.show()

            visualize_graph_layer(graph, 2)
            pyplot.show()
Example #6
0
    def test_wrong_args(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=1, position=(0.5, 0.5), label='E')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaisesRegex(ValueError, 'not enough values to unpack'):
            P1().apply(graph, [])

        self.assertEqual(len(graph.nodes()), 1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
Example #7
0
    def test_wrong_label(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0.5, 0.5), label='e')

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        with self.assertRaisesRegex(ValueError, 'bad label'):
            P1().apply(graph, [initial_node])

        self.assertEqual(len(graph.nodes()), 1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()
def derive_e():
    g = Graph()
    initial_node_name = gen_name()
    g.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    # Layer 1
    [a1, a2] = P1().apply(g, [initial_node_name])

    # Layer 2
    [b3, b2] = P2().apply(g, [a1], orientation=1)
    [b4, b1] = P2().apply(g, [a2], orientation=1)
    P6().apply(g, [a1, a2, b1, b2, b3, b4])

    # Layer 3
    [c3, c2] = P2().apply(g, [b1], orientation=1)
    [i11, c1] = P2().apply(g, [b2])
    [i13, i12] = P2().apply(g, [b3], orientation=1)
    [i14, c4] = P2().apply(g, [b4])
    P12().apply(g, [b3, b4, i13, i14])
    P12().apply(g, [b1, b4, c3, c4])
    P12().apply(g, [b2, b1, c1, c2])
    P13().apply(g, [b2, b3, i11, i12])

    # Layer 4
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [i22, d1] = P2().apply(g, [c1], orientation=1)
    [d3, d2] = P2().apply(g, [c2], orientation=1)
    [d4, d5] = P2().apply(g, [c3], orientation=2)
    [i25, d6] = P2().apply(g, [c4], orientation=2)
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i14, c4, new_i14, i25])
    P12().apply(g, [i11, c1, new_i11, i22])
    P12().apply(g, [c2, c3, d3, d4])
    P6().apply(g, [c1, c2, d1, d2, d3, i22])
    P6().apply(g, [c3, c4, d4, d5, d6, i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    i11 = new_i11
    i12 = new_i12
    i13 = new_i13
    i14 = new_i14

    # Layer 5
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [new_i22] = P9().apply(g, [i22])
    [new_i25] = P9().apply(g, [i25])
    [i21, e1] = P2().apply(g, [d1])
    [e3, e2] = P2().apply(g, [d2], orientation=1)
    [i23, e4] = P2().apply(g, [d3])
    [i24, e5] = P2().apply(g, [d4])
    [e7, e6] = P2().apply(g, [d5], orientation=1)
    [i26, e8] = P2().apply(g, [d6])
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i11, i22, new_i11, new_i22])
    P12().apply(g, [i14, i25, new_i14, new_i25])
    P12().apply(g, [i22, d1, new_i22, i21])
    P12().apply(g, [i25, d6, new_i25, i26])
    P12().apply(g, [d2, d3, e3, e4])
    P12().apply(g, [d1, d2, e1, e2])
    P12().apply(g, [d4, d5, e5, e6])
    P12().apply(g, [d5, d6, e7, e8])
    P6().apply(g, [d3, d4, e4, e5, i23, i24])
    P13().apply(g, [d3, i22, new_i22, i23])
    P13().apply(g, [d4, i25, i24, new_i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    i11 = new_i11
    i12 = new_i12
    i13 = new_i13
    i14 = new_i14
    i22 = new_i22
    i25 = new_i25

    # Layer 6
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [new_i21] = P9().apply(g, [i21])
    [new_i22] = P9().apply(g, [i22])
    [new_i23] = P9().apply(g, [i23])
    [new_i24] = P9().apply(g, [i24])
    [new_i25] = P9().apply(g, [i25])
    [new_i26] = P9().apply(g, [i26])
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i11, i22, new_i11, new_i22])
    P12().apply(g, [i14, i25, new_i14, new_i25])
    P12().apply(g, [i21, i22, new_i21, new_i22])
    P12().apply(g, [i25, i26, new_i25, new_i26])
    [i31, i41] = P2().apply(g, [e1], orientation=1)
    [i42, f1] = P2().apply(g, [e2], orientation=1)
    [i43, f2] = P2().apply(g, [e3], orientation=2)
    [i32, i44] = P2().apply(g, [e4], orientation=2)
    [i33, i45] = P2().apply(g, [e5], orientation=1)
    [i46, f3] = P2().apply(g, [e6], orientation=1)
    [i47, f4] = P2().apply(g, [e7], orientation=2)
    [i34, i48] = P2().apply(g, [e8], orientation=2)
    P6().apply(g, [e1, e2, f1, i41, i42, i31])
    P6().apply(g, [e3, e4, f2, i43, i44, i32])
    P6().apply(g, [e5, e6, f3, i45, i46, i33])
    P6().apply(g, [e7, e8, f4, i47, i48, i34])
    P12().apply(g, [e2, e3, i42, i43])
    P12().apply(g, [e4, e5, i44, i45])
    P12().apply(g, [e6, e7, i46, i47])
    P12().apply(g, [i21, e1, new_i21, i31])
    P12().apply(g, [i23, e4, new_i23, i32])
    P12().apply(g, [i24, e5, new_i24, i33])
    P12().apply(g, [i26, e8, new_i26, i34])
    P13().apply(g, [i22, i23, new_i22, new_i23])
    P13().apply(g, [i23, i24, new_i23, new_i24])
    P13().apply(g, [i24, i25, new_i24, new_i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    i11 = new_i11
    i12 = new_i12
    i13 = new_i13
    i14 = new_i14
    i21 = new_i21
    i22 = new_i22
    i23 = new_i23
    i24 = new_i24
    i25 = new_i25
    i26 = new_i26

    # Layer 7
    [new_i11] = P9().apply(g, [i11])
    [new_i12] = P9().apply(g, [i12])
    [new_i13] = P9().apply(g, [i13])
    [new_i14] = P9().apply(g, [i14])
    [new_i21] = P9().apply(g, [i21])
    [new_i22] = P9().apply(g, [i22])
    [new_i23] = P9().apply(g, [i23])
    [new_i24] = P9().apply(g, [i24])
    [new_i25] = P9().apply(g, [i25])
    [new_i26] = P9().apply(g, [i26])
    [new_i31] = P9().apply(g, [i31])
    [new_i32] = P9().apply(g, [i32])
    [new_i33] = P9().apply(g, [i33])
    [new_i34] = P9().apply(g, [i34])
    [new_i41] = P9().apply(g, [i41])
    [new_i42] = P9().apply(g, [i42])
    [new_i43] = P9().apply(g, [i43])
    [new_i44] = P9().apply(g, [i44])
    [new_i45] = P9().apply(g, [i45])
    [new_i46] = P9().apply(g, [i46])
    [new_i47] = P9().apply(g, [i47])
    [new_i48] = P9().apply(g, [i48])
    P12().apply(g, [i12, i13, new_i12, new_i13])
    P12().apply(g, [i13, i14, new_i13, new_i14])
    P12().apply(g, [i11, i22, new_i11, new_i22])
    P12().apply(g, [i14, i25, new_i14, new_i25])
    P12().apply(g, [i21, i22, new_i21, new_i22])
    P12().apply(g, [i25, i26, new_i25, new_i26])
    P12().apply(g, [i21, i31, new_i21, new_i31])
    P12().apply(g, [i23, i32, new_i23, new_i32])
    P12().apply(g, [i24, i33, new_i24, new_i33])
    P12().apply(g, [i26, i34, new_i26, new_i34])
    P12().apply(g, [i31, i41, new_i31, new_i41])
    P12().apply(g, [i31, i42, new_i31, new_i42])
    P12().apply(g, [i32, i43, new_i32, new_i43])
    P12().apply(g, [i32, i44, new_i32, new_i44])
    P12().apply(g, [i33, i45, new_i33, new_i45])
    P12().apply(g, [i33, i46, new_i33, new_i46])
    P12().apply(g, [i34, i47, new_i34, new_i47])
    P12().apply(g, [i34, i48, new_i34, new_i48])
    P12().apply(g, [i42, i43, new_i42, new_i43])
    P12().apply(g, [i44, i45, new_i44, new_i45])
    P12().apply(g, [i46, i47, new_i46, new_i47])
    [i52, i51] = P2().apply(g, [f1], orientation=1)
    [i54, i53] = P2().apply(g, [f2], orientation=1)
    [i56, i55] = P2().apply(g, [f3], orientation=1)
    [i58, i57] = P2().apply(g, [f4], orientation=1)
    P12().apply(g, [i41, f1, new_i41, i51])
    P12().apply(g, [i43, f2, new_i43, i53])
    P12().apply(g, [i45, f3, new_i45, i55])
    P12().apply(g, [i47, f4, new_i47, i57])
    P13().apply(g, [i22, i23, new_i22, new_i23])
    P13().apply(g, [i23, i24, new_i23, new_i24])
    P13().apply(g, [i24, i25, new_i24, new_i25])
    P13().apply(g, [i11, i12, new_i11, new_i12])
    P13().apply(g, [i42, f1, new_i42, i52])
    P13().apply(g, [i44, f2, new_i44, i54])
    P13().apply(g, [i46, f3, new_i46, i56])
    P13().apply(g, [i48, f4, new_i48, i58])

    return g
Example #9
0
"""
This is an example derivation.
If you want to test something you can use it.
It's better to copy-paste this file as `test.py` in order
not to accidentally commit this file.
"""
from matplotlib import pyplot
from networkx import Graph

from agh_graphs.productions.p1 import P1
from agh_graphs.productions.p2 import P2
from agh_graphs.utils import gen_name
from agh_graphs.visualize import visualize_graph_layer, visualize_graph_3d

if __name__ == '__main__':
    graph = Graph()
    initial_node_name = gen_name()
    graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

    [i1, i2] = P1().apply(graph, [initial_node_name])
    [i1_1, i1_2] = P2().apply(graph, [i1])
    [i2_1, i2_2] = P2().apply(graph, [i2])
    [i3_1, i3_2] = P2().apply(graph, [i1_1])

    visualize_graph_3d(graph)
    pyplot.show()

    visualize_graph_layer(graph, 2)
    pyplot.show()
Example #10
0
    def testOnBiggerGraph(self):
        graph = Graph()
        initial_node_name = gen_name()
        graph.add_node(initial_node_name, layer=0, position=(0.5, 0.5), label='E')

        [i1, i2] = P1().apply(graph, [initial_node_name])
        [i1_1, i1_2] = P2().apply(graph, [i1], orientation=1)
        [i2_1] = P9().apply(graph, [i2], orientation=1)

        if visualize_tests:
            visualize_graph_3d(graph)
            pyplot.show()

        [i1_1new, i1_2new, i2_1new] = P10().apply(graph, [i1_1, i1_2, i2_1])

        self.assertEqual(len(graph.nodes()), 15)
        self.assertEqual(len(graph.edges()), 32)

        e = get_node_at(graph=graph, layer=0, pos=(0.5, 0.5))
        e1 = get_node_at(graph=graph, layer=1, pos=(0.0, 0.0))
        e2 = get_node_at(graph=graph, layer=1, pos=(1.0, 0.0))
        e3 = get_node_at(graph=graph, layer=1, pos=(1.0, 1.0))
        e4 = get_node_at(graph=graph, layer=1, pos=(0.0, 1.0))
        e5 = get_node_at(graph=graph, layer=2, pos=(0.0, 0.0))
        e6 = get_node_at(graph=graph, layer=2, pos=(1.0, 0.0))
        e7 = get_node_at(graph=graph, layer=2, pos=(1.0, 1.0))
        e8 = get_node_at(graph=graph, layer=2, pos=(0.0, 1.0))
        e9 = get_node_at(graph=graph, layer=2, pos=(0.5, 0.5))

        # check position
        self.assertIsNotNone(e)
        self.assertIsNotNone(e1)
        self.assertIsNotNone(e2)
        self.assertIsNotNone(e3)
        self.assertIsNotNone(e4)
        self.assertIsNotNone(e5)
        self.assertIsNotNone(e6)
        self.assertIsNotNone(e7)
        self.assertIsNotNone(e8)
        self.assertIsNotNone(e9)

        self.assertEqual(graph.nodes[i1_1new]['position'], graph.nodes[i1_1]['position'])
        self.assertEqual(graph.nodes[i1_2new]['position'], graph.nodes[i1_2]['position'])
        self.assertEqual(graph.nodes[i2_1new]['position'], graph.nodes[i2]['position'])  # ten co jest z prawe

        self.assertEqual(graph.nodes[i1_1new]['layer'], graph.nodes[i1_1]['layer'])
        self.assertEqual(graph.nodes[i1_2new]['layer'], graph.nodes[i1_2]['layer'])
        self.assertEqual(graph.nodes[i2_1new]['layer'], graph.nodes[i2_1]['layer'])

        i1_new = get_node_at(graph=graph, layer=1, pos=graph.nodes[i1]['position'])
        i2_new = get_node_at(graph=graph, layer=1, pos=graph.nodes[i2]['position'])

        self.assertIsNotNone(i1_new)
        self.assertIsNotNone(i2_new)

        # zero
        self.assertTrue(graph.has_edge(e, i1_new))
        self.assertTrue(graph.has_edge(e, i2_new))

        # first
        self.assertTrue(graph.has_edge(e1, e2))
        self.assertTrue(graph.has_edge(e1, e3))
        self.assertTrue(graph.has_edge(e1, e4))
        self.assertTrue(graph.has_edge(e1, i2_new))
        self.assertTrue(graph.has_edge(e1, i1_new))
        self.assertTrue(graph.has_edge(e2, e3))
        self.assertTrue(graph.has_edge(e2, i2_new))
        self.assertTrue(graph.has_edge(e3, e4))
        self.assertTrue(graph.has_edge(e3, i1_new))
        self.assertTrue(graph.has_edge(e3, i2_new))
        self.assertTrue(graph.has_edge(e4, i1_new))

        # second
        self.assertTrue(graph.has_edge(e5, e6))
        self.assertTrue(graph.has_edge(e5, e9))
        self.assertTrue(graph.has_edge(e5, e8))
        self.assertTrue(graph.has_edge(e6, e7))
        self.assertTrue(graph.has_edge(e7, e9))
        self.assertTrue(graph.has_edge(e7, e8))

        self.assertTrue(graph.has_edge(i1_new, i1_1new))
        self.assertTrue(graph.has_edge(i1_new, i1_2new))
        self.assertTrue(graph.has_edge(i2_new, i2_1new))

        self.assertTrue(graph.has_edge(e5, i1_1new))
        self.assertTrue(graph.has_edge(e8, i1_1new))
        self.assertTrue(graph.has_edge(e9, i1_1new))

        self.assertTrue(graph.has_edge(e7, i1_2new))
        self.assertTrue(graph.has_edge(e8, i1_2new))
        self.assertTrue(graph.has_edge(e9, i1_2new))

        self.assertTrue(graph.has_edge(e5, i2_1new))
        self.assertTrue(graph.has_edge(e6, i2_1new))
        self.assertTrue(graph.has_edge(e7, i2_1new))

        # check labels
        self.assertEqual(graph.nodes[e]['label'], 'e')

        self.assertEqual(graph.nodes[e1]['label'], 'E')
        self.assertEqual(graph.nodes[e2]['label'], 'E')
        self.assertEqual(graph.nodes[e3]['label'], 'E')
        self.assertEqual(graph.nodes[e4]['label'], 'E')
        self.assertEqual(graph.nodes[e5]['label'], 'E')
        self.assertEqual(graph.nodes[e6]['label'], 'E')
        self.assertEqual(graph.nodes[e7]['label'], 'E')
        self.assertEqual(graph.nodes[e8]['label'], 'E')
        self.assertEqual(graph.nodes[e9]['label'], 'E')

        self.assertEqual(graph.nodes[i1_1new]['label'], 'I')
        self.assertEqual(graph.nodes[i1_2new]['label'], 'I')
        self.assertEqual(graph.nodes[i2_1new]['label'], 'I')
        self.assertEqual(graph.nodes[i1_new]['label'], 'i')
        self.assertEqual(graph.nodes[i2_new]['label'], 'i')