-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuppersink_deeponet_uq_multisite.py
More file actions
executable file
·1154 lines (962 loc) · 43.3 KB
/
Copy pathuppersink_deeponet_uq_multisite.py
File metadata and controls
executable file
·1154 lines (962 loc) · 43.3 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
#author: alex sun
#==========This is for multi-site illustration=====================================
#rev date: 09282023, use this for exasheds final allhands meeting
#Note: need to install hydrostats and deap every time
#rev date: 11072023, update the code for new ensemble
#rev date: 11092023, fixed a bug in subsetting the ensemble output data (see getQarray())
#rev date: 11102023, split time calibration/testing
#Note: to detach container w/o stopping it, ctrl+P, ctrl+Q
#rev date: 01212024, add UQ
#rev date: removed gage 01434092 permanently
#rev date: 2/7/2024, add barplot
#rev date: this is the final version used for generating deeponet paper results
#=========================================================================================
import torch
import numpy as np
import modulus
from modulus.hydra import to_absolute_path, instantiate_arch, ModulusConfig, to_yaml
from modulus.solver import Solver
from modulus.domain import Domain
from modulus.models.fully_connected import FullyConnectedArch
from modulus.models.fourier_net import FourierNetArch
from modulus.models.deeponet import DeepONetArch
from modulus.domain.constraint.continuous import DeepONetConstraint
from modulus.domain.validator.discrete import GridValidator
from modulus.dataset.discrete import DictGridDataset
from modulus.domain.inferencer import PointwiseInferencer
from modulus.graph import Graph
from modulus.domain.constraint import Constraint
from modulus.key import Key
import pickle as pkl
import random
import pandas as pd
from einops import rearrange, repeat
import matplotlib.pyplot as plt
from sklearn.preprocessing import PowerTransformer, StandardScaler
from torch.utils.data import DataLoader
from tqdm import tqdm
import hydrostats as HydroStats
import time
import sys
from myutil import LpLoss, MyGridValidator, getEnsembleUSGSData,transformQ, inverseTransformQ,printParams, setLossFun
def getData(cfg, testid=None, mode='train'):
""" Data preparation for DeepONet
"""
from sklearn.model_selection import KFold
#this random seed determines the ats ensemble train/test split
random.seed(cfg.custom.ats_seed)
def getQarray(gageidset):
#get flow rates into Qarr, dimensions N_gage x N_ens x N_times
Qarr = None
for ix, gage_id in enumerate(gageidset):
print (gage_id)
Qsingle = []
for rz in rzList:
#get Q
stationDF = allDFDict[gage_id]
#11/09/2023 bug fix, added ensemble output subsetting!
stationDF = stationDF[(stationDF.index >= pd.to_datetime(startdate)) & (stationDF.index <= pd.to_datetime(enddate)) ]
#01/23/2024 drop bad values
stationDF = stationDF.dropna()
Q = stationDF[f'ens.{rz}'].to_numpy()
Qsingle.append(transformQ(Q/86400, imethod=1)) #convert to m3/s
Qsingle= np.stack(Qsingle)
if Qarr is None:
#dimensions: nGages, nEns, nTimes
Qarr = np.zeros((len(gageidset), Qsingle.shape[0], Qsingle.shape[1]))
Qarr[ix, :, :] = Qsingle
return Qarr
gageids = [
'01435000', '01434498', '01434176', '01434105', '01434025',
'0143402265', '01434021', '01434017', '01434013', '0143400680'
]
"""
#number of records at each site
#gageid, number of records
0 01435000 10258
1 01434498 9955
2 01434176 1115
3 01434105 1825
4 01434025 10258
5 0143402265 1176
6 01434021 8126
7 01434017 9955
8 01434013 1175
9 0143400680 8218
"""
#split into train/test
#select gages for training
#01232024, don't use 01434092, the ensemble runs are problematic!!!!
id_train = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
expno= 5
if expno==1:
id_ga_train = [0, 1, 4, 6, 7, 9]
id_ga_test = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
cal_startDate = '1993/10/01'
cal_endDate = '2000/09/30'
test_startDate= '1991/10/01'
test_endDate = '1993/09/30'
elif expno==2:
#exp1 [base case]
#for training deeponet
#for training GA
id_ga_train = [0, 1, 4, 6]
#for testing GA
id_ga_test = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
cal_startDate = '1993/10/01'
cal_endDate = '2000/09/30'
test_startDate= '1991/10/01'
test_endDate = '1993/09/30'
elif expno==3:
id_ga_train = [0, 7, 9]
id_ga_test = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
cal_startDate = '1993/10/01'
cal_endDate = '2000/09/30'
test_startDate= '1991/10/01'
test_endDate = '1993/09/30'
elif expno==4:
id_ga_train = [0, 1]
id_ga_test = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
cal_startDate = '1993/10/01'
cal_endDate = '2000/09/30'
test_startDate= '1991/10/01'
test_endDate = '1993/09/30'
elif expno==5:
id_ga_train = [0]
id_ga_test = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
cal_startDate = '1993/10/01'
cal_endDate = '2000/09/30'
test_startDate= '1991/10/01'
test_endDate = '1993/09/30'
#
#01222024, weights for GA loss function
#
all_gage_weights = [10, 5, 1, 1, 10, 1, 5, 5, 1, 5]
gage_weights = [all_gage_weights[id] for id in id_train]
#ensemble startdate '1990-10-3' and enddate = '2000-10-01'
startdate = cfg.custom.start_date
enddate = cfg.custom.end_date
#11102023, add calibration/test period definitions
pred_startDate= cfg.custom.pred_start_date
pred_endDate = cfg.custom.pred_end_date
#These are taken from D:\Princeton_HiRes\neversink\neversinkgageloc_utm18.shp
#https://www.latlong.net/lat-long-utm.html
#removed
gage_x = [534058.964434, 535267.218737, 537748.640447, 539611.662819, 541321.973647,
542994.032181,548468.171454, 538144.921077, 541032.131468, 545722.512904]
gage_y = [4637647.10367, 4641018.68265, 4645171.17167, 4647365.05997,4649355.43552,
4648846.28256, 4651189.53034,4641636.11094, 4643041.40612,4646253.69601]
#get distances relative to the most downstream gage
gage_x = np.array(gage_x) - gage_x[0]
gage_y = np.array(gage_y) - gage_y[0]
#Location normalization [0, 1]
gage_x = gage_x/np.max(gage_x)
gage_y = gage_y/np.max(gage_y)
#Load ensemble data [these are generated using readensemble.py in atsauto]
paramDF, allDFDict, allForcingDFDict, allUSGSData = pkl.load(open(to_absolute_path(cfg.custom.data_file), 'rb'))
rzList = paramDF['realization_id'].values.tolist()
nRZ = len(rzList)
if mode == 'train':
#gages for deeponet training
gage_ids_train = [gageids[i] for i in id_train]
gage_x_train = gage_x[id_train]
gage_y_train = gage_y[id_train]
nGages_train = len(gage_ids_train)
gage_ids_test = gage_ids_train
nGages_test = nGages_train
gage_x_test = gage_x_train
gage_y_test = gage_y_train
else:
#gages for ga training
gage_ids_train = [gageids[i] for i in id_ga_train]
nGages_train = len(gage_ids_train)
gage_x_train = gage_x[id_ga_train]
gage_y_train = gage_y[id_ga_train]
#gages for ga testing
gage_ids_test = [gageids[i] for i in id_ga_test]
nGages_test = len(gage_ids_test)
gage_x_test = gage_x[id_ga_test]
gage_y_test = gage_y[id_ga_test]
#theta shape is (596, 13), 13 parameters, 596 realizations
theta =np.stack([paramDF[paramDF['realization_id']==rz].iloc[:, 1:].to_numpy().squeeze() for rz in rzList])
#Split train/test for parameter domain
kf = KFold(n_splits=5, random_state=cfg.custom.ats_seed, shuffle=True)
for ifold, (train_indices, test_indices) in enumerate(kf.split(range(nRZ))):
if ifold == cfg.custom.fold_no:
train_indices = sorted(train_indices)
test_indices = np.array(list(sorted(set(range(nRZ)).difference(train_indices))))
print (len(train_indices)+len(test_indices))
break
#train_indices = np.array(sorted(np.random.choice(range(nRZ), int(0.8*nRZ), replace=False))) #account for 1-based index
#test_indices = np.array(list(sorted(set(range(nRZ)).difference(train_indices))))
#get one realization for testing
if not testid is None:
test_indices = [test_indices[testid]]
if mode == 'train':
#Form train/test for branch domain
Qarr = getQarray(gage_ids_train)
#separate train/test arrays for branchnet
Q_train = Qarr[:, train_indices,:]
Q_test = Qarr[:, test_indices, :]
else:
#ensemble for training GA
Qarr = getQarray(gage_ids_train)
Q_train = Qarr[:, train_indices, :]
#ensemble for testing GA on realizations not used in either surrogate modeling or GA calibration
#
Qarr = getQarray(gage_ids_test)
Q_test = Qarr[:, test_indices, :]
_, _, nT = Qarr.shape
#0210, get the median of predictions
Q_pretrain = np.nanmedian(Qarr, axis=1)
x_coord = np.arange(nT)/nT
del Qarr
timeSampling = 'all'
if timeSampling=='random':
#randomly sample coordinates
t_train_ind = sorted(np.random.choice(range(nT), 2000, replace=False))
remain_ind = sorted(set(range(nT)).difference(t_train_ind))
t_test_ind = sorted(np.random.choice(remain_ind, 500, replace=False))
elif timeSampling == 'all':
#split according to time>
#original ATS simulation time: from 1990/10/3 to 2000/10/01
full_daterng = pd.date_range(startdate, enddate, freq='1D')
#use calibration/testing period
#find index of calibration start/end
tstart = None
tend = None
t_test_start = None
t_test_end = None
for ix,item in enumerate(full_daterng):
if item == pd.to_datetime(cal_startDate):
tstart=ix
elif item == pd.to_datetime(cal_endDate):
tend = ix
elif item == pd.to_datetime(test_startDate):
t_test_start = ix
elif item == pd.to_datetime(test_endDate):
t_test_end = ix
if (tstart is None) or (tend is None):
raise Exception('calibration period not set right')
if (t_test_start is None) or (t_test_end is None):
raise Exception('test period not set right')
#use a subset to train surrgate model
t_obs_train_ind = list(range(tstart, tend))
#======assuming train dates are contiguous
if mode == 'train':
#if training, we only split realizations, but keep the time the same between train/test
#train deeponet on the full time range because it does not extrapolate
t_train_ind = list(range(nT))
t_test_ind = t_train_ind
else:
#if calibration, we split realizations and split the times
t_train_ind = list(range(tstart, tend+1))
t_test_ind = list(range(t_test_start, t_test_end+1))
#t_test_ind = list(set(range(nT)).difference(t_train_ind))
t_train_ind = np.array(t_train_ind, dtype=int)
t_test_ind = np.array(t_test_ind, dtype=int)
t_obs_train_ind = np.array(t_obs_train_ind, dtype=int)
#note: for calibration, nT_train is different from nT_test
nT_train = len(t_train_ind)
nT_test = len(t_test_ind)
#LOAD OBS DATA
#Note: this only matters to GA model calibration
obsTrainDict = {}
obsTestDict = {}
mu = None
#must include the downstream gage so it can be used for normalization?
assert ('01435000' in gage_ids_train)
daterng = pd.to_datetime(pd.date_range(start=startdate, end=enddate, freq='1D', tz='UTC'))
for gage_id in gageids:
#obsdf = loadUSGSObs(gageid=gage_id, startDate=startdate, endDate=enddate, returnDF=True)
obsdf = getEnsembleUSGSData(allUSGSData, gageid=gage_id, startDate=startdate, endDate=enddate, returnDF=True)
#this makes all DF the same length
obsdf = obsdf.reindex(daterng)
#generate binary mask
mask = np.ones(obsdf.shape[0])
mask[obsdf['Q'].isna()] = 0
obsdata = transformQ(obsdf.values, imethod=1)
if gage_id == '01435000':
#data normalization
mu = np.nanmean(obsdata[t_obs_train_ind])
std = np.nanstd(obsdata[t_obs_train_ind])
obsdata = (obsdata - mu)/std
obsdf = pd.DataFrame(obsdata, index=obsdf.index)
if gage_id in gage_ids_train:
obsTrainDict[gage_id] = {'Q':obsdf.values[t_train_ind], 'mask':mask[t_train_ind]}
if gage_id in gage_ids_test:
obsTestDict[gage_id] = {'Q':obsdf.values[t_test_ind], 'mask':mask[t_test_ind]}
#normalize Q using '01435000' obs statistics
Q_train= (Q_train - mu)/std
Q_test = (Q_test - mu)/std
#for training/testing surrogate models (t_train_ind == t_test_ind)
#[ngage, nEns, nT]
Q_train = Q_train[:, :, t_train_ind]
Q_test = Q_test[:,:, t_test_ind]
Q_pretrain = Q_pretrain[:, t_test_ind]
#[ngages*nTrain,1]
Q0 = Q_train[:,0,:].reshape(-1,1)
Q_train = rearrange(Q_train, 'a b c ->(a b c) 1')
Q_test = rearrange(Q_test, 'a b c ->(a b c) 1')
theta_train = theta[train_indices,:]
theta_test = theta[test_indices,:]
#Normalize parameters theta
theta_scaling = cfg.custom.theta_scaling
theta_param1=None
theta_param2=None
if theta_scaling in ['gaussian', 'normal']:
theta_param1 = np.mean(theta_train, axis=0)
theta_param2 = np.mean(theta_train, axis=0)
theta_train = (theta_train - theta_param1)/theta_param2
theta_test = (theta_test - theta_param1)/theta_param2
elif theta_scaling == 'minmax':
# to range[-1,1]
theta_param1 = np.min(theta_train, axis=0)
theta_param2 = np.max(theta_train, axis=0)
theta_train = (theta_train - theta_param1)/(theta_param2-theta_param1)*2 -1.0
theta_test = (theta_test - theta_param1)/(theta_param2-theta_param1)*2 -1.0
else:
raise NotImplementedError('scaling method not implemented')
print ('Theta min/max', np.min(theta_test, axis=0), np.max(theta_train,axis=0))
theta_0 = np.mean(theta_train, axis=0).reshape(1, 1, -1) #initial guess?
theta_train = np.expand_dims(theta_train, axis=0)
theta_test_oo = np.expand_dims(theta_test, axis=0)
#Repeat theta to match row size of Q
#the last dimension is number of parameters
theta_train = repeat(theta_train, 'a b c -> (a rep1) (b rep2) c', rep1=nGages_train, rep2=nT_train)
theta_test = repeat(theta_test_oo, 'a b c -> (a rep1) (b rep2) c', rep1=nGages_test, rep2=nT_test)
theta_0 = repeat(theta_0, 'a b c -> (a rep1) (b rep2) c', rep1=nGages_train, rep2=nT_train)
theta_train = rearrange(theta_train, 'a b c -> (a b) c')
theta_test = rearrange(theta_test, 'a b c -> (a b) c')
theta_0 = rearrange(theta_0, 'a b c -> (a b) c')
#Repeat time coordinates
#[nGages, nEns, nT]
t_train0 = np.reshape(x_coord[t_train_ind], (1, 1, nT_train)) #keep the original train vector
t_test_oo= np.reshape(x_coord[t_test_ind], (1, 1, nT_test))
t_train = repeat(t_train0, 'a b c -> (a rep1) (b rep2) c', rep1=nGages_train, rep2=len(train_indices))
t_test = repeat(t_test_oo, 'a b c -> (a rep1) (b rep2) c', rep1=nGages_test, rep2=len(test_indices))
t_train = rearrange(t_train, 'a b c -> (a b c) 1')
t_test = rearrange(t_test, 'a b c -> (a b c) 1')
#Repeat x, y coordinates
gagex_train = np.reshape(gage_x_train, (-1,1,1))
gagey_train = np.reshape(gage_y_train, (-1,1,1))
gagex_test = np.reshape(gage_x_test, (-1,1,1))
gagey_test = np.reshape(gage_y_test, (-1,1,1))
x_train = repeat(gagex_train, 'a b c -> a (b rep1) (c rep2)', rep1 =len(train_indices), rep2=nT_train)
x_test = repeat(gagex_train, 'a b c -> a (b rep1) (c rep2)', rep1 =len(test_indices), rep2=nT_test)
y_train = repeat(gagey_train, 'a b c -> a (b rep1) (c rep2)', rep1 =len(train_indices), rep2=nT_train)
y_test = repeat(gagey_train, 'a b c -> a (b rep1) (c rep2)', rep1 =len(test_indices), rep2=nT_test)
x_train = rearrange(x_train, 'a b c -> (a b c) 1')
x_test = rearrange(x_test, 'a b c -> (a b c) 1')
y_train = rearrange(y_train, 'a b c -> (a b c) 1')
y_test = rearrange(y_test, 'a b c -> (a b c) 1')
t0 = repeat(t_train0, 'a b c -> (a rep1) b c', rep1=nGages_train)
x0 = repeat(gagex_train, 'a b c -> a b (c rep2)', rep2=nT_train)
y0 = repeat(gagey_train, 'a b c -> a b (c rep2)', rep2=nT_train)
t0 = rearrange(t0, 'a b c ->(a b c) 1')
x0 = rearrange(x0, 'a b c ->(a b c) 1')
y0 = rearrange(y0, 'a b c ->(a b c) 1')
#for new sites
tnew = repeat(t_test_oo, 'a b c -> (a rep1) b c', rep1=nGages_test)
xnew = repeat(gagex_test, 'a b c -> a b (c rep2)', rep2=nT_test)
ynew = repeat(gagey_test, 'a b c -> a b (c rep2)', rep2=nT_test)
tnew = rearrange(tnew, 'a b c ->(a b c) 1')
xnew = rearrange(xnew, 'a b c ->(a b c) 1')
ynew = rearrange(ynew, 'a b c ->(a b c) 1')
print ('train x_train', x_train.shape, y_train.shape, t_train.shape, 'theta_train', theta_train.shape, 'u train', Q_train.shape)
print ('test x_test', x_test.shape, y_test.shape, t_test.shape, 'theta_test', theta_test.shape, 'u test', Q_test.shape, 'train indices', len(t_train_ind))
print ('a0 ', theta_0.shape, 't_0', t0.shape, 'x0', x0.shape, 'y0', y0.shape, 'u0', Q0.shape)
print ('xnew', xnew.shape, 'ynew', ynew.shape, 'tnew', tnew.shape, nT_train, nT_test)
data={
't_train': t_train,
't_test': t_test,
'x_train': x_train,
'x_test': x_test,
'y_train': y_train,
'y_test': y_test,
'a_train': theta_train,
'a_test' : theta_test,
'u_train': Q_train,
'u_test' : Q_test,
'scaler' : (mu, std),
'time_axis': full_daterng,
'train_ind': t_train_ind,
'test_ind' : t_test_ind,
'gage_id' : gage_id,
'n_test' : len(test_indices),
'n_gages': nGages_train,
'n_gages_test': nGages_test,
'nt_train': nT_train,
'nt_test': nT_test,
'obs_dict_train': obsTrainDict,
'obs_dict_test': obsTestDict,
'gage_weights': gage_weights,
'a_0': theta_0,
't_0': t0,
'x_0': x0,
'y_0': y0,
'u_0': Q0,
'id_train':id_train,
'tnew': tnew,
'xnew': xnew,
'ynew': ynew,
'theta_scaler': (theta_param1, theta_param2),
'expno':expno,
'gageids': gageids,
'Q_pretrain': Q_pretrain,
'fold_no': cfg.custom.fold_no,
}
return data
def genModel(cfg: ModulusConfig) -> DeepONetArch:
# [init-model]
trunk_net = instantiate_arch(
cfg=cfg.arch.trunk,
input_keys=[Key("t"),Key("x"),Key("y")],
output_keys=[Key("trunk", cfg.arch.trunk.layer_size)],
)
# set any normalization value that you want to apply to the data.
# For this dataset, calculate the scale and shift parameters
# it's easier to do normalization outside modulus !!!
branch_net = instantiate_arch(
cfg=cfg.arch.branch,
input_keys=[Key("a", 13)],
output_keys=[Key("branch", cfg.arch.branch.layer_size)],
)
deeponet = instantiate_arch(
cfg=cfg.arch.deeponet,
output_keys=[Key("u")],
branch_net=branch_net,
trunk_net=trunk_net,
)
total_params = sum(p.numel() for p in deeponet.parameters())
print(f"Number of parameters: {total_params}")
return deeponet
def getNetwork_dir(trainGageList,fold_no):
#set network_dir using train_id
exp_str = ""
for id in trainGageList:
exp_str += f"_{id}"
exp_str += f"fold_{fold_no}"
return exp_str
@modulus.main(config_path="conf", config_name="config_uns_uq_multi")
def run(cfg: ModulusConfig) -> None:
reTrain = cfg.custom.retrain
dataDict = getData(cfg, mode='train')
cfg.network_dir = getNetwork_dir(dataDict['id_train'], dataDict['fold_no'])
print ('Network dir is', cfg.network_dir)
deeponet = genModel(cfg)
nodes = [deeponet.make_node('deepo')]
# [init-model]
# [datasets]
x_train = dataDict["x_train"]
t_train = dataDict["t_train"]
y_train = dataDict["y_train"]
a_train = dataDict["a_train"]
u_train = dataDict["u_train"]
# load test dataset 1 [testing at training gage sites for test period]
x_test = dataDict["x_test"]
t_test = dataDict["t_test"]
y_test = dataDict["y_test"]
a_test = dataDict["a_test"]
u_test = dataDict["u_test"]
# [datasets]
# [constraint]
# make domain
domain = Domain()
data = DeepONetConstraint.from_numpy(
nodes=nodes,
invar={"a": a_train, "x": x_train, "t": t_train, "y":y_train},
outvar={"u": u_train},
batch_size=cfg.batch_size.train,
)
domain.add_constraint(data, "data")
# [constraint]
# [validator]
# add validators
# sites used during training
invar_valid = {
"a": a_test,
"x": x_test,
"t": t_test,
"y": y_test
}
print ('x_test.shape', x_test.shape, 'a_test', a_test.shape, 'u_test', u_test.shape)
outvar_valid = {"u": u_test}
dataset = DictGridDataset(invar_valid, outvar_valid)
validator = MyGridValidator(nodes=nodes, dataset=dataset, plotter=None)
domain.add_validator(validator, "validator")
#[validator]
# make solver
slv = Solver(cfg, domain)
if reTrain:
# start solver
starttime = time.time()
slv.solve()
print ('training took ', time.time()-starttime)
slv.eval()
#[plotting]
#=================do test dataset 1 ==================
varr = np.load(to_absolute_path(f'outputs/{cfg.custom.validate_subfolder}/{cfg.network_dir}/validators/validator.npz'), allow_pickle=True)
varr = np.atleast_1d(varr.f.arr_0)[0]
x = varr['x'].flatten()
y = varr['y'].flatten()
t_axis = dataDict['time_axis'][dataDict['train_ind']]
predU = varr['pred_u']
trueU = varr['true_u']
nt_test = dataDict['nt_train']
nGages = dataDict['n_gages']
nTest = dataDict['n_test']
print (nt_test, nGages)
predU = predU.reshape(nGages,nTest,nt_test)
trueU = trueU.reshape(nGages,nTest,nt_test)
allKGE = np.zeros((nGages,nTest))
allNRMSE = np.zeros((nGages,nTest))
for igage in range(nGages):
for itest in range(nTest):
pred_u = predU[igage, itest,:]
true_u = trueU[igage, itest,:]
pred_u = inverseTransformQ(dataDict['scaler'], pred_u, imethod=1).flatten()
true_u = inverseTransformQ(dataDict['scaler'], true_u, imethod=1).flatten()
allKGE[igage, itest] = HydroStats.kge_2012(pred_u,true_u )
allNRMSE[igage, itest] = HydroStats.nrmse_range(pred_u,true_u )
allKGE_mean = np.mean(allKGE, axis=1)
allKGE_median = np.median(allKGE, axis=1)
print ("Test data 1 ")
print ('mean', allKGE_mean, 'median', allKGE_median)
plotViolinplot(allKGE.T, allNRMSE.T, dataDict)
def plotViolinplot(kge_arr, nrmse_arr, dataDict):
"""This generates Figure 3 in the manuscript
"""
import seaborn as sns
#form a DF
df = pd.DataFrame(kge_arr)
fig,axes = plt.subplots(1,2, figsize=(12, 6), sharey=True)
sns.boxplot(data=kge_arr, native_scale=True, ax=axes[0], orient='h', width=0.5)
#!!!! need to first fake the ticks before modifying the tick labels
axes[0].yaxis.set_ticks(range(kge_arr.shape[1]))
axes[0].set_yticklabels(dataDict['gageids'], fontsize=12)
axes[0].set_xlabel('KGE', fontsize=12)
sns.boxplot(data=nrmse_arr, native_scale=True, ax=axes[1], orient='h', width=0.5)
#!!!! need to first fake the ticks before modifying the tick labels
axes[1].yaxis.set_ticks(range(nrmse_arr.shape[1]))
axes[1].set_yticklabels(dataDict['gageids'], fontsize=12)
axes[1].set_xlabel('NRMSE', fontsize=12)
plt.tight_layout(w_pad=0.2)
plt.savefig(to_absolute_path(f"outputs/surrogate_metrics_fold{dataDict['fold_no']}.eps"))
plt.close()
def genAttr():
"""Generate uniform random numbers
"""
return np.random.rand()*2-1.0
def getLoss(y, obsdict, loss_fun, gage_weights, device):
"""Multistation loss fun
"""
if gage_weights is None:
weights = np.zeros(len(obsdict.keys()))+1.0
else:
weights = gage_weights
loss = 0
for ix, gage_id in enumerate(obsdict.keys()):
mask = torch.IntTensor(obsdict[gage_id]['mask']).to(device)
obs_val = torch.FloatTensor(obsdict[gage_id]['Q'].squeeze()).to(device)
loss += weights[ix]*loss_fun(y[ix,0,:].squeeze()[mask==1], obs_val[mask==1])
loss = loss/np.sum(weights)
return loss.item()
def evalOneMin(individual, model, x0, y0, t0, obsdict, loss_fun, device, nGages, nT, return_y=False, gage_weights=None):
a_in = torch.FloatTensor(np.array(individual)).unsqueeze(0)
a_in = a_in.repeat(len(x0), 1)
a_in = torch.FloatTensor(a_in).to(device)
x0 = torch.FloatTensor(x0).to(device)
y0 = torch.FloatTensor(y0).to(device)
t0 = torch.FloatTensor(t0).to(device)
invar = {'a':a_in, 'x':x0, 'y':y0, 't': t0}
out = model(invar)
predU = out['u'].reshape(nGages,1, nT)
if return_y:
return predU.data.cpu().numpy()
else:
return getLoss(predU, obsdict, loss_fun, gage_weights, device),
@modulus.main(config_path="conf", config_name="config_uns_uq_multi")
def dodeap(cfg: ModulusConfig) -> None:
from deap import base
from deap import creator
from deap import tools
import seaborn as sns
import time
starttime = time.time()
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
dataDict = getData(cfg, mode='test')
cfg.network_dir = getNetwork_dir(dataDict['id_train'], dataDict['fold_no'])
print ('Network dir is', cfg.network_dir)
deeponet = genModel(cfg)
nodes = [deeponet.make_node('deepo')]
# [datasets for training GA]
x_train = dataDict["x_train"]
t_train = dataDict["t_train"]
y_train = dataDict["y_train"]
a_train = dataDict["a_train"]
u_train = dataDict["u_train"]
# make domain
domain = Domain()
data = DeepONetConstraint.from_numpy(
nodes=nodes,
invar={"a": a_train, "x": x_train, "t": t_train, "y":y_train},
outvar={"u": u_train},
batch_size=cfg.batch_size.train,
)
domain.add_constraint(data, "data")
#load trained model
slv = Solver(cfg, domain)
#print Success loading model: outputs/uppersink_deeponet/deepo.0.pth if successful
slv.eval()
# load test dataset 1
x_test = dataDict["x_test"]
t_test = dataDict["t_test"]
y_test = dataDict["y_test"]
a_test = dataDict["a_test"]
u_test = dataDict["u_test"]
obs_dict = dataDict['obs_dict_train']
gage_weights = dataDict['gage_weights']
invar_valid = {
"a": a_test,
"x": x_test,
"t": t_test,
"y": y_test
}
outvar_valid = {"u": u_test}
dataset = DictGridDataset(invar_valid, outvar_valid)
model = Graph(
nodes,
Key.convert_list(dataset.invar_keys),
Key.convert_list(dataset.outvar_keys),
)
device=torch.device("cuda:0")
model.to(device)
model.eval()
x0 = dataDict['x_0']
y0 = dataDict['y_0']
t0 = dataDict['t_0']
a0 = dataDict['a_0']
nGages = dataDict['n_gages']
nT = dataDict['nt_train']
loss_func = setLossFun(itype=cfg.custom.ga.loss_fun)
# Attribute generator
toolbox.register("attr_bool", genAttr)
# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_bool, 13)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", evalOneMin, model=model, loss_fun=loss_func,
x0=x0, y0=y0, t0=t0, obsdict = obs_dict, nGages=nGages, nT=nT, gage_weights=gage_weights,
device=device )
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=0.05)
toolbox.register("select", tools.selTournament, tournsize=3)
pop = toolbox.population(n=cfg.custom.ga.population)
# Evaluate the entire population
fitnesses = list(map(toolbox.evaluate, pop))
for ind, fit in zip(pop, fitnesses):
ind.fitness.values = fit
# CXPB is the probability with which two individuals
# are crossed
#
# MUTPB is the probability for mutating an individual
CXPB, MUTPB = 0.46, 0.005
# Extracting all the fitnesses of
fits = [ind.fitness.values[0] for ind in pop]
# Variable keeping track of the number of generations
max_gen = int(cfg.custom.ga.generation)
g = 0
# Begin the evolution
while max(fits) < 10000 and g < max_gen:
# A new generation
g = g + 1
print("-- Generation %i --" % g)
# Select the next generation individuals
offspring = toolbox.select(pop, len(pop))
# Clone the selected individuals
offspring = list(map(toolbox.clone, offspring))
# Apply crossover and mutation on the offspring
for child1, child2 in zip(offspring[::2], offspring[1::2]):
# cross two individuals with probability CXPB
if random.random() < CXPB:
toolbox.mate(child1, child2)
# fitness values of the children
# must be recalculated later
del child1.fitness.values
del child2.fitness.values
for mutant in offspring:
# mutate an individual with probability MUTPB
if random.random() < MUTPB:
toolbox.mutate(mutant)
del mutant.fitness.values
# Evaluate the individuals with an invalid fitness
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
fitnesses = map(toolbox.evaluate, invalid_ind)
for ind, fit in zip(invalid_ind, fitnesses):
ind.fitness.values = fit
print(" Evaluated %i individuals" % len(invalid_ind))
# The population is entirely replaced by the offspring
pop[:] = offspring
# Gather all the fitnesses in one list and print the stats
fits = [ind.fitness.values[0] for ind in pop]
length = len(pop)
mean = sum(fits) / length
sum2 = sum(x*x for x in fits)
std = abs(sum2 / length - mean**2)**0.5
print(" Min %s" % min(fits))
print(" Max %s" % max(fits))
print(" Avg %s" % mean)
print(" Std %s" % std)
print("-- End of (successful) evolution --")
print ('Wallclock training time', time.time()-starttime)
best_ind = tools.selBest(pop, 1)[0]
print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
theta = np.array(best_ind)
#save best parameter for UQ
np.save(to_absolute_path(f"outputs/{cfg.custom.validate_subfolder}/{cfg.network_dir}/best_param_exp{dataDict['expno']}.npy"), theta)
theta_min, theta_max = dataDict['theta_scaler']
theta = 0.5*(theta+1.0)*(theta_max-theta_min)+theta_min
printParams(theta.tolist())
#Get best model results for training period
best_u = evalOneMin(best_ind, model, x0, y0, t0, obs_dict, loss_func, device, nGages, nT, return_y=True)
#get date range
t_axis = dataDict['time_axis'][dataDict['train_ind']]
best_u_dict_train = {}
for iobs, gage_id in enumerate(obs_dict.keys()):
obs_u = obs_dict[gage_id]['Q']
mask = obs_dict[gage_id]['mask']
pred_u = inverseTransformQ(dataDict['scaler'], best_u[iobs, 0, :]).flatten()
obs_u = inverseTransformQ(dataDict['scaler'], obs_u).flatten()
kge_final = HydroStats.kge_2012(pred_u[mask==1], obs_u[mask==1])
print ('gage_id', gage_id, f'KGE final {kge_final:4.3f}')
best_u_dict_train[gage_id] = pred_u
#Get best model results for testing period
print ('*'*80)
best_u_dict_test = {}
obs_dict=dataDict['obs_dict_test']
print (dataDict['n_gages_test'], dataDict['nt_test'])
best_u = evalOneMin(best_ind, model, dataDict['xnew'], dataDict['ynew'], dataDict['tnew'],
obs_dict, loss_func, device, dataDict['n_gages_test'], dataDict['nt_test'], return_y=True)
#for latex
latex_str = ''
kge_arr = []
for iobs, gage_id in enumerate(obs_dict.keys()):
obs_u = obs_dict[gage_id]['Q']
mask = obs_dict[gage_id]['mask']
pred_u = inverseTransformQ(dataDict['scaler'], best_u[iobs, 0, :]).flatten()
obs_u = inverseTransformQ(dataDict['scaler'], obs_u).flatten()
kge_final = HydroStats.kge_2012(pred_u[mask==1], obs_u[mask==1])
kge_arr.append(kge_final)
print ('gage_id', gage_id, f'KGE final {kge_final:4.3f}')
latex_str += f'{kge_final:4.3f} &'
best_u_dict_test[gage_id] = pred_u
print ('*'*10, dataDict['expno'])
print (latex_str)
#save the best solution
pkl.dump([best_u_dict_train, best_u_dict_test], open(to_absolute_path(f"data/best_u_spacetime_{dataDict['expno']}.pkl"), 'wb'))
pkl.dump(kge_arr, open(to_absolute_path(f"data/best_kge_spacetime_{dataDict['expno']}.pkl"), 'wb'))
#0210, add pretrain results for table 2
latex_str = ''
Q_pretrain = dataDict['Q_pretrain']
for iobs, gage_id in enumerate(obs_dict.keys()):
obs_u = obs_dict[gage_id]['Q']
mask = obs_dict[gage_id]['mask']
pred_u = Q_pretrain[iobs, :].flatten()
obs_u = inverseTransformQ(dataDict['scaler'], obs_u).flatten()
kge_final = HydroStats.kge_2012(pred_u[mask==1], obs_u[mask==1])
print ('gage_id', gage_id, f'Pretrain KGE final {kge_final:4.3f}')
latex_str += f'{kge_final:4.3f} &'
print ('*'*10, 'pretrain')
print (latex_str)
@modulus.main(config_path="conf", config_name="config_uns_uq_multi")
def plotGAExp(cfg: ModulusConfig) -> None:
"""This generates Figure 4 in the paper
"""
import itertools
import seaborn as sns
nExp = 5
dataDict = getData(cfg, mode='test')
cfg.network_dir = getNetwork_dir(dataDict['id_train'], dataDict['fold_no'])
theta_min, theta_max = dataDict['theta_scaler']
#Experiments start with index 1
allParam = []
paramNames = [
"PT-canopy",
"PT-ground",
"PT-snow",
"PT-trans",
"sm-rate",
"sm-diff",
"manning_n",
"perm-S1",
"perm_S2",
"perm_S3",
"perm_S4",
"perm_S5",
"perm_bedrock"]
fig = plt.figure(figsize=(16,9))
gs = fig.add_gridspec(1, 4, width_ratios=(4,2,1,4),
left=0.05, right=0.95, bottom=0.15, top=0.9,
wspace=0.25, hspace=0.0)
ax0 = fig.add_subplot(gs[0,0])
ax1 = fig.add_subplot(gs[0,1])
ax2 = fig.add_subplot(gs[0,2])
ax3 = fig.add_subplot(gs[0,3])
palette = itertools.cycle(sns.color_palette('tab10'))
for i in range(1,nExp+1):
theta = np.load(to_absolute_path(f"outputs/{cfg.custom.validate_subfolder}/{cfg.network_dir}/best_param_exp{i}.npy"))
best_params = 0.5*(theta+1.0)*(theta_max-theta_min)+theta_min
print ('------Exp', i, '-------')
print (printParams(best_params))
expcolor = next(palette)
markersize= 10
ax0.plot(best_params[:4], 'o', color=expcolor, label=f'Exp{i}', markersize=markersize,alpha=0.75)
ax1.plot(best_params[4:6], 'o', color=expcolor, markersize=markersize,alpha=0.75)
ax2.plot(best_params[6], 'o', color=expcolor,markersize=markersize, alpha=0.75)
ax3.plot(best_params[7:], 'o', color=expcolor,markersize=markersize,alpha=0.75)
ax0.set_xticks(range(4))
ax0.set_xticklabels(paramNames[:4], rotation='vertical', fontsize=12)
ax1.set_xticks(range(2))
ax1.set_xticklabels(paramNames[4:6], rotation='vertical', fontsize=12)
ax2.set_xticks(range(1))
ax2.set_xticklabels(paramNames[6:7], rotation='vertical', fontsize=12)
ax3.set_xticks(range(6))
ax3.set_xticklabels(paramNames[7:], rotation='vertical', fontsize=12)
#handles, labels = ax0.get_legend_handles_labels()
ax0.legend(loc='best', fontsize=13)
plt.savefig(to_absolute_path('outputs/ga_result_barplot.eps'))