Example #1
0
 def test_layout_invalid_columns(self):
     with patch('IPython.display.display'):
         df = pd.DataFrame([1, 2], columns=['1'])
         PerspectiveWidget(df, plugin=Plugin.YBAR, columns=['1'])
         try:
             PerspectiveWidget(df, plugin=Plugin.YBAR, columns=5)
             assert False
         except PerspectiveError:
             pass
Example #2
0
    def test_colpivots(self):
        arrays = [
            np.array([
                'bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo',
                'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux'
            ]),
            np.array([
                'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one',
                'one', 'two', 'two', 'one', 'one', 'two', 'two'
            ]),
            np.array([
                'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y',
                'X', 'Y', 'X', 'Y'
            ])
        ]
        tuples = list(zip(*arrays))
        index = pd.MultiIndex.from_tuples(tuples,
                                          names=['first', 'second', 'third'])

        df_both = pd.DataFrame(np.random.randn(3, 16),
                               index=['A', 'B', 'C'],
                               columns=index)
        psp = PerspectiveWidget(df_both)
        assert psp.columns == [' ']
        assert psp.columnpivots == ['first', 'second', 'third']
        assert psp.rowpivots == ['index']
        assert psp.schema == {
            'first': 'string',
            'second': 'string',
            'third': 'string',
            'index': 'string',
            ' ': 'float'
        }
Example #3
0
 def test_rowpivots(self):
     # basic
     df_pivoted = DF.set_index(['Country', 'Region'])
     psp = PerspectiveWidget(df_pivoted)
     assert psp.rowpivots == ['Country', 'Region']
     assert psp.columnpivots == []
     assert sorted(psp.columns) == sorted([
         'Category', 'City', 'Customer ID', 'Discount', 'Order Date',
         'Order ID', 'Postal Code', 'Product ID', 'Profit', 'Quantity',
         'Row ID', 'Sales', 'Segment', 'Ship Date', 'Ship Mode', 'State',
         'Sub-Category'
     ])
     assert psp.schema == {
         'Country': 'string',
         'Region': 'string',
         'Category': 'string',
         'City': 'string',
         'Customer ID': 'string',
         'Discount': 'float',
         'Order Date': 'date',
         'Order ID': 'string',
         'Postal Code': 'string',
         'Product ID': 'string',
         'Profit': 'float',
         'Quantity': 'integer',
         'Row ID': 'integer',
         'Sales': 'integer',
         'Segment': 'string',
         'Ship Date': 'date',
         'Ship Mode': 'string',
         'State': 'string',
         'Sub-Category': 'string'
     }
Example #4
0
 def test_widget_client_np_recarray(self):
     data = np.array([(1, 2), (3, 4)],
                     dtype=[("a", "int64"),
                            ("b", "int64")]).view(np.recarray)
     widget = PerspectiveWidget(data, client=True)
     assert widget.table is None
     assert widget._data == {"a": [1, 3], "b": [2, 4]}
Example #5
0
    def test_1(self):
        import tributary as t

        def foo1(on_data):
            x = 0
            while x < 100:
                on_data({
                    'a': random.random(),
                    'b': random.randint(0, 1000),
                    'x': x
                })
                time.sleep(.1)
                x = x + 1

        def foo2(data, callback):
            callback([{
                'a': data['a'] * 1000,
                'b': data['b'],
                'c': 'AAPL',
                'x': data['x']
            }])

        p = PerspectiveWidget([],
                              view='y_line',
                              columns=['a', 'b'],
                              rowpivots=['x'],
                              colpivots=['c'])

        t.pipeline([foo1, foo2], ['on_data', 'callback'], on_data=p.update)
        time.sleep(10)
        t.stop()
Example #6
0
 def test_widget_load_column_pivots(self):
     arrays = [
         np.array([
             'bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo',
             'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux'
         ]),
         np.array([
             'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one',
             'one', 'two', 'two', 'one', 'one', 'two', 'two'
         ]),
         np.array([
             'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y',
             'X', 'Y', 'X', 'Y'
         ])
     ]
     tuples = list(zip(*arrays))
     index = pd.MultiIndex.from_tuples(tuples,
                                       names=['first', 'second', 'third'])
     df_both = pd.DataFrame(np.random.randn(3, 16),
                            index=['A', 'B', 'C'],
                            columns=index)
     widget = PerspectiveWidget(df_both)
     assert widget.columns == [' ']
     assert widget.column_pivots == ['first', 'second', 'third']
     assert widget.row_pivots == ['index']
Example #7
0
    def test_widget_delete_with_view(self):
        data = {"a": np.arange(0, 50)}
        widget = PerspectiveWidget(data)

        # create a view on the manager
        table_name, table = list(widget.manager._tables.items())[0]
        make_view_message = {
            "id": 1,
            "table_name": table_name,
            "view_name": "view1",
            "cmd": "view",
            "config": {
                "group_by": ["a"]
            }
        }
        widget.manager._process(make_view_message, lambda x: True)

        assert len(widget.manager._views) == 1

        mocked_post = partial(mock_post, assert_msg={"cmd": "delete"})

        widget.post = MethodType(mocked_post, widget)
        widget.delete()

        assert widget.table is None
 def test_widget_load_split_by_preserve_user_settings(self):
     arrays = [
         np.array([
             'bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo',
             'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux'
         ]),
         np.array([
             'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one',
             'one', 'two', 'two', 'one', 'one', 'two', 'two'
         ]),
         np.array([
             'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y',
             'X', 'Y', 'X', 'Y'
         ])
     ]
     tuples = list(zip(*arrays))
     index = pd.MultiIndex.from_tuples(tuples,
                                       names=['first', 'second', 'third'])
     df_both = pd.DataFrame(np.random.randn(3, 16),
                            index=['A', 'B', 'C'],
                            columns=index)
     widget = PerspectiveWidget(df_both, columns=["first", "third"])
     assert widget.columns == ['first', "third"]
     assert widget.split_by == ['first', 'second', 'third']
     assert widget.group_by == ['index']
    def test_pivottable_values_index(self):
        arrays = {
            'A': [
                'bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo',
                'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux'
            ],
            'B': [
                'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one',
                'one', 'two', 'two', 'one', 'one', 'two', 'two'
            ],
            'C': [
                'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y',
                'X', 'Y', 'X', 'Y'
            ],
            'D':
            np.arange(16)
        }

        df = pd.DataFrame(arrays)
        df_pivot = df.pivot_table(values=['D'],
                                  index=['A'],
                                  columns=['B', 'C'],
                                  aggfunc={'D': 'count'})
        widget = PerspectiveWidget(df_pivot)
        assert widget.columns == ['value']
        assert widget.split_by == ['B', 'C']
        assert widget.group_by == ['A']
Example #10
0
    def test_widget_load_table_df(self):
        table = Table(DF)
        widget = PerspectiveWidget(table)
        assert widget.table.schema() == {
            'Country': str,
            'index': int,
            'Region': str,
            'Category': str,
            'City': str,
            'Customer ID': str,
            'Discount': float,
            'Order Date': date,
            'Order ID': str,
            'Postal Code': str,
            'Product ID': str,
            'Profit': float,
            'Quantity': int,
            'Row ID': int,
            'Sales': int,
            'Segment': str,
            'Ship Date': date,
            'Ship Mode': str,
            'State': str,
            'Sub-Category': str
        }

        assert sorted(widget.columns) == sorted([
            'Category', 'City', 'Country', 'Customer ID', 'Discount', 'index',
            'Order Date', 'Order ID', 'Postal Code', 'Product ID', 'Profit',
            'Quantity', 'Region', 'Row ID', 'Sales', 'Segment', 'Ship Date',
            'Ship Mode', 'State', 'Sub-Category'
        ])
        view = widget.table.view()
        assert view.num_rows() == len(DF)
        assert view.num_columns() == len(DF.columns) + 1  # index column
Example #11
0
 def test_widget_load_column_pivots_client(self):
     # behavior should not change for client mode
     arrays = [
         np.array([
             'bar', 'bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo',
             'foo', 'foo', 'foo', 'qux', 'qux', 'qux', 'qux'
         ]),
         np.array([
             'one', 'one', 'two', 'two', 'one', 'one', 'two', 'two', 'one',
             'one', 'two', 'two', 'one', 'one', 'two', 'two'
         ]),
         np.array([
             'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y', 'X', 'Y',
             'X', 'Y', 'X', 'Y'
         ])
     ]
     tuples = list(zip(*arrays))
     index = pd.MultiIndex.from_tuples(tuples,
                                       names=['first', 'second', 'third'])
     df_both = pd.DataFrame(np.random.randn(3, 16),
                            index=['A', 'B', 'C'],
                            columns=index)
     widget = PerspectiveWidget(df_both, client=True)
     assert widget.table is None
     assert widget.columns == [' ']
     assert widget.column_pivots == ['first', 'second', 'third']
     assert widget.row_pivots == ['index']
Example #12
0
 def test_widget_delete(self):
     data = {"a": np.arange(0, 50)}
     widget = PerspectiveWidget(data)
     mocked_post = partial(mock_post, assert_msg={"cmd": "delete"})
     widget.post = MethodType(mocked_post, widget)
     widget.delete()
     assert widget.table is None
Example #13
0
 def test_widget_client_clear(self):
     data = {"a": np.arange(0, 50)}
     widget = PerspectiveWidget(data, client=True)
     mocked_post = partial(mock_post, assert_msg={"cmd": "clear"})
     widget.post = MethodType(mocked_post, widget)
     widget.clear()
     assert widget._data is None
Example #14
0
 def test_pivottable(self):
     pt = pd.pivot_table(DF, values='Discount', index=['Country', 'Region'], columns='Category')
     psp = PerspectiveWidget(pt)
     assert psp.rowpivots == ['Country', 'Region']
     assert psp.columnpivots == []
     assert psp.columns == ['Financials', 'Industrials', 'Technology']
     assert psp.schema == {'Country': 'string', 'Region': 'string', 'Financials': 'float',
                           'Industrials': 'float', 'Technology': 'float'}
 def test_aggregates_widget_load_weighted_mean(self):
     aggs = {
         "a": Aggregate.AVG,
         "b": ["weighted mean", "a"]
     }
     data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
     widget = PerspectiveWidget(data, aggregates=aggs)
     assert widget.aggregates == aggs
 def test_aggregates_widget_load(self):
     aggs = {
         "a": Aggregate.AVG,
         "b": Aggregate.LAST
     }
     data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
     widget = PerspectiveWidget(data, aggregates=aggs)
     assert widget.aggregates == aggs
Example #17
0
    def test_widget_load_series(self):
        series = pd.Series(DF["Profit"].values, name="profit")
        widget = PerspectiveWidget(series)
        assert widget.table.schema() == {'index': int, 'profit': float}

        assert sorted(widget.columns) == sorted(["index", "profit"])
        view = widget.table.view()
        assert view.num_rows() == len(DF)
        assert view.num_columns() == 2
Example #18
0
 def test_widget_delete_view(self):
     data = {"a": np.arange(0, 50)}
     table = Table(data)
     view = table.view()
     widget = PerspectiveWidget(view)
     mocked_post = partial(mock_post, assert_msg={"cmd": "delete"})
     widget.post = MethodType(mocked_post, widget)
     widget.delete()
     assert widget._view is None
     assert widget.table is None
Example #19
0
 def test_pivottable(self):
     pt = pd.pivot_table(DF, values='Discount', index=['Country', 'Region'], columns='Category')
     psp = PerspectiveWidget(pt)
     assert psp.rowpivots == ['Country', 'Region']
     assert psp.columnpivots == []
     assert psp.columns == ['Consumer Discretionary', 'Consumer Staples', 'Energy', 'Financials',
                            'Health Care', 'Industrials', 'Information Technology', 'Materials', 'Real Estate', 'Telecommunication Services', 'Utilities']
     assert psp.schema == {'Country': 'string', 'Region': 'string', 'Consumer Discretionary': 'float', 'Consumer Staples': 'float', 'Energy': 'float', 'Financials': 'float',
                           'Health Care': 'float', 'Industrials': 'float', 'Information Technology': 'float', 'Materials': 'float', 'Real Estate': 'float',
                           'Telecommunication Services': 'float', 'Utilities': 'float'}
 def test_aggregates_widget_setattr(self):
     data = {"a": [1, 2, 3], "b": ["a", "b", "c"]}
     widget = PerspectiveWidget(data)
     widget.aggregates = {
         "a": Aggregate.ANY,
         "b": Aggregate.LAST
     }
     assert widget.aggregates == {
         "a": "any",
         "b": "last"
     }
Example #21
0
 def test_widget_load_data_df(self):
     widget = PerspectiveWidget(DF)
     assert sorted(widget.columns) == sorted([
         'Category', 'City', 'Country', 'Customer ID', 'Discount', 'index',
         'Order Date', 'Order ID', 'Postal Code', 'Product ID', 'Profit',
         'Quantity', 'Region', 'Row ID', 'Sales', 'Segment', 'Ship Date',
         'Ship Mode', 'State', 'Sub-Category'
     ])
     view = widget.table.view()
     assert view.num_rows() == len(DF)
     assert view.num_columns() == 20
Example #22
0
 def test_widget_schema(self):
     schema = {
         "a": int,
         "b": float,
         "c": bool,
         "d": date,
         "e": datetime,
         "f": str
     }
     widget = PerspectiveWidget(schema)
     assert widget.table.schema() == schema
Example #23
0
 def test_schema_conversion(self):
     psp = PerspectiveWidget(DF2)
     assert psp.schema == {
         'index': 'integer',
         'date': 'date',
         'datetime': 'date',
         'float': 'float',
         'int': 'integer',
         'object': 'string',
         'string': 'date'
     }
Example #24
0
 def test_widget_load_table_server(self):
     table = Table({"a": np.arange(0, 50)})
     widget = PerspectiveWidget(table, server=True)
     load_msg = widget._make_load_message()
     assert load_msg.to_dict() == {
         "id": -2,
         "type": "table",
         "data": {
             "table_name": widget.table_name
         }
     }
Example #25
0
 def test_widget_client_update(self):
     data = {"a": np.arange(0, 50)}
     comparison_data = {"a": [i for i in range(50)]}
     widget = PerspectiveWidget(data, client=True)
     mocked_post = partial(mock_post,
                           assert_msg={
                               "cmd": "update",
                               "data": comparison_data
                           })
     widget.post = MethodType(mocked_post, widget)
     widget.update(data)
     assert widget.table is None
Example #26
0
 def test_widget_eventual_data_server(self):
     widget = PerspectiveWidget(None, plugin="x_bar", server=True)
     assert widget.plugin == "x_bar"
     widget.load({"a": np.arange(0, 50)}, index="a")
     load_msg = widget._make_load_message()
     assert load_msg.to_dict() == {
         "id": -2,
         "type": "table",
         "data": {
             "table_name": widget.table_name,
         }
     }
Example #27
0
 def test_widget_client_replace(self):
     data = {"a": np.arange(0, 50)}
     new_data = {"a": [1]}
     widget = PerspectiveWidget(data, client=True)
     mocked_post = partial(mock_post,
                           assert_msg={
                               "cmd": "replace",
                               "data": new_data
                           })
     widget.post = MethodType(mocked_post, widget)
     widget.replace(new_data)
     assert widget._data is new_data
 def test_widget_load_pivot_table(self):
     pivot_table = pd.pivot_table(DF,
                                  values='Discount',
                                  index=['Country', 'Region'],
                                  columns=['Category', 'Segment'])
     widget = PerspectiveWidget(pivot_table)
     assert widget.group_by == ['Country', 'Region']
     assert widget.split_by == ['Category', 'Segment']
     assert widget.columns == ['value']
     # table should host flattened data
     view = widget.table.view()
     assert view.num_rows() == 60
     assert view.num_columns() == 6
Example #29
0
 def test_widget_load_pivot_table(self):
     pivot_table = pd.pivot_table(DF,
                                  values='Discount',
                                  index=['Country', 'Region'],
                                  columns='Category')
     widget = PerspectiveWidget(pivot_table)
     assert widget.row_pivots == ['Country', 'Region']
     assert widget.column_pivots == []
     assert widget.columns == ['Financials', 'Industrials', 'Technology']
     # table should host flattened data
     view = widget.table.view()
     assert view.num_rows() == 5
     assert view.num_columns() == 6
Example #30
0
 def test_widget(self):
     data = {"a": np.arange(0, 50)}
     widget = PerspectiveWidget(data, plugin="x_bar")
     assert widget.plugin == "x_bar"
     load_msg = widget._make_load_message()
     assert load_msg.to_dict() == {
         "id": -2,
         "type": "table",
         "data": {
             "table_name": widget.table_name,
             "options": {}
         }
     }