Skip to content

Commit 2cdd619

Browse files
committed
Increase coverage
1 parent 8d2a546 commit 2cdd619

File tree

7 files changed

+101
-212
lines changed

7 files changed

+101
-212
lines changed

mplaltair/_axis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def _set_scale_type(channel, ax):
9191
if 'base' in channel.scale:
9292
base = channel.scale['base']
9393

94-
if channel.channel == 'x':
94+
if channel.name == 'x':
9595
ax.set_xscale('log', basex=base)
9696
# lower limit: round down to nearest major tick (using log base change rule)
9797
lims['left'] = base**np.floor(np.log10(channel.data.min())/np.log10(base))

mplaltair/_data.py

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from datetime import datetime
77
import numpy as np
88

9+
910
def _normalize_data(chart):
1011
"""Converts the data to a Pandas dataframe
1112
@@ -39,118 +40,6 @@ def _normalize_data(chart):
3940
chart.data = df
4041

4142

42-
# def _locate_channel_dtype(chart, channel):
43-
# """Locates dtype used for each channel
44-
# Parameters
45-
# ----------
46-
# chart
47-
# The Altair chart
48-
# channel
49-
# The Altair channel being examined
50-
#
51-
# Returns
52-
# -------
53-
# A string representing the data type from the Altair chart ('quantitative', 'ordinal', 'numeric', 'temporal')
54-
# """
55-
#
56-
# channel_val = chart.to_dict()['encoding'][channel]
57-
# if channel_val.get('type'):
58-
# return channel_val.get('type')
59-
# else:
60-
# # TODO: find some way to deal with 'value' so that, opacity, for instance, can be plotted with a value defined
61-
# if channel_val.get('value'):
62-
# raise NotImplementedError
63-
# raise NotImplementedError
64-
65-
66-
# def _locate_channel_data(chart, channel):
67-
# """Locates data used for each channel
68-
#
69-
# Parameters
70-
# ----------
71-
# chart
72-
# The Altair chart
73-
# channel
74-
# The Altair channel being examined
75-
#
76-
# Returns
77-
# -------
78-
# A numpy ndarray containing the data used for the channel
79-
#
80-
# Raises
81-
# ------
82-
# ValidationError
83-
# Raised when the specification does not contain any data attribute
84-
#
85-
# """
86-
#
87-
# channel_val = chart.to_dict()['encoding'][channel]
88-
# if channel_val.get('value'):
89-
# data = channel_val.get('value')
90-
# elif channel_val.get('aggregate'):
91-
# data = _aggregate_channel()
92-
# elif channel_val.get('timeUnit'):
93-
# data = _handle_timeUnit()
94-
# else: # field is required if the above are not present.
95-
# data = chart.data[channel_val.get('field')].values
96-
#
97-
# return data
98-
#
99-
#
100-
# def _aggregate_channel():
101-
# raise NotImplementedError
102-
#
103-
#
104-
# def _handle_timeUnit():
105-
# raise NotImplementedError
106-
#
107-
#
108-
# def _locate_channel_scale(chart, channel):
109-
# """Locates the channel's scale information.
110-
#
111-
# Parameters
112-
# ----------
113-
# chart
114-
# The Altair chart
115-
# channel
116-
# The Altair channel being examined
117-
#
118-
# Returns
119-
# -------
120-
# A dictionary with the scale information
121-
# """
122-
# channel_val = chart.to_dict()['encoding'][channel]
123-
# if channel_val.get('scale'):
124-
# return channel_val.get('scale')
125-
# else:
126-
# return {}
127-
#
128-
#
129-
# def _locate_channel_axis(chart, channel):
130-
# """Locates the channel's scale information.
131-
#
132-
# Parameters
133-
# ----------
134-
# chart
135-
# The Altair chart
136-
# channel
137-
# The Altair channel being examined
138-
#
139-
# Returns
140-
# -------
141-
# A dictionary with the axis information
142-
# """
143-
# channel_val = chart.to_dict()['encoding'][channel]
144-
# if channel_val.get('axis'):
145-
# return channel_val.get('axis')
146-
# else:
147-
# return {}
148-
#
149-
#
150-
# def _locate_channel_field(chart, channel):
151-
# return chart.to_dict()['encoding'][channel]['field']
152-
153-
15443
def _convert_to_mpl_date(data):
15544
"""Converts datetime, datetime64, strings, and Altair DateTime objects to Matplotlib dates.
15645

mplaltair/parse_chart.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ def _locate_channel_data(self, alt_chart):
5454
A numpy ndarray containing the data used for the channel
5555
5656
"""
57-
if not alt_chart.to_dict().get('encoding'):
58-
raise ValueError("Encoding is not provided with the chart specification")
5957

6058
channel_val = alt_chart.to_dict()['encoding'][self.name]
6159
if channel_val.get('value'):
@@ -103,10 +101,8 @@ class ChartMetadata(object):
103101

104102
def __init__(self, alt_chart):
105103

106-
if not alt_chart.to_dict()['mark']:
107-
raise ValueError("Mark not provided")
108104
if not alt_chart.to_dict().get('encoding'):
109-
raise ValueError("Ranged encoding channels like x2, y2 not allowed for Mark: {}".format(alt_chart.mark))
105+
raise ValueError("Encoding is not provided with the chart specification")
110106

111107
_normalize_data(alt_chart)
112108
self.data = alt_chart.data

mplaltair/tests/test_axis.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def test_axis_set_tick_formatter_fail():
5353
from .._axis import _set_tick_formatter
5454
_, ax = plt.subplots()
5555
chart = ChartMetadata(alt.Chart(df_quant).mark_point().encode('a:N', 'c:O'))
56-
_set_tick_formatter(chart.encoding['x'], ax)
5756

5857

5958
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_axis')

mplaltair/tests/test_convert.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def test_encoding_not_provided(): # TODO: move to the parse_chart tests
2727
chart_spec = alt.Chart(df).mark_point()
2828
with pytest.raises(ValueError):
2929
chart = ChartMetadata(chart_spec)
30-
# _convert(chart)
3130

3231
def test_invalid_encodings():
3332
chart_spec = alt.Chart(df).encode(x2='quant').mark_point()
@@ -39,7 +38,6 @@ def test_invalid_encodings():
3938
def test_invalid_temporal(): # TODO: move to parse_chart tests???
4039
chart = alt.Chart(df).mark_point().encode(alt.X('quant:T'))
4140
ChartMetadata(chart)
42-
# _convert(chart)
4341

4442
@pytest.mark.parametrize('channel', ['quant', 'ord', 'nom'])
4543
def test_convert_x_success(channel):
@@ -58,7 +56,6 @@ def test_convert_x_success_temporal(column):
5856
def test_convert_x_fail():
5957
with pytest.raises(KeyError):
6058
chart_spec = ChartMetadata(alt.Chart(df).encode(x='b:N').mark_point())
61-
_convert(chart_spec)
6259

6360
@pytest.mark.parametrize('channel', ['quant', 'ord', 'nom'])
6461
def test_convert_y_success(channel):
@@ -75,7 +72,6 @@ def test_convert_y_success_temporal(column):
7572
def test_convert_y_fail():
7673
with pytest.raises(KeyError):
7774
chart_spec = ChartMetadata(alt.Chart(df).encode(y='b:N').mark_point())
78-
_convert(chart_spec)
7975

8076
@pytest.mark.xfail(raises=ValueError, reason="It doesn't make sense to have x2 and y2 on scatter plots")
8177
def test_quantitative_x2_y2():
@@ -105,10 +101,9 @@ def test_convert_color_success_temporal(column):
105101
mapping = _convert(chart)
106102
assert list(mapping['c']) == list(mdates.date2num(df[column].values))
107103

108-
def test_convert_color_fail(): # TODO: What is this covering?
104+
def test_convert_color_fail():
109105
with pytest.raises(KeyError):
110106
chart_spec = ChartMetadata(alt.Chart(df).encode(color='b:N').mark_point())
111-
_convert(chart_spec)
112107

113108
@pytest.mark.parametrize('channel,type', [('quant', 'Q'), ('ord', 'O')])
114109
def test_convert_fill(channel, type):
@@ -128,10 +123,9 @@ def test_convert_fill_success_temporal(column):
128123
assert list(mapping['c']) == list(mdates.date2num(df[column].values))
129124

130125

131-
def test_convert_fill_fail(): # TODO: what is this covering?
126+
def test_convert_fill_fail():
132127
with pytest.raises(KeyError):
133128
chart_spec = ChartMetadata(alt.Chart(df).encode(fill='b:N').mark_point())
134-
_convert(chart_spec)
135129

136130
@pytest.mark.xfail(raises=NotImplementedError, reason="The marker argument in scatter() cannot take arrays")
137131
def test_quantitative_shape():
@@ -141,13 +135,12 @@ def test_quantitative_shape():
141135
@pytest.mark.xfail(raises=NotImplementedError, reason="The marker argument in scatter() cannot take arrays")
142136
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
143137
def test_convert_shape_fail_temporal(column):
144-
chart = ChartMetadata(alt.Chart(df).mark_point().encode(alt.Shape(column)))
145-
mapping = _convert(chart)
138+
chart = alt.Chart(df).mark_point().encode(alt.Shape(column))
139+
convert(chart)
146140

147141
@pytest.mark.xfail(raises=NotImplementedError, reason="Merge: the dtype for opacity isn't assumed to be quantitative")
148142
def test_quantitative_opacity_value():
149143
chart = ChartMetadata(alt.Chart(df_quant).mark_point().encode(opacity=alt.value(.5)))
150-
mapping = _convert(chart)
151144

152145
@pytest.mark.xfail(raises=NotImplementedError, reason="The alpha argument in scatter() cannot take arrays")
153146
def test_quantitative_opacity_array():
@@ -174,7 +167,6 @@ def test_convert_size_success_nominal():
174167
def test_convert_size_fail():
175168
with pytest.raises(KeyError):
176169
chart_spec = ChartMetadata(alt.Chart(df).encode(size='b:N').mark_point())
177-
_convert(chart_spec)
178170

179171
@pytest.mark.xfail(raises=NotImplementedError, reason="Dates would need to be normalized for the size.")
180172
@pytest.mark.parametrize("column", ["years", "months", "days", "hrs", "combination"])
@@ -201,12 +193,11 @@ def test_convert_stroke_fail_temporal(column):
201193
def test_quantitative_x_count_y():
202194
df_count = pd.DataFrame({"a": [1, 1, 2, 3, 5], "b": [1.4, 1.4, 2.9, 3.18, 5.3]})
203195
chart = ChartMetadata(alt.Chart(df_count).mark_point().encode(alt.X('a'), alt.Y('count()')))
204-
mapping = _convert(chart)
196+
205197

206198
@pytest.mark.xfail(raises=NotImplementedError, reason="specifying timeUnit is not supported yet")
207199
def test_timeUnit():
208200
chart = ChartMetadata(alt.Chart(df).mark_point().encode(alt.X('date(combination)')))
209-
_convert(chart)
210201

211202
# Plots
212203

@@ -279,7 +270,6 @@ def test_line_color(self):
279270
)
280271
fig, _ = convert(chart)
281272
return fig
282-
# plt.show()
283273

284274
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_convert')
285275
@pytest.mark.parametrize('o', ['d:Q', 'c:O', 'dates:T'])
@@ -291,7 +281,6 @@ def test_line_opacity(self, o):
291281
)
292282
fig, ax = convert(chart)
293283
return fig
294-
# plt.show()
295284

296285
@pytest.mark.mpl_image_compare(baseline_dir='baseline_images/test_convert')
297286
@pytest.mark.parametrize('c,o', [('d:Q', 'd:Q'), ('c:N', 'c:O'), ('dates:T', 'dates:T')])

mplaltair/tests/test_data.py

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"quantitative": [1.1, 2.1, 3.1, 4.1, 5.1]
1818
})
1919

20+
2021
def test_data_list():
2122
chart = alt.Chart(pd.DataFrame({'a': [1], 'b': [2], 'c': [3]})).mark_point()
2223
_normalize_data(chart)
@@ -27,83 +28,6 @@ def test_data_url():
2728
_normalize_data(chart)
2829
assert type(chart.data) == pd.DataFrame
2930

30-
# _locate_channel_data() tests
31-
32-
@pytest.mark.parametrize("column, dtype", [
33-
('a', 'quantitative'), ('b', 'quantitative'), ('c', 'quantitative'), ('combination', 'temporal')
34-
])
35-
def test_data_field_quantitative(column, dtype):
36-
chart = alt.Chart(df).mark_point().encode(alt.X(field=column, type=dtype))
37-
for channel in chart.to_dict()['encoding']:
38-
data = _data._locate_channel_data(chart, channel)
39-
assert list(data) == list(df[column].values)
40-
41-
42-
@pytest.mark.parametrize("column", ['a', 'b', 'c'])
43-
def test_data_shorthand_quantitative(column):
44-
chart = alt.Chart(df).mark_point().encode(alt.X(column))
45-
for channel in chart.to_dict()['encoding']:
46-
data = _data._locate_channel_data(chart, channel)
47-
assert list(data) == list(df[column].values)
48-
49-
50-
def test_data_shorthand_temporal():
51-
chart = alt.Chart(df).mark_point().encode(alt.X('combination'))
52-
for channel in chart.to_dict()['encoding']:
53-
data = _data._locate_channel_data(chart, channel)
54-
assert list(data) == list(df['combination'].values)
55-
56-
57-
def test_data_value_quantitative():
58-
chart = alt.Chart(df).mark_point().encode(opacity=alt.value(0.5))
59-
for channel in chart.to_dict()['encoding']:
60-
data = _data._locate_channel_data(chart, channel)
61-
assert list(data) == list(_data._convert_to_mpl_date(df['combination'].values))
62-
63-
64-
@pytest.mark.parametrize("column", ['a', 'b', 'c'])
65-
def test_data_aggregate_quantitative_fail(column):
66-
""""'Passes' if it raises a NotImplementedError"""
67-
chart = alt.Chart(df).mark_point().encode(alt.X(field=column, type='quantitative', aggregate='average'))
68-
for channel in chart.to_dict()['encoding']:
69-
with pytest.raises(NotImplementedError):
70-
data = _data._locate_channel_data(chart, channel)
71-
72-
73-
def test_data_timeUnit_shorthand_temporal_fail():
74-
chart = alt.Chart(df).mark_point().encode(alt.X('month(combination):T'))
75-
for channel in chart.to_dict()['encoding']:
76-
with pytest.raises(NotImplementedError):
77-
data = _data._locate_channel_data(chart, channel)
78-
79-
80-
def test_data_timeUnit_field_temporal_fail():
81-
""""'Passes' if it raises a NotImplementedError"""
82-
chart = alt.Chart(df).mark_point().encode(alt.X(field='combination', type='temporal', timeUnit='month'))
83-
for channel in chart.to_dict()['encoding']:
84-
with pytest.raises(NotImplementedError):
85-
data = _data._locate_channel_data(chart, channel)
86-
87-
88-
# _locate_channel_dtype() tests
89-
90-
@pytest.mark.parametrize('column, expected', [
91-
('a:Q', 'quantitative'), ('nom:N', 'nominal'), ('ord:O', 'ordinal'), ('combination:T', 'temporal')
92-
])
93-
def test_data_dtype(column, expected):
94-
chart = alt.Chart(df).mark_point().encode(alt.X(column))
95-
for channel in chart.to_dict()['encoding']:
96-
dtype = _data._locate_channel_dtype(chart, channel)
97-
assert dtype == expected
98-
99-
100-
def test_data_dtype_fail():
101-
""""'Passes' if it raises a NotImplementedError"""
102-
chart = alt.Chart(df).mark_point().encode(opacity=alt.value(.5))
103-
for channel in chart.to_dict()['encoding']:
104-
with pytest.raises(NotImplementedError):
105-
dtype = _data._locate_channel_dtype(chart, channel)
106-
10731
# test date conversion:
10832

10933
df_nonstandard = pd.DataFrame({

0 commit comments

Comments
 (0)