-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_dataset.py
77 lines (63 loc) · 2.08 KB
/
3_dataset.py
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
import torch
import pandas as pd
from torch.utils.data import Dataset
import torch.nn.functional as F
from typing import List, Dict
class PassingIntentionDataset(Dataset):
X_COLS = [
"x",
"y",
"rel_dist",
"heading_converted",
"sin_rel_bearing",
"cos_rel_bearing",
]
Y_COLS = ["label"]
CLASSES_DICT = {"L": 0, "R": 1}
def __init__(
self,
parquet_path: str,
row_dim: int,
X_cols: List[str] = X_COLS,
y_cols: List[str] = Y_COLS,
classes_dict: Dict[int, str] = CLASSES_DICT, # binary classification by default
):
self.df = pd.read_parquet(parquet_path)
self.row_dim = row_dim
self.X_cols = X_cols
self.y_cols = y_cols
self.n_classes = len(classes_dict)
self.classes_dict = classes_dict
self.data_array = []
self.label_array = []
for _, df_by_ID in self.df.groupby("obj_index"):
X = torch.tensor(df_by_ID[self.X_cols].values.tolist())
X = F.pad(X, pad=(0, 0, 0, row_dim - X.shape[0]))
self.data_array.append(X)
y = [0.0] * len(self.classes_dict)
y[self.classes_dict[df_by_ID[self.y_cols].values[0].item()]] = 1.0
self.label_array.append(torch.tensor(y))
def __len__(self):
return len(self.label_array)
def __getitem__(self, idx):
X = self.data_array[idx]
y = self.label_array[idx]
return X, y
if __name__ == "__main__":
### Usage example
TRAIN_PARQUET_PATH = (
"./datasets/preprocessed_train_dataset.parquet"
)
TEST_PARQUET_PATH = (
"./datasets/preprocessed_test_dataset.parquet"
)
row_dim = max(
pd.read_parquet(TEST_PARQUET_PATH).groupby("obj_index").size().max(),
pd.read_parquet(TRAIN_PARQUET_PATH).groupby("obj_index").size().max(),
)
train_dataset = PassingIntentionDataset(
parquet_path=TRAIN_PARQUET_PATH, row_dim=row_dim
)
test_dataset = PassingIntentionDataset(
parquet_path=TEST_PARQUET_PATH, row_dim=row_dim
)