From 5e75f78ad27a8c761029779f3f76d262bf54ab0f Mon Sep 17 00:00:00 2001 From: Chaoming Wang Date: Thu, 20 Jun 2024 16:07:26 +0800 Subject: [PATCH] Fix the incorrect verbose of `clear_name_cache()` (#681) * fix the incorrect verbose of `clear_name_cache()` * add the test for naming --- brainpy/_src/math/object_transform/naming.py | 2 +- .../object_transform/tests/test_naming.py | 31 +++++++++++++++++++ .../customize_dynamical_systems.ipynb | 2 +- 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 brainpy/_src/math/object_transform/tests/test_naming.py diff --git a/brainpy/_src/math/object_transform/naming.py b/brainpy/_src/math/object_transform/naming.py index 1181e003..717b9af8 100644 --- a/brainpy/_src/math/object_transform/naming.py +++ b/brainpy/_src/math/object_transform/naming.py @@ -25,7 +25,7 @@ def check_name_uniqueness(name, obj): f'In BrainPy, each object should have a unique name. ' f'However, we detect that {obj} has a used name "{name}". \n' f'If you try to run multiple trials, you may need \n\n' - f'>>> brainpy.brainpy_object.clear_name_cache() \n\n' + f'>>> brainpy.math.clear_name_cache() \n\n' f'to clear all cached names. ' ) else: diff --git a/brainpy/_src/math/object_transform/tests/test_naming.py b/brainpy/_src/math/object_transform/tests/test_naming.py new file mode 100644 index 00000000..bceee561 --- /dev/null +++ b/brainpy/_src/math/object_transform/tests/test_naming.py @@ -0,0 +1,31 @@ +# Copyright 2024 BDP Ecosystem Limited. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + + +import brainpy as bp +import brainpy.math as bm +import unittest + + +class TestNaming(unittest.TestCase): + + def test_clear_name_cache(self): + lif = bp.dyn.LifRef(1, name='a') + with self.assertRaises(bp.errors.UniqueNameError): + lif = bp.dyn.LifRef(1, name='a') + bm.clear_name_cache(ignore_warn=True) + lif = bp.dyn.LifRef(1, name='a') + bm.clear_name_cache() + bm.clear_buffer_memory(array=True, compilation=True) diff --git a/docs/tutorial_building/customize_dynamical_systems.ipynb b/docs/tutorial_building/customize_dynamical_systems.ipynb index ec19c06a..f0679241 100644 --- a/docs/tutorial_building/customize_dynamical_systems.ipynb +++ b/docs/tutorial_building/customize_dynamical_systems.ipynb @@ -626,7 +626,7 @@ } ], "source": [ - "runner = bp.dyn.DSRunner(fhn_net,\n", + "runner = bp.DSRunner(fhn_net,\n", " monitors=['f1.v', 'X.v'], \n", " inputs=[('f1.I', 1.5), # relative access to variable \"I\" in 'fhn1'\n", " ('X.I', 1.0),]) # absolute access to variable \"I\" in 'fhn2'\n",