nengo / nengo

A Python library for creating and simulating large-scale brain models
https://www.nengo.ai/nengo
Other
824 stars 179 forks source link

Connection syntax for the lazy #747

Closed jgosmann closed 7 years ago

jgosmann commented 9 years ago

Some types of network connectivity require a lot of boilerplate code. If I want A -> B -> C -> D, I have to write three almost identical lines

nengo.Connection(A, B)
nengo.Connection(B, C)
nengo.Conneciton(C, D)

Same if I want D -> E, D -> F and D -> G. I was thinking it might be useful to have a more compact syntax like

connect(A, B, C, D, [E, F, G])

This connect function would take a variable number of Nengo objects and chains all of the together. If a list of Nengo objects is passed in the previous object would be connected to all objects in the list and all objects in the list would connect to the next object.

Connection parameters could either be allowed as **kwargs and would apply to all connections or dictionaries with these parameters could be interleaved with the Nengo objects to allow different parameters (but this could get quite ugly?).

This is not supposed to cover ways to connect things up (and the old syntax would still be available for those cases), but would make some types of connectivity less tedious to create.

hunse commented 9 years ago
chain = [A, B, C, D]
[nengo.Connection(a, b) for a, b in zip(chain[:-1], chain[1:])]

or

chain = lambda x: zip(x[:-1], x[1:])
[nengo.Connection(a, b) for a, b in chain([A, B, C, D])]

and

[nengo.Connection(D, x) for x in [E, F, G]]

A helper function could be useful, but I'm wondering a) how many times do people want to do this without specifying functions, transforms, etc., and b) if people are specifying those things, how quickly will it get ugly. I would probably rather see it done programatically in those cases, for clarity. For example, in your example connect(A, B, C, D, [E, F, G]), if I wanted to specify functions, would I have to give a list of four functions (and all the connections out of D would have the same) or six? Six makes more sense to me, but at the same time it's not clear that connect(A, [B, C, D], function=f) shouldn't work (or maybe it should, and we'd also support connect(A, [B, C, D], function=[f, g, h]), but then we quickly get back to my original question if you have connect(A, [B, C, D], E)).

drasmuss commented 9 years ago

Yeah I think I'm with Eric on this one. My vote would be to preserve the clarity we get from doing things explicitly, even if it makes for slightly less compact syntax.

studywolf commented 9 years ago

samesies

tbekolay commented 9 years ago

Definitely this would be a good helper function that one could put in their own script and find it useful. For that reason, I could see adding it as a utility function (nengo.utils.connections.connect_all or chain or something like that; it'd be analogous to nengo.utils.probe.probe_all). But I don't think I'd use this in very many (if any) examples.

xchoo commented 9 years ago

+1 for including a helper / utility function and not including it in the current nengo.Connection call.

tbekolay commented 7 years ago

If anyone comes across this issue and implements a simple helper function to do what's described in this issue, we'd be happy to include that helper function in either https://github.com/nengo/nengo_extras/ or as part of an example in https://github.com/nengo/nengo_examples :smile: :rainbow: