Skip to content

Commit

Permalink
Merge pull request #86 from SpiNNakerManchester/overrides_check
Browse files Browse the repository at this point in the history
Overrides check
  • Loading branch information
Christian-B authored Jan 15, 2024
2 parents 8ffea86 + 3d63879 commit b35b2af
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 68 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/python_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ jobs:
path: support
- name: Install pip, etc
uses: ./support/actions/python-tools
- name: Install mypy
run: pip install mypy

- name: Install Spinnaker Dependencies
uses: ./support/actions/install-spinn-deps
Expand Down Expand Up @@ -90,3 +92,5 @@ jobs:
if: matrix.python-version == 3.12
uses: dieghernan/cff-validator@main

- name: Lint with mypy
run: mypy spinn_gym
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta:"
build-backend = "setuptools.build_meta:"

[[tool.mypy.overrides]]
module = ["pyNN.*", "quantities", "neo", "scipy", "scipy.*", "lazyarray", "IPython", "matplotlib.*"]
ignore_missing_imports = true
4 changes: 2 additions & 2 deletions spinn_gym/games/breakout/breakout.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Breakout(SpinnGymApplicationVertex):
numpy.random.randint(10000),
numpy.random.randint(10000)]

__slots__ = ["__source_vertex"]
__slots__ = ("__source_vertex")

def __init__(self, x_factor=16, y_factor=16, width=160, height=128,
colour_bits=2, label="Breakout",
Expand All @@ -60,7 +60,7 @@ def __init__(self, x_factor=16, y_factor=16, width=160, height=128,

@property
@overrides(SpinnGymApplicationVertex.score_format)
def score_format(self):
def score_format(self) -> type:
return numpy.int32

@property
Expand Down
33 changes: 25 additions & 8 deletions spinn_gym/games/breakout/breakout_machine_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from enum import Enum
from typing import cast, TYPE_CHECKING

from spinn_utilities.overrides import overrides

from pacman.model.placements import Placement

# SpinnFrontEndCommon imports
from spinn_front_end_common.utilities import helpful_functions
from spinn_front_end_common.abstract_models.\
Expand All @@ -27,7 +30,8 @@
import AbstractHasAssociatedBinary
from spinn_front_end_common.interface.buffer_management \
import recording_utilities
from spinn_front_end_common.interface.ds import DataType
from spinn_front_end_common.interface.ds import (
DataSpecificationGenerator, DataType)
from spinn_front_end_common.interface.simulation import simulation_utilities
from spinn_front_end_common.utilities import constants as \
front_end_common_constants
Expand All @@ -39,6 +43,9 @@
# spinn_gym imports
from spinn_gym.games import SpinnGymMachineVertex

if TYPE_CHECKING:
from .breakout import Breakout


# ----------------------------------------------------------------------------
# BreakoutMachineVertex
Expand All @@ -54,10 +61,10 @@ class BreakoutMachineVertex(SpinnGymMachineVertex):
('RECORDING', 2),
('PARAMS', 3)])

__slots__ = ["_x_factor", "_y_factor", "_colour_bits", "_bricking"]
__slots__ = ("_x_factor", "_y_factor", "_colour_bits", "_bricking")

def __init__(
self, label, app_vertex, n_neurons,
self, label, app_vertex: 'Breakout', n_neurons,
simulation_duration_ms, random_seed,
x_factor, y_factor, colour_bits, bricking):
"""
Expand Down Expand Up @@ -96,11 +103,18 @@ def __init__(
self._colour_bits = colour_bits
self._bricking = bricking

@property
@overrides(SpinnGymMachineVertex.app_vertex)
def app_vertex(self) -> 'Breakout':
# type checked by init
return cast('Breakout', self._app_vertex)

# ------------------------------------------------------------------------
# AbstractGeneratesDataSpecification overrides
# ------------------------------------------------------------------------
@overrides(AbstractGeneratesDataSpecification.generate_data_specification)
def generate_data_specification(self, spec, placement):
def generate_data_specification(
self, spec: DataSpecificationGenerator, placement: Placement):
# pylint: disable=arguments-differ
vertex = placement.vertex

Expand All @@ -127,6 +141,7 @@ def generate_data_specification(self, spec, placement):
spec.comment("\nWriting setup region:\n")
spec.switch_write_focus(
BreakoutMachineVertex._BREAKOUT_REGIONS.SYSTEM.value)
assert isinstance(vertex, AbstractHasAssociatedBinary)
spec.write_array(simulation_utilities.get_simulation_header_array(
vertex.get_binary_file_name()))

Expand All @@ -135,8 +150,10 @@ def generate_data_specification(self, spec, placement):
spec.switch_write_focus(
BreakoutMachineVertex._BREAKOUT_REGIONS.BREAKOUT.value)
routing_info = SpynnakerDataView.get_routing_infos()
spec.write_value(routing_info.get_first_key_from_pre_vertex(
vertex, constants.SPIKE_PARTITION_ID))
data = routing_info.get_first_key_from_pre_vertex(
vertex, constants.SPIKE_PARTITION_ID)
assert data is not None
spec.write_value(data)
if self.app_vertex.source_vertex is None:
raise ValueError(
"The breakout vertex doesn't have a source vertex!")
Expand Down Expand Up @@ -168,10 +185,10 @@ def generate_data_specification(self, spec, placement):
spec.end_specification()

@overrides(SpinnGymMachineVertex.get_recording_region_base_address)
def get_recording_region_base_address(self, placement):
def get_recording_region_base_address(self, placement: Placement) -> int:
return helpful_functions.locate_memory_region_for_placement(
placement, self._BREAKOUT_REGIONS.RECORDING.value)

@overrides(AbstractHasAssociatedBinary.get_binary_file_name)
def get_binary_file_name(self):
def get_binary_file_name(self) -> str:
return "breakout.aplx"
4 changes: 2 additions & 2 deletions spinn_gym/games/double_inverted_pendulum/double_pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DoublePendulum(SpinnGymApplicationVertex):
ONE_WEEK_IN_MS = 1000 * 60 * 60 * 24 * 7 # 1 week
RANDOM_SEED = [0, 1, 2, 3]

__slots__ = []
__slots__ = ()

def __init__(
self, encoding=0, time_increment=20,
Expand Down Expand Up @@ -78,5 +78,5 @@ def __init__(

@property
@overrides(SpinnGymApplicationVertex.score_format)
def score_format(self):
def score_format(self) -> type:
return numpy.float32
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

from spinn_utilities.overrides import overrides

from pacman.model.placements import Placement

# SpinnFrontEndCommon imports
from spinn_front_end_common.utilities import helpful_functions
from spinn_front_end_common.abstract_models.abstract_has_associated_binary \
import AbstractHasAssociatedBinary
from spinn_front_end_common.interface.buffer_management \
import recording_utilities
from spinn_front_end_common.interface.ds import DataType
from spinn_front_end_common.interface.ds import (
DataSpecificationGenerator, DataType)
from spinn_front_end_common.interface.simulation import simulation_utilities
from spinn_front_end_common.utilities import constants as \
front_end_common_constants
Expand All @@ -43,11 +46,11 @@ class DoublePendulumMachineVertex(SpinnGymMachineVertex):
PENDULUM_REGION_BYTES = 4
DATA_REGION_BYTES = 17 * 4

__slots__ = [
__slots__ = (
"_bin_overlap", "_central", "_encoding", "_force_increments",
"_max_firing_rate", "_number_of_bins", "_pole_angle", "_pole2_angle",
"_pole_length", "_pole2_length", "_reward_based", "_tau_force",
"_time_increment"]
"_time_increment")

_DOUBLE_PENDULUM_REGIONS = Enum(
value="_DOUBLE_PENDULUM_REGIONS",
Expand Down Expand Up @@ -127,7 +130,8 @@ def __init__(
# AbstractGeneratesDataSpecification overrides
# ------------------------------------------------------------------------
@overrides(SpinnGymMachineVertex.generate_data_specification)
def generate_data_specification(self, spec, placement):
def generate_data_specification(
self, spec: DataSpecificationGenerator, placement: Placement):
# pylint: disable=arguments-differ
vertex = placement.vertex

Expand All @@ -154,6 +158,7 @@ def generate_data_specification(self, spec, placement):
spec.comment("\nWriting setup region:\n")
spec.switch_write_focus(
self._DOUBLE_PENDULUM_REGIONS.SYSTEM.value)
assert isinstance(vertex, AbstractHasAssociatedBinary)
spec.write_array(simulation_utilities.get_simulation_header_array(
vertex.get_binary_file_name()))

Expand All @@ -162,8 +167,10 @@ def generate_data_specification(self, spec, placement):
spec.switch_write_focus(
self._DOUBLE_PENDULUM_REGIONS.PENDULUM.value)
routing_info = SpynnakerDataView.get_routing_infos()
spec.write_value(routing_info.get_first_key_from_pre_vertex(
vertex, constants.SPIKE_PARTITION_ID))
data = routing_info.get_first_key_from_pre_vertex(
vertex, constants.SPIKE_PARTITION_ID)
assert data is not None
spec.write_value(data)

# Write recording region for score
spec.comment("\nWriting double pendulum recording region:\n")
Expand Down Expand Up @@ -198,10 +205,10 @@ def generate_data_specification(self, spec, placement):
spec.end_specification()

@overrides(SpinnGymMachineVertex.get_recording_region_base_address)
def get_recording_region_base_address(self, placement):
def get_recording_region_base_address(self, placement: Placement) -> int:
return helpful_functions.locate_memory_region_for_placement(
placement, self._DOUBLE_PENDULUM_REGIONS.RECORDING.value)

@overrides(AbstractHasAssociatedBinary.get_binary_file_name)
def get_binary_file_name(self):
def get_binary_file_name(self) -> str:
return "double_inverted_pendulum.aplx"
4 changes: 2 additions & 2 deletions spinn_gym/games/inverted_pendulum/inverted_pendulum.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Pendulum(SpinnGymApplicationVertex):
ONE_WEEK_IN_MS = 1000 * 60 * 60 * 24 * 7 # 1 week
RANDOM_SEED = [0, 1, 2, 3]

__slots__ = []
__slots__ = ()

def __init__(self, encoding=0, time_increment=20,
pole_length=1.0, pole_angle=0.1, reward_based=1,
Expand Down Expand Up @@ -80,5 +80,5 @@ def __init__(self, encoding=0, time_increment=20,

@property
@overrides(SpinnGymApplicationVertex.score_format)
def score_format(self):
def score_format(self) -> type:
return numpy.float32
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

from spinn_utilities.overrides import overrides

from pacman.model.placements import Placement

# SpinnFrontEndCommon imports
from spinn_front_end_common.utilities import helpful_functions
from spinn_front_end_common.abstract_models.abstract_has_associated_binary \
Expand All @@ -26,7 +28,8 @@
from spinn_front_end_common.abstract_models \
.abstract_generates_data_specification \
import AbstractGeneratesDataSpecification
from spinn_front_end_common.interface.ds import DataType
from spinn_front_end_common.interface.ds import (
DataSpecificationGenerator, DataType)
from spinn_front_end_common.interface.simulation import simulation_utilities
from spinn_front_end_common.utilities import constants as \
front_end_common_constants
Expand All @@ -53,10 +56,10 @@ class PendulumMachineVertex(SpinnGymMachineVertex):
('RECORDING', 2),
('DATA', 3)])

__slots__ = ["_bin_overlap", "_central", "_encoding", "_force_increments",
__slots__ = ("_bin_overlap", "_central", "_encoding", "_force_increments",
"_max_firing_rate", "_number_of_bins", "_pole_angle",
"_pole_length", "_reward_based", "_tau_force",
"_time_increment"]
"_time_increment")

def __init__(self, label, app_vertex, n_neurons,
simulation_duration_ms, random_seed,
Expand Down Expand Up @@ -125,7 +128,8 @@ def __init__(self, label, app_vertex, n_neurons,
# AbstractGeneratesDataSpecification overrides
# ------------------------------------------------------------------------
@overrides(AbstractGeneratesDataSpecification.generate_data_specification)
def generate_data_specification(self, spec, placement):
def generate_data_specification(
self, spec: DataSpecificationGenerator, placement: Placement):
# pylint: disable=arguments-differ
vertex = placement.vertex

Expand All @@ -152,6 +156,7 @@ def generate_data_specification(self, spec, placement):
spec.comment("\nWriting setup region:\n")
spec.switch_write_focus(
self._PENDULUM_REGIONS.SYSTEM.value)
assert isinstance(vertex, AbstractHasAssociatedBinary)
spec.write_array(simulation_utilities.get_simulation_header_array(
vertex.get_binary_file_name()))

Expand All @@ -160,8 +165,10 @@ def generate_data_specification(self, spec, placement):
spec.switch_write_focus(
self._PENDULUM_REGIONS.PENDULUM.value)
routing_info = SpynnakerDataView.get_routing_infos()
spec.write_value(routing_info.get_first_key_from_pre_vertex(
vertex, constants.SPIKE_PARTITION_ID))
data = routing_info.get_first_key_from_pre_vertex(
vertex, constants.SPIKE_PARTITION_ID)
assert data is not None
spec.write_value(data)

# Write recording region for score
spec.comment("\nWriting pendulum recording region:\n")
Expand Down Expand Up @@ -193,10 +200,10 @@ def generate_data_specification(self, spec, placement):
# End-of-Spec:
spec.end_specification()

def get_recording_region_base_address(self, placement):
def get_recording_region_base_address(self, placement: Placement):
return helpful_functions.locate_memory_region_for_placement(
placement, self._PENDULUM_REGIONS.RECORDING.value)

@overrides(AbstractHasAssociatedBinary.get_binary_file_name)
def get_binary_file_name(self):
def get_binary_file_name(self) -> str:
return "inverted_pendulum.aplx"
4 changes: 2 additions & 2 deletions spinn_gym/games/logic/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Logic(SpinnGymApplicationVertex):
numpy.random.randint(10000),
numpy.random.randint(10000)]

__slots__ = []
__slots__ = ()

def __init__(
self, truth_table, input_sequence, rate_on=20.0, rate_off=5.0,
Expand All @@ -66,5 +66,5 @@ def __init__(

@property
@overrides(SpinnGymApplicationVertex.score_format)
def score_format(self):
def score_format(self) -> type:
return numpy.int32
Loading

0 comments on commit b35b2af

Please sign in to comment.