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

Pragmatic solution to parameter changes in standalone mode #1429

Merged
merged 40 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
969201b
WIP: Enable changing parameters via CLI for standalone
mstimberg Jul 25, 2022
2618fc1
Make it possible to change results dir in standalone
mstimberg Jul 27, 2022
1f72b81
Fix standalone's delete method for new results dir
mstimberg Jul 27, 2022
b9a11b4
fix typo in after_run header guard
mstimberg Sep 5, 2022
3909d8d
Fix and test setting args from command line
mstimberg Sep 9, 2022
33c11ce
Remove left-over print debugging and fix tests for parallel execution
mstimberg Sep 13, 2022
bf13473
First implementation of dict syntax for run args
mstimberg Sep 13, 2022
2f8a375
fix dtype for run args
mstimberg Sep 13, 2022
00d27a7
Fix test dtype for 32bit
mstimberg Sep 13, 2022
3cdce40
More error checking for run args
mstimberg Sep 13, 2022
035c9c5
fix typo in test
mstimberg Sep 14, 2022
e3bd709
Update cibuildwheel version
mstimberg Sep 14, 2022
052c562
Do not access underlying data for attributes like dtype
mstimberg Sep 14, 2022
33d774d
test changing parameters in parallel processes
mstimberg Sep 14, 2022
1125856
Use os.path.join for correct path on Windows
mstimberg Sep 15, 2022
316d9bb
Use configured results directory for stdout etc.
mstimberg Sep 30, 2022
0992e46
Fix check for empty set of clocks
mstimberg Sep 30, 2022
179bbea
Avoid directly accessing owner.name in a few places
mstimberg Sep 30, 2022
1f95127
Fix multiprocessing test for Windows
mstimberg Sep 30, 2022
eed8476
Make device pickleable
mstimberg Oct 4, 2022
f544d98
Remove left-over print statements
mstimberg Oct 4, 2022
1c83a99
Make pickling work more easily
mstimberg Oct 6, 2022
cabf880
Fix missing trailing slash in results directory
mstimberg Oct 6, 2022
a162aec
Change name of variable initialization file
mstimberg Oct 7, 2022
dda8052
Document the parameter change mechanism
mstimberg Oct 7, 2022
adbbf67
Force using older sympy version for doc building
mstimberg Sep 5, 2022
3fae1a0
Support setting synaptic values as run_args
mstimberg Oct 12, 2022
9660eac
Merge branch 'master' into standalone_parameter_change
mstimberg Nov 22, 2022
fb71e7a
Merge remote-tracking branch 'origin/master' into standalone_paramete…
mstimberg Jul 12, 2023
682361e
Simplify C++ code for setting parameters via name
mstimberg Jul 20, 2023
739b704
Check for errors when setting variables from file
mstimberg Jul 24, 2023
2391ea6
Add setting TimedArray values by run args
mstimberg Jul 24, 2023
5302563
Simplify template code by using a macro
mstimberg Jul 24, 2023
d30a0cd
Improve pickling of Synapses objects by not using lambda
mstimberg Jul 24, 2023
01b6c63
Add/modify examples using new run_args feature
mstimberg Jul 24, 2023
d3d9e16
Fix changes for better pickling of Synapses
mstimberg Jul 25, 2023
eae3eca
Avoid test failure due to minor numeric differences with 32bit
mstimberg Jul 25, 2023
c0afd5d
Update resource submodule
mstimberg Aug 1, 2023
ec15a37
Fix debug output for boolean variables
mstimberg Aug 1, 2023
ec6f868
Test more data types for run_args feature
mstimberg Aug 1, 2023
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
28 changes: 20 additions & 8 deletions brian2/codegen/codeobject.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""
Module providing the base `CodeObject` and related functions.
"""

import collections
import copy
import platform
import weakref

from brian2.core.functions import Function
from brian2.core.base import weakproxy_with_fallback
from brian2.core.functions import DEFAULT_FUNCTIONS, Function
from brian2.core.names import Nameable
from brian2.equations.unitcheck import check_units_statements
from brian2.utils.logger import get_logger
Expand Down Expand Up @@ -73,11 +72,7 @@ def __init__(
name="codeobject*",
):
Nameable.__init__(self, name=name)
try:
owner = weakref.proxy(owner)
except TypeError:
pass # if owner was already a weakproxy then this will be the error raised
self.owner = owner
self.owner = weakproxy_with_fallback(owner)
self.code = code
self.compiled_code = {}
self.variables = variables
Expand All @@ -86,6 +81,23 @@ def __init__(
self.template_source = template_source
self.compiler_kwds = compiler_kwds

def __getstate__(self):
state = self.__dict__.copy()
state["owner"] = self.owner.__repr__.__self__
# Replace Function objects for standard functions by their name
state["variables"] = self.variables.copy()
for k, v in state["variables"].items():
if isinstance(v, Function) and v is DEFAULT_FUNCTIONS[k]:
state["variables"][k] = k
return state

def __setstate__(self, state):
state["owner"] = weakproxy_with_fallback(state["owner"])
for k, v in state["variables"].items():
if isinstance(v, str):
state["variables"][k] = DEFAULT_FUNCTIONS[k]
self.__dict__ = state

@classmethod
def is_available(cls):
"""
Expand Down
30 changes: 27 additions & 3 deletions brian2/core/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ def __init__(
#: Whether the variable is an array
self.array = array

def __getstate__(self):
state = self.__dict__.copy()
state["owner"] = state["owner"].__repr__.__self__ # replace proxy
return state

def __setstate__(self, state):
state["owner"] = weakproxy_with_fallback(state["owner"])
self.__dict__ = state

@property
def is_boolean(self):
return np.issubdtype(self.dtype, np.bool_)
Expand Down Expand Up @@ -1561,18 +1570,24 @@ def __repr__(self):

return f"<{self.group_name}.{varname}: {values}>"

def __hash__(self):
return hash((self.group_name, self.name))

# Get access to some basic properties of the underlying array
@property
def shape(self):
return self.get_item(slice(None), level=1).shape
if self.ndim == 1:
return (self.variable.size,)
else:
return self.variable.size

@property
def ndim(self):
return self.get_item(slice(None), level=1).ndim
return getattr(self.variable, "ndim", 1)

@property
def dtype(self):
return self.get_item(slice(None), level=1).dtype
return self.variable.dtype


class Variables(Mapping):
Expand Down Expand Up @@ -1607,6 +1622,15 @@ def __init__(self, owner, default_index="_idx"):
# Note that by using functools.partial (instead of e.g. a lambda
# function) above, this object remains pickable.

def __getstate__(self):
state = self.__dict__.copy()
state["owner"] = state["owner"].__repr__.__self__
return state

def __setstate__(self, state):
state["owner"] = weakproxy_with_fallback(state["owner"])
self.__dict__ = state

def __getitem__(self, item):
return self._variables[item]

Expand Down
Loading