Beispiel #1
0
def test_infer_types():
    data = dict(col1=[1.0, 2.0, 3.0],
                col2=[0.1, 0.2, 0.3],
                col3=['A', 'B', 'C'],
                col4=[True, False, True],
                col5=[0.1, 0.2, 0.3],
                col6=pd.date_range('2012', periods=3, freq='A'),
                col7=np.arange(3),
                col8=np.arange(3),
                col9=np.arange(3))
    kwargs = dict(x=('col1', 'Q'),
                  y=('col2', 'Q'),
                  row=('col3', 'N'),
                  col=('col4', 'N'),
                  size=('col5', 'Q'),
                  color=('col6', 'T'),
                  shape=('col7', 'Q'),
                  text=('col8', 'Q'),
                  detail=('col9', 'Q'))

    spec = api.Viz(data).encode(**{key: val[0] for key, val in kwargs.items()})
    for key, val in kwargs.items():
        name, typ = val
        assert getattr(spec.encoding, key).name == name
        assert getattr(spec.encoding, key).type == typ
Beispiel #2
0
def test_encode_aggregates():
    data = dict(col1=[1.0, 2.0, 3.0],
                col2=[0.1, 0.2, 0.3],
                col3=['A', 'B', 'C'],
                col4=[True, False, True],
                col5=[0.1, 0.2, 0.3],
                col6=pd.date_range('2012', periods=3, freq='A'),
                col7=np.arange(3),
                col8=np.arange(3),
                col9=np.arange(3))
    kwargs = dict(x=('count', 'col1'),
                  y=('count', 'col2'),
                  row=('count', 'col3'),
                  col=('count', 'col4'),
                  size=('avg', 'col5'),
                  color=('max', 'col6'),
                  shape=('count', 'col7'),
                  text=('avg', 'col8'),
                  detail=('count', 'col9'))

    spec = api.Viz(data).encode(
        **{key: "{0}({1})".format(*val)
           for key, val in kwargs.items()})
    for key, val in kwargs.items():
        agg, name = val
        assert getattr(spec.encoding, key).name == name
        assert getattr(spec.encoding, key).aggregate == agg
Beispiel #3
0
def test_select_x():
    def _check(data, expected):
        v = api.Viz(data)
        v.select_x()
        assert v.encoding.x.name == expected

    data = dict(
        col1=[1.0, 2.0, 3.0],  # Q
        col2=['A', 'B', 'C'],  # N
        col3=pd.date_range('2012', periods=3, freq='A'),  # T
        col4=pd.date_range('2012', periods=3, freq='A'))  # T
    _check(data, 'col3')

    data = dict(
        col1=[1.0, 2.0, 3.0],  # Q
        col2=['A', 'B', 'C'],  # N
        col3=['A', 'B', 'C'])  # N
    _check(data, 'col2')

    data = dict(
        col1=[1.0, 2.0, 3.0],  # Q
        col2=np.arange(3))  # Q
    _check(data, 'col1')

    # No data
    with pytest.raises(AssertionError):
        v = api.Viz(None)
        v.select_x()

    # Empty data
    with pytest.raises(AssertionError):
        v = api.Viz(dict())
        v.select_x()

    # Custom order
    data = dict(
        col1=[1.0, 2.0, 3.0],  # Q
        col2=['A', 'B', 'C'],  # N
        col3=pd.date_range('2012', periods=3, freq='A'),  # T
        col4=pd.date_range('2012', periods=3, freq='A'))  # T
    v = api.Viz(data)
    v.select_x(['N', 'T', 'Q', 'O'])
    assert v.encoding.x.name == 'col2'
Beispiel #4
0
 def _check(data, expected, aggregator=None):
     v = api.Viz(data)
     v.encode()
     v.encoding.x = 'col1'
     if aggregator is None:
         v.select_y()
         assert v.encoding.y.name == expected
     else:
         v.select_y(None, aggregator)
         assert v.encoding.y.name == expected
         assert v.encoding.y.aggregate == aggregator
Beispiel #5
0
def test_encode():
    data = dict(col1=[1.0, 2.0, 3.0],
                col2=[0.1, 0.2, 0.3],
                col3=['A', 'B', 'C'],
                col4=[True, False, True],
                col5=[0.1, 0.2, 0.3],
                col6=pd.date_range('2012', periods=3, freq='A'),
                col7=np.arange(3))
    kwargs = dict(x='col1',
                  y='col2',
                  row='col3',
                  col='col4',
                  size='col5',
                  color='col6',
                  shape='col7')

    spec = api.Viz(data).encode(**kwargs)
    for key, name in kwargs.items():
        assert getattr(spec.encoding, key).name == name
Beispiel #6
0
def test_to_dict():
    data = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
    spec = api.Viz(data).encode(x='x', y='y')

    D = spec.to_dict()

    assert D == {
        'config': {
            'gridColor': 'black',
            'gridOpacity': 0.08,
            'height': 400,
            'width': 600
        },
        'data': {
            'formatType': 'json',
            'values': [{
                'x': 1,
                'y': 4
            }, {
                'x': 2,
                'y': 5
            }, {
                'x': 3,
                'y': 6
            }]
        },
        'encoding': {
            'x': {
                'bin': False,
                'name': 'x',
                'type': 'Q'
            },
            'y': {
                'bin': False,
                'name': 'y',
                'type': 'Q'
            }
        },
        'marktype': 'point'
    }
Beispiel #7
0
def test_dataframe_data():
    spec, data = build_simple_spec()
    data = data = pd.DataFrame(data)
    spec = api.Viz(data)
    assert np.all(spec.data == data)
Beispiel #8
0
def test_hist():
    data = dict(x=[1, 2, 3], y=[4, 5, 6])

    viz1 = api.Viz(data).hist(x='x')
    assert viz1.encoding.x.name == "x"
    assert viz1.encoding.x.bin.maxbins == 10
    assert viz1.encoding.y.name == "x"
    assert viz1.encoding.y.type == "Q"
    assert viz1.encoding.y.aggregate == "count"

    viz2 = api.Viz(data).hist(x="x", bins=30)
    assert viz2.encoding.x.bin.maxbins == 30
    expected = {
        'config': {
            'gridColor': 'black',
            'gridOpacity': 0.08,
            'height': 400,
            'width': 600
        },
        'data': {
            'formatType': 'json',
            'values': [{
                'x': 1,
                'y': 4
            }, {
                'x': 2,
                'y': 5
            }, {
                'x': 3,
                'y': 6
            }]
        },
        'encoding': {
            'x': {
                'bin': {
                    'maxbins': 30
                },
                'name': 'x',
                'type': 'Q'
            },
            'y': {
                'aggregate': 'count',
                'bin': False,
                'name': 'x',
                'type': 'Q'
            }
        },
        'marktype': 'bar'
    }

    assert viz2.to_dict() == expected

    viz3 = api.Viz(data).hist(x="x:O",
                              color=api.Color(shorthand="bar", type="N"))
    assert viz3.encoding.x.name == "x"
    assert viz3.encoding.x.type == "O"

    expected = {
        'config': {
            'gridColor': 'black',
            'gridOpacity': 0.08,
            'height': 400,
            'width': 600
        },
        'data': {
            'formatType': 'json',
            'values': [{
                'x': 1,
                'y': 4
            }, {
                'x': 2,
                'y': 5
            }, {
                'x': 3,
                'y': 6
            }]
        },
        'encoding': {
            'x': {
                'bin': {
                    'maxbins': 10
                },
                'name': 'x',
                'type': 'O'
            },
            'y': {
                'aggregate': 'count',
                'bin': False,
                'name': 'x',
                'type': 'Q'
            },
            'color': {
                'bin': False,
                'name': 'bar',
                'opacity': 1.0,
                'type': 'N',
                'value': '#4682b4'
            }
        },
        'marktype': 'bar'
    }

    assert viz3.to_dict() == expected

    viz4 = api.Viz(data).hist(x=api.X(shorthand="x", bin=api.Bin(maxbins=40)))
    assert viz4.encoding.x.name == "x"
    assert viz4.encoding.x.bin.maxbins == 40
Beispiel #9
0
def build_simple_spec():
    data = dict(x=[1, 2, 3], y=[4, 5, 6])
    return api.Viz(data), data
Beispiel #10
0
def test_select_y():
    def _check(data, expected, aggregator=None):
        v = api.Viz(data)
        v.encode()
        v.encoding.x = 'col1'
        if aggregator is None:
            v.select_y()
            assert v.encoding.y.name == expected
        else:
            v.select_y(None, aggregator)
            assert v.encoding.y.name == expected
            assert v.encoding.y.aggregate == aggregator

    data = dict(
        col1=[1.0, 2.0, 3.0],  # Chosen X
        col2=['A', 'B', 'C'],  # N
        col3=pd.date_range('2012', periods=3, freq='A'),  # T
        col4=pd.date_range('2012', periods=3, freq='A'),  # T
        col5=[1.0, 2.0, 3.0],  # Q
        col6=[1.0, 2.0, 3.0])  # Q
    _check(data, 'col5')
    _check(data, 'col5', 'avg')

    data = dict(
        col1=[1.0, 2.0, 3.0],  # Chosen X
        col2=['A', 'B', 'C'],  # N
        col3=pd.date_range('2012', periods=3, freq='A'),  # T
        col4=['A', 'B', 'C'])  # N
    _check(data, 'col2')
    _check(data, 'col2', 'sum')

    data = dict(
        col1=[1.0, 2.0, 3.0],  # Chosen X
        col2=pd.date_range('2012', periods=3, freq='A'),  # T
        col3=pd.date_range('2012', periods=3, freq='A'))  # T
    _check(data, 'col2')
    _check(data, 'col2', 'sum')

    # No data
    with pytest.raises(AssertionError):
        v = api.Viz(None)
        v.select_y()

    # Just one column
    with pytest.raises(AssertionError):
        v = api.Viz(dict(col1=[1.0, 2.0]))
        v.select_y()

    # No encoding
    with pytest.raises(AssertionError):
        v = api.Viz(dict(col1=[1.0, 2.0], col2=[1.0, 2.0]))
        v.select_y()

    # No X
    with pytest.raises(AssertionError):
        v = api.Viz(dict(col1=[1.0, 2.0], col2=[1.0, 2.0]))
        v.encode()
        v.select_y()

    # Custom order
    data = dict(
        col1=[1.0, 2.0, 3.0],  # Chosen X
        col2=['A', 'B', 'C'],  # N
        col3=pd.date_range('2012', periods=3, freq='A'),  # T
        col4=pd.date_range('2012', periods=3, freq='A'),  # T
        col5=[1.0, 2.0, 3.0],  # Q
        col6=[1.0, 2.0, 3.0])  # Q
    v = api.Viz(data)
    v.encode()
    v.encoding.x = 'col1'
    v.select_y(['N', 'T', 'Q', 'O'])
    assert v.encoding.y.name == 'col2'
Beispiel #11
0
 def _check(data, expected):
     v = api.Viz(data)
     v.select_x()
     assert v.encoding.x.name == expected