Skip to content

Generate drift - #2683

Merged
samuelgarcia merged 24 commits into
SpikeInterface:mainfrom
samuelgarcia:generate_drift
Apr 11, 2024
Merged

samuelgarcia merged 24 commits into
SpikeInterface:mainfrom
samuelgarcia:generate_drift

Conversation

@samuelgarcia

@samuelgarcia samuelgarcia commented Apr 9, 2024 •

Copy link
Copy Markdown
Member

@alejoe91 : this is a important piece that @yger will need during my vaccation.

This refactor a bit some parameters in core.generate.

But this mainly add the function generate_drifting_recording() with a nice and simple API to generate drift recordings.

Do you think we could merge this very soon ?

@alejoe91 alejoe91 added the generators Related to generator tools label Apr 10, 2024
@samuelgarcia
samuelgarcia marked this pull request as ready for review April 10, 2024 12:23

@h-mayorquin h-mayorquin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would like to point out an issue of design.

When I came up with the idea of the NoiseGenerator what I wanted is to have an object whose memory footprint I could control finely to solve memory bugs. Then, as this object became the center of the hybrid project more and more bells and whistles were added to it. Overall I feel that's fine because the second objective seems more important than the first.

However, I don't see why we can't have both. I would propose creating a WhiteNoiseGenerator that just generates noise and has a memory profile that we can finely control.

Then we can use that WhiteNoiseGenerator and wrap it to produce the data sampled from a multivariate normal that you want here by using the spectral formulation:

https://en.wikipedia.org/wiki/Multivariate_normal_distribution#Computational_methods

Another option is just to have two generators and eat some duplication of code which is fine as well.

Comment thread src/spikeinterface/core/generate.py Outdated
if isinstance(firing_rates, tuple):
rng = np.random.default_rng(seed=seed)
lim0, lim1 = firing_rates
firing_rates = rng.random(num_units) * (lim1 - lim0) + lim0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I was thinking that random was uniform no ?
Lets use uniform it is more explicit maybe.

@h-mayorquin h-mayorquin Apr 10, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yeah, but only between 0 and 1. With uniform you don't need to do the math yourself as you can specificy high and low.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also vote for uniform(lim0, lim1)

firing_rates : float or array_like, default: 3.0
firing_rates : float or array_like or tuple, default: 3.0
Firing rate(s) in Hz. Can be a single value for all units or an array of firing rates with
each element being the firing rate for one unit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's add documentation for the tuple.

rng.standard_normal(size=(self.noise_block_size, self.num_channels), dtype=self.dtype) * noise_levels
)

if self.cov_matrix is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Another option is that the cov is the identity by default and then you don't need this if. But I am not sure if this is less performant.

Maybe we can do that later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This internally make a np.dot I think so handling None is good no ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I meant that if cov=None then cov=np.eye(number_of_channels) and then you don't have the if.

But I think this version is more memory efficient when cov is None as it avoids the creation of a large matrix.

Comment thread src/spikeinterface/core/generate.py Resolved

if np.isscalar(firing_rates):
firing_rates = np.full(num_units, firing_rates, dtype="float64")
firing_rates = _ensure_firing_rates(firing_rates, num_units, seed)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can pass the rng as well as mentioned above?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The thing is that if the rng is used before or not depending other global option then we do not have the same.
With this seed given we somehow reset the generator whatever happen.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You are correct. This is a very good point for reproducibility. Passing the rng means you are passing state which is wrong.

return displacement_vectors, displacement_unit_factor, displacement_sampling_frequency, displacements_steps


def generate_noise(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please make a noise_tools.py file

@alejoe91 alejoe91 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See comment

@samuelgarcia

Copy link
Copy Markdown
Member Author

@h-mayorquin.
I know that you had in mind the memory foot print when making the design but I had mind also this generator for simulation!! héhé!

If the object would become too complex, ok for several classes. But here it is really a minimal change and I think having only one class is ok no ?

@h-mayorquin

Copy link
Copy Markdown
Contributor

@h-mayorquin. I know that you had in mind the memory foot print when making the design but I had mind also this generator for simulation!! héhé!

If the object would become too complex, ok for several classes. But here it is really a minimal change and I think having only one class is ok no ?

Yeah, the complexity is not there yet but it always accumulates sloly. The memory foot-print is still the same for the cov=None case I think.

I was mentioning this as something to keep an eye on but this should not stop this PR from being merged. Once the two different purposes drift enough appart we should separate.

@samuelgarcia
samuelgarcia merged commit 8c35f1c into SpikeInterface:main Apr 11, 2024
@samuelgarcia
samuelgarcia deleted the generate_drift branch July 29, 2025 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

generators Related to generator tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants