Skip to content

Keep the noise of silence_periods the same in copies when seed is None - #4956

Merged
alejoe91 merged 2 commits into
SpikeInterface:mainfrom
grahamfindlay:silence-periods-seed
Dec 2, 2026
Merged

alejoe91 merged 2 commits into
SpikeInterface:mainfrom
grahamfindlay:silence-periods-seed

Conversation

@grahamfindlay

Copy link
Copy Markdown
Collaborator

With mode="noise" and seed=None (the default), silence_periods and detect_and_remove_artifacts store seed=None in their kwargs, so every copy rebuilt from the recording's dict picks a new seed and fills the periods with different noise:

import numpy as np
from spikeinterface.core import generate_recording, load
from spikeinterface.core.base import base_period_dtype
from spikeinterface.preprocessing import silence_periods

rec = generate_recording(durations=[2.0])
periods = np.array([(0, 0, 1000)], dtype=base_period_dtype)
silenced = silence_periods(rec, periods=periods, mode="noise")
copy = load(silenced.to_dict())
np.array_equal(silenced.get_traces(0, 0, 1000), copy.get_traces(0, 0, 1000))  # returns False

The solution is to pick a seed with _ensure_seed when none is provided, and to store it like the noise_levels extension does. Similarly, DetectAndRemoveArtifactsRecording just needs to store the parent's seed.

This is basically what I proposed doing for quality metrics in #4927.

@grahamfindlay grahamfindlay added bug Something isn't working preprocessing Related to preprocessing module labels Nov 20, 2026
@JoeZiminski

Copy link
Copy Markdown
Contributor

Nice catch! My only thoughts are if its more readable to ensure_seed once at the top of DetectAndRemoveArtifactsRecording, then the seed used for detection and filling are the same, and its a bit earlier to follow than the round-trip of self.kwargs["seed"] through SilencePeriods.

Detection and the noise fill now share one seed, drawn up front if None,
instead of reading back the seed the parent drew.
@grahamfindlay

Copy link
Copy Markdown
Collaborator Author

Nice catch! My only thoughts are if its more readable to ensure_seed once at the top of DetectAndRemoveArtifactsRecording, then the seed used for detection and filling are the same, and its a bit earlier to follow than the round-trip of self.kwargs["seed"] through SilencePeriods.

Good idea! Done.

@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.

Nice catch!

@alejoe91
alejoe91 merged commit 4d0ce17 into SpikeInterface:main Dec 2, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working preprocessing Related to preprocessing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants