예제 #1
0
def create_graph_0(dump_path, nodes_file, edges_file):

    print 'Creating graph ...'

    graph = Graph()

    # add the nodes
    nodes_list = dump_path + nodes_file
    nodes_data = np.loadtxt(nodes_list, skiprows=1)
    n_nodes, n_dim = nodes_data.shape
    for i in range(n_nodes):
        node_id = int(nodes_data[i,0])
        if node_id < 0:
            continue
        x_pos = nodes_data[i,1]
        y_pos = nodes_data[i,2]
        size = nodes_data[i,3]
        b_fix = int(nodes_data[i,4])

        rgb_color = nodes_data[i,5:8]
        color_energy = get_color_energy(rgb_color, size)
        print color_energy

        node = Node(node_id,\
                    seg_id=node_id, \
                    x_pos=x_pos, \
                    y_pos=y_pos, \
                    b_fix=b_fix, \
                    size=size, \
                    color_energy=color_energy, \
                    color=rgb_color, \
                    saliency=1.0, \
                    radius=size/2)

        graph.add_node(node)
        # add this node to the list of people nodes
        # graph.add_pnode(node)
        # graph.num_people += 1

    edges_list = dump_path + edges_file

    edges_data = np.loadtxt(edges_list, skiprows=1)
    n_edges, n_dim = edges_data.shape

    for i in range(n_edges):
        src_id = edges_data[i,0]
        if src_id < 0:
            continue
        dst_id = edges_data[i,1]
        length = edges_data[i,2]
        
        src_node = graph.get_node(src_id)
        dst_node = graph.get_node(dst_id)

        assert src_node is not None
        assert dst_node is not None

        graph.add_edge(src_node, dst_node, spring_length=length)

    return graph
예제 #2
0
    def test_add_edge_directed(self):
        graph = Graph(directed=True)
        graph.add_node(1)
        graph.add_node(2)
        graph.add_edge(1, 2)

        self.assertEqual(graph.nodes, {1: [(2, 0)], 2: []})
예제 #3
0
    def __getitem__(self, item):
        data = self.ann[item]
        dependencies = data['depends']
        g = Graph()
        for dep in dependencies:
            gov_node = g.add_node(dep['governorGloss'], dep['governor'], "")
            dep_node = g.add_node(dep['dependentGloss'], dep['dependent'], "")
            g.add_edge(gov_node, dep_node, dep['dep'])
        tree = g.to_tree()
        tree.padding()

        for n in tree.nodes.values():
            word_idx = self.vocab(
                n.lex.lower() if n.lex != "<EOB>" else "<EOB>")
            n.word_idx = word_idx

        fc_feat = np.load(
            os.path.join(self.cocotalk_fc,
                         str(data['img_id']) + '.npy'))
        if self.caption_model == 'att_model' or self.caption_model == 'tree_model_md_att':
            att_feat = np.load(
                os.path.join(self.cocotalk_att,
                             str(data['img_id']) + '.npz'))['feat']
            att_feat = att_feat.reshape(-1, att_feat.shape[-1])
            return fc_feat, att_feat, tree
        elif self.caption_model == 'tree_model' or self.caption_model == 'tree_model_1' \
                or self.caption_model == 'tree_model_md' or self.caption_model == 'tree_model_2':
            return fc_feat, tree
        else:
            raise Exception("Caption model not supported: {}".format(
                self.caption_model))
예제 #4
0
def test_size():
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    expected = 2
    actual = graph.size()
    assert actual == expected
예제 #5
0
    def test_add_node(self):
        graph = Graph()
        graph.add_node(1)
        graph.add_node(2)

        self.assertEqual(graph.nodes, {1: [], 2: []})
        self.assertRaises(Exception, Graph.add_node, 1)
예제 #6
0
    def test_check_cycles(self):
        G = Graph()
        a, b, c, d, e, f, g = Node('A'), Node('B'), Node('C'), Node('D'), Node(
            'E'), Node('F'), Node('G')

        N = [a, b, c, d, e, f, g]

        for node in N:
            G.add_node(node)

        E = [
            Edge(a, b),
            Edge(b, c),
            Edge(c, d),
            Edge(a, d),
            Edge(d, e),
            Edge(e, a),
            Edge(e, f),
            Edge(f, g),
            Edge(g, f),
        ]

        for edge in E:
            G.add_edge(edge)

        self.assertEqual(Graph.check_cycles(G, True),
                         [[g, f], [e, d, c, b, a]])
        self.assertEqual(Graph.check_cycles(G, False), [[g, f]])
예제 #7
0
def test_size_fail():
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    expected = 3
    actual = graph.size()
    assert actual != expected
예제 #8
0
def test_get_nodes():
    graph = Graph()
    graph.add_node("banana")
    graph.add_node("apple")
    expected = 2
    actual = len(graph.get_nodes())
    assert actual == expected
예제 #9
0
def test_size_two():
    graph = Graph()
    graph.add_node("spam")
    graph.add_node("bacon")
    expected = 2
    actual = graph.size()
    assert actual == expected
def test_2():
    graph = Graph()
    start = graph.add_node('first node')
    end = graph.add_node('edge')
    graph.add_edge(start , end , 0)
    actual = graph._adjacency_list[start][0].vertex
    expected = end
    assert actual == expected
예제 #11
0
def test_get_neighbors_returns_edges_with_custom_weight():
    graph = Graph()
    banana = graph.add_node("banana")
    apple = graph.add_node("apple")
    graph.add_edge(apple, banana, 44)
    neighbors = graph.get_neighbors(apple)
    neighbor_edge = neighbors[0]
    assert neighbor_edge.weight == 44
def test_3():
    graph = Graph()
    v1 = graph.add_node('first node')
    v2 = graph.add_node('second node')
    v3 = graph.add_node('last node')
    actual = graph.get_nodes()
    expected = {v1:0,v2:0,v3:0}.keys()
    assert actual == expected
예제 #13
0
    def test_add_edge_undirected(self):
        graph = Graph()
        graph.add_node(1)
        graph.add_node(2)
        graph.add_edge(1, 2)

        self.assertEqual(graph.nodes, {1: [(2, 0)], 2: [(1, 0)]})
        self.assertRaises(Exception, Graph.add_edge, 0, 3)
예제 #14
0
def create_graph(s_objects, p_objects):

    print 'Creating graph ...'

    graph = Graph()

    node_id = 0  # use this as node id as some segments might have similar id

    # add the imp object nodes
    for s_obj in s_objects:
        node = Node(node_id, \
                    seg_id=s_obj.id, \
                    x_pos=s_obj.pos[1], \
                    y_pos=s_obj.pos[0], \
                    b_fix=True, \
                    size=s_obj.size, \
                    color_energy=s_obj.color_energy, \
                    color=s_obj.color, \
                    saliency=s_obj.saliency, \
                    radius=np.sqrt(s_obj.size)/4)

        graph.add_node(node)
        node_id += 1

    max_snode_id = node_id

    # add the people node
    for p_obj in p_objects:
        node = Node(node_id,\
                    seg_id=p_obj.id, \
                    x_pos=p_obj.pos[1], \
                    y_pos=p_obj.pos[0], \
                    b_fix=False, \
                    size=p_obj.size, \
                    color_energy=1.2*p_obj.color_energy, \
                    color=p_obj.color, \
                    saliency=1.2*p_obj.saliency, \
                    radius=np.sqrt(p_obj.size)/4)

        graph.add_node(node)
        # add this node to the list of people nodes
        graph.add_pnode(node)
        graph.num_people += 1

        # create edges from the people node to rest of nodes
        for dst_id in range(max_snode_id):
            src_id = node_id
            src_node = node
            dst_node = graph.get_node(dst_id)

            assert src_node is not None
            assert dst_node is not None

            graph.add_edge(src_node, dst_node, spring_length=0.0)

        node_id += 1

    return graph
예제 #15
0
def test_get_node_fail():
    graph = Graph()
    graph.add_node('a')
    graph.add_node('b')
    graph.add_node('c')
    graph.add_node('d')
    expected = 7
    actual = len(graph.get_node())
    assert actual != expected
def test_add_edge_other_case():

    graph = Graph()

    end = graph.add_node('b')

    start = graph.add_node('a')

    graph.add_edge(start, end)
def test_add_edge_groovy():

    graph = Graph()

    end = graph.add_node('end')

    start = graph.add_node('start')

    graph.add_edge(start, end)
예제 #18
0
def test_get_neighbors_returns_edges_with_default_weight():
    graph = Graph()
    banana = graph.add_node("banana")
    apple = graph.add_node("apple")
    graph.add_edge(apple, banana)
    neighbors = graph.get_neighbors(apple)
    actual = neighbors[0].weight
    expected = 0
    assert actual == expected
예제 #19
0
def test_get_neighbors_returns_edges():
    graph = Graph()
    banana = graph.add_node("banana")
    apple = graph.add_node("apple")
    graph.add_edge(apple, banana)
    neighbors = graph.get_neighbors(apple)
    assert len(neighbors) == 1
    neighbor = neighbors[0]
    assert isinstance(neighbor, Edge)
    assert neighbor.vertex.value == 'banana'
예제 #20
0
def test_size_returns_correct_size():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    vertex3 = Vertex(3)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_node(vertex3)
    actual = graph.size()
    assert actual == 3
예제 #21
0
def test_get_neighbors():
    graph = Graph()
    a = graph.add_node('a')
    b = graph.add_node('b')
    c = graph.add_node('c')
    graph.add_edge(a, b)
    graph.add_edge(a, c, 2)
    actual = graph.get_neighbors(a)
    expected = [{'Value': 'b', 'Weight': 1}, {'Value': 'c', 'Weight': 2}]
    assert actual == expected
예제 #22
0
    def test_get_node_degree_undirected(self):
        graph = Graph()
        graph.add_node(1)
        graph.add_node(2)
        graph.add_node(3)

        graph.add_edge(1, 2)
        graph.add_edge(1, 3)
        graph.add_edge(3, 2)

        self.assertEqual(graph.get_node_degree(1), 2)
예제 #23
0
    def test_get_node_degree_directed(self):
        graph = Graph(directed=True)
        graph.add_node(1)
        graph.add_node(2)
        graph.add_node(3)

        graph.add_edge(1, 2)
        graph.add_edge(2, 1)
        graph.add_edge(3, 1)

        self.assertEqual(graph.get_node_degree(1), 3)
예제 #24
0
def test_an_edge_can_be_added_to_graph():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_edge(vertex1, vertex2, 20)
    vertex1_list = graph._adjacency_list[vertex1]
    first_edge = vertex1_list[0]
    assert first_edge.vertex == vertex2
    assert first_edge.weight == 20
예제 #25
0
def test_neighbor():
    g = Graph()
    a = g.add_node('a')
    b = g.add_node('b')
    c = g.add_node('c')
    d = g.add_node('d')
    g.add_edge(a, b)
    g.add_edge(a, c)
    g.add_edge(a, d)
    neighbor = g.get_neighbor(a)
    assert True
def test_size():

    graph = Graph()

    graph.add_node('ayman')

    expected = 1

    actual = graph.size()

    assert actual == expected
def test_neighbors_with_weight_between_nodes():
    graph = Graph()
    start = graph.add_node('start')
    end = graph.add_node('end')
    mid = graph.add_node('middle')
    other = graph.add_node('other')

    graph.add_edge(start, end, 1)
    graph.add_edge(start, mid, 2)
    graph.add_edge(start, other, 3)

    assert graph.get_neighbors(start) == [(end, 1), (mid, 2), (other, 3)]
def test_get_edges():
    graph = Graph()
    pandora = graph.add_node('pandora')
    arendelle = graph.add_node('arendelle')
    metroville = graph.add_node('metroville')
    naboo = graph.add_node('naboo')
    graph.add_edge(pandora, arendelle, 150)
    graph.add_edge(arendelle, metroville, 99)
    graph.add_edge(metroville, naboo, 73)
    expected = (True, '$150')
    actual = get_edge(graph, [pandora, arendelle])
    assert actual == expected
def test_get_edges_with_cities_that_do_not_connect():
    graph = Graph()
    pandora = graph.add_node('pandora')
    arendelle = graph.add_node('arendelle')
    metroville = graph.add_node('metroville')
    naboo = graph.add_node('naboo')
    graph.add_edge(pandora, arendelle, 150)
    graph.add_edge(arendelle, metroville, 99)
    graph.add_edge(metroville, naboo, 73)
    expected = (False, '$0')
    actual = get_edge(graph, [pandora, naboo])
    assert actual == expected
예제 #30
0
def test_a_collection_of_all_nodes_can_be_retrieved_from_graph():
    graph = Graph()
    vertex1 = Vertex(1)
    vertex2 = Vertex(2)
    vertex3 = Vertex(3)
    graph.add_node(vertex1)
    graph.add_node(vertex2)
    graph.add_node(vertex3)
    list = graph.get_nodes()
    expected = [1, 2, 3]
    for i, obj in enumerate(list):
        assert obj.value == expected[i]
예제 #31
0
class TestFlightGraph(TestCase):
  def setUp(self):
    f = open('assets/data/map_data.json', 'r')
    decoded = json.loads(f.read())
    self.g = Graph()
    self.g.build_nodes(decoded['metros'])
    self.g.build_edges(decoded['routes'])
    self.utils = GraphUtils()

  def testInit(self):
    assert (self.g.nodes.get('ALG').name == 'Algiers')

  def testEdgeDestinations(self):
    assert (self.g.nodes.get('CHI').get_destinations() == ['MEX', 'LAX', 'SFO', 'YYZ', 'ATL'])

  def testAddCity(self):
    data = {'code': 'CBS', 'name': 'Test City', 'country': 'US', 'continent': 'Europe',
            'timezone': 0, 'coordinates': 'adssa', 'population': 3, 'region': 'south'}
    print(data)
    self.g.add_node(data)
    assert self.g.get_node_from_code('CBS').name == 'Test City'

  def testAddRoute(self):
    self.g.add_route('TYO', 'YYZ', 2000)
    node = self.g.get_node_from_code('TYO')
    e = Edge('YYZ', 2000)
    bool = False
    for edge in node.edges:
      if e.destination == edge.destination and e.distance == edge.distance:
        bool = True
        break
    assert bool == True

  def testRemoveRoute(self):
    self.g.add_route('TYO', 'YYZ', 2000)
    self.g.remove_route('TYO', 'YYZ')
    node = self.g.get_node_from_code('TYO')
    e = Edge('YYZ', 2000)
    bool = False
    for edge in node.edges:
      if e.destination == edge.destination and e.distance == edge.distance:
        bool = True
        break
    assert bool != True

  def testEditCity(self):
    data = {'code': 'CHI', 'name': 'Test City', 'country': 'US', 'continent': 'Europe',
            'timezone': 0, 'coordinates': 'adssa', 'population': 3, 'region': 'south'}
    self.g.edit_node(data)
    assert self.g.get_node_from_code('CHI').name == 'Test City'
    def construct_triangle_network(self):
        """Constructs a triangle, clearly not bipartite"""
        network = Graph()
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)

        network.add_node(n1)
        network.add_node(n2)
        network.add_node(n3)

        network.add_edge(UndirectedEdge(n1, n2))
        network.add_edge(UndirectedEdge(n2, n3))
        network.add_edge(UndirectedEdge(n3, n1))

        return network
    def construct_bp_network(self):
        network = Graph()
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)
        n4 = Node(4)
        n5 = Node(5)
        n6 = Node(6)

        # Add nodes to graph
        network.add_node(n1)
        network.add_node(n2)
        network.add_node(n3)
        network.add_node(n4)
        network.add_node(n5)
        network.add_node(n6)

        # Add edges in zig-zag pattern
        network.add_edge(UndirectedEdge(n1, n2))
        network.add_edge(UndirectedEdge(n2, n3))
        network.add_edge(UndirectedEdge(n3, n4))
        network.add_edge(UndirectedEdge(n4, n5))
        network.add_edge(UndirectedEdge(n5, n6))

        return network