OutputMixedEffect

Bases: ModelOutputItem

Source code in wt_ml/output/output_mixed_effect.py
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
class OutputMixedEffect(ModelOutputItem):
    def __init__(
        self,
        curve_values: CurveTrackers | list[CurveTrackers] = None,
        encodings: Encodings | None = None,
        combine_granularities_flag: bool = False,
        final_df: pd.DataFrame | list[pd.DataFrame] = None,
        is_animation_call: bool = False,
    ):
        super().__init__(final_df, [curve_values, encodings])
        self.final_df = final_df
        self.is_animation_call = is_animation_call
        if self.final_df is None:
            self.curve_values = [
                RecursiveNamespace.parse(inter) if isinstance(inter, Mapping) else inter
                for inter in (curve_values if isinstance(curve_values, list) else [curve_values])
            ]
            self.combine_granularities_flag = combine_granularities_flag
            self.encodings = encodings

    def get_mixed_effect_df(
        self,
        curve_values: CurveTrackers,
    ) -> pd.DataFrame:
        index, curve_np = get_mixed_effect_matrix(curve_values)

        index = pd.Index(index, name="spend")
        granularity_names, level_names = get_granularity_names(self.encodings, curve_values.input)
        signal_names = to_signal_names(curve_values.signal_names)
        columns = pd.MultiIndex.from_tuples(
            [
                (*names, signal, vehicle)
                for names in granularity_names
                for signal in ("impact", "slope")
                for vehicle in signal_names
            ],
            names=(*level_names, "value", "signal"),
        )
        me_df = pd.DataFrame(
            curve_np.reshape(index.shape[0], -1),
            index=index,
            columns=columns,
        )
        step_cols = pd.MultiIndex.from_tuples(
            [(*names, "step", signal) for names in granularity_names for signal in signal_names],
            names=(*level_names, "value", "signal"),
        )
        return pd.concat(
            [
                me_df,
                pd.DataFrame(
                    np.full((len(me_df.index), len(step_cols)), to_numpy(curve_values.step), dtype=np.int32),
                    index=me_df.index,
                    columns=step_cols,
                ),
            ],
            axis=1,
        )

    @cached_property
    def df(self) -> pd.DataFrame:
        """Get mixed effects dataframe"""
        if self.final_df is not None:
            return self.final_df

        mixed_effect_dfs = []
        for step_curve_values in self.curve_values:
            step_mixed_effect_df = self.get_mixed_effect_df(step_curve_values)
            mixed_effect_dfs.append(
                combine_granularities(step_mixed_effect_df) if self.combine_granularities_flag else step_mixed_effect_df
            )
        mixed_effect_dfs = mixed_effect_dfs[0] if len(self.curve_values) == 1 else mixed_effect_dfs

        return mixed_effect_dfs

    def visualize(
        self,
        signal_encodings: dict[str, int] = None,
        wibbles: list[str] | None = None,
        wibble_encodings: dict | None = None,
        group_by_granularity: bool = True,
        show_both=False,
        height: int = 800,
        **kwargs,
    ) -> dict[str, go.Figure]:
        """Visualize mixed effects

        Args:
            signal_encodings (dict[str, int]): Signal encodings
            wibbles (list[str], optional): Names of all the wibbles/keys. Defaults to None.
            wibble_encodings (dict[str, int]): Granularity encodings
            group_by_granularity (bool, optional): Flag to indicate whether to group and view the plots by granularity.
            Defaults to True.
            show_both (bool, optional): Flag to indicate whether to group plots both by granularity level and signal
            level. Defaults to False.
            height (int, optional): The height of the line or the scatter plot. Defaults to 800.
            show_plots (bool, optional): Flag to indicate whether to show the plots. Defaults to True.

        Returns:
            dict[str, go.Figure]: Prepared line or scatter plots of mixed effects dataframe of granularity names and/or
            signal names.
        """
        all_plots = {}
        animation_frame = "step" if self.is_animation_call else None
        me_df = self.df
        if isinstance(me_df, list):
            if self.is_animation_call:
                me_df = pd.concat(me_df, axis=0)
            else:
                me_df = pd.concat(me_df, axis=1)

        if wibbles is None:
            if wibble_encodings:
                wibbles = list(wibble_encodings.keys())
            else:
                wibbles = me_df.columns.unique("granularity").tolist()

        if signal_encodings is None:
            signal_encodings = {signal: i for i, signal in enumerate(me_df.columns.unique("signal"))}

        if group_by_granularity:
            all_plots = make_plots(
                me_df,
                wibbles,
                "granularity",
                list(signal_encodings.keys()),
                animation_frame,
                height,
                **kwargs,
            )
        if not group_by_granularity or show_both:
            all_plots |= make_plots(
                me_df,
                list(signal_encodings.keys()),
                "signal",
                wibbles,
                animation_frame,
                height,
                **kwargs,
            )

        return all_plots

df: pd.DataFrame cached property

Get mixed effects dataframe

visualize(signal_encodings=None, wibbles=None, wibble_encodings=None, group_by_granularity=True, show_both=False, height=800, **kwargs)

Visualize mixed effects

Parameters:

Name Type Description Default
signal_encodings dict[str, int]

Signal encodings

None
wibbles list[str]

Names of all the wibbles/keys. Defaults to None.

None
wibble_encodings dict[str, int]

Granularity encodings

None
group_by_granularity bool

Flag to indicate whether to group and view the plots by granularity.

True
show_both bool

Flag to indicate whether to group plots both by granularity level and signal

False
height int

The height of the line or the scatter plot. Defaults to 800.

800
show_plots bool

Flag to indicate whether to show the plots. Defaults to True.

required

Returns:

Type Description
dict[str, Figure]

dict[str, go.Figure]: Prepared line or scatter plots of mixed effects dataframe of granularity names and/or

dict[str, Figure]

signal names.

Source code in wt_ml/output/output_mixed_effect.py
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def visualize(
    self,
    signal_encodings: dict[str, int] = None,
    wibbles: list[str] | None = None,
    wibble_encodings: dict | None = None,
    group_by_granularity: bool = True,
    show_both=False,
    height: int = 800,
    **kwargs,
) -> dict[str, go.Figure]:
    """Visualize mixed effects

    Args:
        signal_encodings (dict[str, int]): Signal encodings
        wibbles (list[str], optional): Names of all the wibbles/keys. Defaults to None.
        wibble_encodings (dict[str, int]): Granularity encodings
        group_by_granularity (bool, optional): Flag to indicate whether to group and view the plots by granularity.
        Defaults to True.
        show_both (bool, optional): Flag to indicate whether to group plots both by granularity level and signal
        level. Defaults to False.
        height (int, optional): The height of the line or the scatter plot. Defaults to 800.
        show_plots (bool, optional): Flag to indicate whether to show the plots. Defaults to True.

    Returns:
        dict[str, go.Figure]: Prepared line or scatter plots of mixed effects dataframe of granularity names and/or
        signal names.
    """
    all_plots = {}
    animation_frame = "step" if self.is_animation_call else None
    me_df = self.df
    if isinstance(me_df, list):
        if self.is_animation_call:
            me_df = pd.concat(me_df, axis=0)
        else:
            me_df = pd.concat(me_df, axis=1)

    if wibbles is None:
        if wibble_encodings:
            wibbles = list(wibble_encodings.keys())
        else:
            wibbles = me_df.columns.unique("granularity").tolist()

    if signal_encodings is None:
        signal_encodings = {signal: i for i, signal in enumerate(me_df.columns.unique("signal"))}

    if group_by_granularity:
        all_plots = make_plots(
            me_df,
            wibbles,
            "granularity",
            list(signal_encodings.keys()),
            animation_frame,
            height,
            **kwargs,
        )
    if not group_by_granularity or show_both:
        all_plots |= make_plots(
            me_df,
            list(signal_encodings.keys()),
            "signal",
            wibbles,
            animation_frame,
            height,
            **kwargs,
        )

    return all_plots