Exemplo n.º 1
0
    def show_more(self):
        from lux.action.custom import custom
        from lux.action.correlation import correlation
        from lux.action.univariate import univariate
        from lux.action.enhance import enhance
        from lux.action.filter import filter
        from lux.action.generalize import generalize
        from lux.action.row_group import row_group
        from lux.action.column_group import column_group

        self._rec_info = []
        if (self.pre_aggregated):
            if (self.columns.name is not None):
                self._append_recInfo(row_group(self))
            if (self.index.name is not None):
                self._append_recInfo(column_group(self))
        else:
            if (self.current_vis is None):
                no_vis = True
                one_current_vis = False
                multiple_current_vis = False
            else:
                no_vis = len(self.current_vis) == 0
                one_current_vis = len(self.current_vis) == 1
                multiple_current_vis = len(self.current_vis) > 1

            if (no_vis):
                self._append_recInfo(correlation(self))
                self._append_recInfo(univariate(self, "quantitative"))
                self._append_recInfo(univariate(self, "nominal"))
                self._append_recInfo(univariate(self, "temporal"))
            elif (one_current_vis):
                self._append_recInfo(enhance(self))
                self._append_recInfo(filter(self))
                self._append_recInfo(generalize(self))
            elif (multiple_current_vis):
                self._append_recInfo(custom(self))

        # Store _rec_info into a more user-friendly dictionary form
        self.recommendation = {}
        for rec_info in self._rec_info:
            action_type = rec_info["action"]
            vc = rec_info["collection"]
            if (self.plot_config):
                for vis in self.current_vis:
                    vis.plot_config = self.plot_config
                for vis in vc:
                    vis.plot_config = self.plot_config
            if (len(vc) > 0):
                self.recommendation[action_type] = vc

        self.clear_filter()
Exemplo n.º 2
0
    def maintain_recs(self):
        # `rec_df` is the dataframe to generate the recommendations on
        # check to see if globally defined actions have been registered/removed
        if lux.update_actions["flag"] == True:
            self._recs_fresh = False
        show_prev = False  # flag indicating whether rec_df is showing previous df or current self
        if self._prev is not None:
            rec_df = self._prev
            rec_df._message = Message()
            rec_df.maintain_metadata()  # the prev dataframe may not have been printed before
            last_event = self.history._events[-1].name
            rec_df._message.add(
                f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>."
            )
            show_prev = True
        else:
            rec_df = self
            rec_df._message = Message()
        # Add warning message if there exist ID fields
        id_fields_str = ""
        if len(rec_df.data_type["id"]) > 0:
            for id_field in rec_df.data_type["id"]:
                id_fields_str += f"<code>{id_field}</code>, "
            id_fields_str = id_fields_str[:-2]
            rec_df._message.add(f"{id_fields_str} is not visualized since it resembles an ID field.")
        rec_df._prev = None  # reset _prev

        # Check that recs has not yet been computed
        if not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh:
            rec_infolist = []
            from lux.action.custom import custom
            from lux.action.custom import custom_actions
            from lux.action.correlation import correlation
            from lux.action.univariate import univariate
            from lux.action.enhance import enhance
            from lux.action.filter import filter
            from lux.action.generalize import generalize
            from lux.action.row_group import row_group
            from lux.action.column_group import column_group

            if rec_df.pre_aggregated:
                if rec_df.columns.name is not None:
                    rec_df._append_rec(rec_infolist, row_group(rec_df))
                rec_df._append_rec(rec_infolist, column_group(rec_df))
            else:
                if rec_df.recommendation == {}:
                    # display conditions for default actions
                    no_vis = lambda ldf: (ldf.current_vis is None) or (
                        ldf.current_vis is not None and len(ldf.current_vis) == 0
                    )
                    one_current_vis = (
                        lambda ldf: ldf.current_vis is not None and len(ldf.current_vis) == 1
                    )
                    multiple_current_vis = (
                        lambda ldf: ldf.current_vis is not None and len(ldf.current_vis) > 1
                    )

                    # globally register default actions
                    lux.register_action("correlation", correlation, no_vis)
                    lux.register_action("distribution", univariate, no_vis, "quantitative")
                    lux.register_action("occurrence", univariate, no_vis, "nominal")
                    lux.register_action("temporal", univariate, no_vis, "temporal")

                    lux.register_action("Enhance", enhance, one_current_vis)
                    lux.register_action("Filter", filter, one_current_vis)
                    lux.register_action("Generalize", generalize, one_current_vis)

                    lux.register_action("Custom", custom, multiple_current_vis)

                # generate vis from globally registered actions and append to dataframe
                custom_action_collection = custom_actions(rec_df)
                for rec in custom_action_collection:
                    rec_df._append_rec(rec_infolist, rec)
                lux.update_actions["flag"] = False

            # Store _rec_info into a more user-friendly dictionary form
            rec_df.recommendation = {}
            for rec_info in rec_infolist:
                action_type = rec_info["action"]
                vlist = rec_info["collection"]
                if len(vlist) > 0:
                    rec_df.recommendation[action_type] = vlist
            rec_df._rec_info = rec_infolist
            self._widget = rec_df.render_widget()
        # re-render widget for the current dataframe if previous rec is not recomputed
        elif show_prev:
            self._widget = rec_df.render_widget()
        self._recs_fresh = True
Exemplo n.º 3
0
    def maintain_recs(self):
        # `rec_df` is the dataframe to generate the recommendations on
        show_prev = False  # flag indicating whether rec_df is showing previous df or current self
        if self._prev is not None:
            rec_df = self._prev
            rec_df._message = Message()
            rec_df.maintain_metadata(
            )  # the prev dataframe may not have been printed before
            last_event = self.history._events[-1].name
            rec_df._message.append(
                f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>."
            )
            show_prev = True
        else:
            rec_df = self
            rec_df._message = Message()
        # Add warning message if there exist ID fields
        id_fields_str = ""
        if (len(rec_df.data_type["id"]) > 0):
            for id_field in rec_df.data_type["id"]:
                id_fields_str += f"<code>{id_field}</code>, "
            id_fields_str = id_fields_str[:-2]
            rec_df._message.append(
                f"{id_fields_str} is not visualized since it resembles an ID field."
            )
        rec_df._prev = None  # reset _prev

        if (not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh
            ):  # Check that recs has not yet been computed
            rec_infolist = []
            from lux.action.custom import custom
            from lux.action.correlation import correlation
            from lux.action.univariate import univariate
            from lux.action.enhance import enhance
            from lux.action.filter import filter
            from lux.action.generalize import generalize
            from lux.action.row_group import row_group
            from lux.action.column_group import column_group
            if (rec_df.pre_aggregated):
                if (rec_df.columns.name is not None):
                    rec_df._append_rec(rec_infolist, row_group(rec_df))
                if (rec_df.index.name is not None):
                    rec_df._append_rec(rec_infolist, column_group(rec_df))
            else:
                if (rec_df.current_vis is None):
                    no_vis = True
                    one_current_vis = False
                    multiple_current_vis = False
                else:
                    no_vis = len(rec_df.current_vis) == 0
                    one_current_vis = len(rec_df.current_vis) == 1
                    multiple_current_vis = len(rec_df.current_vis) > 1

                if (no_vis):
                    rec_df._append_rec(rec_infolist, correlation(rec_df))
                    rec_df._append_rec(rec_infolist,
                                       univariate(rec_df, "quantitative"))
                    rec_df._append_rec(rec_infolist,
                                       univariate(rec_df, "nominal"))
                    rec_df._append_rec(rec_infolist,
                                       univariate(rec_df, "temporal"))
                elif (one_current_vis):
                    rec_df._append_rec(rec_infolist, enhance(rec_df))
                    rec_df._append_rec(rec_infolist, filter(rec_df))
                    rec_df._append_rec(rec_infolist, generalize(rec_df))
                elif (multiple_current_vis):
                    rec_df._append_rec(rec_infolist, custom(rec_df))

            # Store _rec_info into a more user-friendly dictionary form
            rec_df.recommendation = {}
            for rec_info in rec_infolist:
                action_type = rec_info["action"]
                vlist = rec_info["collection"]
                if (rec_df._plot_config):
                    for vis in rec_df.current_vis:
                        vis._plot_config = rec_df.plot_config
                    for vis in vlist:
                        vis._plot_config = rec_df.plot_config
                if (len(vlist) > 0):
                    rec_df.recommendation[action_type] = vlist
            rec_df._rec_info = rec_infolist
            self._widget = rec_df.render_widget()
        elif (
                show_prev
        ):  # re-render widget for the current dataframe if previous rec is not recomputed
            self._widget = rec_df.render_widget()
        self._recs_fresh = True
Exemplo n.º 4
0
    def maintain_recs(self, is_series="DataFrame"):
        # `rec_df` is the dataframe to generate the recommendations on
        # check to see if globally defined actions have been registered/removed
        if lux.config.update_actions["flag"] == True:
            self._recs_fresh = False
        show_prev = False  # flag indicating whether rec_df is showing previous df or current self
        if self._prev is not None:
            rec_df = self._prev
            rec_df._message = Message()
            rec_df.maintain_metadata(
            )  # the prev dataframe may not have been printed before
            last_event = self.history._events[-1].name
            rec_df._message.add(
                f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>."
            )
            show_prev = True
        else:
            rec_df = self
            rec_df._message = Message()
        # Add warning message if there exist ID fields
        if len(rec_df) == 0:
            rec_df._message.add(f"Lux cannot operate on an empty {is_series}.")
        elif len(rec_df) < 5 and not rec_df.pre_aggregated:
            rec_df._message.add(
                f"The {is_series} is too small to visualize. To generate visualizations in Lux, the {is_series} must contain at least 5 rows."
            )
        elif self.index.nlevels >= 2 or self.columns.nlevels >= 2:
            rec_df._message.add(
                f"Lux does not currently support visualizations in a {is_series} "
                f"with hierarchical indexes.\n"
                f"Please convert the {is_series} into a flat "
                f"table via pandas.DataFrame.reset_index.")
        else:
            id_fields_str = ""
            inverted_data_type = lux.config.executor.invert_data_type(
                rec_df.data_type)
            if len(inverted_data_type["id"]) > 0:
                for id_field in inverted_data_type["id"]:
                    id_fields_str += f"<code>{id_field}</code>, "
                id_fields_str = id_fields_str[:-2]
                rec_df._message.add(
                    f"{id_fields_str} is not visualized since it resembles an ID field."
                )

        rec_df._prev = None  # reset _prev

        # Check that recs has not yet been computed
        if not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh:
            is_sql_tbl = lux.config.executor.name == "SQLExecutor"
            rec_infolist = []
            from lux.action.row_group import row_group
            from lux.action.column_group import column_group

            # TODO: Rewrite these as register action inside default actions
            if rec_df.pre_aggregated:
                if rec_df.columns.name is not None:
                    rec_df._append_rec(rec_infolist, row_group(rec_df))
                rec_df._append_rec(rec_infolist, column_group(rec_df))
            elif not (len(rec_df) < 5 and not rec_df.pre_aggregated
                      and not is_sql_tbl) and not (self.index.nlevels >= 2 or
                                                   self.columns.nlevels >= 2):
                from lux.action.custom import custom_actions

                # generate vis from globally registered actions and append to dataframe
                custom_action_collection = custom_actions(rec_df)
                for rec in custom_action_collection:
                    rec_df._append_rec(rec_infolist, rec)
                lux.config.update_actions["flag"] = False

            # Store _rec_info into a more user-friendly dictionary form
            rec_df._recommendation = {}
            for rec_info in rec_infolist:
                action_type = rec_info["action"]
                vlist = rec_info["collection"]
                if len(vlist) > 0:
                    rec_df._recommendation[action_type] = vlist
            rec_df._rec_info = rec_infolist
            rec_df.show_all_column_vis()
            self._widget = rec_df.render_widget()
        # re-render widget for the current dataframe if previous rec is not recomputed
        elif show_prev:
            rec_df.show_all_column_vis()
            self._widget = rec_df.render_widget()
        self._recs_fresh = True
Exemplo n.º 5
0
    def maintain_recs(self):
        # `rec_df` is the dataframe to generate the recommendations on
        # check to see if globally defined actions have been registered/removed
        if lux.config.update_actions["flag"] == True:
            self._recs_fresh = False
        show_prev = False  # flag indicating whether rec_df is showing previous df or current self
        if self._prev is not None:
            rec_df = self._prev
            rec_df._message = Message()
            rec_df.maintain_metadata(
            )  # the prev dataframe may not have been printed before
            last_event = self.history._events[-1].name
            rec_df._message.add(
                f"Lux is visualizing the previous version of the dataframe before you applied <code>{last_event}</code>."
            )
            show_prev = True
        else:
            rec_df = self
            rec_df._message = Message()
        # Add warning message if there exist ID fields
        id_fields_str = ""
        inverted_data_type = lux.config.executor.invert_data_type(
            rec_df.data_type)
        if len(inverted_data_type["id"]) > 0:
            for id_field in inverted_data_type["id"]:
                id_fields_str += f"<code>{id_field}</code>, "
            id_fields_str = id_fields_str[:-2]
            rec_df._message.add(
                f"{id_fields_str} is not visualized since it resembles an ID field."
            )
        rec_df._prev = None  # reset _prev

        # Check that recs has not yet been computed
        if not hasattr(rec_df, "_recs_fresh") or not rec_df._recs_fresh:
            rec_infolist = []
            from lux.action.row_group import row_group
            from lux.action.column_group import column_group

            # TODO: Rewrite these as register action inside default actions
            if rec_df.pre_aggregated:
                if rec_df.columns.name is not None:
                    rec_df._append_rec(rec_infolist, row_group(rec_df))
                rec_df._append_rec(rec_infolist, column_group(rec_df))
            else:
                # if rec_df._recommendation == {}:
                from lux.action.custom import custom_actions

                # generate vis from globally registered actions and append to dataframe
                custom_action_collection = custom_actions(rec_df)
                for rec in custom_action_collection:
                    rec_df._append_rec(rec_infolist, rec)
                lux.config.update_actions["flag"] = False

            # Store _rec_info into a more user-friendly dictionary form
            rec_df._recommendation = {}
            for rec_info in rec_infolist:
                action_type = rec_info["action"]
                vlist = rec_info["collection"]
                if len(vlist) > 0:
                    rec_df._recommendation[action_type] = vlist
            rec_df._rec_info = rec_infolist
            self._widget = rec_df.render_widget()
        # re-render widget for the current dataframe if previous rec is not recomputed
        elif show_prev:
            self._widget = rec_df.render_widget()
        self._recs_fresh = True