Skip to content

Commit 7bcab2d

Browse files
authored
Merge pull request #19 from palnabarun/reintroduce-utils
Reintroduce utils and related functions
2 parents a4f7dbc + 7c1e98f commit 7bcab2d

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

mplaltair/_convert.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import matplotlib.dates as mdates
2-
import numpy as np
3-
from ._data import _locate_channel_data, _locate_channel_dtype, _convert_to_mpl_date
2+
from ._data import _locate_channel_data, _locate_channel_dtype, _convert_to_mpl_date, _normalize_data
43

54
def _allowed_ranged_marks(enc_channel, mark):
65
"""TODO: DOCS
@@ -110,6 +109,8 @@ def _convert(chart):
110109
"""
111110
mapping = {}
112111

112+
_normalize_data(chart)
113+
113114
if not chart.to_dict().get('encoding'):
114115
raise ValueError("Encoding not provided with the chart specification")
115116

mplaltair/_data.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,42 @@
44
from datetime import datetime
55
import numpy as np
66

7+
import pandas as pd
8+
9+
from ._utils import _fetch
10+
11+
def _normalize_data(chart):
12+
"""Converts the data to a Pandas dataframe
13+
14+
Parameters
15+
----------
16+
chart : altair.Chart
17+
The vega-lite specification in json format
18+
19+
Returns
20+
-------
21+
None
22+
23+
Raises
24+
------
25+
ValidationError
26+
Raised when the specification does not contain any data attribute
27+
28+
NotImplementedError
29+
Raised when the data specification has an unsupported data source
30+
"""
31+
32+
spec = chart.to_dict()
33+
34+
if spec['data'].get('url'):
35+
df = pd.DataFrame(_fetch(spec['data']['url']))
36+
elif spec['data'].get('values'):
37+
return
38+
else:
39+
raise NotImplementedError('Given data specification is unsupported at the moment.')
40+
41+
chart.data = df
42+
743
def _locate_channel_dtype(chart, channel):
844
"""Locates dtype used for each channel
945
Parameters

mplaltair/_utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from urllib.request import urlopen
2+
from urllib.error import HTTPError
3+
4+
import pandas as pd
5+
6+
_PD_READERS = {
7+
'json': pd.read_json,
8+
'csv': pd.read_csv
9+
}
10+
11+
def _get_format(url):
12+
"""Gives back the format of the file from url
13+
14+
WARNING: It might break. Trying to find a better way.
15+
"""
16+
return url.split('.')[-1]
17+
18+
def _fetch(url):
19+
"""Downloads the file from the given url as a Pandas DataFrame
20+
21+
Parameters
22+
----------
23+
url : string
24+
URL of the file to be downloaded
25+
26+
Returns
27+
-------
28+
pd.DataFrame
29+
Data in the format of a DataFrame
30+
31+
Raises
32+
------
33+
NotImplementedError
34+
Raises when an unsupported file format is given as an URL
35+
"""
36+
try:
37+
ext = _get_format(url)
38+
reader = _PD_READERS[ext]
39+
df = reader(urlopen(url).read())
40+
except KeyError:
41+
raise NotImplementedError('File format not implemented')
42+
return df

mplaltair/tests/test_data.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import matplotlib.dates as mdates
44
import mplaltair._data as _data
55
import pytest
6+
from vega_datasets import data
7+
8+
from mplaltair._data import _normalize_data
9+
from mplaltair._exceptions import ValidationError
610

711
df = pd.DataFrame({
812
"a": [1, 2, 3, 4, 5], "b": [1.1, 2.2, 3.3, 4.4, 5.5], "c": [1, 2.2, 3, 4.4, 5],
@@ -13,6 +17,15 @@
1317
"quantitative": [1.1, 2.1, 3.1, 4.1, 5.1]
1418
})
1519

20+
def test_data_list():
21+
chart = alt.Chart(pd.DataFrame({'a': [1], 'b': [2], 'c': [3]})).mark_point()
22+
_normalize_data(chart)
23+
assert type(chart.data) == pd.DataFrame
24+
25+
def test_data_url():
26+
chart = alt.Chart(data.cars.url).mark_point()
27+
_normalize_data(chart)
28+
assert type(chart.data) == pd.DataFrame
1629

1730
# _locate_channel_data() tests
1831

mplaltair/tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
import pandas as pd
3+
from vega_datasets import data
4+
5+
from mplaltair import _utils
6+
7+
def test_get_format():
8+
assert _utils._get_format(data.cars.url) == 'json'
9+
10+
def test_fetch_success():
11+
assert type(_utils._fetch(data.cars.url)) == pd.DataFrame
12+
13+
def test_fetch_error():
14+
with pytest.raises(NotImplementedError):
15+
_utils._fetch('https://test.tld/dataset.tsv')

0 commit comments

Comments
 (0)