Skip to content

Commit 52e944f

Browse files
committed
typing
1 parent 8d9a40c commit 52e944f

3 files changed

Lines changed: 50 additions & 28 deletions

File tree

examples/external_devices_examples/live_examples/balanced_random_live_rate.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from pyNN.random import RandomDistribution
2020
from pyNN.utility.plotting import Figure, Panel
2121

22+
from spynnaker.pyNN.connections import SpynnakerPoissonControlConnection
23+
2224
# We need a time scale factor here as we are interacting live, so too fast
2325
# otherwise!
2426
p.setup(timestep=0.1, time_scale_factor=10.0)
@@ -93,7 +95,8 @@
9395
pop_input, database_notify_port_num=poisson_control.local_port)
9496

9597

96-
def start_callback(label, connection):
98+
def start_callback(
99+
label: str, connection: SpynnakerPoissonControlConnection) -> None:
97100
"""
98101
Changes the connection rate very 10 seconds
99102

examples/extra_models_examples/LGN_Izhikevich.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,42 +41,47 @@
4141
"""
4242

4343
import math
44-
import pyNN.spiNNaker as p
44+
from typing import List
45+
46+
import matplotlib.pyplot as plt
47+
import neo
4548
import numpy as np
49+
import pyNN.spiNNaker as p
4650
from pyNN.random import RandomDistribution, NumpyRNG
47-
4851
# for plotting
4952
from pyNN.utility.plotting import Figure, Panel
50-
import matplotlib.pyplot as plt
53+
import quantities
54+
55+
from spynnaker.pyNN.models.populations.population import Population
5156

5257
# pylint: disable=pointless-string-statement,disable=wrong-spelling-in-comment
5358

5459

55-
def get_mean_rate(numCells, population):
60+
def get_mean_rate(numCells: int, block: neo.Block) -> float:
5661
"""
5762
Calculate the average number of spikes ber neuron
5863
5964
:param int numCells:
60-
:param population: neo block
65+
:param block: neo block
6166
:return:
6267
"""
6368
firing_rate = [] # format = < neuron_id, rate (spikes/ms) >
6469

6570
for index in range(0, numCells):
66-
rate = len(population.segments[0].spiketrains[index])/TotalDuration
71+
rate = len(block.segments[0].spiketrains[index])/TotalDuration
6772
firing_rate.append(rate)
6873

6974
return sum(firing_rate)/len(firing_rate)
7075

7176

72-
def calc_irregularity(segment):
77+
def calc_irregularity(segment: neo.Segment) -> float:
7378
"""
7479
Calculate the irregularity of spikes
7580
7681
:param segment: neo Segment
7782
"""
78-
irregularity = 0
79-
isi_array = []
83+
irregularity: float = 0
84+
isi_array: List[List[quantities.Quantity]] = []
8085
for spiketrain in segment.spiketrains:
8186
if len(spiketrain) > 2:
8287
isi_array.append([])
@@ -92,7 +97,7 @@ def calc_irregularity(segment):
9297
return irregularity
9398

9499

95-
def print_irregularity():
100+
def print_irregularity() -> None:
96101
"""
97102
Calculate and prints the irregularity of spikes
98103
"""
@@ -101,7 +106,7 @@ def print_irregularity():
101106
print("TRN irregularity: ", calc_irregularity(TRN_spikes.segments[0]))
102107

103108

104-
def calc_synchrony(segment):
109+
def calc_synchrony(segment: neo.Segment) -> float:
105110
"""
106111
Calculate the synchrony of spikes
107112
@@ -119,7 +124,7 @@ def calc_synchrony(segment):
119124
return synchrony
120125

121126

122-
def print_synchrony():
127+
def print_synchrony() -> None:
123128
"""
124129
Calculate and print the synchrony of spikes
125130
"""

examples/extra_models_examples/vogel_2011/vogels_2011.py

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import os
15+
from typing import Optional, Tuple
16+
1517
import pyNN.spiNNaker as sim
1618
import numpy
1719
import matplotlib.pyplot as pylab
20+
21+
from spynnaker.pyNN.models.neuron import ConnectionHolder
22+
from spynnaker.pyNN.models.projection import Projection
23+
from spynnaker.pyNN.models.populations.population import Population
1824
from spynnaker.pyNN.utilities import neo_convertor
1925

26+
2027
# how much slowdown to put into the network to allow it to run without any
2128
# runtime errors
2229

@@ -25,7 +32,7 @@
2532
SLOWDOWN_PLASTIC = 10
2633

2734
# bool hard code for extracting the weights or not
28-
EXTRACT_WEIGHTS = False
35+
EXTRACT_WEIGHTS = True
2936
GENERATE_PLOT = True
3037

3138
# how many boards to use for this test
@@ -96,7 +103,9 @@ class Vogels2011(object):
96103
# file name for plastic in spikes
97104
PLASTIC_IN_SPIKES_FILE_NAME = "plasticInhibSpikes"
98105

99-
def _build_network(self, uses_stdp, slow_down):
106+
def _build_network(self, uses_stdp: bool, slow_down: int) -> Tuple[
107+
Population, Population, Projection, Projection, Projection,
108+
Projection]:
100109
""" builds the network with either stdp or not, and with a given
101110
slowdown
102111
@@ -167,7 +176,7 @@ def _build_network(self, uses_stdp, slow_down):
167176
return ex_pop, in_pop, ie_projection, proj1, proj2, proj3
168177

169178
@staticmethod
170-
def save_name(spike_name):
179+
def save_name(spike_name: str) -> str:
171180
"""
172181
Gets the name of a none existing file based on this name.
173182
@@ -184,7 +193,10 @@ def save_name(spike_name):
184193
file_name = spike_name + str(index)
185194
return file_name
186195

187-
def run(self, slow_down_static, slow_down_plastic, extract_weights):
196+
def run(self, slow_down_static: int, slow_down_plastic: int,
197+
extract_weights: bool) -> Tuple[
198+
Optional[ConnectionHolder], Optional[numpy.ndarray],
199+
Optional[numpy.ndarray]]:
188200
""" builds and runs a network
189201
190202
:param slow_down_static: the slowdown for the network during \
@@ -195,10 +207,9 @@ def run(self, slow_down_static, slow_down_plastic, extract_weights):
195207
extraction
196208
:return: plastic weights, the static and plastic spikes.
197209
"""
198-
199-
static_ex_spikes_numpy = None
200-
plastic_weights = None
201-
plastic_spikes_numpy = None
210+
static_ex_spikes_numpy: Optional[numpy.ndarray] = None
211+
plastic_weights: Optional[ConnectionHolder] = None
212+
plastic_spikes_numpy: Optional[numpy.ndarray] = None
202213

203214
if self.RUN_STATIC_VERSION:
204215
print("Generating Static network")
@@ -218,8 +229,7 @@ def run(self, slow_down_static, slow_down_plastic, extract_weights):
218229
index += 1
219230

220231
# Get static spikes
221-
static_ex_spikes_numpy = None
222-
static_in_spikes_numpy = None
232+
static_in_spikes_numpy: Optional[numpy.ndarray] = None
223233
if self.EXTRACT_SPIKES:
224234
static_ex_spikes = static_ex_pop.get_data('spikes')
225235
static_ex_spikes_numpy = neo_convertor.convert_spikes(
@@ -228,10 +238,12 @@ def run(self, slow_down_static, slow_down_plastic, extract_weights):
228238
static_in_spikes_numpy = neo_convertor.convert_spikes(
229239
static_in_spikes)
230240

231-
if self.SAVE_SPIKES:
241+
if self.EXTRACT_SPIKES and self.SAVE_SPIKES:
232242
ex_name = self.save_name(self.STATIC_EX_SPIKES_FILE_NAME)
233243
in_name = self.save_name(self.STATIC_IN_SPIKES_FILE_NAME)
244+
assert static_ex_spikes_numpy is not None # for mypy
234245
numpy.savetxt(ex_name, static_ex_spikes_numpy)
246+
assert static_in_spikes_numpy is not None # for mypy
235247
numpy.savetxt(in_name, static_in_spikes_numpy)
236248

237249
# end static simulation
@@ -259,8 +271,6 @@ def run(self, slow_down_static, slow_down_plastic, extract_weights):
259271
index += 1
260272

261273
# Get plastic spikes and save to disk
262-
static_in_spikes_numpy = None
263-
plastic_spikes_numpy = None
264274
if self.EXTRACT_SPIKES:
265275
plastic_spikes = plastic_ex_pop.get_data('spikes')
266276
plastic_spikes_numpy = (
@@ -269,10 +279,12 @@ def run(self, slow_down_static, slow_down_plastic, extract_weights):
269279
static_in_spikes_numpy = neo_convertor.convert_spikes(
270280
static_in_spikes)
271281

272-
if self.SAVE_SPIKES:
282+
if self.EXTRACT_SPIKES and self.SAVE_SPIKES:
273283
ex_name = self.save_name(self.PLASTIC_EX_SPIKES_FILE_NAME)
274284
in_name = self.save_name(self.PLASTIC_IN_SPIKES_FILE_NAME)
285+
assert plastic_spikes_numpy is not None # for mypy
275286
numpy.savetxt(ex_name, plastic_spikes_numpy)
287+
assert static_in_spikes_numpy is not None # for mypy
276288
numpy.savetxt(in_name, static_in_spikes_numpy)
277289

278290
if extract_weights:
@@ -285,7 +297,9 @@ def run(self, slow_down_static, slow_down_plastic, extract_weights):
285297
return plastic_weights, static_ex_spikes_numpy, plastic_spikes_numpy
286298

287299
def plot(
288-
self, plastic_weights, static_spikes_numpy, plastic_spikes_numpy):
300+
self, plastic_weights: Optional[ConnectionHolder],
301+
static_spikes_numpy: Optional[numpy.ndarray],
302+
plastic_spikes_numpy: Optional[numpy.ndarray]) -> None:
289303
""" generates plots for a paper
290304
291305
:param plastic_weights: the plastic weights.

0 commit comments

Comments
 (0)