-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_004_yaml.py
142 lines (123 loc) · 4.49 KB
/
test_004_yaml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import tempfile
from contextlib import contextmanager
import pytest
from pytest_dependency import depends
import pastastore as pst
@contextmanager
def tempyaml(yaml):
temp = tempfile.NamedTemporaryFile(delete=False)
temp.write(yaml.encode("utf-8"))
temp.close()
try:
yield temp.name
finally:
os.unlink(temp.name)
@pytest.mark.dependency()
def test_load_yaml_rechargemodel(pstore):
yamlstr = """
my_first_model: # model name
oseries: oseries2 # head timeseries name, obtained from pastastore
stressmodels: # stressmodels dictionary
recharge: # name of the recharge stressmodel
class: RechargeModel # type of pastas StressModel
prec: prec2 # name of precipitation stress, obtained from pastastore
evap: evap2 # name of evaporation stress, obtained from pastastore
recharge: Linear # pastas recharge type
rfunc: Exponential # response function
"""
with tempyaml(yamlstr) as f:
ml = pstore.yaml.load(f)[0]
pstore.add_model(ml)
return
@pytest.mark.dependency()
def test_load_yaml_stressmodel(pstore):
yamlstr = """
my_second_model: # model name
oseries: oseries2 # head timeseries name, obtained from pastastore
stressmodels: # stressmodels dictionary
prec: # name of the recharge stressmodel
class: StressModel # type of pastas StressModel
stress: prec2 # name of precipitation stress, obtained from pastastore
rfunc: Gamma # response function
"""
with tempyaml(yamlstr) as f:
ml = pstore.yaml.load(f)[0]
pstore.add_model(ml)
return
@pytest.mark.dependency()
def test_load_yaml_wellmodel(pstore):
yamlstr = """
my_third_model: # model name
oseries: oseries1 # head timeseries name, obtained from pastastore
stressmodels: # stressmodels dictionary
well: # name of the recharge stressmodel
class: WellModel # type of pastas StressModel
stress: well1 # name of well stress, obtained from pastastore
distances: [100]
"""
with tempyaml(yamlstr) as f:
ml = pstore.yaml.load(f)[0]
pstore.add_model(ml)
return
@pytest.mark.dependency()
def test_write_load_compare_yaml(request, pstore):
depends(
request,
[
f"test_load_yaml_rechargemodel[{pstore.type}]",
f"test_load_yaml_stressmodel[{pstore.type}]",
f"test_load_yaml_wellmodel[{pstore.type}]",
],
)
pstore.yaml.export_models(modelnames=["my_first_model"])
ml1 = pstore.models["my_first_model"]
ml2 = pstore.yaml.load("my_first_model.yaml")[0]
assert (
pst.util.compare_models(ml1, ml2, detailed_comparison=True).iloc[1:, -1].all()
)
os.remove("my_first_model.yaml")
return
@pytest.mark.dependency()
def test_write_yaml_per_oseries(request, pstore):
depends(
request,
[
f"test_load_yaml_rechargemodel[{pstore.type}]",
f"test_load_yaml_stressmodel[{pstore.type}]",
f"test_load_yaml_wellmodel[{pstore.type}]",
],
)
pstore.yaml.export_stored_models_per_oseries()
os.remove("oseries1.yaml")
os.remove("oseries2.yaml")
return
@pytest.mark.dependency()
def test_write_yaml_minimal(request, pstore):
depends(
request,
[
f"test_load_yaml_rechargemodel[{pstore.type}]",
f"test_load_yaml_stressmodel[{pstore.type}]",
f"test_load_yaml_wellmodel[{pstore.type}]",
],
)
ml = pstore.models["my_first_model"]
pstore.yaml.export_model(ml, minimal_yaml=True)
os.remove("my_first_model.yaml")
return
@pytest.mark.dependency()
@pytest.mark.xfail(reason="settings not parsed correctly in pastas")
def test_write_yaml_minimal_nearest(request, pstore):
depends(
request,
[
f"test_load_yaml_rechargemodel[{pstore.type}]",
f"test_load_yaml_stressmodel[{pstore.type}]",
f"test_load_yaml_wellmodel[{pstore.type}]",
],
)
ml = pstore.models["my_third_model"]
pstore.yaml.export_model(ml, minimal_yaml=True, use_nearest=True)
os.remove("my_third_model.yaml")
return