def test_lasso_selection_callback(self, df_type): bnac = BaseNonAggregate() bnac.x = "a" bnac.y = "b" bnac.chart_type = "temp" def t_function(data, patch_update=False): self.result = data bnac.reload_chart = t_function df = initialize_df(df_type, {"a": [0, 1, 1, 2], "b": [0, 1, 2, 0]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) geometry = np.array([[1, 1, 2], [1, 2, 1]]) t = bnac.get_lasso_select_callback(dashboard) with mock.patch("cuspatial.point_in_polygon") as pip: if isinstance(df, dask_cudf.DataFrame): # point in polygon is called per partition, # in this case 2 partitions of length 2 each pip.return_value = cudf.DataFrame({"selection": [True, False]}) else: pip.return_value = cudf.DataFrame( {"selection": [True, False, True, False]}) t(geometry) assert pip.called assert isinstance(self.result, df_type)
def test_add_events(self, df_type, add_interaction, reset_event, event_1, event_2): bnac = BaseNonAggregate() bnac.add_interaction = add_interaction bnac.reset_event = reset_event bnac.chart = hv.InteractiveDatashader() df = initialize_df(df_type, {"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) self.event_1 = None self.event_2 = None def t_func(fn): self.event_1 = fn.__name__ def t_func1(fn): self.event_2 = fn.__name__ bnac.chart.add_box_select_callback = t_func bnac.chart.add_lasso_select_callback = t_func1 bnac.add_events(dashboard) assert self.event_1 == event_1 assert self.event_2 == event_2
def test_lasso_election_callback(self): nodes = cudf.DataFrame({ "vertex": [0, 1, 2, 3], "x": [0, 1, 1, 2], "y": [0, 1, 2, 0] }) edges = cudf.DataFrame({ "source": [1, 1, 1, 1], "target": [0, 1, 2, 3] }) dashboard = DashBoard(dataframe=DataFrame.load_graph((nodes, edges))) bg = BaseGraph() bg.chart_type = "temp" bg.nodes = nodes bg.edges = edges bg.inspect_neighbors = CustomInspectTool(_active=False) def t_function(nodes, edges=None, patch_update=False): pass bg.reload_chart = t_function class evt: geometry = dict(x=[1, 1, 2], y=[1, 2, 1], type="poly") final = True t = bg.get_selection_geometry_callback(dashboard) with mock.patch("cuspatial.point_in_polygon") as pip: pip.return_value = cudf.DataFrame( {"selection": [True, False, True, False]}) t(evt) assert pip.called
def test_add_reset_event(self): bg = BaseGraph() bg.edges = None bg.chart_type = "test" bg.x = "a" bg.x_range = (0, 2) bg.y_range = (3, 5) df = cudf.DataFrame({"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) dashboard._active_view = "a_test" def t_func1(event, fn): fn("event") def reload_fn(nodes, edges=None, patch_update=False): pass bg.add_event = t_func1 bg.reload_chart = reload_fn bg.add_reset_event(dashboard) assert bg.x_range is None assert bg.y_range is None assert dashboard._active_view == "a_test"
def test_box_selection_callback(self, inspect_neighbors, result): nodes = cudf.DataFrame({ "vertex": [0, 1, 2, 3], "x": [0, 1, 1, 2], "y": [0, 1, 2, 0] }) edges = cudf.DataFrame({ "source": [1, 1, 1, 1], "target": [0, 1, 2, 3] }) dashboard = DashBoard(dataframe=DataFrame.load_graph((nodes, edges))) bg = BaseGraph() bg.chart_type = "temp" bg.nodes = nodes bg.edges = edges bg.inspect_neighbors = inspect_neighbors self.result = None def t_function(nodes, edges=None, patch_update=False): self.result = nodes.reset_index(drop=True) bg.reload_chart = t_function dashboard._active_view = bg.name class evt: geometry = dict(x0=1, x1=3, y0=0, y1=1, type="rect") t = bg.get_selection_geometry_callback(dashboard) t(evt) assert self.result.equals(result)
def test_get_selection_geometry_callback(self, df_type): bnac = BaseNonAggregate() df = initialize_df(df_type, {"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) assert callable(type(bnac.get_box_select_callback(dashboard))) assert callable(type(bnac.get_lasso_select_callback(dashboard)))
def test_get_selection_geometry_callback(self): bg = BaseGraph() df = cudf.DataFrame({"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) assert (bg.get_selection_geometry_callback(dashboard).__name__ == "selection_callback") assert callable(type(bg.get_selection_geometry_callback(dashboard)))
def test_dashboard(self): df = cudf.DataFrame({ "key": [0, 1, 2, 3, 4], "val": [float(i + 10) for i in range(5)] }) cux_df = DataFrame.from_dataframe(df) dashboard = cux_df.dashboard(charts=[], title="test_title") assert dashboard._cuxfilter_df.data.equals(df) assert dashboard.title == "test_title" assert ( dashboard._dashboard.__class__ == cuxfilter.layouts.single_feature) assert dashboard._theme == cuxfilter.themes.light
def test_compute_query_dict(self, x_range, y_range, query, local_dict): bg = BaseGraph() bg.chart_type = "test" bg.x_range = x_range bg.y_range = y_range df = cudf.DataFrame({"x": [1, 2, 2], "y": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) bg.compute_query_dict(dashboard._query_str_dict, dashboard._query_local_variables_dict) assert dashboard._query_str_dict["x_test"] == query for key in local_dict: assert ( dashboard._query_local_variables_dict[key] == local_dict[key])
def test_add_reset_event(self, df_type): bnac = BaseNonAggregate() bnac.chart_type = "test" bnac.x = "a" bnac.x_range = (0, 2) bnac.y_range = (3, 5) bnac.chart = hv.InteractiveDatashader() df = initialize_df(df_type, {"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) dashboard._active_view = bnac def t_func1(event, fn): fn("event") bnac.add_event = t_func1 bnac.add_reset_event(dashboard) assert bnac.selected_indices is None
def test_add_reset_event(self): bnac = BaseNonAggregate() bnac.chart_type = "test" bnac.x = "a" bnac.x_range = (0, 2) bnac.y_range = (3, 5) df = cudf.DataFrame({"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) dashboard._active_view = bnac def t_func1(event, fn): fn("event") bnac.add_event = t_func1 bnac.add_reset_event(dashboard) assert bnac.x_range is None assert bnac.y_range is None
def test_box_selection_callback(self): bnac = BaseNonAggregate() bnac.x = "a" bnac.y = "b" bnac.chart_type = "temp" self.result = None def t_function(data, patch_update=False): self.result = data bnac.reload_chart = t_function df = cudf.DataFrame({"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) dashboard._active_view = bnac.name class evt: geometry = dict(x0=1, x1=2, y0=3, y1=4, type="rect") t = bnac.get_selection_geometry_callback(dashboard) t(evt) assert self.result.equals(df.query("1<=a<=2 and 3<=b<=4"))
def test_compute_query_dict(self, x_range, y_range, query, local_dict): bnac = BaseNonAggregate() bnac.chart_type = "test" bnac.x = "x" bnac.y = "y" bnac.x_range = x_range bnac.y_range = y_range df = cudf.DataFrame({"x": [1, 2, 2], "y": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) bnac.compute_query_dict(dashboard._query_str_dict, dashboard._query_local_variables_dict) bnac_key = (f"{bnac.x}_{bnac.y}" f"{'_' + bnac.aggregate_col if bnac.aggregate_col else ''}" f"_{bnac.aggregate_fn}_{bnac.chart_type}_{bnac.title}") assert dashboard._query_str_dict[bnac_key] == query for key in local_dict: assert ( dashboard._query_local_variables_dict[key] == local_dict[key])
class TestDataFrame: @pytest.mark.parametrize("cux_df", [(DataFrame.from_dataframe(cudf.DataFrame()))]) def test_init(self, cux_df): assert isinstance(cux_df.data, cudf.DataFrame) assert cux_df.data.equals(cudf.DataFrame()) assert cux_df.data.equals(cux_df.backup) def test_dashboard(self): df = cudf.DataFrame({ "key": [0, 1, 2, 3, 4], "val": [float(i + 10) for i in range(5)] }) cux_df = DataFrame.from_dataframe(df) dashboard = cux_df.dashboard(charts=[], title="test_title") assert dashboard._data.equals(df) assert dashboard.title == "test_title" assert ( dashboard._dashboard.__class__ == cuxfilter.layouts.single_feature) assert dashboard._theme == cuxfilter.themes.light assert dashboard.data_size_widget is True
def test_compute_query_dict( self, df_type, x_range, y_range, query, local_dict ): bg = BaseGraph() bg.chart_type = "test" bg.box_selected_range = local_dict bg.x_range = x_range bg.y_range = y_range df = initialize_df(df_type, {"x": [1, 2, 2], "y": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) bg.compute_query_dict( dashboard._query_str_dict, dashboard._query_local_variables_dict ) assert ( dashboard._query_str_dict[f"x_y_vertex_count_test_{bg.title}"] == query ) for key in local_dict: assert ( dashboard._query_local_variables_dict[key] == local_dict[key] )
def test_box_selection_callback( self, df_type, inspect_neighbors, result, index ): nodes = initialize_df( df_type, {"vertex": [0, 1, 2, 3], "x": [0, 1, 1, 2], "y": [0, 1, 2, 0]}, ) edges = initialize_df( df_type, {"source": [1, 1, 1, 1], "target": [0, 1, 2, 3]} ) dashboard = DashBoard(dataframe=DataFrame.load_graph((nodes, edges))) bg = BaseGraph() bg.chart_type = "temp" bg.nodes = nodes bg.edges = edges bg.inspect_neighbors = inspect_neighbors self.result = None def t_function(data, edges=None, patch_update=False): self.result = data.reset_index(drop=True) bg.reload_chart = t_function dashboard._active_view = bg class evt: bounds = (1, 3, 0, 1) x_selection = (1, 3) y_selection = (0, 1) t = bg.get_box_select_callback(dashboard) t(evt.bounds, evt.x_selection, evt.y_selection) result = initialize_df(df_type, result, index) assert df_equals(self.result, result)
def test_lasso_selection_callback(self, df_type): nodes = initialize_df( df_type, {"vertex": [0, 1, 2, 3], "x": [0, 1, 1, 2], "y": [0, 1, 2, 0]}, ) edges = initialize_df( df_type, {"source": [1, 1, 1, 1], "target": [0, 1, 2, 3]} ) dashboard = DashBoard(dataframe=DataFrame.load_graph((nodes, edges))) bg = BaseGraph() bg.chart_type = "temp" bg.nodes = nodes bg.edges = edges bg.inspect_neighbors = CustomInspectTool(_active=False) def t_function(data, edges=None, patch_update=False): self.result = data bg.reload_chart = t_function geometry = np.array([[1, 1, 2], [1, 2, 1]]) t = bg.get_lasso_select_callback(dashboard) with mock.patch("cuspatial.point_in_polygon") as pip: if isinstance(nodes, dask_cudf.DataFrame): # point in polygon is called per partition, # in this case 2 partitions of length 2 each pip.return_value = cudf.DataFrame({"selection": [True, False]}) else: pip.return_value = cudf.DataFrame( {"selection": [True, False, True, False]} ) t(geometry) assert pip.called assert isinstance(self.result, df_type)
def test_add_events(self, add_interaction, reset_event, event_1, event_2): bg = BaseGraph() bg.add_interaction = add_interaction bg.reset_event = reset_event df = cudf.DataFrame({"x": [1, 2, 2], "y": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) self.event_1 = None self.event_2 = None def t_func(fn): self.event_1 = fn.__name__ def t_func1(event, fn): self.event_2 = fn.__name__ bg.add_selection_geometry_event = t_func bg.add_event = t_func1 bg.add_events(dashboard) assert self.event_1 == event_1 assert self.event_2 == event_2
def test_add_reset_event(self, df_type): bg = BaseGraph() bg.edges = None bg.chart_type = "test" bg.x = "a" bg.x_range = (0, 2) bg.y_range = (3, 5) bg.chart = hv.InteractiveDatashader() df = initialize_df(df_type, {"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) dashboard._active_view = bg def t_func1(event, fn): fn("event") def reload_fn(data, edges=None, patch_update=False): pass bg.add_event = t_func1 bg.reload_chart = reload_fn bg.add_reset_event(dashboard) assert bg.selected_indices is None
def test_lasso_election_callback(self): bnac = BaseNonAggregate() bnac.x = "a" bnac.y = "b" bnac.chart_type = "temp" def t_function(data, patch_update=False): self.result = data bnac.reload_chart = t_function df = cudf.DataFrame({"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) class evt: geometry = dict(x=[1, 1, 2], y=[1, 2, 1], type="poly") final = True t = bnac.get_selection_geometry_callback(dashboard) with mock.patch("cuspatial.point_in_polygon") as pip: pip.return_value = cudf.DataFrame( {"selection": [True, False, True]}) t(evt) assert pip.called
def test_box_selection_callback(self, df_type): bnac = BaseNonAggregate() bnac.x = "a" bnac.y = "b" bnac.chart_type = "temp" self.result = None def t_function(data, patch_update=False): self.result = data bnac.reload_chart = t_function df = initialize_df(df_type, {"a": [1, 2, 2], "b": [3, 4, 5]}) dashboard = DashBoard(dataframe=DataFrame.from_dataframe(df)) dashboard._active_view = bnac class evt: bounds = (1, 2, 3, 4) x_selection = (1, 2) y_selection = (3, 4) t = bnac.get_box_select_callback(dashboard) t(evt.bounds, evt.x_selection, evt.y_selection) assert df_equals(self.result, df.query("1<=a<=2 and 3<=b<=4"))
def test_init(self): cux_df = DataFrame.from_dataframe( cudf.DataFrame( { "states": [float(i + 30) for i in range(10)], "val": [float(i + 10) for i in range(10)], "val_t": [float(i + 100) for i in range(10)], } ) ) choropleth3d_chart = charts.choropleth( x="states", color_column="val", elevation_column="val_t", color_aggregate_fn="mean", elevation_aggregation_fn="count", data_points=57, add_interaction=False, elevation_factor=100000, geoJSONSource=( "https://raw.githubusercontent.com/loganpowell/census-geojson" "/master/GeoJSON/5m/2018/state.json" ), geoJSONProperty="STATEFP", ) cux_df.dashboard([choropleth3d_chart]) assert isinstance(choropleth3d_chart, charts.deckgl.plots.Choropleth) assert choropleth3d_chart.deck_spec == { "mapboxApiAccessToken": None, "map_style": None, "initialViewState": { "latitude": 28.400005999999998, "longitude": 0.31556500000000653, "zoom": 3, "max_zoom": 16, }, "controller": True, "layers": [ { "opacity": 1, "getLineWidth": 10, "getPolygon": "coordinates", "getElevation": "val_t*100000", "getFillColor": "[__r__, __g__, __b__, __a__]", "stroked": True, "filled": True, "extruded": True, "lineWidthScale": 10, "lineWidthMinPixels": 1, "highlightColor": [200, 200, 200, 200], "visible": True, "pickable": True, "getLineColor": [0, 188, 212], "autoHighlight": True, "elevationScale": 0.8, "pickMultipleObjects": True, } ], } assert ( choropleth3d_chart.chart._deck.map_style == "mapbox://styles/mapbox/dark-v9" ) assert isinstance(choropleth3d_chart.chart, PanelDeck) assert choropleth3d_chart.chart.x == "states" assert choropleth3d_chart.chart.data.equals( choropleth3d_chart.source_df ) assert choropleth3d_chart.chart.colors.equals( choropleth3d_chart.source_df[choropleth3d_chart.rgba_columns], ) assert choropleth3d_chart.chart.indices == set() assert choropleth3d_chart.chart.multi_select is False assert choropleth3d_chart.chart.sizing_mode == "scale_both"
import pytest from cuxfilter import charts from cuxfilter import DataFrame from cuxfilter.charts.deckgl.bindings import PanelDeck from ..utils import initialize_df, df_types df_args = { "states": [float(i + 30) for i in range(10)], "val": [float(i + 10) for i in range(10)], "val_t": [float(i + 100) for i in range(10)], } dfs = [initialize_df(type, df_args) for type in df_types] cux_dfs = [DataFrame.from_dataframe(df) for df in dfs] class TestDeckGL: @pytest.mark.parametrize("cux_df", cux_dfs) def test_init(self, cux_df): choropleth3d_chart = charts.choropleth( x="states", color_column="val", elevation_column="val_t", color_aggregate_fn="mean", elevation_aggregation_fn="count", data_points=57, add_interaction=False, elevation_factor=100000, geoJSONSource=( "https://raw.githubusercontent.com/loganpowell/census-geojson" "/master/GeoJSON/5m/2018/state.json"),