Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/spikeinterface/core/baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def __init__(self, sampling_frequency: float, channel_ids: list, dtype):
self._sampling_frequency = float(sampling_frequency)
self._dtype = np.dtype(dtype)
self._probegroup = None
# random_slices_kwargs that drew each noise-level property, kept in memory only
self._noise_levels_params = {}
# initialize main annotation and properties
self.annotate(is_filtered=False)

Expand Down Expand Up @@ -337,6 +339,12 @@ def is_filtered(self):
# the is_filtered is handle with annotation
return self._annotations.get("is_filtered", False)

def set_property(self, key, values, ids=None, missing_value=None) -> None:
# when anything writes new values to a noise-level property, the old params no longer describe what's stored
# (get_noise_levels writes through this method too, and stores its params afterwards)
self._noise_levels_params.pop(key, None)
BaseExtractor.set_property(self, key, values, ids=ids, missing_value=missing_value)

def set_channel_groups(self, groups, channel_ids=None):
if "probes" in self._annotations:
warnings.warn("set_channel_groups() destroys the probe description. Using set_probe() is preferable")
Expand Down
56 changes: 37 additions & 19 deletions src/spikeinterface/core/recording_tools.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import deepcopy
from typing import Literal
import inspect
import warnings
from pathlib import Path
import os
Expand Down Expand Up @@ -470,6 +471,13 @@ def get_closest_channels(recording, channel_ids=None, num_channels=None):
return np.array(closest_channels_inds), np.array(dists)


def _fill_random_slices_kwargs(recording, random_slices_kwargs):
"""Return random_slices_kwargs with the defaults of get_random_sample_slices() filled in."""
arguments = inspect.signature(get_random_sample_slices).bind(recording, **random_slices_kwargs)
arguments.apply_defaults()
return {name: value for name, value in arguments.arguments.items() if name != "time_series"}


def _noise_level_chunk(segment_index, start_frame, end_frame, worker_ctx):
recording = worker_ctx["recording"]

Expand Down Expand Up @@ -520,7 +528,10 @@ def get_noise_levels(
Finally the average of all MAD/STD values is performed.

The result is cached in a property of the recording, so that the next call on the same
recording will use the cached result unless `force_recompute=True`.
recording will use the cached result unless `force_recompute=True` or the call's
`random_slices_kwargs` differ from those that drew it. A call without a seed accepts
any seed. Noise levels set by hand count as drawn with the default `random_slices_kwargs`
and no seed.

Parameters
----------
Expand Down Expand Up @@ -549,26 +560,32 @@ def get_noise_levels(
else:
key = f"noise_level_{method}_raw"

if key in recording.get_property_keys() and not force_recompute:
# This is to keep backward compatibility
# lets keep for a while and remove this maybe in 0.103.0
# chunk_size used to be in the signature and now is ambiguous
random_slices_kwargs_, job_kwargs = split_job_kwargs(kwargs)
if len(random_slices_kwargs_) > 0 or "chunk_size" in job_kwargs:
msg = (
"get_noise_levels(recording, num_chunks_per_segment=20) is deprecated\n"
"Now, you need to use get_noise_levels(recording, random_slices_kwargs=dict(num_chunks_per_segment=20, chunk_size=1000))\n"
"Please read get_random_sample_slices() documentation for more options."
)
# if the user use both the old and the new behavior then an error is raised
assert len(random_slices_kwargs) == 0, msg
warnings.warn(msg)
random_slices_kwargs = random_slices_kwargs_
if "chunk_size" in job_kwargs:
random_slices_kwargs["chunk_size"] = job_kwargs["chunk_size"]

params = _fill_random_slices_kwargs(recording, random_slices_kwargs)
# noise levels without stored params (set by hand, or loaded from a folder) count as an unseeded default draw
cached_params = recording._noise_levels_params.get(key, _fill_random_slices_kwargs(recording, {}))
if params["seed"] is None:
cached_params = {**cached_params, "seed": None} # a call without a seed accepts any seed

if key in recording.get_property_keys() and not force_recompute and cached_params == params:
noise_levels = recording.get_property(key=key)
else:
# This is to keep backward compatibility
# lets keep for a while and remove this maybe in 0.103.0
# chunk_size used to be in the signature and now is ambiguous
random_slices_kwargs_, job_kwargs = split_job_kwargs(kwargs)
if len(random_slices_kwargs_) > 0 or "chunk_size" in job_kwargs:
msg = (
"get_noise_levels(recording, num_chunks_per_segment=20) is deprecated\n"
"Now, you need to use get_noise_levels(recording, random_slices_kwargs=dict(num_chunks_per_segment=20, chunk_size=1000))\n"
"Please read get_random_sample_slices() documentation for more options."
)
# if the user use both the old and the new behavior then an error is raised
assert len(random_slices_kwargs) == 0, msg
warnings.warn(msg)
random_slices_kwargs = random_slices_kwargs_
if "chunk_size" in job_kwargs:
random_slices_kwargs["chunk_size"] = job_kwargs["chunk_size"]

slices = get_random_sample_slices(recording, **random_slices_kwargs)

noise_levels_chunks = []
Expand All @@ -595,6 +612,7 @@ def append_noise_chunk(res):

# set property
recording.set_property(key, noise_levels)
recording._noise_levels_params[key] = params

return noise_levels

Expand Down
18 changes: 18 additions & 0 deletions src/spikeinterface/core/tests/test_recording_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,24 @@ def test_get_noise_levels():
get_noise_levels(recording, method="std", return_in_uV=False, **job_kwargs), [std, std], rtol=1e-2, atol=1e-3
)

def mad_raw(**random_slices_kwargs):
return get_noise_levels(rec, return_in_uV=False, random_slices_kwargs=random_slices_kwargs, **job_kwargs)

# a call gets noise levels drawn with its random_slices_kwargs, where no seed accepts any seed
fewer_chunks = mad_raw(num_chunks_per_segment=5, seed=1)
assert not np.array_equal(fewer_chunks, noise_levels_1)
assert np.array_equal(mad_raw(num_chunks_per_segment=5), fewer_chunks)
unseeded = mad_raw()
assert not np.array_equal(unseeded, fewer_chunks)
seeded = mad_raw(seed=1)
assert not np.array_equal(seeded, unseeded)
assert np.array_equal(mad_raw(), seeded)

# calls that leave random_slices_kwargs at their defaults get noise levels set by hand, which drop the stored ones
rec.set_property("noise_level_mad_raw", [1.0, 2.0])
assert np.array_equal(mad_raw(), [1.0, 2.0])
assert np.array_equal(mad_raw(seed=1), seeded)


def test_get_noise_levels_output():
# Generate a recording following a gaussian distribution to check the result of get_noise.
Expand Down
7 changes: 2 additions & 5 deletions src/spikeinterface/preprocessing/highpass_spatial_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,8 @@ def __init__(

# Compute or retrieve RMS values
if rms_values is None:
if "noise_level_rms_raw" in recording.get_property_keys():
rms_values = recording.get_property("noise_level_rms_raw")
else:
random_slice_kwargs = {} if random_slice_kwargs is None else random_slice_kwargs
rms_values = get_noise_levels(recording, method="rms", return_in_uV=False, **random_slice_kwargs)
random_slice_kwargs = {} if random_slice_kwargs is None else random_slice_kwargs
rms_values = get_noise_levels(recording, method="rms", return_in_uV=False, **random_slice_kwargs)

# Pre-compute spatial filtering parameters
butter_kwargs = dict(btype="highpass", N=highpass_butter_order, Wn=highpass_butter_wn)
Expand Down
Loading