test_conflicting_time_index()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nop 0
1
# -*- coding: utf-8 -
2
3
"""Test the definition of the time index of the model.
4
5
SPDX-FileCopyrightText: Uwe Krien <[email protected]>
6
SPDX-FileCopyrightText: Stephan Günther
7
8
SPDX-License-Identifier: MIT
9
"""
10
11
import pandas as pd
12
import pytest
13
from oemof.tools import debugging
14
15
from oemof import solph
16
17
18
def test_increment_from_timeindex():
19
    dtindex1 = pd.date_range("1/1/2012", periods=24, freq="h")
20
    dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
21
    dtindex = dtindex1.union(dtindex2)
22
    es = solph.EnergySystem(timeindex=dtindex)
23
    assert (es.timeindex == dtindex).all()
24
    assert (es.timeincrement == 24 * [1] + 48 * [0.5]).all()
25
26
27
def test_increment_from_numeric_index():
28
    index = [0, 1, 3, 4, 5]
29
    es = solph.EnergySystem(timeindex=index)
30
    assert es.timeindex == index
31
    assert (es.timeincrement == [1, 2, 1, 1]).all()
32
33
34
def test_energysystem_with_datetimeindex_infer_last_interval():
35
    """Test EnergySystem with DatetimeIndex (equidistant)"""
36
    datetimeindex = pd.date_range("1/1/2012", periods=24, freq="h")
37
    es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=True)
38
    assert es.timeincrement[1] == 1.0
39
    assert es.timeincrement.sum() == 24
40
41
42
def test_energysystem_with_datetimeindex():
43
    datetimeindex = pd.date_range("1/1/2012", periods=24, freq="h")
44
    es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=False)
45
    assert es.timeincrement[1] == 1.0
46
    assert es.timeincrement.sum() == 23
47
48
49
def test_energysystem_interval_warning():
50
    with pytest.warns(FutureWarning):
51
        _ = solph.EnergySystem(timeincrement=[1, 2, 1])
52
53
54
def test_energysystem_with_datetimeindex_non_equidistant_infer_last_interval():
55
    """Test EnergySystem with DatetimeIndex (non-equidistant)"""
56
    dtindex1 = pd.date_range("1/1/2012", periods=24, freq="h")
57
    dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
58
    dtindex = dtindex1.union(dtindex2)
59
    msg = (
60
        "You cannot infer the last interval if the 'freq' attribute of your "
61
        "DatetimeIndex is None."
62
    )
63
    with pytest.raises(AttributeError, match=msg):
64
        solph.EnergySystem(timeindex=dtindex, infer_last_interval=True)
65
66
67
def test_energysystem_with_datetimeindex_non_equidistant():
68
    """Test EnergySystem with DatetimeIndex (non-equidistant)"""
69
    dtindex1 = pd.date_range("1/1/2012", periods=24, freq="h")
70
    dtindex2 = pd.date_range("1/2/2012", periods=49, freq="30min")
71
    dtindex = dtindex1.union(dtindex2)
72
    es = solph.EnergySystem(timeindex=dtindex, infer_last_interval=False)
73
    assert es.timeincrement.sum() == 48.0
74
    assert es.timeincrement[0] == 1
75
    assert es.timeincrement[25] == 0.5
76
77
78
def test_energysystem_with_numeric_index_infer_last_interval():
79
    """Test EnergySystem with numeric index (equidistant)"""
80
    time_increments = [1, 1, 1, 1, 1]
81
    with pytest.warns(
82
        FutureWarning,
83
        match="timeincrement",
84
    ):
85
        es = solph.EnergySystem(timeincrement=time_increments)
86
    assert es.timeincrement[1] == 1.0
87
    assert pd.Series(es.timeincrement).sum() == 5
88
89
90 View Code Duplication
def test_energysystem_with_numeric_index():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
91
    """Test EnergySystem with numeric index (equidistant)"""
92
    time_increments = [1, 1, 1, 1, 1]
93
94
    with pytest.warns(
95
        FutureWarning,
96
        match="timeincrement",
97
    ):
98
        es = solph.EnergySystem(
99
            timeincrement=time_increments,
100
            infer_last_interval=False,
101
        )
102
    assert es.timeincrement[1] == 1.0
103
    assert pd.Series(es.timeincrement).sum() == 5
104
105
106 View Code Duplication
def test_energysystem_with_numeric_index_non_equidistant_infer_last_interval():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
    """
108
    Test EnergySystem with DatetimeIndex (non-equidistant)
109
    'infer_last_interval=True/False' does not have any effect.
110
    """
111
    time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5]
112
113
    with pytest.warns(
114
        FutureWarning,
115
        match="timeincrement",
116
    ):
117
        es = solph.EnergySystem(
118
            timeincrement=time_increments,
119
            infer_last_interval=True,
120
        )
121
    assert pd.Series(es.timeincrement).sum() == 7.0
122
    assert es.timeincrement[0] == 1
123
    assert es.timeincrement[6] == 0.5
124
125
126 View Code Duplication
def test_energysystem_with_numeric_index_non_equidistant():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
127
    """
128
    Test EnergySystem with timeincrement
129
    'infer_last_interval=True/False' does not have any effect.
130
    """
131
    time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5]
132
133
    with pytest.warns(
134
        FutureWarning,
135
        match="timeincrement",
136
    ):
137
        es = solph.EnergySystem(
138
            timeincrement=time_increments,
139
            infer_last_interval=False,
140
        )
141
    assert pd.Series(es.timeincrement).sum() == 7.0
142
    assert es.timeincrement[0] == 1
143
    assert es.timeincrement[8] == 0.25
144
145
146
def test_model_timeincrement_with_valid_timeindex():
147
    datetimeindex = pd.date_range("1/1/2012", periods=5, freq="h")
148
    es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=True)
149
    m = solph._models.Model(es)
150
    assert es.timeincrement.sum() == 5
151
    assert m.timeincrement.sum() == 5
152
    assert m.timeincrement[2] == 1
153
154
155
def test_timeincrement_with_non_valid_timeindex():
156
    with pytest.raises(ValueError, match="Invalid timeindex."):
157
        solph.EnergySystem(timeindex=4)
158
159
160
def test_conflicting_time_index():
161
    msg = (
162
        "Specifying the timeincrement and the timeindex parameter at the same "
163
        "time is not allowed"
164
    )
165
    with pytest.raises(AttributeError, match=msg):
166
        solph.EnergySystem(
167
            timeindex=pd.date_range("1/1/2012", periods=2, freq="h"),
168
            timeincrement=[1, 2, 3, 4],
169
            infer_last_interval=False,
170
        )
171
172
173
def test_missing_timeincrement():
174
    msg = (
175
        "The EnergySystem needs to have a valid 'timeincrement' attribute to "
176
        "build a model."
177
    )
178
    es = solph.EnergySystem()
179
    with pytest.raises(AttributeError, match=msg):
180
        solph.Model(es)
181
182
183
def test_overwrite_timeincrement():
184
    es = solph.EnergySystem(
185
        timeindex=pd.date_range("1/1/2012", periods=2, freq="h"),
186
        infer_last_interval=True,
187
    )
188
    assert es.timeincrement[0] == 1
189
    with pytest.warns(debugging.SuspiciousUsageWarning):
190
        m = solph._models.Model(es, timeincrement=[3])
191
    assert m.timeincrement[0] == 3
192
193
194
def test_model_timeincrement_list():
195
    with pytest.warns(
196
        FutureWarning,
197
        match="timeincrement",
198
    ):
199
        es = solph.EnergySystem(timeincrement=[0.1, 1, 2, 3])
200
    m = solph._models.Model(es)
201
    assert m.timeincrement[3] == 3
202
203
204
def test_nonequ_inconsistent_timeindex():
205
    # with pytest.raises(IndexError):
206
    timeindex_one = pd.date_range("1/1/2019", periods=1, freq="h")
207
    timeindex_hourly = pd.date_range("1/1/2019", periods=3, freq="h")
208
    timeindex_45mins = pd.date_range("1/1/2019", periods=2, freq="45min")
209
    timeindex1 = timeindex_one.append(timeindex_hourly)
210
    timeindex2 = timeindex_hourly.append([timeindex_45mins])
211
    with pytest.raises(TypeError):
212
        solph.EnergySystem(timeindex=timeindex1, infer_last_interval=False)
213
    with pytest.raises(TypeError):
214
        solph.EnergySystem(timeindex=timeindex2, infer_last_interval=False)
215