Skip to content

Commit

Permalink
added plot_is_static, colors, timeseries notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
AlxdrPolyakov committed Feb 2, 2024
1 parent c271b58 commit 70b0b97
Show file tree
Hide file tree
Showing 7 changed files with 145,924 additions and 183 deletions.
145,515 changes: 145,515 additions & 0 deletions data/synth_time_data.csv

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions notebooks/Finding interesting segments (continuous segments).ipynb

Large diffs are not rendered by default.

262 changes: 262 additions & 0 deletions notebooks/Finding interesting segments in time series.ipynb

Large diffs are not rendered by default.

235 changes: 82 additions & 153 deletions notebooks/Finding interesting segments.ipynb

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions wise_pizza/explain.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,16 @@ def explain_timeseries(
assert almost_equals(fitted_sizes, sf2.weights)

out = SlicerPair(sf1, sf2)
out.plot = lambda width=600, height=1200, average_name=None, use_fitted_weights=False: plot_ts_pair(
out.relevant_cluster_names1 = sf1.relevant_cluster_names
out.relevant_cluster_names2 = sf2.relevant_cluster_names

out.plot = lambda plot_is_static=False, width=600, height=1200, return_fig=False, average_name=None, use_fitted_weights=False: plot_ts_pair(
out.s1,
out.s2,
plot_is_static=plot_is_static,
width=width,
height=height,
return_fig=return_fig,
average_name=average_name,
use_fitted_weights=use_fitted_weights,
)
Expand Down Expand Up @@ -557,7 +562,7 @@ def _explain_timeseries(
for c in chosen_cols:
pre_basis[c + "_a"] = pre_basis["Slope"] - pre_basis[c]

print("yay!")
# print("yay!")

df, avg_df = add_average_over_time(
df,
Expand Down Expand Up @@ -632,10 +637,10 @@ def _explain_timeseries(
# sf.reg.intercept_ += average
sf.plot = lambda plot_is_static=False, width=1200, height=2000, return_fig=False, average_name=None: plot_time(
sf,
# plot_is_static=plot_is_static,
plot_is_static=plot_is_static,
width=width,
height=height,
# return_fig=return_fig,
return_fig=return_fig,
average_name=average_name,
)
sf.task = "time"
Expand Down
48 changes: 38 additions & 10 deletions wise_pizza/plotting_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import numpy as np
import pandas as pd
from IPython.display import Image, display
import plotly.io as pio
from plotly.io import to_image
from plotly import graph_objects as go
from plotly.subplots import make_subplots

Expand All @@ -25,6 +28,8 @@ def plot_time(
width: int = 1000,
height: int = 1000,
average_name: Optional[str] = None,
plot_is_static: bool = False,
return_fig: bool = False,
):
plot_data = preprocess_for_ts_plot(sf, average_name)
num_rows = len(plot_data.nonflat_segments) + 1
Expand All @@ -48,7 +53,14 @@ def plot_time(
width=width,
height=height,
)
fig.show()
if plot_is_static:
image_bytes = to_image(fig, format="png", scale=2)
return Image(image_bytes, height=height, width=width)
else:
if return_fig:
return fig
else:
fig.show()


def plot_ts_pair(
Expand All @@ -57,6 +69,8 @@ def plot_ts_pair(
width,
height,
average_name: str = None,
plot_is_static: bool = False,
return_fig: bool = False,
use_fitted_weights: bool = False,
):
# if use_fitted_weights:
Expand Down Expand Up @@ -102,7 +116,15 @@ def plot_ts_pair(
width=width,
height=height,
)
fig.show()

if plot_is_static:
image_bytes = to_image(fig, format="png", scale=2)
return Image(image_bytes, height=height, width=width)
else:
if return_fig:
return fig
else:
fig.show()


def plot_single_ts(
Expand Down Expand Up @@ -246,13 +268,19 @@ def preprocess_for_ts_plot(
global_time_label = ""

seg_names = ["All" + global_time_label] + [
str(drop_time(s["segment"])) for s in nonflat_segments
drop_time(s["segment"]) for s in nonflat_segments
]
sub_titles = [
[
f"{sf.size_name} for {s} ",
f"{average_name} for {s}",
f"{sf.total_name} for {s}",
f"{sf.size_name} for <br>" + f"{s}",
f"{average_name} for <br>" + f"{s}",
f"{sf.total_name} for <br>" + f"{s}",
]
if s != "All" and s != global_time_label
else [
f"{sf.size_name} for " + "<br>".join([key for key in s]),
f"{average_name} for " + "<br>".join([key for key in s]),
f"{sf.total_name} for " + "<br>".join([key for key in s]),
]
for s in seg_names
]
Expand Down Expand Up @@ -306,7 +334,7 @@ def simple_ts_plot(
x=time,
y=totals * mult,
name=f"Actuals",
marker=dict(color="orange"),
marker=dict(color="#ffc091"),
showlegend=showlegend and col == col_nums[0],
),
row=row_num,
Expand All @@ -319,7 +347,7 @@ def simple_ts_plot(
y=reg_totals * mult,
mode="lines",
name=f"Regression",
line=dict(color="blue"),
line=dict(color="#a0e1e1"),
showlegend=showlegend and col == col_nums[0],
),
row=row_num,
Expand All @@ -332,7 +360,7 @@ def simple_ts_plot(
y=reg_seg * mult,
mode="lines",
name=f"Segment's reg contribution (Right axis)",
line=dict(color="teal"),
line=dict(color="#9fe870"),
showlegend=showlegend and col == col_nums[0],
),
row=row_num,
Expand All @@ -345,7 +373,7 @@ def simple_ts_plot(
x=time,
y=leftover_totals if col == 1 else leftover_avgs,
name=f"Leftover actuals",
marker=dict(color="purple"),
marker=dict(color="#ff685f"),
showlegend=showlegend and col == col_nums[0],
),
row=row_num,
Expand Down
2 changes: 2 additions & 0 deletions wise_pizza/slicer_facades.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def __init__(
# Now let's make sure single-segment impacts add up to total impact
self.segment_mult = 1.0

self.relevant_cluster_names = sf.relevant_cluster_names

# Try to make individual segment impacts add up to total regression post-transform
# Didn't really make much difference
# sum_marginals = 0
Expand Down

0 comments on commit 70b0b97

Please sign in to comment.