-
Notifications
You must be signed in to change notification settings - Fork 193
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
Add a fast correlogram merge #3607
base: main
Are you sure you want to change the base?
Add a fast correlogram merge #3607
Conversation
cool! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool. Someone else can check the math :P. Just one comment :) . And then I would request a little explanation of the strategy either as docstring or comment so we can look back and know why this works.
My hope is that the tests are robust enough that no one has to check the maths! Docstring done |
need_to_append = True | ||
delete_from = 0 | ||
|
||
if need_to_append is True: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it really matters, but since need_to_append
is truthy as bool you don't need to check with is. Just the fact that it is True/False
will trigger or not the branch. :)
Have added a fast method to re-compute correlograms when units are merged.
When two units are merged, their correlograms with other units become the sum of the previous correlograms. Let$C_{i,j}$ be the cross-correlogram of units $i$ and $j$ . If we merge units 2, 4 and 6 into a new unit 10, the cross-correlograms of unit 10 with unit 1 would be:
Similarly, the auto-correlogram of the new unit 10 is
You can implement this with two matrix operations (sum all merged-unit columns together, then sum all merged-unit rows together), which I've implemented.
The most annoying thing is tracking the
new_unit_index
, and there's more code which deals with this than the actual new algorithm. It's much easier to deal with thetake_first
new_id_strategy than theappend
one. Any advice here is welcome.If you don't believe my maths, I've added tests to show that the results are the same if you use the fast merging method or if we merge, then re-compute the correlograms. If you can think of more painful tests than I've written, let me know!
This is a big speedup for my kilosort'd NP2.0 sortings (on my laptop, 25s -> 0.2s for a single pairwise merge). And yup, I do have numba installed.
It only becomes a lot faster for generated recordings when the correlograms become a bit more full. This requires a high firing rate and small refractory period. Here's some benchmarking code:
and I get:
giving the following plot