From 9bf6749440ff0cdf202f031a6d85ff1a969d57c6 Mon Sep 17 00:00:00 2001 From: Graham Findlay Date: Tue, 15 Sep 2026 15:23:08 -0500 Subject: [PATCH 1/4] Reorder spike vectors with a record-dtype numba kernel The counting-sort kernel from #4695 moved rows through a flat (N, num_fields) int64 view and addressed unit_index and segment_index as columns 1 and 2, essentially assuming minimum_spike_dtype. Samuel suggest that we might want to change the spike vector dtype in the future, so this is a more flexible variant that can take narrow index dtypes (e.g. int8). Also updated the numpy fallback to work for narrow index fields. Measured on ~400M spikes, 342 units: - 11.6 -> 12.0 s (3x int64 fields) - 13.1 -> 13.0 s (4x int64 fields) - 9.1 s (1 each of int64/int32/int8 fields) --- src/spikeinterface/core/sorting_tools.py | 86 ++++++++++--------- .../core/tests/test_sorting_tools.py | 78 +++++++++++++++++ 2 files changed, 123 insertions(+), 41 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index f37b24046d..dde71bbdb6 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -4,7 +4,7 @@ import numpy as np -from spikeinterface.core.base import BaseExtractor, minimum_spike_dtype, unit_period_dtype +from spikeinterface.core.base import BaseExtractor, unit_period_dtype from spikeinterface.core.basesorting import BaseSorting from spikeinterface.core.numpyextractors import NumpySorting @@ -149,26 +149,18 @@ def vector_to_list_of_spiketrain_numba(sample_indices, unit_indices, num_units): return vector_to_list_of_spiketrain_numba -def _is_flat_int64_view(dtype: np.dtype) -> bool: +def _numba_can_reorder(dtype: np.dtype) -> bool: """ - Whether a spike-vector dtype can be safely viewed as a flat (num_spikes, num_fields) int64 - matrix, which is what the numba counting sort moves rows through. + Whether the numba counting sort can handle this spike-vector dtype. - Requires every field to be int64, packed with no padding, and the first three fields to be - `minimum_spike_dtype`'s in order, because the kernel addresses unit_index and segment_index - positionally (columns 1 and 2) rather than by name. + We need integer `unit_index` and `segment_index` fields and no object fields. - The all-int64 rule is deliberately stricter than correctness demands -- the kernel copies rows - bitwise, so any 8-byte field would in fact round-trip through the view. Keeping it narrow means - the kernel only ever sees the layout it is written for, and it costs nothing in practice: every - spike-vector dtype spikeinterface constructs is all-int64. + Numba can handle any other fields (e.g. extra float fields, string fields) just fine through the record copy: """ names = dtype.names - if names is None or names[:3] != tuple(name for name, _ in minimum_spike_dtype): + if names is None or dtype.hasobject: return False - if dtype.itemsize != 8 * len(names): - return False - return all(dtype.fields[name][0] == np.int64 and dtype.fields[name][1] == 8 * i for i, name in enumerate(names)) + return all(name in names and dtype.fields[name][0].kind in "iu" for name in ("unit_index", "segment_index")) def reorder_spike_vector_by_unit_and_segment( @@ -245,28 +237,37 @@ def reorder_spike_vector_by_unit_and_segment( f"`spike_vector` has a unit_index outside [0, {num_units}) or a segment_index outside [0, {num_segments})." ) - # The numba kernel expects an all-int64 unpadded dtype (e.g. `minimum_spike_dtype`), but it is - # possible that a spike vector has extra fields with other dtypes (`NumpySorting` allows that). - # So we check taht the numba path is safe, and anything else takes the dtype-agnostic numpy path. - if HAVE_NUMBA and _is_flat_int64_view(spike_vector.dtype): + # If the spike-vector's dtype has object fields and/or non-integer + # unit_index and segment_index fields, we have to use numpy. + # Otherwise, we can use the numba kernel. + if HAVE_NUMBA and _numba_can_reorder(spike_vector.dtype): reorder_spike_vector = get_numba_reorder_spike_vector() - num_fields = len(spike_vector.dtype.names) - # These flat (num_spikes, num_fields) int64 views are zero-copy - in_flat = np.ascontiguousarray(spike_vector).view(np.int64).reshape(num_spikes, num_fields) - out_flat = np.empty((num_spikes, num_fields), dtype=np.int64) + spike_vector = np.ascontiguousarray(spike_vector) + ordered_spikes = np.empty(num_spikes, dtype=spike_vector.dtype) order = np.empty(num_spikes, dtype=np.int64) counts = np.empty(num_buckets, dtype=np.int64) - in_range = reorder_spike_vector(in_flat, unit_stride, segment_stride, num_buckets, out_flat, order, counts) + in_range = reorder_spike_vector( + spike_vector, + spike_vector["unit_index"], + spike_vector["segment_index"], + unit_stride, + segment_stride, + num_buckets, + ordered_spikes, + order, + counts, + ) if not in_range: raise ValueError(out_of_range_error) - - ordered_spikes = out_flat.view(spike_vector.dtype).reshape(num_spikes) return ordered_spikes, order, counts # numpy fallback: a stable argsort by bucket is equivalent to the counting sort above. - bucket_index = spike_vector["unit_index"] * unit_stride + spike_vector["segment_index"] * segment_stride + # Widen to int64 first so narrow index fields (e.g. int8) don't overflow when multiplied by the stride. + unit_index = spike_vector["unit_index"].astype(np.int64, copy=False) + segment_index = spike_vector["segment_index"].astype(np.int64, copy=False) + bucket_index = unit_index * unit_stride + segment_index * segment_stride # Must be checked before narrowing: a negative or oversized bucket would silently wrap. if bucket_index.min() < 0 or bucket_index.max() >= num_buckets: @@ -290,11 +291,15 @@ def get_numba_reorder_spike_vector(): from numba import jit @jit(nopython=True, nogil=True, cache=False) - def reorder_spike_vector_numba(in_flat, unit_stride, segment_stride, num_buckets, out_flat, order, counts): + def reorder_spike_vector_numba( + spikes, unit_index, segment_index, unit_stride, segment_stride, num_buckets, out, order, counts + ): """ - Stable counting-sort of a (N, num_fields) int64 spike-vector flat-buffer view by - (unit, segment). `num_fields` is 3 for `minimum_spike_dtype`, more when the spike vector - carries extra int64 fields; the extra columns are copied along with their spike. + Stable counting-sort of a structured spike vector by (unit, segment). + + `unit_index` and `segment_index` are the field views (i.e., zero-copy, strided) of `spikes`. + they are widened to int64 so narrow fields can't overflow the bucket arithmetic. + Rows move as whole records (`out[pos] = spikes[i]`), so any extra fields travel with their spike. Each spike's bucket is derived on the fly as `unit_index * unit_stride + segment_index * segment_stride`, so no bucket array is needed. @@ -302,25 +307,25 @@ def reorder_spike_vector_numba(in_flat, unit_stride, segment_stride, num_buckets Two O(N) passes: 1. histogram the buckets into `counts`, 2. cumulative-sum to per-bucket write positions, then scatter each - row of `in_flat` to its destination in `out_flat` and record the - source index in `order` so that ``in[order] == out``. + record of `spikes` to its destination in `out` and record the + source index in `order` so that ``spikes[order] == out``. - `out_flat`, `order` and `counts` are filled in place. + `out`, `order` and `counts` are filled in place. Stability: within each bucket, rows keep their input order, so any - ordering already present in `in_flat` (e.g. ascending sample_index - within a (segment, unit) group) carries over to `out_flat`. + ordering already present in `spikes` (e.g. ascending sample_index + within a (segment, unit) group) carries over to `out`. Returns False if any spike falls outside [0, num_buckets), in which case the outputs are meaningless; True otherwise. """ - num_spikes, num_fields = in_flat.shape + num_spikes = spikes.shape[0] # Pass 1: histogram the buckets and do bounds-check (free! we already have to make the pass) for b in range(num_buckets): counts[b] = 0 for i in range(num_spikes): - bucket = in_flat[i, 1] * unit_stride + in_flat[i, 2] * segment_stride + bucket = np.int64(unit_index[i]) * unit_stride + np.int64(segment_index[i]) * segment_stride if bucket < 0 or bucket >= num_buckets: return False counts[bucket] += 1 @@ -335,10 +340,9 @@ def reorder_spike_vector_numba(in_flat, unit_stride, segment_stride, num_buckets # Pass 2: scatter each spike into its bucket, recording where it came from. for i in range(num_spikes): - bucket = in_flat[i, 1] * unit_stride + in_flat[i, 2] * segment_stride + bucket = np.int64(unit_index[i]) * unit_stride + np.int64(segment_index[i]) * segment_stride pos = write_pos[bucket] - for field in range(num_fields): - out_flat[pos, field] = in_flat[i, field] + out[pos] = spikes[i] order[pos] = i write_pos[bucket] = pos + 1 diff --git a/src/spikeinterface/core/tests/test_sorting_tools.py b/src/spikeinterface/core/tests/test_sorting_tools.py index 7ad8291073..3e4391ee3e 100644 --- a/src/spikeinterface/core/tests/test_sorting_tools.py +++ b/src/spikeinterface/core/tests/test_sorting_tools.py @@ -94,6 +94,9 @@ def test_reorder_spike_vector_by_unit_and_segment_raises(force_numba): reorder_spike_vector_by_unit_and_segment(spikes, 1, 1) # unit_index 1 >= num_units with pytest.raises(ValueError, match="outside"): reorder_spike_vector_by_unit_and_segment(_make_spike_vector([0], [0], [5]), 1, 1) + # numba wraps a negative index, so check the lower bound + with pytest.raises(ValueError, match="outside"): + reorder_spike_vector_by_unit_and_segment(_make_spike_vector([0], [-1], [0]), 1, 1) @pytest.mark.parametrize("num_units", [2, 300, 70_000], ids=["uint8", "uint16", "uint32"]) @@ -175,6 +178,81 @@ def test_reorder_spike_vector_by_unit_and_segment_non_uniform_dtype(force_numba, assert np.array_equal(ordered_spikes, _legacy_reorder(spikes)) +NARROW_INDEX_DTYPES = { + "int64/int32/int8": np.dtype([("sample_index", "int64"), ("unit_index", "int32"), ("segment_index", "int8")]), + "uint16/int8": np.dtype([("sample_index", "int64"), ("unit_index", "uint16"), ("segment_index", "int8")]), +} + + +def _make_random_spikes(dtype, num_units, num_segments, num_spikes, seed=0): + """Spikes in valid spike-vector order (segment-blocked, sample-ascending) with random units.""" + rng = np.random.default_rng(seed) + spikes = np.empty(num_spikes, dtype=dtype) + segment_indices = np.sort(rng.integers(0, num_segments, size=num_spikes)) + spikes["segment_index"] = segment_indices + for segment_index in range(num_segments): + in_segment = segment_indices == segment_index + spikes["sample_index"][in_segment] = np.sort(rng.integers(0, 1_000, size=in_segment.sum())) + spikes["unit_index"] = rng.integers(0, num_units, size=num_spikes) + return spikes + + +@pytest.mark.parametrize("unit_major", [True, False], ids=["unit_major", "segment_major"]) +@pytest.mark.parametrize("dtype", list(NARROW_INDEX_DTYPES.values()), ids=list(NARROW_INDEX_DTYPES.keys())) +def test_reorder_spike_vector_by_unit_and_segment_narrow_index_dtypes(force_numba, dtype, unit_major): + """Make sure that narrow unit_index / segment_index fields use numba. + + """ + # 200 units x 2 segments puts the bucket index outside int8 range, + # so the kernel has to widen the fields before the bucket math. + num_units, num_segments = 200, 2 + + spikes = _make_random_spikes(dtype, num_units, num_segments, num_spikes=2_000) + if force_numba: + from spikeinterface.core.sorting_tools import _numba_can_reorder + + assert _numba_can_reorder(spikes.dtype) + + ordered_spikes, order, counts = reorder_spike_vector_by_unit_and_segment( + spikes, num_units, num_segments, unit_major=unit_major + ) + + assert ordered_spikes.dtype == spikes.dtype + assert np.array_equal(ordered_spikes, spikes[order]) + assert np.array_equal(ordered_spikes, _legacy_reorder(spikes, unit_major=unit_major)) + assert counts.sum() == spikes.size + + +def test_reorder_spike_vector_by_unit_and_segment_object_field_falls_back(): + """Make sure spike vectors with object fields use numpy""" + dtype = minimum_spike_dtype + [("label", "O")] + spikes = np.empty(6, dtype=dtype) + spikes["sample_index"] = [10, 10, 11, 12, 12, 13] + spikes["unit_index"] = [2, 0, 1, 2, 0, 0] + spikes["segment_index"] = 0 + spikes["label"] = list("abcdef") + + ordered_spikes, order, counts = reorder_spike_vector_by_unit_and_segment(spikes, 3, 1) + + assert np.array_equal(counts, [3, 1, 2]) + assert np.array_equal(ordered_spikes, spikes[order]) + assert list(ordered_spikes["label"]) == ["b", "e", "f", "c", "a", "d"] + + +def test_numba_can_reorder(): + from spikeinterface.core.sorting_tools import _numba_can_reorder + + assert _numba_can_reorder(np.dtype(minimum_spike_dtype)) + assert _numba_can_reorder(np.dtype([("sample_index", "int64"), ("unit_index", "int32"), ("segment_index", "int8")])) + + assert not _numba_can_reorder(np.dtype(minimum_spike_dtype + [("label", "O")])) + assert not _numba_can_reorder(np.dtype([("sample_index", "int64"), ("unit_index", "int64")])) + assert not _numba_can_reorder( + np.dtype([("sample_index", "int64"), ("unit_index", "float64"), ("segment_index", "int64")]) + ) + assert not _numba_can_reorder(np.dtype("int64")) + + def test_random_spikes_selection(): recording, sorting = generate_ground_truth_recording( durations=[20.0, 10.0], From ca3bb1b063829c20d8389070e1cde27dc97040f9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Sep 2026 20:27:03 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/spikeinterface/core/tests/test_sorting_tools.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/spikeinterface/core/tests/test_sorting_tools.py b/src/spikeinterface/core/tests/test_sorting_tools.py index 3e4391ee3e..ad684b1f31 100644 --- a/src/spikeinterface/core/tests/test_sorting_tools.py +++ b/src/spikeinterface/core/tests/test_sorting_tools.py @@ -200,9 +200,7 @@ def _make_random_spikes(dtype, num_units, num_segments, num_spikes, seed=0): @pytest.mark.parametrize("unit_major", [True, False], ids=["unit_major", "segment_major"]) @pytest.mark.parametrize("dtype", list(NARROW_INDEX_DTYPES.values()), ids=list(NARROW_INDEX_DTYPES.keys())) def test_reorder_spike_vector_by_unit_and_segment_narrow_index_dtypes(force_numba, dtype, unit_major): - """Make sure that narrow unit_index / segment_index fields use numba. - - """ + """Make sure that narrow unit_index / segment_index fields use numba.""" # 200 units x 2 segments puts the bucket index outside int8 range, # so the kernel has to widen the fields before the bucket math. num_units, num_segments = 200, 2 From 99ab7c5d63176bd376b6d3bd6b014e4aa68e50a1 Mon Sep 17 00:00:00 2001 From: Graham Findlay Date: Thu, 29 Oct 2026 15:57:24 -0500 Subject: [PATCH 3/4] Update reorder docstrings for flexible spike-vector dtypes --- src/spikeinterface/core/sorting_tools.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index dde71bbdb6..19f56865bb 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -155,7 +155,7 @@ def _numba_can_reorder(dtype: np.dtype) -> bool: We need integer `unit_index` and `segment_index` fields and no object fields. - Numba can handle any other fields (e.g. extra float fields, string fields) just fine through the record copy: + Any other fields (e.g. floats, fixed-length strings) are fine. """ names = dtype.names if names is None or dtype.hasobject: @@ -191,10 +191,11 @@ def reorder_spike_vector_by_unit_and_segment( Parameters ---------- spike_vector : np.ndarray or array-like - Structured array whose first three fields are `minimum_spike_dtype`. Extra trailing fields - (e.g. the "channel_index" that `to_spike_vector(main_channel_indices=...)` adds) are carried - through to the output. Anything array-like is accepted and materialised with `np.asarray`, - so lazily-backed spike vectors work, at the cost of being read into memory. + Structured array with integer "unit_index" and "segment_index" fields, in any order and of + any width. Any other fields (e.g. the "channel_index" that + `to_spike_vector(main_channel_indices=...)` adds) are carried through to the output. + Anything array-like is accepted and materialised with `np.asarray`, so lazily-backed spike + vectors work, at the cost of being read into memory. num_units : int The number of units. Every `unit_index` must be in [0, num_units). num_segments : int @@ -298,7 +299,7 @@ def reorder_spike_vector_numba( Stable counting-sort of a structured spike vector by (unit, segment). `unit_index` and `segment_index` are the field views (i.e., zero-copy, strided) of `spikes`. - they are widened to int64 so narrow fields can't overflow the bucket arithmetic. + They are widened to int64 so narrow fields can't overflow the bucket arithmetic. Rows move as whole records (`out[pos] = spikes[i]`), so any extra fields travel with their spike. Each spike's bucket is derived on the fly as From 5650bbf7021d5fbf681d878ca3e31532095f29ce Mon Sep 17 00:00:00 2001 From: Graham Findlay Date: Thu, 19 Nov 2026 09:44:07 -0600 Subject: [PATCH 4/4] Move records through the reorder kernel as raw bytes Fields numba can't compile crashed the kernel instead of falling back to numpy. Since the kernel only moves whole records, it now gets them as opaque bytes, so only object fields and non-native or non-integer index fields use numpy. Also rename and docstring clarifications from review. --- src/spikeinterface/core/sorting_tools.py | 68 ++++++++++++------- .../core/tests/test_sorting_tools.py | 10 ++- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/spikeinterface/core/sorting_tools.py b/src/spikeinterface/core/sorting_tools.py index 19f56865bb..dd85f5a1eb 100644 --- a/src/spikeinterface/core/sorting_tools.py +++ b/src/spikeinterface/core/sorting_tools.py @@ -153,14 +153,17 @@ def _numba_can_reorder(dtype: np.dtype) -> bool: """ Whether the numba counting sort can handle this spike-vector dtype. - We need integer `unit_index` and `segment_index` fields and no object fields. - - Any other fields (e.g. floats, fixed-length strings) are fine. + The kernel moves each record as raw bytes, so any field works except Python objects, whose references + can't be safely copied except through the Python interpreter. It reads `unit_index` and `segment_index`, + so these must be integers in native byte order. """ names = dtype.names if names is None or dtype.hasobject: return False - return all(name in names and dtype.fields[name][0].kind in "iu" for name in ("unit_index", "segment_index")) + return all( + name in names and dtype.fields[name][0].kind in "iu" and dtype.fields[name][0].isnative + for name in ("unit_index", "segment_index") + ) def reorder_spike_vector_by_unit_and_segment( @@ -194,6 +197,7 @@ def reorder_spike_vector_by_unit_and_segment( Structured array with integer "unit_index" and "segment_index" fields, in any order and of any width. Any other fields (e.g. the "channel_index" that `to_spike_vector(main_channel_indices=...)` adds) are carried through to the output. + Object fields force the slower numpy path. Anything array-like is accepted and materialised with `np.asarray`, so lazily-backed spike vectors work, at the cost of being read into memory. num_units : int @@ -205,11 +209,11 @@ def reorder_spike_vector_by_unit_and_segment( Returns ------- - ordered_spikes : np.ndarray + ordered_spike_vector : np.ndarray Structured array of the same dtype and length as `spike_vector`, with the spikes grouped by bucket. order : np.ndarray - 1d int64 array such that `spike_vector[order]` equals `ordered_spikes`. + 1d int64 array such that `spike_vector[order]` equals `ordered_spike_vector`. counts : np.ndarray 1d int64 array of length `num_units * num_segments`, the number of spikes in each bucket, in bucket order. @@ -238,31 +242,34 @@ def reorder_spike_vector_by_unit_and_segment( f"`spike_vector` has a unit_index outside [0, {num_units}) or a segment_index outside [0, {num_segments})." ) - # If the spike-vector's dtype has object fields and/or non-integer - # unit_index and segment_index fields, we have to use numpy. + # If the spike vector has object fields, or unit_index and segment_index fields + # that aren't native-order integers, we have to use numpy. # Otherwise, we can use the numba kernel. if HAVE_NUMBA and _numba_can_reorder(spike_vector.dtype): reorder_spike_vector = get_numba_reorder_spike_vector() spike_vector = np.ascontiguousarray(spike_vector) - ordered_spikes = np.empty(num_spikes, dtype=spike_vector.dtype) + ordered_spike_vector = np.empty(num_spikes, dtype=spike_vector.dtype) order = np.empty(num_spikes, dtype=np.int64) counts = np.empty(num_buckets, dtype=np.int64) + # The kernel only moves whole records, so it gets them as opaque bytes and never has to + # understand the other fields (numba can't compile some of them, e.g. float16). + record_bytes = np.dtype([("bytes", "uint8", (spike_vector.dtype.itemsize,))]) in_range = reorder_spike_vector( - spike_vector, + spike_vector.view(record_bytes), spike_vector["unit_index"], spike_vector["segment_index"], unit_stride, segment_stride, num_buckets, - ordered_spikes, + ordered_spike_vector.view(record_bytes), order, counts, ) if not in_range: raise ValueError(out_of_range_error) - return ordered_spikes, order, counts + return ordered_spike_vector, order, counts # numpy fallback: a stable argsort by bucket is equivalent to the counting sort above. # Widen to int64 first so narrow index fields (e.g. int8) don't overflow when multiplied by the stride. @@ -281,8 +288,8 @@ def reorder_spike_vector_by_unit_and_segment( bucket_index = bucket_index.astype(np.min_scalar_type(num_buckets - 1), copy=False) order = np.argsort(bucket_index, kind="stable") # radix sort, because of integer key - ordered_spikes = spike_vector[order] - return ordered_spikes, order, counts + ordered_spike_vector = spike_vector[order] + return ordered_spike_vector, order, counts def get_numba_reorder_spike_vector(): @@ -293,14 +300,25 @@ def get_numba_reorder_spike_vector(): @jit(nopython=True, nogil=True, cache=False) def reorder_spike_vector_numba( - spikes, unit_index, segment_index, unit_stride, segment_stride, num_buckets, out, order, counts + spike_vector, + unit_index, + segment_index, + unit_stride, + segment_stride, + num_buckets, + ordered_spike_vector, + order, + counts, ): """ Stable counting-sort of a structured spike vector by (unit, segment). - `unit_index` and `segment_index` are the field views (i.e., zero-copy, strided) of `spikes`. - They are widened to int64 so narrow fields can't overflow the bucket arithmetic. - Rows move as whole records (`out[pos] = spikes[i]`), so any extra fields travel with their spike. + `spike_vector` and `ordered_spike_vector` hold each record as opaque bytes, so the kernel is the same + whatever the fields. `unit_index` and `segment_index` are the field views (i.e., zero-copy, strided) + of the spike vector. They can be of any integer dtype, so they are widened to int64 to keep narrow + fields from overflowing the bucket arithmetic. + Rows move as whole records (`ordered_spike_vector[pos] = spike_vector[i]`), so every field travels + with its spike. Each spike's bucket is derived on the fly as `unit_index * unit_stride + segment_index * segment_stride`, so no bucket array is needed. @@ -308,19 +326,19 @@ def reorder_spike_vector_numba( Two O(N) passes: 1. histogram the buckets into `counts`, 2. cumulative-sum to per-bucket write positions, then scatter each - record of `spikes` to its destination in `out` and record the - source index in `order` so that ``spikes[order] == out``. + record of `spike_vector` to its destination in `ordered_spike_vector` and record the + source index in `order` so that ``spike_vector[order] == ordered_spike_vector``. - `out`, `order` and `counts` are filled in place. + `ordered_spike_vector`, `order` and `counts` are filled in place. Stability: within each bucket, rows keep their input order, so any - ordering already present in `spikes` (e.g. ascending sample_index - within a (segment, unit) group) carries over to `out`. + ordering already present in `spike_vector` (e.g. ascending sample_index + within a (segment, unit) group) carries over to `ordered_spike_vector`. Returns False if any spike falls outside [0, num_buckets), in which case the outputs are meaningless; True otherwise. """ - num_spikes = spikes.shape[0] + num_spikes = spike_vector.shape[0] # Pass 1: histogram the buckets and do bounds-check (free! we already have to make the pass) for b in range(num_buckets): @@ -343,7 +361,7 @@ def reorder_spike_vector_numba( for i in range(num_spikes): bucket = np.int64(unit_index[i]) * unit_stride + np.int64(segment_index[i]) * segment_stride pos = write_pos[bucket] - out[pos] = spikes[i] + ordered_spike_vector[pos] = spike_vector[i] order[pos] = i write_pos[bucket] = pos + 1 diff --git a/src/spikeinterface/core/tests/test_sorting_tools.py b/src/spikeinterface/core/tests/test_sorting_tools.py index ad684b1f31..afd608739d 100644 --- a/src/spikeinterface/core/tests/test_sorting_tools.py +++ b/src/spikeinterface/core/tests/test_sorting_tools.py @@ -157,10 +157,10 @@ def test_reorder_spike_vector_by_unit_and_segment_extra_fields(force_numba, unit assert counts.sum() == num_spikes -@pytest.mark.parametrize("extra_field", [("amplitude", "float32"), ("amplitude", "float64")]) +@pytest.mark.parametrize("extra_field", [("amplitude", "float32"), ("amplitude", "float16")]) def test_reorder_spike_vector_by_unit_and_segment_non_uniform_dtype(force_numba, extra_field): """`NumpySorting` stores whatever dtype its caller hands it. Make sure these weird spike - vectors still get reordered correctly. + vectors still get reordered correctly, including fields numba itself can't compile (float16). """ dtype = minimum_spike_dtype + [extra_field] spikes = np.empty(6, dtype=dtype) @@ -242,8 +242,14 @@ def test_numba_can_reorder(): assert _numba_can_reorder(np.dtype(minimum_spike_dtype)) assert _numba_can_reorder(np.dtype([("sample_index", "int64"), ("unit_index", "int32"), ("segment_index", "int8")])) + # records move as raw bytes, so fields numba itself can't compile are fine + assert _numba_can_reorder(np.dtype(minimum_spike_dtype + [("amplitude", "float16"), ("flags", "?", (4,))])) assert not _numba_can_reorder(np.dtype(minimum_spike_dtype + [("label", "O")])) + non_native_int32 = np.dtype("int32").newbyteorder() + assert not _numba_can_reorder( + np.dtype([("sample_index", "int64"), ("unit_index", non_native_int32), ("segment_index", "int8")]) + ) assert not _numba_can_reorder(np.dtype([("sample_index", "int64"), ("unit_index", "int64")])) assert not _numba_can_reorder( np.dtype([("sample_index", "int64"), ("unit_index", "float64"), ("segment_index", "int64")])