Skip to content

Commit dba6ba0

Browse files
committed
docs: add build_conn part in docs
1 parent 85556c5 commit dba6ba0

File tree

1 file changed

+44
-18
lines changed

1 file changed

+44
-18
lines changed

docs/tutorial_toolbox/synaptic_connections.ipynb

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,45 @@
109109
"- `TwoEndConnector`: It has two input parameters `pre_size` and `post_size`, each representing the size of the pre- and post-synaptic neuron group. It will result in a connection matrix with the shape of (pre_num, post_num).\n",
110110
"- `OneEndConnector`: It has only one parameter `pre_size` which represent the size of the neuron group. It will result in a connection matrix with the shape of (pre_num, pre_num).\n",
111111
"\n",
112-
"The `__call__` function returns the class itself.\n",
112+
"The `__call__` function returns the class itself."
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"source": [
118+
"### Connector.build_conn()\n",
119+
"Users can customize the connection in `build_conn()` function. Notice there are three connection types users can provide:\n",
120+
"| Connection Types | Definition |\n",
121+
"| :- | :- |\n",
122+
"| 'mat' | Dense conncetion, including a connection matrix. |\n",
123+
"| 'ij' | Index projection, including a pre-neuron index vector and a post-neuron index vector. |\n",
124+
"| 'csr' | Sparse connection, including a index vector and a indptr vector. |\n",
125+
"\n",
126+
"Return type can be either a `dict` or a `tuple`. Here are two examples of how to return your connection data:\n",
127+
"\n",
128+
"Example 1:\n",
129+
"```python\n",
130+
"def build_conn(self):\n",
131+
" ind = np.arange(self.pre_num)\n",
132+
" indptr = np.arange(self.pre_num + 1)\n",
133+
"\n",
134+
" return dict(csr=(ind, indptr), mat=None, ij=None)\n",
135+
"```\n",
136+
"\n",
137+
"Example 2:\n",
138+
"```python\n",
139+
"def build_conn(self):\n",
140+
" ind = np.arange(self.pre_num)\n",
141+
" indptr = np.arange(self.pre_num + 1)\n",
142+
"\n",
143+
" return 'csr', (ind, indptr)\n",
144+
"```\n",
113145
"\n",
114146
"After creating the synaptic connection, users can use the `require()` method to access some useful properties of the connection."
115-
]
147+
],
148+
"metadata": {
149+
"collapsed": false
150+
}
116151
},
117152
{
118153
"cell_type": "markdown",
@@ -2006,7 +2041,7 @@
20062041
"\n",
20072042
"- Your connection class should inherit from `brainpy.connect.TwoEndConnector` or `brainpy.connect.OneEndConnector`.\n",
20082043
"- `__init__` function should be implemented and essential parameters should be initialized.\n",
2009-
"- Users should also overwrite `require()` function to describe how to build your connection. Remember in `require()` users have to call two built-in functions: `self.check(structures)` and `self.make_returns(structures, [csr, mat, ij])`.\n",
2044+
"- Users should also overwrite `build_conn()` function to describe how to build your connection.\n",
20102045
"\n",
20112046
"Let's take an example to illustrate the details of customization."
20122047
]
@@ -2038,20 +2073,17 @@
20382073
" self.seed = seed\n",
20392074
" self.rng = np.random.RandomState(seed=seed)\n",
20402075
"\n",
2041-
" def require(self, *structures):\n",
2042-
" # please call this function for auto-checking inputs.\n",
2043-
" self.check(structures)\n",
2044-
"\n",
2076+
" def build_conn(self):\n",
20452077
" ind = []\n",
20462078
" count = np.zeros(self.pre_num, dtype=np.uint32)\n",
2047-
" \n",
2079+
"\n",
20482080
" def _random_prob_conn(rng, pre_i, num_post, prob, include_self):\n",
20492081
" p = rng.random(num_post) <= prob\n",
20502082
" if (not include_self) and pre_i < num_post:\n",
20512083
" p[pre_i] = False\n",
20522084
" conn_j = np.asarray(np.where(p)[0], dtype=np.uint32)\n",
20532085
" return conn_j\n",
2054-
" \n",
2086+
"\n",
20552087
" for i in range(self.pre_num):\n",
20562088
" posts = _random_prob_conn(self.rng, pre_i=i, num_post=self.post_num,\n",
20572089
" prob=self.prob, include_self=self.include_self)\n",
@@ -2060,20 +2092,14 @@
20602092
"\n",
20612093
" ind = np.concatenate(ind)\n",
20622094
" indptr = np.concatenate(([0], count)).cumsum()\n",
2063-
" \n",
2064-
" # please use this built-in function to auto-return all data structures you need.\n",
2065-
" return self.make_returns(structures, csr=(ind, indptr))"
2095+
"\n",
2096+
" return 'csr', (ind, indptr)"
20662097
]
20672098
},
20682099
{
20692100
"cell_type": "markdown",
20702101
"metadata": {},
20712102
"source": [
2072-
"Users can customize the connection in `require()` function. And at last user will call `self.make_returns(structures, [csr, mat, ij])` function to automatically produce the structures in parameters. Notice there are also three optional parameters users can provide:\n",
2073-
"- csr: sparse connection, including a index vector and a indptr vector. \n",
2074-
"- mat: dense conncetion, including a connection matrix.\n",
2075-
"- ij: index projection, including a pre-neuron index vector and a post-neuron index vector.\n",
2076-
"\n",
20772103
"Then users can initialize the your own connections as below:"
20782104
]
20792105
},
@@ -2192,4 +2218,4 @@
21922218
},
21932219
"nbformat": 4,
21942220
"nbformat_minor": 4
2195-
}
2221+
}

0 commit comments

Comments
 (0)