Skip to content

Commit d18cec2

Browse files
authored
Merge pull request #2902 from h-mayorquin/simplify_edge_case_chunking
Making `chunk_size=None` behavior for n_jobs=1 explicit
2 parents 6eff3a3 + 34b671f commit d18cec2

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

src/spikeinterface/core/job_tools.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def split_job_kwargs(mixed_kwargs):
131131
def divide_segment_into_chunks(num_frames, chunk_size):
132132
if chunk_size is None:
133133
chunks = [(0, num_frames)]
134+
elif chunk_size > num_frames:
135+
chunks = [(0, num_frames)]
134136
else:
135137
n = num_frames // chunk_size
136138

@@ -245,12 +247,12 @@ def ensure_chunk_size(
245247
else:
246248
raise ValueError("chunk_duration must be str or float")
247249
else:
250+
# Edge case to define single chunk per segment for n_jobs=1.
251+
# All chunking parameters equal None mean single chunk per segment
248252
if n_jobs == 1:
249-
# not chunk computing
250-
# TODO Discuss, Sam, is this something that we want to do?
251-
# Even in single process mode, we should chunk the data to avoid loading the whole thing into memory I feel
252-
# Am I wrong?
253-
chunk_size = None
253+
num_segments = recording.get_num_segments()
254+
samples_in_larger_segment = max([recording.get_num_samples(segment) for segment in range(num_segments)])
255+
chunk_size = samples_in_larger_segment
254256
else:
255257
raise ValueError("For n_jobs >1 you must specify total_memory or chunk_size or chunk_memory")
256258

src/spikeinterface/core/tests/test_job_tools.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ def test_ensure_n_jobs():
4242

4343

4444
def test_ensure_chunk_size():
45-
recording = generate_recording(num_channels=2)
45+
recording = generate_recording(num_channels=2, durations=[5.0, 2.5]) # This is the default value for two semgents
4646
dtype = recording.get_dtype()
4747
assert dtype == "float32"
48-
# make serializable
49-
recording = recording.save()
5048

5149
chunk_size = ensure_chunk_size(recording, total_memory="512M", chunk_size=None, chunk_memory=None, n_jobs=2)
5250
assert chunk_size == 32000000
@@ -69,6 +67,15 @@ def test_ensure_chunk_size():
6967
chunk_size = ensure_chunk_size(recording, chunk_duration="500ms")
7068
assert chunk_size == 15000
7169

70+
# Test edge case to define single chunk for n_jobs=1
71+
chunk_size = ensure_chunk_size(recording, n_jobs=1, chunk_size=None)
72+
chunks = divide_recording_into_chunks(recording, chunk_size)
73+
assert len(chunks) == recording.get_num_segments()
74+
for chunk in chunks:
75+
segment_index, start_frame, end_frame = chunk
76+
assert start_frame == 0
77+
assert end_frame == recording.get_num_frames(segment_index=segment_index)
78+
7279

7380
def func(segment_index, start_frame, end_frame, worker_ctx):
7481
import os

0 commit comments

Comments
 (0)