Exemple #1
0
    def test_happy_path(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e2, e3)

        i = add_interior(graph, e1, e2, e3)

        [i1, i2] = P2().apply(graph, [i])

        self.assertEqual(len(graph.nodes()), 10)
        self.assertEqual(len(graph.edges()), 19)

        self.assertEqual(graph.nodes[i]['label'], 'i')
        self.assertTrue(graph.has_edge(i, i1))
        self.assertTrue(graph.has_edge(i, i2))

        self.assertEqual(graph.nodes[i1]['label'], 'I')
        self.assertEqual(graph.nodes[i2]['label'], 'I')
        self.assertEqual(graph.nodes[i1]['layer'], graph.nodes[i]['layer'] + 1)
        self.assertEqual(graph.nodes[i2]['layer'], graph.nodes[i]['layer'] + 1)

        i1_neighbors = get_neighbors_at(graph, i1, graph.nodes[i1]['layer'])
        self.assertEqual(len(i1_neighbors), 3)
        i2_neighbors = get_neighbors_at(graph, i2, graph.nodes[i2]['layer'])
        self.assertEqual(len(i2_neighbors), 3)

        common_neighbors = [x for x in i1_neighbors if x in i2_neighbors]

        for n in i1_neighbors:
            if n not in common_neighbors:
                self.assertEqual(graph.nodes[n]['label'], 'E')
                self.assertEqual(
                    len(get_neighbors_at(graph, n, graph.nodes[i1]['layer'])),
                    3)

        for n in i2_neighbors:
            if n not in common_neighbors:
                self.assertEqual(graph.nodes[n]['label'], 'E')
                self.assertEqual(
                    len(get_neighbors_at(graph, n, graph.nodes[i2]['layer'])),
                    3)

        for c_neighbor in common_neighbors:
            self.assertEqual(graph.nodes[c_neighbor]['label'], 'E')
            self.assertEqual(
                len(
                    get_neighbors_at(graph, c_neighbor,
                                     graph.nodes[i1]['layer'])), 5)
Exemple #2
0
    def test_bad_input_vertex_count(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='I')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)
        graph.add_edge(e2, e3)

        with self.assertRaises(AssertionError):
            P2().apply(graph, [e1])
Exemple #3
0
    def test_bad_input_links(self):
        graph = Graph()
        e1 = gen_name()
        e2 = gen_name()
        e3 = gen_name()
        graph.add_node(e1, layer=1, position=(1.0, 2.0), label='E')
        graph.add_node(e2, layer=1, position=(1.0, 1.0), label='E')
        graph.add_node(e3, layer=1, position=(2.0, 1.0), label='E')

        graph.add_edge(e1, e2)
        graph.add_edge(e1, e3)

        i = add_interior(graph, e1, e2, e3)

        with self.assertRaises(AssertionError):
            P2().apply(graph, [i])
Exemple #4
0
    def think(self, **kwargs):
        if self._done: return self._results()
        if not 'content' in kwargs: raise Exception('No content found.')
        self._t_start = time.time()
        self._kwargs = kwargs

        if kwargs:

            for key in kwargs:
                # set all args as attributes. NB: will overwrite an existing attribute
                if not key == 'attribs': exec('self._%s = kwargs[key]' % key)
                exec('self._attribs.append("_%s")' % key)
        self.t_hash = hash_sum(str(
            kwargs['content']))  # hash of thought content, string or list
        self.t_name = gen_name('thot_%d_' % self.t_hash,
                               3,
                               namespace=self._mind.get_thots())
        result = self._handle_types(kwargs)
        ## finish up
        self._concs['name'] = self.t_name
        self._concs['kwargs'] = kwargs
        self._done = True
        self._t_end = time.time()
        self._dispatch()
        return result
Exemple #5
0
 def create_nodes(self, graph, _layer, _label, vertex_positions):
     nodes = []
     for x, y in vertex_positions:
         e = gen_name()
         graph.add_node(e, layer=_layer, position=(x, y), label=_label)
         nodes.append(e)
     return nodes
Exemple #6
0
    def apply(self,
              graph: Graph,
              prod_input: List[str],
              orientation: int = 0,
              **kwargs) -> List[str]:
        [initial_node_id] = prod_input
        initial_node_data = graph.nodes[initial_node_id]

        if initial_node_data['layer'] != 0:
            raise ValueError('bad layer')

        if initial_node_data['label'] != 'E':
            raise ValueError('bad label')

        positions = self.__get_positions(kwargs['positions'] if 'positions' in
                                         kwargs else None)

        # change label
        initial_node_data['label'] = 'e'

        vx_tl = gen_name()
        vx_tr = gen_name()
        vx_bl = gen_name()
        vx_br = gen_name()
        graph.add_node(vx_bl, layer=1, position=positions[0], label='E')
        graph.add_node(vx_br, layer=1, position=positions[1], label='E')
        graph.add_node(vx_tl, layer=1, position=positions[2], label='E')
        graph.add_node(vx_tr, layer=1, position=positions[3], label='E')

        if orientation % 2 == 1:
            [vx_bl, vx_br, vx_tr, vx_tl] = [vx_br, vx_tr, vx_tl, vx_bl]

        graph.add_edge(vx_tl, vx_tr)
        graph.add_edge(vx_tr, vx_br)
        graph.add_edge(vx_br, vx_bl)
        graph.add_edge(vx_bl, vx_tl)
        graph.add_edge(vx_tr, vx_bl)

        i1 = add_interior(graph, vx_tl, vx_tr, vx_bl)
        i2 = add_interior(graph, vx_tr, vx_br, vx_bl)

        graph.add_edge(i1, initial_node_id)
        graph.add_edge(i2, initial_node_id)

        return [i1, i2]
Exemple #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')

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

        self.assertEqual(len(graph.nodes()), 1)
Exemple #8
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')

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

        self.assertEqual(len(graph.nodes()), 1)
Exemple #9
0
    def apply(self, graph: Graph, prod_input: List[str], orientation: int = 0, **kwargs) -> List[str]:
        [i] = prod_input
        i_data = graph.nodes[i]
        self.__check_prod_input(graph, prod_input)

        i_data['label'] = 'i'
        i_layer = i_data['layer']
        new_layer = i_layer + 1

        i_neighbors = get_neighbors_at(graph, i, i_layer)

        # e1 doesn't mean e1 with (x1, y1)
        vx_e1 = gen_name()
        vx_e2 = gen_name()
        vx_e3 = gen_name()

        e1_pos = graph.nodes[i_neighbors[0]]['position']
        e2_pos = graph.nodes[i_neighbors[1]]['position']
        e3_pos = graph.nodes[i_neighbors[2]]['position']

        graph.add_node(vx_e1, layer=new_layer, position=e1_pos, label='E')
        graph.add_node(vx_e2, layer=new_layer, position=e2_pos, label='E')
        graph.add_node(vx_e3, layer=new_layer, position=e3_pos, label='E')

        graph.add_edge(vx_e1, vx_e2)
        graph.add_edge(vx_e2, vx_e3)
        graph.add_edge(vx_e3, vx_e1)

        b = add_break(graph, [(vx_e1, vx_e2), (vx_e2, vx_e3), (vx_e3, vx_e1)])
        b_neighbors = get_neighbors_at(graph, b, i_layer + 1)
        remaining = [x for x in [vx_e1, vx_e2, vx_e3] if x not in b_neighbors][0]
        graph.add_edge(b, remaining)

        i1 = add_interior(graph, b_neighbors[0], b, remaining)
        i2 = add_interior(graph, b_neighbors[1], b, remaining)

        graph.add_edge(i1, i)
        graph.add_edge(i2, i)

        return [i1, i2]
Exemple #10
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')

        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))
Exemple #11
0
    def test_different_position(self):
        graph = Graph()
        initial_node = gen_name()
        graph.add_node(initial_node, layer=0, position=(0, 0), label='E')

        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)
Exemple #12
0
    def test_bad_input_vertex_label(self):
        graph = Graph()
        positions = [(1.0, 1.0), (1.0, 2.0), (1.0, 3.0), (2.0, 3.0),
                     (3.0, 3.0)]

        [e1, e12, e2, e23, e3] = self.create_nodes(graph, 1, 'E', positions)
        e31 = gen_name()
        graph.add_node(e31, layer=1, position=(2.0, 2.0), label='e')

        self.create_edges_chain(graph, [e1, e12, e2, e23, e3, e31, e1])

        i = add_interior(graph, e1, e2, e3)

        with self.assertRaises(AssertionError):
            [i1, i3, i2a, i2b] = P5().apply(graph, [i])
        self.assertEqual(len(graph.nodes()), 7)
        self.assertEqual(len(graph.edges()), 9)

        if self.showPlots:
            pyplot.title("Wrong 'e' label", fontsize=16)
            visualize_graph_3d(graph)
            pyplot.show()
Exemple #13
0
 sdf = 0.01
 sfreq = 0.8
 nchan = 2
 aa = ap.cal.get_aa('ant_array',sdf,sfreq,nchan)
 ras = np.linspace(235.0,265.0,360) # ra, in degree
 decs = np.linspace(25.0,55.0,360) # dec, in degree
 ras = [ut.deg2hstr(ra) for ra in ras] # now in hh:mm:ss
 decs = [ut.deg2str(dec) for dec in decs] # now in deg:mm:ss
 nra,ndec = len(ras),len(decs)
 nra_ctr,ndec_ctr = nra/2,ndec/2 # center index
 cat = ap.fit.SrcCatalog([])
 for i in range(nra):
     for j in range(ndec):
         if i == nra_ctr and j == ndec_ctr:
             ref_src = ap.fit.RadioFixedBody(ras[i],decs[j],mfreq=sfreq,name='center') # the center reference source
             cat.add_srcs([ref_src])
         else:
             cat.add_srcs([ap.fit.RadioFixedBody(ras[i],decs[j],mfreq=sfreq,name=ut.gen_name(i,j,nra,ndec))])
 # aa.select_chans([0]) # Select which channels are used in computations
 aa.set_ephemtime('2013/6/1 12:00') # observing time
 cat.compute(aa)
 lms = []
 # for src in cat.get_srcs(): # bugs in aipy.phs.SrcCatalog.get_srcs function. No return value for case len(srcs) == 0.
 for src in [cat[s] for s in cat.keys()]:
     lms.append(get_lm(src,ref_src,aa))
 print len(lms)
 ls = [l1 for (l1,m1) in lms]
 ms = [m2 for (l2,m2) in lms]
 plt.figure(figsize=(8,6))
 plt.scatter(ls,ms)
 plt.show()
Exemple #14
0
    def apply(self,
              graph: Graph,
              prod_input: List[str],
              orientation: int = 0,
              **kwargs) -> List[str]:
        eps = kwargs.get('epsilon', 1e-6)
        self.__check_prod_input(graph, prod_input, eps)

        [i] = prod_input
        i_data = graph.nodes[i]
        i_data['label'] = 'i'
        i_layer = i_data['layer']
        new_layer = i_layer + 1

        # get 'E' nodes from the left side of production
        [e1, e2, e3] = self.get_corner_nodes(graph, i, i_layer, orientation)

        e12 = self.get_node_between(graph, e1, e2, i_layer, eps)
        e23 = self.get_node_between(graph, e2, e3, i_layer, eps)
        e31 = self.get_node_between(graph, e3, e1, i_layer, eps)

        # create new 'E' nodes in the next layer
        new_e1 = gen_name()
        new_e2 = gen_name()
        new_e3 = gen_name()

        new_e12 = gen_name()
        new_e23 = gen_name()
        new_e31 = gen_name()

        graph.add_node(new_e1,
                       layer=new_layer,
                       position=graph.nodes[e1]['position'],
                       label='E')
        graph.add_node(new_e2,
                       layer=new_layer,
                       position=graph.nodes[e2]['position'],
                       label='E')
        graph.add_node(new_e3,
                       layer=new_layer,
                       position=graph.nodes[e3]['position'],
                       label='E')

        graph.add_node(new_e12,
                       layer=new_layer,
                       position=graph.nodes[e12]['position'],
                       label='E')
        graph.add_node(new_e23,
                       layer=new_layer,
                       position=graph.nodes[e23]['position'],
                       label='E')
        graph.add_node(new_e31,
                       layer=new_layer,
                       position=graph.nodes[e31]['position'],
                       label='E')

        # create edges between new 'E' nodes
        graph.add_edge(new_e1, new_e12)
        graph.add_edge(new_e12, new_e2)

        graph.add_edge(new_e2, new_e23)
        graph.add_edge(new_e23, new_e3)

        graph.add_edge(new_e3, new_e31)
        graph.add_edge(new_e31, new_e1)

        graph.add_edge(new_e23, new_e31)
        graph.add_edge(new_e12, new_e31)
        graph.add_edge(new_e2, new_e31)

        # create new 'I' nodes and edges between new 'I' nodes and new 'E' nodes
        i1 = add_interior(graph, new_e1, new_e12, new_e31)
        i3 = add_interior(graph, new_e3, new_e23, new_e31)
        i2a = add_interior(graph, new_e2, new_e12, new_e31)
        i2b = add_interior(graph, new_e2, new_e23, new_e31)

        # create edges between new 'I' nodes and parent 'i' node
        graph.add_edge(i1, i)
        graph.add_edge(i3, i)
        graph.add_edge(i2a, i)
        graph.add_edge(i2b, i)

        return [i1, i3, i2a, i2b]
"""
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 productions.p1 import P1
from productions.p2 import P2
from utils import gen_name
from 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()