Skip to content

Commit

Permalink
Merge branch 'placement_json' into pylint_default
Browse files Browse the repository at this point in the history
# Conflicts:
#	pacman/utilities/json_utils.py
  • Loading branch information
Christian-B committed Feb 5, 2024
2 parents 0860981 + 62355d2 commit cd75f48
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
62 changes: 24 additions & 38 deletions pacman/utilities/json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@

import json
import gzip
from typing import List, Optional, Union
from typing import cast, Iterable, List, Union

from spinn_utilities.typing.json import JsonArray, JsonObject

from pacman.data import PacmanDataView
from pacman.model.graphs.application import (
ApplicationGraph, ApplicationVertex)
from pacman.model.graphs.machine import MachineVertex, SimpleMachineVertex
from pacman.model.placements.placement import Placement
from pacman.model.resources import (
Expand Down Expand Up @@ -94,14 +92,13 @@ def iptag_resource_from_json(json_dict: JsonObject) -> IPtagResource:
:param dict(str, object) json_dict:
:rtype: IPtagResource
"""
port = json_dict.get("port")
tag = json_dict.get("tag")
return IPtagResource(
json_dict["ip_address"], port, json_dict["strip_sdp"], tag,
json_dict["traffic_identifier"])
cast(str, json_dict["ip_address"]), cast(int, json_dict.get("port")),
cast(bool, json_dict["strip_sdp"]), cast(int, json_dict.get("tag")),
cast(str, json_dict["traffic_identifier"]))


def iptag_resources_to_json(iptags: List[IPtagResource]) -> JsonArray:
def iptag_resources_to_json(iptags: Iterable[IPtagResource]) -> JsonArray:
"""
Converts a list of iptags to json.
Expand All @@ -114,7 +111,8 @@ def iptag_resources_to_json(iptags: List[IPtagResource]) -> JsonArray:
return json_list


def iptag_resources_from_json(json_list: JsonArray) -> List[IPtagResource]:
def iptag_resources_from_json(
json_list: List[JsonObject]) -> List[IPtagResource]:
"""
Creates a list of iptags from json.
Expand Down Expand Up @@ -153,13 +151,14 @@ def reverse_iptag_from_json(json_dict: JsonObject) -> ReverseIPtagResource:
:param dict(str, object) json_dict:
:rtype: ReverseIPtagResource
"""
port = json_dict.get("port")
sdp_port = json_dict["sdp_port"]
tag = json_dict.get("tag")
port = cast(int, json_dict.get("port"))
sdp_port = cast(int, json_dict["sdp_port"])
tag = cast(int, json_dict.get("tag"))
return ReverseIPtagResource(port, sdp_port, tag)


def reverse_iptags_to_json(iptags: List[ReverseIPtagResource]) -> JsonArray:
def reverse_iptags_to_json(
iptags: Iterable[ReverseIPtagResource]) -> JsonArray:
"""
Converts a list of reverse iptags to json
Expand All @@ -173,7 +172,7 @@ def reverse_iptags_to_json(iptags: List[ReverseIPtagResource]) -> JsonArray:


def reverse_iptags_from_json(
json_list: JsonArray) -> List[ReverseIPtagResource]:
json_list: List[JsonObject]) -> List[ReverseIPtagResource]:
"""
Creates a list of ReverseIPtagResource from json
Expand Down Expand Up @@ -216,29 +215,17 @@ def vertex_from_json(json_dict: JsonObject) -> SimpleMachineVertex:
:rtype: SimpleMachineVertex
"""
sdram = VariableSDRAM(
json_dict["fixed_sdram"], json_dict["per_timestep_sdram"])
iptags = iptag_resources_from_json(json_dict["iptags"])
reverse_iptags = reverse_iptags_from_json(json_dict["reverse_iptags"])
cast(int, json_dict["fixed_sdram"]),
cast(float, json_dict["per_timestep_sdram"]))
iptags = iptag_resources_from_json(
cast(List[JsonObject], json_dict["iptags"]))
reverse_iptags = reverse_iptags_from_json(
cast(List[JsonObject], json_dict["reverse_iptags"]))
return SimpleMachineVertex(
sdram=sdram, label=json_dict["label"], iptags=iptags,
reverse_iptags=reverse_iptags)


def vertex_lookup(
label, graph: Optional[ApplicationGraph] = None) -> ApplicationVertex:
"""
Looks up the vertex in the graph or create a simple vertex if no graph
:param str label:
:param graph: ApplicationGraph if applicable
:type graph: ApplicationGraph or None
:rtype: ApplicationVertex
"""
if graph:
return graph.vertex_by_label(label)
return SimpleMachineVertex(None, label)


def placement_to_json(placement: Placement) -> JsonObject:
"""
Converts a Placement to json
Expand Down Expand Up @@ -267,16 +254,15 @@ def placements_to_json() -> JsonArray:
return json_list


def placement_from_json(json_dict: JsonObject,
graph: Optional[ApplicationGraph] = None) -> Placement:
def placement_from_json(json_dict: JsonObject) -> Placement:
"""
Gets a Placement based on the json.
:param dict(str, object) json_dict:
:param graph: Application Graph (if used)
:type graph: ApplicationGraph or None
:rtype: Placement
"""
vertex = vertex_lookup(json_dict["vertex_label"], graph)
vertex = SimpleMachineVertex(None, cast(str, json_dict["vertex_label"]))
# The cast(int tells mypy to assume the value can be converted to an int
return Placement(
vertex, int(json_dict["x"]), int(json_dict["y"]), int(json_dict["p"]))
vertex, int(cast(int, json_dict["x"])),
int(cast(int, json_dict["y"])), int(cast(int, json_dict["p"])))
26 changes: 25 additions & 1 deletion unittests/utilities_tests/test_json_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import unittest
import json
from pacman.config_setup import unittest_setup
from pacman.model.placements import Placement
from pacman.model.resources import (
ConstantSDRAM, IPtagResource, ReverseIPtagResource)
from pacman.utilities.json_utils import (vertex_to_json, vertex_from_json)
from pacman.utilities.json_utils import (
placement_from_json, placement_to_json, vertex_to_json, vertex_from_json)
from pacman.model.graphs.machine import SimpleMachineVertex


Expand Down Expand Up @@ -47,6 +49,12 @@ def _compare_vertex(self, v1, v2, seen=None):
self.assertEqual(v1.sdram_required, v2.sdram_required)
seen.append(v1.label)

def _compare_placement(self, p1, p2, seen=None):
self.assertEqual(p1.x, p2.x)
self.assertEqual(p1.y, p2.y)
self.assertEqual(p1.p, p2.p)
self.assertEqual(p1.vertex.label, p2.vertex.label)

# ------------------------------------------------------------------
# Composite JSON round-trip testing schemes
# ------------------------------------------------------------------
Expand All @@ -58,6 +66,13 @@ def vertex_there_and_back(self, there):
back = vertex_from_json(j_object2)
self._compare_vertex(there, back)

def placement_there_and_back(self, there):
j_object = placement_to_json(there)
j_str = json.dumps(j_object)
j_object2 = json.loads(j_str)
back = placement_from_json(j_object2)
self._compare_placement(there, back)

# ------------------------------------------------------------------
# Test cases
# ------------------------------------------------------------------
Expand All @@ -69,3 +84,12 @@ def test_vertex(self):
reverse_iptags=[ReverseIPtagResource(port=25, sdp_port=2, tag=5)],
label="Vertex")
self.vertex_there_and_back(s1)

def test_placement(self):
s1 = SimpleMachineVertex(
sdram=ConstantSDRAM(0),
iptags=[IPtagResource("127.0.0.1", port=None, strip_sdp=True)],
reverse_iptags=[ReverseIPtagResource(port=25, sdp_port=2, tag=5)],
label="PVertex")
p1 = Placement(s1, 1, 2, 3)
self.placement_there_and_back(p1)

0 comments on commit cd75f48

Please sign in to comment.