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

Better viewing #447

Merged
merged 72 commits into from
Oct 23, 2024
Merged

Better viewing #447

merged 72 commits into from
Oct 23, 2024

Conversation

jnsbck
Copy link
Contributor

@jnsbck jnsbck commented Oct 10, 2024

I have been working on this for a while, but I think I have figured 99% of the difficult things out, so it's time to make a PR to get your feedback on it and to slowly integrate it into main. I suspect it will take a while since it messes with a lot of stuff under the hood, but in the long run it should make life much easier.

The aim of this PR is to change how View works in Jaxley. At the moment Module and View are two different objects and hence they behave very differently. For example a View cannot be integrated. Under the hood treating Module and View differently also leads to a lot of duplicate methods and boilerplate since all methods that we want to have in both Module and View have to be implemented in Module and wrapped once to expose them for Module and once to expose them for View via the pointer that points back to Module. I therefore propose to handle viewing differently, namely:

  1. Make View a sublcass of Module. This means if Module returns a View all attrs and methods still work.
  2. View controls how the attrs of Module are presented, i.e. cell.branch(0).nodes will return a dataframe with the subset of nodes that are part of branch 0. Which part of a Module is in view is kept track of internally by an index.
  3. Compute global and local indexes at init and add them to nodes.

Since View is a Module, viewing is recursive, i.e. View(View(Module)) works as well. Furthermore and importantly, the current API stays the same for the user.

This is very cool, because it allows for a lot of new features:

Indexing is now more flexible, we could i.e. sample them and index into the Module using at or index into comps straight from Network.

# more flexible indexing / selection with at 
# (tracked via dataframe index, which does not change with scope)
rnd_inds = np.random.randint(0, len(net.nodes),10)
net.at(rnd_inds)

# arbitrary selection
net.branch(0).show()
net.comp(0).show()

# lazy indexing
net[0,0,0].show()

# shape
net.shape
net.cell(0).shape

Additionally, keeping track of global and local indexes in nodes allows to select which "scope" to use when indexing.

# scope
net.set_scope("global")
net.cell([0,2]).branch([0]).comp([1,2]).show() # -> [1,2] 

net.set_scope("local")
net.cell([0,2]).branch([0]).comp([1,2]).show() # -> [1,2,41,42]

net.scope("local").comp(0).show()
# vs.
net.scope("global").comp(0).show()

Its now also easy to add context management and iterables.

# context management
with net.cell(0).branch(0).comp(0) as comp0:
    comp0.set("v", -70)
    comp0.set("radius", 0.1)
net.cell(0).branch(0).comp([0,1]).show()[["v", "radius"]]

# iterables
for cell in net.cells:
    for branch in cell.branches:
        for comp in branch.comps:
            comp.set("v", -71)

for comp in net.cell(0).branch(0).comps:
    comp.set("v", -72)
net.show()[["v"]]

Groups, channels and synapses now basically behave the same and can be called at any time.

# groups
net.cell(1).branch(0).add_group("group")
net.group.show()

# Channel and Synapse views
net.HH.show()
net.cell(0).HH.nodes
net.HH.cell(0).nodes

net.TestSynapse.nodes
net.cell([0,1]).TestSynapse.nodes
net.TestSynapse.cell(0).nodes

And indexing into edges/synapses is now also possible using edge

# edges
net.edge([0,1,2]).edges

And arguably the coolest feature imo is being able to copy / extract elements of a Module, since Views <=> Module, which could be used to simulate only parts of a network for example.

# copying
cell0 = net.cell(0).copy()
cell0.show()

Very curious to hear your feedback or suggestions you have. If you wanna play around with this yourselves, you can pull from this branch.

Merging this will make #329, #330, #351 obsolete and close #377, #354, probably #338 and I guess also #133
Will also make #264 and #134 trivial
Also might wanna revisit #291 after this

@jnsbck jnsbck mentioned this pull request Oct 10, 2024
9 tasks
@jnsbck jnsbck changed the title Better Viewing Better viewing Oct 10, 2024
@jnsbck
Copy link
Contributor Author

jnsbck commented Oct 22, 2024

Awesome! Thanks for the review. Changed a bunch of stuff and left some things open for further discussions.

@jnsbck jnsbck merged commit 26acd32 into main Oct 23, 2024
1 check passed
@jnsbck jnsbck deleted the better_viewing branch October 23, 2024 07:27
jnsbck added a commit that referenced this pull request Nov 3, 2024
* add: add first version of new tests

* add: add more tests for new view

* fix: add cumsum_nseg to View and remove cell_list in Network after used to init SolveIndexer

* fix: ammend prev commit

* fix: fold todos into funcs

* wip: save wip, rm pre/post,make delete methods local, fixes

* fix: fix trainables and local inds in edges

* fix: fix jit simulate with data_stimulate issues and streamline edges

* fix: make remaining tests pass

* fix: ammend last

* doc: prepare for review

* wip: save wip

* fix: all tests passing, address review comments

* fix/rm: rm test for laxy indexing into groups, and rebase onto main.

* fix: small refactor

* fix: fix move_to issues. caused by not allowing cell.cells

* doc: add comments
@jnsbck jnsbck mentioned this pull request Nov 8, 2024
michaeldeistler pushed a commit that referenced this pull request Nov 13, 2024
* add: add first version of new tests

* add: add more tests for new view

* fix: add cumsum_nseg to View and remove cell_list in Network after used to init SolveIndexer

* fix: ammend prev commit

* fix: fold todos into funcs

* wip: save wip, rm pre/post,make delete methods local, fixes

* fix: fix trainables and local inds in edges

* fix: fix jit simulate with data_stimulate issues and streamline edges

* fix: make remaining tests pass

* fix: ammend last

* doc: prepare for review

* wip: save wip

* fix: all tests passing, address review comments

* fix/rm: rm test for laxy indexing into groups, and rebase onto main.

* fix: small refactor

* fix: fix move_to issues. caused by not allowing cell.cells

* doc: add comments
@jnsbck jnsbck mentioned this pull request Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow cell.comp(0) or net.branch(0) or net.comp(0)
2 participants