Upload prepare_denmark_data.py with huggingface_hub
Browse files- prepare_denmark_data.py +95 -0
prepare_denmark_data.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Generate Denmark_data .mat files from the weather-denmark CSV
|
| 3 |
+
to match the format expected by the EQL-Wind-Speed-Forecasting repo.
|
| 4 |
+
"""
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from scipy.io import savemat
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
def main():
|
| 11 |
+
csv_path = '/tmp/weather-denmark/weather-denmark.csv'
|
| 12 |
+
df = pd.read_csv(csv_path)
|
| 13 |
+
df['DateTime'] = pd.to_datetime(df['DateTime'])
|
| 14 |
+
|
| 15 |
+
cities = ['Aalborg', 'Aarhus', 'Esbjerg', 'Odense', 'Roskilde']
|
| 16 |
+
features = ['Temp', 'Pressure', 'WindSpeed', 'WindDir']
|
| 17 |
+
target_feature = 'WindSpeed'
|
| 18 |
+
n_lags = 4
|
| 19 |
+
steps_ahead = 6
|
| 20 |
+
train_ratio = 0.9
|
| 21 |
+
|
| 22 |
+
df_hourly = []
|
| 23 |
+
for city in cities:
|
| 24 |
+
city_df = df[df['City'] == city].copy()
|
| 25 |
+
city_df = city_df.set_index('DateTime').sort_index()
|
| 26 |
+
city_hourly = city_df[features].resample('h').mean()
|
| 27 |
+
city_hourly = city_hourly.ffill().bfill()
|
| 28 |
+
city_hourly['City'] = city
|
| 29 |
+
df_hourly.append(city_hourly)
|
| 30 |
+
|
| 31 |
+
merged = pd.concat(df_hourly, axis=1, keys=cities)
|
| 32 |
+
merged = merged.dropna()
|
| 33 |
+
print(f"Total hourly samples after merge: {len(merged)}")
|
| 34 |
+
|
| 35 |
+
data_array = np.zeros((len(merged), len(cities), len(features)))
|
| 36 |
+
for i, city in enumerate(cities):
|
| 37 |
+
for j, feat in enumerate(features):
|
| 38 |
+
data_array[:, i, j] = merged[(city, feat)].values
|
| 39 |
+
|
| 40 |
+
valid_len = len(data_array) - steps_ahead - (n_lags - 1)
|
| 41 |
+
X = np.zeros((valid_len, len(cities), n_lags, len(features)))
|
| 42 |
+
Y = np.zeros((valid_len, len(cities)))
|
| 43 |
+
|
| 44 |
+
for t in range(valid_len):
|
| 45 |
+
start_idx = t + n_lags - 1
|
| 46 |
+
for lag in range(n_lags):
|
| 47 |
+
X[t, :, lag, :] = data_array[start_idx - lag, :, :]
|
| 48 |
+
Y[t, :] = data_array[start_idx + steps_ahead, :, features.index(target_feature)]
|
| 49 |
+
|
| 50 |
+
n_train = int(valid_len * train_ratio)
|
| 51 |
+
Xtr = X[:n_train]
|
| 52 |
+
Xtest = X[n_train:]
|
| 53 |
+
Ytr = Y[:n_train]
|
| 54 |
+
Ytest = Y[n_train:]
|
| 55 |
+
|
| 56 |
+
x_min = np.zeros((len(cities), len(features)))
|
| 57 |
+
x_max = np.zeros((len(cities), len(features)))
|
| 58 |
+
for i in range(len(cities)):
|
| 59 |
+
for j in range(len(features)):
|
| 60 |
+
vals = Xtr[:, i, :, j].flatten()
|
| 61 |
+
x_min[i, j] = vals.min()
|
| 62 |
+
x_max[i, j] = vals.max()
|
| 63 |
+
rng = x_max[i, j] - x_min[i, j]
|
| 64 |
+
if rng < 1e-8:
|
| 65 |
+
rng = 1.0
|
| 66 |
+
Xtr[:, i, :, j] = (Xtr[:, i, :, j] - x_min[i, j]) / rng
|
| 67 |
+
Xtest[:, i, :, j] = (Xtest[:, i, :, j] - x_min[i, j]) / rng
|
| 68 |
+
|
| 69 |
+
y_min_tr = np.zeros((1, len(cities)))
|
| 70 |
+
y_max_tr = np.zeros((1, len(cities)))
|
| 71 |
+
for i in range(len(cities)):
|
| 72 |
+
y_min_tr[0, i] = Ytr[:, i].min()
|
| 73 |
+
y_max_tr[0, i] = Ytr[:, i].max()
|
| 74 |
+
rng = y_max_tr[0, i] - y_min_tr[0, i]
|
| 75 |
+
if rng < 1e-8:
|
| 76 |
+
rng = 1.0
|
| 77 |
+
Ytr[:, i] = (Ytr[:, i] - y_min_tr[0, i]) / rng
|
| 78 |
+
Ytest[:, i] = (Ytest[:, i] - y_min_tr[0, i]) / rng
|
| 79 |
+
|
| 80 |
+
os.makedirs('Denmark_data/wind_speed', exist_ok=True)
|
| 81 |
+
os.makedirs('Denmark_data/temp', exist_ok=True)
|
| 82 |
+
|
| 83 |
+
for feat_dir, label in [('wind_speed', 'wind_speed'), ('temp', 'temp')]:
|
| 84 |
+
savemat(f'Denmark_data/{feat_dir}/step1.mat', {
|
| 85 |
+
'Xtr': Xtr, 'Xtest': Xtest, 'Ytr': Ytr, 'Ytest': Ytest,
|
| 86 |
+
'y_min_tr': y_min_tr, 'y_max_tr': y_max_tr,
|
| 87 |
+
}, do_compression=True)
|
| 88 |
+
|
| 89 |
+
np.savez('Denmark_data/scaling_params.npz',
|
| 90 |
+
x_min=x_min, x_max=x_max, y_min_tr=y_min_tr, y_max_tr=y_max_tr,
|
| 91 |
+
cities=cities, features=features)
|
| 92 |
+
print(f"Saved Denmark_data. Xtr:{Xtr.shape} Ytr:{Ytr.shape}")
|
| 93 |
+
|
| 94 |
+
if __name__ == '__main__':
|
| 95 |
+
main()
|