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

Copying a SPA model fails #1266

Closed
tcstewar opened this issue Feb 13, 2017 · 0 comments
Closed

Copying a SPA model fails #1266

tcstewar opened this issue Feb 13, 2017 · 0 comments

Comments

@tcstewar
Copy link
Contributor

Any model with nengo.spa doesn't seem to be able to be copied:

import nengo.spa as spa

model = spa.SPA()
with model:
    model.a = spa.Buffer(16)
m2 = model.copy()
AttributeError                            Traceback (most recent call last)
<ipython-input-8-4c0bfc568332> in <module>()
      4 with model:
      5     model.a = spa.Buffer(16)
----> 6 m2 = model.copy()

/home/tcstewar/github/nengo/nengo/network.pyc in copy(self, add_to_container)
    227             # However, we deal with it here, so we ignore the warning.
    228             warnings.simplefilter('ignore', category=NotAddedToNetworkWarning)
--> 229             c = deepcopy(self)
    230         if add_to_container is None:
    231             add_to_container = len(Network.context) > 0

/usr/lib/python2.7/copy.pyc in deepcopy(x, memo, _nil)
    188                             raise Error(
    189                                 "un(deep)copyable object of type %s" % cls)
--> 190                 y = _reconstruct(x, rv, 1, memo)
    191 
    192     memo[d] = y

/usr/lib/python2.7/copy.pyc in _reconstruct(x, info, deep, memo)
    334             state = deepcopy(state, memo)
    335         if hasattr(y, '__setstate__'):
--> 336             y.__setstate__(state)
    337         else:
    338             if isinstance(state, tuple) and len(state) == 2:

/home/tcstewar/github/nengo/nengo/network.pyc in __setstate__(self, state)
    206     def __setstate__(self, state):
    207         for k, v in iteritems(state):
--> 208             setattr(self, k, v)
    209         if len(Network.context) > 0:
    210             warnings.warn(NotAddedToNetworkWarning(self))

/home/tcstewar/github/nengo/nengo/spa/spa.pyc in __setattr__(self, key, value)
    108             if value.label is None:
    109                 value.label = key
--> 110             self._modules[key] = value
    111             for k, (obj, v) in iteritems(value.inputs):
    112                 if type(v) == int:

AttributeError: 'SPA' object has no attribute '_modules'
@jgosmann jgosmann self-assigned this Feb 13, 2017
jgosmann added a commit that referenced this issue Feb 15, 2017
The keys of the WeakKeyIDDictionaries do not get updated on a normal
copy operation. This is rarely a problem because Parameters are usually
class variables that will not get copied, but there is the possibility
to define instance parameters (slightly obscure usage, but is for
example used in the current SPA system). In this case the Parameter
instance needs to be copied. Adding the __getstate__ and __setstate__
handler allows to convert the WeakKeyIDDictionaries to normal
dictionaries with strong references and convert them back. This will
cause the keys to be updated (partially due to Pythons copy magic).

It also seems to me that obtaining strong references during the copy
process is a good idea because than none of the objects can disappear
during copying (or serialization).

Addresses #1266.
jgosmann added a commit that referenced this issue Feb 15, 2017
The involved __getattr__/__setattr__ logic would otherwise screw up.

Addresses #1266.
jgosmann added a commit that referenced this issue Feb 15, 2017
When reconstructing objects during copy, the check for whether a module
has been added twice to a model needs to be avoided.

Addresses #1266.
@jgosmann jgosmann mentioned this issue Feb 16, 2017
5 tasks
@jgosmann jgosmann removed their assignment Feb 16, 2017
tbekolay pushed a commit that referenced this issue Feb 17, 2017
The keys of the WeakKeyIDDictionaries do not get updated on a normal
copy operation. This is rarely a problem because Parameters are usually
class variables that will not get copied, but there is the possibility
to define instance parameters (slightly obscure usage, but is for
example used in the current SPA system). In this case the Parameter
instance needs to be copied. Adding the __getstate__ and __setstate__
handler allows to convert the WeakKeyIDDictionaries to normal
dictionaries with strong references and convert them back. This will
cause the keys to be updated (partially due to Python's copy magic).

It also seems to me that obtaining strong references during the copy
process is a good idea because than none of the objects can disappear
during copying (or serialization).

Addresses #1266.
tbekolay pushed a commit that referenced this issue Feb 17, 2017
The involved __getattr__/__setattr__ logic would otherwise screw up.

Addresses #1266.
tbekolay pushed a commit that referenced this issue Feb 17, 2017
When reconstructing objects during copy, the check for whether a
module has been added twice to a model needs to be avoided.

Addresses #1266.
@jgosmann jgosmann added this to the Next generation SPA module milestone Mar 31, 2017
adityagilra pushed a commit to adityagilra/nengo that referenced this issue Jun 21, 2017
The keys of the WeakKeyIDDictionaries do not get updated on a normal
copy operation. This is rarely a problem because Parameters are usually
class variables that will not get copied, but there is the possibility
to define instance parameters (slightly obscure usage, but is for
example used in the current SPA system). In this case the Parameter
instance needs to be copied. Adding the __getstate__ and __setstate__
handler allows to convert the WeakKeyIDDictionaries to normal
dictionaries with strong references and convert them back. This will
cause the keys to be updated (partially due to Python's copy magic).

It also seems to me that obtaining strong references during the copy
process is a good idea because than none of the objects can disappear
during copying (or serialization).

Addresses nengo#1266.
adityagilra pushed a commit to adityagilra/nengo that referenced this issue Jun 21, 2017
The involved __getattr__/__setattr__ logic would otherwise screw up.

Addresses nengo#1266.
adityagilra pushed a commit to adityagilra/nengo that referenced this issue Jun 21, 2017
When reconstructing objects during copy, the check for whether a
module has been added twice to a model needs to be avoided.

Addresses nengo#1266.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants