Skip to content

Commit

Permalink
Merge pull request #1527 from SpiNNakerManchester/test
Browse files Browse the repository at this point in the history
Fix some new typing errors
  • Loading branch information
rowleya authored Jan 21, 2025
2 parents 2717a40 + 66bd36c commit 7826574
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 19 deletions.
5 changes: 4 additions & 1 deletion spynnaker/plot_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from types import ModuleType
from typing import Optional
import numpy as np
from numpy.typing import NDArray

# pylint: disable=invalid-name
plt: Optional[ModuleType]
try:
Expand Down Expand Up @@ -113,6 +115,7 @@ def heat_plot(data_sets, ylabel=None, title=None):


def _get_colour():
""" Yields a colour"""
yield "b."
yield "g."
yield "r."
Expand Down Expand Up @@ -184,7 +187,7 @@ def plot_spikes(spikes, title="spikes"):
if __name__ == "__main__":
spike_data = np.loadtxt("spikes.csv", delimiter=',')
plot_spikes(spike_data)
doubled_spike_data = np.loadtxt("spikes.csv", delimiter=',')
doubled_spike_data: NDArray = np.loadtxt("spikes.csv", delimiter=',')
for _i, doubled_spike_data_i in enumerate(doubled_spike_data):
doubled_spike_data_i[0] = doubled_spike_data[_i][0] + 5
plot_spikes([spike_data, doubled_spike_data])
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ def gen_connector_params(
synapse_info.n_post_neurons)
has_weights = int(self.__weights is not None)
params = numpy.array([n_values, has_weights], dtype=uint32)
weights: NDArray[uint32]
if self.__weights is None:
weights = numpy.zeros(0, dtype=uint32)
else:
Expand Down
1 change: 1 addition & 0 deletions spynnaker/pyNN/models/neuron/abstract_population_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,7 @@ def get_connections_from_machine(
return self.__connection_cache[app_edge, synapse_info]

# Start with something in the list so that concatenate works
connections: List[ConnectionsArray]
connections = [numpy.zeros(0, dtype=NUMPY_CONNECTORS_DTYPE)]
progress = ProgressBar(
len(self.machine_vertices),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ def get_mean_positive_weight(
pos_weights = conn.kernel_weights[conn.kernel_weights > 0]
if len(pos_weights) == 0:
return 0
return numpy.mean(pos_weights)
return float(numpy.mean(pos_weights))

@overrides(AbstractSupportsSignedWeights.get_mean_negative_weight)
def get_mean_negative_weight(
Expand All @@ -329,7 +329,7 @@ def get_mean_negative_weight(
neg_weights = conn.kernel_weights[conn.kernel_weights < 0]
if len(neg_weights) == 0:
return 0
return numpy.mean(neg_weights)
return float(numpy.mean(neg_weights))

@overrides(AbstractSupportsSignedWeights.get_variance_positive_weight)
def get_variance_positive_weight(
Expand All @@ -338,7 +338,7 @@ def get_variance_positive_weight(
pos_weights = conn.kernel_weights[conn.kernel_weights > 0]
if len(pos_weights) == 0:
return 0
return numpy.var(pos_weights)
return float(numpy.var(pos_weights))

@overrides(AbstractSupportsSignedWeights.get_variance_negative_weight)
def get_variance_negative_weight(
Expand All @@ -347,4 +347,4 @@ def get_variance_negative_weight(
neg_weights = conn.kernel_weights[conn.kernel_weights < 0]
if len(neg_weights) == 0:
return 0
return numpy.var(neg_weights)
return float(numpy.var(neg_weights))
22 changes: 12 additions & 10 deletions spynnaker/pyNN/models/neuron/population_machine_neurons.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,14 +317,15 @@ def _write_neuron_core_parameters(
# Write the keys
spec.write_array(keys)

def __in_selector(self, n: int, selector: Selector) -> bool:
def __in_selector(
self, n: Union[int, numpy.integer], selector: Selector) -> bool:
if isinstance(selector, Container):
return n in selector
return n == selector

def _write_current_source_parameters(
self, spec: DataSpecificationBase):
n_atoms = self._vertex_slice.n_atoms
n_atoms: int = self._vertex_slice.n_atoms

spec.comment(
f"\nWriting Current Source Parameters for {n_atoms} Neurons:\n")
Expand All @@ -350,7 +351,7 @@ def _write_current_source_parameters(
if current_sources:
# Array to keep track of the number of each type of current source
# (there are four, but they are numbered 1 to 4, so five elements)
cs_index_array = [0, 0, 0, 0, 0]
cs_index_array: List[int] = [0, 0, 0, 0, 0]

# Data sent to the machine will be current sources per neuron
# This array will have the first entry indicating the number of
Expand All @@ -363,9 +364,10 @@ def _write_current_source_parameters(
cs_id = current_source.current_source_id

# Only use IDs that are on this core
for i, n in enumerate(self._vertex_slice.get_raster_ids()):
for i, raster_id in enumerate(
self._vertex_slice.get_raster_ids()):
if self.__in_selector(
n, current_source_id_list[current_source]):
raster_id, current_source_id_list[current_source]):
# I think this is now right, but test it more...
neuron_current_sources[i][0] += 1
neuron_current_sources[i].append(cs_id)
Expand All @@ -378,16 +380,16 @@ def _write_current_source_parameters(

# Now loop over the neurons on this core and write the current
# source ID and index for sources attached to each neuron
for n in range(n_atoms):
n_current_sources = neuron_current_sources[n][0]
for atom in range(n_atoms):
n_current_sources = neuron_current_sources[atom][0]
spec.write_value(n_current_sources)
if n_current_sources != 0:
for csid in range(n_current_sources * 2):
spec.write_value(neuron_current_sources[n][csid+1])
spec.write_value(neuron_current_sources[atom][csid+1])

# Write the number of each type of current source
for n in range(1, len(cs_index_array)):
spec.write_value(cs_index_array[n])
for cs_index in range(1, len(cs_index_array)):
spec.write_value(cs_index_array[cs_index])

# Now loop over the current sources and write the data required
# for each type of current source
Expand Down
2 changes: 2 additions & 0 deletions spynnaker/pyNN/models/neuron/synapse_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ def _get_row_data(
# Blank the plastic data
fp_data = numpy.zeros((n_rows, 0), dtype=uint32)
pp_data = numpy.zeros((n_rows, 0), dtype=uint32)
fp_size: NDArray[uint32]
fp_size = numpy.zeros((n_rows, 1), dtype=uint32)
pp_size: NDArray[uint32]
pp_size = numpy.zeros((n_rows, 1), dtype=uint32)
else:
assert isinstance(synapse_dynamics, AbstractPlasticSynapseDynamics)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def __init__(
self.__spike_recorder = MultiSpikeRecorder()

if max_rate is None:
all_rates = list(_flatten(self.__data["rates"]))
all_rates: List[numpy.floating] = list(
_flatten(self.__data["rates"]))
self.__max_rate = numpy.amax(all_rates) if all_rates else 0
else:
self.__max_rate = max_rate
Expand Down
6 changes: 3 additions & 3 deletions spynnaker/pyNN/utilities/neo_buffer_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import re
import struct
from typing import (
Any, Collection, Dict, Iterable, List, Optional, Sequence, Tuple, Union,
TYPE_CHECKING)
Any, Collection, Dict, Iterable, List, Optional, Sequence, Set, Tuple,
TYPE_CHECKING, Union)

import numpy
from numpy import floating, integer, uint8, uint32
Expand Down Expand Up @@ -785,7 +785,7 @@ def __combine_indexes(
data_set = set(data_indexes)
indexes = [i for i in view_indexes if i in data_set]
# check for missing and report
view_set = set(view_indexes)
view_set: Set[int] = set(view_indexes)
missing = view_set.difference(data_indexes)
if missing:
logger.warning("No {} available for neurons {}",
Expand Down

0 comments on commit 7826574

Please sign in to comment.