Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a4c4c98
BaseChunkExecutor
alejoe91 Nov 14, 2025
bef0fce
Create BaseChunkExecutor class
alejoe91 Nov 14, 2025
c82eb32
Even better: just ChunkExecutor
alejoe91 Nov 14, 2025
7d1d64e
some more renaming
alejoe91 Nov 14, 2025
56baa5d
get_sample_size_in_bytes
alejoe91 Nov 21, 2025
3da238b
get_sample_size -> get_sample_size_in_bytes
alejoe91 Nov 26, 2025
31c7130
Merge branch 'main' into base-chunk-executor
alejoe91 Nov 27, 2025
adc0683
Make ChunkableMixin to generalize paralleization machinery
alejoe91 Nov 28, 2025
d92f22c
Add chunkable mixin and tools
alejoe91 Nov 28, 2025
13e1d38
Move more methods to chunkable_tools
alejoe91 Nov 28, 2025
42cace4
Make ChunkableMixin a proper abstract class
alejoe91 Nov 28, 2025
df347ee
fix kilosort4 write_binary call
alejoe91 Nov 28, 2025
4691004
Fix _preferred_mp_context resolution and deepinterpolation tests
alejoe91 Dec 2, 2025
6695d65
Fix conflicts
alejoe91 Dec 11, 2025
6d72750
Merge branch 'main' of github.com:SpikeInterface/spikeinterface into …
alejoe91 Dec 11, 2025
998ba7c
Fix segments in operators
alejoe91 Dec 11, 2025
122d7c2
Fix add_segment
alejoe91 Dec 12, 2025
ec8e07a
Add __future__ annotations
alejoe91 Dec 12, 2025
7515bf5
Move chunkable to its own file and move time operations to chunkable …
alejoe91 Jan 19, 2026
d4ebe86
Solve conflicts
alejoe91 Jan 19, 2026
3ad3ef3
dolve conflicts 2
alejoe91 Jan 19, 2026
393ffe5
Move get_durations to chunkable mixin
alejoe91 Jan 19, 2026
04f44e8
Move get_memory_size to chunkable
alejoe91 Jan 19, 2026
8da015a
fix tests
alejoe91 Jan 20, 2026
9f270d1
Fix tests
alejoe91 Jan 20, 2026
b77a6e0
fix conflicts
alejoe91 Jan 20, 2026
ce5c07f
solve conflicts
alejoe91 Feb 18, 2026
7d6031e
Move write_chunkable_to_zarr to chunkable_tools
alejoe91 Feb 18, 2026
5151b38
typos
alejoe91 Feb 24, 2026
2768ea7
Merge branch 'base-chunk-executor' of github.com:alejoe91/spikeinterf…
alejoe91 Feb 27, 2026
564b123
Fix chunkable zar rmanagement
alejoe91 Feb 27, 2026
f6fbef9
Add zarr_class_info to zarr attrs
alejoe91 Mar 2, 2026
0b17fa2
fix: pass dataset names for datasets externally
alejoe91 Mar 13, 2026
793aab6
fix: support partial time vectors
alejoe91 Mar 13, 2026
8e79c03
Merge branch 'base-chunk-executor' of github.com:alejoe91/spikeinterf…
alejoe91 Mar 18, 2026
ad17c88
wip
alejoe91 Mar 18, 2026
e1014dd
Make PipelineNode work with ChunkableMixin
alejoe91 Mar 19, 2026
5604c21
fix sortingcomponents
alejoe91 Mar 19, 2026
2432ef6
make check graph optional
alejoe91 Mar 19, 2026
a861af7
solve conflicts
alejoe91 Mar 19, 2026
4f395c6
oups
alejoe91 Mar 19, 2026
3e15601
fix conflixts
alejoe91 Mar 19, 2026
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
2 changes: 1 addition & 1 deletion doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Low-level
.. automodule:: spikeinterface.core
:noindex:

.. autoclass:: ChunkRecordingExecutor
.. autoclass:: ChunkExecutor


Back-compatibility with ``WaveformExtractor`` (version > 0.100.0)
Expand Down
2 changes: 1 addition & 1 deletion src/spikeinterface/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
get_best_job_kwargs,
ensure_n_jobs,
ensure_chunk_size,
ChunkRecordingExecutor,
ChunkExecutor,
split_job_kwargs,
fix_job_kwargs,
)
Expand Down
14 changes: 14 additions & 0 deletions src/spikeinterface/core/baserecording.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ def add_recording_segment(self, recording_segment):
self._recording_segments.append(recording_segment)
recording_segment.set_parent_extractor(self)

def get_sample_size(self):
"""
Returns the size of a single sample across all channels in bytes.

Returns
-------
int
The size of a single sample in bytes
"""
num_channels = self.get_num_channels()
dtype_size_bytes = self.get_dtype().itemsize
sample_size = num_channels * dtype_size_bytes
return sample_size

def get_num_samples(self, segment_index: int | None = None) -> int:
"""
Returns the number of samples for a segment.
Expand Down
94 changes: 50 additions & 44 deletions src/spikeinterface/core/job_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,16 @@ def divide_segment_into_chunks(num_frames, chunk_size):
return chunks


def divide_recording_into_chunks(recording, chunk_size):
recording_slices = []
def divide_extractor_into_chunks(recording, chunk_size):
slices = []
for segment_index in range(recording.get_num_segments()):
num_frames = recording.get_num_samples(segment_index)
chunks = divide_segment_into_chunks(num_frames, chunk_size)
recording_slices.extend([(segment_index, frame_start, frame_stop) for frame_start, frame_stop in chunks])
return recording_slices
slices.extend([(segment_index, frame_start, frame_stop) for frame_start, frame_stop in chunks])
return slices


def ensure_n_jobs(recording, n_jobs=1):
def ensure_n_jobs(extractor, n_jobs=1):
if n_jobs == -1:
n_jobs = os.cpu_count()
elif n_jobs == 0:
Expand All @@ -244,10 +244,10 @@ def ensure_n_jobs(recording, n_jobs=1):
print(f"Python {sys.version} does not support parallel processing")
n_jobs = 1

if not recording.check_if_memory_serializable():
if not extractor.check_if_memory_serializable():
if n_jobs != 1:
raise RuntimeError(
"Recording is not serializable to memory and can't be processed in parallel. "
"Extractor is not serializable to memory and can't be processed in parallel. "
"You can use the `rec = recording.save(folder=...)` function or set 'n_jobs' to 1."
)

Expand All @@ -271,10 +271,10 @@ def chunk_duration_to_chunk_size(chunk_duration, recording):


def ensure_chunk_size(
recording, total_memory=None, chunk_size=None, chunk_memory=None, chunk_duration=None, n_jobs=1, **other_kwargs
extractor, total_memory=None, chunk_size=None, chunk_memory=None, chunk_duration=None, n_jobs=1, **other_kwargs
):
"""
"chunk_size" is the traces.shape[0] for each worker.
"chunk_size" is the number of samples for each worker.

Flexible chunk_size setter with 3 ways:
* "chunk_size" : is the length in sample for each chunk independently of channel count and dtype.
Expand Down Expand Up @@ -305,34 +305,30 @@ def ensure_chunk_size(
assert total_memory is None
# set by memory per worker size
chunk_memory = convert_string_to_bytes(chunk_memory)
n_bytes = np.dtype(recording.get_dtype()).itemsize
num_channels = recording.get_num_channels()
chunk_size = int(chunk_memory / (num_channels * n_bytes))
chunk_size = int(chunk_memory / extractor.get_sample_size())
elif total_memory is not None:
# clip by total memory size
n_jobs = ensure_n_jobs(recording, n_jobs=n_jobs)
n_jobs = ensure_n_jobs(extractor, n_jobs=n_jobs)
total_memory = convert_string_to_bytes(total_memory)
n_bytes = np.dtype(recording.get_dtype()).itemsize
num_channels = recording.get_num_channels()
chunk_size = int(total_memory / (num_channels * n_bytes * n_jobs))
chunk_size = int(total_memory / (extractor.get_sample_size() * n_jobs))
elif chunk_duration is not None:
chunk_size = chunk_duration_to_chunk_size(chunk_duration, recording)
chunk_size = chunk_duration_to_chunk_size(chunk_duration, extractor)
else:
# Edge case to define single chunk per segment for n_jobs=1.
# All chunking parameters equal None mean single chunk per segment
if n_jobs == 1:
num_segments = recording.get_num_segments()
samples_in_larger_segment = max([recording.get_num_samples(segment) for segment in range(num_segments)])
num_segments = extractor.get_num_segments()
samples_in_larger_segment = max([extractor.get_num_samples(segment) for segment in range(num_segments)])
chunk_size = samples_in_larger_segment
else:
raise ValueError("For n_jobs >1 you must specify total_memory or chunk_size or chunk_memory")

return chunk_size


class ChunkRecordingExecutor:
class ChunkExecutor:
"""
Core class for parallel processing to run a "function" over chunks on a recording.
Core class for parallel processing to run a "function" over chunks on a chunkable extractor.

It supports running a function:
* in loop with chunk processing (low RAM usage)
Expand All @@ -344,8 +340,9 @@ class ChunkRecordingExecutor:

Parameters
----------
recording : RecordingExtractor
The recording to be processed
extractor : BaseExtractor
The extractor to be processed.
It needs to implement the `get_sample_size()`, `get_num_samples()` and `get_num_segments()`
func : function
Function that runs on each chunk
init_func : function
Expand Down Expand Up @@ -393,7 +390,7 @@ class ChunkRecordingExecutor:

def __init__(
self,
recording,
extractor: "BaseExtractor",
func,
init_func,
init_args,
Expand All @@ -412,14 +409,15 @@ def __init__(
max_threads_per_worker=1,
need_worker_index=False,
):
self.recording = recording
self.extractor = extractor
self.func = func
self.init_func = init_func
self.init_args = init_args

if pool_engine == "process":
if mp_context is None:
mp_context = recording.get_preferred_mp_context()
if hasattr(extractor, "get_preferred_mp_context"):
mp_context = extractor.get_preferred_mp_context()
if mp_context is not None and platform.system() == "Windows":
assert mp_context != "fork", "'fork' mp_context not supported on Windows!"
elif mp_context == "fork" and platform.system() == "Darwin":
Expand All @@ -433,9 +431,8 @@ def __init__(
self.handle_returns = handle_returns
self.gather_func = gather_func

self.n_jobs = ensure_n_jobs(recording, n_jobs=n_jobs)
self.chunk_size = ensure_chunk_size(
recording,
self.n_jobs = ensure_n_jobs(self.extractor, n_jobs=n_jobs)
self.chunk_size = self.ensure_chunk_size(
total_memory=total_memory,
chunk_size=chunk_size,
chunk_memory=chunk_memory,
Expand All @@ -450,9 +447,9 @@ def __init__(
self.need_worker_index = need_worker_index

if verbose:
chunk_memory = self.chunk_size * recording.get_num_channels() * np.dtype(recording.get_dtype()).itemsize
chunk_memory = self.get_chunk_memory()
total_memory = chunk_memory * self.n_jobs
chunk_duration = self.chunk_size / recording.get_sampling_frequency()
chunk_duration = self.chunk_size / extractor.sampling_frequency
chunk_memory_str = convert_bytes_to_str(chunk_memory)
total_memory_str = convert_bytes_to_str(total_memory)
chunk_duration_str = convert_seconds_to_str(chunk_duration)
Expand All @@ -467,13 +464,24 @@ def __init__(
f"chunk_duration={chunk_duration_str}",
)

def run(self, recording_slices=None):
def get_chunk_memory(self):
return self.chunk_size * self.extractor.get_sample_size()

def ensure_chunk_size(
self, total_memory=None, chunk_size=None, chunk_memory=None, chunk_duration=None, n_jobs=1, **other_kwargs
):
return ensure_chunk_size(
self.extractor, total_memory, chunk_size, chunk_memory, chunk_duration, n_jobs, **other_kwargs
)

def run(self, slices=None):
"""
Runs the defined jobs.
"""

if recording_slices is None:
recording_slices = divide_recording_into_chunks(self.recording, self.chunk_size)
if slices is None:
# TODO: rename
slices = divide_extractor_into_chunks(self.extractor, self.chunk_size)

if self.handle_returns:
returns = []
Expand All @@ -482,23 +490,21 @@ def run(self, recording_slices=None):

if self.n_jobs == 1:
if self.progress_bar:
recording_slices = tqdm(
recording_slices, desc=f"{self.job_name} (no parallelization)", total=len(recording_slices)
)
slices = tqdm(slices, desc=f"{self.job_name} (no parallelization)", total=len(slices))

worker_dict = self.init_func(*self.init_args)
if self.need_worker_index:
worker_dict["worker_index"] = 0

for segment_index, frame_start, frame_stop in recording_slices:
for segment_index, frame_start, frame_stop in slices:
res = self.func(segment_index, frame_start, frame_stop, worker_dict)
if self.handle_returns:
returns.append(res)
if self.gather_func is not None:
self.gather_func(res)

else:
n_jobs = min(self.n_jobs, len(recording_slices))
n_jobs = min(self.n_jobs, len(slices))

if self.pool_engine == "process":

Expand Down Expand Up @@ -526,11 +532,11 @@ def run(self, recording_slices=None):
array_pid,
),
) as executor:
results = executor.map(process_function_wrapper, recording_slices)
results = executor.map(process_function_wrapper, slices)

if self.progress_bar:
results = tqdm(
results, desc=f"{self.job_name} (workers: {n_jobs} processes)", total=len(recording_slices)
results, desc=f"{self.job_name} (workers: {n_jobs} processes)", total=len(slices)
)

for res in results:
Expand All @@ -549,7 +555,7 @@ def run(self, recording_slices=None):
if self.progress_bar:
# here the tqdm threading do not work (maybe collision) so we need to create a pbar
# before thread spawning
pbar = tqdm(desc=f"{self.job_name} (workers: {n_jobs} threads)", total=len(recording_slices))
pbar = tqdm(desc=f"{self.job_name} (workers: {n_jobs} threads)", total=len(slices))

if self.need_worker_index:
lock = threading.Lock()
Expand All @@ -570,8 +576,8 @@ def run(self, recording_slices=None):
),
) as executor:

recording_slices2 = [(thread_local_data,) + tuple(args) for args in recording_slices]
results = executor.map(thread_function_wrapper, recording_slices2)
slices2 = [(thread_local_data,) + tuple(args) for args in slices]
results = executor.map(thread_function_wrapper, slices2)

for res in results:
if self.progress_bar:
Expand Down
10 changes: 5 additions & 5 deletions src/spikeinterface/core/node_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np

from spikeinterface.core import BaseRecording, get_chunk_with_margin
from spikeinterface.core.job_tools import ChunkRecordingExecutor, fix_job_kwargs, _shared_job_kwargs_doc
from spikeinterface.core.job_tools import ChunkExecutor, fix_job_kwargs, _shared_job_kwargs_doc
from spikeinterface.core import get_channel_distances


Expand Down Expand Up @@ -533,7 +533,7 @@ def run_node_pipeline(
names=None,
verbose=False,
skip_after_n_peaks=None,
recording_slices=None,
slices=None,
):
"""
Machinery to compute in parallel operations on peaks and traces.
Expand Down Expand Up @@ -585,7 +585,7 @@ def run_node_pipeline(
skip_after_n_peaks : None | int
Skip the computation after n_peaks.
This is not an exact because internally this skip is done per worker in average.
recording_slices : None | list[tuple]
slices : None | list[tuple]
Optionaly give a list of slices to run the pipeline only on some chunks of the recording.
It must be a list of (segment_index, frame_start, frame_stop).
If None (default), the function iterates over the entire duration of the recording.
Expand Down Expand Up @@ -616,7 +616,7 @@ def run_node_pipeline(

init_args = (recording, nodes, skip_after_n_peaks_per_worker)

processor = ChunkRecordingExecutor(
processor = ChunkExecutor(
recording,
_compute_peak_pipeline_chunk,
_init_peak_pipeline,
Expand All @@ -627,7 +627,7 @@ def run_node_pipeline(
**job_kwargs,
)

processor.run(recording_slices=recording_slices)
processor.run(slices=slices)

outs = gather_func.finalize_buffers(squeeze_output=squeeze_output)
return outs
Expand Down
Loading