Shourya Bose
commited on
Commit
·
f8976a0
1
Parent(s):
251a42c
upload custom dataset
Browse files- custom_dataset.py +137 -0
custom_dataset.py
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
from torch.utils.data import Dataset
|
5 |
+
from typing import Union, List, Tuple, Dict
|
6 |
+
|
7 |
+
class NRELComstock(Dataset):
|
8 |
+
|
9 |
+
def __init__(
|
10 |
+
self,
|
11 |
+
data_array: np.ndarray,
|
12 |
+
num_bldg: int = 12,
|
13 |
+
lookback: int = 12,
|
14 |
+
lookahead:int = 4,
|
15 |
+
normalize: bool = True,
|
16 |
+
dtype: torch.dtype = torch.float32,
|
17 |
+
mean: np.ndarray = None,
|
18 |
+
std: np.ndarray = None,
|
19 |
+
transformer: bool = True
|
20 |
+
):
|
21 |
+
|
22 |
+
if data_array.shape[0] < num_bldg:
|
23 |
+
raise ValueError('More buildings than present in file!')
|
24 |
+
else:
|
25 |
+
self.data = data_array[:num_bldg,:,:]
|
26 |
+
self.num_clients = num_bldg
|
27 |
+
|
28 |
+
# lookback and lookahead
|
29 |
+
self.lookback, self.lookahead = lookback, lookahead
|
30 |
+
|
31 |
+
# Calculate statistics
|
32 |
+
stacked = self.data.reshape(self.data.shape[0]*self.data.shape[1],self.data.shape[2])
|
33 |
+
if (mean is None) or (std is None): # statistics are not provided. Generate it from data
|
34 |
+
self.mean = stacked.mean(axis=0,keepdims=True)
|
35 |
+
self.std = stacked.std(axis=0,keepdims=True)
|
36 |
+
else: # statistics are provided. Use the provided statistics
|
37 |
+
self.mean = mean
|
38 |
+
self.std = std
|
39 |
+
|
40 |
+
if transformer:
|
41 |
+
# TRANSFORMER SPECIFIC: do not normalize date and time
|
42 |
+
self.mean[0,1], self.std[0,1] = 0., 1.
|
43 |
+
self.mean[0,2], self.std[0,2] = 0., 1.
|
44 |
+
|
45 |
+
self.ndata = (self.data-self.mean)/self.std # normalized data
|
46 |
+
|
47 |
+
# disambiguating between clients
|
48 |
+
len_per_client = self.data.shape[1] - lookback - lookahead + 1
|
49 |
+
self.total_len = len_per_client * self.num_clients
|
50 |
+
self.splits = np.array_split(np.arange(self.total_len),self.num_clients)
|
51 |
+
|
52 |
+
# save whether to normalize, and the data type
|
53 |
+
self.normalize = normalize
|
54 |
+
self.dtype = dtype
|
55 |
+
|
56 |
+
# if normalization is disabled, return to default statistics
|
57 |
+
if not self.normalize:
|
58 |
+
self.mean = np.zeros_like(self.mean)
|
59 |
+
self.std = np.ones_like(self.std)
|
60 |
+
|
61 |
+
def _client_and_idx(self, idx):
|
62 |
+
|
63 |
+
part_size = self.total_len // self.num_clients
|
64 |
+
part_index = idx // part_size
|
65 |
+
relative_position = idx % part_size
|
66 |
+
|
67 |
+
return relative_position, part_index
|
68 |
+
|
69 |
+
def __len__(self):
|
70 |
+
|
71 |
+
return self.total_len
|
72 |
+
|
73 |
+
def __getitem__(self, idx):
|
74 |
+
|
75 |
+
tidx, cidx = self._client_and_idx(idx)
|
76 |
+
|
77 |
+
if self.normalize:
|
78 |
+
x = self.ndata[cidx,tidx:tidx+self.lookback,:]
|
79 |
+
y = self.ndata[cidx,tidx+self.lookback+self.lookahead-1,[0]]
|
80 |
+
else:
|
81 |
+
x = self.data[cidx,tidx:tidx+self.lookback,:]
|
82 |
+
y = self.data[cidx,tidx+self.lookback+self.lookahead-1,[0]]\
|
83 |
+
|
84 |
+
# Future time and weather indices
|
85 |
+
fut_time = self.data[cidx,tidx+self.lookback:tidx+self.lookback+self.lookahead,1:3]
|
86 |
+
|
87 |
+
x,y,fut_time,= torch.tensor(x,dtype=self.dtype), torch.tensor(y,dtype=self.dtype), torch.tensor(fut_time,dtype=self.dtype)
|
88 |
+
|
89 |
+
return x,y,fut_time
|
90 |
+
|
91 |
+
|
92 |
+
def get_data_and_generate_train_val_test_sets(
|
93 |
+
data_array: np.ndarray, # data matrix of shape (num_bldg,num_time_points,num_features). NOTE that features 2 and 3 are categorical features to embed time indices
|
94 |
+
split_ratios: Union[List,Tuple], # 3-element list containing the ratios of train-val-test
|
95 |
+
dataset_kwargs: Tuple # ONLY include num_bldg, lookback, lookahead, normalize, dtype, and transformer keys. See NRELComstock definition above for details.
|
96 |
+
):
|
97 |
+
|
98 |
+
assert len(split_ratios) == 3, "The split list must contain three elements."
|
99 |
+
assert all(isinstance(i, (int, float)) for i in split_ratios), "List contains non-numeric elements"
|
100 |
+
assert sum(split_ratios) <= 1, "Ratios must not sum upto more than 1."
|
101 |
+
|
102 |
+
cum_splits = np.cumsum(split_ratios)
|
103 |
+
|
104 |
+
train = data_array[:,:int(cum_splits[0]*data_array.shape[1]),:]
|
105 |
+
val = data_array[:,int(cum_splits[0]*data_array.shape[1]):int(cum_splits[1]*data_array.shape[1]),:]
|
106 |
+
test = data_array[:,int(cum_splits[1]*data_array.shape[1]):,:]
|
107 |
+
|
108 |
+
if 0 in train.shape:
|
109 |
+
train_set = None
|
110 |
+
raise ValueError("Train set is empty. Possibly empty data matrix or 0 ratio for train set has been input.")
|
111 |
+
else:
|
112 |
+
train_set = NRELComstock(
|
113 |
+
data_array = train,
|
114 |
+
**dataset_kwargs
|
115 |
+
)
|
116 |
+
mean, std = train_set.mean, train_set.std
|
117 |
+
if 0 in val.shape:
|
118 |
+
val_set = None
|
119 |
+
else:
|
120 |
+
val_set = NRELComstock(
|
121 |
+
data_array = val,
|
122 |
+
mean = mean,
|
123 |
+
std = std,
|
124 |
+
**dataset_kwargs
|
125 |
+
)
|
126 |
+
if 0 in test.shape:
|
127 |
+
test_set = None
|
128 |
+
else:
|
129 |
+
test_set = NRELComstock(
|
130 |
+
data_array = test,
|
131 |
+
mean = mean,
|
132 |
+
std = std,
|
133 |
+
**dataset_kwargs
|
134 |
+
)
|
135 |
+
|
136 |
+
# return the three datasets, as well as the statistics
|
137 |
+
return train_set, val_set, test_set, mean, std
|