Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vega-Lite Auto Examples #107

Merged
merged 5 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions altair/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
"""Code for Example Plots"""
import os
import json

JSON_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), 'json'))

def load_example(name):
import os
import json

JSON_DIR = os.path.join(os.path.dirname(__file__), 'json')
if name not in os.listdir(JSON_DIR):
raise ValueError("Example='{0}' not valid.".format(name))
def load_example(filename):
"""Load the JSON string corresponding to the given filename"""
if filename not in os.listdir(JSON_DIR):
raise ValueError("Example='{0}' not valid.".format(filename))

with open(os.path.join(JSON_DIR, name)) as f:
schema = json.load(f)
with open(os.path.join(JSON_DIR, filename)) as f:
return json.load(f)

return schema

def iter_examples():
for filename in os.listdir(JSON_DIR):
yield filename, load_example(filename)
12 changes: 0 additions & 12 deletions altair/examples/scatterplot.py

This file was deleted.

12 changes: 0 additions & 12 deletions altair/examples/scatterplot_with_filled_circles.py

This file was deleted.

17 changes: 0 additions & 17 deletions altair/examples/simple_bar_chart.py

This file was deleted.

45 changes: 4 additions & 41 deletions altair/examples/tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,19 @@

import pytest

from .. import iter_examples
from ...datasets import connection_ok, URLError
from ... import examples
from ... import *


def iter_submodules(package, include_packages=False):
"""Iterate importable submodules of a module"""
prefix = package.__name__ + "."
for importer, modname, ispkg in pkgutil.iter_modules(package.__path__,
prefix):
if not include_packages and ispkg:
continue
yield modname


def iter_json_examples():
json_dir = os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', 'json'))
for json_file in os.listdir(json_dir):
yield os.path.join(json_dir, json_file)


@pytest.mark.skipif(not connection_ok(), reason='No Internet Connection')
@pytest.mark.parametrize('modname', iter_submodules(examples))
def test_examples_output(modname):
# Don't compare data here, as the JSON representation is tricky
example = __import__(modname, fromlist="dummy")
v = example.v
vdict = v.to_dict(data=False)

# test that output matches expected output
assert vdict == example.expected_output

# test from_dict methods
v2 = Layer.from_dict(example.expected_output)
assert v2.to_dict() == vdict

# test to_altair methods
v3 = eval(v.to_altair())
assert v3.to_dict() == vdict


@pytest.mark.skipif(not connection_ok(), reason='No Internet Connection')
@pytest.mark.parametrize('json_path', iter_json_examples())
def test_json_examples_round_trip(json_path):
@pytest.mark.parametrize('example', iter_examples())
def test_json_examples_round_trip(example):
"""
Test that Altair correctly round-trips JSON with to_dict() and to_altair()
"""
with open(json_path) as f:
json_dict = json.load(f)
filename, json_dict = example

v = Layer.from_dict(json_dict)
v_dict = v.to_dict()
Expand Down
27 changes: 0 additions & 27 deletions altair/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@
from .. import *
from ..utils import parse_shorthand, infer_vegalite_type

EXAMPLE_JSON = """
{
"data": {"url": "http://vega.github.io/vega-lite/data/unemployment-across-industries.json"},
"mark": "area",
"encoding": {
"x": {
"timeUnit": "yearmonth", "field": "date", "type": "temporal",
"scale": {"nice": "month"},
"axis": {"axisWidth": 0, "format": "%Y", "labelAngle": 0, "tickSize": 0}
},
"y": {
"aggregate": "sum", "field": "count","type": "quantitative",
"axis": false
},
"color": {"field":"series", "type":"nominal", "scale":{"range": "category20b"}}
},
"config": {"cell": {"width": 300, "height": 200}, "mark": {"stacked": "center"}}
}
"""


def test_encode_update():
# Test that encode updates rather than overwrites
Expand Down Expand Up @@ -92,10 +72,3 @@ def test_to_altair_stocks():
layer2 = eval(code)

assert layer.to_dict() == layer2.to_dict()


def test_from_json():
layer = Layer.from_json(EXAMPLE_JSON)
D1 = json.loads(EXAMPLE_JSON)
D2 = layer.to_dict()
assert D1 == D2
5 changes: 5 additions & 0 deletions altair/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def sanitize_dataframe(df):
* Raise ValueError is it has a hierarchical index.
* Convert categoricals to strings.
* Convert np.int dtypes to Python int objects
* Convert DateTime dtypes into appropriate string representations
"""
import pandas as pd
import numpy as np
Expand All @@ -188,6 +189,10 @@ def sanitize_dataframe(df):
df[col_name] = df[col_name].astype(str)
elif np.issubdtype(dtype, np.integer):
df[col_name] = np.array(list(map(int, df[col_name])), dtype=object)
elif str(dtype) == 'datetime64[ns]':
# this is the format used in Vega-Lite example area.json
to_str = lambda x: x.strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
df[col_name] = df[col_name].apply(to_str)
return df

def dataframe_to_json(df):
Expand Down
20 changes: 19 additions & 1 deletion notebooks/Index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,25 @@
"3. [Line Plots](LinePlots.ipynb)\n",
"4. [Area Plots](AreaPlots.ipynb)\n",
"5. [Exploring Cars Data](Cars.ipynb)\n",
"6. [PairGrid](PairGrid.ipynb)\n",
"6. [PairGrid](PairGrid.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples from Vega-Lite\n",
"\n",
"The Vega-Lite package includes dozens of example plots; examples of how to create them in Alair are shown here:\n",
"\n",
"1. [Vega-Lite Example Plots](auto_examples/Index.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## More Documentation\n",
"\n",
"The remainder of the documentation is in a set of notebooks diving into the use of Altair:\n",
"\n",
Expand Down
92 changes: 92 additions & 0 deletions notebooks/auto_examples/Index.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Auto-Generated Altair Examples\n",
"\n",
"All the following notebooks are auto-generated from the example specifications\n",
"in the [Vega-Lite](http://vega.github.io/vega-lite/) project.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [Area](area.ipynb): *--*\n",
"- [Area Vertical](area_vertical.ipynb): *Area chart showing weight of cars over time (vertical).*\n",
"- [Bar 1D](bar_1d.ipynb): *--*\n",
"- [Bar Aggregate](bar_aggregate.ipynb): *A bar chart showing the US population distribution of age groups in 2000.*\n",
"- [Bar Aggregate Size](bar_aggregate_size.ipynb): *A bar chart showing the US population distribution of age groups in 2000.*\n",
"- [Bar Aggregate Vertical](bar_aggregate_vertical.ipynb): *--*\n",
"- [Bar Grouped](bar_grouped.ipynb): *--*\n",
"- [Bar Grouped Horizontal](bar_grouped_horizontal.ipynb): *--*\n",
"- [Bar Layered Transparent](bar_layered_transparent.ipynb): *A bar chart showing the US population distribution of age groups and gender in 2000.*\n",
"- [Bar Yearmonth](bar_yearmonth.ipynb): *Temperature in Seattle as a bar chart with yearmonth time unit.*\n",
"- [Circle](circle.ipynb): *--*\n",
"- [Histogram](histogram.ipynb): *--*\n",
"- [Line](line.ipynb): *Google's stock price over time.*\n",
"- [Line Color](line_color.ipynb): *Stock prices of 5 Tech Companies Over Time.*\n",
"- [Line Detail](line_detail.ipynb): *Stock prices of 5 Tech Companies Over Time.*\n",
"- [Line Monotone](line_monotone.ipynb): *--*\n",
"- [Line Month](line_month.ipynb): *--*\n",
"- [Line Slope](line_slope.ipynb): *Slope graph showing the change in yield for different barley sites. It shows the error in the year labels for the Morris site.*\n",
"- [Line Step](line_step.ipynb): *Google's stock price over time.*\n",
"- [Point 1D](point_1d.ipynb): *--*\n",
"- [Point Dot Timeunit Color](point_dot_timeunit_color.ipynb): *--*\n",
"- [Point Filled](point_filled.ipynb): *--*\n",
"- [Scatter](scatter.ipynb): *A scatterplot showing horsepower and miles per gallons for various cars.*\n",
"- [Scatter Aggregate Detail](scatter_aggregate_detail.ipynb): *A scatterplot showing average horsepower and displacement for cars from different origins.*\n",
"- [Scatter Binned](scatter_binned.ipynb): *--*\n",
"- [Scatter Binned Color](scatter_binned_color.ipynb): *A scatterplot showing horsepower and miles per gallons with binned acceleration on color.*\n",
"- [Scatter Binned Size](scatter_binned_size.ipynb): *A scatterplot showing horsepower and miles per gallons with binned acceleration on size.*\n",
"- [Scatter Bubble](scatter_bubble.ipynb): *A bubbleplot showing horsepower on x, miles per gallons on y, and binned acceleration on size.*\n",
"- [Scatter Color](scatter_color.ipynb): *--*\n",
"- [Scatter Color Custom](scatter_color_custom.ipynb): *--*\n",
"- [Scatter Color Order](scatter_color_order.ipynb): *--*\n",
"- [Scatter Color Ordinal](scatter_color_ordinal.ipynb): *--*\n",
"- [Scatter Color Ordinal Custom](scatter_color_ordinal_custom.ipynb): *--*\n",
"- [Scatter Color Quantitative](scatter_color_quantitative.ipynb): *--*\n",
"- [Scatter Color Shape Constant](scatter_color_shape_constant.ipynb): *--*\n",
"- [Scatter Colored With Shape](scatter_colored_with_shape.ipynb): *A scatterplot showing horsepower and miles per gallons.*\n",
"- [Scatter Connected](scatter_connected.ipynb): *--*\n",
"- [Square](square.ipynb): *--*\n",
"- [Stacked Area](stacked_area.ipynb): *Area chart showing weight of cars over time.*\n",
"- [Stacked Area Normalize](stacked_area_normalize.ipynb): *--*\n",
"- [Stacked Area Ordinal](stacked_area_ordinal.ipynb): *--*\n",
"- [Stacked Area Stream](stacked_area_stream.ipynb): *--*\n",
"- [Stacked Bar 1D](stacked_bar_1d.ipynb): *--*\n",
"- [Stacked Bar H](stacked_bar_h.ipynb): *--*\n",
"- [Stacked Bar H Order](stacked_bar_h_order.ipynb): *--*\n",
"- [Stacked Bar Normalize](stacked_bar_normalize.ipynb): *--*\n",
"- [Stacked Bar Population](stacked_bar_population.ipynb): *A bar chart showing the US population distribution of age groups and gender in 2000.*\n",
"- [Stacked Bar V](stacked_bar_v.ipynb): *--*\n",
"- [Stacked Bar Weather](stacked_bar_weather.ipynb): *--*\n",
"- [Text Scatter Colored](text_scatter_colored.ipynb): *--*\n",
"- [Text Table Heatmap](text_table_heatmap.ipynb): *--*\n",
"- [Tick Dot](tick_dot.ipynb): *--*\n",
"- [Tick Dot Thickness](tick_dot_thickness.ipynb): *--*\n",
"- [Tick Strip](tick_strip.ipynb): *Shows the relationship between horsepower and the numbver of cylinders using tick marks.*\n",
"- [Trellis Bar](trellis_bar.ipynb): *A trellis bar chart showing the US population distribution of age groups and gender in 2000.*\n",
"- [Trellis Bar Histogram](trellis_bar_histogram.ipynb): *--*\n",
"- [Trellis Barley](trellis_barley.ipynb): *The Trellis display by Becker et al. helped establish small multiples as a “powerful mechanism for understanding interactions in studies of how a response depends on explanatory variables”. Here we reproduce a trellis of Barley yields from the 1930s, complete with main-effects ordering to facilitate comparison.*\n",
"- [Trellis Row Column](trellis_row_column.ipynb): *--*\n",
"- [Trellis Scatter](trellis_scatter.ipynb): *--*\n",
"- [Trellis Scatter Binned Row](trellis_scatter_binned_row.ipynb): *A trellis scatterplot showing Horsepower and Miles per gallons, faceted by binned values of Acceleration.*\n",
"- [Trellis Stacked Bar](trellis_stacked_bar.ipynb): *--*"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"env": {},
"language": "",
"name": "python3"
},
"language": "python"
},
"nbformat": 4,
"nbformat_minor": 0
}