diff --git a/docs/Hydrological_model.md b/docs/Hydrological_model.md index fe25527f..09cf2bd4 100644 --- a/docs/Hydrological_model.md +++ b/docs/Hydrological_model.md @@ -142,8 +142,9 @@ Keep in mind, that the \__init\__ function is only called once during the sampli The more you can separate from you model into the \__init\__ function, the faster you sampling will be. class spotpy_setup(object): - def __init__(self, used_algorithm = 'default'): - self._used_algorithm = _used_algorithm + def __init__(self, obj_func=None): + #Just a way to keep this example flexible and applicable to various examples + self.obj_func = obj_func datastart = datetime(1998,6,1) dataend = datetime(2000,1,1) analysestart = datetime(1999,1,1) @@ -172,11 +173,18 @@ To define the VanGenuchten parameter boundaries we use a normal distribution. return self.cmfmodel.observations def objectivefunction(self,simulation,evaluation): - if self._used_algorithm == 'sceua': # The SCE-UA algorithm minimizes the objectivefunction - like = spotpy.objectivefunctions.rmse(evaluation,simulation) - else: # All other implemented algorithm maximize the objectivefunction - objectivefunction= -spotpy.objectivefunctions.rmse(evaluation,simulation) - return objectivefunction + #SPOTPY expects to get one or multiple values back, + #that define the performence of the model run + if not self.obj_func: + # This is used if not overwritten by user + # RMSE (root mean squared error) works good for the SCE-UA algorithm, + # as it minimizes the objective function. + # All other implemented algorithm maximize the objectivefunction + model_performance = spotpy.objectivefunctions.rmse(evaluation,simulation) + else: + #Way to ensure flexible spot setup class + model_performance = self.obj_func(evaluation,simulation) + return model_performance ## Sampling diff --git a/spotpy/examples/gui_hymod.py b/spotpy/examples/gui_hymod.py index a1d8ffd7..d0f7f9b3 100644 --- a/spotpy/examples/gui_hymod.py +++ b/spotpy/examples/gui_hymod.py @@ -10,9 +10,10 @@ import spotpy from spotpy.gui.mpl import GUI from spotpy.examples.spot_setup_hymod_python import spot_setup +from spotpy.objectivefunctions import rmse if __name__ == '__main__': - setup_class=spot_setup(_used_algorithm='sceua') + setup_class=spot_setup(rmse) #Select number of maximum allowed repetitions rep=10000 diff --git a/spotpy/examples/spot_setup_hymod_python.py b/spotpy/examples/spot_setup_hymod_python.py index 7fdefa2f..876f1f41 100644 --- a/spotpy/examples/spot_setup_hymod_python.py +++ b/spotpy/examples/spot_setup_hymod_python.py @@ -73,6 +73,6 @@ def objectivefunction(self,simulation,evaluation, params=None): # This is used if not overwritten by user like = rmse(evaluation,simulation) else: - #Way to ensure on flexible spot setup class + #Way to ensure flexible spot setup class like = self.obj_func(evaluation,simulation) return like \ No newline at end of file diff --git a/spotpy/examples/spot_setup_hymod_python_pareto.py b/spotpy/examples/spot_setup_hymod_python_pareto.py index cc15b13c..ceb2fffa 100644 --- a/spotpy/examples/spot_setup_hymod_python_pareto.py +++ b/spotpy/examples/spot_setup_hymod_python_pareto.py @@ -24,8 +24,7 @@ class spot_setup(object): Ks = spotpy.parameter.Uniform(low=0.0 , high=0.10, optguess=0.0404) Kq = spotpy.parameter.Uniform(low=0.1 , high=0.99, optguess=0.5592) - def __init__(self, _used_algorithm = 'default'): - self._used_algorithm = _used_algorithm + def __init__(self): #Transform [mm/day] into [l s-1], where 1.783 is the catchment area self.Factor = 1.783 * 1000 * 1000 / (60 * 60 * 24) #Load Observation data from file diff --git a/spotpy/examples/tutorial_dream_hymod.py b/spotpy/examples/tutorial_dream_hymod.py index 543ac514..c7cc3d9a 100644 --- a/spotpy/examples/tutorial_dream_hymod.py +++ b/spotpy/examples/tutorial_dream_hymod.py @@ -13,20 +13,20 @@ #from spotpy.examples.spot_setup_hymod_exe import spot_setup from spotpy.examples.spot_setup_hymod_python import spot_setup import matplotlib.pyplot as plt - +from spotpy.likelihoods import gaussianLikelihoodMeasErrorOut as GausianLike if __name__ == "__main__": parallel ='seq' # Initialize the Hymod example (will only work on Windows systems) #spot_setup=spot_setup(parallel=parallel) - spot_setup=spot_setup(obj_func = spotpy.likelihoods.gaussianLikelihoodMeasErrorOut) + spot_setup=spot_setup(GausianLike) # Create the Dream sampler of spotpy, al_objfun is set to None to force SPOTPY # to jump into the def objectivefunction in the spot_setup class (default is # spotpy.objectivefunctions.log_p) #Select number of maximum repetitions - rep=10000 + rep=5000 # Select five chains and set the Gelman-Rubin convergence limit nChains = 4 diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index 63578543..f6bfce4b 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -17,6 +17,8 @@ from spotpy.examples.tutorial_padds import padds_spot_setup from spotpy.describe import describe import os +from spotpy.likelihoods import gaussianLikelihoodMeasErrorOut as GausianLike + #https://docs.python.org/3/library/unittest.html @@ -50,37 +52,37 @@ def test_mle(self): self.assertEqual(len(results), self.rep) def test_mcmc(self): - sampler=spotpy.algorithms.mcmc(spot_setup(used_algorithm='mcmc'),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler=spotpy.algorithms.mcmc(spot_setup(GausianLike),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) sampler.sample(self.rep) results = sampler.getdata() self.assertEqual(len(results), self.rep) def test_demcz(self): - sampler=spotpy.algorithms.demcz(spot_setup(used_algorithm='demcz'),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler=spotpy.algorithms.demcz(spot_setup(GausianLike),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) sampler.sample(self.rep, convergenceCriteria=0) results = sampler.getdata() self.assertEqual(len(results), self.rep) def test_dream(self): - sampler=spotpy.algorithms.dream(spot_setup(used_algorithm='dream'),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler=spotpy.algorithms.dream(spot_setup(GausianLike),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) sampler.sample(self.rep, convergence_limit=0.9, runs_after_convergence=500) results = sampler.getdata() self.assertEqual(len(results), self.rep) def test_sceua(self): - sampler=spotpy.algorithms.sceua(spot_setup(used_algorithm='sceua'),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler=spotpy.algorithms.sceua(spot_setup(),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) sampler.sample(self.rep) results = sampler.getdata() self.assertLessEqual(len(results), self.rep) #Sceua save per definition not all sampled runs def test_abc(self): - sampler=spotpy.algorithms.abc(spot_setup(used_algorithm='abc'),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler=spotpy.algorithms.abc(spot_setup(),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) sampler.sample(self.rep) results = sampler.getdata() self.assertEqual(len(results), self.rep) def test_fscabc(self): - sampler=spotpy.algorithms.fscabc(spot_setup(used_algorithm='fscabc'),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler=spotpy.algorithms.fscabc(spot_setup(),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) sampler.sample(self.rep) results = sampler.getdata() self.assertEqual(len(results), self.rep) diff --git a/tests/test_analyser.py b/tests/test_analyser.py index 3379427d..ba35f13f 100644 --- a/tests/test_analyser.py +++ b/tests/test_analyser.py @@ -23,7 +23,7 @@ from spotpy.examples.spot_setup_rosenbrock import spot_setup as rosenbrock_setup from spotpy.examples.spot_setup_griewank import spot_setup as griewank_setup from spotpy.examples.spot_setup_hymod_python import spot_setup as hymod_setup - +from spotpy.likelihoods import gaussianLikelihoodMeasErrorOut as GausianLike class TestAnalyser(unittest.TestCase): @classmethod @@ -51,7 +51,7 @@ def setUpClass(self): sampler.sample(self.rep) self.sens_results = sampler.getdata() #Hymod resuts are empty with Python <3.6 - sampler = spotpy.algorithms.dream(hymod_setup(_used_algorithm='dream'), + sampler = spotpy.algorithms.dream(hymod_setup(GausianLike), sim_timeout=self.timeout) self.r_hat = sampler.sample(self.rep) self.hymod_results = sampler.getdata()