def gaussian_draw_test(): import os import numpy as np from pyemu import MonteCarlo,Cov,ParameterEnsemble from datetime import datetime jco = os.path.join("pst","pest.jcb") pst = jco.replace(".jcb",".pst") mc = MonteCarlo(jco=jco,pst=pst) num_reals = 100 start = datetime.now() mc.draw(num_reals=num_reals,how="gaussian") print(mc.parensemble.head()) print(datetime.now() - start) vals = mc.pst.parameter_data.parval1.values cov = Cov.from_parameter_data(mc.pst) start = datetime.now() val_array = np.random.multivariate_normal(vals, cov.as_2d,num_reals) print(datetime.now() - start) start = datetime.now() pe = ParameterEnsemble.from_gaussian_draw(mc.pst,cov,num_reals=num_reals) pet = pe._transform(inplace=False) pe = pet._back_transform(inplace=False) print(datetime.now() - start) print(mc.parensemble.head()) print(pe.head())
def parfile_test(): import os import numpy as np import pandas as pd from pyemu import MonteCarlo, Ensemble, ParameterEnsemble, Pst, Cov jco = os.path.join("pst", "pest.jcb") pst = jco.replace(".jcb", ".pst") mc = MonteCarlo(jco=jco, pst=pst) mc.pst.parameter_data.loc[mc.pst.par_names[1], "scale"] = 0.001 mc.draw(10) mc.parensemble.to_parfiles(os.path.join("temp", "testpar")) pst = Pst(pst) pst.parameter_data = pst.parameter_data.iloc[1:] pst.parameter_data["test", "parmne"] = "test" parfiles = [ os.path.join("temp", f) for f in os.listdir("temp") if "testpar" in f ] rnames = ["test{0}".format(i) for i in range(len(parfiles))] pe = ParameterEnsemble.from_parfiles(pst=pst, parfile_names=parfiles, real_names=rnames)
def fixed_par_test(): import os import numpy as np from pyemu import MonteCarlo,ParameterEnsemble jco = os.path.join("pst","pest.jcb") pst = jco.replace(".jcb",".pst") mc = MonteCarlo(jco=jco,pst=pst) mc.pst.parameter_data.loc["mult1","partrans"] = "fixed" mc.draw(10) assert np.all(mc.parensemble.loc[:,"mult1"] == mc.pst.parameter_data.loc["mult1","parval1"]) pe = ParameterEnsemble.from_gaussian_draw(mc.pst,mc.parcov,2)
def from_dataframe_test(): import os import numpy as np import pandas as pd from pyemu import MonteCarlo,Ensemble,ParameterEnsemble,Pst, Cov jco = os.path.join("pst","pest.jcb") pst = jco.replace(".jcb",".pst") mc = MonteCarlo(jco=jco,pst=pst) names = ["par_{0}".format(_) for _ in range(10)] df = pd.DataFrame(np.random.random((10,mc.pst.npar)),columns=mc.pst.par_names) mc.parensemble = ParameterEnsemble.from_dataframe(df=df,pst=mc.pst) print(mc.parensemble.shape) mc.project_parensemble() mc.parensemble.to_csv(os.path.join("temp","test.csv")) pstc = Pst(pst) par = pstc.parameter_data par.sort_values(by="parnme",ascending=False,inplace=True) cov = Cov.from_parameter_data(pstc) pe = ParameterEnsemble.from_gaussian_draw(pst=mc.pst,cov=cov)
def from_dataframe_test(): import os import numpy as np import pandas as pd from pyemu import MonteCarlo,Ensemble,ParameterEnsemble,Pst jco = os.path.join("pst","pest.jcb") pst = jco.replace(".jcb",".pst") mc = MonteCarlo(jco=jco,pst=pst) names = ["par_{0}".format(_) for _ in range(10)] df = pd.DataFrame(np.random.random((10,mc.pst.npar)),columns=mc.pst.par_names) mc.parensemble = ParameterEnsemble.from_dataframe(df=df,pst=mc.pst) print(mc.parensemble.shape) mc.project_parensemble() mc.parensemble.to_csv(os.path.join("temp","test.csv"))
def diagonal_cov_draw_test(): import os import numpy as np from pyemu import MonteCarlo,Cov,Pst,ParameterEnsemble jco = os.path.join("pst","pest.jcb") pst = Pst(jco.replace(".jcb",".pst")) mc = MonteCarlo(jco=jco,pst=pst) num_reals = 100 mc.draw(num_reals,obs=True) print(mc.obsensemble) pe1 = mc.parensemble.copy() cov = Cov(x=mc.parcov.as_2d,names=mc.parcov.row_names) #print(type(cov)) mc = MonteCarlo(jco=jco,pst=pst) mc.parensemble.reseed() mc.draw(num_reals,cov=cov) pe2 = mc.parensemble pe3 = ParameterEnsemble.from_gaussian_draw(mc.pst,num_reals=num_reals,cov=mc.parcov)
def parfile_test(): import os import numpy as np import pandas as pd from pyemu import MonteCarlo, Ensemble, ParameterEnsemble, Pst, Cov jco = os.path.join("pst", "pest.jcb") pst = jco.replace(".jcb", ".pst") mc = MonteCarlo(jco=jco, pst=pst) mc.pst.parameter_data.loc[mc.pst.par_names[1], "scale"] = 0.001 mc.draw(10) mc.parensemble.to_parfiles(os.path.join("temp","testpar")) pst = Pst(pst) pst.parameter_data = pst.parameter_data.iloc[1:] pst.parameter_data["test","parmne"] = "test" parfiles = [os.path.join("temp",f) for f in os.listdir("temp") if "testpar" in f] rnames = ["test{0}".format(i) for i in range(len(parfiles))] pe = ParameterEnsemble.from_parfiles(pst=pst,parfile_names=parfiles,real_names=rnames)