brian-team/brian2

A bug in the Synapse.connect documentation

Jasmine969 opened this issue · 2 comments

Python version: 3.8. Platform: windows 10. Brian2 version: 2.5.1
In the documentation of Synapses class, there is an example of connect usage

S.connect(j='i+(-1)**k for k in range(2) if i>0 and i<N_pre-1') # connect neuron i to its neighbours if it has both neighbours

I ran it myself, but was given the following error:

Error compiling Cython file:
------------------------------------------------------------
...
        for k in range(_iter_low, _iter_high, _iter_step):

                        
            _pre_idx = _raw_pre_idx
            i = _i
            _j = i + ((- 1) ** k)
                   ^
------------------------------------------------------------

C:\Users\13372\.cython\brian_extensions\_cython_magic_16a1c724b1adb9773451a3d083b2c890.pyx:190:19: Cannot assign type 'double' to 'int32_t'

It seems that i is int32_t while (-1)**k is double, and these two cannot summed directly. So I cast the latter into int32_t manually:

S.connect(j='i+int((-1)**k) for k in range(2) if i>0 and i<N_pre-1')

And now it works. Hope this will be revised in future.

Dear @Jasmine969, many thanks for the report. I did not yet look into it in detail, but from what you state it rather looks like a bug in the Cython code generation ((-1)**k should stay an int), than a bug in the documentation. I think it has to do with a change in semantics between Cython 0.29 (our minimal dependency) and Cython 3.0 (the latest version). We can probably/hopefully fix it quite easily with a cpow compiler directive – could you maybe quickly confirm that you are using Cython 3.x? Thanks!

Dear @Jasmine969, many thanks for the report. I did not yet look into it in detail, but from what you state it rather looks like a bug in the Cython code generation ((-1)**k should stay an int), than a bug in the documentation. I think it has to do with a change in semantics between Cython 0.29 (our minimal dependency) and Cython 3.0 (the latest version). We can probably/hopefully fix it quite easily with a cpow compiler directive – could you maybe quickly confirm that you are using Cython 3.x? Thanks!

it is cython 3.0.8