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

Copy #984

Merged
merged 1 commit into from
Nov 4, 2016
Merged

Copy #984

merged 1 commit into from
Nov 4, 2016

Conversation

jgosmann
Copy link
Collaborator

Addresses #977.

Nengo objects should be copyable and pickable now. There might be some weird effects for objects referencing other objects (e.g., connections). I might have to think about those and whether that works as expected (though, technically it works in some way).

Still todo:

  • Networks
  • FrozenObjects: Process, LearningRule, Synapse (also check for potential code duplication)
  • FIXMEs

@jgosmann jgosmann self-assigned this Mar 16, 2016
@jgosmann jgosmann added this to the 2.1.1 release milestone Mar 16, 2016
@hunse
Copy link
Collaborator

hunse commented Mar 16, 2016

FrozenObjects already have __getstate__ and __setstate__, so hopefully getting them working is as easy as just calling copy.copy in our own copy function.

@jgosmann
Copy link
Collaborator Author

I'm slightly surprised by the two failing test runs ... as the other test envs pass it is probably something going wrong with the warnings filtering?

@jgosmann
Copy link
Collaborator Author

😠 The debug commit made the tests pass ... what the hell?

@jgosmann
Copy link
Collaborator Author

I got a bit closer to the issue: Network.context is empty ... despite being in a with network block. Just not sure why it is empty.

@jgosmann jgosmann force-pushed the copy branch 2 times, most recently from 4b7540b to 6f45862 Compare March 18, 2016 15:35
@jgosmann
Copy link
Collaborator Author

@tbekolay Is py.test running tests in parallel on Travis-CI?

@tbekolay
Copy link
Member

Yes, it runs with -n 2
On Mar 18, 2016 11:50, "Jan Gosmann" notifications@github.com wrote:

@tbekolay https://github.com/tbekolay Is py.test running tests in
parallel on Travis-CI?


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#984 (comment)

@jgosmann jgosmann removed the reviewed label Aug 29, 2016
@jgosmann
Copy link
Collaborator Author

I added a commit 41a0bba that makes all the tests pass, see the commit message for details and #1152 for related discussion.

I set the label back to "needs review" because these changes are more far reaching than just a simple fixup.

@hunse
Copy link
Collaborator

hunse commented Aug 29, 2016

Those changes look okay to me.

@Seanny123
Copy link
Contributor

Found a bug that won't let connections with slices be pickled.

Given the following code:

import nengo
import cPickle as pickle

with nengo.Network() as model:
    out_nd = nengo.Node(size_in=1)

    osc = nengo.networks.Oscillator(0.1, 1, 300)


    nengo.Connection(osc.ensemble[0], out_nd)

with open("model", "w") as ff:
    pickle.dump(model, ff)

I get the error:

Traceback (most recent call last):
  File "pickle_test.py", line 14, in <module>
    pickle.dump(model, ff)
  File "/usr/lib/python2.7/copy_reg.py", line 84, in _reduce_ex
    dict = getstate()
  File "/home/saubin/git/nengo-copy/nengo/base.py", line 175, in __getstate__
    raise NotImplementedError("Nengo objects do not support pickling")
NotImplementedError: Nengo objects do not support pickling

@jgosmann
Copy link
Collaborator Author

jgosmann commented Sep 1, 2016

I just looked over the code; it might work if you just remove the __getstate__ and __setstate__ methods on ObjView.

@Seanny123
Copy link
Contributor

Yep, just removing the code solved the problem! Thanks Jan!

@jgosmann
Copy link
Collaborator Author

jgosmann commented Sep 1, 2016

One note is that I did change the public interface for NengoObject in this commit: 1b5290a

However, I don't think anyone is using this right now (it didn't require any changes to unit tests or examples) so I'm inclined to do this without deprecation and requiring a new major version...

nengo_gui is using it: nengo/nengo-gui#819

I suppose the options are:

  1. Revert those changes.
  2. Fix nengo_gui breaking compatibility to older Nengo versions.
  3. Fix nengo_gui with keeping compatibility to previous Nengo versions.

The last option might lead to a lot of conditionals. So I would probably prefer option 1 or 2. The new interface is nice, but I'm fine with 1 if people think that the compatibility to older Nengo versions is important.

@hunse
Copy link
Collaborator

hunse commented Sep 2, 2016

I'd be in favour of partially reverting the changes, and making params back into a property.

I could also go for option 2. The situation I worry about is someone upgrading Nengo, and not not realizing they also have to upgrade nengo_gui. How quickly does it fail?

I don't think option 3 really needs that many conditionals. We should just be able to do params = self.config._clsparams.params; params = params if is_iterable(params) else params() or something like that.

EDIT: I should clarify, I think params is fine as a property because calling it takes very little computation. In fact, I'm not sure if any real computation happens because it's just making an iterator, which I don't think should be evaluated much at all until someone starts iterating over it. I think the only real computation that happens right when params is called is an inspect.isclass(obj) and a dir(obj), both of which should be really fast.

EDIT2: So I should also clarify, I am most in favour of just making params back into a property, but I think any of the solutions are workable. If @tbekolay is okay with making params a property, then let's go with that.

@Seanny123
Copy link
Contributor

I could also go for option 2. The situation I worry about is someone upgrading Nengo, and not not realizing they also have to upgrade nengo_gui. How quickly does it fail?

It fails immediately by stopping you from running nengo_gui in any way.

@jgosmann
Copy link
Collaborator Author

jgosmann commented Sep 2, 2016

I don't think option 3 really needs that many conditionals.

Yeah, it seems to be only that one place in the GUI.

@jgosmann
Copy link
Collaborator Author

jgosmann commented Sep 7, 2016

I made the necessary changes to allow copies of object views and added some tests for that. Still waiting on @tbekolay to give his ok to revert params back into a property.

@jgosmann jgosmann removed their assignment Sep 7, 2016
@jgosmann
Copy link
Collaborator Author

I added a commit to turn params back into a property. I haven't tested whether this fixes nengo/nengo-gui#819. Right now the property returns an iterator, but to me it seems that it is uncommon for properties to do that. So maybe we want to turn the interator into a tuple or list. This should not affect any existing code.

@tbekolay
Copy link
Member

tbekolay commented Nov 3, 2016

After a bit of head scratching, I decided the history of this branch is not too important and squashed this all into one commit with co-authors. I added a fixup (4790cbb) to change the properties that were returning iterators to instead return lists (or tuples where appropriate), which I think is the right call. I'll test this with the snippet in nengo/nengo-gui#819 to see if it works, but assuming it does, I'll merge this tomorrow morning (in case anyone wants to take a final look at this branch today).

catcher.__enter__()
warnings.simplefilter(
'error', category=NotAddedToNetworkWarning)
request.addfinalizer(lambda: catcher.__exit__(None, None, None))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes all the with warns(NotAddedToNetworkWarning) tests fail (since they error when the warning is raised). It seemed more complicated than it was worth to disable this check in the cases in which we wanted to check the warning. Any ideas on how to do it simply?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, this is a fixture not a test. Removing this LGTM.

@Seanny123
Copy link
Contributor

LGTM. Excited to see this merged!

This commit makes it possible to copy and pickle NengoObjects,
and objects related to NengoObjects like Neuron and LearningRule.

In order to make Neurons and LearningRules copyable, some changes
were made to those classes:

* When accessing special connection points like Ensemble.neurons or
  Connection.learning_rule we now recreate the corresponding object.
  Those objects are basically just indicators from or to where a
  connection is; they neither implement any behaviour on their own nor
  do they store a lot of data (except the back-reference to their
  parent). That resolves cyclic references (always good) and the
  performance and memory impact should be negligible.

* The weak references in those classes are turned into strong
  references. Thanks to the broken reference cycle we don't have
  to worry about these being garbage collected anymore (and even with
  strong reference and a reference cycle they would be garbage collected
  sooner or later). The main benefit is that we don't have to manually
  handle those references when copying or pickling objects.

* Methods for hashing and equality testing have been implemented on
  those classes to keep the builder working that uses them as keys in
  a dictionary. Unrelated to that, this seems to be the proper thing to
  do as two Neurons objects referring to the same ensemble should be
  treated as equal.

None of these changes should affect user models.

Some other miscellaneous cleanup is done in this commit as well,
including removing some within function imports, and making some
properties return lists instead of iterables.
See the diff for more details.

Co-authored by: Eric Hunsberger <erichuns@gmail.com>
Co-authored by: Trevor Bekolay <tbekolay@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants