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
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
def test_compound_add_selections(charttype): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = charttype(base.add_selection(selection), base.add_selection(selection)) chart2 = charttype(base, base).add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
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)
def test_selection_expression(): selection = alt.selection_single(fields=["value"]) assert isinstance(selection.value, alt.expr.Expression) assert selection.value.to_dict() == "{0}.value".format(selection.name) assert isinstance(selection["value"], alt.expr.Expression) assert selection["value"].to_dict() == "{0}['value']".format( selection.name)
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
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()
def test_selection_expression(): selection = alt.selection_single(fields=["value"]) assert isinstance(selection.value, alt.expr.Expression) assert selection.value.to_dict() == "{0}.value".format(selection.name) assert isinstance(selection["value"], alt.expr.Expression) assert selection["value"].to_dict() == "{0}['value']".format( selection.name) with pytest.raises(AttributeError): selection.__magic__
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)
def test_layer_add_selection(): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = alt.layer(base.add_selection(selection), base) chart2 = alt.layer(base, base).add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
def test_facet_add_selections(): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = base.add_selection(selection).facet("val:Q") chart2 = base.facet("val:Q").add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
def test_repeat_add_selections(): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = base.add_selection(selection).repeat(list("ABC")) chart2 = base.repeat(list("ABC")).add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
from matplotlib import MatplotlibDeprecationWarning warnings.filterwarnings("ignore", category=MatplotlibDeprecationWarning) # %% import altair.vegalite.v3 as alt import pandas as pd import numpy as np rand = np.random.RandomState(42) df = pd.DataFrame({"xval": range(100), "yval": rand.randn(100).cumsum()}) slider1 = alt.binding_range(min=0, max=100, step=1, name="cutoff1:") selector1 = alt.selection_single(name="SelectorName1", fields=["cutoff1"], bind=slider1, init={"cutoff1": 50}) slider2 = alt.binding_range(min=0, max=100, step=1, name="cutoff2:") selector2 = alt.selection_single(name="SelectorName2", fields=["cutoff2"], bind=slider2, init={"cutoff2": 50}) ch_base = (alt.Chart(df).mark_point().encode( x="xval", y="yval", color=alt.condition(alt.datum.xval < selector1.cutoff1, alt.value("red"), alt.value("blue")), ))