コード例 #1
0
def test_selection():
    # test instantiation of selections
    interval = alt.selection_interval(name="selec_1")
    assert interval.selection.type == "interval"
    assert interval.name == "selec_1"

    single = alt.selection_single(name="selec_2")
    assert single.selection.type == "single"
    assert single.name == "selec_2"

    multi = alt.selection_multi(name="selec_3")
    assert multi.selection.type == "multi"
    assert multi.name == "selec_3"

    # test adding to chart
    chart = alt.Chart().add_selection(single)
    chart = chart.add_selection(multi, interval)
    assert set(chart.selection.keys()) == {"selec_1", "selec_2", "selec_3"}

    # test logical operations
    assert isinstance(single & multi, alt.Selection)
    assert isinstance(single | multi, alt.Selection)
    assert isinstance(~single, alt.Selection)
    assert isinstance((single & multi)[0].group, alt.SelectionAnd)
    assert isinstance((single | multi)[0].group, alt.SelectionOr)
    assert isinstance((~single)[0].group, alt.SelectionNot)

    # test that default names increment (regression for #1454)
    sel1 = alt.selection_single()
    sel2 = alt.selection_multi()
    sel3 = alt.selection_interval()
    names = {s.name for s in (sel1, sel2, sel3)}
    assert len(names) == 3
コード例 #2
0
ファイル: test_api.py プロジェクト: xianjiaodahe91/altair
def test_selection():
    # test instantiation of selections
    interval = alt.selection_interval(name='selec_1')
    assert interval.selection.type == 'interval'
    assert interval.name == 'selec_1'

    single = alt.selection_single(name='selec_2')
    assert single.selection.type == 'single'
    assert single.name == 'selec_2'

    multi = alt.selection_multi(name='selec_3')
    assert multi.selection.type == 'multi'
    assert multi.name == 'selec_3'

    # test adding to chart
    chart = alt.Chart().add_selection(single)
    chart = chart.add_selection(multi, interval)
    assert set(chart.selection.keys()) == {'selec_1', 'selec_2', 'selec_3'}

    # test logical operations
    assert isinstance(single & multi, alt.SelectionAnd)
    assert isinstance(single | multi, alt.SelectionOr)
    assert isinstance(~single, alt.SelectionNot)

    # test that default names increment (regression for #1454)
    sel1 = alt.selection_single()
    sel2 = alt.selection_multi()
    sel3 = alt.selection_interval()
    names = {s.name for s in (sel1, sel2, sel3)}
    assert len(names) == 3
コード例 #3
0
ファイル: test_api.py プロジェクト: xianjiaodahe91/altair
def test_filter_transform_selection_predicates():
    selector1 = alt.selection_interval(name='s1')
    selector2 = alt.selection_interval(name='s2')
    base = alt.Chart('data.txt').mark_point()

    chart = base.transform_filter(selector1)
    assert chart.to_dict()['transform'] == [{'filter': {'selection': 's1'}}]

    chart = base.transform_filter(~selector1)
    assert chart.to_dict()['transform'] == [{
        'filter': {
            'selection': {
                'not': 's1'
            }
        }
    }]

    chart = base.transform_filter(selector1 & selector2)
    assert chart.to_dict()['transform'] == [{
        'filter': {
            'selection': {
                'and': ['s1', 's2']
            }
        }
    }]

    chart = base.transform_filter(selector1 | selector2)
    assert chart.to_dict()['transform'] == [{
        'filter': {
            'selection': {
                'or': ['s1', 's2']
            }
        }
    }]
コード例 #4
0
ファイル: test_api.py プロジェクト: tkf/altair
def test_selection_property():
    sel = alt.selection_interval()
    chart = alt.Chart('data.csv').mark_point().properties(
        selection=sel
    )

    assert list(chart['selection'].keys()) == [sel.name]
コード例 #5
0
ファイル: test_api.py プロジェクト: sid-kap/altair
def test_SelectionMapping():
    # test instantiation of selections
    interval = alt.selection_interval(name='selec_1')
    assert interval['selec_1'].type == 'interval'
    assert interval._get_name() == 'selec_1'

    single = alt.selection_single(name='selec_2')
    assert single['selec_2'].type == 'single'
    assert single._get_name() == 'selec_2'

    multi = alt.selection_multi(name='selec_3')
    assert multi['selec_3'].type == 'multi'
    assert multi._get_name() == 'selec_3'

    # test addition
    x = single + multi + interval
    assert set(x.to_dict().keys()) == {'selec_1', 'selec_2', 'selec_3'}

    y = single.copy()
    y += multi
    y += interval
    assert x.to_dict() == y.to_dict()

    # test logical operations
    x = single & multi
    assert isinstance(x, alt.SelectionAnd)

    y = single | multi
    assert isinstance(y, alt.SelectionOr)
コード例 #6
0
def test_add_selection():
    selections = [
        alt.selection_interval(),
        alt.selection_single(),
        alt.selection_multi(),
    ]
    chart = (alt.Chart().mark_point().add_selection(
        selections[0]).add_selection(selections[1], selections[2]))
    expected = {s.name: s.selection for s in selections}
    assert chart.selection == expected
コード例 #7
0
ファイル: test_api.py プロジェクト: sid-kap/altair
def test_add_selection():
    selections = [
        alt.selection_interval(),
        alt.selection_single(),
        alt.selection_multi()
    ]
    chart = alt.Chart().mark_point().add_selection(
        selections[0]).add_selection(selections[1], selections[2])
    expected = selections[0] + selections[1] + selections[2]
    assert chart.selection.to_dict() == expected.to_dict()
コード例 #8
0
    def make_ridge_chart(self):
        self.plot_df['year'] = pd.to_datetime(
            self.plot_df['year'].astype(str) + '-01-01')
        brush = alt.selection_interval(encodings=['x'])
        step = 18
        overlap = 7
        # Topic selection
        selection = alt.selection_multi(fields=['Topic'])

        # смена цвета на выборе темы -- всё что не выделили делается серым
        color = alt.condition(
            selection,
            alt.Color('Topic:N',
                      legend=None,
                      scale=alt.Scale(scheme="rainbow")),
            alt.value('lightgray'))

        a = alt.hconcat(
            alt.Chart(self.plot_df).mark_area(
                fillOpacity=0.6, interpolate='monotone').encode(
                    x=alt.X('year:T'),
                    y=alt.Y('rate:Q',
                            scale=alt.Scale(range=[0, -overlap * (step + 1)]),
                            axis=None),
                    row=alt.Row('Topic:N',
                                header=alt.Header(title=None,
                                                  labelPadding=0,
                                                  labelFontSize=0)),
                    color=color).add_selection(selection).properties(
                        width=800,
                        height=step,
                        bounds='flush',
                    ).transform_filter(brush),
            alt.Chart(self.plot_df,
                      height=500).mark_point().encode(
                          y=alt.Y('Topic:N', axis=alt.Axis(orient='right')),
                          color=color).add_selection(selection))

        b = alt.Chart(self.plot_df).mark_area(interpolate='monotone').encode(
            y='sum(rate):Q',
            x='year:T',
        ).properties(width=800, height=100).add_selection(brush)

        if self.add_data_names != ['All']:
            add_charts = [
                self.make_add_data_chart(i, brush) for i in self.add_data_names
            ]
            chart = alt.vconcat(a, *add_charts, b, padding=0,
                                spacing=0).configure_view(
                                    stroke=None).configure_axis(grid=False)
            return chart
        else:
            chart = alt.vconcat(a, b, padding=0, spacing=0).configure_view(
                stroke=None).configure_axis(grid=False)
            return chart
コード例 #9
0
ファイル: test_api.py プロジェクト: yy/altair
def test_selection():
    # test instantiation of selections
    interval = alt.selection_interval(name='selec_1')
    assert interval.selection.type == 'interval'
    assert interval.name == 'selec_1'

    single = alt.selection_single(name='selec_2')
    assert single.selection.type == 'single'
    assert single.name == 'selec_2'

    multi = alt.selection_multi(name='selec_3')
    assert multi.selection.type == 'multi'
    assert multi.name == 'selec_3'

    # test adding to chart
    chart = alt.Chart().add_selection(single)
    chart = chart.add_selection(multi, interval)
    assert set(chart.selection.keys()) == {'selec_1', 'selec_2', 'selec_3'}

    # test logical operations
    assert isinstance(single & multi, alt.SelectionAnd)
    assert isinstance(single | multi, alt.SelectionOr)
    assert isinstance(~single, alt.SelectionNot)
コード例 #10
0
def test_filter_transform_selection_predicates():
    selector1 = alt.selection_interval(name="s1")
    selector2 = alt.selection_interval(name="s2")
    base = alt.Chart("data.txt").mark_point()

    chart = base.transform_filter(selector1)
    assert chart.to_dict()["transform"] == [{"filter": {"selection": "s1"}}]

    chart = base.transform_filter(~selector1)
    assert chart.to_dict()["transform"] == [{
        "filter": {
            "selection": {
                "not": "s1"
            }
        }
    }]

    chart = base.transform_filter(selector1 & selector2)
    assert chart.to_dict()["transform"] == [{
        "filter": {
            "selection": {
                "and": ["s1", "s2"]
            }
        }
    }]

    chart = base.transform_filter(selector1 | selector2)
    assert chart.to_dict()["transform"] == [{
        "filter": {
            "selection": {
                "or": ["s1", "s2"]
            }
        }
    }]

    chart = base.transform_filter(selector1 | ~selector2)
    assert chart.to_dict()["transform"] == [{
        "filter": {
            "selection": {
                "or": ["s1", {
                    "not": "s2"
                }]
            }
        }
    }]

    chart = base.transform_filter(~selector1 | ~selector2)
    assert chart.to_dict()["transform"] == [{
        "filter": {
            "selection": {
                "or": [{
                    "not": "s1"
                }, {
                    "not": "s2"
                }]
            }
        }
    }]

    chart = base.transform_filter(~(selector1 & selector2))
    assert chart.to_dict()["transform"] == [{
        "filter": {
            "selection": {
                "not": {
                    "and": ["s1", "s2"]
                }
            }
        }
    }]