Skip to content

Commit

Permalink
update loop docs and apis (#261)
Browse files Browse the repository at this point in the history
update loop docs and apis
  • Loading branch information
chaoming0625 authored Sep 15, 2022
2 parents f087e9d + 8d70a27 commit 64d1e21
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 147 deletions.
2 changes: 1 addition & 1 deletion brainpy/dyn/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ def __init__(
for _ in range(v.batch_axis - len(self.index) + 1)])))
else:
index = self.index
self.slice_vars[k] = bm.VariableRef(v, index)
self.slice_vars[k] = bm.VariableView(v, index)

# sub-nodes
nodes = target.nodes(method='relative', level=1, include_self=False).subset(DynamicalSystem)
Expand Down
4 changes: 4 additions & 0 deletions brainpy/math/controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,10 @@ def _body_fun(op):
if not isinstance(static_vals, (tuple, list)):
static_vals = (static_vals, )
new_vals = body_fun(*static_vals)
if new_vals is None:
new_vals = tuple()
if not isinstance(new_vals, tuple):
new_vals = (new_vals, )
return [v.value for v in dyn_vars], new_vals

def _cond_fun(op):
Expand Down
14 changes: 10 additions & 4 deletions brainpy/math/jaxarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'Variable',
'TrainVar',
'Parameter',
'VariableRef',
'VariableView',
]

# Ways to change values in a zero-dimensional array
Expand Down Expand Up @@ -1494,14 +1494,20 @@ def __init__(self, value, dtype=None, batch_axis: int = None):
lambda aux_data, flat_contents: Parameter(*flat_contents))


class VariableRef(Variable):
"""A reference of Variable instance."""
class VariableView(Variable):
"""A view of a Variable instance.
This class is used to create a slice view of ``brainpy.math.Variable``.
``VariableView`` can be used to update the subset of the original
Variable instance, and make operations on this subset of the Variable.
"""
def __init__(self, value: Variable, index):
self.index = index
if not isinstance(value, Variable):
raise ValueError('Must be instance of Variable.')
temp_shape = tuple([1] * len(index))
super(VariableRef, self).__init__(jnp.zeros(temp_shape), batch_axis=value.batch_axis)
super(VariableView, self).__init__(jnp.zeros(temp_shape), batch_axis=value.batch_axis)
self._value = value

@property
Expand Down
23 changes: 4 additions & 19 deletions docs/tutorial_building/build_conductance_neurons.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"cell_type": "markdown",
"source": [
"On the other hand, simplified models do not care about the physiological features of neurons but mainly focus on how to reproduce the exact spike timing. Therefore, they are more simplified and maybe not biologically explicable.\n",
"\n",
Expand All @@ -47,21 +45,19 @@
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
"name": "#%% md\n"
}
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"cell_type": "markdown",
"source": [
"## Building an ion channel"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
"name": "#%% md\n"
}
}
},
Expand Down Expand Up @@ -436,17 +432,6 @@
"source": [
"By combining different ion channels, we can get different types of conductance-based neuron models easily and straightforwardly. To see all predifined channel models in BrainPy, please click [here](../apis/dyn.rst)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 64d1e21

Please sign in to comment.