示例#1
0
def register_new_action(validator: bool = True):
    df = pd.read_csv("lux/data/car.csv")

    def random_categorical(ldf):
        intent = [lux.Clause("?", data_type="nominal")]
        vlist = VisList(intent, ldf)
        for vis in vlist:
            vis.score = 10
        vlist = vlist.topK(15)
        return {
            "action": "bars",
            "description": "Random list of Bar charts",
            "collection": vlist
        }

    def contain_horsepower(df):
        for clause in df.intent:
            if clause.get_attr() == "Horsepower":
                return True
        return False

    if validator:
        lux.register_action("bars", random_categorical, contain_horsepower)
    else:
        lux.register_action("bars", random_categorical)
    return df
示例#2
0
def test_invalid_validator():
    df = pd.read_csv("lux/data/car.csv")

    def random_categorical(ldf):
        intent = [lux.Clause("?", data_type="nominal")]
        vlist = VisList(intent, ldf)
        for vis in vlist:
            vis.score = 10
        vlist = vlist.topK(15)
        return {
            "action": "bars",
            "description": "Random list of Bar charts",
            "collection": vlist
        }

    with pytest.raises(ValueError, match="Value must be a callable"):
        lux.register_action("bars", random_categorical, "not a Callable")
示例#3
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
示例#4
0
def test_invalid_function():
    df = pd.read_csv("lux/data/car.csv")
    with pytest.raises(ValueError, match="Value must be a callable"):
        lux.register_action("bars", "not a Callable")