Skip to content

Conversation

@alejoe91
Copy link
Member

@samuelgarcia let's discuss!

@alejoe91 alejoe91 added the preprocessing Related to preprocessing module label Nov 25, 2025
@yger
Copy link
Collaborator

yger commented Nov 25, 2025

I think this is a great feature, but I already told @samuelgarcia that maybe we should at very least warn (prevent ?) users that will use very low frequencies for filters, trying to get LFP and/or low pass versions of the data. If the automatic margin starts to be really large (maybe larger than the default chunk size of 1s), then we should warn users that they are doing something clearly suboptimal.

@alejoe91
Copy link
Member Author

I think this is a great feature, but I already told @samuelgarcia that maybe we should at very least warn (prevent ?) users that will use very low frequencies for filters, trying to get LFP and/or low pass versions of the data. If the automatic margin starts to be really large (maybe larger than the default chunk size of 1s), then we should warn users that they are doing something clearly suboptimal.

Sub-optimal but somehow needed! Maybe we can warn if the margin is greater than the max_margin_s (default 5 s). What do you think?

@yger
Copy link
Collaborator

yger commented Nov 25, 2025

Yes, I've read the PR, and indeed, at least a warning that the theoretical margin should be XX, and that it has be clipped to max_margin_s. The thing is that if users really want to do that, then the warning message could also advise them to increase chunk_duration in the default_job_kwargs, to reduce IO overloads

@samuelgarcia samuelgarcia added this to the 0.104.0 milestone Dec 4, 2025
@samuelgarcia
Copy link
Member

We democratically decided to remove the max_margin kwargs and instead make a mechanism that raise an error if the end-user try to filter in LFP band and then point to a link to a clear and didactic and really exemplar tutorial about chunking and its bad effect on low freqs.

@alejoe91
Copy link
Member Author

alejoe91 commented Dec 5, 2025

This is ready for final review. here's a new doc page explaining the issue: https://spikeinterface--4227.org.readthedocs.build/en/4227/how_to/extract_lfps.html

# **Why does this fail?**
#
# The error occurs because by default in SpikeInterface when highpass filtering below 100 Hz.
# Filters with very low cutoff frequencies have long impulse responses, which require larger margins to avoid edge artifacts between chunks.
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to explain what a margin and a chunk is more clearly before this?
If I were writing it, I'd have a first section called "1. Chunks and margins" and explain what chunking is, why we do it, and what a margin is. Or put in more explanation here.

Copy link
Member

@chrishalcrow chrishalcrow Dec 5, 2025

Choose a reason for hiding this comment

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

Oh, I've read more now... so I would put Section 3 earlier in the tutorial. And put in a longer description of a chunk and margin.

Copy link
Member Author

Choose a reason for hiding this comment

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

added at the top of the file

@chrishalcrow
Copy link
Member

Hello, amazing!

You could set this doc up as a jupyter notebook that gets run in the CI (e.g. https://github.com/SpikeInterface/spikeinterface/tree/main/examples/tutorials/forhowto)

Then we don't have to do all the .py -> .rst conversion annoying stuff.

@alejoe91
Copy link
Member Author

alejoe91 commented Dec 9, 2025

Hello, amazing!

You could set this doc up as a jupyter notebook that gets run in the CI (e.g. https://github.com/SpikeInterface/spikeinterface/tree/main/examples/tutorials/forhowto)

Then we don't have to do all the .py -> .rst conversion annoying stuff.

Done! Also moved the forhowto out of tutorials, so it's a nice separation!

@alejoe91
Copy link
Member Author

alejoe91 commented Dec 9, 2025

@chrishalcrow needed some juggling to make it work with RTD RAM (the script used to save everything in memory), but it works now!

Let me know if you have more comments: https://spikeinterface--4227.org.readthedocs.build/en/4227/forhowto/plot_extract_lfps.html

##############################################################################
# **Why does this fail?**
#
# The error occurs because by default in SpikeInterface when highpass filtering below 100 Hz.
Copy link
Member

Choose a reason for hiding this comment

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

Current sentence doesn't make sense. Maybe:

Suggested change
# The error occurs because by default in SpikeInterface when highpass filtering below 100 Hz.
# The error always occurs in SpikeInterface when highpass filtering below 100 Hz, to remind the user that they need to be careful.

?

# a "margin" (extra samples at the edges) to avoid edge artifacts when filtering. Let's demonstrate
# this by saving the filtered data with different chunking strategies.
#
# **This error is to inform the user that extra care should be used when dealing with LFP signals!**
Copy link
Member

Choose a reason for hiding this comment

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

The start of this section is a bit confusing. This line refers to the error from the previous section -- but since we're in a new section I think the reader will be confused. Maybe move this statement to just after the error?

#
# We can ignore this error, but let's make sure we understand what it's happening.

# We can ignore this error, but let's see what is happening
Copy link
Member

Choose a reason for hiding this comment

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

This line and the line above repeat

print(f"Margin: {margin_in_s} s")

##############################################################################
# This effectively means that if we plot 1-s snippet of traces, a total of 11 s will actually be read and filtered.
Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to mention the overhead here, since this sentence explains very clearly why there is a big overhead. E.g.

Suggested change
# This effectively means that if we plot 1-s snippet of traces, a total of 11 s will actually be read and filtered.
# This effectively means that if we plot 1-s snippet of traces, a total of 11 s will actually be read and filtered. Hence the computational "overhead" is very large.

_ = sw.plot_traces(recording_filt, time_range=[20, 21])

##############################################################################
# A warning tells us that what we are doing is not optimized, since in order to get the requested traces
Copy link
Member

Choose a reason for hiding this comment

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

There's no warning in the output now, sorry, probably something to do with changing how it's rendered?

Copy link
Member Author

Choose a reason for hiding this comment

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

I do see the warning locally, but not on the build docs...not sure why! Should I just remove it?

Copy link
Member

Choose a reason for hiding this comment

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

yeah - just remove it

##############################################################################
# From the plot, we can see that there is a very small error when the margin size is large (green),
# a larger error when the margin is smaller (orange) and a large error when the margin is small (blue).
# So we need large margins (compared to the chunk size) if we want accurate filtered.
Copy link
Member

Choose a reason for hiding this comment

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

Beautiful!

Copy link
Member

Choose a reason for hiding this comment

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

Apart from the lil things, this is great - I'm happy!

ftype="butter",
filter_mode="sos",
margin_ms=5.0,
margin_ms=5,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
margin_ms=5,
margin_ms=5.0,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preprocessing Related to preprocessing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants