File size: 5,714 Bytes
4a9362b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Shared voxelization and representation utilities for eAP data."""

from __future__ import annotations

from collections.abc import Iterable, Sequence

import numpy as np
import torch

from e_jepa_ttc.data.eap import EAP_IMAGE_SIZE
from e_jepa_ttc.data.types import EventBatch
from e_jepa_ttc.representations.voxel_grid import encode_voxel_grid, robust_normalize
from e_jepa_ttc.training.carla_jepa import (
    EVTTC_BASE_EVENT_CHANNELS,
    EVTTC_BASE_INPUT_CHANNELS,
)


def downsample_full_frame(
    events: dict[str, np.ndarray],
    *,
    sequence_id: str,
    start_us: int,
    end_us: int,
    width: int,
    height: int,
) -> EventBatch:
    source_width, source_height = EAP_IMAGE_SIZE
    x = np.minimum(
        np.asarray(events["x"], dtype=np.int64) * width // source_width,
        width - 1,
    ).astype(np.int32)
    y = np.minimum(
        np.asarray(events["y"], dtype=np.int64) * height // source_height,
        height - 1,
    ).astype(np.int32)
    return EventBatch(
        x=x,
        y=y,
        t_us=np.asarray(events["t"], dtype=np.int64),
        polarity=np.where(np.asarray(events["p"]) > 0, 1, -1).astype(np.int8),
        width=width,
        height=height,
        sequence_id=sequence_id,
        t_start_us=start_us,
        t_end_us=end_us,
    )


def base_compatible_voxel(events: EventBatch, *, bins: int) -> torch.Tensor:
    voxel = encode_voxel_grid(events, bins=bins, normalize=True)
    tensor = np.zeros(
        (EVTTC_BASE_INPUT_CHANNELS, events.height, events.width),
        dtype=np.float32,
    )
    tensor[:EVTTC_BASE_EVENT_CHANNELS] = voxel
    duration_s = max((events.t_end_us - events.t_start_us) * 1e-6, 1e-6)
    tensor[10] = np.log1p(events.num_events)
    tensor[11] = np.log1p(events.num_events / duration_s)
    return torch.from_numpy(tensor)


def event_voxel_with_scalars(events: EventBatch, *, bins_per_polarity: int) -> torch.Tensor:
    """Encode an arbitrary temporal resolution plus count and rate channels.

    Five bins per polarity reproduce the first twelve channels of
    :func:`base_compatible_voxel`. Higher resolutions do not allocate the nine
    historical compatibility channels.
    """

    if bins_per_polarity <= 0:
        raise ValueError("bins_per_polarity must be positive")
    voxel = encode_voxel_grid(events, bins=bins_per_polarity, normalize=True)
    event_channels = 2 * bins_per_polarity
    if voxel.shape[0] != event_channels:
        raise RuntimeError("voxel channel count differs from the temporal schema")
    tensor = np.zeros(
        (event_channels + 2, events.height, events.width),
        dtype=np.float32,
    )
    tensor[:event_channels] = voxel
    duration_s = max((events.t_end_us - events.t_start_us) * 1e-6, 1e-6)
    tensor[event_channels] = np.log1p(events.num_events)
    tensor[event_channels + 1] = np.log1p(events.num_events / duration_s)
    return torch.from_numpy(tensor)


def base_compatible_voxel_chunks(
    chunks: Iterable[dict[str, np.ndarray]],
    *,
    sequence_id: str,
    start_us: int,
    end_us: int,
    width: int,
    height: int,
    bins: int,
) -> torch.Tensor:
    """Voxelize an iterable of raw event chunks with bounded temporary memory."""

    return base_compatible_voxel_windows_chunks(
        chunks,
        windows=((start_us, end_us),),
        sequence_id=sequence_id,
        width=width,
        height=height,
        bins=bins,
    )[0]


def base_compatible_voxel_windows_chunks(
    chunks: Iterable[dict[str, np.ndarray]],
    *,
    windows: Sequence[tuple[int, int]],
    sequence_id: str,
    width: int,
    height: int,
    bins: int,
) -> torch.Tensor:
    """Voxelize several disjoint or overlapping windows from one chunk stream.

    The raw event stream is read once over the enclosing interval while each
    requested window gets an independent accumulator and event count. This
    preserves per-window normalization and avoids four separate HDF5 reads for
    a JEPA context/future tuple.
    """

    if not windows:
        raise ValueError("At least one event window is required.")
    if any(end_us <= start_us for start_us, end_us in windows):
        raise ValueError("All event windows require positive duration.")
    raw = [np.zeros((EVTTC_BASE_EVENT_CHANNELS, height, width), dtype=np.float32) for _ in windows]
    event_counts = [0] * len(windows)
    for chunk in chunks:
        timestamps = np.asarray(chunk["t"], dtype=np.int64)
        for index, (start_us, end_us) in enumerate(windows):
            selected = (timestamps >= start_us) & (timestamps < end_us)
            if not np.any(selected):
                continue
            selected_events = {key: np.asarray(values)[selected] for key, values in chunk.items()}
            event_batch = downsample_full_frame(
                selected_events,
                sequence_id=sequence_id,
                start_us=start_us,
                end_us=end_us,
                width=width,
                height=height,
            )
            raw[index] += encode_voxel_grid(event_batch, bins=bins, normalize=False)
            event_counts[index] += event_batch.num_events

    tensors: list[torch.Tensor] = []
    for (start_us, end_us), values, event_count in zip(
        windows,
        raw,
        event_counts,
        strict=True,
    ):
        tensor = np.zeros((EVTTC_BASE_INPUT_CHANNELS, height, width), dtype=np.float32)
        tensor[:EVTTC_BASE_EVENT_CHANNELS] = robust_normalize(values)
        duration_s = max((end_us - start_us) * 1e-6, 1e-6)
        tensor[10] = np.log1p(event_count)
        tensor[11] = np.log1p(event_count / duration_s)
        tensors.append(torch.from_numpy(tensor))
    return torch.stack(tensors)