test_preprocess_data.py
10.1 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
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
import re
import numpy as np
import pytest
from matplotlib import _preprocess_data
from matplotlib.axes import Axes
from matplotlib.testing.decorators import check_figures_equal
# Notes on testing the plotting functions itself
# * the individual decorated plotting functions are tested in 'test_axes.py'
# * that pyplot functions accept a data kwarg is only tested in
# test_axes.test_pie_linewidth_0
# this gets used in multiple tests, so define it here
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
def plot_func(ax, x, y, ls="x", label=None, w="xyz"):
return ("x: %s, y: %s, ls: %s, w: %s, label: %s" % (
list(x), list(y), ls, w, label))
all_funcs = [plot_func]
all_func_ids = ['plot_func']
def test_compiletime_checks():
"""Test decorator invocations -> no replacements."""
def func(ax, x, y): pass
def func_args(ax, x, y, *args): pass
def func_kwargs(ax, x, y, **kwargs): pass
def func_no_ax_args(*args, **kwargs): pass
# this is ok
_preprocess_data(replace_names=["x", "y"])(func)
_preprocess_data(replace_names=["x", "y"])(func_kwargs)
# this has "enough" information to do all the replaces
_preprocess_data(replace_names=["x", "y"])(func_args)
# no positional_parameter_names but needed due to replaces
with pytest.raises(AssertionError):
# z is unknown
_preprocess_data(replace_names=["x", "y", "z"])(func_args)
# no replacements at all -> all ok...
_preprocess_data(replace_names=[], label_namer=None)(func)
_preprocess_data(replace_names=[], label_namer=None)(func_args)
_preprocess_data(replace_names=[], label_namer=None)(func_kwargs)
_preprocess_data(replace_names=[], label_namer=None)(func_no_ax_args)
# label namer is unknown
with pytest.raises(AssertionError):
_preprocess_data(label_namer="z")(func)
with pytest.raises(AssertionError):
_preprocess_data(label_namer="z")(func_args)
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
def test_function_call_without_data(func):
"""Test without data -> no replacements."""
assert (func(None, "x", "y") ==
"x: ['x'], y: ['y'], ls: x, w: xyz, label: None")
assert (func(None, x="x", y="y") ==
"x: ['x'], y: ['y'], ls: x, w: xyz, label: None")
assert (func(None, "x", "y", label="") ==
"x: ['x'], y: ['y'], ls: x, w: xyz, label: ")
assert (func(None, "x", "y", label="text") ==
"x: ['x'], y: ['y'], ls: x, w: xyz, label: text")
assert (func(None, x="x", y="y", label="") ==
"x: ['x'], y: ['y'], ls: x, w: xyz, label: ")
assert (func(None, x="x", y="y", label="text") ==
"x: ['x'], y: ['y'], ls: x, w: xyz, label: text")
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
def test_function_call_with_dict_input(func):
"""Tests with dict input, unpacking via preprocess_pipeline"""
data = {'a': 1, 'b': 2}
assert(func(None, data.keys(), data.values()) ==
"x: ['a', 'b'], y: [1, 2], ls: x, w: xyz, label: None")
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
def test_function_call_with_dict_data(func):
"""Test with dict data -> label comes from the value of 'x' parameter."""
data = {"a": [1, 2], "b": [8, 9], "w": "NOT"}
assert (func(None, "a", "b", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")
assert (func(None, x="a", y="b", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")
assert (func(None, "a", "b", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (func(None, "a", "b", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
assert (func(None, x="a", y="b", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (func(None, x="a", y="b", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
def test_function_call_with_dict_data_not_in_data(func):
"""Test the case that one var is not in data -> half replaces, half kept"""
data = {"a": [1, 2], "w": "NOT"}
assert (func(None, "a", "b", data=data) ==
"x: [1, 2], y: ['b'], ls: x, w: xyz, label: b")
assert (func(None, x="a", y="b", data=data) ==
"x: [1, 2], y: ['b'], ls: x, w: xyz, label: b")
assert (func(None, "a", "b", label="", data=data) ==
"x: [1, 2], y: ['b'], ls: x, w: xyz, label: ")
assert (func(None, "a", "b", label="text", data=data) ==
"x: [1, 2], y: ['b'], ls: x, w: xyz, label: text")
assert (func(None, x="a", y="b", label="", data=data) ==
"x: [1, 2], y: ['b'], ls: x, w: xyz, label: ")
assert (func(None, x="a", y="b", label="text", data=data) ==
"x: [1, 2], y: ['b'], ls: x, w: xyz, label: text")
@pytest.mark.parametrize('func', all_funcs, ids=all_func_ids)
def test_function_call_with_pandas_data(func, pd):
"""Test with pandas dataframe -> label comes from ``data["col"].name``."""
data = pd.DataFrame({"a": np.array([1, 2], dtype=np.int32),
"b": np.array([8, 9], dtype=np.int32),
"w": ["NOT", "NOT"]})
assert (func(None, "a", "b", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")
assert (func(None, x="a", y="b", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")
assert (func(None, "a", "b", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (func(None, "a", "b", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
assert (func(None, x="a", y="b", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (func(None, x="a", y="b", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
def test_function_call_replace_all():
"""Test without a "replace_names" argument, all vars should be replaced."""
data = {"a": [1, 2], "b": [8, 9], "x": "xyz"}
@_preprocess_data(label_namer="y")
def func_replace_all(ax, x, y, ls="x", label=None, w="NOT"):
return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (
list(x), list(y), ls, w, label)
assert (func_replace_all(None, "a", "b", w="x", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")
assert (func_replace_all(None, x="a", y="b", w="x", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: b")
assert (func_replace_all(None, "a", "b", w="x", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (
func_replace_all(None, "a", "b", w="x", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
assert (
func_replace_all(None, x="a", y="b", w="x", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (
func_replace_all(None, x="a", y="b", w="x", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
def test_no_label_replacements():
"""Test with "label_namer=None" -> no label replacement at all."""
@_preprocess_data(replace_names=["x", "y"], label_namer=None)
def func_no_label(ax, x, y, ls="x", label=None, w="xyz"):
return "x: %s, y: %s, ls: %s, w: %s, label: %s" % (
list(x), list(y), ls, w, label)
data = {"a": [1, 2], "b": [8, 9], "w": "NOT"}
assert (func_no_label(None, "a", "b", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: None")
assert (func_no_label(None, x="a", y="b", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: None")
assert (func_no_label(None, "a", "b", label="", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: ")
assert (func_no_label(None, "a", "b", label="text", data=data) ==
"x: [1, 2], y: [8, 9], ls: x, w: xyz, label: text")
def test_more_args_than_pos_parameter():
@_preprocess_data(replace_names=["x", "y"], label_namer="y")
def func(ax, x, y, z=1):
pass
data = {"a": [1, 2], "b": [8, 9], "w": "NOT"}
with pytest.raises(TypeError):
func(None, "a", "b", "z", "z", data=data)
def test_docstring_addition():
@_preprocess_data()
def funcy(ax, *args, **kwargs):
"""Funcy does nothing"""
assert re.search(r"every other argument", funcy.__doc__)
assert not re.search(r"the following arguments", funcy.__doc__)
@_preprocess_data(replace_names=[])
def funcy(ax, x, y, z, bar=None):
"""Funcy does nothing"""
assert not re.search(r"every other argument", funcy.__doc__)
assert not re.search(r"the following arguments", funcy.__doc__)
@_preprocess_data(replace_names=["bar"])
def funcy(ax, x, y, z, bar=None):
"""Funcy does nothing"""
assert not re.search(r"every other argument", funcy.__doc__)
assert not re.search(r"the following arguments .*: \*bar\*\.",
funcy.__doc__)
@_preprocess_data(replace_names=["x", "t"])
def funcy(ax, x, y, z, t=None):
"""Funcy does nothing"""
assert not re.search(r"every other argument", funcy.__doc__)
assert not re.search(r"the following arguments .*: \*x\*, \*t\*\.",
funcy.__doc__)
class TestPlotTypes:
plotters = [Axes.scatter, Axes.bar, Axes.plot]
@pytest.mark.parametrize('plotter', plotters)
@check_figures_equal(extensions=['png'])
def test_dict_unpack(self, plotter, fig_test, fig_ref):
x = [1, 2, 3]
y = [4, 5, 6]
ddict = dict(zip(x, y))
plotter(fig_test.subplots(),
ddict.keys(), ddict.values())
plotter(fig_ref.subplots(), x, y)
@pytest.mark.parametrize('plotter', plotters)
@check_figures_equal(extensions=['png'])
def test_data_kwarg(self, plotter, fig_test, fig_ref):
x = [1, 2, 3]
y = [4, 5, 6]
plotter(fig_test.subplots(), 'xval', 'yval',
data={'xval': x, 'yval': y})
plotter(fig_ref.subplots(), x, y)