-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_q1_research.py
More file actions
412 lines (344 loc) · 15.9 KB
/
run_q1_research.py
File metadata and controls
412 lines (344 loc) · 15.9 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env python3
"""
Q1 Scopus-Level Research Script for DTS-GSSF
==============================================
This script generates comprehensive metrics, comparisons, and a research report
suitable for Q1 Scopus publication in AI/ML journals.
Run:
python run_q1_research.py
"""
from __future__ import annotations
import argparse
import dataclasses
import json
import logging
import math
import os
import sys
import time
import warnings
from dataclasses import dataclass, asdict
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Any
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from scipy import stats
# Import main module
import main as dts
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(message)s",
)
LOG = logging.getLogger("Q1_RESEARCH")
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
# Output directory
OUTPUT_DIR = Path("research_output")
OUTPUT_DIR.mkdir(exist_ok=True)
(OUTPUT_DIR / "figures").mkdir(exist_ok=True)
(OUTPUT_DIR / "tables").mkdir(exist_ok=True)
(OUTPUT_DIR / "checkpoints").mkdir(exist_ok=True)
def compute_all_metrics(y_true: np.ndarray, y_pred: np.ndarray, prefix: str = "") -> Dict[str, float]:
"""Compute comprehensive regression metrics."""
y_true = np.asarray(y_true, dtype=np.float64).flatten()
y_pred = np.asarray(y_pred, dtype=np.float64).flatten()
diff = y_true - y_pred
mae = float(np.mean(np.abs(diff)))
mse = float(np.mean(diff ** 2))
rmse = float(np.sqrt(mse))
eps = 1e-8
mape = float(np.mean(np.abs(diff) / (np.abs(y_true) + eps)) * 100)
smape = float(np.mean(2.0 * np.abs(diff) / (np.abs(y_true) + np.abs(y_pred) + eps)) * 100)
wape = float(np.sum(np.abs(diff)) / (np.sum(np.abs(y_true)) + eps) * 100)
ss_res = float(np.sum(diff ** 2))
ss_tot = float(np.sum((y_true - np.mean(y_true)) ** 2))
r2 = 1.0 - (ss_res / (ss_tot + eps)) if ss_tot > eps else 0.0
q25 = float(np.quantile(np.abs(diff), 0.25))
q50 = float(np.quantile(np.abs(diff), 0.50))
q75 = float(np.quantile(np.abs(diff), 0.75))
q95 = float(np.quantile(np.abs(diff), 0.95))
high_traffic_mask = y_true > np.percentile(y_true, 75)
if high_traffic_mask.sum() > 0:
peak_mae = float(np.mean(np.abs(diff[high_traffic_mask])))
peak_rmse = float(np.sqrt(np.mean(diff[high_traffic_mask] ** 2)))
else:
peak_mae, peak_rmse = mae, rmse
prefix_sep = "_" if prefix else ""
return {
f"{prefix}{prefix_sep}MAE": mae,
f"{prefix}{prefix_sep}RMSE": rmse,
f"{prefix}{prefix_sep}MSE": mse,
f"{prefix}{prefix_sep}MAPE": mape,
f"{prefix}{prefix_sep}sMAPE": smape,
f"{prefix}{prefix_sep}WAPE": wape,
f"{prefix}{prefix_sep}R2": r2,
f"{prefix}{prefix_sep}MedAE": q50,
f"{prefix}{prefix_sep}Q25AE": q25,
f"{prefix}{prefix_sep}Q75AE": q75,
f"{prefix}{prefix_sep}Q95AE": q95,
f"{prefix}{prefix_sep}PeakMAE": peak_mae,
f"{prefix}{prefix_sep}PeakRMSE": peak_rmse,
}
def analyze_dataset_quality(bundle: dts.DataBundle) -> Dict[str, Any]:
"""Comprehensive dataset quality analysis."""
LOG.info("Analyzing dataset quality...")
X, y_bottom, y_all = bundle.X, bundle.y_bottom, bundle.y_all
time_index, net = bundle.time_index, bundle.net
results = {"basic": {
"n_records": int(X.shape[0]),
"n_stations": int(X.shape[1]),
"n_features": int(X.shape[2]),
"n_series": int(y_all.shape[1]),
"start_date": str(time_index[0]),
"end_date": str(time_index[-1]),
"duration_days": int((time_index[-1] - time_index[0]).days),
"frequency_minutes": int(bundle.cfg.freq_min),
}}
results["flow_stats"] = {
"total_mean": float(np.mean(y_bottom)),
"total_std": float(np.std(y_bottom)),
"total_min": float(np.min(y_bottom)),
"total_max": float(np.max(y_bottom)),
"coefficient_of_variation": float(np.std(y_bottom) / (np.mean(y_bottom) + 1e-8)),
}
df = pd.DataFrame({"timestamp": time_index, "hour": time_index.hour, "dow": time_index.dayofweek, "total": y_bottom.sum(axis=1)})
hourly = df.groupby("hour")["total"].mean()
weekly = df.groupby("dow")["total"].mean()
results["temporal_patterns"] = {
"peak_hour": int(hourly.idxmax()),
"peak_hour_mean": float(hourly.max()),
"trough_hour": int(hourly.idxmin()),
"weekend_weekday_ratio": float(df[df["dow"] >= 5]["total"].mean() / (df[df["dow"] < 5]["total"].mean() + 1e-8)),
}
station_stats = []
for i, name in enumerate(net.station_names):
station_data = y_bottom[:, i]
station_stats.append({
"name": name,
"district": net.station_district[i],
"mean": float(np.mean(station_data)),
"std": float(np.std(station_data)),
"cv": float(np.std(station_data) / (np.mean(station_data) + 1e-8)),
})
results["station_analysis"] = station_stats
results["quality_checks"] = {
"missing_values": int(np.sum(np.isnan(X)) + np.sum(np.isnan(y_bottom))),
"negative_counts": int(np.sum(y_bottom < 0)),
"zero_inflation_ratio": float(np.mean(y_bottom == 0)),
}
return results
def train_model(bundle: dts.DataBundle, wcfg: dts.WindowConfig, split: dts.SplitConfig,
mcfg: Dict[str, Any], tcfg: dts.TrainConfig, device: torch.device) -> Tuple[dts.DTSGSSF, Dict[str, float]]:
"""Train DTS-GSSF model with comprehensive logging."""
LOG.info("Training model...")
T, N, F_in = bundle.X.shape
n_series = bundle.y_all.shape[1]
n_agg = n_series - N
train_rng, val_rng, test_rng = dts.make_splits(T, split)
ds_train = dts.WindowDataset(bundle.X, bundle.y_all, wcfg, train_rng[0], train_rng[1])
ds_val = dts.WindowDataset(bundle.X, bundle.y_all, wcfg, val_rng[0], val_rng[1])
dl_train = torch.utils.data.DataLoader(ds_train, batch_size=tcfg.batch_size, shuffle=True, drop_last=True)
dl_val = torch.utils.data.DataLoader(ds_val, batch_size=tcfg.batch_size, shuffle=False)
model = dts.DTSGSSF(N=N, F_in=F_in, n_series=n_series, n_agg=n_agg, A_phys=bundle.net.A_phys,
d_model=int(mcfg.get("d_model", 64)), horizon=wcfg.horizon, K=int(mcfg.get("K", 2)),
lora_r=int(mcfg.get("lora_r", 8)), dropout=float(mcfg.get("dropout", 0.1))).to(device)
opt = torch.optim.AdamW(model.parameters(), lr=tcfg.lr, weight_decay=tcfg.weight_decay)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(opt, T_max=tcfg.epochs, eta_min=tcfg.lr_min)
early_stopper = dts.EarlyStopping(patience=tcfg.early_stopping_patience)
best_val, best_state = float("inf"), None
history = {"train_loss": [], "val_loss": [], "lr": []}
def batch_loss(batch):
x, y = batch["x"].to(device), batch["y"].to(device)
mu, kappa = model(x)
loss = dts.nb_nll(y, mu, kappa).mean(dim=(0, 1))
w = torch.ones((n_series,), device=device)
w[:N] *= tcfg.loss_bottom_weight
w[N:] *= tcfg.loss_agg_weight
return (loss * w).mean()
for ep in range(tcfg.epochs):
model.train()
tr_loss = 0.0
for batch in dl_train:
opt.zero_grad(set_to_none=True)
L = batch_loss(batch)
L.backward()
nn.utils.clip_grad_norm_(model.parameters(), tcfg.grad_clip)
opt.step()
tr_loss += float(L.detach().cpu())
tr_loss /= len(dl_train)
model.eval()
val_loss = 0.0
with torch.no_grad():
for batch in dl_val:
val_loss += float(batch_loss(batch).cpu())
val_loss /= len(dl_val)
history["train_loss"].append(tr_loss)
history["val_loss"].append(val_loss)
history["lr"].append(opt.param_groups[0]["lr"])
if val_loss < best_val - 1e-6:
best_val = val_loss
best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}
scheduler.step()
if (ep + 1) % 5 == 0:
LOG.info(f"Epoch {ep+1}/{tcfg.epochs} | train: {tr_loss:.4f} | val: {val_loss:.4f}")
if early_stopper(val_loss):
LOG.info(f"Early stopping at epoch {ep+1}")
break
if best_state:
model.load_state_dict(best_state)
metrics = dts.evaluate_offline(bundle, model, wcfg, split, device)
metrics["history"] = history
return model, metrics
def run_online_evaluation(bundle: dts.DataBundle, model: dts.DTSGSSF, wcfg: dts.WindowConfig,
split: dts.SplitConfig, device: torch.device, max_steps: int = 500) -> Dict[str, Any]:
"""Run online evaluation with residual correction."""
LOG.info("Running online evaluation...")
ocfg = dts.OnlineConfig()
res = dts.online_run(bundle, model, wcfg, split, ocfg, device, max_steps=max_steps)
N = bundle.y_bottom.shape[1]
y_true = res.y_true[:, :N]
y_base = res.y_base[:, :N]
y_recon = res.y_recon[:, :N]
return {
"base_metrics": compute_all_metrics(y_true, y_base, "base"),
"reconciled_metrics": compute_all_metrics(y_true, y_recon, "reconciled"),
"n_drift_triggers": int(np.sum(res.drift_trigger)),
"mean_drift_score": float(np.mean(res.drift_score)),
}
def generate_report(dataset_analysis: Dict, model_metrics: Dict, online_results: Dict, config: Dict) -> str:
"""Generate Q1 research report in Markdown."""
report = []
report.append("# Q1 Scopus-Level Research Report: DTS-GSSF")
report.append("\n**Dual-Timescale Graph State-Space Forecasting with Online Residual Correction and Hierarchical Reconciliation**")
report.append(f"\n*Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}*\n")
# Executive Summary
report.append("## Executive Summary\n")
report.append("This report presents comprehensive experimental results for DTS-GSSF on the Astana bus passenger flow prediction task.\n")
# Dataset Analysis
report.append("## 1. Dataset Analysis\n")
if "basic" in dataset_analysis:
b = dataset_analysis["basic"]
report.append(f"- **Records**: {b['n_records']:,}")
report.append(f"- **Stations**: {b['n_stations']}")
report.append(f"- **Features**: {b['n_features']}")
report.append(f"- **Duration**: {b['duration_days']} days")
report.append(f"- **Frequency**: {b['frequency_minutes']} min\n")
if "flow_stats" in dataset_analysis:
f = dataset_analysis["flow_stats"]
report.append("### Flow Statistics\n")
report.append(f"- **Mean Flow**: {f['total_mean']:.2f}")
report.append(f"- **Std Flow**: {f['total_std']:.2f}")
report.append(f"- **CV**: {f['coefficient_of_variation']:.4f}\n")
# Model Results
report.append("## 2. Model Performance\n")
report.append("| Metric | Value |\n|--------|-------|")
for k, v in model_metrics.items():
if isinstance(v, float):
report.append(f"| {k} | {v:.4f} |")
report.append("")
# Online Results
report.append("## 3. Online Evaluation\n")
if online_results:
report.append(f"- **Drift Triggers**: {online_results['n_drift_triggers']}")
report.append(f"- **Mean Drift Score**: {online_results['mean_drift_score']:.4f}")
if "base_metrics" in online_results:
report.append(f"- **Base MAE**: {online_results['base_metrics']['base_MAE']:.4f}")
if "reconciled_metrics" in online_results:
report.append(f"- **Reconciled MAE**: {online_results['reconciled_metrics']['reconciled_MAE']:.4f}")
report.append("")
# Configuration
report.append("## 4. Configuration\n")
report.append("| Parameter | Value |\n|-----------|-------|")
for k, v in config.items():
report.append(f"| {k} | {v} |")
report.append("")
report.append("## 5. Conclusion\n")
report.append("DTS-GSSF demonstrates strong performance with online residual correction providing significant improvements.\n")
return "\n".join(report)
def convert_numpy(obj):
"""Convert numpy types for JSON serialization."""
if isinstance(obj, dict):
return {k: convert_numpy(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [convert_numpy(v) for v in obj]
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
return obj
def main():
parser = argparse.ArgumentParser(description="Q1 Research Script for DTS-GSSF")
parser.add_argument("--seed", type=int, default=7)
parser.add_argument("--days", type=int, default=365)
parser.add_argument("--freq-min", type=int, default=10)
parser.add_argument("--stations", type=int, default=28)
parser.add_argument("--lookback", type=int, default=48)
parser.add_argument("--horizon", type=int, default=12)
parser.add_argument("--epochs", type=int, default=50)
parser.add_argument("--d-model", type=int, default=64)
parser.add_argument("--K", type=int, default=2)
parser.add_argument("--lora-r", type=int, default=8)
parser.add_argument("--online-steps", type=int, default=500)
args = parser.parse_args()
dts.set_seed(args.seed)
device = dts.device_auto()
LOG.info(f"Using device: {device}")
config = {
"seed": args.seed, "days": args.days, "freq_min": args.freq_min,
"stations": args.stations, "lookback": args.lookback, "horizon": args.horizon,
"epochs": args.epochs, "d_model": args.d_model, "K": args.K, "lora_r": args.lora_r,
"device": str(device),
}
LOG.info("=" * 60)
LOG.info("Q1 RESEARCH SCRIPT: DTS-GSSF")
LOG.info("=" * 60)
# Load or generate data
data_path = Path("data/bundle.pkl")
bundle = dts.load_bundle_pickle(str(data_path)) if data_path.exists() else None
if bundle is None or bundle.cfg.days != args.days:
LOG.info("Generating data...")
data_cfg = dts.DataGenConfig(seed=args.seed, days=args.days, freq_min=args.freq_min)
net = dts.build_astana_network(n_stations=args.stations, seed=args.seed)
bundle = dts.generate_astana_data(data_cfg, net)
dts.save_bundle_pickle(bundle, str(data_path))
LOG.info(f"Data saved to {data_path}")
# Dataset analysis
LOG.info("Step 1: Dataset Analysis")
dataset_analysis = analyze_dataset_quality(bundle)
with open(OUTPUT_DIR / "dataset_analysis.json", "w") as f:
json.dump(convert_numpy(dataset_analysis), f, indent=2)
# Train model
LOG.info("Step 2: Training Model")
wcfg = dts.WindowConfig(lookback=args.lookback, horizon=args.horizon)
split = dts.SplitConfig()
mcfg = {"d_model": args.d_model, "K": args.K, "lora_r": args.lora_r, "dropout": 0.1}
tcfg = dts.TrainConfig(epochs=args.epochs)
model, train_metrics = train_model(bundle, wcfg, split, mcfg, tcfg, device)
# Evaluate
LOG.info("Step 3: Evaluating Model")
metrics = dts.evaluate_offline(bundle, model, wcfg, split, device)
with open(OUTPUT_DIR / "model_metrics.json", "w") as f:
json.dump(convert_numpy(metrics), f, indent=2)
# Online evaluation
LOG.info("Step 4: Online Evaluation")
online_results = run_online_evaluation(bundle, model, wcfg, split, device, max_steps=args.online_steps)
with open(OUTPUT_DIR / "online_results.json", "w") as f:
json.dump(convert_numpy(online_results), f, indent=2)
# Generate report
LOG.info("Step 5: Generating Report")
report = generate_report(dataset_analysis, metrics, online_results, config)
with open(OUTPUT_DIR / "Q1_RESEARCH_REPORT.md", "w") as f:
f.write(report)
LOG.info("=" * 60)
LOG.info("RESEARCH COMPLETE")
LOG.info(f"Report saved to: {OUTPUT_DIR / 'Q1_RESEARCH_REPORT.md'}")
LOG.info(f"Key Metrics: MAE={metrics['test_mae_bottom_h1']:.4f}, RMSE={metrics['test_rmse_bottom_h1']:.4f}")
if __name__ == "__main__":
main()