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_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_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 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
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)