def test_id(self): temp1 = Vertex("A") temp1.add_neighbours({"A": 10}) self.assertEqual(temp1.id, "A") temp2 = Vertex("B") temp2.add_neighbours({"A": 20}) self.assertEqual(temp2.id, "B")
def test_add_neighbor(self): vertex1 = Vertex("apple") vertex2 = Vertex("banana") vertex3 = Vertex("coconut") vertex1.add_neighbor(vertex2) vertex1.add_neighbor(vertex3, 3) self.assertEqual(2, len(vertex1.neighbors)) self.assertEqual(1, vertex1.neighbors[vertex2]) self.assertEqual(3, vertex1.neighbors[vertex3])
def test_deduplicate_vertices_different(): node = Vertex(0, 'node0') vertex = Vertex(0, 'vertex0') point = Vertex(0, 'point0') f_n = Hyperedge([node]) f_p = Hyperedge([point]) f_nv = Hyperedge([node, vertex]) f_pn = Hyperedge([point, node]) f_vp = Hyperedge([vertex, point]) f_pnv = Hyperedge([point, vertex, node]) print(f_nv, f_pn, f_vp, f_pnv) assert all([f_n == f_nv, f_n == f_pn, f_p == f_vp, f_n == f_pnv])
def add_vertex(self, id, **properties): #check that a Vertex with this id is not already in the graph if id in self.vertex_set: raise Exception("A Vertex with this ID already exists in the graph.") else: newVertex = Vertex(id, **properties) self._add_vertex(newVertex)
def from_JSON(cls, file_path): with open(file_path) as file: json_data = json.load(file) # graph_properties = json_data["properties"] graph_vertex_set = json_data["vertex_set"] temp_vertex_set = dict() for vertex in graph_vertex_set: vertex_name = vertex["name"] temp_vertex_set[vertex_name] = Vertex(id = vertex_name, **vertex["properties"]) graph_edge_set = json_data["edge_set"] temp_edge_set = {'DirectedEdges': dict(), 'UndirectedEdges': dict()} for edge in graph_edge_set: if edge["directed"]: endpoints = tuple(edge["endpoints"]) tail, head = endpoints tail = temp_vertex_set.get(tail) head = temp_vertex_set.get(head) temp_edge_set['DirectedEdges'][endpoints] = DirectedEdge(tail, head, **edge["properties"]) elif not edge["directed"]: endpoints = frozenset(edge["endpoints"]) endVertices = (temp_vertex_set.get(point) for point in endpoints) temp_edge_set['UndirectedEdges'][endpoints] = UndirectedEdge(*endVertices, **edge["properties"]) else: raise Exception("All Edges must have a boolean-valued 'directed' field.") return Graph._from_vertex_and_edge_sets( temp_vertex_set.values(), itertools.chain( temp_edge_set['DirectedEdges'].values(), temp_edge_set['UndirectedEdges'].values() ) )
def apply(self, edge: Edge) -> bool: RemoveConstraintEdgeAction().apply(edge) e_index = self.polygon.edges.index(edge) self.polygon.edges.remove(edge) new_vertex = Vertex(line_middle_point(edge.v1.point, edge.v2.point)) v1_index = v2_index = -1 for i in range(len(self.polygon.vertices)): vertex = self.polygon.vertices[i] if vertex == edge.v1: v1_index = i if vertex == edge.v2: v2_index = i if v1_index < 0 or v2_index < 0: return False # first and last vertex if abs(v1_index - v2_index) == len(self.polygon.vertices) - 1: self.polygon.vertices.append(new_vertex) else: self.polygon.vertices.insert(max(v1_index, v2_index), new_vertex) self.polygon.edges.insert(e_index, Edge(edge.v1, new_vertex)) self.polygon.edges.insert(e_index + 1, Edge(edge.v2, new_vertex)) return True
def test_adding_neighbour(): v = Vertex(1, 1) v.add_neighbour(2) v.add_neighbour(3) assert len(v.get_neighbours()) == 2 assert v.get_neighbours()[0] == 2 assert v.get_neighbours()[1] == 3
def __init__(self): self.ids = [[], [0], [0, 1], [0, 1, 2]] self.labels = [['vertex{}'.format(i) for i in ids] for ids in self.ids] self.vertices = [[Vertex(i, l) for i, l in zip(ids, labels)] for ids, labels in zip(self.ids, self.labels)] self.ids_V = [VertexSet(ids) for ids in self.ids] self.labels_V = [VertexSet(labels) for labels in self.labels] self.vertices_V = [VertexSet(vertices) for vertices in self.vertices]
def test_mutability(): v = Vertex(1, 1) assert not v.get_is_untouchable() v.set_is_untouchable() assert v.get_is_untouchable()
def test_index(): v = Vertex(1, 1) assert v.get_index() == 1 v.set_index(5) assert v.get_index() == 5
def test_color(): v = Vertex(1, 1) v.set_color("Blue") assert v.get_color() == "Blue"
def test_cleaning_neighbours(): v = Vertex(1, 1) v.add_neighbour(2) v.add_neighbour(3) v.clean_neighbours() assert len(v.get_neighbours()) == 0
def test_removing_neighbour(): v = Vertex(1, 1) v.add_neighbour(2) v.add_neighbour(3) v.remove_neighbour(2) assert 2 not in v.get_neighbours()
def test_initalization(): LineSegment(Vertex(1, 2, 3), Vertex(4, 5, 6), 3.2)
def test_init_column_vector_labeled(): S = IncidenceMatrix([[1], [2], [3]], [Vertex(i, 'vertex{}'.format(i)) for i in range(1, 4)]) S.pprint(True) assert S.shape == (3, 1)
def add_vertex(self, key): """Add a new vertex object to the graph with the given key and return the vertex.""" if key in self.vert_list: raise Exception(f"Vertex {key} already in graph.") self.vert_list[key] = Vertex(key) return self.vert_list[key]
def test_end(): line_segment = LineSegment(Vertex(0, 0, 0), Vertex(1, 1, 1), 3.5) assert line_segment.end == Vertex(3.5, 3.5, 3.5)
def test_deduplicate_vertices_same(): f = Hyperedge([Vertex(0, 'node0'), Vertex(0, 'node0')]) print(f) assert len(f) == 1
def __init__(self): self.u = Vertex() self.v = Vertex() self.u0 = Vertex(0) self.v0 = Vertex(0) self.u0l = Vertex(0, 'vertex0') self.v0l = Vertex(0, 'vertex0') self.u1 = Vertex(1) self.v1 = Vertex(1) self.u1l = Vertex(1, 'vertex1') self.v1l = Vertex(1, 'vertex1') self.v1l_ = Vertex(1, 'node1')
def test_add_neighbours(self): temp = Vertex("C") new_neighbour = {"B": 10} temp.add_neighbours(new_neighbour) self.assertDictEqual(temp.neighbours, new_neighbour)
def test_constructor(self, id, kwargs): self.assertIsInstance(Vertex(id, **kwargs), Vertex)
def setUp(self): self.v1 = Vertex(id = "1") self.v2 = Vertex(id = "2") self.properties = {'weight': 10, 'color': 'blue'} self.testEdge = UndirectedEdge(self.v1, self.v2, **self.properties)
def test_init_row_vector_labeled(): S = IncidenceMatrix([[1, 2, 3]], [Vertex(i, 'vertex{}'.format(i)) for i in range(1, 2)]) S.pprint(True) assert S.shape == (1, 3)
def add_vertex(self, name: str): self.vertices[name] = Vertex(name)
def test_init_matrix_labeled(): S = IncidenceMatrix([[1, 2, 3], [4, 5, 6]], [Vertex(i, 'vertex{}'.format(i)) for i in range(1, 3)]) S.pprint(True) assert S.shape == (2, 3)
def test_initalization(): Vertex(1, 2, 3.5)