Skip to content

Commit 35a9a1c

Browse files
authored
Merge pull request #1970 from h-mayorquin/change_default_in_generate_recording
Some additions to generate.py after #1948
2 parents 5ee4563 + ac65bc5 commit 35a9a1c

File tree

1 file changed

+31
-26
lines changed

1 file changed

+31
-26
lines changed

src/spikeinterface/core/generate.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import warnings
33
import numpy as np
44
from typing import Union, Optional, List, Literal
5+
import warnings
56

67

78
from .numpyextractors import NumpyRecording, NumpySorting
@@ -31,7 +32,7 @@ def generate_recording(
3132
set_probe: Optional[bool] = True,
3233
ndim: Optional[int] = 2,
3334
seed: Optional[int] = None,
34-
mode: Literal["lazy", "legacy"] = "legacy",
35+
mode: Literal["lazy", "legacy"] = "lazy",
3536
) -> BaseRecording:
3637
"""
3738
Generate a recording object.
@@ -51,10 +52,10 @@ def generate_recording(
5152
The number of dimensions of the probe, by default 2. Set to 3 to make 3 dimensional probes.
5253
seed : Optional[int]
5354
A seed for the np.ramdom.default_rng function
54-
mode: str ["lazy", "legacy"] Default "legacy".
55+
mode: str ["lazy", "legacy"] Default "lazy".
5556
"legacy": generate a NumpyRecording with white noise.
56-
This mode is kept for backward compatibility and will be deprecated in next release.
57-
"lazy": return a NoiseGeneratorRecording
57+
This mode is kept for backward compatibility and will be deprecated version 0.100.0.
58+
"lazy": return a NoiseGeneratorRecording instance.
5859
5960
Returns
6061
-------
@@ -64,6 +65,10 @@ def generate_recording(
6465
seed = _ensure_seed(seed)
6566

6667
if mode == "legacy":
68+
warnings.warn(
69+
"generate_recording() : mode='legacy' will be deprecated in version 0.100.0. Use mode='lazy' instead.",
70+
DeprecationWarning,
71+
)
6772
recording = _generate_recording_legacy(num_channels, sampling_frequency, durations, seed)
6873
elif mode == "lazy":
6974
recording = NoiseGeneratorRecording(
@@ -538,7 +543,7 @@ def synthetize_spike_train_bad_isi(duration, baseline_rate, num_violations, viol
538543

539544
class NoiseGeneratorRecording(BaseRecording):
540545
"""
541-
A lazy recording that generates random samples if and only if `get_traces` is called.
546+
A lazy recording that generates white noise samples if and only if `get_traces` is called.
542547
543548
This done by tiling small noise chunk.
544549
@@ -555,7 +560,7 @@ class NoiseGeneratorRecording(BaseRecording):
555560
The sampling frequency of the recorder.
556561
durations : List[float]
557562
The durations of each segment in seconds. Note that the length of this list is the number of segments.
558-
noise_level: float, default 5:
563+
noise_level: float, default 1:
559564
Std of the white noise
560565
dtype : Optional[Union[np.dtype, str]], default='float32'
561566
The dtype of the recording. Note that only np.float32 and np.float64 are supported.
@@ -581,7 +586,7 @@ def __init__(
581586
num_channels: int,
582587
sampling_frequency: float,
583588
durations: List[float],
584-
noise_level: float = 5.0,
589+
noise_level: float = 1.0,
585590
dtype: Optional[Union[np.dtype, str]] = "float32",
586591
seed: Optional[int] = None,
587592
strategy: Literal["tile_pregenerated", "on_the_fly"] = "tile_pregenerated",
@@ -647,7 +652,7 @@ def __init__(
647652
if self.strategy == "tile_pregenerated":
648653
rng = np.random.default_rng(seed=self.seed)
649654
self.noise_block = (
650-
rng.standard_normal(size=(self.noise_block_size, self.num_channels)).astype(self.dtype) * noise_level
655+
rng.standard_normal(size=(self.noise_block_size, self.num_channels), dtype=self.dtype) * noise_level
651656
)
652657
elif self.strategy == "on_the_fly":
653658
pass
@@ -664,35 +669,35 @@ def get_traces(
664669
start_frame = 0 if start_frame is None else max(start_frame, 0)
665670
end_frame = self.num_samples if end_frame is None else min(end_frame, self.num_samples)
666671

667-
start_frame_mod = start_frame % self.noise_block_size
668-
end_frame_mod = end_frame % self.noise_block_size
672+
start_frame_within_block = start_frame % self.noise_block_size
673+
end_frame_within_block = end_frame % self.noise_block_size
669674
num_samples = end_frame - start_frame
670675

671676
traces = np.empty(shape=(num_samples, self.num_channels), dtype=self.dtype)
672677

673-
start_block_index = start_frame // self.noise_block_size
674-
end_block_index = end_frame // self.noise_block_size
678+
first_block_index = start_frame // self.noise_block_size
679+
last_block_index = end_frame // self.noise_block_size
675680

676681
pos = 0
677-
for block_index in range(start_block_index, end_block_index + 1):
682+
for block_index in range(first_block_index, last_block_index + 1):
678683
if self.strategy == "tile_pregenerated":
679684
noise_block = self.noise_block
680685
elif self.strategy == "on_the_fly":
681686
rng = np.random.default_rng(seed=(self.seed, block_index))
682-
noise_block = rng.standard_normal(size=(self.noise_block_size, self.num_channels)).astype(self.dtype)
687+
noise_block = rng.standard_normal(size=(self.noise_block_size, self.num_channels), dtype=self.dtype)
683688
noise_block *= self.noise_level
684689

685-
if block_index == start_block_index:
686-
if start_block_index != end_block_index:
687-
end_first_block = self.noise_block_size - start_frame_mod
688-
traces[:end_first_block] = noise_block[start_frame_mod:]
690+
if block_index == first_block_index:
691+
if first_block_index != last_block_index:
692+
end_first_block = self.noise_block_size - start_frame_within_block
693+
traces[:end_first_block] = noise_block[start_frame_within_block:]
689694
pos += end_first_block
690695
else:
691696
# special case when unique block
692-
traces[:] = noise_block[start_frame_mod : start_frame_mod + traces.shape[0]]
693-
elif block_index == end_block_index:
694-
if end_frame_mod > 0:
695-
traces[pos:] = noise_block[:end_frame_mod]
697+
traces[:] = noise_block[start_frame_within_block : start_frame_within_block + num_samples]
698+
elif block_index == last_block_index:
699+
if end_frame_within_block > 0:
700+
traces[pos:] = noise_block[:end_frame_within_block]
696701
else:
697702
traces[pos : pos + self.noise_block_size] = noise_block
698703
pos += self.noise_block_size
@@ -710,7 +715,7 @@ def get_traces(
710715

711716
def generate_recording_by_size(
712717
full_traces_size_GiB: float,
713-
num_channels: int = 1024,
718+
num_channels: int = 384,
714719
seed: Optional[int] = None,
715720
strategy: Literal["tile_pregenerated", "on_the_fly"] = "tile_pregenerated",
716721
) -> NoiseGeneratorRecording:
@@ -719,15 +724,15 @@ def generate_recording_by_size(
719724
This is a convenience wrapper around the NoiseGeneratorRecording class where only
720725
the size in GiB (NOT GB!) is specified.
721726
722-
It is generated with 1024 channels and a sampling frequency of 1 Hz. The duration is manipulted to
727+
It is generated with 384 channels and a sampling frequency of 1 Hz. The duration is manipulted to
723728
produced the desired size.
724729
725730
Seee GeneratorRecording for more details.
726731
727732
Parameters
728733
----------
729734
full_traces_size_GiB : float
730-
The size in gibibyte (GiB) of the recording.
735+
The size in gigabytes (GiB) of the recording.
731736
num_channels: int
732737
Number of channels.
733738
seed : int, optional
@@ -740,7 +745,7 @@ def generate_recording_by_size(
740745

741746
dtype = np.dtype("float32")
742747
sampling_frequency = 30_000.0 # Hz
743-
num_channels = 1024
748+
num_channels = 384
744749

745750
GiB_to_bytes = 1024**3
746751
full_traces_size_bytes = int(full_traces_size_GiB * GiB_to_bytes)

0 commit comments

Comments
 (0)