Skip to content

Commit

Permalink
Unify use of spot_setup hymod example
Browse files Browse the repository at this point in the history
  • Loading branch information
thouska committed Aug 15, 2020
1 parent ffb1747 commit 3dab429
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 22 deletions.
22 changes: 15 additions & 7 deletions docs/Hydrological_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion spotpy/examples/gui_hymod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spotpy/examples/spot_setup_hymod_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions spotpy/examples/spot_setup_hymod_python_pareto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spotpy/examples/tutorial_dream_hymod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3dab429

Please sign in to comment.