Skip to content

Commit

Permalink
Merge pull request #1466 from SpiNNakerManchester/sets_for_speed
Browse files Browse the repository at this point in the history
Sets for speed
  • Loading branch information
rowleya authored Jul 16, 2024
2 parents aa78940 + 6b2be73 commit 1eae4df
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
14 changes: 9 additions & 5 deletions spynnaker/pyNN/data/spynnaker_data_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from __future__ import annotations
import logging
from typing import (
Iterator, List, Optional, Set, Tuple, Type, Union, TYPE_CHECKING)
Iterator, Optional, Set, Tuple, Type, Union, TYPE_CHECKING)
from spinn_utilities.log import FormatAdapter
from spinn_front_end_common.data import FecDataView
from spynnaker import _version
Expand Down Expand Up @@ -70,8 +70,8 @@ def _clear(self) -> None:
self._min_delay: Optional[float] = None
# Using a dict to verify if later could be stored here only
self._neurons_per_core_set: Set[Type[AbstractPyNNModel]] = set()
self._populations: List[Population] = []
self._projections: List[Projection] = []
self._populations: Set[Population] = set()
self._projections: Set[Projection] = set()
self._segment_counter = 0

def _hard_reset(self) -> None:
Expand Down Expand Up @@ -139,6 +139,8 @@ def iterate_projections(cls) -> Iterator[Projection]:
The iteration will be empty if no projections added.
Note: This method is backed by a set so does not guarantee order
:rtype: iterable(Projection)
"""
return iter(cls.__spy_data._projections)
Expand Down Expand Up @@ -174,7 +176,7 @@ def add_projection(cls, projection: Projection):
"This method should only be called from the Projection init")
if not isinstance(projection, Proj):
raise TypeError("The projection must be a Projection")
cls.__spy_data._projections.append(projection)
cls.__spy_data._projections.add(projection)

@classmethod
def iterate_populations(cls) -> Iterator[Population]:
Expand All @@ -183,6 +185,8 @@ def iterate_populations(cls) -> Iterator[Population]:
The iteration will be empty if no populations added.
Note: This method is backed by a set so does not guarantee order
:rtype: iterable(~spynnaker.pyNN.models.populations.Population)
"""
return iter(cls.__spy_data._populations)
Expand Down Expand Up @@ -229,7 +233,7 @@ def add_population(cls, population: Population):
"This method should only be called from the Population init")
first_id = cls.__spy_data._id_counter
cls.__spy_data._id_counter += population.size
cls.__spy_data._populations.append(population)
cls.__spy_data._populations.add(population)
return first_id, cls.__spy_data._id_counter-1

@classmethod
Expand Down
6 changes: 5 additions & 1 deletion spynnaker/pyNN/utilities/neo_buffer_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ def __get_region_metadata(self, rec_id: int) -> Iterable[Tuple[
SELECT region_id, recording_neurons_st, vertex_slice, base_key
FROM region_metadata
WHERE rec_id = ?
ORDER BY region_metadata_id
ORDER BY region_id, recording_neurons_st, vertex_slice,
base_key
""", (rec_id,))):
vertex_slice = MDSlice.from_string(
self._string(row["vertex_slice"]))
Expand Down Expand Up @@ -1459,6 +1460,9 @@ def write_metadata(self) -> None:
"""
Write the current metadata to the database.
The underlying call does not guarantee order
so there order the metadata is added is not consistent,
.. note::
The database must be writable for this to work!
"""
Expand Down
16 changes: 8 additions & 8 deletions unittests/data/test_simulator_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def test_populations_and_projections(self):
self.assertEqual(5, writer._SpynnakerDataWriter__spy_data._id_counter)
pop_2 = Population(size=15, cellclass=model)

self.assertListEqual(
[pop_1, pop_2], list(SpynnakerDataView.iterate_populations()))
self.assertListEqual([pop_1, pop_2], sorted(
SpynnakerDataView.iterate_populations(), key=lambda x: x.label))
self.assertEqual(2, SpynnakerDataView.get_n_populations())
# Hack to check internal data
# DO NOT COPY as unsupported
Expand All @@ -114,8 +114,8 @@ def test_populations_and_projections(self):
self.assertEqual(1, SpynnakerDataView.get_n_projections())
pro_2 = Projection(
pop_2, pop_1, OneToOneConnector(), receptor_type='excitatory')
self.assertListEqual(
[pro_1, pro_2], list(SpynnakerDataView.iterate_projections()))
self.assertListEqual([pro_1, pro_2], sorted(
SpynnakerDataView.iterate_projections(), key=lambda x: x.label))
self.assertEqual(2, SpynnakerDataView.get_n_projections())
writer.start_run()
# Unable to add while running
Expand All @@ -127,11 +127,11 @@ def test_populations_and_projections(self):
writer.finish_run()
writer.hard_reset()
# population not changed by hard reset
self.assertListEqual(
[pop_1, pop_2], list(SpynnakerDataView.iterate_populations()))
self.assertListEqual([pop_1, pop_2], sorted(
SpynnakerDataView.iterate_populations(), key=lambda x: x.label))
self.assertEqual(2, SpynnakerDataView.get_n_populations())
self.assertListEqual(
[pro_1, pro_2], list(SpynnakerDataView.iterate_projections()))
self.assertListEqual([pro_1, pro_2], sorted(
SpynnakerDataView.iterate_projections(), key=lambda x: x.label))
self.assertEqual(2, SpynnakerDataView.get_n_projections())
self.assertEqual(20, writer._SpynnakerDataWriter__spy_data._id_counter)
with self.assertRaises(TypeError):
Expand Down

0 comments on commit 1eae4df

Please sign in to comment.