-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathtest_variables.py
More file actions
150 lines (107 loc) · 4.12 KB
/
test_variables.py
File metadata and controls
150 lines (107 loc) · 4.12 KB
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
#!/usr/bin/env python3
"""
This module aims at testing the correct behavior of the Variables class.
"""
import warnings
from datetime import datetime
import numpy as np
import pandas as pd
import pytest
import xarray as xr
import xarray.core.indexes
import xarray.core.utils
from pytz import UTC
import linopy
from linopy import Model
from linopy.common import CoordAlignWarning
from linopy.testing import assert_varequal
from linopy.variables import ScalarVariable
@pytest.fixture
def m() -> Model:
m = Model()
m.add_variables(coords=[pd.RangeIndex(10, name="first")], name="x")
m.add_variables(coords=[pd.Index([1, 2, 3], name="second")], name="y")
m.add_variables(0, 10, name="z")
return m
def test_variables_repr(m: Model) -> None:
m.variables.__repr__()
def test_variables_inherited_properties(m: Model) -> None:
assert isinstance(m.variables.attrs, dict)
assert isinstance(m.variables.coords, xr.Coordinates)
assert isinstance(m.variables.indexes, xarray.core.indexes.Indexes)
assert isinstance(m.variables.sizes, xarray.core.utils.Frozen)
def test_variables_getattr_formatted() -> None:
m = Model()
m.add_variables(name="y-0")
assert_varequal(m.variables.y_0, m.variables["y-0"])
def test_variables_assignment_with_merge() -> None:
"""
Test the merger of a variables with same dimension name but with different
lengths.
New coordinates are aligned to the existing ones. Thus this should
raise a warning.
"""
m = Model()
upper = pd.Series(np.ones(10))
var0 = m.add_variables(upper)
upper = pd.Series(np.ones(12))
var1 = m.add_variables(upper)
with pytest.warns(UserWarning):
assert m.variables.labels.var0[-1].item() == -1
assert_varequal(var0, m.variables.var0)
assert_varequal(var1, m.variables.var1)
def test_variables_assignment_with_reindex(m: Model) -> None:
shuffled_coords = [pd.Index([2, 1, 3, 4, 6, 5, 7, 9, 8, 0], name="first")]
m.add_variables(coords=shuffled_coords, name="a")
with pytest.warns(UserWarning):
m.variables.labels
for dtype in m.variables.labels.dtypes.values():
assert np.issubdtype(dtype, np.integer)
for dtype in m.variables.lower.dtypes.values():
assert np.issubdtype(dtype, np.floating)
for dtype in m.variables.upper.dtypes.values():
assert np.issubdtype(dtype, np.floating)
def test_scalar_variables_name_counter() -> None:
m = Model()
m.add_variables()
m.add_variables()
assert "var0" in m.variables
assert "var1" in m.variables
def test_variables_binaries(m: Model) -> None:
assert isinstance(m.binaries, linopy.variables.Variables)
def test_variables_integers(m: Model) -> None:
assert isinstance(m.integers, linopy.variables.Variables)
def test_variables_nvars(m: Model) -> None:
assert m.variables.nvars == 14
idx = pd.RangeIndex(10, name="first")
mask = pd.Series([True] * 5 + [False] * 5, idx)
m.add_variables(coords=[idx], mask=mask)
assert m.variables.nvars == 19
def test_variables_get_name_by_label(m: Model) -> None:
assert m.variables.get_name_by_label(4) == "x"
assert m.variables.get_name_by_label(12) == "y"
with pytest.raises(ValueError):
m.variables.get_name_by_label(30)
with pytest.raises(ValueError):
m.variables.get_name_by_label("anystring") # type: ignore
def test_scalar_variable(m: Model) -> None:
x = ScalarVariable(label=0, model=m)
assert isinstance(x, ScalarVariable)
assert x.__rmul__(x) is NotImplemented # type: ignore
def test_timezone_alignment_with_multiplication() -> None:
utc_index = pd.date_range(
start=datetime(2025, 1, 1),
freq="15min",
periods=4,
tz=UTC,
name="time",
)
model = Model()
series1 = pd.Series(index=utc_index, data=1.0)
var1 = model.add_variables(coords=[utc_index], name="var1")
with warnings.catch_warnings():
warnings.simplefilter("error", CoordAlignWarning)
expr = var1 * series1
index: pd.DatetimeIndex = expr.coords["time"].to_index()
assert index.equals(utc_index)
assert index.tzinfo is UTC