Completed
Push — dev ( 68ddc7...49e927 )
by Patrik
58s queued 48s
created

tests.test_time_index   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 26
eloc 117
dl 0
loc 190
rs 10
c 0
b 0
f 0

18 Functions

Rating   Name   Duplication   Size   Complexity  
A test_energysystem_with_datetimeindex_non_equidistant_infer_last_interval() 0 11 2
A test_energysystem_with_numeric_index_infer_last_interval() 0 6 1
A test_energysystem_with_datetimeindex() 0 5 1
A test_energysystem_with_numeric_index() 0 8 1
A test_increment_from_timeindex() 0 7 1
A test_energysystem_with_datetimeindex_non_equidistant() 0 9 1
A test_energysystem_with_numeric_index_non_equidistant_infer_last_interval() 0 13 1
A test_increment_from_numeric_index() 0 5 1
A test_model_timeincrement_list() 0 4 1
A test_energysystem_interval_warning() 0 3 2
A test_nonequ_inconsistent_timeindex() 0 11 3
A test_energysystem_with_numeric_index_non_equidistant() 0 12 1
A test_timeincrement_with_non_valid_timeindex() 0 3 2
A test_overwrite_timeincrement() 0 9 2
A test_model_timeincrement_with_valid_timeindex() 0 7 1
A test_conflicting_time_index() 0 10 2
A test_energysystem_with_datetimeindex_infer_last_interval() 0 6 1
A test_missing_timeincrement() 0 8 2
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
    es = solph.EnergySystem(timeincrement=time_increments)
82
    assert es.timeincrement[1] == 1.0
83
    assert pd.Series(es.timeincrement).sum() == 5
84
85
86
def test_energysystem_with_numeric_index():
87
    """Test EnergySystem with numeric index (equidistant)"""
88
    time_increments = [1, 1, 1, 1, 1]
89
    es = solph.EnergySystem(
90
        timeincrement=time_increments, infer_last_interval=False
91
    )
92
    assert es.timeincrement[1] == 1.0
93
    assert pd.Series(es.timeincrement).sum() == 5
94
95
96
def test_energysystem_with_numeric_index_non_equidistant_infer_last_interval():
97
    """
98
    Test EnergySystem with DatetimeIndex (non-equidistant)
99
    'infer_last_interval=True/False' does not have any effect.
100
    """
101
    time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5]
102
103
    es = solph.EnergySystem(
104
        timeincrement=time_increments, infer_last_interval=True
105
    )
106
    assert pd.Series(es.timeincrement).sum() == 7.0
107
    assert es.timeincrement[0] == 1
108
    assert es.timeincrement[6] == 0.5
109
110
111
def test_energysystem_with_numeric_index_non_equidistant():
112
    """
113
    Test EnergySystem with DatetimeIndex (non-equidistant)
114
    'infer_last_interval=True/False' does not have any effect.
115
    """
116
    time_increments = [1, 1, 1, 1, 1, 0.5, 0.5, 0.25, 0.25, 0.5]
117
    es = solph.EnergySystem(
118
        timeincrement=time_increments, infer_last_interval=False
119
    )
120
    assert pd.Series(es.timeincrement).sum() == 7.0
121
    assert es.timeincrement[0] == 1
122
    assert es.timeincrement[8] == 0.25
123
124
125
def test_model_timeincrement_with_valid_timeindex():
126
    datetimeindex = pd.date_range("1/1/2012", periods=5, freq="h")
127
    es = solph.EnergySystem(timeindex=datetimeindex, infer_last_interval=True)
128
    m = solph._models.Model(es)
129
    assert es.timeincrement.sum() == 5
130
    assert m.timeincrement.sum() == 5
131
    assert m.timeincrement[2] == 1
132
133
134
def test_timeincrement_with_non_valid_timeindex():
135
    with pytest.raises(ValueError, match="Invalid timeindex."):
136
        solph.EnergySystem(timeindex=4)
137
138
139
def test_conflicting_time_index():
140
    msg = (
141
        "Specifying the timeincrement and the timeindex parameter at the same "
142
        "time is not allowed"
143
    )
144
    with pytest.raises(AttributeError, match=msg):
145
        solph.EnergySystem(
146
            timeindex=pd.date_range("1/1/2012", periods=2, freq="h"),
147
            timeincrement=[1, 2, 3, 4],
148
            infer_last_interval=False,
149
        )
150
151
152
def test_missing_timeincrement():
153
    msg = (
154
        "The EnergySystem needs to have a valid 'timeincrement' attribute to "
155
        "build a model."
156
    )
157
    es = solph.EnergySystem()
158
    with pytest.raises(AttributeError, match=msg):
159
        solph.Model(es)
160
161
162
def test_overwrite_timeincrement():
163
    es = solph.EnergySystem(
164
        timeindex=pd.date_range("1/1/2012", periods=2, freq="h"),
165
        infer_last_interval=True,
166
    )
167
    assert es.timeincrement[0] == 1
168
    with pytest.warns(debugging.SuspiciousUsageWarning):
169
        m = solph._models.Model(es, timeincrement=[3])
170
    assert m.timeincrement[0] == 3
171
172
173
def test_model_timeincrement_list():
174
    es = solph.EnergySystem(timeincrement=[0.1, 1, 2, 3])
175
    m = solph._models.Model(es)
176
    assert m.timeincrement[3] == 3
177
178
179
def test_nonequ_inconsistent_timeindex():
180
    # with pytest.raises(IndexError):
181
    timeindex_one = pd.date_range("1/1/2019", periods=1, freq="h")
182
    timeindex_hourly = pd.date_range("1/1/2019", periods=3, freq="h")
183
    timeindex_45mins = pd.date_range("1/1/2019", periods=2, freq="45min")
184
    timeindex1 = timeindex_one.append(timeindex_hourly)
185
    timeindex2 = timeindex_hourly.append([timeindex_45mins])
186
    with pytest.raises(TypeError):
187
        solph.EnergySystem(timeindex=timeindex1, infer_last_interval=False)
188
    with pytest.raises(TypeError):
189
        solph.EnergySystem(timeindex=timeindex2, infer_last_interval=False)
190