Example #1
0
    def create_plot_manager(self, df=None, n_plots=0):
        if df is None:
            df = self.df

        plot_manager = DataFramePlotManager(data_source=df)
        if n_plots > 0:
            config1 = ScatterPlotConfigurator(data_source=df,
                                              plot_title="Plot1")
            config1.x_col_name = "a"
            config1.y_col_name = "b"
            plot_manager._add_new_plot(config1)

        if n_plots > 1:
            config2 = ScatterPlotConfigurator(data_source=df,
                                              plot_title="Plot2")
            config2.x_col_name = "a"
            config2.y_col_name = "b"
            plot_manager._add_new_plot(config2)

        return plot_manager
class TestPlotManagerExportVega(BasePlotManagerExportTester, TestCase,
                                UnittestTools):
    def setUp(self):
        super(TestPlotManagerExportVega, self).setUp()

        if isdir(self.target_dir):
            rmtree(self.target_dir)

        self.converter = "to_vega"
        self.model = DataFramePlotManager(data_source=TEST_DF)
        self.exporter = NonInteractiveExporter(
            df_plotter=self.model, target_dir=self.target_dir,
            export_format=VEGA_FORMAT, export_data=EXPORT_NO
        )
        self.target_filename = "temp.json"
        self.target_file = join(self.target_dir, self.target_filename)

    def test_export_no_plot_no_data_no_file(self):
        content = self.exporter.to_vega()
        self.assertEqual(content[CONTENT_KEY], [])
        self.assertEqual(content[DATASETS_KEY], {})

    def test_export_no_plot_w_data_no_file(self):
        self.exporter.export_data = EXPORT_IN_FILE
        content = self.exporter.to_vega()
        self.assertEqual(content[CONTENT_KEY], [])
        self.assert_rebuild_df(content)

    def test_export_line_plot_w_data(self):
        config = LinePlotConfigurator(data_source=TEST_DF,
                                      plot_title="Plot")
        config.x_col_name = "a"
        config.y_col_name = "b"
        self.model._add_new_plot(config)

        self.exporter.export_data = EXPORT_IN_FILE
        content = self.exporter.to_vega()
        dataset = content[DATASETS_KEY][DEFAULT_DATASET_NAME]
        self.assertIsInstance(dataset["data"], list)
        self.assert_rebuild_df(content, cascading_data=False)
        self.assertEqual(len(content[CONTENT_KEY]), 1)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {'x': {'field': 'a', 'type': 'quantitative'},
                                 'y': {'field': 'b', 'type': 'quantitative'}},
                    'mark': 'line'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)

    def test_export_1_plot_to_file(self):
        config = LinePlotConfigurator(data_source=TEST_DF,
                                      plot_title="Plot")
        config.x_col_name = "a"
        config.y_col_name = "b"
        self.model._add_new_plot(config)

        self.exporter.target_file = self.target_file
        content = self.exporter.to_vega()
        self.assertTrue(isfile(self.target_file))
        file_content = json.load(open(self.target_file))
        self.assertEqual(content, file_content)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {},
                    'encoding': {'x': {'field': 'a', 'type': 'quantitative'},
                                 'y': {'field': 'b', 'type': 'quantitative'}},
                    'mark': 'line'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)
        self.assertEqual(content[DATASETS_KEY], {})

    def test_export_2line_plots_w_data(self):
        config = LinePlotConfigurator(data_source=TEST_DF,
                                      plot_title="Plot")
        config.x_col_name = "a"
        config.y_col_name = "b"
        self.model._add_new_plot(config)

        config = LinePlotConfigurator(data_source=TEST_DF,
                                      plot_title="Plot2")
        config.x_col_name = "b"
        config.y_col_name = "a"
        self.model._add_new_plot(config)

        self.exporter.export_data = EXPORT_IN_FILE
        content = self.exporter.to_vega()
        self.assert_rebuild_df(content)
        self.assertEqual(len(content[CONTENT_KEY]), 2)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {'x': {'field': 'a', 'type': 'quantitative'},
                                 'y': {'field': 'b', 'type': 'quantitative'}},
                    'mark': 'line'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)

        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {'x': {'field': 'b', 'type': 'quantitative'},
                                 'y': {'field': 'a', 'type': 'quantitative'}},
                    'mark': 'line'}
        self.assert_vega_export_equal(content[CONTENT_KEY][1], expected)

    def test_export_2line_plots_w_cascading_data(self):
        config = LinePlotConfigurator(data_source=TEST_DF,
                                      plot_title="Plot")
        config.x_col_name = "a"
        config.y_col_name = "b"
        self.model._add_new_plot(config)

        config = LinePlotConfigurator(data_source=TEST_DF,
                                      plot_title="Plot2")
        config.x_col_name = "b"
        config.y_col_name = "a"
        self.model._add_new_plot(config)

        # In the cascade style, the data is rewritten in the description of
        # every plot:
        self.exporter.export_data = EXPORT_INLINE
        content = self.exporter.to_vega()
        self.assertEqual(content[DATASETS_KEY], {})
        self.assert_rebuild_df(content, cascading_data=True)
        self.assertEqual(len(content[CONTENT_KEY]), 2)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"values": df_to_vega(TEST_DF),
                             IDX_NAME_KEY: None},
                    'encoding': {'x': {'field': 'a', 'type': 'quantitative'},
                                 'y': {'field': 'b', 'type': 'quantitative'}},
                    'mark': 'line'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)

        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"values": df_to_vega(TEST_DF),
                             IDX_NAME_KEY: None},
                    'encoding': {'x': {'field': 'b', 'type': 'quantitative'},
                                 'y': {'field': 'a', 'type': 'quantitative'}},
                    'mark': 'line'}
        self.assert_vega_export_equal(content[CONTENT_KEY][1], expected)

    def test_export_2scatter_plots_w_data(self):
        config = ScatterPlotConfigurator(data_source=TEST_DF,
                                         plot_title="Plot")
        config.x_col_name = "a"
        config.y_col_name = "b"
        self.model._add_new_plot(config)

        config = ScatterPlotConfigurator(data_source=TEST_DF,
                                         plot_title="Plot2")
        config.x_col_name = "b"
        config.y_col_name = "a"
        self.model._add_new_plot(config)

        self.exporter.export_data = EXPORT_IN_FILE
        content = self.exporter.to_vega()
        self.assert_rebuild_df(content)
        self.assertEqual(len(content[CONTENT_KEY]), 2)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {'x': {'field': 'a', 'type': 'quantitative'},
                                 'y': {'field': 'b', 'type': 'quantitative'}},
                    'mark': 'point'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)

        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {'x': {'field': 'b', 'type': 'quantitative'},
                                 'y': {'field': 'a', 'type': 'quantitative'}},
                    'mark': 'point'}
        self.assert_vega_export_equal(content[CONTENT_KEY][1], expected)

    def test_export_2hist_plots_w_data(self):
        config = HistogramPlotConfigurator(data_source=TEST_DF,
                                           plot_title="Plot")
        config.x_col_name = "a"
        self.model._add_new_plot(config)

        config = HistogramPlotConfigurator(data_source=TEST_DF,
                                           plot_title="Plot2")
        config.x_col_name = "b"
        self.model._add_new_plot(config)

        self.exporter.export_data = EXPORT_IN_FILE
        content = self.exporter.to_vega()
        self.assert_rebuild_df(content)
        self.assertEqual(len(content[CONTENT_KEY]), 2)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {
                        "x": {
                          "bin": True,
                          "field": "a",
                          "type": "quantitative"
                        },
                        "y": {
                          "aggregate": "count",
                          "type": "quantitative"
                        }
                    },
                    'mark': 'bar'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)

        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {
                        "x": {
                          "bin": True,
                          "field": "b",
                          "type": "quantitative"
                        },
                        "y": {
                          "aggregate": "count",
                          "type": "quantitative"
                        }
                    },
                    'mark': 'bar'}
        self.assert_vega_export_equal(content[CONTENT_KEY][1], expected)

    def test_export_multi_hist_plots_w_data(self):
        config = MultiHistogramPlotConfigurator(data_source=TEST_DF,
                                                plot_title="Plot {i}")
        config.x_col_names = ["a", "b"]
        self.model._add_new_plots(config)

        self.exporter.export_data = EXPORT_IN_FILE
        content = self.exporter.to_vega()
        self.assert_rebuild_df(content)
        self.assertEqual(len(content[CONTENT_KEY]), 2)
        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {
                        "x": {
                          "bin": True,
                          "field": "a",
                          "type": "quantitative"
                        },
                        "y": {
                          "aggregate": "count",
                          "type": "quantitative"
                        }
                    },
                    'mark': 'bar'}
        self.assert_vega_export_equal(content[CONTENT_KEY][0], expected)

        expected = {'$schema': TARGET_VEGA_SCHEMA,
                    'data': {"name": DEFAULT_DATASET_NAME},
                    'encoding': {
                        "x": {
                          "bin": True,
                          "field": "b",
                          "type": "quantitative"
                        },
                        "y": {
                          "aggregate": "count",
                          "type": "quantitative"
                        }
                    },
                    'mark': 'bar'}
        self.assert_vega_export_equal(content[CONTENT_KEY][1], expected)

    # Assertion methods -------------------------------------------------------

    def assert_vega_export_equal(self, desc1, desc2):
        self.assertEqual(set(desc1.keys()), set(desc2.keys()))
        for key in desc1:
            self.assertEqual(desc1[key], desc2[key],
                             msg="{} unequal".format(key))

    def assert_rebuild_df(self, content, cascading_data=False):
        if cascading_data:
            for desc in content[CONTENT_KEY]:
                dataset = desc["data"]
                idx_col = dataset[IDX_NAME_KEY]
                if idx_col is None:
                    rebuilt_df = DataFrame(dataset["values"]).set_index(
                        "index")
                    rebuilt_df.index.name = None
                else:
                    rebuilt_df = DataFrame(dataset["values"]).set_index(
                        idx_col)

                assert_frame_equal(rebuilt_df, TEST_DF, check_index_type=False,
                                   check_names=False)
        else:
            datasets = content[DATASETS_KEY]
            for name, dataset in datasets.items():
                self.assertIsInstance(dataset["data"], list)
                idx_col = dataset[IDX_NAME_KEY]
                if idx_col is None:
                    rebuilt_df = DataFrame(dataset["data"]).set_index("index")
                    rebuilt_df.index.name = None
                else:
                    rebuilt_df = DataFrame(dataset["data"]).set_index(idx_col)

                assert_frame_equal(rebuilt_df, TEST_DF, check_index_type=False,
                                   check_names=False)