Skip to content

More updates #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@



.. image:: https://github.com/PKU-NIP-Lab/BrainPy/blob/master/docs/images/logo.png
:target: https://github.com/PKU-NIP-Lab/BrainPy
:align: center
Expand Down Expand Up @@ -65,8 +64,11 @@ Install ``BrainPy`` using ``pip``::

Install from source code::

> git clone https://github.com/PKU-NIP-Lab/BrainPy
> python setup.py install
>
> # or
>
> pip install git+https://github.com/PKU-NIP-Lab/BrainPy


Expand Down
225 changes: 151 additions & 74 deletions brainpy/connectivity/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

from .. import numpy as np
from .. import profile
from ..errors import ModelUseError

try:
import numba as nb
except ImportError:
nb = None

__all__ = ['mat2ij', 'pre2post', 'pre2syn', 'post2syn', 'post2pre']
__all__ = [
'mat2ij',
'pre2post',
'pre2syn',
'post2syn',
'post2pre',
'post_slicing_by_pre',
'pre_slicing_by_post',
]


def mat2ij(conn_mat):
Expand All @@ -17,23 +26,20 @@ def mat2ij(conn_mat):
Parameters
----------
conn_mat : numpy.ndarray
Connectivity matrix with `(num_pre x num_post)` shape.
Connectivity matrix with `(num_pre, num_post)` shape.

Returns
-------
conn_tuple : tuple
(Pre-synaptic neuron indexes,
post-synaptic neuron indexes).
"""
pre_ids = []
post_ids = []
num_pre = conn_mat.shape[0]
for pre_idx in range(num_pre):
post_idxs = np.where(conn_mat[pre_idx] > 0)[0]
post_ids.extend(post_idxs)
pre_ids.extend([pre_idx] * len(post_idxs))
pre_ids = np.array(pre_ids, dtype=np.uint64)
post_ids = np.array(post_ids, dtype=np.uint64)
conn_mat = np.asarray(conn_mat)
try:
assert np.ndim(conn_mat) == 2
except AssertionError:
raise ModelUseError('Connectivity matrix must be in the shape of (num_pre, num_post).')
pre_ids, post_ids = np.where(conn_mat > 0)
return pre_ids, post_ids


Expand All @@ -42,34 +48,32 @@ def pre2post(i, j, num_pre):

Parameters
----------
i : a_list, numpy.ndarray
i : list, numpy.ndarray
The pre-synaptic neuron indexes.
j : a_list, numpy.ndarray
j : list, numpy.ndarray
The post-synaptic neuron indexes.
num_pre : int, None
The number of the pre-synaptic neurons.

Returns
-------
conn : list
The conn a_list of pre2post.
The conn list of pre2post.
"""
i, j = np.asarray(i), np.asarray(j)
try:
assert len(i) == len(j)
except AssertionError:
raise ModelUseError('The length of "i" and "j" must be the same.')

pre2post_list = [[] for _ in range(num_pre)]
for pre_id, post_id in zip(i, j):
pre2post_list[pre_id].append(post_id)

if profile.is_numba_bk():
pre2post_list = nb.typed.List()

for pre_i in range(num_pre):
index = np.where(i == pre_i)[0]
post_idx = j[index]
pre2post_list.append(np.uint64(post_idx))
else:
pre2post_list = []

for pre_i in range(num_pre):
index = np.where(i == pre_i)[0]
post_idx = j[index]
pre2post_list.append(np.uint64(post_idx))
pre2post_list_nb = nb.typed.List()
for pre_id in range(num_pre):
pre2post_list_nb.append(np.int64(pre2post_list[pre_id]))
pre2post_list = pre2post_list_nb
return pre2post_list


Expand All @@ -78,94 +82,167 @@ def post2pre(i, j, num_post):

Parameters
----------
i : a_list, numpy.ndarray
i : list, numpy.ndarray
The pre-synaptic neuron indexes.
j : a_list, numpy.ndarray
j : list, numpy.ndarray
The post-synaptic neuron indexes.
num_post : int, None
The number of the post-synaptic neurons.

Returns
-------
conn : list
The conn a_list of post2pre.
The conn list of post2pre.
"""
i, j = np.asarray(i), np.asarray(j)

try:
assert len(i) == len(j)
except AssertionError:
raise ModelUseError('The length of "i" and "j" must be the same.')

post2pre_list = [[] for _ in range(num_post)]
for pre_id, post_id in zip(i, j):
post2pre_list[post_id].append(pre_id)

if profile.is_numba_bk():
post2pre_list = nb.typed.List()
for post_i in range(num_post):
index = np.where(j == post_i)[0]
pre_idx = i[index]
post2pre_list.append(np.uint64(pre_idx))
else:
post2pre_list = []
for post_i in range(num_post):
index = np.where(j == post_i)[0]
pre_idx = i[index]
post2pre_list.append(np.uint64(pre_idx))
post2pre_list_nb = nb.typed.List()
for post_id in range(num_post):
post2pre_list_nb.append(np.int64(post2pre_list[post_id]))
post2pre_list = post2pre_list_nb
return post2pre_list


def pre2syn(i, j, num_pre):
def pre2syn(i, num_pre):
"""Get pre2syn connections from `i` and `j` indexes.

Parameters
----------
i : a_list, numpy.ndarray
i : list, numpy.ndarray
The pre-synaptic neuron indexes.
j : a_list, numpy.ndarray
The post-synaptic neuron indexes.
num_pre : int
The number of the pre-synaptic neurons.

Returns
-------
conn : list
The conn a_list of pre2syn.
The conn list of pre2syn.
"""
i, j = np.asarray(i), np.asarray(j)
pre2syn_list = [[] for _ in range(num_pre)]
for syn_id, pre_id in enumerate(i):
pre2syn_list[pre_id].append(syn_id)

if profile.is_numba_bk():
post2syn_list = nb.typed.List()
for pre_i in range(num_pre):
index = np.where(i == pre_i)[0]
post2syn_list.append(np.uint64(index))
else:
post2syn_list = []
for pre_i in range(num_pre):
index = np.where(i == pre_i)[0]
post2syn_list.append(np.uint64(index))
return post2syn_list
pre2syn_list_nb = nb.typed.List()
for pre_ids in pre2syn_list:
pre2syn_list_nb.append(np.int64(pre_ids))
pre2syn_list = pre2syn_list_nb
return pre2syn_list


def post2syn(i, j, num_post):
def post2syn(j, num_post):
"""Get post2syn connections from `i` and `j` indexes.

Parameters
----------
i : a_list, numpy.ndarray
The pre-synaptic neuron indexes.
j : a_list, numpy.ndarray
j : list, numpy.ndarray
The post-synaptic neuron indexes.
num_post : int
The number of the post-synaptic neurons.

Returns
-------
conn : list
The conn a_list of post2syn.
The conn list of post2syn.
"""
i, j = np.asarray(i), np.asarray(j)
post2syn_list = [[] for _ in range(num_post)]
for syn_id, post_id in enumerate(j):
post2syn_list[post_id].append(syn_id)

if profile.is_numba_bk():
post2syn_list = nb.typed.List()
for post_i in range(num_post):
index = np.where(j == post_i)[0]
post2syn_list.append(np.uint64(index))
else:
post2syn_list = []
for post_i in range(num_post):
index = np.where(j == post_i)[0]
post2syn_list.append(np.uint64(index))
post2syn_list_nb = nb.typed.List()
for pre_ids in post2syn_list:
post2syn_list_nb.append(np.int64(pre_ids))
post2syn_list = post2syn_list_nb
return post2syn_list


def post_slicing_by_pre(i, j, num_pre):
"""Get post slicing connections by pre-synaptic ids.

Parameters
----------
i : list, numpy.ndarray
The pre-synaptic neuron indexes.
j : list, numpy.ndarray
The post-synaptic neuron indexes.
num_pre : int
The number of the pre-synaptic neurons.

Returns
-------
conn : list
The conn list of post2syn.
"""
try:
assert len(i) == len(j)
except AssertionError:
raise ModelUseError('The length of "i" and "j" must be the same.')

# pre2post connection
pre2post_list = [[] for _ in range(num_pre)]
for pre_id, post_id in zip(i, j):
pre2post_list[pre_id].append(post_id)
post_indices = np.concatenate(pre2post_list)
post_indices = np.asarray(post_indices, dtype=np.int_)

# pre2post slicing
post_slicing = []
start = 0
for post_ids in pre2post_list:
end = start + len(post_ids)
post_slicing.append([start, end])
start = end
post_slicing = np.asarray(post_slicing, dtype=np.int_)

return post_indices, post_slicing


def pre_slicing_by_post(i, j, num_post):
"""Get pre slicing connections by post-synaptic ids.

Parameters
----------
i : list, numpy.ndarray
The pre-synaptic neuron indexes.
j : list, numpy.ndarray
The post-synaptic neuron indexes.
num_post : int
The number of the post-synaptic neurons.

Returns
-------
conn : list
The conn list of post2syn.
"""
try:
assert len(i) == len(j)
except AssertionError:
raise ModelUseError('The length of "i" and "j" must be the same.')

# post2pre connection
post2pre_list = [[] for _ in range(num_post)]
for pre_id, post_id in zip(i, j):
post2pre_list[post_id].append(pre_id)
pre_indices = np.concatenate(post2pre_list)
pre_indices = np.asarray(pre_indices, dtype=np.int_)

# post2pre slicing
pre_slicing = []
start = 0
for pre_ids in post2pre_list:
end = start + len(pre_ids)
pre_slicing.append([start, end])
start = end
pre_slicing = np.asarray(pre_slicing, dtype=np.int_)

return pre_indices, pre_slicing
Loading