brainpy / BrainPy

Brain Dynamics Programming in Python
https://brainpy.readthedocs.io/
GNU General Public License v3.0
536 stars 94 forks source link

Incorrect `FixedProb` probability when multiple data structures `require`d #695

Open ChromatinRemodeling opened 1 day ago

ChromatinRemodeling commented 1 day ago

When using brainpy.connect.TwoEndConnector.require to require multiple data structures instead of a single 'conn_mat', the real connection frequency is significantly lower than the assigned connection probability. Code to reproduce:

>>> conn = bp.conn.FixedProb(prob=0.99, include_self=False, seed=134); conn(pre_size=4, post_size=4); conn.require('conn_mat', 'pre_ids')
FixedProb(prob=0.99, pre_ratio=1.0, include_self=False, allow_multi_conn=False, seed=134)
(Array([[False,  True,  True, False],
       [ True, False,  True, False],
       [False,  True, False,  True],
       [ True, False,  True, False]], dtype=bool), Array([0, 0, 1, 1, 2, 2, 3, 3], dtype=int32))
>>> conn = bp.conn.FixedProb(prob=0.99, include_self=False, seed=134); conn(pre_size=4, post_size=4); conn.require('conn_mat')
FixedProb(prob=0.99, pre_ratio=1.0, include_self=False, allow_multi_conn=False, seed=134)
Array([[False,  True,  True,  True],
       [ True, False,  True,  True],
       [ True,  True, False,  True],
       [ True,  True,  True, False]], dtype=bool)
>>>

Reproduced in 2 environments, one with python 3.10, another with python 3.12.

My python environment:

Python 3.10.15 | packaged by conda-forge | (main, Oct 16 2024, 01:15:49) [MSC v.1941 64 bit (AMD64)] on win32
Package                   Version
------------------------- ------------------
anyio                     4.6.2.post1
argon2-cffi               23.1.0
argon2-cffi-bindings      21.2.0
arrow                     1.3.0
asttokens                 2.4.1
async-lru                 2.0.4
attrs                     24.2.0
babel                     2.16.0
beautifulsoup4            4.12.3
bleach                    6.2.0
brainpy                   2.6.0.post20241025
certifi                   2024.8.30
cffi                      1.17.1
charset-normalizer        3.4.0
colorama                  0.4.6
comm                      0.2.2
contourpy                 1.3.1
cycler                    0.12.1
debugpy                   1.8.8
decorator                 5.1.1
defusedxml                0.7.1
dill                      0.3.9
exceptiongroup            1.2.2
executing                 2.1.0
fastjsonschema            2.20.0
fonttools                 4.54.1
fqdn                      1.5.1
h11                       0.14.0
httpcore                  1.0.7
httpx                     0.27.2
idna                      3.10
imagecodecs               2024.9.22
imageio                   2.36.0
importlib_metadata        8.5.0
ipykernel                 6.29.5
ipython                   8.29.0
isoduration               20.11.0
jax                       0.4.35
jaxlib                    0.4.35
jedi                      0.19.2
Jinja2                    3.1.4
json5                     0.9.28
jsonpointer               3.0.0
jsonschema                4.23.0
jsonschema-specifications 2024.10.1
jupyter_client            8.6.3
jupyter_core              5.7.2
jupyter-events            0.10.0
jupyter-lsp               2.2.5
jupyter_server            2.14.2
jupyter_server_terminals  0.5.3
jupyterlab                4.2.6
jupyterlab_pygments       0.3.0
jupyterlab_server         2.27.3
kiwisolver                1.4.7
lazy_loader               0.4
markdown-it-py            3.0.0
MarkupSafe                3.0.2
matplotlib                3.9.2
matplotlib-inline         0.1.7
mdurl                     0.1.2
mistune                   3.0.2
ml_dtypes                 0.5.0
munkres                   1.1.4
nbclient                  0.10.0
nbconvert                 7.16.4
nbformat                  5.10.4
nest-asyncio              1.6.0
networkx                  3.4.2
notebook                  7.2.2
notebook_shim             0.2.4
numpy                     2.1.3
opt_einsum                3.4.0
overrides                 7.7.0
packaging                 24.2
pandas                    2.2.3
pandocfilters             1.5.1
parso                     0.8.4
pillow                    11.0.0
pip                       24.3.1
platformdirs              4.3.6
prometheus_client         0.21.0
prompt_toolkit            3.0.48
psutil                    6.1.0
pure_eval                 0.2.3
pycparser                 2.22
Pygments                  2.18.0
pyparsing                 3.2.0
PySide6                   6.8.0.2
python-dateutil           2.9.0
python-json-logger        2.0.7
pytz                      2024.1
PyWavelets                1.7.0
pywin32                   308
pywinpty                  2.0.14
PyYAML                    6.0.2
pyzmq                     26.2.0
referencing               0.35.1
requests                  2.32.3
rfc3339-validator         0.1.4
rfc3986-validator         0.1.1
rich                      13.9.4
rpds-py                   0.21.0
scikit-image              0.24.0
scipy                     1.14.1
Send2Trash                1.8.3
setuptools                75.3.0
shiboken6                 6.8.0.2
six                       1.16.0
sniffio                   1.3.1
soupsieve                 2.6
stack-data                0.6.3
taichi                    1.7.2
terminado                 0.18.1
tifffile                  2024.9.20
tinycss2                  1.4.0
tomli                     2.1.0
tornado                   6.4.1
tqdm                      4.67.0
traitlets                 5.14.3
types-python-dateutil     2.9.0.20241003
typing_extensions         4.12.2
tzdata                    2024.2
unicodedata2              15.1.0
uri-template              1.3.0
urllib3                   2.2.3
wcwidth                   0.2.13
webcolors                 24.11.1
webencodings              0.5.1
websocket-client          1.8.0
wheel                     0.45.0
zipp                      3.21.0
ChromatinRemodeling commented 14 hours ago

To be clear, the first input gives significantly lower number of Trues than the prob argument required. The second input is supposed to be a control group. I changed the prob to a smaller number like 0.2 and saw this problem persists.