From 5e566760d383336986574a102e8af9d9ff87da52 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Thu, 17 Mar 2022 13:55:11 +0000 Subject: [PATCH 1/3] stop using DefaultOrderedDict --- .../graphs/abstract_multiple_partition.py | 7 +++---- .../graphs/application/application_graph.py | 4 ++-- pacman/model/graphs/graph.py | 13 ++++++------- pacman/model/graphs/machine/machine_graph.py | 18 +++++++++--------- .../zoned_routing_table_generator.py | 4 ++-- pacman/utilities/vertex_sorter.py | 5 ++--- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pacman/model/graphs/abstract_multiple_partition.py b/pacman/model/graphs/abstract_multiple_partition.py index 50ec48529..d3edad63a 100644 --- a/pacman/model/graphs/abstract_multiple_partition.py +++ b/pacman/model/graphs/abstract_multiple_partition.py @@ -13,8 +13,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict -from spinn_utilities.default_ordered_dict import DefaultOrderedDict +from collections import defaultdict from spinn_utilities.ordered_set import OrderedSet from spinn_utilities.overrides import overrides from pacman.exceptions import PacmanConfigurationException @@ -38,8 +37,8 @@ def __init__( identifier=identifier, allowed_edge_types=allowed_edge_types, constraints=constraints, label=label, traffic_weight=traffic_weight, class_name=class_name) - self._pre_vertices = OrderedDict() - self._destinations = DefaultOrderedDict(OrderedSet) + self._pre_vertices = dict() + self._destinations = defaultdict(OrderedSet) # hard code dict of lists so that only these are acceptable. for pre_vertex in pre_vertices: diff --git a/pacman/model/graphs/application/application_graph.py b/pacman/model/graphs/application/application_graph.py index 3747fd41e..7d1647c7a 100644 --- a/pacman/model/graphs/application/application_graph.py +++ b/pacman/model/graphs/application/application_graph.py @@ -13,10 +13,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from collections import defaultdict from .application_edge import ApplicationEdge from .application_vertex import ApplicationVertex from .application_edge_partition import ApplicationEdgePartition -from spinn_utilities.default_ordered_dict import DefaultOrderedDict from spinn_utilities.ordered_set import OrderedSet from spinn_utilities.overrides import overrides from pacman.exceptions import ( @@ -40,7 +40,7 @@ def __init__(self, label): """ super().__init__(ApplicationVertex, ApplicationEdge, label) self._outgoing_edge_partitions_by_pre_vertex = \ - DefaultOrderedDict(OrderedSet) + defaultdict(OrderedSet) def forget_machine_graph(self): """ Forget the whole mapping from this graph to an application graph. diff --git a/pacman/model/graphs/graph.py b/pacman/model/graphs/graph.py index 95338ff3a..19d1e88cb 100644 --- a/pacman/model/graphs/graph.py +++ b/pacman/model/graphs/graph.py @@ -13,10 +13,9 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict +from collections import defaultdict from spinn_utilities.abstract_base import ( AbstractBase, abstractmethod, abstractproperty) -from spinn_utilities.default_ordered_dict import DefaultOrderedDict from spinn_utilities.ordered_set import OrderedSet from pacman.exceptions import ( PacmanAlreadyExistsException, PacmanInvalidParameterException) @@ -72,11 +71,11 @@ def __init__(self, allowed_vertex_types, allowed_edge_types, label): self._vertices = [] self._vertex_by_label = dict() self._unlabelled_vertex_count = 0 - self._outgoing_edge_partitions_by_name = OrderedDict() - self._outgoing_edges = DefaultOrderedDict(OrderedSet) - self._incoming_edges = DefaultOrderedDict(OrderedSet) - self._incoming_edges_by_partition_name = DefaultOrderedDict(list) - self._outgoing_edge_partition_by_edge = OrderedDict() + self._outgoing_edge_partitions_by_name = dict() + self._outgoing_edges = defaultdict(OrderedSet) + self._incoming_edges = defaultdict(OrderedSet) + self._incoming_edges_by_partition_name = defaultdict(list) + self._outgoing_edge_partition_by_edge = dict() self._label = label @property diff --git a/pacman/model/graphs/machine/machine_graph.py b/pacman/model/graphs/machine/machine_graph.py index 16d85de5b..0957d99d5 100644 --- a/pacman/model/graphs/machine/machine_graph.py +++ b/pacman/model/graphs/machine/machine_graph.py @@ -13,11 +13,11 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from collections import defaultdict from .machine_vertex import MachineVertex from .machine_edge import MachineEdge from spinn_utilities.ordered_set import OrderedSet from spinn_utilities.overrides import overrides -from spinn_utilities.default_ordered_dict import DefaultOrderedDict from pacman.exceptions import ( PacmanAlreadyExistsException, PacmanInvalidParameterException) from pacman.model.graphs import Graph @@ -79,21 +79,21 @@ def __init__(self, label, application_graph=None): else: # Must be false as there is no App_graph self._application_level_used = False - self._multicast_partitions = DefaultOrderedDict( - lambda: DefaultOrderedDict(set)) + self._multicast_partitions = defaultdict( + lambda:defaultdict(set)) self._edge_partitions = OrderedSet() self._fixed_route_edge_partitions_by_pre_vertex = ( - DefaultOrderedDict(OrderedSet)) + defaultdict(OrderedSet)) self._multicast_edge_partitions_by_pre_vertex = ( - DefaultOrderedDict(OrderedSet)) + defaultdict(OrderedSet)) self._sdram_edge_partitions_by_pre_vertex = ( - DefaultOrderedDict(OrderedSet)) + defaultdict(OrderedSet)) self._fixed_route_edge_partitions_by_post_vertex = ( - DefaultOrderedDict(OrderedSet)) + defaultdict(OrderedSet)) self._multicast_edge_partitions_by_post_vertex = ( - DefaultOrderedDict(OrderedSet)) + defaultdict(OrderedSet)) self._sdram_edge_partitions_by_post_vertex = ( - DefaultOrderedDict(OrderedSet)) + defaultdict(OrderedSet)) @overrides(Graph.add_edge) def add_edge(self, edge, outgoing_edge_partition_name): diff --git a/pacman/operations/routing_table_generators/zoned_routing_table_generator.py b/pacman/operations/routing_table_generators/zoned_routing_table_generator.py index a36b6158a..47a57fcb1 100644 --- a/pacman/operations/routing_table_generators/zoned_routing_table_generator.py +++ b/pacman/operations/routing_table_generators/zoned_routing_table_generator.py @@ -13,8 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from collections import defaultdict from spinn_utilities.progress_bar import ProgressBar -from spinn_utilities.default_ordered_dict import DefaultOrderedDict from spinn_machine import MulticastRoutingEntry from pacman.model.routing_tables import ( UnCompressedMulticastRoutingTable, MulticastRoutingTables) @@ -89,7 +89,7 @@ def _create_routing_table(self, chip, partitions_in_table, routing_infos, :rtype: MulticastRoutingTable """ table = UnCompressedMulticastRoutingTable(chip.x, chip.y) - partitions_by_app_vertex = DefaultOrderedDict(set) + partitions_by_app_vertex = defaultdict(set) for partition in partitions_in_table: partitions_by_app_vertex[partition.pre_vertex.app_vertex].add( partition) diff --git a/pacman/utilities/vertex_sorter.py b/pacman/utilities/vertex_sorter.py index 58a955fe5..ce513ce39 100644 --- a/pacman/utilities/vertex_sorter.py +++ b/pacman/utilities/vertex_sorter.py @@ -13,9 +13,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from collections import defaultdict import sys -from spinn_utilities.default_ordered_dict import DefaultOrderedDict - class ConstraintOrder(object): """ A constraint order definition for sorting. @@ -91,7 +90,7 @@ def __init__(self, constraint_order): The order in which the constraints are to be sorted """ # Group constraints based on the class - self._constraints = DefaultOrderedDict(list) + self._constraints = defaultdict(list) for c in constraint_order: self._constraints[c.constraint_class].append( (c.relative_order, c.required_optional_properties)) From 3d4ae1c2746c354ccc985c61fe92bf4a622ab54c Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Thu, 17 Mar 2022 14:11:12 +0000 Subject: [PATCH 2/3] no need for OrderedDict --- .../abstract_splitter_common.py | 3 +-- pacman/model/placements/placements.py | 5 ++--- .../multicast_routing_table_by_partition.py | 5 ++--- .../multicast_routing_tables.py | 5 ++--- .../uncompressed_multicast_routing_table.py | 3 +-- .../splitter_partitioner.py | 3 +-- .../routing_compression_checker.py | 3 +-- .../partition_algorithm_utilities.py | 3 +-- .../placer_algorithm_utilities.py | 3 +-- .../routing_info_allocator_utilities.py | 3 +-- pacman/utilities/json_utils.py | 19 +++++++++---------- 11 files changed, 22 insertions(+), 33 deletions(-) diff --git a/pacman/model/partitioner_splitters/abstract_splitters/abstract_splitter_common.py b/pacman/model/partitioner_splitters/abstract_splitters/abstract_splitter_common.py index 576046bd8..655745a52 100644 --- a/pacman/model/partitioner_splitters/abstract_splitters/abstract_splitter_common.py +++ b/pacman/model/partitioner_splitters/abstract_splitters/abstract_splitter_common.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . import sys -from collections import OrderedDict from spinn_utilities.abstract_base import AbstractBase, abstractmethod from pacman.exceptions import PacmanConfigurationException from pacman.model.constraints.partitioner_constraints import ( @@ -85,7 +84,7 @@ def _get_map(self, edge_types): :return: dict of vertex as key, edge types as list in value :rtype: dict(MachineVertex, EdgeType) """ - result = OrderedDict() + result = dict() for vertex in self._governed_app_vertex.machine_vertices: result[vertex] = edge_types return result diff --git a/pacman/model/placements/placements.py b/pacman/model/placements/placements.py index 855daeba2..ce303e1d2 100644 --- a/pacman/model/placements/placements.py +++ b/pacman/model/placements/placements.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict from pacman.exceptions import ( PacmanAlreadyPlacedError, PacmanNotPlacedError, PacmanProcessorAlreadyOccupiedError, PacmanProcessorNotOccupiedError) @@ -41,8 +40,8 @@ def __init__(self, placements=None): :raise PacmanProcessorAlreadyOccupiedError: If two placements are made to the same processor. """ - self._placements = OrderedDict() - self._machine_vertices = OrderedDict() + self._placements = dict() + self._machine_vertices = dict() if placements is not None: self.add_placements(placements) diff --git a/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py b/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py index baf48b224..dab7c7d21 100644 --- a/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py +++ b/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict class MulticastRoutingTableByPartition(object): @@ -26,7 +25,7 @@ class MulticastRoutingTableByPartition(object): ] def __init__(self): - self._router_to_entries_map = OrderedDict() + self._router_to_entries_map = dict() def add_path_entry(self, entry, router_x, router_y, partition): """ Adds a multicast routing path entry @@ -41,7 +40,7 @@ def add_path_entry(self, entry, router_x, router_y, partition): # update router_to_entries_map key = (router_x, router_y) if key not in self._router_to_entries_map: - self._router_to_entries_map[key] = OrderedDict() + self._router_to_entries_map[key] = dict() if partition not in self._router_to_entries_map[key]: self._router_to_entries_map[key][partition] = entry diff --git a/pacman/model/routing_tables/multicast_routing_tables.py b/pacman/model/routing_tables/multicast_routing_tables.py index b00ab9c3f..1d6eaf98a 100644 --- a/pacman/model/routing_tables/multicast_routing_tables.py +++ b/pacman/model/routing_tables/multicast_routing_tables.py @@ -15,7 +15,6 @@ import json import gzip -from collections import OrderedDict from pacman.exceptions import PacmanAlreadyExistsException from .uncompressed_multicast_routing_table import \ UnCompressedMulticastRoutingTable @@ -119,12 +118,12 @@ def __iter__(self): def to_json(router_table): json_list = [] for routing_table in router_table: - json_routing_table = OrderedDict() + json_routing_table = dict() json_routing_table["x"] = routing_table.x json_routing_table["y"] = routing_table.y entries = [] for entry in routing_table.multicast_routing_entries: - json_entry = OrderedDict() + json_entry = dict() json_entry["key"] = entry.routing_entry_key json_entry["mask"] = entry.mask json_entry["defaultable"] = entry.defaultable diff --git a/pacman/model/routing_tables/uncompressed_multicast_routing_table.py b/pacman/model/routing_tables/uncompressed_multicast_routing_table.py index fc98ae156..3d53345af 100644 --- a/pacman/model/routing_tables/uncompressed_multicast_routing_table.py +++ b/pacman/model/routing_tables/uncompressed_multicast_routing_table.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict import csv import gzip import logging @@ -70,7 +69,7 @@ def __init__(self, x, y, multicast_routing_entries=None): self._y = y self._number_of_defaulted_routing_entries = 0 self._multicast_routing_entries = list() - self._entries_by_key_mask = OrderedDict() + self._entries_by_key_mask = dict() self._entries_by_key = dict() if multicast_routing_entries is not None: diff --git a/pacman/operations/partition_algorithms/splitter_partitioner.py b/pacman/operations/partition_algorithms/splitter_partitioner.py index be7db2e54..34ef24a7a 100644 --- a/pacman/operations/partition_algorithms/splitter_partitioner.py +++ b/pacman/operations/partition_algorithms/splitter_partitioner.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict from pacman.exceptions import (PacmanConfigurationException) from pacman.model.constraints.partitioner_constraints import ( MaxVertexAtomsConstraint, FixedVertexAtomsConstraint) @@ -157,7 +156,7 @@ def order_vertices_for_dependent_splitters(self, vertices): :return: vertices in list with new ordering :rtype: iterable(ApplicationVertex) """ - dependent_vertices = OrderedDict() + dependent_vertices = dict() other_vertices = set() for vertex in vertices: if isinstance(vertex.splitter, AbstractDependentSplitter): diff --git a/pacman/operations/router_compressors/routing_compression_checker.py b/pacman/operations/router_compressors/routing_compression_checker.py index 4beafe85e..eacc19870 100644 --- a/pacman/operations/router_compressors/routing_compression_checker.py +++ b/pacman/operations/router_compressors/routing_compression_checker.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict import logging from spinn_utilities.log import FormatAdapter from pacman.exceptions import PacmanRoutingException @@ -62,7 +61,7 @@ def codify_table(table, length=32): :param int length: :rtype: dict(str, ~spinn_machine.MulticastRoutingEntry) """ - code_dict = OrderedDict() + code_dict = dict() for route in table.multicast_routing_entries: code_dict[codify(route, length)] = route return code_dict diff --git a/pacman/utilities/algorithm_utilities/partition_algorithm_utilities.py b/pacman/utilities/algorithm_utilities/partition_algorithm_utilities.py index 4ae33019a..882cabe5c 100644 --- a/pacman/utilities/algorithm_utilities/partition_algorithm_utilities.py +++ b/pacman/utilities/algorithm_utilities/partition_algorithm_utilities.py @@ -16,7 +16,6 @@ """ A collection of methods which support partitioning algorithms. """ -from collections import OrderedDict from spinn_utilities.ordered_set import OrderedSet from pacman.utilities import utility_calls as utils from pacman.exceptions import PacmanPartitionException @@ -82,7 +81,7 @@ def get_same_size_vertex_groups(vertices): # Dict of vertex to list of vertices with same size # (repeated lists expected) - same_size_vertices = OrderedDict() + same_size_vertices = dict() for vertex in vertices: diff --git a/pacman/utilities/algorithm_utilities/placer_algorithm_utilities.py b/pacman/utilities/algorithm_utilities/placer_algorithm_utilities.py index fbba11dfa..397db0d14 100644 --- a/pacman/utilities/algorithm_utilities/placer_algorithm_utilities.py +++ b/pacman/utilities/algorithm_utilities/placer_algorithm_utilities.py @@ -14,7 +14,6 @@ # along with this program. If not, see . import functools -from collections import OrderedDict from pacman.model.resources import ResourceContainer, ConstantSDRAM from spinn_utilities.ordered_set import OrderedSet @@ -75,7 +74,7 @@ def get_same_chip_vertex_groups(graph): get_vertices_on_same_chip, graph=graph)) # Dict of vertex to set of vertices on same chip (repeated lists expected) # A empty set value indicates a set that is too big. - same_chip_vertices = OrderedDict() + same_chip_vertices = dict() for group in groups: for vertex in group: same_chip_vertices[vertex] = group diff --git a/pacman/utilities/algorithm_utilities/routing_info_allocator_utilities.py b/pacman/utilities/algorithm_utilities/routing_info_allocator_utilities.py index 4f3c02d54..70669a0df 100644 --- a/pacman/utilities/algorithm_utilities/routing_info_allocator_utilities.py +++ b/pacman/utilities/algorithm_utilities/routing_info_allocator_utilities.py @@ -13,7 +13,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -from collections import OrderedDict import logging from spinn_utilities.log import FormatAdapter from spinn_utilities.ordered_set import OrderedSet @@ -81,7 +80,7 @@ def get_mulitcast_edge_groups(machine_graph): """ # mapping between partition and shared key group it is in - partition_groups = OrderedDict() + partition_groups = dict() # process each partition one by one in a bubble sort kinda way for vertex in machine_graph.vertices: diff --git a/pacman/utilities/json_utils.py b/pacman/utilities/json_utils.py index 201443685..454f6f677 100644 --- a/pacman/utilities/json_utils.py +++ b/pacman/utilities/json_utils.py @@ -16,7 +16,6 @@ Miscellaneous minor functions for converting between JSON and Python objects. """ -from collections import OrderedDict import json import gzip from pacman.model.constraints.key_allocator_constraints import ( @@ -78,7 +77,7 @@ def constraint_to_json(constraint): :return: A dict describing the constraint :rtype: dict """ - json_dict = OrderedDict() + json_dict = dict() try: json_dict["class"] = constraint.__class__.__name__ if isinstance(constraint, BoardConstraint): @@ -167,7 +166,7 @@ def constraints_from_json(json_list, graph): def key_mask_to_json(key_mask): try: - json_object = OrderedDict() + json_object = dict() json_object["key"] = key_mask.key json_object["mask"] = key_mask.mask except Exception as ex: # pylint: disable=broad-except @@ -194,7 +193,7 @@ def key_masks_from_json(json_list): def resource_container_to_json(container): - json_dict = OrderedDict() + json_dict = dict() try: json_dict["dtcm"] = container.dtcm.get_value() json_dict["cpu_cycles"] = container.cpu_cycles.get_value() @@ -221,7 +220,7 @@ def resource_container_from_json(json_dict): def iptag_resource_to_json(iptag): - json_dict = OrderedDict() + json_dict = dict() try: json_dict["ip_address"] = iptag.ip_address if iptag.port is not None: @@ -258,7 +257,7 @@ def iptag_resources_from_json(json_list): def vertex_to_json(vertex): - json_dict = OrderedDict() + json_dict = dict() try: json_dict["class"] = vertex.__class__.__name__ json_dict["label"] = vertex.label @@ -289,7 +288,7 @@ def vertex_add_contstraints_from_json(json_dict, graph): def edge_to_json(edge): - json_dict = OrderedDict() + json_dict = dict() try: json_dict["pre_vertex"] = edge.pre_vertex.label json_dict["post_vertex"] = edge.post_vertex.label @@ -312,7 +311,7 @@ def edge_from_json(json_dict, graph=None): def graph_to_json(graph): # TODO Appplication vertex info needed for ZonedRoutingInfoAllocator - json_dict = OrderedDict() + json_dict = dict() try: if graph.label is not None: json_dict["label"] = graph.label @@ -350,7 +349,7 @@ def vertex_lookup(label, graph=None): def placement_to_json(placement): - json_dict = OrderedDict() + json_dict = dict() try: json_dict["vertex_label"] = placement.vertex.label json_dict["x"] = placement.x @@ -377,7 +376,7 @@ def placement_from_json(json_dict, graph=None): def partition_to_n_keys_map_to_json(partition_to_n_keys_map): json_list = [] for partition in partition_to_n_keys_map: - json_dict = OrderedDict() + json_dict = dict() try: json_dict["pre_vertex_label"] = partition.pre_vertex.label json_dict["identifier"] = partition.identifier From 987f6f7f5b7e47b5b4a8798b6b14bde711102a72 Mon Sep 17 00:00:00 2001 From: "Christian Y. Brenninkmeijer" Date: Thu, 17 Mar 2022 16:09:53 +0000 Subject: [PATCH 3/3] flake8 --- pacman/model/graphs/machine/machine_graph.py | 2 +- .../multicast_routing_table_by_partition.py | 1 - pacman/utilities/vertex_sorter.py | 1 + 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pacman/model/graphs/machine/machine_graph.py b/pacman/model/graphs/machine/machine_graph.py index 0957d99d5..05be4e2d8 100644 --- a/pacman/model/graphs/machine/machine_graph.py +++ b/pacman/model/graphs/machine/machine_graph.py @@ -80,7 +80,7 @@ def __init__(self, label, application_graph=None): # Must be false as there is no App_graph self._application_level_used = False self._multicast_partitions = defaultdict( - lambda:defaultdict(set)) + lambda: defaultdict(set)) self._edge_partitions = OrderedSet() self._fixed_route_edge_partitions_by_pre_vertex = ( defaultdict(OrderedSet)) diff --git a/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py b/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py index dab7c7d21..f49755ca0 100644 --- a/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py +++ b/pacman/model/routing_table_by_partition/multicast_routing_table_by_partition.py @@ -14,7 +14,6 @@ # along with this program. If not, see . - class MulticastRoutingTableByPartition(object): """ A set of multicast routing path objects """ diff --git a/pacman/utilities/vertex_sorter.py b/pacman/utilities/vertex_sorter.py index ce513ce39..8da0ceaf3 100644 --- a/pacman/utilities/vertex_sorter.py +++ b/pacman/utilities/vertex_sorter.py @@ -16,6 +16,7 @@ from collections import defaultdict import sys + class ConstraintOrder(object): """ A constraint order definition for sorting. """