Skip to content

Commit

Permalink
VersionStrings
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Apr 12, 2024
1 parent f2b414a commit 4a17c2a
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 83 deletions.
2 changes: 2 additions & 0 deletions spinn_machine/spinn_machine.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[Machine]
version = None
# Used Instead of version if multiple versions should be tested!
versions = None
width = None
height = None
# Note: if json_path is set all other configs for virtual boards are ignored
Expand Down
8 changes: 2 additions & 6 deletions spinn_machine/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .version_factory import (
ANY_VERSION, BIG_MACHINE, FIVE, FOUR_PLUS_CHIPS, MULTIPLE_BOARDS,
SPIN2_1CHIP, THREE, version_factory, WRAPPABLE)
from .version_factory import FIVE, SPIN2_1CHIP, THREE, version_factory

__all__ = ["ANY_VERSION", "BIG_MACHINE", "FIVE", "FOUR_PLUS_CHIPS",
"MULTIPLE_BOARDS", "SPIN2_1CHIP", "THREE", "version_factory",
"WRAPPABLE"]
__all__ = ["FIVE", "SPIN2_1CHIP", "THREE", "version_factory"]
17 changes: 7 additions & 10 deletions spinn_machine/version/version_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_config_int_or_none, get_config_str_or_none)
from spinn_utilities.log import FormatAdapter
from spinn_machine.exceptions import SpinnMachineException
from .version_strings import VersionStrings
if TYPE_CHECKING:
from .abstract_version import AbstractVersion

Expand Down Expand Up @@ -62,17 +63,13 @@ def version_factory() -> AbstractVersion:
from .version_201 import Version201

version = get_config_int_or_none("Machine", "version")

if version is not None and version < 0:
if version == ANY_VERSION:
options = [THREE, FIVE, SPIN2_1CHIP]
elif version == FOUR_PLUS_CHIPS:
options = [THREE]
elif version in [BIG_MACHINE, MULTIPLE_BOARDS, WRAPPABLE]:
options = [FIVE]
else:
versions = get_config_str_or_none("Machine", "versions")
if versions is not None:
if version is not None:
raise SpinnMachineException(
f"Unexpected cfg [Machine]version {version}")
f"Both {version=} and {versions=} found in cfg")
vs = VersionStrings.from_String(versions)
options = vs.options
# Use the fact that we run actions against different python versions
minor = sys.version_info.minor
version = options[minor % len(options)]
Expand Down
68 changes: 68 additions & 0 deletions spinn_machine/version/version_strings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright (c) 2024 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from enum import StrEnum
from typing import List
from spinn_machine.exceptions import SpinnMachineException


class VersionStrings(StrEnum):
"""
A description of strings in cfg settings to say test versions
As additional Versions are added this should allow easy testing of these
as far as possible.
"""

ANY = ("Any", [3, 5, 201])
FOUR_PLUS = ("Four plus", [3, 5])
# To test stuff with more than four chips.
BIG = ("Big", [5])
# Specifically to test stuff over multiple boards
MULTIPLE_BOARDS = ("Multiple boards", [5])
# Specifically to test the various wrapping Machine classes
WRAPPABLE = ("Wrappable", [5])

def __new__(cls, *args, **kwds):
s = str.__new__(cls)
s._value_ = args[0]
return s

# ignore the first param since it's already set by __new__
def __init__(self, _: str, versions: List[int]):
self._versions = versions

def __str__(self):
return self.value

@property
def short_str(self):
return self.shorten(self.value)

# this makes sure that the description is read-only
@property
def options(self) -> List[int]:
return self._versions

@classmethod
def shorten(cls, value):
return value.lower().replace("_", "").replace("-", "").replace(" ", "")

@classmethod
def from_String(cls, value):
value_short = cls.shorten(value)
for vs in cls:
if value_short == vs.short_str:
return vs
raise SpinnMachineException(f"No version for {value=}")
20 changes: 11 additions & 9 deletions unittests/data/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from spinn_machine.config_setup import unittest_setup
from spinn_machine.data import MachineDataView
from spinn_machine.data.machine_data_writer import MachineDataWriter
from spinn_machine.version import ANY_VERSION, FOUR_PLUS_CHIPS
from spinn_machine.version import FIVE, THREE
from spinn_machine.version.version_strings import VersionStrings


class TestSimulatorData(unittest.TestCase):

def setUp(self):
unittest_setup()
set_config("Machine", "version", 5)

def test_setup(self):
# What happens before setup depends on the previous test
Expand All @@ -38,12 +38,12 @@ def test_setup(self):
MachineDataView.get_chip_at(1, 1)

def test_mock(self):
MachineDataWriter.mock()
set_config("Machine", "version", FIVE)
self.assertEqual(3, MachineDataView.get_chip_at(3, 5).x)
self.assertEqual(48, MachineDataView.get_machine().n_chips)

def test_machine(self):
set_config("Machine", "version", 3)
set_config("Machine", "version", THREE)
writer = MachineDataWriter.setup()

with self.assertRaises(DataNotYetAvialable):
Expand All @@ -59,6 +59,7 @@ def test_machine(self):
self.assertTrue(MachineDataView.has_machine())

def test_where_is_mocked(self):
set_config("Machine", "versions", VersionStrings.MULTIPLE_BOARDS.value)
writer = MachineDataWriter.mock()
self.assertEqual(
"No Machine created yet",
Expand All @@ -76,6 +77,7 @@ def test_where_is_mocked(self):
MachineDataView.where_is_chip(machine.get_chip_at(2, 8)))

def test_where_is_setup(self):
set_config("Machine", "versions", VersionStrings.MULTIPLE_BOARDS.value)
writer = MachineDataWriter.setup()
self.assertEqual(
"No Machine created yet",
Expand All @@ -98,24 +100,24 @@ def test_where_is_setup(self):

def test_mock_any(self):
# Should work with any version
set_config("Machine", "version", ANY_VERSION)
set_config("Machine", "versions", VersionStrings.ANY.value)
# check there is a value not what it is
machine = MachineDataView.get_machine()
self.assertGreaterEqual(machine.n_chips, 1)

def test_mock_4_or_more(self):
# Should work with any version
set_config("Machine", "version", FOUR_PLUS_CHIPS)
set_config("Machine", "versions", VersionStrings.FOUR_PLUS.value)
# check there is a value not what it is
machine = MachineDataView.get_machine()
self.assertGreaterEqual(machine.n_chips, 4)

def test_mockAny(self):
def test_mock_big(self):
# Should work with any version
set_config("Machine", "version", ANY_VERSION)
set_config("Machine", "versions", VersionStrings.BIG.value)
# check there is a value not what it is
machine = MachineDataView.get_machine()
self.assertGreaterEqual(machine.n_chips, 1)
self.assertGreaterEqual(machine.n_chips, 48)

def test_mock3(self):
# Should work with any version
Expand Down
4 changes: 2 additions & 2 deletions unittests/test_chip.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
from spinn_utilities.ordered_set import OrderedSet
from spinn_machine import Link, Router, Chip
from spinn_machine.config_setup import unittest_setup
from spinn_machine.version import ANY_VERSION
from spinn_machine.version.version_strings import VersionStrings


class TestingChip(unittest.TestCase):

def setUp(self):
unittest_setup()
set_config("Machine", "version", ANY_VERSION)
set_config("Machine", "versions", VersionStrings.ANY.value)
self._x = 0
self._y = 1

Expand Down
12 changes: 6 additions & 6 deletions unittests/test_json_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from spinn_machine.data.machine_data_writer import MachineDataWriter
from spinn_machine.config_setup import unittest_setup
from spinn_machine.json_machine import (machine_from_json, to_json_path)
from spinn_machine.version import (
BIG_MACHINE, FOUR_PLUS_CHIPS, FIVE, MULTIPLE_BOARDS, SPIN2_1CHIP, THREE)
from spinn_machine.version import (FIVE, SPIN2_1CHIP, THREE)
from spinn_machine.version.version_strings import VersionStrings


class TestJsonMachine(unittest.TestCase):
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_json_version_201(self):
self.assertEqual(str(vchip), str(jchip))

def test_json_hole(self):
set_config("Machine", "version", BIG_MACHINE)
set_config("Machine", "versions", VersionStrings.BIG.value)
set_config("Machine", "down_chips", "3,3")
writer = MachineDataWriter.mock()
vm = virtual_machine_by_min_size(5, 5)
Expand All @@ -84,7 +84,7 @@ def test_json_hole(self):
self.assertEqual(str(vchip), str(jchip))

def test_exceptions(self):
set_config("Machine", "version", FOUR_PLUS_CHIPS)
set_config("Machine", "versions", VersionStrings.FOUR_PLUS.value)
writer = MachineDataWriter.mock()
vm = virtual_machine_by_boards(1)
writer.set_machine(vm)
Expand All @@ -104,7 +104,7 @@ def test_exceptions(self):
self.assertEqual(vchip10.tag_ids, chip10.tag_ids)

def test_monitor_exceptions(self):
set_config("Machine", "version", FOUR_PLUS_CHIPS)
set_config("Machine", "versions", VersionStrings.FOUR_PLUS.value)
vm = virtual_machine_by_boards(1)
MachineDataWriter.mock().set_machine(vm)
for chip in vm.chips:
Expand All @@ -121,7 +121,7 @@ def test_monitor_exceptions(self):
machine_from_json(jpath)

def test_ethernet_exceptions(self):
set_config("Machine", "version", MULTIPLE_BOARDS)
set_config("Machine", "versions", VersionStrings.MULTIPLE_BOARDS.value)
vm = virtual_machine_by_boards(2)
MachineDataWriter.mock().set_machine(vm)
eth2 = vm.ethernet_connected_chips[1]
Expand Down
Loading

0 comments on commit 4a17c2a

Please sign in to comment.