Skip to content

Commit

Permalink
Merge pull request #873 from brian-team/fix_if_in_short_generator_con…
Browse files Browse the repository at this point in the history
…nect

Allow the use of "if" in the shorthand generator syntax
  • Loading branch information
mstimberg committed Aug 31, 2017
2 parents c151f19 + 977ef59 commit 109ed5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
14 changes: 12 additions & 2 deletions brian2/synapses/synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1305,8 +1305,18 @@ def connect(self, condition=None, i=None, j=None, p=1., n=1,
raise ValueError("Generator syntax cannot be combined with "
"p argument")
if not re.search(r'\bfor\b', j):
j = '{j} for _ in range(1)'.format(j=j)
# will now call standard generator syntax (see below)
if_split = j.split(' if ')
if len(if_split) == 1:
j = '{j} for _ in range(1)'.format(j=j)
elif len(if_split) == 2:
j = '{target} for _ in range(1) if {cond}'.format(target=if_split[0],
cond=if_split[1])
else:
raise SyntaxError("Error parsing expression '{j}'. "
"Expression must have generator "
"syntax, for example 'k for k in "
"range(i-10, i+10)'".format(j=j))
# will now call standard generator syntax (see below)
else:
raise ValueError("Must specify at least one of condition, i or "
"j arguments")
Expand Down
9 changes: 8 additions & 1 deletion brian2/tests/test_synapses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2063,6 +2063,13 @@ def test_synapse_generator_deterministic():
expected_offdiagonal[np.arange(len(G2)-1), np.arange(len(G2)-1)+1] = 1
expected_offdiagonal[np.arange(len(G2)-1)+1, np.arange(len(G2)-1)] = 1

# Converging connection pattern with restriction
S14 = Synapses(G, G2, 'w:1', 'v+=w')
S14.connect(j='int(i/4) if i % 2 == 0')
expected_converging_restricted = np.zeros((len(G), len(G2)), dtype=np.int32)
for target in xrange(4):
expected_converging_restricted[np.arange(4, step=2) + target * 4, target] = 1

with catch_logs() as _: # Ignore warnings about empty synapses
run(0*ms) # for standalone

Expand All @@ -2079,7 +2086,7 @@ def test_synapse_generator_deterministic():
_compare(S11, expected_diverging)
_compare(S12, expected_converging)
_compare(S13, expected_offdiagonal)

_compare(S14, expected_converging_restricted)

@attr('standalone-compatible')
@with_setup(teardown=reinit_devices)
Expand Down
9 changes: 6 additions & 3 deletions docs_sphinx/user/synapses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ simplest way to give a 1-to-1 connection would be::

S.connect(j='i')

This mapping can also use a restricting condition with ``if``, e.g. to connect
neurons 0, 2, 4, 6, ... to neurons 0, 1, 2, 3, ... you could write::

S.connect(j='int(i/2) if i % 2 == 0')


Accessing synaptic variables
----------------------------
Synaptic variables can be accessed in a similar way as `NeuronGroup` variables. They can be indexed
Expand Down Expand Up @@ -248,9 +254,6 @@ the indices.

.. admonition:: The following topics are not essential for beginners.

|

Creating synapses with the generator syntax
-------------------------------------------

Expand Down

0 comments on commit 109ed5b

Please sign in to comment.