• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thouska / spotpy / 7709518362

30 Jan 2024 10:11AM UTC coverage: 77.332% (-0.2%) from 77.518%
7709518362

push

github

thouska
Merge branch 'master' of https://github.com/thouska/spotpy

4162 of 5382 relevant lines covered (77.33%)

3.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.27
/src/spotpy/cli.py
1
import inspect
6✔
2
import io
6✔
3
import os
6✔
4

5
import click
6✔
6

7
from . import algorithms, database, describe
6✔
8

9

10
def get_config_from_file():
6✔
11
    """
12
    Gets the spotpy configuration from a config file 'spotpy.conf'.
13

14
    Example:
15

16
        sampler = mc
17
        dbtype = csv
18
        parallel = seq
19
        # This is a comment
20
        runs = 10
21
    """
22
    config = {}
4✔
23
    if os.path.exists("spotpy.conf"):
4✔
24
        with io.open("spotpy.conf") as f:
4✔
25
            for line in f:
4✔
26
                if not line.strip().startswith("#"):
4✔
27
                    try:
4✔
28
                        k, v = line.split("=", 1)
4✔
29
                        config[k.strip()] = v.strip()
4✔
30
                    except ValueError:
4✔
31
                        pass
4✔
32
    return config
4✔
33

34

35
def get_sampler_from_string(sampler_name):
6✔
36
    return getattr(algorithms, sampler_name)
4✔
37

38

39
def make_type_from_module(module, *exclude):
6✔
40
    def use(cl):
6✔
41
        # Check if class name starts with an exclusion term
42
        return inspect.isclass(cl) and not any(
6✔
43
            [cl.__name__.startswith(ex) for ex in ("_",) + exclude]
44
        )
45

46
    members = inspect.getmembers(module, use)
6✔
47
    return click.Choice([n for n, m in members if not n.startswith("_")])
6✔
48

49

50
@click.group(context_settings=dict(help_option_names=["-h", "--help"]))
6✔
51
def cli():
3✔
52
    pass
4✔
53

54

55
@cli.command()
6✔
56
@click.pass_context
6✔
57
@click.option(
6✔
58
    "--sampler",
59
    "-s",
60
    type=make_type_from_module(algorithms),
61
    default="mc",
62
    help="Select the spotpy sampler",
63
)
64
@click.option(
6✔
65
    "--dbformat",
66
    type=click.Choice(database.__dir__()),
67
    default="ram",
68
    help="The type of the database",
69
)
70
@click.option(
6✔
71
    "--dbname", type=click.STRING, help="The name of the database, leave open for ram"
72
)
73
@click.option(
6✔
74
    "--parallel",
75
    "-p",
76
    type=click.Choice(["seq", "mpc", "mpi"]),
77
    default="seq",
78
    help="Parallelization: seq = no parallelization, mpi = MPI (for clusters), mpc = multiprocessing",
79
)
80
@click.option("--runs", "-n", type=click.INT, default=1, help="Number of runs")
6✔
81
@click.option(
6✔
82
    "--config",
83
    "-c",
84
    is_flag=True,
85
    help="Print only the configuration, can be used to create a config file with your_model.py > spotpy.conf",
86
)
87
def run(ctx, **kwargs):
3✔
88
    """
89
    Runs a sampler for automatic calibration
90
    """
91
    setup = ctx.obj
4✔
92
    if kwargs.pop("config", None):
4✔
93
        click.echo("\n".join("{} = {}".format(k, v) for k, v in kwargs.items()))
4✔
94
    else:
95
        sampler_name = kwargs.pop("sampler")
4✔
96
        sampler_class = get_sampler_from_string(sampler_name)
4✔
97
        runs = kwargs.pop("runs")
4✔
98
        sampler = sampler_class(setup, **kwargs)
4✔
99
        sampler.sample(runs)
4✔
100

101

102
@cli.command()
6✔
103
@click.pass_context
6✔
104
def gui(ctx):
3✔
105
    """
106
    Shows a GUI for manual calibration
107
    """
108
    from spotpy.gui.mpl import GUI
×
109

110
    setup = ctx.obj
×
111
    gui = GUI(setup)
×
112
    gui.show()
×
113

114

115
def main(setup):
6✔
116
    # Prevent help text from wrapping
117
    cli.help = "\b\n" + describe.setup(setup).replace("\n\n", "\n\b\n")
×
118
    config = get_config_from_file()
×
119
    cli(obj=setup, auto_envvar_prefix="SPOTPY", default_map=config)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc