-
Notifications
You must be signed in to change notification settings - Fork 115
/
test_feature_rename.py
324 lines (252 loc) · 10.8 KB
/
test_feature_rename.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import copy
import pytest
import numpy as np
import pandas as pd
from numpy import testing as npt
from pyam import IamDataFrame, META_IDX, IAMC_IDX, compare
from pyam.testing import assert_iamframe_equal
from .conftest import META_COLS
RENAME_DF = IamDataFrame(
pd.DataFrame(
[
["model", "scen", "region_a", "test_1", "unit", 1, 5],
["model", "scen", "region_a", "test_2", "unit", 2, 6],
["model", "scen", "region_a", "test_3", "unit", 3, 7],
["model", "scen", "region_b", "test_3", "unit", 4, 8],
],
columns=IAMC_IDX + [2005, 2010],
)
)
# expected output
EXP_RENAME_DF = (
IamDataFrame(
pd.DataFrame(
[
["model", "scen", "region_c", "test", "unit", 4, 12],
["model", "scen", "region_a", "test_2", "unit", 2, 6],
["model", "scen", "region_b", "test_3", "unit", 4, 8],
],
columns=IAMC_IDX + [2005, 2010],
)
)
.data.sort_values(by="region")
.reset_index(drop=True)
)
def test_append_other_scenario(test_df):
other = test_df.filter(scenario="scen_b").rename({"scenario": {"scen_b": "scen_c"}})
test_df.set_meta([0, 1], name="col1")
test_df.set_meta(["a", "b"], name="col2")
other.set_meta(2, name="col1")
other.set_meta("x", name="col3")
df = test_df.append(other)
# check that the original meta dataframe is not updated
obs = test_df.meta.index.get_level_values(1)
npt.assert_array_equal(obs, ["scen_a", "scen_b"])
# assert that merging of meta works as expected
exp = pd.DataFrame(
[
["model_a", "scen_a", False, 0, "a", np.nan],
["model_a", "scen_b", False, 1, "b", np.nan],
["model_a", "scen_c", False, 2, np.nan, "x"],
],
columns=["model", "scenario", "exclude", "col1", "col2", "col3"],
).set_index(["model", "scenario"])
# sort columns for assertion in older pandas versions
df.meta = df.meta.reindex(columns=exp.columns)
pd.testing.assert_frame_equal(df.meta, exp)
# assert that appending data works as expected
ts = df.timeseries()
npt.assert_array_equal(ts.iloc[2].values, ts.iloc[3].values)
def test_append_reconstructed_time(test_df):
# check appending dfs with equal time cols created by different methods
other = test_df.filter(scenario="scen_b").rename({"scenario": {"scen_b": "scen_c"}})
other.time_col = other.time_col[0:1] + other.time_col[1:]
test_df.append(other, inplace=True)
assert "scen_c" in test_df.scenario
def test_append_same_scenario(test_df):
other = test_df.filter(scenario="scen_b").rename(
{"variable": {"Primary Energy": "Primary Energy clone"}}
)
test_df.set_meta([0, 1], name="col1")
other.set_meta(2, name="col1")
other.set_meta("b", name="col2")
# check that non-matching meta raise an error
pytest.raises(ValueError, test_df.append, other=other)
# check that ignoring meta conflict works as expected
df = test_df.append(other, ignore_meta_conflict=True)
# check that the new meta.index is updated, but not the original one
cols = ["exclude"] + META_COLS + ["col1"]
npt.assert_array_equal(test_df.meta.columns, cols)
# assert that merging of meta works as expected
exp = test_df.meta.copy()
exp["col2"] = [np.nan, "b"]
pd.testing.assert_frame_equal(df.meta, exp)
# assert that appending data works as expected
ts = df.timeseries()
npt.assert_array_equal(ts.iloc[2], ts.iloc[3])
@pytest.mark.parametrize("shuffle_cols", [True, False])
def test_append_extra_col(test_df, shuffle_cols):
base_data = test_df.data.copy()
base_data["col_1"] = "hi"
base_data["col_2"] = "bye"
base_df = IamDataFrame(base_data)
other_data = base_data[base_data["variable"] == "Primary Energy"].copy()
other_data["variable"] = "Primary Energy|Gas"
other_df = IamDataFrame(other_data)
if shuffle_cols:
c1_idx = other_df.dimensions.index("col_1")
c2_idx = other_df.dimensions.index("col_2")
other_df.dimensions[c1_idx] = "col_2"
other_df.dimensions[c2_idx] = "col_1"
res = base_df.append(other_df)
def check_meta_is(iamdf, meta_col, val):
for checker in [iamdf.timeseries().reset_index(), iamdf.data]:
meta_vals = checker[meta_col].unique()
assert len(meta_vals) == 1, meta_vals
assert meta_vals[0] == val, meta_vals
# ensure meta merged correctly
check_meta_is(res, "col_1", "hi")
check_meta_is(res, "col_2", "bye")
@pytest.mark.parametrize("inplace", (True, False))
def test_append_duplicates_raises(test_df_year, inplace):
# Merging objects with overlapping values (merge conflict) raises an error
other = copy.deepcopy(test_df_year)
with pytest.raises(ValueError, match="Timeseries data has overlapping values:"):
test_df_year.append(other=other, inplace=inplace)
@pytest.mark.parametrize("inplace", (True, False))
def test_append_incompatible_col_raises(test_pd_df, inplace):
# Merging objects with different data index dimensions raises an error
df = IamDataFrame(test_pd_df)
test_pd_df["foo"] = "baz"
other = IamDataFrame(test_pd_df)
with pytest.raises(ValueError, match="Incompatible timeseries data index"):
df.append(other=other, inplace=inplace)
def test_rename_data_cols_by_dict():
mapping = dict(
variable={"test_1": "test", "test_3": "test"}, region={"region_a": "region_c"}
)
obs = RENAME_DF.rename(mapping, check_duplicates=False).data.reset_index(drop=True)
pd.testing.assert_frame_equal(obs, EXP_RENAME_DF, check_index_type=False)
def test_rename_data_cols_by_kwargs():
args = {
"variable": {"test_1": "test", "test_3": "test"},
"region": {"region_a": "region_c"},
}
obs = RENAME_DF.rename(**args, check_duplicates=False).data.reset_index(drop=True)
pd.testing.assert_frame_equal(obs, EXP_RENAME_DF, check_index_type=False)
def test_rename_data_cols_by_mixed():
args = {
"mapping": {"variable": {"test_1": "test", "test_3": "test"}},
"region": {"region_a": "region_c"},
}
obs = RENAME_DF.rename(**args, check_duplicates=False).data.reset_index(drop=True)
pd.testing.assert_frame_equal(obs, EXP_RENAME_DF, check_index_type=False)
def test_rename_conflict(test_df):
mapping = {"scenario": {"scen_a": "scen_b"}}
pytest.raises(ValueError, test_df.rename, mapping, **mapping)
def test_rename_empty(test_df_year):
"""Check that renaming an empty IamDataFrame does not raise an error"""
empty_df = test_df_year.filter(model="foo")
assert_iamframe_equal(empty_df, empty_df.rename(model={"model_a": "model_b"}))
@pytest.mark.parametrize("append", (False, True))
@pytest.mark.parametrize("inplace", (False, True))
def test_rename_no_change(test_df_year, append, inplace):
"""Check that renaming with an "irrelevant" mapping works as expected"""
df = test_df_year.copy()
mapping = dict(variable={"Primary Energy": "Other Variable"}, unit={"foo": "bar"})
obs = df.rename(**mapping, append=append, inplace=inplace)
if inplace:
assert obs is None
assert_iamframe_equal(df, test_df_year)
else:
assert_iamframe_equal(obs, test_df_year)
def test_rename_index_data_fail(test_df):
mapping = {
"scenario": {"scen_a": "scen_c"},
"variable": {"Primary Energy|Coal": "Primary Energy|Gas"},
}
pytest.raises(ValueError, test_df.rename, mapping)
def test_rename_index_fail_duplicates(test_df):
mapping = {"scenario": {"scen_a": "scen_b"}}
pytest.raises(ValueError, test_df.rename, mapping)
def test_rename_index(test_df):
mapping = {"model": {"model_a": "model_b"}}
obs = test_df.rename(mapping, scenario={"scen_a": "scen_c"})
# test data changes
times = [2005, 2010] if obs.time_col == "year" else obs.data.time.unique()
exp = (
pd.DataFrame(
[
["model_b", "scen_c", "World", "Primary Energy", "EJ/yr", 1, 6.0],
["model_b", "scen_c", "World", "Primary Energy|Coal", "EJ/yr", 0.5, 3],
["model_a", "scen_b", "World", "Primary Energy", "EJ/yr", 2, 7],
],
columns=IAMC_IDX + list(times),
)
.set_index(IAMC_IDX)
.sort_index()
)
if "year" in test_df.data:
exp.columns = list(map(int, exp.columns))
else:
exp.columns = pd.to_datetime(exp.columns)
pd.testing.assert_frame_equal(obs.timeseries().sort_index(), exp)
# test meta changes
exp = pd.DataFrame(
[
["model_b", "scen_c", False, 1, "foo"],
["model_a", "scen_b", False, 2, np.nan],
],
columns=["model", "scenario", "exclude"] + META_COLS,
).set_index(META_IDX)
pd.testing.assert_frame_equal(obs.meta, exp)
def test_rename_append(test_df):
mapping = {"model": {"model_a": "model_b"}, "scenario": {"scen_a": "scen_c"}}
obs = test_df.rename(mapping, append=True)
# test data changes
times = [2005, 2010] if obs.time_col == "year" else obs.data.time.unique()
exp = (
pd.DataFrame(
[
["model_a", "scen_a", "World", "Primary Energy", "EJ/yr", 1, 6.0],
["model_a", "scen_a", "World", "Primary Energy|Coal", "EJ/yr", 0.5, 3],
["model_a", "scen_b", "World", "Primary Energy", "EJ/yr", 2, 7],
["model_b", "scen_c", "World", "Primary Energy", "EJ/yr", 1, 6.0],
["model_b", "scen_c", "World", "Primary Energy|Coal", "EJ/yr", 0.5, 3],
],
columns=IAMC_IDX + list(times),
)
.set_index(IAMC_IDX)
.sort_index()
)
if "year" in test_df.data:
exp.columns = list(map(int, exp.columns))
else:
exp.columns = pd.to_datetime(exp.columns)
pd.testing.assert_frame_equal(obs.timeseries().sort_index(), exp)
# test meta changes
exp = pd.DataFrame(
[
["model_a", "scen_a", False, 1, "foo"],
["model_a", "scen_b", False, 2, np.nan],
["model_b", "scen_c", False, 1, "foo"],
],
columns=["model", "scenario", "exclude"] + META_COLS,
).set_index(META_IDX)
pd.testing.assert_frame_equal(obs.meta, exp)
def test_rename_duplicates():
mapping = {"variable": {"test_1": "test_3"}}
pytest.raises(ValueError, RENAME_DF.rename, **mapping)
obs = RENAME_DF.rename(check_duplicates=False, **mapping)
exp = IamDataFrame(
pd.DataFrame(
[
["model", "scen", "region_a", "test_2", "unit", 2, 6],
["model", "scen", "region_a", "test_3", "unit", 4, 12],
["model", "scen", "region_b", "test_3", "unit", 4, 8],
],
columns=IAMC_IDX + [2005, 2010],
)
)
assert compare(obs, exp).empty
pd.testing.assert_frame_equal(obs.data, exp.data)