Skip to content

Commit

Permalink
update docs (#260)
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
chaoming0625 authored Sep 9, 2022
2 parents 9fd679e + c6ee3e8 commit f087e9d
Show file tree
Hide file tree
Showing 11 changed files with 517 additions and 1,392 deletions.
4 changes: 2 additions & 2 deletions brainpy/connect/random_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def build_conn(self):
if posts is not None:
ind.append(posts)
count[i] = len(posts)
ind = np.concatenate(ind)
ind = np.concatenate(ind) if len(ind) > 0 else np.asarray([], dtype=IDX_DTYPE)
indptr = np.concatenate(([0], count)).cumsum()

return 'csr', (ind, indptr)
Expand Down Expand Up @@ -143,7 +143,7 @@ def build_conn(self):
for i in range(self.post_num):
pres = self._connect(num_need=num, num_total=self.pre_num, i=i)
pre_ids.append(pres)
pre_ids = np.concatenate(pre_ids)
pre_ids = np.concatenate(pre_ids) if len(pre_ids) > 0 else np.asarray([], dtype=IDX_DTYPE)
post_ids = np.repeat(np.arange(self.post_num), num)

return 'ij', (pre_ids, post_ids)
Expand Down
17 changes: 11 additions & 6 deletions brainpy/dyn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,13 @@ def update_local_delays(self, nodes: Union[Sequence, Dict] = None):
"""
# update delays
if nodes is None:
nodes = self.nodes(level=1, include_self=False).subset(DynamicalSystem).unique().values()
nodes = tuple(self.nodes(level=1, include_self=False).subset(DynamicalSystem).unique().values())
elif isinstance(nodes, DynamicalSystem):
nodes = (nodes, )
elif isinstance(nodes, dict):
nodes = nodes.values()
nodes = tuple(nodes.values())
if not isinstance(nodes, (tuple, list)):
raise ValueError('Please provide nodes as a list/tuple/dict of DynamicalSystem.')
for node in nodes:
for name in node.local_delay_vars:
delay = self.global_delay_data[name][0]
Expand Down Expand Up @@ -554,7 +558,8 @@ def update(self, *args, **kwargs):
nodes = nodes.unique()
neuron_groups = nodes.subset(NeuGroup)
synapse_groups = nodes.subset(SynConn)
other_nodes = nodes - neuron_groups - synapse_groups
ds_views = nodes.subset(DSView)
other_nodes = nodes - neuron_groups - synapse_groups - ds_views

# shared arguments
shared = args[0]
Expand Down Expand Up @@ -636,11 +641,11 @@ def __init__(
if len(size) <= 0:
raise ModelBuildError(f'size must be int, or a tuple/list of int. '
f'But we got {type(size)}')
if not isinstance(size[0], int):
if not isinstance(size[0], (int, np.integer)):
raise ModelBuildError('size must be int, or a tuple/list of int.'
f'But we got {type(size)}')
size = tuple(size)
elif isinstance(size, int):
elif isinstance(size, (int, np.integer)):
size = (size,)
else:
raise ModelBuildError('size must be int, or a tuple/list of int.'
Expand Down Expand Up @@ -1318,7 +1323,7 @@ def __setattr__(self, key, value):
super(DSView, self).__setattr__(key, value)

def update(self, *args, **kwargs):
pass
raise NoImplementationError(f'DSView {self} cannot be updated. Please update its parent {self.target}')

def reset_state(self, batch_size=None):
pass
Expand Down
3 changes: 3 additions & 0 deletions brainpy/math/delayvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ def __init__(
# initialization
self.reset(delay_target, delay_len, initial_delay_data, batch_axis)

def __repr__(self):
return f'{self.__class__.__name__}(num_delay_step={self.num_delay_step}, delay_target_shape={self.data.shape[1:]})'

def reset(
self,
delay_target,
Expand Down
4 changes: 2 additions & 2 deletions brainpy/tools/others/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def feedback(self):


def size2num(size):
if isinstance(size, int):
if isinstance(size, (int, np.integer)):
return size
elif isinstance(size, (tuple, list)):
a = 1
Expand All @@ -53,7 +53,7 @@ def size2num(size):
def to_size(x) -> Optional[Tuple[int]]:
if isinstance(x, (tuple, list)):
return tuple(x)
if isinstance(x, int):
if isinstance(x, (int, np.integer)):
return (x, )
if x is None:
return x
Expand Down
105 changes: 105 additions & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,111 @@ tackling the shortcomings of brainpy 2.1.x generation,
effectively bringing it to research needs and standards.




Version 2.2.1 (2022.09.09)
==========================

This release fixes bugs found in the codebase and improves the usability and functions of BrainPy.

Bug fixes
~~~~~~~~~~~~~~


#. Fix the bug of operator customization in ``brainpy.math.XLACustomOp`` and ``brainpy.math.register_op``. Now, it supports operator customization by using NumPy and Numba interface. For instance,

.. code-block:: python
import brainpy.math as bm
def abs_eval(events, indices, indptr, post_val, values):
return post_val
def con_compute(outs, ins):
post_val = outs
events, indices, indptr, _, values = ins
for i in range(events.size):
if events[i]:
for j in range(indptr[i], indptr[i + 1]):
index = indices[j]
old_value = post_val[index]
post_val[index] = values + old_value
event_sum = bm.XLACustomOp(eval_shape=abs_eval, con_compute=con_compute)
#. Fix the bug of ``brainpy.tools.DotDict``. Now, it is compatible with the transformations of JAX. For instance,

.. code-block:: python
import brainpy as bp
from jax import vmap
@vmap
def multiple_run(I):
hh = bp.neurons.HH(1)
runner = bp.dyn.DSRunner(hh, inputs=('input', I), numpy_mon_after_run=False)
runner.run(100.)
return runner.mon
mon = multiple_run(bp.math.arange(2, 10, 2))
New features
~~~~~~~~~~~~~~


#. Add numpy operators ``brainpy.math.mat``\ , ``brainpy.math.matrix``\ , ``brainpy.math.asmatrix``.
#. Improve translation rules of brainpylib operators, improve its running speeds.
#. Support ``DSView`` of ``DynamicalSystem`` instance. Now, it supports defining models with a slice view of a DS instance. For example,

.. code-block:: python
import brainpy as bp
import brainpy.math as bm
class EINet_V2(bp.dyn.Network):
def __init__(self, scale=1.0, method='exp_auto'):
super(EINet_V2, self).__init__()
# network size
num_exc = int(3200 * scale)
num_inh = int(800 * scale)
# neurons
self.N = bp.neurons.LIF(num_exc + num_inh,
V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
method=method, V_initializer=bp.initialize.Normal(-55., 2.))
# synapses
we = 0.6 / scale # excitatory synaptic weight (voltage)
wi = 6.7 / scale # inhibitory synaptic weight
self.Esyn = bp.synapses.Exponential(pre=self.N[:num_exc], post=self.N,
conn=bp.connect.FixedProb(0.02),
g_max=we, tau=5.,
output=bp.synouts.COBA(E=0.),
method=method)
self.Isyn = bp.synapses.Exponential(pre=self.N[num_exc:], post=self.N,
conn=bp.connect.FixedProb(0.02),
g_max=wi, tau=10.,
output=bp.synouts.COBA(E=-80.),
method=method)
net = EINet_V2(scale=1., method='exp_auto')
# simulation
runner = bp.dyn.DSRunner(
net,
monitors={'spikes': net.N.spike},
inputs=[(net.N.input, 20.)]
)
runner.run(100.)
# visualization
bp.visualize.raster_plot(runner.mon.ts, runner.mon['spikes'], show=True)
Version 2.2.0 (2022.08.12)
==========================

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The code of BrainPy is open-sourced at GitHub:
tutorial_toolbox/synaptic_weights
tutorial_toolbox/optimizers
tutorial_toolbox/saving_and_loading
tutorial_toolbox/inputs


.. toctree::
Expand Down
Loading

0 comments on commit f087e9d

Please sign in to comment.