-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_generator.py
More file actions
1339 lines (1148 loc) · 46.7 KB
/
plot_generator.py
File metadata and controls
1339 lines (1148 loc) · 46.7 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ============================================================
# GLOBAL IMPORTS & STYLE
# ============================================================
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
import torch
sns.set_style("whitegrid")
mpl.rcParams.update({
"text.usetex": True,
"font.family": "serif",
"text.latex.preamble": r"\usepackage{amsmath}",
"axes.labelsize": 18,
"axes.titlesize": 18,
"xtick.labelsize": 16,
"ytick.labelsize": 16,
"legend.fontsize": 16
})
sns.set_palette("colorblind")
DT = 0.01
DATASETS = ["Sys_1", "Sys_2", "Sys_3"]
# ============================================================
# CONSISTENT COLOR MAP (used everywhere)
# ============================================================
COLORS = {
"GT": "#000000", # black
"PhyGRU": "#FF7F0E", # orange (matplotlib default orange)
"GRU": "#1f77b4", # blue (matplotlib default blue)
"GRU_obs": "#2ca02c", # green
"InitialGuess": "#7f7f7f" # gray for initial guess in identified plot
}
# ============================================================
# UTILS
# ============================================================
def ensure_dir(path):
os.makedirs(path, exist_ok=True)
def apply_smart_ylim(ax, arrays, force_abs=4):
if len(arrays) == 0:
return
y = np.concatenate([a.ravel() for a in arrays])
ymin, ymax = int(np.floor(y.min())), int(np.ceil(y.max()))
if abs(ymin) > force_abs or abs(ymax) > force_abs:
ax.set_ylim([-force_abs, force_abs])
else:
ax.set_ylim([ymin, ymax])
# ============================================================
# DATASET OVERVIEW (TI / TV)
# ============================================================
def plot_dataset_overview(folder, tag):
#fig_dir = os.path.join(folder, "figures")
fig_dir = "./figures"
ensure_dir(fig_dir)
fig, axes = plt.subplots(
2 * len(DATASETS), 1,
figsize=(13, 4.2 * len(DATASETS)),
sharex=True
)
row = 0
for name in DATASETS:
data = np.load(f"{folder}/results_npz/{name}_dataset.npz")
t = data["time"]
x_tr, x_va, x_te = data["x_train"], data["x_val"], data["x_test"]
u_tr, u_va, u_te = data["u_train"], data["u_val"], data["u_test"]
axx = axes[row]
axx.plot(t, x_tr, label="Train")
axx.plot(t, x_va, label="Val")
axx.plot(t, x_te, label="Test")
axx.set_ylabel(r"$x(t)$")
axx.set_title(rf"\textbf{{{name}}} — State")
axx.grid(True, alpha=0.3)
if row == 0:
axx.legend()
axu = axes[row + 1]
axu.plot(t, u_tr)
axu.plot(t, u_va)
axu.plot(t, u_te)
axu.set_ylabel(r"$u(t)$")
axu.set_title(rf"\textbf{{{name}}} — Input")
axu.grid(True, alpha=0.3)
row += 2
axes[-1].set_xlabel(r"$t\,[\mathrm{s}]$")
plt.tight_layout()
plt.savefig(os.path.join(fig_dir, f"{tag}_dataset_overview.pdf"), bbox_inches="tight")
plt.show()
# ============================================================
# PREDICTIONS (TI / TV / TV_STACK)
# ============================================================
def plot_predictions(folder, split, tag):
#fig_dir = os.path.join(folder, "figures")
fig_dir = "./figures"
ensure_dir(fig_dir)
fig, axes = plt.subplots(
len(DATASETS), 1,
figsize=(13, 4.2 * len(DATASETS)),
sharex=True
)
for ax, name in zip(axes, DATASETS):
data = np.load(f"{folder}/results_npz/{name}_predictions.npz")
gt = data["GRTH"] if split == "test" else data["GRTH_VAL"]
time = np.arange(len(gt)) * DT
# Collect keys by model type to control colors and alpha
gru_keys = [k for k in data.files if k.startswith("GRU_") and not k.startswith("GRU_obs") and ( (split=="test" and not k.endswith("_val")) or (split=="val" and k.endswith("_val")) )]
gru_obs_keys = [k for k in data.files if k.startswith("GRU_obs") and ( (split=="test" and not k.endswith("_val")) or (split=="val" and k.endswith("_val")) )]
phy_keys = [k for k in data.files if k.startswith("PhyGRU") and ( (split=="test" and not k.endswith("_val")) or (split=="val" and k.endswith("_val")) )]
curves = [gt]
# GRU_obs (green)
n_obs = len(gru_obs_keys)
if n_obs > 0:
alpha_member = 0.4 if n_obs > 1 else 0.6
for k in gru_obs_keys:
ax.plot(time, data[k], color=COLORS["GRU_obs"], alpha=alpha_member, label="_nolegend_")
curves.append(data[k])
# mean
mean_obs = np.mean([data[k] for k in gru_obs_keys], axis=0)
ax.plot(time, mean_obs, color=COLORS["GRU_obs"], lw=2, alpha=1.0, label=r"$\mathrm{GRU_{obs}}$")
curves.append(mean_obs)
# GRU (blue)
n_gru = len(gru_keys)
if n_gru > 0:
alpha_member = 0.4 if n_gru > 1 else 0.6
for k in gru_keys:
ax.plot(time, data[k], color=COLORS["GRU"], alpha=alpha_member, label="_nolegend_")
curves.append(data[k])
mean_gru = np.mean([data[k] for k in gru_keys], axis=0)
ax.plot(time, mean_gru, color=COLORS["GRU"], lw=2, alpha=1.0, label=r"$\mathrm{GRU}$")
curves.append(mean_gru)
# PhyGRU (orange)
n_phy = len(phy_keys)
if n_phy > 0:
alpha_member = 0.4 if n_phy > 1 else 0.6
for k in phy_keys:
ax.plot(time, data[k], color=COLORS["PhyGRU"], alpha=alpha_member, label="_nolegend_")
curves.append(data[k])
mean_phy = np.mean([data[k] for k in phy_keys], axis=0)
ax.plot(time, mean_phy, color=COLORS["PhyGRU"], lw=2, alpha=1.0, label=r"$\mathrm{PhyGRU}$")
curves.append(mean_phy)
# Ground truth (black dashed)
ax.plot(time, gt, color=COLORS["GT"], linestyle="--", lw=2, label=r"Ground Truth")
apply_smart_ylim(ax, curves)
ax.set_title(rf"\textbf{{{name}}} — {split.upper()}")
ax.set_ylabel(r"$x(t)$")
ax.grid(True, alpha=0.3)
if ax is axes[0]:
ax.legend()
axes[-1].set_xlabel(r"$t\,[\mathrm{s}]$")
plt.tight_layout()
plt.savefig(os.path.join(fig_dir, f"{tag}_{split}.pdf"), bbox_inches="tight")
plt.show()
# ============================================================
# UPDATE GATE DIAGNOSTICS (ALL LATENTS)
# ============================================================
def run_update_gate_analysis_TI(
dataset_name="Sys_3",
latent_dim=2,
T=6000,
dt=0.01,
base_dir="TI",
save_fig=True
):
"""
Update-gate analysis for PhyGRU using TI checkpoints.
Computes physics candidate, all latent candidates, and gate activation.
Fully self-contained and ready to paste.
"""
import os
import math
import torch
import torch.nn as nn
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
torch.set_grad_enabled(False)
# ------------------ COLORS ------------------
COLORS = {
"GT": "black",
"PhyGRU": "#1f77b4", # blue
"GRU": "#ff7f0e", # orange
"GRU_obs": "#2ca02c", # green (will cycle for multiple latents)
}
# ------------------ PATHS ------------------
ckpt_dir = os.path.join(base_dir, "checkpoints")
#fig_dir = os.path.join(base_dir, "figures")
fig_dir = "./figures"
os.makedirs(fig_dir, exist_ok=True)
ckpt_path = os.path.join(
ckpt_dir, f"{dataset_name}_PhyGRU_l{latent_dim}_best.pt"
)
time = np.arange(T) * dt
# ============================================================
# DATASET GENERATORS
# ============================================================
def generate_data_1(u_fn):
x, xd = 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xdd = (u - 0.5 * xd - 0.2 * x)
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
def generate_data_3(u_fn):
x, xd, xdd = 0.0, 0.0, 0.0
xs, us = [], []
for t in range(T):
u = u_fn(t)
xddd = u - 3.0 * xdd - 2.0 * math.tanh(xd) - 0.1 * math.tanh(x)
xdd += dt * xddd
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
if dataset_name == "Sys_1":
x_test, u_test = generate_data_1(lambda t: math.tanh(
(0.5 - 0.00005 * t) * (
(0.30 - 0.002 * t) * math.sin((0.00050 + 0.0000005 * t) * t)
+ (0.15 + 0.001 * t) * math.sin((0.000004 - 0.000001 * t) * t)
)
))
else: # Sys_3
x_test, u_test = generate_data_3(lambda t: math.tanh(
(0.5 - 0.00005 * t) * (
(0.30 - 0.002 * t) * math.sin((0.00050 + 0.0000005 * t) * t)
+ (0.15 + 0.001 * t) * math.sin((0.000004 - 0.000001 * t) * t)
)
))
x_test = x_test.unsqueeze(0)
u_test = u_test.unsqueeze(0)
x_test /= torch.max(torch.abs(x_test))
u_test /= torch.max(torch.abs(u_test))
# ============================================================
# PHYSICS LAW
# ============================================================
class MassSpringDamperLaw(nn.Module):
def __init__(self):
super().__init__()
self.a = nn.Parameter(torch.tensor(0.5))
self.b = nn.Parameter(torch.tensor(0.6))
self.c = nn.Parameter(torch.tensor(0.7))
def forward(self, state, u):
x, xd = state[:, 0], state[:, 1]
u = u.squeeze()
xdd = (u - self.b * xd - self.c * x) / (self.a + 1e-12)
return torch.stack([xd, xdd], dim=1)
# ============================================================
# PHYGRU
# ============================================================
class PhyGRUCell(nn.Module):
def __init__(self, state_dim, input_dim, physics_law, latent_dim):
super().__init__()
total = state_dim + latent_dim
self.state_dim = state_dim
self.latent_dim = latent_dim
self.physics_law = physics_law
self.latent_dyn = nn.Linear(total + input_dim, latent_dim)
self.z_gate = nn.Sequential(nn.Linear(total + input_dim, total), nn.Sigmoid())
def forward(self, state, u):
phys_dot = self.physics_law(state[:, :self.state_dim], u)
phys_next = state[:, :self.state_dim] + dt * phys_dot
latent = state[:, self.state_dim:]
latent_dot = self.latent_dyn(torch.cat([state, u], dim=1))
latent_next = latent + dt * latent_dot
candidate = torch.cat([phys_next, latent_next], dim=1)
z = self.z_gate(torch.cat([state, u], dim=1))
state_next = z * candidate + (1.0 - z) * state
return state_next, phys_next, latent_next, candidate, z
class PhyGRU(nn.Module):
def __init__(self, physics_law, state_dim, input_dim, latent_dim):
super().__init__()
self.cell = PhyGRUCell(state_dim, input_dim, physics_law, latent_dim)
# ============================================================
# LOAD MODEL
# ============================================================
model = PhyGRU(MassSpringDamperLaw(), state_dim=2, input_dim=1, latent_dim=latent_dim)
model.load_state_dict(torch.load(ckpt_path, map_location="cpu"))
model.eval()
# ============================================================
# INFERENCE + LOGGING
# ============================================================
state = torch.zeros(1, 2 + latent_dim)
y_pred = []
candidate_phys = []
candidate_latents = [[] for _ in range(latent_dim)]
gate_x = []
for t in range(T):
u_t = u_test[:, t]
state, phys, latent, candidate, z = model.cell(state, u_t)
y_pred.append(state[0, 0].item())
candidate_phys.append(candidate[0, 0].item())
# log all latent candidates
for i in range(latent_dim):
candidate_latents[i].append(candidate[0, 2 + i].item())
gate_x.append(z[0, 0].item())
y_pred = np.array(y_pred)
candidate_phys = np.array(candidate_phys)
candidate_latents = [np.array(c) for c in candidate_latents]
gate_x = np.array(gate_x)
# ============================================================
# PLOTS (GT vs pred, physics + all latents, gate)
# ============================================================
fig, axes = plt.subplots(3, 1, figsize=(13, 10), sharex=True)
# Ground truth vs prediction
axes[0].plot(time, x_test[0, :, 0], linestyle="--", color=COLORS["GT"], lw=2, label="Ground Truth")
axes[0].plot(time, y_pred, color=COLORS["PhyGRU"], lw=2, label="PhyGRU")
axes[0].set_ylabel(r"$x(t)$")
#axes[0].set_title(rf"\textbf{{{dataset_name}}} --- Prediction")
axes[0].set_title(rf"\textbf{{{dataset_name}}}")
axes[0].legend()
axes[0].grid(True, alpha=0.3)
# Candidates: physics + all latent candidates
axes[1].plot(time, candidate_phys, label="Physics Candidate", color=COLORS["PhyGRU"])
for i, cl in enumerate(candidate_latents):
axes[1].plot(time, cl, label=f"Latent {i+1} Candidate", color=plt.cm.Set2(i % 8))
axes[1].set_ylabel(r"$\tilde{\mathbf{h}}(t)$")
#axes[1].set_title(rf"\textbf{{{dataset_name}}} --- Candidates (physics + latents)")
axes[1].legend()
axes[1].grid(True, alpha=0.3)
# Gate
axes[2].plot(time, gate_x, color=COLORS["GRU"], label=r"Gate $z_x$")
axes[2].set_ylabel(r"$z(t)$")
axes[2].set_xlabel(r"$t\,[\mathrm{s}]$")
axes[2].set_ylim(0, 1)
axes[2].grid(True, alpha=0.3)
#axes[2].legend()
plt.tight_layout()
if save_fig:
plt.savefig(os.path.join(fig_dir, f"TI_update_gate_{dataset_name}.pdf"), bbox_inches="tight")
plt.show()
return {
"time": time,
"y_pred": y_pred,
"candidate_phys": candidate_phys,
"candidate_latents": candidate_latents,
"gate_x": gate_x,
}
# ============================================================
# IDENTIFIED MODELS PLOT (CONSISTENT STYLE)
# ============================================================
def plot_identified_phygru_models(
dataset_name="Sys_1",
results_dir="results_npz",
dt=0.01,
save_fig=True,
fig_dir="figures"
):
"""
Plot comparison between:
- Ground Truth trajectory
- Initial Guess physics
- Identified PhyGRU physics parameters (latent models)
LaTeX-styled, publication-ready and consistent with other figures.
"""
import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
# ------------------ STYLE ------------------
plt.rcParams.update({
"text.usetex": True,
"font.family": "serif",
"font.size": 16,
"axes.labelsize": 18,
"axes.titlesize": 18,
"legend.fontsize": 14,
"xtick.labelsize": 14,
"ytick.labelsize": 14,
})
sns.set_palette("colorblind")
#sns.set_style("whitegrid")
os.makedirs(fig_dir, exist_ok=True)
# ------------------ PARAMETERS ------------------
GT = [1.0, 0.5, 0.2] # Ground Truth (a, b, c)
IG = [0.5, 0.6, 0.7] # Initial Guess
identified = {
"l0": [0.631, 0.915, 0.826],
"l1": [0.382, 0.735, 0.800],
"l2": [0.658, 0.698, 0.841],
"l3": [0.436, 0.832, 0.858],
}
# ------------------ MSD SIM ------------------
def msd_simulate(a, b, c, u_seq):
x, xd = 0.0, 0.0
xs = []
for u in u_seq:
xdd = (u - b * xd - c * x) / (a + 1e-12)
xd += dt * xdd
x += dt * xd
xs.append(x)
return np.array(xs)
# ------------------ LOAD DATA ------------------
data = np.load(os.path.join(results_dir, f"{dataset_name}_predictions.npz"))
u_test = data["U"]
gt_test = data["GRTH"]
time = np.arange(len(gt_test)) * dt
# Normalize input (training-consistent)
u_abs_max = np.max(np.abs(u_test))
if u_abs_max == 0:
u_abs_max = 1.0
u_test_norm = u_test / u_abs_max
# ------------------ SIMULATIONS ------------------
sim_gt = msd_simulate(*GT, u_test_norm)
sim_ig = msd_simulate(*IG, u_test_norm)
sim_ids = {k: msd_simulate(*v, u_test_norm) for k, v in identified.items()}
# ------------------ PLOT ------------------
fig, ax = plt.subplots(figsize=(14, 6))
# Ground Truth (black dashed)
ax.plot(time, gt_test, "k--", lw=2.5, label=r"Ground Truth")
# Initial Guess (gray)
COLORS = {"InitialGuess": "#7f7f7f"} # keep consistent with other figures
ax.plot(time, sim_ig, "--", lw=2.5, color=COLORS["InitialGuess"], label=r"Initial Guess")
# Identified latent models (Set2 palette)
colors = sns.color_palette("Set2", len(sim_ids))
for i, (k, xsim) in enumerate(sim_ids.items()):
ax.plot(time, xsim, lw=2.0, color=colors[i], label=rf"Identified {k}")
ax.set_xlabel(r"$t\,[\mathrm{s}]$")
ax.set_ylabel(r"$x(t)$")
#ax.set_title(r"\textbf{{{dataset_name}}} --- Identified PhyGRU Physics Parameters")
#ax.legend()
ax.grid(True, alpha=0.3)
plt.tight_layout()
if save_fig:
plt.savefig(os.path.join(fig_dir, f"{dataset_name}_identified_phygru.pdf"), bbox_inches="tight")
plt.show()
def plot_TI_TV_datasets_overview(
T=6000,
dt=0.01,
mode="TI", # "TI" or "TV"
save_fig=True,
fig_dir="figures"
):
"""
Plot normalized Train / Validation / Test trajectories
for TI or TV systems (Sys_1, Sys_2, Sys_3) in LaTeX style.
"""
import os
import math
import torch
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_palette("colorblind")
os.makedirs(fig_dir, exist_ok=True)
t = np.arange(T) * dt
# ============================================================
# DATASET GENERATORS
# ============================================================
if mode == "TI":
def generate_data_1(u_fn):
x, xd = 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xdd = (u - 0.5 * xd - 0.2 * x)
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
def generate_data_2(u_fn):
x, xd = 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xdd = (u - 0.5 * xd - 0.2 * x + math.tanh(x * xd))
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
def generate_data_3(u_fn):
x, xd, xdd = 0.0, 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xddd = u - 3 * xdd - 2 * math.tanh(xd) - 0.1 * math.tanh(x)
xdd += dt * xddd
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
elif mode == "TV":
def generate_data_1(u_fn):
x, xd = 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xdd = (u - (0.5 + 0.00009 * t_) * xd - (0.2 + 0.0000001 * t_**2) * x)
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
def generate_data_2(u_fn):
x, xd = 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xdd = (
u
- (0.5 + 0.00009 * t_) * xd
- (0.2 + 0.0000001 * t_**2) * x
+ (1.0 + 0.00009 * t_) * math.tanh(x * xd)
)
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
def generate_data_3(u_fn):
x, xd, xdd = 0.0, 0.0, 0.0
xs, us = [], []
for t_ in range(T):
u = u_fn(t_)
xddd = (
u
- (3.0 - 0.0002 * t_) * xdd
- (2.0 + 0.0009 * t_) * math.tanh(xd)
- (0.1 + 0.0000003 * t_**2) * math.tanh(x)
)
xdd += dt * xddd
xd += dt * xdd
x += dt * xd
xs.append([x])
us.append([u])
return torch.tensor(xs), torch.tensor(us)
else:
raise ValueError("mode must be 'TI' or 'TV'")
systems = [
("Sys_1", generate_data_1),
("Sys_2", generate_data_2),
("Sys_3", generate_data_3),
]
# ============================================================
# INPUT FUNCTIONS
# ============================================================
u_train_fn = lambda t: math.tanh(
(0.3 - 0.00005 * t)
* (
(0.25 - 0.001 * t) * math.sin((0.00007 + 0.0000001 * t) * t)
+ (0.10 + 0.001 * t) * math.sin((0.000001 - 0.000001 * t) * t)
)
)
u_val_fn = lambda t: math.tanh(
(0.4 - 0.00005 * t)
* (
(0.35 - 0.003 * t) * math.sin((0.00009 + 0.0000002 * t) * t)
+ (0.15 + 0.001 * t) * math.sin((0.000003 - 0.000001 * t) * t)
)
)
u_test_fn = lambda t: math.tanh(
(0.5 - 0.00005 * t)
* (
(0.30 - 0.002 * t) * math.sin((0.00050 + 0.0000005 * t) * t)
+ (0.15 + 0.001 * t) * math.sin((0.000004 - 0.000001 * t) * t)
)
)
# ============================================================
# PLOTS
# ============================================================
fig, axes = plt.subplots(
nrows=2 * len(systems),
ncols=1,
figsize=(13, 4.2 * len(systems)),
sharex=True
)
row = 0
for name, data_fn in systems:
x_train, u_train = data_fn(u_train_fn)
x_val, u_val = data_fn(u_val_fn)
x_test, u_test = data_fn(u_test_fn)
x_abs_max = max(
float(torch.max(torch.abs(x_train))),
float(torch.max(torch.abs(x_val))),
float(torch.max(torch.abs(x_test)))
) or 1.0
u_abs_max = max(
float(torch.max(torch.abs(u_train))),
float(torch.max(torch.abs(u_val))),
float(torch.max(torch.abs(u_test)))
) or 1.0
x_train /= x_abs_max
x_val /= x_abs_max
x_test /= x_abs_max
u_train /= u_abs_max
u_val /= u_abs_max
u_test /= u_abs_max
ax_x = axes[row]
ax_x.plot(t, x_train.squeeze(), label=r"Train")
ax_x.plot(t, x_val.squeeze(), label=r"Validation")
ax_x.plot(t, x_test.squeeze(), label=r"Test")
ax_x.set_ylabel(r"$x(t)$")
ax_x.set_title(rf"\textbf{{{name}}} --- State")
ax_x.grid(True, alpha=0.3)
if row == 0:
ax_x.legend(loc="upper right")
ax_u = axes[row + 1]
ax_u.plot(t, u_train.squeeze())
ax_u.plot(t, u_val.squeeze())
ax_u.plot(t, u_test.squeeze())
ax_u.set_ylabel(r"$u(t)$")
ax_u.set_title(rf"\textbf{{{name}}} --- Input")
ax_u.grid(True, alpha=0.3)
row += 2
axes[-1].set_xlabel(r"$t\,[\mathrm{s}]$")
plt.tight_layout()
if save_fig:
plt.savefig(
os.path.join(fig_dir, f"{mode}_datasets_overview.pdf"),
bbox_inches="tight"
)
plt.show()
def compare_incremental_stability_phygru_vs_gru(
dataset_name="Sys_1",
latent_dim=1,
T=6000,
dt=0.01,
N_PERTURB=100,
perturb_range=3.0,
base_dir="TI",
fig_dir="./figures",
save_fig=True,
device="cpu",
gru_hidden=32,
colors=None
):
"""
Compare incremental stability of a trained PhyGRU vs a standard GRU (hidden=gru_hidden).
- Runs N_PERTURB perturbed initial conditions for each model (same input sequence).
- Plots: (top) all perturbed trajectories + references + GT; (bottom) distances to reference (log-scale) and mean.
- Attempts to load GRU checkpoint flexibly; if incompatible, GRU comparison is skipped.
- Returns a dict with arrays for later inspection.
"""
import os
import math
import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt
torch.set_grad_enabled(False)
dev = torch.device(device)
if colors is None:
# fallback to global COLORS if present in caller's namespace
try:
colors = globals()["COLORS"]
except Exception:
colors = {
"GT": "#000000",
"PhyGRU": "#FF7F0E",
"GRU": "#1f77b4",
"GRU_obs": "#2ca02c",
"InitialGuess": "#7f7f7f"
}
os.makedirs(fig_dir, exist_ok=True)
# ------------------- input / GT generators (Sys_1 used as GT) -------------------
def generate_input_seq(T):
u = []
for t in range(T):
val = math.tanh(
(0.5 - 0.00005 * t) * (
(0.30 - 0.002 * t) * math.sin((0.00050 + 0.0000005 * t) * t)
+ (0.15 + 0.001 * t) * math.sin((0.000004 - 0.000001 * t) * t)
)
)
u.append([val])
u = torch.tensor(u, dtype=torch.float32).unsqueeze(0) # (1,T,1)
u /= torch.max(torch.abs(u)) + 1e-12
return u
def generate_sys1_trajectory(T, dt):
x, xd = 0.0, 0.0
xs = []
for t in range(T):
u = math.tanh(
(0.5 - 0.00005 * t) * (
(0.30 - 0.002 * t) * math.sin((0.00050 + 0.0000005 * t) * t)
+ (0.15 + 0.001 * t) * math.sin((0.000004 - 0.000001 * t) * t)
)
)
xdd = (u - 0.5 * xd - 0.2 * x)
xd += dt * xdd
x += dt * xd
xs.append(x)
xs = np.array(xs)
xs /= np.max(np.abs(xs)) + 1e-12
return xs
u_seq = generate_input_seq(T) # (1,T,1)
time = np.arange(T) * dt
gt_traj = generate_sys1_trajectory(T, dt) # normalized GT
# ------------------- PhyGRU model definition & loading -------------------
class MassSpringDamperLaw(nn.Module):
def __init__(self):
super().__init__()
self.a = nn.Parameter(torch.tensor(0.5))
self.b = nn.Parameter(torch.tensor(0.6))
self.c = nn.Parameter(torch.tensor(0.7))
def forward(self, state, u):
x, xd = state[:, 0], state[:, 1]
u = u[:, 0]
xdd = (u - self.b * xd - self.c * x) / (self.a + 1e-12)
return torch.stack([xd, xdd], dim=1)
class PhyGRUCell(nn.Module):
def __init__(self, state_dim, input_dim, physics_law, latent_dim):
super().__init__()
total = state_dim + latent_dim
self.state_dim = state_dim
self.latent_dim = latent_dim
self.physics_law = physics_law
self.latent_dyn = nn.Linear(total + input_dim, latent_dim)
self.z_gate = nn.Sequential(nn.Linear(total + input_dim, total), nn.Sigmoid())
def forward(self, state, u):
phys_dot = self.physics_law(state[:, :self.state_dim], u)
phys_next = state[:, :self.state_dim] + dt * phys_dot
latent = state[:, self.state_dim:]
latent_dot = self.latent_dyn(torch.cat([state, u], dim=1))
latent_next = latent + dt * latent_dot
candidate = torch.cat([phys_next, latent_next], dim=1)
z = self.z_gate(torch.cat([state, u], dim=1))
state_next = z * candidate + (1.0 - z) * state
return state_next, phys_next, latent_next, candidate, z
class PhyGRU(nn.Module):
def __init__(self, physics_law, state_dim, input_dim, latent_dim):
super().__init__()
self.cell = PhyGRUCell(state_dim, input_dim, physics_law, latent_dim)
phyckpt = os.path.join(base_dir, "checkpoints", f"{dataset_name}_PhyGRU_l{latent_dim}_best.pt")
if not os.path.exists(phyckpt):
raise FileNotFoundError(f"[compare_incremental_stability] PhyGRU checkpoint not found at {phyckpt}")
phy_model = PhyGRU(MassSpringDamperLaw(), state_dim=2, input_dim=1, latent_dim=latent_dim)
phy_model.load_state_dict(torch.load(phyckpt, map_location=dev))
phy_model.to(dev)
phy_model.eval()
# ------------------- Std GRU flexible wrapper & loading attempts -------------------
class StdGRUModelFC(nn.Module):
def __init__(self, input_dim=1, hidden_dim=32, n_layers=1):
super().__init__()
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.gru = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, 1)
def forward_step(self, h_prev, u_t):
# h_prev: (n_layers, batch, hidden)
u_t = u_t.unsqueeze(1) # (batch,1,input_dim)
out, h_next = self.gru(u_t, h_prev)
y = self.fc(out[:,0,:])
return h_next, y
class StdGRUModelReadout(nn.Module):
def __init__(self, input_dim=1, hidden_dim=32, n_layers=1):
super().__init__()
self.hidden_dim = hidden_dim
self.n_layers = n_layers
self.gru = nn.GRU(input_dim, hidden_dim, n_layers, batch_first=True)
self.readout = nn.Linear(hidden_dim, 1)
def forward_step(self, h_prev, u_t):
u_t = u_t.unsqueeze(1)
out, h_next = self.gru(u_t, h_prev)
y = self.readout(out[:,0,:])
return h_next, y
gru_ckpt = os.path.join(base_dir, "checkpoints", f"{dataset_name}_GRU_h{gru_hidden}_best.pt")
gru_loaded = False
gru_model = None
if os.path.exists(gru_ckpt):
sd = torch.load(gru_ckpt, map_location=dev)
# try FC wrapper first
try:
temp = StdGRUModelFC(input_dim=1, hidden_dim=gru_hidden)
temp.load_state_dict(sd)
gru_model = temp.to(dev)
gru_loaded = True
except Exception:
# try readout wrapper
try:
temp = StdGRUModelReadout(input_dim=1, hidden_dim=gru_hidden)
temp.load_state_dict(sd)
gru_model = temp.to(dev)
gru_loaded = True
except Exception:
# try partial (intersection) load into FC
try:
temp = StdGRUModelFC(input_dim=1, hidden_dim=gru_hidden)
common = {k: v for k, v in sd.items() if k in temp.state_dict().keys()}
if len(common) > 0:
temp.load_state_dict(common, strict=False)
gru_model = temp.to(dev)
gru_loaded = True
except Exception:
gru_loaded = False
if not gru_loaded:
print(f"[compare_incremental_stability] [WARN] No compatible GRU checkpoint loaded from {gru_ckpt}. GRU comparison will be skipped.")
else:
gru_model.eval()
print(f"[compare_incremental_stability] Loaded GRU model from {gru_ckpt}")
# ------------------- simulation helpers -------------------
def simulate_phygru(state_init, u_seq):
state = state_init.clone().to(dev)
traj = []
for t in range(T):
u_t = u_seq[:, t, :].to(dev) # (1,1)
state, _, _, _, _ = phy_model.cell(state, u_t)
traj.append(state[0, 0].item())
traj = np.array(traj)
# normalize to max abs (same as GT normalization)
if np.max(np.abs(traj)) > 0:
traj = traj / (np.max(np.abs(traj)) + 1e-12)
return traj
def simulate_gru(h0, u_seq):
h = h0.clone().to(dev)
traj = []
for t in range(T):
u_t = u_seq[:, t, :].to(dev)
h, y = gru_model.forward_step(h, u_t)
traj.append(float(y[0, 0]))
traj = np.array(traj)
if np.max(np.abs(traj)) > 0:
traj = traj / (np.max(np.abs(traj)) + 1e-12)
return traj
# ------------------- compute references -------------------
ref_state_phy = torch.zeros(1, 2 + latent_dim, dtype=torch.float32, device=dev)
ref_traj_phy = simulate_phygru(ref_state_phy, u_seq)
if gru_loaded:
# prepare reference hidden with shape (n_layers, batch, hidden)
if hasattr(gru_model, "n_layers"):
n_layers = gru_model.n_layers
else:
n_layers = 1
h0_ref = torch.zeros(n_layers, 1, gru_model.hidden_dim, dtype=torch.float32, device=dev)
ref_traj_gru = simulate_gru(h0_ref, u_seq)
else:
ref_traj_gru = None
# ------------------- run perturbation experiments -------------------
all_trajs_phy = []
all_dists_phy = []
for i in range(N_PERTURB):
pert = (torch.rand(1, 2 + latent_dim, dtype=torch.float32) - 0.5) * 2 * perturb_range
traj = simulate_phygru(pert, u_seq)
all_trajs_phy.append(traj)
all_dists_phy.append(np.abs(traj - ref_traj_phy))
all_trajs_phy = np.array(all_trajs_phy)
all_dists_phy = np.array(all_dists_phy)
all_trajs_gru = None
all_dists_gru = None
if gru_loaded:
all_trajs_gru = []
all_dists_gru = []
for i in range(N_PERTURB):
pert_h = (torch.rand(n_layers, 1, gru_model.hidden_dim, dtype=torch.float32) - 0.5) * 2 * perturb_range
traj = simulate_gru(pert_h, u_seq)
all_trajs_gru.append(traj)
all_dists_gru.append(np.abs(traj - ref_traj_gru))
all_trajs_gru = np.array(all_trajs_gru)
all_dists_gru = np.array(all_dists_gru)
# ------------------- plotting -------------------
fig, axes = plt.subplots(2, 1, figsize=(14, 9), sharex=True)
# Top: trajectories
ax = axes[0]
# PhyGRU perturbed
for traj in all_trajs_phy:
ax.plot(time, traj, color=colors["PhyGRU"], alpha=0.25, lw=0.8)
ax.plot(time, ref_traj_phy, color=colors["PhyGRU"], lw=2.0, label=r"PhyGRU reference")
# GRU perturbed + ref
if gru_loaded:
for traj in all_trajs_gru:
ax.plot(time, traj, color=colors["GRU"], alpha=0.25, lw=0.8)