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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185 | class OutputDecay(ModelOutputItem):
def __init__(
self,
intermediaries: EconomicIntermediaries | list[EconomicIntermediaries] = None,
encodings: Encodings | None = None,
max_time: int = 20,
desired_num_points: int = 256,
combine_granularities_flag: bool = False,
use_vehicles: Sequence[str] | None = None,
final_df: pd.DataFrame | list[pd.DataFrame] = None,
is_animation_call: bool = False,
):
super().__init__(final_df, [encodings, intermediaries])
self.final_df = final_df
self.is_animation_call = is_animation_call
if self.final_df is None:
self.encodings = encodings
self.intermediaries = [
RecursiveNamespace.parse(inter) if isinstance(inter, Mapping) else inter
for inter in (intermediaries if isinstance(intermediaries, list) else [intermediaries])
]
self.max_time = max_time
self.desired_num_points = desired_num_points
self.combine_granularities_flag = combine_granularities_flag
def get_decay_df(self, intermediaries: EconomicIntermediaries, cumulative_decay_flag=False):
if intermediaries.impacts.roicurve is None:
return pd.DataFrame()
step = to_numpy(intermediaries.step)
vehicle_names = to_signal_names(intermediaries.impacts.roicurve.signal_names)
decayed_amount, time_points = get_decay_impact_matrix(
to_numpy(intermediaries.impacts.roicurve.betagamma.beta),
to_numpy(intermediaries.impacts.roicurve.betagamma.gamma),
self.max_time,
self.desired_num_points,
cumulative_decay_flag,
)
granularity_names, level_names = get_granularity_names(self.encodings, intermediaries.inputs)
decay_df = pd.DataFrame(
decayed_amount,
columns=pd.MultiIndex.from_tuples(
[(*granularity, "impact", label) for granularity in granularity_names for label in vehicle_names],
names=(*level_names, "value", "vehicle"),
),
index=pd.Index(time_points.reshape(-1), name="week"),
)
step_cols = pd.MultiIndex.from_tuples(
[(*names, "step", vehicle) for names in granularity_names for vehicle in vehicle_names],
names=(*level_names, "value", "vehicle"),
)
return pd.concat(
[
decay_df,
pd.DataFrame(
np.full((len(decay_df.index), len(step_cols)), step, dtype=np.int32),
index=decay_df.index,
columns=step_cols,
),
],
axis=1,
)
@cached_property
def df(
self,
) -> pd.DataFrame:
"""Get decay dataframe"""
if self.final_df is not None:
return self.final_df
decay_dfs = []
for step_intermediary in self.intermediaries:
step_decay_df = self.get_decay_df(step_intermediary)
decay_dfs.append(combine_granularities(step_decay_df) if self.combine_granularities_flag else step_decay_df)
decay_dfs = decay_dfs[0] if len(self.intermediaries) == 1 else decay_dfs
return decay_dfs
@property
def cumulative_df(
self,
) -> pd.DataFrame:
if self.final_df:
return self.final_df
cumulative_decay_dfs = []
for step_intermediary in self.intermediaries:
step_cumulative_decay_df = self.get_decay_df(step_intermediary, True)
cumulative_decay_dfs.append(
combine_granularities(step_cumulative_decay_df)
if self.combine_granularities_flag
else step_cumulative_decay_df
)
return cumulative_decay_dfs
def visualize(
self,
height: int,
wibbles: list[str] = None,
vehicle_names: Sequence[str] | None = None,
wibble_encodings: dict[str, int] | None = None,
vehicle_encodings: dict[str, int] | None = None,
get_range: Callable[[pd.DataFrame, list[str]], tuple[int, int]] = lambda df, cols: (0, 1),
) -> dict[str, go.Figure]:
"""Visualize decay dataframe
Args:
height (int): The height of the line or the scatter plot.
wibbles (list[str]): Wibble names
vehicle_names (Sequence[str] | None, optional): Vehicle names. Defaults to None.
wibble_encodings (dict[str, int] | None, optional): Granularity encodings. Defaults to None.
vehicle_encodings (dict[str, int] | None, optional): Vehcile encodings. Defaults to None.
get_range (Callable[[pd.DataFrame, list[str]], tuple[int, int]], optional): Function to get the range of
values for y axis. Defaults to lambda df, cols: (0, 1).
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 decay dataframe of granularity names.
"""
all_decay_df = self.df
animation_frame = "step" if self.is_animation_call else None
if isinstance(self.df, list):
if self.is_animation_call:
all_decay_df = pd.concat(all_decay_df, axis=0)
else:
all_decay_df = pd.concat(all_decay_df, axis=1)
if wibbles is None:
if wibble_encodings:
wibbles = list(wibble_encodings.keys())
else:
wibbles = all_decay_df.columns.unique("granularity").tolist()
if vehicle_names is None:
if vehicle_encodings is None:
vehicle_names = list(all_decay_df.columns.unique("vehicle"))
else:
vehicle_names = list(vehicle_encodings.keys())
all_decay_df = filter_level_values(all_decay_df, "vehicle", vehicle_names)
all_plots = {}
for name in wibbles:
decay_df = all_decay_df.xs(name, level="granularity", axis=1)
line_names = [name for name in decay_df.columns if name != "step"]
range_y = get_range(decay_df, line_names)
# Scatter animates much more cleanly
plt_fcn = px.line if animation_frame is None else px.scatter
decay_plot = plt_fcn(
decay_df,
x=decay_df.index,
y=line_names,
animation_frame=animation_frame,
animation_group=None if animation_frame is None else decay_df.index,
range_y=range_y,
height=height,
title=name,
)
all_plots[name] = decay_plot
return all_plots
|