Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default math parameter setting bug #572

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions brainpy/_src/deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def f_input_or_monitor():
'''



def _deprecate(msg):
warnings.simplefilter('always', DeprecationWarning) # turn off filter
warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
Expand All @@ -61,25 +60,25 @@ def new_func(*args, **kwargs):
return new_func


def deprecation_getattr(module, deprecations, redirects=None):
def deprecation_getattr(module, deprecations, redirects=None, redirect_module=None):
redirects = redirects or {}

def getattr(name):
def get_attr(name):
if name in deprecations:
message, fn = deprecations[name]
if fn is None:
raise AttributeError(message)
_deprecate(message)
return fn
if name in redirects:
return redirects[name]
return getattr(redirect_module, name)
raise AttributeError(f"module {module!r} has no attribute {name!r}")

return getattr
return get_attr


def deprecation_getattr2(module, deprecations):
def getattr(name):
def get_attr(name):
if name in deprecations:
old_name, new_name, fn = deprecations[name]
message = f"{old_name} is deprecated. "
Expand All @@ -91,4 +90,4 @@ def getattr(name):
return fn
raise AttributeError(f"module {module!r} has no attribute {name!r}")

return getattr
return get_attr
10 changes: 0 additions & 10 deletions brainpy/_src/math/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,3 @@
# '''Default complex data type.'''
complex_ = jnp.complex128 if config.read('jax_enable_x64') else jnp.complex64

# redirects
redirects = {'mode': mode,
'membrane_scaling': membrane_scaling,
'dt': dt,
'bool_': bool_,
'int_': int_,
'ti_int': ti_int,
'float_': float_,
'ti_float': ti_float,
'complex_': complex_}
36 changes: 36 additions & 0 deletions brainpy/_src/math/tests/test_defaults.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest

import brainpy.math as bm


class TestDefaults(unittest.TestCase):
def test_dt(self):
with bm.environment(dt=1.0):
self.assertEqual(bm.dt, 1.0)
self.assertEqual(bm.get_dt(), 1.0)

def test_bool(self):
with bm.environment(bool_=bm.int32):
self.assertTrue(bm.bool_ == bm.int32)
self.assertTrue(bm.get_bool() == bm.int32)

def test_int(self):
with bm.environment(int_=bm.int32):
self.assertTrue(bm.int == bm.int32)
self.assertTrue(bm.get_int() == bm.int32)

def test_float(self):
with bm.environment(float_=bm.float32):
self.assertTrue(bm.float_ == bm.float32)
self.assertTrue(bm.get_float() == bm.float32)

def test_complex(self):
with bm.environment(complex_=bm.complex64):
self.assertTrue(bm.complex_ == bm.complex64)
self.assertTrue(bm.get_complex() == bm.complex64)

def test_mode(self):
mode = bm.TrainingMode()
with bm.environment(mode=mode):
self.assertTrue(bm.mode == mode)
self.assertTrue(bm.get_mode() == mode)
3 changes: 2 additions & 1 deletion brainpy/math/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@
"Use brainpy.math.event.info instead.",
event.info),
}
__getattr__ = deprecation_getattr(__name__, __deprecations, defaults.redirects)

__getattr__ = deprecation_getattr(__name__, __deprecations, redirects=defaults.__all__, redirect_module=defaults)
del deprecation_getattr, defaults
22 changes: 22 additions & 0 deletions docs/apis/brainpy.math.defaults.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Default Math Parameters
=======================

.. currentmodule:: brainpy.math
.. automodule:: brainpy.math

.. autosummary::
:toctree: generated/
:nosignatures:

mode
membrane_scaling
dt
bool_
int_
ti_int
float_
ti_float
complex_


16 changes: 16 additions & 0 deletions docs/apis/brainpy.math.op_register.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ Operator Registration
:depth: 1



General Operator Customization Interface
----------------------------------------

.. currentmodule:: brainpy.math
.. automodule:: brainpy.math

.. autosummary::
:toctree: generated/
:nosignatures:
:template: classtemplate.rst

XLACustomOp



CPU Operator Customization with Numba
-------------------------------------

Expand Down
1 change: 1 addition & 0 deletions docs/apis/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ dynamics programming. For more information and usage examples, please refer to t
:maxdepth: 1

brainpy.math.rst
brainpy.math.defaults.rst
brainpy.math.delayvars.rst
brainpy.math.oo_transform.rst
brainpy.math.pre_syn_post.rst
Expand Down
Loading