-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy path_lsf_dataset.py
218 lines (195 loc) · 7.58 KB
/
_lsf_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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import os
import numpy as np
import pandas as pd
from uni2ts.common.env import env
class LSFDataset:
def __init__(
self,
dataset_name: str,
mode: str = "S",
split: str = "test",
):
self.dataset_name = dataset_name
self.mode = mode
self.split = split
if dataset_name in ["ETTh1", "ETTh2"]:
self._load_etth()
elif dataset_name in ["ETTm1", "ETTm2"]:
self._load_ettm()
elif dataset_name == "METR_LA":
self._load_metr_la()
elif dataset_name == "solar":
self._load_solar()
elif dataset_name == "walmart":
self._load_walmart()
elif dataset_name == "electricity":
self._load_custom("electricity/electricity.csv", "h")
elif dataset_name == "weather":
self._load_custom("weather/weather.csv", "10T")
else:
raise ValueError(f"Unknown dataset name: {dataset_name}")
if mode == "S":
self.target_dim = 1
self.past_feat_dynamic_real_dim = 0
elif mode == "M":
self.target_dim = self.data.shape[-1]
self.past_feat_dynamic_real_dim = 0
elif mode == "MS":
self.target_dim = 1
self.past_feat_dynamic_real_dim = self.data.shape[-1] - 1
else:
raise ValueError(f"Unknown mode: {mode}")
def __iter__(self):
if self.mode == "S":
for i in range(self.data.shape[-1]):
yield {
"target": self.data[:, i],
"start": self.start,
}
elif self.mode == "M":
yield {
"target": self.data.transpose(1, 0),
"start": self.start,
}
elif self.mode == "MS":
for i in range(self.data.shape[-1]):
yield {
"target": self.data[:, i],
"past_feat_dynamic_real": np.concatenate(
[self.data[:, :i], self.data[:, i + 1 :]], axis=1
).transpose(1, 0),
"start": self.start,
}
def scale(self, data, start, end):
train = data[start:end]
mean = train.mean(axis=0)
std = train.std(axis=0)
return (data - mean) / std
def _load_etth(self):
df = pd.read_csv(
os.path.join(env.LSF_PATH, f"ETT-small/{self.dataset_name}.csv")
)
train_length = 8640
val_length = 2880
test_length = 2880
data = self.scale(df[df.columns[1:]], 0, train_length).to_numpy()
if self.split == "train":
self.data = data[:train_length]
self.length = train_length
elif self.split == "val":
self.data = data[: train_length + val_length]
self.length = val_length
elif self.split == "test":
self.data = data[: train_length + val_length + test_length]
self.length = test_length
self.start = pd.to_datetime(df[["date"]].iloc[0].item())
self.freq = "h"
def _load_ettm(self):
df = pd.read_csv(
os.path.join(env.LSF_PATH, f"ETT-small/{self.dataset_name}.csv")
)
train_length = 34560
val_length = 11520
test_length = 11520
data = self.scale(df[df.columns[1:]], 0, train_length).to_numpy()
if self.split == "train":
self.data = data[:train_length]
self.length = train_length
elif self.split == "val":
self.data = data[: train_length + val_length]
self.length = val_length
elif self.split == "test":
self.data = data[: train_length + val_length + test_length]
self.length = test_length
self.start = pd.to_datetime(df[["date"]].iloc[0].item())
self.freq = "15T"
def _load_solar(self):
df = pd.read_csv(os.path.join(env.LSF_PATH, "Solar/solar_AL.txt"), header=None)
data = df.to_numpy().reshape(8760, 6, 137).sum(1)
train_length = int(len(data) * 0.7)
val_length = int(len(data) * 0.1)
test_length = int(len(data) * 0.2)
data = self.scale(data, 0, train_length).to_numpy()
if self.split == "train":
self.data = data[:train_length]
self.length = train_length
elif self.split == "val":
self.data = data[: train_length + val_length]
self.length = val_length
elif self.split == "test":
self.data = data[: train_length + val_length + test_length]
self.length = test_length
self.start = pd.to_datetime("2006-01-01")
self.freq = "h"
def _load_metr_la(self):
df = pd.read_csv(os.path.join(env.LSF_PATH, "METR_LA/METR_LA.dyna"))
data = []
for id in df.entity_id.unique():
id_df = df[df.entity_id == id]
data.append(id_df.traffic_speed.to_numpy())
data = np.stack(data, 1)
train_length = int(len(data) * 0.7)
val_length = int(len(data) * 0.1)
test_length = int(len(data) * 0.2)
data = self.scale(data, 0, train_length).to_numpy()
if self.split == "train":
self.data = data[:train_length]
self.length = train_length
elif self.split == "val":
self.data = data[: train_length + val_length]
self.length = val_length
elif self.split == "test":
self.data = data[: train_length + val_length + test_length]
self.length = test_length
self.start = pd.to_datetime("2012-03-01")
self.freq = "5T"
def _load_walmart(self):
df = pd.read_csv(
os.path.join(
env.LSF_PATH, "walmart-recruiting-store-sales-forecasting/train.csv"
)
)
data = []
for id, row in df[["Store", "Dept"]].drop_duplicates().iterrows():
row_df = df.query(f"Store == {row.Store} and Dept == {row.Dept}")
if len(row_df) != 143:
continue
data.append(row_df.Weekly_Sales.to_numpy())
data = np.stack(data, 1)
train_length = 143 - 28 - 14
val_length = 14
test_length = 28
data = self.scale(data, 0, train_length)
if self.split == "train":
self.data = data[:train_length]
self.length = train_length
elif self.split == "val":
self.data = data[: train_length + val_length]
self.length = val_length
elif self.split == "test":
self.data = data[: train_length + val_length + test_length]
self.length = test_length
self.start = pd.to_datetime("2010-02-05")
self.freq = "W"
def _load_custom(self, data_path: str, freq: str):
df = pd.read_csv(os.path.join(env.LSF_PATH, data_path))
cols = list(df.columns)
cols.remove("OT")
cols.remove("date")
df = df[["date"] + cols + ["OT"]]
data = df[df.columns[1:]]
train_length = int(len(data) * 0.7)
val_length = int(len(data) * 0.1)
test_length = int(len(data) * 0.2)
data = self.scale(data, 0, train_length).to_numpy()
if self.split == "train":
self.data = data[:train_length]
self.length = train_length
elif self.split == "val":
self.data = data[: train_length + val_length]
self.length = val_length
elif self.split == "test":
self.data = data[: train_length + val_length + test_length]
self.length = test_length
self.start = pd.to_datetime(df[["date"]].iloc[0].item())
self.freq = freq