Skip to content

Commit f087e9d

Browse files
authored
update docs (#260)
updates
2 parents 9fd679e + c6ee3e8 commit f087e9d

File tree

11 files changed

+517
-1392
lines changed

11 files changed

+517
-1392
lines changed

brainpy/connect/random_conn.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def build_conn(self):
6868
if posts is not None:
6969
ind.append(posts)
7070
count[i] = len(posts)
71-
ind = np.concatenate(ind)
71+
ind = np.concatenate(ind) if len(ind) > 0 else np.asarray([], dtype=IDX_DTYPE)
7272
indptr = np.concatenate(([0], count)).cumsum()
7373

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

149149
return 'ij', (pre_ids, post_ids)

brainpy/dyn/base.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,13 @@ def update_local_delays(self, nodes: Union[Sequence, Dict] = None):
269269
"""
270270
# update delays
271271
if nodes is None:
272-
nodes = self.nodes(level=1, include_self=False).subset(DynamicalSystem).unique().values()
272+
nodes = tuple(self.nodes(level=1, include_self=False).subset(DynamicalSystem).unique().values())
273+
elif isinstance(nodes, DynamicalSystem):
274+
nodes = (nodes, )
273275
elif isinstance(nodes, dict):
274-
nodes = nodes.values()
276+
nodes = tuple(nodes.values())
277+
if not isinstance(nodes, (tuple, list)):
278+
raise ValueError('Please provide nodes as a list/tuple/dict of DynamicalSystem.')
275279
for node in nodes:
276280
for name in node.local_delay_vars:
277281
delay = self.global_delay_data[name][0]
@@ -554,7 +558,8 @@ def update(self, *args, **kwargs):
554558
nodes = nodes.unique()
555559
neuron_groups = nodes.subset(NeuGroup)
556560
synapse_groups = nodes.subset(SynConn)
557-
other_nodes = nodes - neuron_groups - synapse_groups
561+
ds_views = nodes.subset(DSView)
562+
other_nodes = nodes - neuron_groups - synapse_groups - ds_views
558563

559564
# shared arguments
560565
shared = args[0]
@@ -636,11 +641,11 @@ def __init__(
636641
if len(size) <= 0:
637642
raise ModelBuildError(f'size must be int, or a tuple/list of int. '
638643
f'But we got {type(size)}')
639-
if not isinstance(size[0], int):
644+
if not isinstance(size[0], (int, np.integer)):
640645
raise ModelBuildError('size must be int, or a tuple/list of int.'
641646
f'But we got {type(size)}')
642647
size = tuple(size)
643-
elif isinstance(size, int):
648+
elif isinstance(size, (int, np.integer)):
644649
size = (size,)
645650
else:
646651
raise ModelBuildError('size must be int, or a tuple/list of int.'
@@ -1318,7 +1323,7 @@ def __setattr__(self, key, value):
13181323
super(DSView, self).__setattr__(key, value)
13191324

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

13231328
def reset_state(self, batch_size=None):
13241329
pass

brainpy/math/delayvars.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ def __init__(
299299
# initialization
300300
self.reset(delay_target, delay_len, initial_delay_data, batch_axis)
301301

302+
def __repr__(self):
303+
return f'{self.__class__.__name__}(num_delay_step={self.num_delay_step}, delay_target_shape={self.data.shape[1:]})'
304+
302305
def reset(
303306
self,
304307
delay_target,

brainpy/tools/others/others.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def feedback(self):
3939

4040

4141
def size2num(size):
42-
if isinstance(size, int):
42+
if isinstance(size, (int, np.integer)):
4343
return size
4444
elif isinstance(size, (tuple, list)):
4545
a = 1
@@ -53,7 +53,7 @@ def size2num(size):
5353
def to_size(x) -> Optional[Tuple[int]]:
5454
if isinstance(x, (tuple, list)):
5555
return tuple(x)
56-
if isinstance(x, int):
56+
if isinstance(x, (int, np.integer)):
5757
return (x, )
5858
if x is None:
5959
return x

changelog.rst

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,111 @@ tackling the shortcomings of brainpy 2.1.x generation,
1010
effectively bringing it to research needs and standards.
1111

1212

13+
14+
15+
Version 2.2.1 (2022.09.09)
16+
==========================
17+
18+
This release fixes bugs found in the codebase and improves the usability and functions of BrainPy.
19+
20+
Bug fixes
21+
~~~~~~~~~~~~~~
22+
23+
24+
#. 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,
25+
26+
.. code-block:: python
27+
28+
import brainpy.math as bm
29+
30+
def abs_eval(events, indices, indptr, post_val, values):
31+
return post_val
32+
33+
def con_compute(outs, ins):
34+
post_val = outs
35+
events, indices, indptr, _, values = ins
36+
for i in range(events.size):
37+
if events[i]:
38+
for j in range(indptr[i], indptr[i + 1]):
39+
index = indices[j]
40+
old_value = post_val[index]
41+
post_val[index] = values + old_value
42+
43+
event_sum = bm.XLACustomOp(eval_shape=abs_eval, con_compute=con_compute)
44+
45+
46+
#. Fix the bug of ``brainpy.tools.DotDict``. Now, it is compatible with the transformations of JAX. For instance,
47+
48+
.. code-block:: python
49+
50+
import brainpy as bp
51+
from jax import vmap
52+
53+
@vmap
54+
def multiple_run(I):
55+
hh = bp.neurons.HH(1)
56+
runner = bp.dyn.DSRunner(hh, inputs=('input', I), numpy_mon_after_run=False)
57+
runner.run(100.)
58+
return runner.mon
59+
60+
mon = multiple_run(bp.math.arange(2, 10, 2))
61+
62+
New features
63+
~~~~~~~~~~~~~~
64+
65+
66+
#. Add numpy operators ``brainpy.math.mat``\ , ``brainpy.math.matrix``\ , ``brainpy.math.asmatrix``.
67+
#. Improve translation rules of brainpylib operators, improve its running speeds.
68+
#. Support ``DSView`` of ``DynamicalSystem`` instance. Now, it supports defining models with a slice view of a DS instance. For example,
69+
70+
.. code-block:: python
71+
72+
import brainpy as bp
73+
import brainpy.math as bm
74+
75+
76+
class EINet_V2(bp.dyn.Network):
77+
def __init__(self, scale=1.0, method='exp_auto'):
78+
super(EINet_V2, self).__init__()
79+
80+
# network size
81+
num_exc = int(3200 * scale)
82+
num_inh = int(800 * scale)
83+
84+
# neurons
85+
self.N = bp.neurons.LIF(num_exc + num_inh,
86+
V_rest=-60., V_th=-50., V_reset=-60., tau=20., tau_ref=5.,
87+
method=method, V_initializer=bp.initialize.Normal(-55., 2.))
88+
89+
# synapses
90+
we = 0.6 / scale # excitatory synaptic weight (voltage)
91+
wi = 6.7 / scale # inhibitory synaptic weight
92+
self.Esyn = bp.synapses.Exponential(pre=self.N[:num_exc], post=self.N,
93+
conn=bp.connect.FixedProb(0.02),
94+
g_max=we, tau=5.,
95+
output=bp.synouts.COBA(E=0.),
96+
method=method)
97+
self.Isyn = bp.synapses.Exponential(pre=self.N[num_exc:], post=self.N,
98+
conn=bp.connect.FixedProb(0.02),
99+
g_max=wi, tau=10.,
100+
output=bp.synouts.COBA(E=-80.),
101+
method=method)
102+
103+
net = EINet_V2(scale=1., method='exp_auto')
104+
# simulation
105+
runner = bp.dyn.DSRunner(
106+
net,
107+
monitors={'spikes': net.N.spike},
108+
inputs=[(net.N.input, 20.)]
109+
)
110+
runner.run(100.)
111+
112+
# visualization
113+
bp.visualize.raster_plot(runner.mon.ts, runner.mon['spikes'], show=True)
114+
115+
116+
117+
13118
Version 2.2.0 (2022.08.12)
14119
==========================
15120

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ The code of BrainPy is open-sourced at GitHub:
7272
tutorial_toolbox/synaptic_weights
7373
tutorial_toolbox/optimizers
7474
tutorial_toolbox/saving_and_loading
75+
tutorial_toolbox/inputs
7576

7677

7778
.. toctree::

0 commit comments

Comments
 (0)