-
Notifications
You must be signed in to change notification settings - Fork 124
/
connectors.py
1130 lines (984 loc) · 49.6 KB
/
connectors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Defines a common implementation of the built-in PyNN Connector classes.
Simulator modules may use these directly, or may implement their own versions
for improved performance.
:copyright: Copyright 2006-2024 by the PyNN team, see AUTHORS.
:license: CeCILL, see LICENSE for details.
"""
from .random import RandomDistribution, AbstractRNG, NumpyRNG
from .core import IndexBasedExpression
from . import errors, descriptions
from .recording import files
from .parameters import LazyArray
from .standardmodels import StandardSynapseType
import numpy as np
from itertools import repeat
import logging
from copy import copy, deepcopy
# the following imports are for use within eval()
from lazyarray import arccos, arcsin, arctan, arctan2, ceil, cos, cosh, exp, \
fabs, floor, fmod, hypot, ldexp, log, log10, modf, power, \
sin, sinh, sqrt, tan, tanh, maximum, minimum # noqa: F401
from numpy import e, pi # noqa: F401
try:
import csa
haveCSA = True
except ImportError:
haveCSA = False
logger = logging.getLogger("PyNN")
def _get_rng(rng):
if isinstance(rng, AbstractRNG):
return rng
elif rng is None:
return NumpyRNG(seed=151985012)
else:
raise Exception("rng must be either None, or a subclass of pyNN.random.AbstractRNG")
class Connector(object):
"""
Base class for connectors.
All connector sub-classes have the following optional keyword arguments:
`location_selector`:
TO DO
`safe`:
if True, check that weights and delays have valid values. If False,
this check is skipped.
`callback`:
a function that will be called with the fractional progress of the
connection routine. An example would be `progress_bar.set_level`.
"""
def __init__(self, location_selector=None, safe=True, callback=None):
"""
docstring needed
"""
self.safe = safe
self.callback = callback
self.location_selector = location_selector
if callback is not None:
assert callable(callback)
def connect(self, projection):
raise NotImplementedError()
def get_parameters(self):
P = {}
for name in self.parameter_names:
P[name] = getattr(self, name)
return P
def _generate_distance_map(self, projection):
position_generators = (projection.pre.position_generator,
projection.post.position_generator)
return LazyArray(projection.space.distance_generator(*position_generators),
shape=projection.shape)
def _parameters_from_synapse_type(self, projection, distance_map=None):
"""
Obtain the parameters to be used for the connections from the projection's `synapse_type`
attribute. Each parameter value is a `LazyArray`.
"""
if distance_map is None:
distance_map = self._generate_distance_map(projection)
parameter_space = projection.synapse_type.native_parameters
# TODO: in the documentation, we claim that a parameter value can be
# a list or 1D array of the same length as the number of connections.
# We do not currently handle this scenario, although it is only
# really useful for fixed-number connectors anyway.
# Probably the best solution is to remove the parameter at this stage,
# then set it after the connections have already been created.
parameter_space.shape = (projection.pre.size, projection.post.size)
for name, map in parameter_space.items():
if callable(map.base_value):
if isinstance(map.base_value, IndexBasedExpression):
# Assumes map is a function of index and hence requires the projection to
# determine its value. It and its index function are copied so as to be able
# to set the projection without altering the connector, which would perhaps
# not be expected from the 'connect' call.
new_map = copy(map)
new_map.base_value = copy(map.base_value)
new_map.base_value.projection = projection
parameter_space[name] = new_map
else:
# Assumes map is a function of distance
parameter_space[name] = map(distance_map)
return parameter_space
def describe(self, template='connector_default.txt', engine='default'):
"""
Returns a human-readable description of the connection method.
The output may be customized by specifying a different template
togther with an associated template engine (see ``pyNN.descriptions``).
If template is None, then a dictionary containing the template context
will be returned.
"""
context = {'name': self.__class__.__name__,
'parameters': self.get_parameters()}
return descriptions.render(engine, template, context)
class MapConnector(Connector):
"""
Abstract base class for Connectors based on connection maps, where a map is a 2D lazy array
containing either the (boolean) connectivity matrix (aka adjacency matrix, connection set
mask, etc.) or the values of a synaptic connection parameter.
"""
def _standard_connect(self, projection, connection_map_generator, distance_map=None):
"""
`connection_map_generator` should be a function or other callable, with one optional
argument `mask`, which returns an iterable.
The iterable should produce one element per post-synaptic neuron.
Each element should be either:
(i) a boolean array, indicating which of the pre-synaptic neurons
should be connected to,
(ii) an integer array indicating the same thing using indices,
(iii) or a single boolean, meaning connect to all/none.
The `mask` argument, a boolean array, can be used to limit processing to just
neurons which exist on the local MPI node.
todo: explain the argument `distance_map`.
"""
column_indices = np.arange(projection.post.size)
postsynaptic_indices = projection.post.id_to_index(projection.post.all_cells)
if (projection.synapse_type.native_parameters.parallel_safe
or hasattr(self, "rng") and self.rng.parallel_safe):
# If any of the synapse parameters are based on parallel-safe random number generators,
# we need to iterate over all post-synaptic cells, so we can generate then
# throw away the random numbers for the non-local nodes.
logger.debug("Parallel-safe iteration.")
components = (
column_indices,
postsynaptic_indices,
projection.post._mask_local,
connection_map_generator())
else:
# Otherwise, we only need to iterate over local post-synaptic cells.
mask = projection.post._mask_local
components = (
column_indices[mask],
postsynaptic_indices[mask],
repeat(True),
connection_map_generator(mask))
parameter_space = self._parameters_from_synapse_type(projection, distance_map)
# Loop over columns of the connection_map array
# (equivalent to looping over post-synaptic neurons)
for count, (col, postsynaptic_index, local, source_mask) in enumerate(zip(*components)):
# `col`: column index
# `postsynaptic_index`: index of the post-synaptic neuron
# `local`: boolean - does the post-synaptic neuron exist on this MPI node
# `source_mask`: boolean numpy array, indicating which of the pre-synaptic neurons
# should be connected to, or a single boolean, meaning connect to
# all/none of the pre-synaptic neurons.
# It can also be an array of addresses.
_proceed = False
if source_mask is True or source_mask.any():
_proceed = True
elif type(source_mask) == np.ndarray:
if source_mask.dtype == bool:
if source_mask.any():
_proceed = True
elif len(source_mask) > 0:
_proceed = True
if _proceed:
# Convert from boolean to integer mask, if necessary
if source_mask is True:
source_mask = np.arange(projection.pre.size, dtype=int)
elif source_mask.dtype == bool:
source_mask = source_mask.nonzero()[0]
# Evaluate the lazy arrays containing the synaptic parameters
connection_parameters = {}
for name, map in parameter_space.items():
if map.is_homogeneous:
connection_parameters[name] = map.evaluate(simplify=True)
else:
connection_parameters[name] = map[source_mask, col]
# Check that parameter values are valid
if self.safe:
# it might be cheaper to do the weight and delay check before evaluating the
# larray, however this is challenging to do if the base value is a function or
# if there are a lot of operations, so for simplicity we do the check after
# evaluation
syn = projection.synapse_type
if hasattr(syn, "parameter_checks"):
for parameter_name, check in syn.parameter_checks.items():
native_parameter_name = syn.translations[parameter_name]["translated_name"] # noqa:E501
# note that for delays we should also apply units scaling to the check
# values, since this currently only affects Brian we can probably
# handle that separately (for weights, checks are all based on zero)
if native_parameter_name in connection_parameters:
check(connection_parameters[native_parameter_name], projection)
if local:
# Connect the neurons
projection._convergent_connect(
source_mask, postsynaptic_index,
location_selector=self.location_selector,
**connection_parameters)
if self.callback:
self.callback(count / projection.post.local_size)
def _connect_with_map(self, projection, connection_map, distance_map=None):
"""
Create connections according to a connection map.
Arguments:
`projection`:
the `Projection` that is being created.
`connection_map`:
a boolean `LazyArray` of the same shape as `projection`,
representing the connectivity matrix.
`distance_map`:
TODO
"""
logger.debug("Connecting %s using a connection map" % projection.label)
self._standard_connect(projection, connection_map.by_column, distance_map)
def _get_connection_map_no_self_connections(self, projection):
from pyNN.common import Population
if (isinstance(projection.pre, Population)
and isinstance(projection.post, Population)
and projection.pre == projection.post):
# special case, expected to be faster than the default, below
connection_map = LazyArray(lambda i, j: i != j, shape=projection.shape)
else:
# this could be optimized by checking parent or component populations
# but should handle both views and assemblies
a = np.broadcast_to(projection.pre.all_cells,
(projection.post.size, projection.pre.size)).T
b = projection.post.all_cells
connection_map = LazyArray(a != b, shape=projection.shape)
return connection_map
def _get_connection_map_no_mutual_connections(self, projection):
from pyNN.common import Population
if (isinstance(projection.pre, Population)
and isinstance(projection.post, Population)
and projection.pre == projection.post):
connection_map = LazyArray(lambda i, j: i > j, shape=projection.shape)
else:
raise NotImplementedError("todo")
return connection_map
class AllToAllConnector(MapConnector):
"""
Connects all cells in the presynaptic population to all cells in the
postsynaptic population.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
"""
parameter_names = ('allow_self_connections',)
def __init__(self, allow_self_connections=True,
location_selector=None,
safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe, callback)
assert isinstance(allow_self_connections, bool)
self.allow_self_connections = allow_self_connections
def connect(self, projection):
if not self.allow_self_connections:
connection_map = self._get_connection_map_no_self_connections(projection)
elif self.allow_self_connections == 'NoMutual':
connection_map = self._get_connection_map_no_mutual_connections(projection)
else:
connection_map = LazyArray(True, shape=projection.shape)
self._connect_with_map(projection, connection_map)
class FixedProbabilityConnector(MapConnector):
"""
For each pair of pre-post cells, the connection probability is constant.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`p_connect`:
a float between zero and one. Each potential connection is created
with this probability.
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`rng`:
an :class:`RNG` instance used to evaluate whether connections exist
"""
parameter_names = ('allow_self_connections', 'p_connect')
def __init__(self, p_connect, allow_self_connections=True,
location_selector=None,
rng=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe, callback)
assert isinstance(allow_self_connections, bool) or allow_self_connections == 'NoMutual'
self.allow_self_connections = allow_self_connections
self.p_connect = float(p_connect)
assert 0 <= self.p_connect
self.rng = _get_rng(rng)
def connect(self, projection):
random_map = LazyArray(RandomDistribution('uniform', (0, 1), rng=self.rng),
projection.shape)
connection_map = random_map < self.p_connect
if not self.allow_self_connections:
mask = self._get_connection_map_no_self_connections(projection)
connection_map *= mask
elif self.allow_self_connections == 'NoMutual':
mask = self._get_connection_map_no_mutual_connections(projection)
connection_map *= mask
self._connect_with_map(projection, connection_map)
class DistanceDependentProbabilityConnector(MapConnector):
"""
For each pair of pre-post cells, the connection probability depends on distance.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`d_expression`:
the right-hand side of a valid Python expression for probability,
involving 'd', e.g. "exp(-abs(d))", or "d<3"
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`rng`:
an :class:`RNG` instance used to evaluate whether connections exist
"""
parameter_names = ('allow_self_connections', 'd_expression')
def __init__(self, d_expression, allow_self_connections=True,
location_selector=None,
rng=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe, callback)
assert isinstance(d_expression, str) or callable(d_expression)
assert isinstance(allow_self_connections, bool) or allow_self_connections == 'NoMutual'
try:
if isinstance(d_expression, str):
d = 0 # noqa: F841 (`d` is used in eval)
assert 0 <= eval(d_expression), eval(d_expression)
d = 1e12 # noqa: F841
assert 0 <= eval(d_expression), eval(d_expression)
except ZeroDivisionError as err:
raise ZeroDivisionError("Error in the distance expression %s. %s" %
(d_expression, err))
self.d_expression = d_expression
self.allow_self_connections = allow_self_connections
self.distance_function = eval("lambda d: %s" % self.d_expression)
self.rng = _get_rng(rng)
def connect(self, projection):
distance_map = self._generate_distance_map(projection)
probability_map = self.distance_function(distance_map)
random_map = LazyArray(RandomDistribution('uniform', (0, 1), rng=self.rng),
projection.shape)
connection_map = random_map < probability_map
if not self.allow_self_connections:
mask = self._get_connection_map_no_self_connections(projection)
connection_map *= mask
elif self.allow_self_connections == 'NoMutual':
mask = self._get_connection_map_no_mutual_connections(projection)
connection_map *= mask
self._connect_with_map(projection, connection_map, distance_map)
class IndexBasedProbabilityConnector(MapConnector):
"""
For each pair of pre-post cells, the connection probability depends on an arbitrary functions
that takes the indices of the pre and post populations.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`index_expression`:
a function that takes the two cell indices as inputs and calculates the
probability matrix from it.
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`rng`:
an :class:`RNG` instance used to evaluate whether connections exist
"""
parameter_names = ('allow_self_connections', 'index_expression')
def __init__(self, index_expression, allow_self_connections=True,
location_selector=None,
rng=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe, callback)
assert callable(index_expression)
assert isinstance(index_expression, IndexBasedExpression)
assert isinstance(allow_self_connections, bool) or allow_self_connections == 'NoMutual'
self.index_expression = index_expression
self.allow_self_connections = allow_self_connections
self.rng = _get_rng(rng)
def connect(self, projection):
# The index function is copied so as to avoid the connector being altered by the "connect"
# function, which is probably unexpected behaviour.
index_expression = copy(self.index_expression)
index_expression.projection = projection
probability_map = LazyArray(index_expression, projection.shape)
random_map = LazyArray(RandomDistribution('uniform', (0, 1), rng=self.rng),
projection.shape)
connection_map = random_map < probability_map
if not self.allow_self_connections:
mask = self._get_connection_map_no_self_connections(projection)
connection_map *= mask
elif self.allow_self_connections == 'NoMutual':
mask = self._get_connection_map_no_mutual_connections(projection)
connection_map *= mask
self._connect_with_map(projection, connection_map)
class DisplacementDependentProbabilityConnector(IndexBasedProbabilityConnector):
"""
For each pair of pre-post cells, the connection probability depends on the
displacement of the two neurons, i.e. on the triplet (dx, dy, dz) where
dx is the distance between the x-coordinates of the two neurons, and so on.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`disp_function`:
the right-hand side of a valid Python expression for probability,
involving an array named 'd' whose first dimension has size 3.
e.g. "(d[0] < 3) * (d[1] < 2) * exp(-abs(d[2]))"
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`rng`:
an :class:`RNG` instance used to evaluate whether connections exist
"""
class DisplacementExpression(IndexBasedExpression):
"""
A displacement based expression function used to determine the connection probability
and the value of variable connection parameters of a projection
"""
def __init__(self, disp_function):
"""
`disp_function`: a function that takes a 3xN numpy displacement matrix and maps each
row (displacement) to a probability between 0 and 1
"""
self._disp_function = disp_function
def __call__(self, i, j):
disp = (self.projection.post.positions.T[j] - self.projection.pre.positions.T[i]).T
return self._disp_function(disp)
def __init__(self, disp_function, allow_self_connections=True,
location_selector=None,
rng=None, safe=True, callback=None):
super(DisplacementDependentProbabilityConnector, self).__init__(
self.DisplacementExpression(disp_function),
allow_self_connections=allow_self_connections, rng=rng, callback=callback)
class FromListConnector(Connector):
"""
Make connections according to a list.
Arguments:
`conn_list`:
a list of tuples, one tuple for each connection. Each tuple should contain:
`(pre_idx, post_idx, p1, p2, ..., pn)` where `pre_idx` is the index
(i.e. order in the Population, not the ID) of the presynaptic
neuron, `post_idx` is the index of the postsynaptic neuron, and
p1, p2, etc. are the synaptic parameters (e.g. weight, delay,
plasticity parameters).
`column_names`:
the names of the parameters p1, p2, etc. If not provided, it is
assumed the parameters are 'weight', 'delay' (for backwards
compatibility). This should be specified using a tuple.
`safe`:
if True, check that weights and delays have valid values. If False,
this check is skipped.
`callback`:
if True, display a progress bar on the terminal.
"""
parameter_names = ('conn_list',)
def __init__(self, conn_list, column_names=None,
location_selector=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe=safe, callback=callback)
self.conn_list = np.array(conn_list)
if len(conn_list) > 0:
n_columns = self.conn_list.shape[1]
if column_names is None:
if n_columns == 2:
self.column_names = ()
elif n_columns == 4:
self.column_names = ('weight', 'delay')
else:
raise TypeError("Argument 'column_names' is required.")
else:
self.column_names = column_names
if n_columns != len(self.column_names) + 2:
raise ValueError(f"connection list has {n_columns - 2} parameter columns, "
f"but {len(self.column_names)} column names provided.")
else:
self.column_names = ()
def connect(self, projection):
"""Connect-up a Projection."""
logger.debug("conn_list (original) = \n%s", self.conn_list)
synapse_parameter_names = projection.synapse_type.get_parameter_names()
for name in self.column_names:
if name not in synapse_parameter_names:
raise ValueError("%s is not a valid parameter for %s" % (
name, projection.synapse_type.__class__.__name__))
if self.conn_list.size == 0:
return
if np.any(self.conn_list[:, 0] >= projection.pre.size):
raise errors.ConnectionError("source index out of range")
# need to do some profiling, to figure out the best way to do this:
# - order of sorting/filtering by local
# - use np.unique, or just do in1d(self.conn_list)?
idx = np.argsort(self.conn_list[:, 1])
targets = np.unique(self.conn_list[:, 1]).astype(int)
local = np.in1d(targets,
np.arange(projection.post.size)[projection.post._mask_local],
assume_unique=True)
local_targets = targets[local]
self.conn_list = self.conn_list[idx]
left = np.searchsorted(self.conn_list[:, 1], local_targets, 'left')
right = np.searchsorted(self.conn_list[:, 1], local_targets, 'right')
logger.debug("idx = %s", idx)
logger.debug("targets = %s", targets)
logger.debug("local_targets = %s", local_targets)
logger.debug("conn_list (sorted by target) = \n%s", self.conn_list)
logger.debug("left = %s", left)
logger.debug("right = %s", right)
for tgt, l, r in zip(local_targets, left, right):
sources = self.conn_list[l:r, 0].astype(int)
connection_parameters = deepcopy(projection.synapse_type.parameter_space)
connection_parameters.shape = (r - l,)
for col, name in enumerate(self.column_names, 2):
connection_parameters.update(**{name: self.conn_list[l:r, col]})
if isinstance(projection.synapse_type, StandardSynapseType):
connection_parameters = projection.synapse_type.translate(
connection_parameters)
connection_parameters.evaluate()
projection._convergent_connect(sources, tgt,
location_selector=self.location_selector,
**connection_parameters)
class FromFileConnector(FromListConnector):
"""
Make connections according to a list read from a file.
Arguments:
`file`:
either an open file object or the filename of a file containing a
list of connections, in the format required by `FromListConnector`.
Column headers, if included in the file, must be specified using
a list or tuple, e.g.::
# columns = ["i", "j", "weight", "delay", "U", "tau_rec"]
Note that the header requires `#` at the beginning of the line.
`distributed`:
if this is True, then each node will read connections from a file
called `filename.x`, where `x` is the MPI rank. This speeds up
loading connections for distributed simulations.
`safe`:
if True, check that weights and delays have valid values. If False,
this check is skipped.
`callback`:
if True, display a progress bar on the terminal.
"""
parameter_names = ('file', 'distributed')
def __init__(self, file, distributed=False,
location_selector=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe=safe, callback=callback)
if isinstance(file, str):
file = files.StandardTextFile(file, mode='r')
self.file = file
self.distributed = distributed
def connect(self, projection):
"""Connect-up a Projection."""
if self.distributed:
self.file.rename("%s.%d" % (self.file.name,
projection._simulator.state.mpi_rank))
self.column_names = self.file.get_metadata().get('columns', ('weight', 'delay'))
for ignore in "ij":
if ignore in self.column_names:
self.column_names.remove(ignore)
self.conn_list = self.file.read()
FromListConnector.connect(self, projection)
class FixedNumberConnector(MapConnector):
# base class - should not be instantiated
parameter_names = ('allow_self_connections', 'n')
def __init__(self, n, allow_self_connections=True, with_replacement=False,
location_selector=None,
rng=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe, callback)
assert isinstance(allow_self_connections, bool) or allow_self_connections == 'NoMutual'
self.allow_self_connections = allow_self_connections
self.with_replacement = with_replacement
self.n = n
if isinstance(n, int):
assert n >= 0
elif isinstance(n, RandomDistribution):
# weak check that the random distribution is ok
err_msg = "the random distribution produces negative numbers"
assert np.all(np.array(n.next(100)) >= 0), err_msg
else:
raise TypeError("n must be an integer or a RandomDistribution object")
self.rng = _get_rng(rng)
def _rng_uniform_int_exclude(self, n, size, exclude):
res = self.rng.next(n, 'uniform_int', {"low": 0, "high": size}, mask=None)
logger.debug("RNG0 res=%s" % res)
idx = np.where(res == exclude)[0]
logger.debug("RNG1 exclude=%d, res=%s idx=%s" % (exclude, res, idx))
while idx.size > 0:
redrawn = self.rng.next(idx.size, 'uniform_int', {"low": 0, "high": size}, mask=None)
res[idx] = redrawn
idx = idx[np.where(res == exclude)[0]]
logger.debug("RNG2 exclude=%d redrawn=%s res=%s idx=%s" % (exclude, redrawn, res, idx))
return res
class FixedNumberPostConnector(FixedNumberConnector):
"""
Each pre-synaptic neuron is connected to exactly `n` post-synaptic neurons
chosen at random.
The sampling behaviour is controlled by the `with_replacement` argument.
"With replacement" means that each post-synaptic neuron is chosen from the
entire population. There is always therefore a possibility of multiple
connections between a given pair of neurons.
"Without replacement" means that once a neuron has been selected, it cannot
be selected again until the entire population has been selected. This means
that if `n` is less than the size of the post-synaptic population, there
are no multiple connections. If `n` is greater than the size of the post-
synaptic population, all possible single connections are made before
starting to add duplicate connections.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`n`:
either a positive integer, or a `RandomDistribution` that produces
positive integers. If `n` is a `RandomDistribution`, then the
number of post-synaptic neurons is drawn from this distribution
for each pre-synaptic neuron.
`with_replacement`:
if True, the selection of neurons to connect is made from the
entire population. If False, once a neuron is selected it cannot
be selected again until the entire population has been connected.
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`rng`:
an :class:`RNG` instance used to evaluate which potential connections
are created.
"""
def _get_num_post(self):
if isinstance(self.n, int):
n_post = self.n
else:
n_post = self.n.next()
return n_post
def connect(self, projection):
connections = [[] for i in range(projection.post.size)]
for source_index in range(projection.pre.size):
n = self._get_num_post()
if self.with_replacement:
if not self.allow_self_connections and projection.pre == projection.post:
targets = self._rng_uniform_int_exclude(n, projection.post.size, source_index)
else:
targets = self.rng.next(
n, 'uniform_int', {"low": 0, "high": projection.post.size}, mask=None)
else:
all_cells = np.arange(projection.post.size)
if not self.allow_self_connections and projection.pre == projection.post:
all_cells = all_cells[all_cells != source_index]
full_sets = n // all_cells.size
remainder = n % all_cells.size
target_sets = []
if full_sets > 0:
target_sets = [all_cells] * full_sets
if remainder > 0:
target_sets.append(self.rng.permutation(all_cells)[:remainder])
targets = np.hstack(target_sets)
assert targets.size == n
for target_index in targets:
connections[target_index].append(source_index)
def build_source_masks(mask=None):
if mask is None:
return [np.array(x) for x in connections]
else:
return [np.array(x) for x in np.array(connections)[mask]]
self._standard_connect(projection, build_source_masks)
class FixedNumberPreConnector(FixedNumberConnector):
"""
Each post-synaptic neuron is connected to exactly `n` pre-synaptic neurons
chosen at random.
The sampling behaviour is controlled by the `with_replacement` argument.
"With replacement" means that each pre-synaptic neuron is chosen from the
entire population. There is always therefore a possibility of multiple
connections between a given pair of neurons.
"Without replacement" means that once a neuron has been selected, it cannot
be selected again until the entire population has been selected. This means
that if `n` is less than the size of the pre-synaptic population, there
are no multiple connections. If `n` is greater than the size of the pre-
synaptic population, all possible single connections are made before
starting to add duplicate connections.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`n`:
either a positive integer, or a `RandomDistribution` that produces
positive integers. If `n` is a `RandomDistribution`, then the
number of pre-synaptic neurons is drawn from this distribution
for each post-synaptic neuron.
`with_replacement`:
if True, the selection of neurons to connect is made from the
entire population. If False, once a neuron is selected it cannot
be selected again until the entire population has been connected.
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`rng`:
an :class:`RNG` instance used to evaluate which potential connections
are created.
"""
def _get_num_pre(self, size, mask=None):
if isinstance(self.n, int):
if mask is None:
n_pre = repeat(self.n, size)
else:
n_pre = repeat(self.n, mask.sum())
else:
if mask is None:
n_pre = self.n.next(size)
else:
if self.n.rng.parallel_safe:
n_pre = self.n.next(size)[mask]
else:
n_pre = self.n.next(mask.sum())
return n_pre
def connect(self, projection):
if self.with_replacement:
if self.allow_self_connections or projection.pre != projection.post:
def build_source_masks(mask=None):
n_pre = self._get_num_pre(projection.post.size, mask)
for n in n_pre:
sources = self.rng.next(
n, 'uniform_int', {"low": 0, "high": projection.pre.size}, mask=None)
assert sources.size == n
yield sources
else:
def build_source_masks(mask=None):
n_pre = self._get_num_pre(projection.post.size, mask)
if self.rng.parallel_safe or mask is None:
for i, n in enumerate(n_pre):
sources = self._rng_uniform_int_exclude(n, projection.pre.size, i)
assert sources.size == n
yield sources
else:
# TODO: use mask to obtain indices i
raise NotImplementedError(
"allow_self_connections=False currently requires a parallel safe RNG.")
else:
if self.allow_self_connections or projection.pre != projection.post:
def build_source_masks(mask=None):
# where n > projection.pre.size, first all pre-synaptic cells
# are connected one or more times, then the remainder
# are chosen randomly
n_pre = self._get_num_pre(projection.post.size, mask)
all_cells = np.arange(projection.pre.size)
for n in n_pre:
full_sets = n // projection.pre.size
remainder = n % projection.pre.size
source_sets = []
if full_sets > 0:
source_sets = [all_cells] * full_sets
if remainder > 0:
source_sets.append(self.rng.permutation(all_cells)[:remainder])
sources = np.hstack(source_sets)
assert sources.size == n
yield sources
else:
def build_source_masks(mask=None):
# where n > projection.pre.size, first all pre-synaptic cells
# are connected one or more times, then the remainder
# are chosen randomly
n_pre = self._get_num_pre(projection.post.size, mask)
all_cells = np.arange(projection.pre.size)
if self.rng.parallel_safe or mask is None:
for i, n in enumerate(n_pre):
full_sets = n // (projection.pre.size - 1)
remainder = n % (projection.pre.size - 1)
allowed_cells = all_cells[all_cells != i]
source_sets = []
if full_sets > 0:
source_sets = [allowed_cells] * full_sets
if remainder > 0:
source_sets.append(self.rng.permutation(allowed_cells)[:remainder])
sources = np.hstack(source_sets)
assert sources.size == n
yield sources
else:
raise NotImplementedError(
"allow_self_connections=False currently requires a parallel safe RNG.")
self._standard_connect(projection, build_source_masks)
class OneToOneConnector(MapConnector):
"""
Where the pre- and postsynaptic populations have the same size, connect
cell *i* in the presynaptic population to cell *i* in the postsynaptic
population for all *i*.
Takes any of the standard :class:`Connector` optional arguments.
"""
parameter_names = tuple()
def connect(self, projection):
"""Connect-up a Projection."""
connection_map = LazyArray(lambda i, j: i == j, shape=projection.shape)
self._connect_with_map(projection, connection_map)
class SmallWorldConnector(Connector):
"""
Connect cells so as to create a small-world network.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`degree`:
the region length where nodes will be connected locally.
`rewiring`:
the probability of rewiring each edge.
`allow_self_connections`:
if the connector is used to connect a Population to itself, this
flag determines whether a neuron is allowed to connect to itself,
or only to other neurons in the Population.
`n_connections`:
if specified, the number of efferent synaptic connections per neuron.
`rng`:
an :class:`RNG` instance used to evaluate which connections
are created.
"""
parameter_names = ('allow_self_connections', 'degree', 'rewiring', 'n_connections')
def __init__(self, degree, rewiring, allow_self_connections=True,
n_connections=None, location_selector=None,
rng=None, safe=True, callback=None):
"""
Create a new connector.
"""
Connector.__init__(self, location_selector, safe, callback)
assert 0 <= rewiring <= 1
assert isinstance(allow_self_connections, bool) or allow_self_connections == 'NoMutual'
self.rewiring = rewiring
self.d_expression = "d < %g" % degree
self.allow_self_connections = allow_self_connections
self.n_connections = n_connections
self.rng = _get_rng(rng)
def connect(self, projection):
"""Connect-up a Projection."""
raise NotImplementedError
class CSAConnector(MapConnector):
"""
Use the Connection Set Algebra (Djurfeldt, 2012) to connect cells.
Takes any of the standard :class:`Connector` optional arguments and, in
addition:
`cset`:
a connection set object.
"""
parameter_names = ('cset',)
if haveCSA:
def __init__(self, cset, location_selector=None, safe=True, callback=None):
"""
"""
Connector.__init__(self, location_selector, safe=safe, callback=callback)
self.cset = cset
arity = csa.arity(cset)
assert arity in (0, 2), 'must specify mask or connection-set with arity 0 or 2'
else:
def __init__(self, cset, safe=True, callback=None):
raise RuntimeError("CSAConnector not available---couldn't import csa module")
def connect(self, projection):
"""Connect-up a Projection."""
# Cut out finite part
c = csa.cross((0, projection.pre.size - 1), (0, projection.post.size - 1)) * \
self.cset # can't we cut out just the columns we want?
if csa.arity(self.cset) == 2:
# Connection-set with arity 2