Skip to content

Commit 9ef83f4

Browse files
committed
Combine test_convert_quantitative.py with test_convert.py
1 parent 7539c70 commit 9ef83f4

File tree

2 files changed

+113
-146
lines changed

2 files changed

+113
-146
lines changed

mplaltair/tests/test_convert.py

Lines changed: 113 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@
77

88
from .._convert import convert
99

10-
df = pd.DataFrame({'quant': [1, 1.5, 2], 'ord': [0, 1, 2], 'nom': ['A', 'B', 'C']})
1110

12-
df_temporal = pd.DataFrame({
11+
df = pd.DataFrame({
12+
'quant': [1, 1.5, 2, 2.5, 3], 'ord': [0, 1, 2, 3, 4], 'nom': ['A', 'B', 'C', 'D', 'E'],
1313
"years": pd.date_range('01/01/2015', periods=5, freq='Y'), "months": pd.date_range('1/1/2015', periods=5, freq='M'),
1414
"days": pd.date_range('1/1/2015', periods=5, freq='D'), "hrs": pd.date_range('1/1/2015', periods=5, freq='H'),
1515
"combination": pd.to_datetime(['1/1/2015', '1/1/2015 10:00:00', '1/2/2015 00:00', '1/4/2016 10:00', '5/1/2016']),
1616
"quantitative": [1.1, 2.1, 3.1, 4.1, 5.1]
1717
})
1818

19+
df_quant = pd.DataFrame({
20+
"a": [1, 2, 3], "b": [1.2, 2.4, 3.8], "c": [7, 5, 3],
21+
"s": [50, 100, 200.0], "alpha": [.1, .5, .8], "shape": [1, 2, 3], "fill": [1, 2, 3]
22+
})
23+
1924
@pytest.mark.parametrize('channel', ['quant', 'ord', 'nom'])
2025
def test_convert_x_success(channel):
2126
chart_spec = alt.Chart(df).encode(x=channel).mark_point()
2227
mapping = convert(chart_spec)
2328
assert list(mapping['x']) == list(df[channel].values)
2429

30+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
31+
def test_convert_x_success_temporal(column):
32+
chart = alt.Chart(df).mark_point().encode(alt.X(column))
33+
mapping = convert(chart)
34+
assert list(mapping['x']) == list(mdates.date2num(df[column].values))
35+
2536
def test_convert_x_fail():
2637
chart_spec = alt.Chart(df).encode(x='b:N').mark_point()
2738
with pytest.raises(KeyError):
@@ -33,11 +44,29 @@ def test_convert_y_success(channel):
3344
mapping = convert(chart_spec)
3445
assert list(mapping['y']) == list(df[channel].values)
3546

47+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
48+
def test_convert_y_success_temporal(column):
49+
chart = alt.Chart(df).mark_point().encode(alt.Y(column))
50+
mapping = convert(chart)
51+
assert list(mapping['y']) == list(mdates.date2num(df[column].values))
52+
3653
def test_convert_y_fail():
3754
chart_spec = alt.Chart(df).encode(y='b:N').mark_point()
3855
with pytest.raises(KeyError):
3956
convert(chart_spec)
4057

58+
#TODO: x2, y2
59+
@pytest.mark.xfail(raises=NotImplementedError, reason="It doesn't make sense to have x2 and y2 on scatter plots")
60+
def test_quantitative_x2_y2():
61+
chart = alt.Chart(df_quant).mark_point().encode(alt.X('a'), alt.Y('b'), alt.X2('c'), alt.Y2('alpha'))
62+
convert(chart)
63+
64+
@pytest.mark.xfail(raises=NotImplementedError)
65+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
66+
def test_convert_x2_y2_fail_temporal(column):
67+
chart = alt.Chart(df).mark_point().encode(alt.X2(column), alt.Y2(column))
68+
convert(chart)
69+
4170
@pytest.mark.parametrize('channel', ['quant', 'ord'])
4271
def test_convert_color_success(channel):
4372
chart_spec = alt.Chart(df).encode(color=channel).mark_point()
@@ -49,6 +78,12 @@ def test_convert_color_success_nominal():
4978
with pytest.raises(NotImplementedError):
5079
convert(chart_spec)
5180

81+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
82+
def test_convert_color_success_temporal(column):
83+
chart = alt.Chart(df).mark_point().encode(alt.Color(column))
84+
mapping = convert(chart)
85+
assert list(mapping['c']) == list(mdates.date2num(df[column].values))
86+
5287
def test_convert_color_fail():
5388
chart_spec = alt.Chart(df).encode(color='b:N').mark_point()
5489
with pytest.raises(KeyError):
@@ -65,11 +100,49 @@ def test_convert_fill_success_nominal():
65100
with pytest.raises(NotImplementedError):
66101
convert(chart_spec)
67102

103+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
104+
def test_convert_fill_success_temporal(column):
105+
chart = alt.Chart(df).mark_point().encode(alt.Fill(column))
106+
mapping = convert(chart)
107+
assert list(mapping['c']) == list(mdates.date2num(df[column].values))
108+
68109
def test_convert_fill_fail():
69110
chart_spec = alt.Chart(df).encode(fill='b:N').mark_point()
70111
with pytest.raises(KeyError):
71112
convert(chart_spec)
72113

114+
# TODO: shape
115+
@pytest.mark.xfail(raises=NotImplementedError, reason="The marker argument in scatter() cannot take arrays")
116+
def test_quantitative_shape():
117+
chart = alt.Chart(df_quant).mark_point().encode(alt.Shape('shape'))
118+
mapping = convert(chart)
119+
assert list(mapping['marker']) == list(df_quant['shape'].values)
120+
121+
@pytest.mark.xfail(raises=NotImplementedError, reason="The marker argument in scatter() cannot take arrays")
122+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
123+
def test_convert_shape_fail_temporal(column):
124+
chart = alt.Chart(df).mark_point().encode(alt.Shape(column))
125+
mapping = convert(chart)
126+
assert list(mapping['s']) == list(mdates.date2num(df[column].values))
127+
128+
# TODO: Opacity
129+
@pytest.mark.xfail(raises=NotImplementedError, reason="Merge: the dtype for opacity isn't assumed to be quantitative")
130+
def test_quantitative_opacity_value():
131+
chart = alt.Chart(df_quant).mark_point().encode(opacity=alt.value(.5))
132+
mapping = convert(chart)
133+
assert mapping['alpha'] == 0.5
134+
135+
@pytest.mark.xfail(raises=NotImplementedError, reason="The alpha argument in scatter() cannot take arrays")
136+
def test_quantitative_opacity_array():
137+
chart = alt.Chart(df_quant).mark_point().encode(alt.Opacity('alpha'))
138+
convert(chart)
139+
140+
@pytest.mark.xfail(raises=NotImplementedError, reason="The alpha argument in scatter() cannot take arrays")
141+
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
142+
def test_convert_opacity_fail_temporal(column):
143+
chart = alt.Chart(df).mark_point().encode(alt.Opacity(column))
144+
convert(chart)
145+
73146
@pytest.mark.parametrize('channel', ['quant', 'ord'])
74147
def test_convert_size_success(channel):
75148
chart_spec = alt.Chart(df).encode(size=channel).mark_point()
@@ -86,73 +159,60 @@ def test_convert_size_fail():
86159
with pytest.raises(KeyError):
87160
convert(chart_spec)
88161

89-
90-
# Temporal tests
91-
162+
@pytest.mark.xfail(raises=NotImplementedError, reason="Dates would need to be normalized for the size.")
92163
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
93-
def test_x_temporal_success(column):
94-
chart = alt.Chart(df_temporal).mark_point().encode(alt.X(column))
95-
mapping = convert(chart)
96-
assert list(mapping['x']) == list(mdates.date2num(df_temporal[column].values))
164+
def test_convert_size_fail_temporal(column):
165+
chart = alt.Chart(df).mark_point().encode(alt.Size(column))
166+
convert(chart)
97167

98-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
99-
def test_y_temporal_success(column):
100-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Y(column))
101-
mapping = convert(chart)
102-
assert list(mapping['y']) == list(mdates.date2num(df_temporal[column].values))
103168

104-
@pytest.mark.xfail(raises=NotImplementedError)
105-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
106-
def test_x2_y2_temporal_fail(column):
107-
chart = alt.Chart(df_temporal).mark_point().encode(alt.X2(column), alt.Y2(column))
169+
# TODO: Stroke
170+
@pytest.mark.xfail(raises=NotImplementedError, reason="Stroke is not well supported in Altair")
171+
def test_quantitative_stroke():
172+
chart = alt.Chart(df_quant).mark_point().encode(alt.Stroke('fill'))
108173
convert(chart)
109174

175+
@pytest.mark.xfail(raises=NotImplementedError, reason="Stroke is not well defined in Altair")
110176
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
111-
def test_color_temporal_success(column):
112-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Color(column))
113-
mapping = convert(chart)
114-
assert list(mapping['c']) == list(mdates.date2num(df_temporal[column].values))
177+
def test_convert_stroke_fail_temporal(column):
178+
chart = alt.Chart(df).mark_point().encode(alt.Stroke(column))
179+
convert(chart)
115180

116-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
117-
def test_fill_temporal_success(column):
118-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Fill(column))
119-
mapping = convert(chart)
120-
assert list(mapping['c']) == list(mdates.date2num(df_temporal[column].values))
121181

122-
@pytest.mark.xfail(raises=NotImplementedError, reason="The alpha argument in scatter() cannot take arrays")
123-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
124-
def test_opacity_temporal_fail(column):
125-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Opacity(column))
126-
convert(chart)
182+
# Aggregations
127183

128-
@pytest.mark.xfail(raises=NotImplementedError, reason="The marker argument in scatter() cannot take arrays")
129-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
130-
def test_temporal_shape_fail(column):
131-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Shape(column))
184+
@pytest.mark.xfail(raises=NotImplementedError, reason="Aggregate functions are not supported yet")
185+
def test_quantitative_x_count_y():
186+
df_count = pd.DataFrame({"a": [1, 1, 2, 3, 5], "b": [1.4, 1.4, 2.9, 3.18, 5.3]})
187+
chart = alt.Chart(df_count).mark_point().encode(alt.X('a'), alt.Y('count()'))
132188
mapping = convert(chart)
133-
assert list(mapping['s']) == list(mdates.date2num(df_temporal[column].values))
189+
assert list(mapping['x']) == list(df_count['a'].values)
190+
assert list(mapping['y']) == list(df_count.groupby(['a']).count().values)
134191

135-
@pytest.mark.xfail(raises=NotImplementedError, reason="Dates would need to be normalized for the size.")
136-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
137-
def test_size_temporal_fail(column):
138-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Size(column))
192+
@pytest.mark.xfail(raises=NotImplementedError, reason="specifying timeUnit is not supported yet")
193+
def test_timeUnit():
194+
chart = alt.Chart(df).mark_point().encode(alt.X('date(combination)'))
139195
convert(chart)
140196

141-
@pytest.mark.xfail(raises=NotImplementedError, reason="Stroke is not well defined in Altair")
142-
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
143-
def test_stroke_temporal_fail(column):
144-
chart = alt.Chart(df_temporal).mark_point().encode(alt.Stroke(column))
145-
convert(chart)
197+
# Plots
198+
199+
chart_quant = alt.Chart(df_quant).mark_point().encode(
200+
alt.X(field='a', type='quantitative'), alt.Y('b'), alt.Color('c:Q'), alt.Size('s')
201+
)
202+
chart_fill_quant = alt.Chart(df_quant).mark_point().encode(
203+
alt.X(field='a', type='quantitative'), alt.Y('b'), alt.Fill('fill:Q')
204+
)
205+
206+
@pytest.mark.parametrize("chart", [chart_quant, chart_fill_quant])
207+
def test_quantitative_scatter(chart):
208+
mapping = convert(chart)
209+
plt.scatter(**mapping)
210+
plt.show()
146211

147212
@pytest.mark.parametrize("channel", [alt.Color("years"), alt.Fill("years")])
148213
def test_scatter_temporal(channel):
149-
chart = alt.Chart(df_temporal).mark_point().encode(alt.X("years"), channel)
214+
chart = alt.Chart(df).mark_point().encode(alt.X("years"), channel)
150215
mapping = convert(chart)
151-
mapping['y'] = df_temporal['quantitative'].values
216+
mapping['y'] = df['quantitative'].values
152217
plt.scatter(**mapping)
153218
plt.show()
154-
155-
@pytest.mark.xfail(raises=NotImplementedError, reason="specifying timeUnit is not supported yet")
156-
def test_timeUnit():
157-
chart = alt.Chart(df_temporal).mark_point().encode(alt.X('date(combination)'))
158-
convert(chart)

mplaltair/tests/test_convert_quantitative.py

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)