|
import numpy as np |
|
import torch |
|
import torch.nn as nn |
|
from torch.utils.data import Dataset |
|
from typing import Union, List, Tuple, Dict |
|
|
|
|
|
|
|
class NRELComstock(Dataset): |
|
""" |
|
torch Dataset class to load in data from numpy array |
|
""" |
|
|
|
def __init__( |
|
self, |
|
data_array: np.ndarray, |
|
num_bldg: int = 12, |
|
lookback: int = 12, |
|
lookahead: int = 4, |
|
normalize: bool = True, |
|
dtype: torch.dtype = torch.float32, |
|
mean: np.ndarray = None, |
|
std: np.ndarray = None, |
|
transformer: bool = True |
|
): |
|
super(NRELComstock, self).__init__() |
|
|
|
if data_array.shape[0] < num_bldg: |
|
raise ValueError('More buildings than present in file!') |
|
else: |
|
self.data = data_array[:num_bldg,:,[0]] |
|
self.num_clients = num_bldg |
|
|
|
|
|
self.lookback, self.lookahead = lookback, lookahead |
|
|
|
|
|
stacked = self.data.reshape(self.data.shape[0]*self.data.shape[1],self.data.shape[2]) |
|
if (mean is None) or (std is None): |
|
self.mean = stacked.mean(axis=0,keepdims=True) |
|
self.std = stacked.std(axis=0,keepdims=True) |
|
else: |
|
self.mean = mean |
|
self.std = std |
|
|
|
|
|
|
|
|
|
|
|
|
|
self.ndata = (self.data-self.mean)/self.std |
|
|
|
|
|
len_per_client = self.data.shape[1] - lookback - lookahead + 1 |
|
self.total_len = len_per_client * self.num_clients |
|
self.splits = np.array_split(np.arange(self.total_len),self.num_clients) |
|
|
|
|
|
self.normalize = normalize |
|
self.dtype = dtype |
|
|
|
|
|
if not self.normalize: |
|
self.mean = np.zeros_like(self.mean) |
|
self.std = np.ones_like(self.std) |
|
|
|
def _client_and_idx(self, idx): |
|
|
|
part_size = self.total_len // self.num_clients |
|
part_index = idx // part_size |
|
relative_position = idx % part_size |
|
|
|
return relative_position, part_index |
|
|
|
def __len__(self): |
|
|
|
return self.total_len |
|
|
|
def __getitem__(self, idx): |
|
|
|
tidx, cidx = self._client_and_idx(idx) |
|
|
|
if self.normalize: |
|
x = self.ndata[cidx,tidx:tidx+self.lookback,0] |
|
y = self.ndata[cidx,tidx+self.lookback:tidx+self.lookback+self.lookahead,0] |
|
else: |
|
x = self.data[cidx,tidx:tidx+self.lookback,0] |
|
y = self.data[cidx,tidx+self.lookback:tidx+self.lookback+self.lookahead,0] |
|
|
|
x, y = torch.tensor(x,dtype=self.dtype), torch.tensor(y,dtype=self.dtype) |
|
|
|
return x,y |
|
|
|
|
|
def get_data_and_generate_train_val_test_sets( |
|
data_array: np.ndarray, |
|
split_ratios: Union[List,Tuple], |
|
dataset_kwargs: Tuple |
|
) -> Tuple[torch.utils.data.Dataset, torch.utils.data.Dataset, torch.utils.data.Dataset, np.ndarray, np.ndarray]: |
|
""" |
|
function to create three torch Dataset objects, each corresponding to train, validation, and test sets |
|
""" |
|
|
|
assert len(split_ratios) == 3, "The split list must contain three elements." |
|
assert all(isinstance(i, (int, float)) for i in split_ratios), "List contains non-numeric elements" |
|
assert sum(split_ratios) <= 1, "Ratios must not sum upto more than 1." |
|
|
|
cum_splits = np.cumsum(split_ratios) |
|
|
|
train = data_array[:,:int(cum_splits[0]*data_array.shape[1]),:] |
|
val = data_array[:,int(cum_splits[0]*data_array.shape[1]):int(cum_splits[1]*data_array.shape[1]),:] |
|
test = data_array[:,int(cum_splits[1]*data_array.shape[1]):,:] |
|
|
|
if 0 in train.shape: |
|
train_set = None |
|
raise ValueError("Train set is empty. Possibly empty data matrix or 0 ratio for train set has been input.") |
|
else: |
|
train_set = NRELComstock( |
|
data_array = train, |
|
**dataset_kwargs |
|
) |
|
mean, std = train_set.mean, train_set.std |
|
if 0 in val.shape: |
|
val_set = None |
|
else: |
|
val_set = NRELComstock( |
|
data_array = val, |
|
mean = mean, |
|
std = std, |
|
**dataset_kwargs |
|
) |
|
if 0 in test.shape: |
|
test_set = None |
|
else: |
|
test_set = NRELComstock( |
|
data_array = test, |
|
mean = mean, |
|
std = std, |
|
**dataset_kwargs |
|
) |
|
|
|
|
|
return train_set, val_set, test_set, mean, std |