Repository navigation
Expand file tree
/
Copy pathdata_loader.py
More file actions
198 lines (164 loc) · 6.44 KB
/
Copy pathdata_loader.py
File metadata and controls
198 lines (164 loc) · 6.44 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
from __future__ import annotations
from dataclasses import dataclass
from typing import Literal
import numpy as np
import pandas as pd
import torch
from datasets import DatasetDict, get_dataset_config_names, load_dataset
from huggingface_hub import hf_hub_download
from torch.utils.data import DataLoader, Dataset as TorchDataset
from preprocessing import (
HF_DATASET_ID,
LABELS_FILENAME,
MAX_FEATURE_DIM,
ModelInterface,
PreprocessConfig,
TelemanomFeatureScaler,
build_channel_windows,
build_model_interface,
chronological_train_val_split,
class_distribution,
hf_split_to_array,
pad_feature_dim,
parse_anomaly_sequences,
)
SplitName = Literal["train", "validation", "test"]
@dataclass
class TelemanomPipelineResult:
x_train: np.ndarray
x_val: np.ndarray
x_test: np.ndarray
y_train: np.ndarray
y_val: np.ndarray
y_test: np.ndarray
model_interface: ModelInterface
config: PreprocessConfig
channel_ids: list[str]
label_distribution: pd.DataFrame
def load_hf_labels(dataset_id: str = HF_DATASET_ID) -> pd.DataFrame:
path = hf_hub_download(dataset_id, LABELS_FILENAME, repo_type="dataset")
labels = pd.read_csv(path)
labels["anomaly_sequences"] = labels["anomaly_sequences"].apply(parse_anomaly_sequences)
return labels
def list_hf_channels(dataset_id: str = HF_DATASET_ID) -> list[str]:
return sorted(get_dataset_config_names(dataset_id))
def load_hf_channel(channel_id: str, dataset_id: str = HF_DATASET_ID) -> DatasetDict:
return load_dataset(dataset_id, name=channel_id)
def channel_to_arrays(channel: DatasetDict) -> tuple[np.ndarray, np.ndarray]:
train_arr = hf_split_to_array(channel["train"].to_pandas())
test_arr = hf_split_to_array(channel["test"].to_pandas())
return train_arr, test_arr
class TelemanomSequenceDataset(TorchDataset):
def __init__(
self,
sequences: np.ndarray,
labels: np.ndarray,
dtype: torch.dtype = torch.float32,
) -> None:
if len(sequences) != len(labels):
raise ValueError("sequences and labels must have the same length")
self.sequences = sequences
self.labels = labels
self.dtype = dtype
def __len__(self) -> int:
return len(self.labels)
def __getitem__(self, index: int) -> tuple[torch.Tensor, torch.Tensor]:
x = torch.as_tensor(self.sequences[index], dtype=self.dtype)
y = torch.as_tensor(self.labels[index], dtype=torch.long)
return x, y
def create_dataloaders(
x_train: np.ndarray,
y_train: np.ndarray,
x_val: np.ndarray,
y_val: np.ndarray,
x_test: np.ndarray,
y_test: np.ndarray,
batch_size: int = 64,
shuffle_train: bool = True,
num_workers: int = 0,
) -> dict[SplitName, DataLoader]:
loaders: dict[SplitName, DataLoader] = {}
for name, x_split, y_split, shuffle in [
("train", x_train, y_train, shuffle_train),
("validation", x_val, y_val, False),
("test", x_test, y_test, False),
]:
loaders[name] = DataLoader(
TelemanomSequenceDataset(x_split, y_split),
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
)
return loaders
def build_telemanom_pipeline(
channel_ids: list[str] | None = None,
config: PreprocessConfig | None = None,
dataset_id: str = HF_DATASET_ID,
max_channels: int | None = None,
) -> TelemanomPipelineResult:
config = config or PreprocessConfig()
labels_df = load_hf_labels(dataset_id)
channels = channel_ids or list_hf_channels(dataset_id)
if max_channels is not None:
channels = channels[:max_channels]
train_arrays: list[np.ndarray] = []
channel_meta: list[tuple[np.ndarray, np.ndarray, list[list[int]] | None]] = []
for channel_id in channels:
train_arr, test_arr = channel_to_arrays(load_hf_channel(channel_id, dataset_id))
train_arrays.append(train_arr)
row = labels_df.loc[labels_df["chan_id"] == channel_id]
sequences = row.iloc[0]["anomaly_sequences"] if not row.empty else None
channel_meta.append((train_arr, test_arr, sequences))
train_arrays = [pad_feature_dim(a, MAX_FEATURE_DIM) for a in train_arrays]
channel_meta = [
(pad_feature_dim(tr, MAX_FEATURE_DIM), pad_feature_dim(te, MAX_FEATURE_DIM), seq)
for tr, te, seq in channel_meta
]
scaler = TelemanomFeatureScaler(config.per_feature_normalize).fit(train_arrays)
train_parts: list[np.ndarray] = []
val_parts: list[np.ndarray] = []
test_parts: list[np.ndarray] = []
test_label_parts: list[np.ndarray] = []
for train_arr, test_arr, sequences in channel_meta:
train_w, test_w, y_test = build_channel_windows(
train_arr, test_arr, sequences, config, scaler
)
if len(train_w):
tr, va = chronological_train_val_split(train_w, config.val_size)
train_parts.append(tr)
val_parts.append(va)
if len(test_w):
test_parts.append(test_w)
test_label_parts.append(y_test)
if not train_parts:
raise RuntimeError("No training windows created — check channels or window size.")
x_train = np.concatenate(train_parts, axis=0)
x_val = np.concatenate(val_parts, axis=0) if val_parts else np.empty((0, *x_train.shape[1:]))
x_test = np.concatenate(test_parts, axis=0) if test_parts else np.empty((0, *x_train.shape[1:]))
y_train = np.zeros(len(x_train), dtype=np.int64)
y_val = np.zeros(len(x_val), dtype=np.int64)
y_test = (
np.concatenate(test_label_parts, axis=0)
if test_label_parts
else np.empty(0, dtype=np.int64)
)
return TelemanomPipelineResult(
x_train=x_train,
x_val=x_val,
x_test=x_test,
y_train=y_train,
y_val=y_val,
y_test=y_test,
model_interface=build_model_interface(config, scaler, MAX_FEATURE_DIM),
config=config,
channel_ids=channels,
label_distribution=class_distribution(y_test) if len(y_test) else class_distribution(y_train),
)
def load_channel_for_analysis(
channel_id: str,
dataset_id: str = HF_DATASET_ID,
) -> tuple[np.ndarray, np.ndarray, pd.Series | None]:
labels_df = load_hf_labels(dataset_id)
train_arr, test_arr = channel_to_arrays(load_hf_channel(channel_id, dataset_id))
row = labels_df.loc[labels_df["chan_id"] == channel_id]
return train_arr, test_arr, row.iloc[0] if not row.empty else None