brainstate 0.1.7__py2.py3-none-any.whl → 0.1.9__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. brainstate/__init__.py +58 -51
  2. brainstate/_compatible_import.py +148 -148
  3. brainstate/_state.py +1605 -1663
  4. brainstate/_state_test.py +52 -52
  5. brainstate/_utils.py +47 -47
  6. brainstate/augment/__init__.py +30 -30
  7. brainstate/augment/_autograd.py +778 -778
  8. brainstate/augment/_autograd_test.py +1289 -1289
  9. brainstate/augment/_eval_shape.py +99 -99
  10. brainstate/augment/_eval_shape_test.py +38 -38
  11. brainstate/augment/_mapping.py +1060 -1060
  12. brainstate/augment/_mapping_test.py +597 -597
  13. brainstate/augment/_random.py +151 -151
  14. brainstate/compile/__init__.py +38 -38
  15. brainstate/compile/_ad_checkpoint.py +204 -204
  16. brainstate/compile/_ad_checkpoint_test.py +49 -49
  17. brainstate/compile/_conditions.py +256 -256
  18. brainstate/compile/_conditions_test.py +220 -220
  19. brainstate/compile/_error_if.py +92 -92
  20. brainstate/compile/_error_if_test.py +52 -52
  21. brainstate/compile/_jit.py +346 -346
  22. brainstate/compile/_jit_test.py +143 -143
  23. brainstate/compile/_loop_collect_return.py +536 -536
  24. brainstate/compile/_loop_collect_return_test.py +58 -58
  25. brainstate/compile/_loop_no_collection.py +184 -184
  26. brainstate/compile/_loop_no_collection_test.py +50 -50
  27. brainstate/compile/_make_jaxpr.py +888 -888
  28. brainstate/compile/_make_jaxpr_test.py +156 -146
  29. brainstate/compile/_progress_bar.py +202 -202
  30. brainstate/compile/_unvmap.py +159 -159
  31. brainstate/compile/_util.py +147 -147
  32. brainstate/environ.py +563 -563
  33. brainstate/environ_test.py +62 -62
  34. brainstate/functional/__init__.py +27 -26
  35. brainstate/graph/__init__.py +29 -29
  36. brainstate/graph/_graph_node.py +244 -244
  37. brainstate/graph/_graph_node_test.py +73 -73
  38. brainstate/graph/_graph_operation.py +1738 -1738
  39. brainstate/graph/_graph_operation_test.py +563 -563
  40. brainstate/init/__init__.py +26 -26
  41. brainstate/init/_base.py +52 -52
  42. brainstate/init/_generic.py +244 -244
  43. brainstate/init/_random_inits.py +553 -553
  44. brainstate/init/_random_inits_test.py +149 -149
  45. brainstate/init/_regular_inits.py +105 -105
  46. brainstate/init/_regular_inits_test.py +50 -50
  47. brainstate/mixin.py +365 -363
  48. brainstate/mixin_test.py +77 -73
  49. brainstate/nn/__init__.py +135 -131
  50. brainstate/{functional → nn}/_activations.py +808 -813
  51. brainstate/{functional → nn}/_activations_test.py +331 -331
  52. brainstate/nn/_collective_ops.py +514 -514
  53. brainstate/nn/_collective_ops_test.py +43 -43
  54. brainstate/nn/_common.py +178 -178
  55. brainstate/nn/_conv.py +501 -501
  56. brainstate/nn/_conv_test.py +238 -238
  57. brainstate/nn/_delay.py +509 -470
  58. brainstate/nn/_delay_test.py +238 -0
  59. brainstate/nn/_dropout.py +426 -426
  60. brainstate/nn/_dropout_test.py +100 -100
  61. brainstate/nn/_dynamics.py +1343 -1361
  62. brainstate/nn/_dynamics_test.py +78 -78
  63. brainstate/nn/_elementwise.py +1119 -1120
  64. brainstate/nn/_elementwise_test.py +169 -169
  65. brainstate/nn/_embedding.py +58 -58
  66. brainstate/nn/_exp_euler.py +92 -92
  67. brainstate/nn/_exp_euler_test.py +35 -35
  68. brainstate/nn/_fixedprob.py +239 -239
  69. brainstate/nn/_fixedprob_test.py +114 -114
  70. brainstate/nn/_inputs.py +608 -608
  71. brainstate/nn/_linear.py +424 -424
  72. brainstate/nn/_linear_mv.py +83 -83
  73. brainstate/nn/_linear_mv_test.py +120 -120
  74. brainstate/nn/_linear_test.py +107 -107
  75. brainstate/nn/_ltp.py +28 -28
  76. brainstate/nn/_module.py +377 -377
  77. brainstate/nn/_module_test.py +40 -208
  78. brainstate/nn/_neuron.py +705 -705
  79. brainstate/nn/_neuron_test.py +161 -161
  80. brainstate/nn/_normalizations.py +975 -918
  81. brainstate/nn/_normalizations_test.py +73 -73
  82. brainstate/{functional → nn}/_others.py +46 -46
  83. brainstate/nn/_poolings.py +1177 -1177
  84. brainstate/nn/_poolings_test.py +217 -217
  85. brainstate/nn/_projection.py +486 -486
  86. brainstate/nn/_rate_rnns.py +554 -554
  87. brainstate/nn/_rate_rnns_test.py +63 -63
  88. brainstate/nn/_readout.py +209 -209
  89. brainstate/nn/_readout_test.py +53 -53
  90. brainstate/nn/_stp.py +236 -236
  91. brainstate/nn/_synapse.py +505 -505
  92. brainstate/nn/_synapse_test.py +131 -131
  93. brainstate/nn/_synaptic_projection.py +423 -423
  94. brainstate/nn/_synouts.py +162 -162
  95. brainstate/nn/_synouts_test.py +57 -57
  96. brainstate/nn/_utils.py +89 -89
  97. brainstate/nn/metrics.py +388 -388
  98. brainstate/optim/__init__.py +38 -38
  99. brainstate/optim/_base.py +64 -64
  100. brainstate/optim/_lr_scheduler.py +448 -448
  101. brainstate/optim/_lr_scheduler_test.py +50 -50
  102. brainstate/optim/_optax_optimizer.py +152 -152
  103. brainstate/optim/_optax_optimizer_test.py +53 -53
  104. brainstate/optim/_sgd_optimizer.py +1104 -1104
  105. brainstate/random/__init__.py +24 -24
  106. brainstate/random/_rand_funs.py +3616 -3616
  107. brainstate/random/_rand_funs_test.py +567 -567
  108. brainstate/random/_rand_seed.py +210 -210
  109. brainstate/random/_rand_seed_test.py +48 -48
  110. brainstate/random/_rand_state.py +1409 -1409
  111. brainstate/random/_random_for_unit.py +52 -52
  112. brainstate/surrogate.py +1957 -1957
  113. brainstate/transform.py +23 -23
  114. brainstate/typing.py +304 -304
  115. brainstate/util/__init__.py +50 -50
  116. brainstate/util/caller.py +98 -98
  117. brainstate/util/error.py +55 -55
  118. brainstate/util/filter.py +469 -469
  119. brainstate/util/others.py +540 -540
  120. brainstate/util/pretty_pytree.py +945 -945
  121. brainstate/util/pretty_pytree_test.py +159 -159
  122. brainstate/util/pretty_repr.py +328 -328
  123. brainstate/util/pretty_table.py +2954 -2954
  124. brainstate/util/scaling.py +258 -258
  125. brainstate/util/struct.py +523 -523
  126. {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info}/METADATA +91 -99
  127. brainstate-0.1.9.dist-info/RECORD +130 -0
  128. {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info}/WHEEL +1 -1
  129. {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info/licenses}/LICENSE +202 -202
  130. brainstate/functional/_normalization.py +0 -81
  131. brainstate/functional/_spikes.py +0 -204
  132. brainstate-0.1.7.dist-info/RECORD +0 -131
  133. {brainstate-0.1.7.dist-info → brainstate-0.1.9.dist-info}/top_level.txt +0 -0
@@ -1,258 +1,258 @@
1
- # Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
- # ==============================================================================
15
-
16
- from typing import Union, Sequence
17
-
18
- __all__ = [
19
- 'MemScaling',
20
- 'IdMemScaling',
21
- ]
22
-
23
-
24
- class MemScaling(object):
25
- """
26
- The scaling object for membrane potential.
27
-
28
- The scaling object is used to transform the membrane potential range to a
29
- standard range. The scaling object can be used to transform the membrane
30
- potential to a standard range, and transform the standard range to the
31
- membrane potential.
32
-
33
- """
34
- __module__ = 'brainstate.util'
35
-
36
- def __init__(self, scale, bias):
37
- self._scale = scale
38
- self._bias = bias
39
-
40
- @classmethod
41
- def transform(
42
- cls,
43
- oring_range: Sequence[Union[float, int]],
44
- target_range: Sequence[Union[float, int]] = (0., 1.)
45
- ) -> 'MemScaling':
46
- """Transform the membrane potential range to a ``Scaling`` instance.
47
-
48
- Args:
49
- oring_range: [V_min, V_max]
50
- target_range: [scaled_V_min, scaled_V_max]
51
-
52
- Returns:
53
- The instanced scaling object.
54
- """
55
- V_min, V_max = oring_range
56
- scaled_V_min, scaled_V_max = target_range
57
- scale = (V_max - V_min) / (scaled_V_max - scaled_V_min)
58
- bias = scaled_V_min * scale - V_min
59
- return cls(scale=scale, bias=bias)
60
-
61
- def scale_offset(self, x, bias=None, scale=None):
62
- """
63
- Transform the membrane potential to the standard range.
64
-
65
- Parameters
66
- ----------
67
- x : array_like
68
- The membrane potential.
69
- bias : float, optional
70
- The bias of the scaling object. If None, the default bias will be used.
71
- scale : float, optional
72
- The scale of the scaling object. If None, the default scale will be used.
73
-
74
- Returns
75
- -------
76
- x : array_like
77
- The standard range of the membrane potential.
78
- """
79
- if bias is None:
80
- bias = self._bias
81
- if scale is None:
82
- scale = self._scale
83
- return (x + bias) / scale
84
-
85
- def scale(self, x, scale=None):
86
- """
87
- Transform the membrane potential to the standard range.
88
-
89
- Parameters
90
- ----------
91
- x : array_like
92
- The membrane potential.
93
- scale : float, optional
94
- The scale of the scaling object. If None, the default scale will be used.
95
-
96
- Returns
97
- -------
98
- x : array_like
99
- The standard range of the membrane potential.
100
- """
101
- if scale is None:
102
- scale = self._scale
103
- return x / scale
104
-
105
- def offset(self, x, bias=None):
106
- """
107
- Transform the membrane potential to the standard range.
108
-
109
- Parameters
110
- ----------
111
- x : array_like
112
- The membrane potential.
113
- bias : float, optional
114
- The bias of the scaling object. If None, the default bias will be used.
115
-
116
- Returns
117
- -------
118
- x : array_like
119
- The standard range of the membrane potential.
120
- """
121
- if bias is None:
122
- bias = self._bias
123
- return x + bias
124
-
125
- def rev_scale(self, x, scale=None):
126
- """
127
- Reversely transform the standard range to the original membrane potential.
128
-
129
- Parameters
130
- ----------
131
- x : array_like
132
- The standard range of the membrane potential.
133
- scale : float, optional
134
- The scale of the scaling object. If None, the default scale will be used.
135
-
136
- Returns
137
- -------
138
- x : array_like
139
- The original membrane potential.
140
- """
141
- if scale is None:
142
- scale = self._scale
143
- return x * scale
144
-
145
- def rev_offset(self, x, bias=None):
146
- """
147
- Reversely transform the standard range to the original membrane potential.
148
-
149
- Parameters
150
- ----------
151
- x : array_like
152
- The standard range of the membrane potential.
153
- bias : float, optional
154
- The bias of the scaling object. If None, the default bias will be used.
155
-
156
- Returns
157
- -------
158
- x : array_like
159
- The original membrane potential.
160
- """
161
- if bias is None:
162
- bias = self._bias
163
- return x - bias
164
-
165
- def rev_scale_offset(self, x, bias=None, scale=None):
166
- """
167
- Reversely transform the standard range to the original membrane potential.
168
-
169
- Parameters
170
- ----------
171
- x : array_like
172
- The standard range of the membrane potential.
173
- bias : float, optional
174
- The bias of the scaling object. If None, the default bias will be used.
175
- scale : float, optional
176
- The scale of the scaling object. If None, the default scale will be used.
177
-
178
- Returns
179
- -------
180
- x : array_like
181
- The original membrane potential.
182
- """
183
- if bias is None:
184
- bias = self._bias
185
- if scale is None:
186
- scale = self._scale
187
- return x * scale - bias
188
-
189
- def clone(self):
190
- """
191
- Clone the scaling object.
192
-
193
- Returns
194
- -------
195
- scaling : MemScaling
196
- The cloned scaling object.
197
- """
198
- return MemScaling(bias=self._bias, scale=self._scale)
199
-
200
-
201
- class IdMemScaling(MemScaling):
202
- """
203
- The identity scaling object.
204
-
205
- The identity scaling object is used to transform the membrane potential to
206
- the standard range, and reversely transform the standard range to the
207
- membrane potential.
208
-
209
- """
210
- __module__ = 'brainstate.util'
211
-
212
- def __init__(self):
213
- super().__init__(scale=1., bias=0.)
214
-
215
- def scale_offset(self, x, bias=None, scale=None):
216
- """
217
- Transform the membrane potential to the standard range.
218
- """
219
- return x
220
-
221
- def scale(self, x, scale=None):
222
- """
223
- Transform the membrane potential to the standard range.
224
- """
225
- return x
226
-
227
- def offset(self, x, bias=None):
228
- """
229
- Transform the membrane potential to the standard range.
230
- """
231
- return x
232
-
233
- def rev_scale(self, x, scale=None):
234
- """
235
- Reversely transform the standard range to the original membrane potential.
236
-
237
- """
238
- return x
239
-
240
- def rev_offset(self, x, bias=None):
241
- """
242
- Reversely transform the standard range to the original membrane potential.
243
-
244
-
245
- """
246
- return x
247
-
248
- def rev_scale_offset(self, x, bias=None, scale=None):
249
- """
250
- Reversely transform the standard range to the original membrane potential.
251
- """
252
- return x
253
-
254
- def clone(self):
255
- """
256
- Clone the scaling object.
257
- """
258
- return IdMemScaling()
1
+ # Copyright 2024 BDP Ecosystem Limited. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ==============================================================================
15
+
16
+ from typing import Union, Sequence
17
+
18
+ __all__ = [
19
+ 'MemScaling',
20
+ 'IdMemScaling',
21
+ ]
22
+
23
+
24
+ class MemScaling(object):
25
+ """
26
+ The scaling object for membrane potential.
27
+
28
+ The scaling object is used to transform the membrane potential range to a
29
+ standard range. The scaling object can be used to transform the membrane
30
+ potential to a standard range, and transform the standard range to the
31
+ membrane potential.
32
+
33
+ """
34
+ __module__ = 'brainstate.util'
35
+
36
+ def __init__(self, scale, bias):
37
+ self._scale = scale
38
+ self._bias = bias
39
+
40
+ @classmethod
41
+ def transform(
42
+ cls,
43
+ oring_range: Sequence[Union[float, int]],
44
+ target_range: Sequence[Union[float, int]] = (0., 1.)
45
+ ) -> 'MemScaling':
46
+ """Transform the membrane potential range to a ``Scaling`` instance.
47
+
48
+ Args:
49
+ oring_range: [V_min, V_max]
50
+ target_range: [scaled_V_min, scaled_V_max]
51
+
52
+ Returns:
53
+ The instanced scaling object.
54
+ """
55
+ V_min, V_max = oring_range
56
+ scaled_V_min, scaled_V_max = target_range
57
+ scale = (V_max - V_min) / (scaled_V_max - scaled_V_min)
58
+ bias = scaled_V_min * scale - V_min
59
+ return cls(scale=scale, bias=bias)
60
+
61
+ def scale_offset(self, x, bias=None, scale=None):
62
+ """
63
+ Transform the membrane potential to the standard range.
64
+
65
+ Parameters
66
+ ----------
67
+ x : array_like
68
+ The membrane potential.
69
+ bias : float, optional
70
+ The bias of the scaling object. If None, the default bias will be used.
71
+ scale : float, optional
72
+ The scale of the scaling object. If None, the default scale will be used.
73
+
74
+ Returns
75
+ -------
76
+ x : array_like
77
+ The standard range of the membrane potential.
78
+ """
79
+ if bias is None:
80
+ bias = self._bias
81
+ if scale is None:
82
+ scale = self._scale
83
+ return (x + bias) / scale
84
+
85
+ def scale(self, x, scale=None):
86
+ """
87
+ Transform the membrane potential to the standard range.
88
+
89
+ Parameters
90
+ ----------
91
+ x : array_like
92
+ The membrane potential.
93
+ scale : float, optional
94
+ The scale of the scaling object. If None, the default scale will be used.
95
+
96
+ Returns
97
+ -------
98
+ x : array_like
99
+ The standard range of the membrane potential.
100
+ """
101
+ if scale is None:
102
+ scale = self._scale
103
+ return x / scale
104
+
105
+ def offset(self, x, bias=None):
106
+ """
107
+ Transform the membrane potential to the standard range.
108
+
109
+ Parameters
110
+ ----------
111
+ x : array_like
112
+ The membrane potential.
113
+ bias : float, optional
114
+ The bias of the scaling object. If None, the default bias will be used.
115
+
116
+ Returns
117
+ -------
118
+ x : array_like
119
+ The standard range of the membrane potential.
120
+ """
121
+ if bias is None:
122
+ bias = self._bias
123
+ return x + bias
124
+
125
+ def rev_scale(self, x, scale=None):
126
+ """
127
+ Reversely transform the standard range to the original membrane potential.
128
+
129
+ Parameters
130
+ ----------
131
+ x : array_like
132
+ The standard range of the membrane potential.
133
+ scale : float, optional
134
+ The scale of the scaling object. If None, the default scale will be used.
135
+
136
+ Returns
137
+ -------
138
+ x : array_like
139
+ The original membrane potential.
140
+ """
141
+ if scale is None:
142
+ scale = self._scale
143
+ return x * scale
144
+
145
+ def rev_offset(self, x, bias=None):
146
+ """
147
+ Reversely transform the standard range to the original membrane potential.
148
+
149
+ Parameters
150
+ ----------
151
+ x : array_like
152
+ The standard range of the membrane potential.
153
+ bias : float, optional
154
+ The bias of the scaling object. If None, the default bias will be used.
155
+
156
+ Returns
157
+ -------
158
+ x : array_like
159
+ The original membrane potential.
160
+ """
161
+ if bias is None:
162
+ bias = self._bias
163
+ return x - bias
164
+
165
+ def rev_scale_offset(self, x, bias=None, scale=None):
166
+ """
167
+ Reversely transform the standard range to the original membrane potential.
168
+
169
+ Parameters
170
+ ----------
171
+ x : array_like
172
+ The standard range of the membrane potential.
173
+ bias : float, optional
174
+ The bias of the scaling object. If None, the default bias will be used.
175
+ scale : float, optional
176
+ The scale of the scaling object. If None, the default scale will be used.
177
+
178
+ Returns
179
+ -------
180
+ x : array_like
181
+ The original membrane potential.
182
+ """
183
+ if bias is None:
184
+ bias = self._bias
185
+ if scale is None:
186
+ scale = self._scale
187
+ return x * scale - bias
188
+
189
+ def clone(self):
190
+ """
191
+ Clone the scaling object.
192
+
193
+ Returns
194
+ -------
195
+ scaling : MemScaling
196
+ The cloned scaling object.
197
+ """
198
+ return MemScaling(bias=self._bias, scale=self._scale)
199
+
200
+
201
+ class IdMemScaling(MemScaling):
202
+ """
203
+ The identity scaling object.
204
+
205
+ The identity scaling object is used to transform the membrane potential to
206
+ the standard range, and reversely transform the standard range to the
207
+ membrane potential.
208
+
209
+ """
210
+ __module__ = 'brainstate.util'
211
+
212
+ def __init__(self):
213
+ super().__init__(scale=1., bias=0.)
214
+
215
+ def scale_offset(self, x, bias=None, scale=None):
216
+ """
217
+ Transform the membrane potential to the standard range.
218
+ """
219
+ return x
220
+
221
+ def scale(self, x, scale=None):
222
+ """
223
+ Transform the membrane potential to the standard range.
224
+ """
225
+ return x
226
+
227
+ def offset(self, x, bias=None):
228
+ """
229
+ Transform the membrane potential to the standard range.
230
+ """
231
+ return x
232
+
233
+ def rev_scale(self, x, scale=None):
234
+ """
235
+ Reversely transform the standard range to the original membrane potential.
236
+
237
+ """
238
+ return x
239
+
240
+ def rev_offset(self, x, bias=None):
241
+ """
242
+ Reversely transform the standard range to the original membrane potential.
243
+
244
+
245
+ """
246
+ return x
247
+
248
+ def rev_scale_offset(self, x, bias=None, scale=None):
249
+ """
250
+ Reversely transform the standard range to the original membrane potential.
251
+ """
252
+ return x
253
+
254
+ def clone(self):
255
+ """
256
+ Clone the scaling object.
257
+ """
258
+ return IdMemScaling()