File size: 5,071 Bytes
1baf215 | 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 | """Leakage-safe historical inputs and horizon-specific normalized state deltas.
History ends at current state t. Action/exogenous row s describes transition
s -> s+1, so historical actions stop at t-1. Planned controls start at t.
"""
import numpy as np
import torch
from torch.utils.data import Dataset
from .schema import FIELD_INDEX, HORIZONS, MAX_STATE_FIELDS, NUM_FIELDS, ROLE_IDS, UNIT_IDS, role_fields
class WindowDataset(Dataset):
def __init__(self, episodes, normalization, history_steps=24, future_steps=8, samples=10000, seed=0):
self.episodes = list(episodes)
self.normalization = normalization
self.history_steps, self.future_steps = int(history_steps), int(future_steps)
if history_steps < 1 or future_steps < max(HORIZONS):
raise ValueError("History must be positive and future_steps must cover horizon 8")
available = []
for i, episode in enumerate(self.episodes):
available.extend((i, t) for t in range(history_steps - 1, len(episode.values) - future_steps))
if not available:
raise ValueError("No episodes long enough for requested window")
if samples is None:
self.indices = available
else:
if int(samples) <= 0:
raise ValueError("samples must be positive")
rng = np.random.default_rng(seed)
self.indices = [available[i] for i in rng.integers(0, len(available), int(samples))]
self.seed = seed
def __len__(self):
return len(self.indices)
def __getitem__(self, index):
episode_index, t = self.indices[index]
ep = self.episodes[episode_index]
h, k = self.history_steps, self.future_steps
history_values = ep.values[t - h + 1 : t + 1].copy()
history_mask = ep.observation_mask[t - h + 1 : t + 1].copy()
# Explicit transition arrays are authoritative for historical controls.
past_actions = (ROLE_IDS == 1) & ep.field_mask
past_exogenous = (ROLE_IDS == 2) & ep.field_mask
history_values[:-1, past_actions] = ep.actions[t - h + 1 : t, past_actions]
history_values[:-1, past_exogenous] = ep.exogenous[t - h + 1 : t, past_exogenous]
# Exclude controls and known forecast inputs applied AFTER the anchor state.
history_mask[-1, ROLE_IDS != 0] = False
history_values[-1, ROLE_IDS != 0] = 0
current_mask = history_mask[-1]
norm = self.normalization
history = norm.transform(history_values, history_mask)
future_actions = np.zeros((k, NUM_FIELDS))
future_exogenous = np.zeros((k, NUM_FIELDS))
action_slots, exogenous_slots = (ROLE_IDS == 1) & ep.field_mask, (ROLE_IDS == 2) & ep.field_mask
future_actions[:, action_slots] = norm.transform(ep.actions[t : t + k])[:, action_slots]
future_exogenous[:, exogenous_slots] = norm.transform(ep.exogenous[t : t + k])[:, exogenous_slots]
query_fields = np.zeros(MAX_STATE_FIELDS * len(HORIZONS), dtype=np.int64)
query_horizons = np.zeros_like(query_fields, dtype=float)
target = np.zeros_like(query_fields, dtype=float)
target_mask = np.zeros_like(query_fields, dtype=bool)
q = 0
for name in role_fields(ep.schema, "state"):
field = FIELD_INDEX[name]
for horizon in HORIZONS:
query_fields[q] = field
query_horizons[q] = ep.timestamps[t + horizon] - ep.timestamps[t]
target_mask[q] = ep.observation_mask[t, field] and ep.observation_mask[t + horizon, field]
if target_mask[q]:
target[q] = (ep.values[t + horizon, field] - ep.values[t, field]) / norm.scale[field]
q += 1
arrays = {
"history": history,
"history_mask": history_mask.astype(float),
"history_dt": ep.timestamps[t - h + 1 : t + 1] - ep.timestamps[t],
"current": history[-1],
"current_mask": current_mask.astype(float),
"future_actions": future_actions,
"future_exogenous": future_exogenous,
"future_dt": ep.timestamps[t + 1 : t + k + 1] - ep.timestamps[t],
"field_mask": ep.field_mask,
"field_ids": np.arange(NUM_FIELDS, dtype=np.int64),
"unit_ids": UNIT_IDS.copy(),
"role_ids": ROLE_IDS.copy(),
"query_fields": query_fields,
"query_horizons": query_horizons,
"target": target,
"target_mask": target_mask,
}
return {
name: torch.from_numpy(
np.asarray(
value,
dtype=(
np.int64
if name.endswith("ids") or name == "query_fields"
else bool
if name in ("field_mask", "target_mask")
else np.float32
),
).copy()
)
for name, value in arrays.items()
}
|