Allow saving of datasets

This commit is contained in:
2025-11-21 09:24:14 +01:00
parent 056bc94b16
commit b2ce697abe
+46
View File
@@ -102,7 +102,53 @@ class BaseDataset(Dataset):
df.loc[:, "last_timestamp"] = last_row["timestamp"]
return df
def save_entire_dataset(self, filepath):
"""Utility to save the entire dataset to a single pth file."""
X_feat_list = []
X_time_list = []
Y_out_list = []
X_context_list = []
for i in range(len(self)):
X_f, X_t, Y, X_c = self[i]
X_feat_list.append(X_f.cpu())
X_time_list.append(X_t.cpu())
Y_out_list.append(Y.cpu())
if X_c is not None:
X_context_list.append(X_c.cpu())
data_dict = {
"X_feat": torch.stack(X_feat_list),
"X_time": torch.stack(X_time_list),
"Y_out": torch.stack(Y_out_list),
}
if len(X_context_list) > 0:
data_dict["X_context"] = torch.stack(X_context_list)
torch.save(data_dict, filepath)
class SaveDataset(Dataset):
def __init__(self, data_dict, device="cpu"):
super().__init__()
self.X_feat = data_dict["X_feat"].to(device)
self.X_time = data_dict["X_time"].to(device)
self.Y_out = data_dict["Y_out"].to(device)
self.device = device
if "X_context" in data_dict:
self.X_context = data_dict["X_context"].to(device)
else:
self.X_context = None
def __len__(self):
return self.X_feat.shape[0]
def __getitem__(self, idx):
X_f = self.X_feat[idx]
X_t = self.X_time[idx]
Y = self.Y_out[idx]
if self.X_context is not None:
X_c = self.X_context[idx]
else:
X_c = None
return X_f, X_t, Y, X_c
class EvenlySpacedDataset(BaseDataset):
def __init__(