Coverage for src/evutils/repr/_voxel.py: 100%
16 statements
« prev ^ index » next coverage.py v7.15.1, created at 2026-07-15 09:33 +0000
« prev ^ index » next coverage.py v7.15.1, created at 2026-07-15 09:33 +0000
2"""Module for generating voxel grid representations from events."""
4from ._histogram import histogram
5from ..chunking import window_delta_t
8import numpy as np
11from typing import Any
12def voxel_histogram(events: np.ndarray, width: int = 1280, height: int = 720, n_bins: int = 10, dt: int = 10_000, dtype: Any = np.uint8) -> np.ndarray:
13 """Generate a voxel grid from the events.
15 Parameters
16 ----------
17 events : np.ndarray
18 Array of events in the :class:`~evutils.types.Events` format.
19 width : int, optional
20 Width of the voxel grid, by default 1280.
21 height : int, optional
22 Height of the voxel grid, by default 720.
23 n_bins : int, optional
24 Number of depth bins (time slices) in the voxel grid, by default 10.
25 dt : int, optional
26 Time delta in microseconds for events buffer by default 10_000 (10 ms).
27 dtype : np.dtype, optional
28 Data type of the output voxel grid, by default np.uint8.
30 Returns
31 -------
32 np.ndarray
33 A numpy array with the voxel grid (n_bins, height, width, 3).
35 Examples
36 --------
37 >>> import numpy as np
38 >>> from evutils.repr import voxel_histogram
39 >>> events = np.array([(10, 20, 100, 1), (15, 25, 200, 0), (20, 30, 10000, 1)],
40 ... dtype=[('x', '<u2'), ('y', '<u2'), ('t', '<i8'), ('p', 'i1')])
41 >>> grid = voxel_histogram(events, width=100, height=100, n_bins=10, dt=10000)
42 >>> grid.shape
43 (10, 100, 100, 3)
44 """
45 buffer = np.zeros((n_bins, height, width, 3), dtype=dtype)
48 if len(events) <= 2:
49 return buffer
51 assert events[-1]['t'] - events[0]['t'] <= dt # Ensure that the events are within the specified time delta
53 bin_dt = dt // n_bins # Time per bin in microseconds
55 for i, e in enumerate(window_delta_t(events, delta_t=bin_dt)):
56 if i >= n_bins:
57 break
58 hist = histogram(e, width=width, height=height, fill=False, dtype=dtype)
60 # Only keep the r and b channels
61 buffer[i] = hist
65 return buffer