forked from oemof/oemof-solph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_time_index.py
176 lines (138 loc) · 6.06 KB
/
test_time_index.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -
"""Test the definition of the time index of the model.
SPDX-FileCopyrightText: Uwe Krien <[email protected]>
SPDX-FileCopyrightText: Stephan Günther
SPDX-License-Identifier: MIT
"""
import pandas as pd
import pytest
from oemof.tools import debugging
from oemof import solph
def test_energysystem_with_datetimeindex_infer_last_interval():
"""Test EnergySystem with DatetimeIndex (equidistant)"""
datetimeindex = pd.date_range("1/1/2012", periods=24, freq="h")
es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=True)
assert es.timeincrement[1] == 1.0
assert es.timeincrement.sum() == 24
def test_energysystem_with_datetimeindex():
datetimeindex = pd.date_range("1/1/2012", periods=24, freq="h")
es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=False)
assert es.timeincrement[1] == 1.0
assert es.timeincrement.sum() == 23
def test_energysystem_interval_inference_warning():
datetimeindex = pd.date_range("1/1/2012", periods=24, freq="h")
with pytest.warns(FutureWarning):
_ = solph.EnergySystem(timeindex=datetimeindex)
def test_energysystem_with_datetimeindex_non_equidistant_infer_last_interval():
"""Test EnergySystem with DatetimeIndex (non-equidistant)"""
dtindex1 = pd.date_range("1/1/2012", periods=24, freq="h")
dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
dtindex = dtindex1.union(dtindex2)
msg = (
"You cannot infer the last interval if the 'freq' attribute of your "
"DatetimeIndex is None."
)
with pytest.raises(AttributeError, match=msg):
solph.EnergySystem(timeindex=dtindex, infer_last_interval=True)
def test_energysystem_with_datetimeindex_non_equidistant():
"""Test EnergySystem with DatetimeIndex (non-equidistant)"""
dtindex1 = pd.date_range("1/1/2012", periods=24, freq="h")
dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
dtindex = dtindex1.union(dtindex2)
es = solph.EnergySystem(timeindex=dtindex, infer_last_interval=False)
assert es.timeincrement.sum() == 48.0
assert es.timeincrement[0] == 1
assert es.timeincrement[25] == 0.5
def test_energysystem_with_numeric_index_infer_last_interval():
"""Test EnergySystem with numeric index (equidistant)"""
time_increments = [1, 1, 1, 1, 1]
es = solph.EnergySystem(timeincrement=time_increments)
assert es.timeincrement[1] == 1.0
assert pd.Series(es.timeincrement).sum() == 5
def test_energysystem_with_numeric_index():
"""Test EnergySystem with numeric index (equidistant)"""
time_increments = [1, 1, 1, 1, 1]
es = solph.EnergySystem(
timeincrement=time_increments, infer_last_interval=False
)
assert es.timeincrement[1] == 1.0
assert pd.Series(es.timeincrement).sum() == 5
def test_energysystem_with_numeric_index_non_equidistant_infer_last_interval():
"""
Test EnergySystem with DatetimeIndex (non-equidistant)
'infer_last_interval=True/False' does not have any effect.
"""
time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5]
es = solph.EnergySystem(
timeincrement=time_increments, infer_last_interval=True
)
assert pd.Series(es.timeincrement).sum() == 7.0
assert es.timeincrement[0] == 1
assert es.timeincrement[6] == 0.5
def test_energysystem_with_numeric_index_non_equidistant():
"""
Test EnergySystem with DatetimeIndex (non-equidistant)
'infer_last_interval=True/False' does not have any effect.
"""
time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5]
es = solph.EnergySystem(
timeincrement=time_increments, infer_last_interval=False
)
assert pd.Series(es.timeincrement).sum() == 7.0
assert es.timeincrement[0] == 1
assert es.timeincrement[8] == 0.25
def test_model_timeincrement_with_valid_timeindex():
datetimeindex = pd.date_range("1/1/2012", periods=5, freq="h")
es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=True)
m = solph._models.Model(es)
assert es.timeincrement.sum() == 5
assert m.timeincrement.sum() == 5
assert m.timeincrement[2] == 1
def test_timeincrement_with_non_valid_timeindex():
with pytest.raises(
TypeError, match="Parameter 'timeindex' has to be of type"
):
solph.EnergySystem(timeindex=4)
def test_conflicting_time_index():
msg = (
"Specifying the timeincrement and the timeindex parameter at the same "
"time is not allowed"
)
with pytest.raises(AttributeError, match=msg):
solph.EnergySystem(
timeindex=pd.date_range("1/1/2012", periods=2, freq="h"),
timeincrement=[1, 2, 3, 4],
infer_last_interval=False,
)
def test_missing_timeincrement():
msg = (
"The EnergySystem needs to have a valid 'timeincrement' attribute to "
"build a model."
)
es = solph.EnergySystem()
with pytest.raises(AttributeError, match=msg):
solph.Model(es)
def test_overwrite_timeincrement():
es = solph.EnergySystem(
timeindex=pd.date_range("1/1/2012", periods=2, freq="h"),
infer_last_interval=True,
)
assert es.timeincrement[0] == 1
with pytest.warns(debugging.SuspiciousUsageWarning):
m = solph._models.Model(es, timeincrement=[3])
assert m.timeincrement[0] == 3
def test_model_timeincrement_list():
es = solph.EnergySystem(timeincrement=[0.1, 1, 2, 3])
m = solph._models.Model(es)
assert m.timeincrement[3] == 3
def test_nonequ_inconsistent_timeindex():
# with pytest.raises(IndexError):
timeindex_one = pd.date_range("1/1/2019", periods=1, freq="h")
timeindex_hourly = pd.date_range("1/1/2019", periods=3, freq="h")
timeindex_45mins = pd.date_range("1/1/2019", periods=2, freq="45min")
timeindex1 = timeindex_one.append(timeindex_hourly)
timeindex2 = timeindex_hourly.append([timeindex_45mins])
with pytest.raises(TypeError):
solph.EnergySystem(timeindex=timeindex1, infer_last_interval=False)
with pytest.raises(TypeError):
solph.EnergySystem(timeindex=timeindex2, infer_last_interval=False)