Skip to content

Commit

Permalink
Merge pull request #1110 from SpiNNakerManchester/get_weight_in_from_…
Browse files Browse the repository at this point in the history
…list

Make return value from projection.get(.., 'list') usable in FromListConnector
  • Loading branch information
rowleya authored Oct 18, 2021
2 parents 40dc454 + 93033b5 commit 632dd5d
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 43 deletions.
15 changes: 12 additions & 3 deletions spynnaker/pyNN/models/neuron/connection_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,24 @@ def _get_data_items(self):
# all the data
if (self.__data_items_to_return is None or
not self.__data_items_to_return):
self.__data_items = connections[order]
data_items = connections[order]
# There is more than one item to return, so let numpy do its magic
elif len(self.__data_items_to_return) > 1:
self.__data_items = \
data_items = \
connections[order][self.__data_items_to_return]
# There is 1 item to return, so make sure only one item exists
else:
self.__data_items = \
data_items = \
connections[order][self.__data_items_to_return[0]]

# Return in a format which can be understood by a FromListConnector
self.__data_items = []
for data_item in data_items:
data_item_list = data_item
if hasattr(data_item_list, "__len__"):
data_item_list = list(data_item)
self.__data_items.append(data_item_list)

else:
if self.__data_items_to_return is None:
return []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ def using_population_views(self):
sim.run(1)
weights = conn.get(['weight', 'delay'], 'list')
sim.end()
target = [(1, 2, 0.5, 2.), (1, 3, 0.5, 2.), (2, 2, 0.5, 2.),
(2, 3, 0.5, 2.)]
self.assertEqual(weights.tolist(), target)
target = [[1, 2, 0.5, 2.], [1, 3, 0.5, 2.], [2, 2, 0.5, 2.],
[2, 3, 0.5, 2.]]
self.assertCountEqual(weights, target)

def test_using_population_views(self):
self.runsafe(self.using_population_views)
46 changes: 24 additions & 22 deletions spynnaker_integration_tests/test_connectors/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def check_connection(self, projection, destination, connections, repeats,
counts = self.calc_spikes_received(v)

expected = numpy.zeros([SOURCES, n_destinations])
for (src, dest, _) in weights:
for weight in weights:
src = weight[0]
dest = weight[1]
expected[src][dest] += 1
the_max = max(map(max, counts))
if not numpy.array_equal(expected, counts):
Expand Down Expand Up @@ -237,8 +239,8 @@ def onetoone_population_views(self):
sim.run(1)
weights = conn.get(['weight', 'delay'], 'list')
sim.end()
target = [(1, 2, 0.5, 2.), (2, 3, 0.5, 2.)]
self.assertEqual(weights.tolist(), target)
target = [[1, 2, 0.5, 2.], [2, 3, 0.5, 2.]]
self.assertCountEqual(weights, target)

def test_onetoone_population_views(self):
self.runsafe(self.onetoone_population_views)
Expand All @@ -259,9 +261,9 @@ def onetoone_multicore_population_views(self):
edge.label == 'machine_edge_for_test')]
sim.end()
# Check the connections are correct
target = [(6, 9, 0.5, 2.), (7, 10, 0.5, 2.), (8, 11, 0.5, 2.),
(9, 12, 0.5, 2.), (10, 13, 0.5, 2.), (11, 14, 0.5, 2.)]
self.assertEqual(weights.tolist(), target)
target = [[6, 9, 0.5, 2.], [7, 10, 0.5, 2.], [8, 11, 0.5, 2.],
[9, 12, 0.5, 2.], [10, 13, 0.5, 2.], [11, 14, 0.5, 2.]]
self.assertCountEqual(weights, target)
# In this instance there should be three MachineEdges: one of the four
# possible at the start should have been filtered out
self.assertEqual(len(projection_edges), 3)
Expand All @@ -278,8 +280,8 @@ def fixedprob_population_views(self):
weights = conn.get(['weight', 'delay'], 'list')
sim.end()
# The fixed seed means this gives the same answer each time
target = [(1, 3, 0.5, 2.), (2, 2, 0.5, 2.), (2, 3, 0.5, 2)]
self.assertEqual(weights.tolist(), target)
target = [[1, 3, 0.5, 2.], [2, 2, 0.5, 2.], [2, 3, 0.5, 2]]
self.assertCountEqual(weights, target)

def test_fixedprob_population_views(self):
self.runsafe(self.fixedprob_population_views)
Expand All @@ -296,9 +298,9 @@ def fixedpre_population_views(self):
weights = conn.get(['weight', 'delay'], 'list')
sim.end()
# The fixed seed means this gives the same answer each time
target = [(1, 1, 0.5, 2.0), (1, 2, 0.5, 2.0), (1, 3, 0.5, 2.0),
(2, 1, 0.5, 2.0), (2, 2, 0.5, 2.0), (2, 3, 0.5, 2.0)]
self.assertEqual(weights.tolist(), target)
target = [[1, 1, 0.5, 2.0], [1, 2, 0.5, 2.0], [1, 3, 0.5, 2.0],
[2, 1, 0.5, 2.0], [2, 2, 0.5, 2.0], [2, 3, 0.5, 2.0]]
self.assertCountEqual(weights, target)

def test_fixedpre_population_views(self):
self.runsafe(self.fixedpre_population_views)
Expand All @@ -315,9 +317,9 @@ def fixedpost_population_views(self):
weights = conn.get(['weight', 'delay'], 'list')
sim.end()
# The fixed seed means this gives the same answer each time
target = [(0, 1, 0.5, 2.0), (0, 3, 0.5, 2.0), (1, 1, 0.5, 2.0),
(1, 3, 0.5, 2.0), (2, 1, 0.5, 2.0), (2, 2, 0.5, 2.0)]
self.assertEqual(weights.tolist(), target)
target = [[0, 1, 0.5, 2.0], [0, 3, 0.5, 2.0], [1, 1, 0.5, 2.0],
[1, 3, 0.5, 2.0], [2, 1, 0.5, 2.0], [2, 2, 0.5, 2.0]]
self.assertCountEqual(weights, target)

def test_fixedpost_population_views(self):
self.runsafe(self.fixedpost_population_views)
Expand All @@ -341,14 +343,14 @@ def fixedtotal_population_views(self):
weights2 = conn2.get(['weight', 'delay'], 'list')
sim.end()
# The fixed seed means this gives the same answer each time
target = [(0, 2, 0.5, 2.0), (0, 3, 0.5, 2.0), (1, 1, 0.5, 2.0),
(1, 3, 0.5, 2.0), (2, 1, 0.5, 2.0)]
target2 = [(0, 2, 0.5, 2.0), (0, 2, 0.5, 2.0), (1, 1, 0.5, 2.0),
(2, 2, 0.5, 2.0), (2, 3, 0.5, 2.0)]
self.assertEqual(weights.tolist(), target)
self.assertEqual(len(weights.tolist()), n_conns)
self.assertEqual(weights2.tolist(), target2)
self.assertEqual(len(weights2.tolist()), n_conns)
target = [[0, 2, 0.5, 2.0], [0, 3, 0.5, 2.0], [1, 1, 0.5, 2.0],
[1, 3, 0.5, 2.0], [2, 1, 0.5, 2.0]]
target2 = [[0, 2, 0.5, 2.0], [0, 2, 0.5, 2.0], [1, 1, 0.5, 2.0],
[2, 2, 0.5, 2.0], [2, 3, 0.5, 2.0]]
self.assertCountEqual(weights, target)
self.assertEqual(len(weights), n_conns)
self.assertCountEqual(weights2, target2)
self.assertEqual(len(weights2), n_conns)

def test_fixedtotal_population_views(self):
self.runsafe(self.fixedtotal_population_views)
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) 2017-2019 The University of Manchester
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import spynnaker8 as sim
from spinnaker_testbase import BaseTestCase


class TestUseProjectionGetInFromList(BaseTestCase):

def do_run(self):
sim.setup(timestep=1.0)
sim.set_number_of_neurons_per_core(sim.IF_curr_exp, 5)

n_neurons = 10
weights = 0.5
delays = 7
n_pre = 2

p1 = sim.Population(n_neurons, sim.IF_curr_exp, {}, label='pop1_1')
p2 = sim.Population(n_neurons, sim.IF_curr_exp, {}, label='pop1_2')

connector_pre = sim.FixedNumberPreConnector(n_pre)

proj_pre = sim.Projection(p1, p2, connector_pre,
synapse_type=sim.StaticSynapse(
weight=weights, delay=delays))

sim.run(10)

weights_delays_pre = proj_pre.get(["weight", "delay"], "list")

sim.end()

sim.setup(timestep=1.0, min_delay=1.0, max_delay=144.0)
sim.set_number_of_neurons_per_core(sim.IF_curr_exp, 5)

p11 = sim.Population(n_neurons, sim.IF_curr_exp, {}, label='pop2_1')
p22 = sim.Population(n_neurons, sim.IF_curr_exp, {}, label='pop2_2')

fromlist_conn = sim.FromListConnector(weights_delays_pre)

proj_new = sim.Projection(p11, p22, fromlist_conn)

sim.run(10)

weights_delays_out = proj_new.get(["weight", "delay"], "list")

sim.end()

self.assertCountEqual(weights_delays_pre, weights_delays_out)

def test_use_projection_get_in_from_list(self):
self.runsafe(self.do_run)
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def compare_before_and_after(self):
assert(os.path.isfile("test_file.txt"))
with open("test_file.txt") as f:
file_weights = numpy.loadtxt(f)
np_weights = post_weights_list.astype(
[('source', '<f8'), ('target', '<f8'), ('weight', '<f8')]).view(
np_list = numpy.array([numpy.array(wl) for wl in post_weights_list])
np_weights = np_list.view(
"float64").reshape((-1, 3))
self.assertTrue(numpy.allclose(file_weights, np_weights))
os.remove("test_file.txt")
Expand Down Expand Up @@ -128,25 +128,25 @@ def test_compare_before_and_after(self):
print(pre_delays_array.shape)
print(pre_delays_array[0])
print("list")
print(pre_delays_list.shape)
print(pre_delays_list[0])
print(len(pre_delays_list))
print(len(pre_delays_list[0]))
print("array")
print(pre_weights_array.shape)
print(pre_weights_array[0])
print("list")
print(pre_weights_list.shape)
print(pre_weights_list[0])
print(len(pre_weights_list))
print(len(pre_weights_list[0]))
print("array")
print(post_delays_array.shape)
print(post_delays_array[0].shape)
print("list")
print(post_delays_list.shape)
print(post_delays_list[0].shape)
print(len(post_delays_list))
print(len(post_delays_list[0]))
print("array")
print(post_weights_array.shape)
print(post_weights_array[0])
print("list")
print(post_weights_list.shape)
print(post_weights_list[0])
print(len(post_weights_list))
print(len(post_weights_list[0]))
with open("test_file.txt") as f:
print(numpy.loadtxt(f))
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ def do_run(self):
conns, num_forms, num_elims, first_f = structural_formation_to_full()
# Should have built all-to-all connectivity
all_to_all_conns = [
(0, 0, 4., 3.), (0, 1, 4., 3.), (0, 2, 4., 3.), (0, 3, 4., 3.),
(1, 0, 4., 3.), (1, 1, 4., 3.), (1, 2, 4., 3.), (1, 3, 4., 3.),
(2, 0, 4., 3.), (2, 1, 4., 3.), (2, 2, 4., 3.), (2, 3, 4., 3.),
(3, 0, 4., 3.), (3, 1, 4., 3.), (3, 2, 4., 3.), (3, 3, 4., 3.)]
[0, 0, 4., 3.], [0, 1, 4., 3.], [0, 2, 4., 3.], [0, 3, 4., 3.],
[1, 0, 4., 3.], [1, 1, 4., 3.], [1, 2, 4., 3.], [1, 3, 4., 3.],
[2, 0, 4., 3.], [2, 1, 4., 3.], [2, 2, 4., 3.], [2, 3, 4., 3.],
[3, 0, 4., 3.], [3, 1, 4., 3.], [3, 2, 4., 3.], [3, 3, 4., 3.]]
first_formation = "3_3_formation"

self.assertEqual(all_to_all_conns, conns.tolist())
self.assertCountEqual(all_to_all_conns, conns)
self.assertEqual(len(conns), 16)
self.assertEqual(num_forms, 16)
self.assertEqual(num_elims, 0)
Expand Down

0 comments on commit 632dd5d

Please sign in to comment.