@@ -607,7 +607,8 @@ def ends_freyberg_test(tmp_path):
607
607
ends = pyemu .EnDS (pst = pst , sim_ensemble = oe , obscov = os .path .join (test_d , "obs.unc" ),predictions = predictions )
608
608
609
609
610
- def ends_freyberg_dsi_test (tmp_path ):
610
+
611
+ def ends_run_freyberg_dsi (tmp_path ,nst = False ,nst_extrap = None ,ztz = False ,energy = 1.0 ):
611
612
import pyemu
612
613
import os
613
614
test_d = "ends_master"
@@ -621,58 +622,44 @@ def ends_freyberg_dsi_test(tmp_path):
621
622
oe_name = pst_name .replace (".pst" , ".0.obs.csv" )
622
623
oe = pyemu .ObservationEnsemble .from_csv (pst = pst , filename = oe_name ).iloc [:100 , :]
623
624
624
-
625
625
ends = pyemu .EnDS (pst = pst , sim_ensemble = oe ,verbose = True )
626
626
t_d = os .path .join (tmp_path ,"dsi_template" )
627
- ends .prep_for_dsi (t_d = t_d )
627
+
628
+ ends .prep_for_dsi (t_d = t_d ,
629
+ use_ztz = ztz ,
630
+ apply_normal_score_transform = nst ,
631
+ nst_extrap = nst_extrap ,
632
+ energy = energy )
628
633
# copy exe to dsi_template
629
634
#shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
630
-
635
+ filename = os .path .join (t_d ,"dsi.0.obs.csv" )
636
+ if os .path .exists (filename ):
637
+ os .remove (filename )
631
638
pst = pyemu .Pst (os .path .join (t_d ,"dsi.pst" ))
632
- pst .control_data .noptmax = 0
639
+ pst .control_data .noptmax = - 1
633
640
pst .write (os .path .join (t_d ,"dsi.pst" ),version = 2 )
634
- #pyemu.os_utils.run("pestpp-ies dsi.pst",cwd="dsi_template")
635
- m_d = os .path .join (tmp_path ,"master_dsi" )
636
- pyemu .os_utils .start_workers (t_d ,"pestpp-ies" ,"dsi.pst" ,num_workers = 15 ,worker_root = tmp_path ,
637
- master_dir = m_d )
641
+ pyemu .os_utils .run ("pestpp-ies dsi.pst" ,cwd = t_d )
638
642
639
- # run test wtih truncated svd
640
- ends .prep_for_dsi (t_d = t_d ,truncated_svd = True )
641
- # copy exe to dsi_template
642
- #shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
643
+ #read in the results
644
+ oe = pyemu .ObservationEnsemble .from_csv (pst = pst , filename = os .path .join (t_d ,"dsi.0.obs.csv" ))
645
+ assert oe .shape [0 ]== 50 , f"{ 50 - oe .shape } failed runs"
646
+ phi_vector = oe .phi_vector .sort_values ().values
647
+ assert phi_vector [0 ] != phi_vector [1 ],phi_vector
643
648
644
- pst = pyemu .Pst (os .path .join (t_d ,"dsi.pst" ))
645
- pst .control_data .noptmax = 0
646
- pst .write (os .path .join (t_d ,"dsi.pst" ),version = 2 )
647
- pyemu .os_utils .start_workers (t_d ,"pestpp-ies" ,"dsi.pst" ,num_workers = 15 ,worker_root = tmp_path ,
648
- master_dir = m_d )
649
+ def ends_freyberg_dsi_test (tmp_path ):
650
+ ends_run_freyberg_dsi (tmp_path )
649
651
652
+ def ends_freyberg_dsi_nst_test (tmp_path ):
653
+ ends_run_freyberg_dsi (tmp_path ,nst = True ,nst_extrap = None )
650
654
651
- # run test wtih normal score transform
652
- ends .prep_for_dsi (t_d = t_d ,apply_normal_score_transform = True )
653
- # copy exe to dsi_template
654
- #shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
655
-
656
- pst = pyemu .Pst (os .path .join (t_d ,"dsi.pst" ))
657
- pst .control_data .noptmax = 0
658
- pst .write (os .path .join (t_d ,"dsi.pst" ),version = 2 )
659
- pyemu .os_utils .start_workers (t_d ,"pestpp-ies" ,"dsi.pst" ,num_workers = 15 ,worker_root = tmp_path ,
660
- master_dir = m_d )
661
-
662
- # run test with log-transform
663
- pst = pyemu .Pst (pst_name )
664
- pst .pestpp_options ["predictions" ] = predictions
665
- pst .observation_data ["obstransform" ] = "log"
666
- ends = pyemu .EnDS (pst = pst , sim_ensemble = oe ,verbose = True )
667
- ends .prep_for_dsi (t_d = t_d ,apply_normal_score_transform = False )
668
- # copy exe to dsi_template
669
- #shutil.copy2(os.path.join(test_d,"pestpp-ies.exe"),os.path.join(t_d,"pestpp-ies.exe"))
670
-
671
- pst = pyemu .Pst (os .path .join (t_d ,"dsi.pst" ))
672
- pst .control_data .noptmax = 0
673
- pst .write (os .path .join (t_d ,"dsi.pst" ),version = 2 )
674
- pyemu .os_utils .start_workers (t_d ,"pestpp-ies" ,"dsi.pst" ,num_workers = 15 ,worker_root = tmp_path ,
675
- master_dir = m_d )
655
+ def ends_freyberg_dsi_extrap_test (tmp_path ):
656
+ ends_run_freyberg_dsi (tmp_path ,nst = True ,nst_extrap = 'quadratic' )
657
+
658
+ def ends_freyberg_dsi_ztz_test (tmp_path ):
659
+ ends_run_freyberg_dsi (tmp_path ,ztz = True )
660
+
661
+ def ends_freyberg_dsi_svd_test (tmp_path ):
662
+ ends_run_freyberg_dsi (tmp_path ,ztz = True ,energy = 0.999 )
676
663
677
664
678
665
def plot_freyberg_dsi ():
@@ -738,15 +725,28 @@ def dsi_normscoretransform_test():
738
725
oe = pyemu .ObservationEnsemble .from_csv (pst = pst , filename = oe_name ).iloc [:100 , :]
739
726
740
727
nstval = randrealgen_optimized (oe .shape [0 ], 1e-7 , 1e4 )
728
+ window_size = 3
729
+ if oe .shape [0 ]> 40 :
730
+ window_size = 5
731
+ if oe .shape [0 ]> 90 :
732
+ window_size = 7
733
+ if oe .shape [0 ]> 200 :
734
+ window_size = 9
741
735
for name in oe .columns :
742
736
print ("transforming:" ,name )
743
737
sorted_values = oe ._df .loc [:,name ].sort_values ().copy ()
738
+ #if all values are the same, skip
739
+ if sorted_values .iloc [0 ] == sorted_values .iloc [- 1 ]:
740
+ print ("all values are the same, skipping" )
741
+ continue
742
+ sorted_values .loc [:] = pyemu .eds .moving_average_with_endpoints (sorted_values .values , window_size )
744
743
transformed_values = np .asarray ([normal_score_transform (nstval , sorted_values , value )[0 ] for value in sorted_values ])
745
744
backtransformed_values = np .asarray ([inverse_normal_score_transform (nstval , sorted_values , value )[0 ] for value in transformed_values ])
746
745
747
746
diff = backtransformed_values - sorted_values
748
747
assert max (abs (diff ))< 1e-7 , backtransformed_values
749
748
749
+
750
750
if __name__ == "__main__" :
751
751
#dsi_normscoretransform_test()
752
752
#ends_freyberg_dev()
0 commit comments