|
import streamlit as st
|
|
from numerize.numerize import numerize
|
|
import numpy as np
|
|
from functools import partial
|
|
from collections import OrderedDict
|
|
from plotly.subplots import make_subplots
|
|
import plotly.graph_objects as go
|
|
from datetime import datetime, timedelta
|
|
from dateutil.relativedelta import relativedelta
|
|
import time
|
|
import Streamlit_functions as sf
|
|
from utilities import (
|
|
format_numbers,
|
|
format_numbers_f,
|
|
load_local_css,
|
|
set_header,
|
|
initialize_data,
|
|
load_authenticator,
|
|
send_email,
|
|
channel_name_formating,
|
|
)
|
|
from io import BytesIO
|
|
|
|
|
|
import warnings
|
|
|
|
|
|
warnings.filterwarnings("ignore")
|
|
warnings.filterwarnings("ignore", category=UserWarning, message="The widget with key")
|
|
|
|
|
|
from classes import class_from_dict, class_to_dict
|
|
import pickle
|
|
import streamlit_authenticator as stauth
|
|
import yaml
|
|
from yaml import SafeLoader
|
|
import re
|
|
import pandas as pd
|
|
import plotly.express as px
|
|
import response_curves_model_quality as rc
|
|
|
|
st.set_page_config(layout="wide")
|
|
load_local_css("styles.css")
|
|
set_header()
|
|
|
|
from pptx import Presentation
|
|
from pptx.util import Inches
|
|
from io import BytesIO
|
|
import plotly.io as pio
|
|
|
|
|
|
for k, v in st.session_state.items():
|
|
if k not in ["logout", "login", "config"] and not k.startswith("FormSubmitter"):
|
|
st.session_state[k] = v
|
|
|
|
|
|
|
|
|
|
|
|
def save_report_forecast(forecasted_table_df, forecasted_table_df2):
|
|
|
|
excel_file = BytesIO()
|
|
with pd.ExcelWriter(excel_file, engine="openpyxl") as writer:
|
|
forecasted_table_df.to_excel(writer, index=True, sheet_name="Forecasted Spends")
|
|
forecasted_table_df2.to_excel(
|
|
writer, sheet_name="Monthly Breakdown", index=True
|
|
)
|
|
|
|
excel_file.seek(0)
|
|
return excel_file
|
|
|
|
|
|
def save_ppt_file(summary_df_sorted, fig1, fig2, fig3):
|
|
summary_df_sorted.index = summary_df_sorted["Channel_name"]
|
|
|
|
prs = Presentation()
|
|
|
|
|
|
def add_plotly_chart_to_slide(slide, fig, left, top, width, height):
|
|
img_stream = BytesIO()
|
|
pio.write_image(fig, img_stream, format="png")
|
|
slide.shapes.add_picture(img_stream, left, top, width, height)
|
|
|
|
for i in range(0, len(channels_list)):
|
|
|
|
slide_1 = prs.slides.add_slide(prs.slide_layouts[6])
|
|
fig = rc.response_curves(
|
|
channels_list[i],
|
|
summary_df_sorted["Optimized_spend"][channels_list[i]],
|
|
summary_df_sorted["New_sales"][channels_list[i]],
|
|
)
|
|
add_plotly_chart_to_slide(
|
|
slide_1, fig, Inches(0.1), Inches(0.1), width=Inches(9), height=Inches(7)
|
|
)
|
|
|
|
|
|
fig1.update_layout(
|
|
legend=dict(
|
|
orientation="h",
|
|
yanchor="top",
|
|
y=-0.4,
|
|
xanchor="center",
|
|
x=0.5,
|
|
)
|
|
)
|
|
|
|
fig2.update_layout(
|
|
legend=dict(
|
|
orientation="h",
|
|
yanchor="top",
|
|
y=-0.4,
|
|
xanchor="center",
|
|
x=0.5,
|
|
)
|
|
)
|
|
|
|
fig3.update_layout(
|
|
legend=dict(
|
|
orientation="h",
|
|
yanchor="top",
|
|
y=-0.4,
|
|
xanchor="center",
|
|
x=0.5,
|
|
)
|
|
)
|
|
|
|
slide_1 = prs.slides.add_slide(prs.slide_layouts[6])
|
|
|
|
add_plotly_chart_to_slide(
|
|
slide_1, fig1, Inches(0.1), Inches(1), width=Inches(9.5), height=Inches(6)
|
|
)
|
|
slide_1 = prs.slides.add_slide(prs.slide_layouts[6])
|
|
add_plotly_chart_to_slide(
|
|
slide_1, fig2, Inches(0.1), Inches(1), width=Inches(9.5), height=Inches(6)
|
|
)
|
|
slide_1 = prs.slides.add_slide(prs.slide_layouts[6])
|
|
add_plotly_chart_to_slide(
|
|
slide_1, fig3, Inches(0.1), Inches(1), width=Inches(9.5), height=Inches(6)
|
|
)
|
|
|
|
|
|
ppt_stream = BytesIO()
|
|
prs.save(ppt_stream)
|
|
ppt_stream.seek(0)
|
|
|
|
return ppt_stream.getvalue()
|
|
|
|
|
|
def first_day_of_next_year(date):
|
|
next_year = date.year + 1
|
|
first_day = datetime(next_year, 1, 1).date()
|
|
|
|
|
|
last_day = first_day + relativedelta(years=1, days=-1)
|
|
|
|
return first_day, last_day
|
|
|
|
|
|
def first_day_of_next_quarter(date):
|
|
current_quarter = (date.month - 1) // 3 + 1
|
|
next_quarter_first_month = ((current_quarter % 4) * 3) + 1
|
|
next_quarter_year = date.year if next_quarter_first_month > 1 else date.year + 1
|
|
|
|
if next_quarter_first_month < 1 or next_quarter_first_month > 12:
|
|
raise ValueError(
|
|
"Calculated month is out of range: {}".format(next_quarter_first_month)
|
|
)
|
|
|
|
first_day_next_quarter = datetime(
|
|
next_quarter_year, next_quarter_first_month, 1
|
|
).date()
|
|
last_day_next_quarter = (
|
|
first_day_next_quarter + relativedelta(months=3)
|
|
) - relativedelta(days=1)
|
|
|
|
return first_day_next_quarter, last_day_next_quarter
|
|
|
|
|
|
def first_day_of_next_month(date):
|
|
next_month_date = date + relativedelta(months=1)
|
|
first_day_next_month = next_month_date.replace(day=1)
|
|
last_day_next_month = (
|
|
first_day_next_month + relativedelta(months=1)
|
|
) - relativedelta(days=1)
|
|
return first_day_next_month, last_day_next_month
|
|
|
|
|
|
def optimize(key, status_placeholder):
|
|
"""
|
|
Optimize the spends for the sales
|
|
"""
|
|
|
|
channel_list = [
|
|
key for key, value in st.session_state["optimization_channels"].items() if value
|
|
]
|
|
|
|
if len(channel_list) > 0:
|
|
scenario = st.session_state["scenario"]
|
|
if key.lower() == "media spends":
|
|
with status_placeholder:
|
|
with st.spinner("Optimizing"):
|
|
|
|
|
|
result = st.session_state["scenario"].optimize(
|
|
st.session_state["total_spends_change"],
|
|
channel_list,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
else:
|
|
with status_placeholder:
|
|
with st.spinner("Optimizing"):
|
|
|
|
result = st.session_state["scenario"].optimize_spends(
|
|
st.session_state["total_sales_change"], channel_list
|
|
)
|
|
for channel_name, modified_spends in result:
|
|
|
|
st.session_state[channel_name] = numerize(
|
|
modified_spends * 1.0,
|
|
1,
|
|
)
|
|
prev_spends = (
|
|
st.session_state["scenario"].channels[channel_name].actual_total_spends
|
|
)
|
|
st.session_state[f"{channel_name}_change"] = round(
|
|
100 * (modified_spends - prev_spends) / prev_spends, 2
|
|
)
|
|
|
|
|
|
def save_scenario(scenario_name):
|
|
"""
|
|
Save the current scenario with the mentioned name in the session state
|
|
|
|
Parameters
|
|
----------
|
|
scenario_name
|
|
Name of the scenario to be saved
|
|
"""
|
|
if "saved_scenarios" not in st.session_state:
|
|
st.session_state = OrderedDict()
|
|
|
|
|
|
st.session_state["saved_scenarios"][scenario_name] = class_to_dict(
|
|
st.session_state["scenario"]
|
|
)
|
|
st.session_state["scenario_input"] = ""
|
|
|
|
|
|
with open("../saved_scenarios.pkl", "wb") as f:
|
|
pickle.dump(st.session_state["saved_scenarios"], f)
|
|
|
|
|
|
if "allow_spends_update" not in st.session_state:
|
|
st.session_state["allow_spends_update"] = True
|
|
|
|
if "allow_sales_update" not in st.session_state:
|
|
st.session_state["allow_sales_update"] = True
|
|
|
|
|
|
def update_sales_abs_slider():
|
|
actual_sales = _scenario.actual_total_sales
|
|
if validate_input(st.session_state["total_sales_change_abs_slider"]):
|
|
modified_sales = extract_number_for_string(
|
|
st.session_state["total_sales_change_abs_slider"]
|
|
)
|
|
st.session_state["total_sales_change"] = round(
|
|
((modified_sales / actual_sales) - 1) * 100
|
|
)
|
|
st.session_state["total_sales_change_abs"] = numerize(modified_sales, 1)
|
|
|
|
|
|
def update_sales_abs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
actual_sales = _scenario.actual_total_sales
|
|
|
|
|
|
|
|
|
|
modified_sales = extract_number_for_string(
|
|
st.session_state["total_sales_change_abs"]
|
|
)
|
|
st.session_state["total_sales_change"] = round(
|
|
((modified_sales / actual_sales) - 1) * 100
|
|
)
|
|
st.session_state["total_sales_change_abs_slider"] = numerize(modified_sales, 1)
|
|
|
|
|
|
def update_sales():
|
|
st.session_state["total_sales_change_abs"] = numerize(
|
|
(1 + st.session_state["total_sales_change"] / 100)
|
|
* _scenario.actual_total_sales,
|
|
1,
|
|
)
|
|
st.session_state["total_sales_change_abs_slider"] = numerize(
|
|
(1 + st.session_state["total_sales_change"] / 100)
|
|
* _scenario.actual_total_sales,
|
|
1,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update_all_spends_abs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
actual_spends = _scenario.actual_total_spends
|
|
if (
|
|
"total_spends_change_abs" in st.session_state and validate_input(st.session_state["total_spends_change_abs"])
|
|
and st.session_state["allow_spends_update"]
|
|
):
|
|
modified_spends = extract_number_for_string(
|
|
st.session_state["total_spends_change_abs"]
|
|
)
|
|
total_spends_change_val = (
|
|
(modified_spends / actual_spends) - 1
|
|
) * 100
|
|
|
|
if "overall_lower_bound" in st.session_state:
|
|
try:
|
|
lower_val = float(st.session_state["overall_lower_bound"])
|
|
except (ValueError, TypeError):
|
|
lower_val = 30.0
|
|
|
|
if "overall_upper_bound" in st.session_state:
|
|
try:
|
|
upper_val = float(st.session_state["overall_upper_bound"])
|
|
except (ValueError, TypeError):
|
|
upper_val = 30.0
|
|
|
|
if total_spends_change_val > upper_val or total_spends_change_val < -lower_val:
|
|
del st.session_state.total_spends_change_abs
|
|
return
|
|
|
|
st.session_state["total_spends_change"] = total_spends_change_val
|
|
st.session_state["total_spends_change_abs_slider"] = st.session_state[
|
|
"total_spends_change_abs"
|
|
]
|
|
|
|
update_all_spends()
|
|
|
|
|
|
def update_spends():
|
|
st.session_state["total_spends_change_abs"] = numerize(
|
|
(1 + st.session_state["total_spends_change"] / 100)
|
|
* _scenario.actual_total_spends,
|
|
1,
|
|
)
|
|
st.session_state["total_spends_change_abs_slider"] = numerize(
|
|
(1 + st.session_state["total_spends_change"] / 100)
|
|
* _scenario.actual_total_spends,
|
|
1,
|
|
)
|
|
|
|
update_all_spends()
|
|
|
|
|
|
def update_all_spends():
|
|
"""
|
|
Updates spends for all the channels with the given overall spends change
|
|
"""
|
|
percent_change = st.session_state["total_spends_change"]
|
|
|
|
for channel_name in st.session_state["channels_list"]:
|
|
channel = st.session_state["scenario"].channels[channel_name]
|
|
current_spends = channel.actual_total_spends
|
|
modified_spends = (1 + percent_change / 100) * current_spends
|
|
st.session_state["scenario"].update(channel_name, modified_spends)
|
|
st.session_state[channel_name] = numerize(
|
|
modified_spends * channel.conversion_rate, 1
|
|
)
|
|
st.session_state[f"{channel_name}_change"] = percent_change
|
|
|
|
|
|
def extract_number_for_string(string_input):
|
|
string_input = string_input.upper()
|
|
if string_input.endswith("K"):
|
|
return float(string_input[:-1]) * 10**3
|
|
elif string_input.endswith("M"):
|
|
return float(string_input[:-1]) * 10**6
|
|
elif string_input.endswith("B"):
|
|
return float(string_input[:-1]) * 10**9
|
|
|
|
|
|
def validate_input(string_input):
|
|
pattern = r"\d+\.?\d*[K|M|B]$"
|
|
match = re.match(pattern, string_input)
|
|
if match is None:
|
|
return False
|
|
return True
|
|
|
|
|
|
def validate_input_lb(string_input):
|
|
pattern = r"\d+\.?\d*[K|M|B]$"
|
|
match = re.match(pattern, string_input)
|
|
if match is None:
|
|
return False
|
|
response_curve_params = pd.read_excel(
|
|
"response_curves_parameters.xlsx", index_col="channel"
|
|
)
|
|
param_dicts = {
|
|
col: response_curve_params[col].to_dict()
|
|
for col in response_curve_params.columns
|
|
}
|
|
if float(string_input) < round(
|
|
param_dicts["x_min"][channel_name]
|
|
* 10400
|
|
/ param_dicts["current_spends"][channel_name]
|
|
):
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def update_data_bound_min(channel_name):
|
|
"""
|
|
Updates the bounds for the given channel
|
|
"""
|
|
if validate_input_lb(st.session_state[f"{channel_name}_lower_bound"]):
|
|
modified_bounds = st.session_state[f"{channel_name}_lower_bound"]
|
|
|
|
|
|
st.session_state["scenario"].update_bounds_min(channel_name, modified_bounds)
|
|
st.write(st.session_state["scenario"].channels[channel_name].channel_bounds_min)
|
|
|
|
|
|
|
|
def update_data_bound_max(channel_name):
|
|
"""
|
|
Updates the bounds for the given channel
|
|
"""
|
|
modified_bounds = st.session_state[f"{channel_name}_upper_bound"]
|
|
|
|
|
|
st.session_state["scenario"].update_bounds_max(channel_name, modified_bounds)
|
|
|
|
|
|
|
|
|
|
def update_data_by_percent(channel_name):
|
|
prev_spends = (
|
|
st.session_state["scenario"].channels[channel_name].actual_total_spends
|
|
* st.session_state["scenario"].channels[channel_name].conversion_rate
|
|
)
|
|
modified_spends = prev_spends * (
|
|
1 + st.session_state[f"{channel_name}_change"] / 100
|
|
)
|
|
st.session_state[channel_name] = numerize(modified_spends, 1)
|
|
st.session_state["scenario"].update(
|
|
channel_name,
|
|
modified_spends
|
|
/ st.session_state["scenario"].channels[channel_name].conversion_rate,
|
|
)
|
|
|
|
|
|
def update_data(channel_name):
|
|
"""
|
|
Updates the spends for the given channel
|
|
"""
|
|
|
|
if validate_input(st.session_state[channel_name]):
|
|
modified_spends = extract_number_for_string(st.session_state[channel_name])
|
|
prev_spends = (
|
|
st.session_state["scenario"].channels[channel_name].actual_total_spends
|
|
* st.session_state["scenario"].channels[channel_name].conversion_rate
|
|
)
|
|
|
|
|
|
channel_spends_change = round(
|
|
100 * (modified_spends - prev_spends) / prev_spends, 2
|
|
)
|
|
|
|
if "overall_lower_bound" in st.session_state:
|
|
try:
|
|
lower_val = float(st.session_state["overall_lower_bound"])
|
|
except (ValueError, TypeError):
|
|
lower_val = 30.0
|
|
|
|
if "overall_upper_bound" in st.session_state:
|
|
try:
|
|
upper_val = float(st.session_state["overall_upper_bound"])
|
|
except (ValueError, TypeError):
|
|
upper_val = 30.0
|
|
|
|
if channel_spends_change > upper_val or channel_spends_change < -lower_val:
|
|
del st.session_state[channel_name]
|
|
return
|
|
|
|
st.session_state[f"{channel_name}_change"] = channel_spends_change
|
|
st.session_state["scenario"].update(
|
|
channel_name,
|
|
modified_spends
|
|
/ st.session_state["scenario"].channels[channel_name].conversion_rate,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def select_channel_for_optimization(channel_name):
|
|
"""
|
|
Marks the given channel for optimization
|
|
"""
|
|
st.session_state["optimization_channels"][channel_name] = st.session_state[
|
|
f"{channel_name}_selected"
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def select_all_channels_for_optimization():
|
|
"""
|
|
Marks all the channel for optimization
|
|
"""
|
|
for channel_name in st.session_state["optimization_channels"].keys():
|
|
st.session_state[f"{channel_name}_selected"] = st.session_state[
|
|
"optimze_all_channels"
|
|
]
|
|
st.session_state["optimization_channels"][channel_name] = st.session_state[
|
|
"optimze_all_channels"
|
|
]
|
|
|
|
|
|
def update_penalty():
|
|
"""
|
|
Updates the penalty flag for sales calculation
|
|
"""
|
|
st.session_state["scenario"].update_penalty(st.session_state["apply_penalty"])
|
|
|
|
|
|
def reset_scenario(panel_selected, file_selected, updated_rcs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if panel_selected == "Total Market":
|
|
initialize_data(
|
|
target_file=file_selected,
|
|
panel=panel_selected,
|
|
updated_rcs=updated_rcs,
|
|
metrics=metrics_selected,
|
|
)
|
|
panel = None
|
|
else:
|
|
initialize_data(
|
|
target_file=file_selected,
|
|
panel=panel_selected,
|
|
updated_rcs=updated_rcs,
|
|
metrics=metrics_selected,
|
|
)
|
|
|
|
for channel_name in st.session_state["channels_list"]:
|
|
st.session_state[f"{channel_name}_selected"] = False
|
|
st.session_state[f"{channel_name}_change"] = 0
|
|
st.session_state["optimze_all_channels"] = False
|
|
|
|
st.session_state["total_sales_change"] = 0
|
|
|
|
if "overall_lower_bound" in st.session_state and "overall_upper_bound" in st.session_state:
|
|
del st.session_state["overall_lower_bound"]
|
|
del st.session_state["overall_upper_bound"]
|
|
|
|
st.session_state["overall_lower_bound"] = 30.0
|
|
st.session_state["overall_upper_bound"] = 30.0
|
|
|
|
update_spends()
|
|
update_sales()
|
|
|
|
reset_inputs()
|
|
|
|
|
|
|
|
|
|
def format_number(num):
|
|
if num >= 1_000_000:
|
|
return f"{num / 1_000_000:.2f}M"
|
|
elif num >= 1_000:
|
|
return f"{num / 1_000:.0f}K"
|
|
else:
|
|
return f"{num:.2f}"
|
|
|
|
|
|
def summary_plot(data, x, y, title, text_column):
|
|
fig = px.bar(
|
|
data,
|
|
x=x,
|
|
y=y,
|
|
orientation="h",
|
|
title=title,
|
|
text=text_column,
|
|
color="Channel_name",
|
|
)
|
|
|
|
|
|
data[text_column] = pd.to_numeric(data[text_column], errors="coerce")
|
|
|
|
|
|
fig.update_traces(
|
|
texttemplate="%{text:.2s}",
|
|
textposition="outside",
|
|
hovertemplate="%{x:.2s}",
|
|
)
|
|
|
|
fig.update_layout(xaxis_title=x, yaxis_title="Channel Name", showlegend=False)
|
|
return fig
|
|
|
|
|
|
def s_curve(x, K, b, a, x0):
|
|
return K / (1 + b * np.exp(-a * (x - x0)))
|
|
|
|
|
|
def find_segment_value(x, roi, mroi):
|
|
start_value = x[0]
|
|
end_value = x[len(x) - 1]
|
|
|
|
|
|
green_condition = (roi > 1) & (mroi > 1)
|
|
left_indices = np.where(green_condition)[0]
|
|
left_value = x[left_indices[0]] if left_indices.size > 0 else x[0]
|
|
|
|
right_indices = np.where(green_condition)[0]
|
|
right_value = x[right_indices[-1]] if right_indices.size > 0 else x[0]
|
|
|
|
return start_value, end_value, left_value, right_value
|
|
|
|
|
|
def calculate_rgba(
|
|
start_value, end_value, left_value, right_value, current_channel_spends
|
|
):
|
|
|
|
alpha = None
|
|
|
|
|
|
if start_value <= current_channel_spends <= left_value:
|
|
color = "yellow"
|
|
relative_position = (current_channel_spends - start_value) / (
|
|
left_value - start_value
|
|
)
|
|
alpha = 0.8 - (0.6 * relative_position)
|
|
|
|
elif left_value < current_channel_spends <= right_value:
|
|
color = "green"
|
|
relative_position = (current_channel_spends - left_value) / (
|
|
right_value - left_value
|
|
)
|
|
alpha = 0.8 - (0.6 * relative_position)
|
|
|
|
elif right_value < current_channel_spends <= end_value:
|
|
color = "red"
|
|
relative_position = (current_channel_spends - right_value) / (
|
|
end_value - right_value
|
|
)
|
|
alpha = 0.2 + (0.6 * relative_position)
|
|
|
|
else:
|
|
|
|
return "rgba(136, 136, 136, 0.5)"
|
|
|
|
|
|
alpha = max(0.2, min(alpha, 0.8))
|
|
|
|
|
|
color_codes = {
|
|
"yellow": "255, 255, 0",
|
|
"green": "0, 128, 0",
|
|
"red": "255, 0, 0",
|
|
}
|
|
|
|
rgba = f"rgba({color_codes[color]}, {alpha})"
|
|
return rgba
|
|
|
|
|
|
def debug_temp(x_test, power, K, b, a, x0):
|
|
|
|
|
|
count_lower_bin = sum(1 for x in x_test if x <= 2524)
|
|
count_center_bin = sum(1 for x in x_test if x > 2524 and x <= 3377)
|
|
count_ = sum(1 for x in x_test if x > 3377)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plot_response_curves(summary_df_sorted):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channel_cols = [
|
|
"BroadcastTV",
|
|
"CableTV",
|
|
"Connected&OTTTV",
|
|
"DisplayProspecting",
|
|
"DisplayRetargeting",
|
|
"Video",
|
|
"SocialProspecting",
|
|
"SocialRetargeting",
|
|
"SearchBrand",
|
|
"SearchNon-brand",
|
|
"DigitalPartners",
|
|
"Audio",
|
|
"Email",
|
|
]
|
|
summary_df_sorted.index = summary_df_sorted["Channel_name"]
|
|
figures = [
|
|
rc.response_curves(
|
|
channels_list[i],
|
|
summary_df_sorted["Optimized_spend"][channels_list[i]],
|
|
summary_df_sorted["New_sales"][channels_list[i]],
|
|
)
|
|
for i in range(13) if channels_list[i] != "Email"
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
col_num = 3
|
|
cols = st.columns(col_num)
|
|
|
|
for idx, fig in enumerate(figures):
|
|
col = cols[idx % col_num]
|
|
with col:
|
|
|
|
current_title = fig.layout.title.text if fig.layout.title else ""
|
|
updated_title = current_title.replace("Response Curve", "").strip().replace("Connected & OTTTV", "Connected & OTT TV")
|
|
|
|
fig.update_layout(height=350, width=350, yaxis_title="Revenue", title=updated_title)
|
|
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def generate_spending_header(heading):
|
|
return st.markdown(
|
|
f"""<h4 class="spends-header"><bold>{heading}<bold></h4>""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with open("config.yaml") as file:
|
|
config = yaml.load(file, Loader=SafeLoader)
|
|
st.session_state["config"] = config
|
|
|
|
authenticator = stauth.Authenticate(
|
|
config["credentials"],
|
|
config["cookie"]["name"],
|
|
config["cookie"]["key"],
|
|
config["cookie"]["expiry_days"],
|
|
config["preauthorized"],
|
|
)
|
|
st.session_state["authenticator"] = authenticator
|
|
name, authentication_status, username = authenticator.login("Login", "main")
|
|
auth_status = st.session_state.get("authentication_status")
|
|
|
|
import os
|
|
import glob
|
|
|
|
|
|
def upload_file_prospects_calc(df):
|
|
df["Prospects"] = 0
|
|
|
|
params = pd.read_excel(r"response_curves_parameters.xlsx", index_col="channel")
|
|
param_dicts = {col: params[col].to_dict() for col in params.columns}
|
|
df.index = df.channel
|
|
|
|
for col in df.channel:
|
|
x = df["Spends"][col]
|
|
dividing_rate = 104
|
|
|
|
x_inp = (x / dividing_rate - param_dicts["x_min"][col]) / (
|
|
param_dicts["x_max"][col] - param_dicts["x_min"][col]
|
|
)
|
|
x_out = x_inp ** param_dicts["n"][col] / (
|
|
param_dicts["Kd"][col] ** param_dicts["n"][col]
|
|
+ x_inp ** param_dicts["n"][col]
|
|
)
|
|
|
|
|
|
x_val_inv = (
|
|
x_out * param_dicts["x_max"][col] + (1 - x_out) * param_dicts["x_min"][col]
|
|
)
|
|
sales = (
|
|
x_val_inv * param_dicts["y_min"][col] / param_dicts["y_max"][col]
|
|
) * dividing_rate
|
|
|
|
|
|
|
|
|
|
|
|
df["Prospects"][col] = sales
|
|
|
|
return df
|
|
|
|
|
|
def upload_file_format(df):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
df1 = df.transpose()
|
|
df1.reset_index(inplace=True)
|
|
df1.columns = df1.iloc[0]
|
|
df1 = df1[1:]
|
|
df1["channel"] = pd.to_datetime("1999-08-06").date()
|
|
df1.rename(columns={"channel": "Date"}, inplace=True)
|
|
|
|
df2 = df1.rename(columns={"Date": "Week"})
|
|
|
|
df3 = upload_file_prospects_calc(df)
|
|
df3 = df3[["Prospects"]].transpose().reset_index()
|
|
df3["index"] = pd.to_datetime("1999-08-06").date()
|
|
df3.rename(columns={"index": "Date"}, inplace=True)
|
|
df3.insert(1, "const", [0])
|
|
|
|
|
|
|
|
|
|
import io
|
|
|
|
output = io.BytesIO()
|
|
|
|
|
|
with pd.ExcelWriter(output, engine="openpyxl") as writer:
|
|
df1.to_excel(writer, index=False, sheet_name="RAW DATA MMM")
|
|
df2.to_excel(writer, index=False, sheet_name="SPEND INPUT")
|
|
df3.to_excel(writer, index=False, sheet_name="CONTRIBUTION MMM")
|
|
|
|
|
|
output.seek(0)
|
|
|
|
with open("Overview_data_uploaded.xlsx", "wb") as f:
|
|
f.write(output.getvalue())
|
|
|
|
return
|
|
|
|
|
|
def get_excel_names(directory):
|
|
|
|
last_portions = []
|
|
|
|
|
|
patterns = [
|
|
os.path.join(directory, "*@#*.xlsx"),
|
|
os.path.join(directory, "*@#*.xls"),
|
|
]
|
|
|
|
|
|
for pattern in patterns:
|
|
files = glob.glob(pattern)
|
|
|
|
|
|
for file in files:
|
|
base_name = os.path.basename(file)
|
|
last_portion = base_name.split("@#")[-1]
|
|
last_portion = last_portion.replace(".xlsx", "").replace(
|
|
".xls", ""
|
|
)
|
|
last_portions.append(last_portion)
|
|
|
|
return last_portions
|
|
|
|
|
|
def name_formating(channel_name):
|
|
|
|
name_mod = channel_name.replace("_", " ")
|
|
|
|
|
|
name_mod = name_mod.title()
|
|
|
|
return name_mod
|
|
|
|
|
|
@st.cache_data(show_spinner=False)
|
|
def panel_fetch(file_selected):
|
|
raw_data_mmm_df = pd.read_excel(file_selected, sheet_name="RAW DATA MMM")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
panel = None
|
|
return panel
|
|
|
|
|
|
def reset_inputs():
|
|
if "total_spends_change_abs" in st.session_state:
|
|
del st.session_state.total_spends_change_abs
|
|
if "total_spends_change" in st.session_state:
|
|
del st.session_state.total_spends_change
|
|
if "total_spends_change_abs_slider" in st.session_state:
|
|
del st.session_state.total_spends_change_abs_slider
|
|
|
|
if "total_sales_change_abs" in st.session_state:
|
|
del st.session_state.total_sales_change_abs
|
|
if "total_sales_change" in st.session_state:
|
|
del st.session_state.total_sales_change
|
|
if "total_sales_change_abs_slider" in st.session_state:
|
|
del st.session_state.total_sales_change_abs_slider
|
|
|
|
st.session_state["initialized"] = False
|
|
|
|
|
|
def scenario_planner_plots2():
|
|
import plotly.graph_objects as go
|
|
from plotly.subplots import make_subplots
|
|
|
|
with open("summary_df.pkl", "rb") as file:
|
|
summary_df_sorted = pickle.load(file)
|
|
|
|
|
|
|
|
summary_df_sorted = summary_df_sorted.sort_values(
|
|
by=["Optimized_spend"], ascending=False
|
|
)
|
|
summary_df_sorted["old_efficiency"] = (
|
|
summary_df_sorted["Old_sales"] / summary_df_sorted["Old_sales"].sum()
|
|
) / (summary_df_sorted["Actual_spend"] / summary_df_sorted["Actual_spend"].sum())
|
|
summary_df_sorted["new_efficiency"] = (
|
|
summary_df_sorted["New_sales"] / summary_df_sorted["New_sales"].sum()
|
|
) / (
|
|
summary_df_sorted["Optimized_spend"]
|
|
/ summary_df_sorted["Optimized_spend"].sum()
|
|
)
|
|
summary_df_sorted["old_roi"] = (
|
|
summary_df_sorted["Old_sales"] / summary_df_sorted["Actual_spend"]
|
|
)
|
|
summary_df_sorted["new_roi"] = (
|
|
summary_df_sorted["New_sales"] / summary_df_sorted["Optimized_spend"]
|
|
)
|
|
|
|
total_actual_spend = summary_df_sorted["Actual_spend"].sum()
|
|
total_optimized_spend = summary_df_sorted["Optimized_spend"].sum()
|
|
actual_spend_percentage = (
|
|
summary_df_sorted["Actual_spend"] / total_actual_spend
|
|
) * 100
|
|
optimized_spend_percentage = (
|
|
summary_df_sorted["Optimized_spend"] / total_optimized_spend
|
|
) * 100
|
|
|
|
light_blue = "rgba(0, 31, 120, 0.7)"
|
|
light_orange = "rgba(0, 181, 219, 0.7)"
|
|
light_green = "rgba(240, 61, 20, 0.7)"
|
|
light_red = "rgba(250, 110, 10, 0.7)"
|
|
light_purple = "rgba(255, 191, 69, 0.7)"
|
|
|
|
fig1 = go.Figure()
|
|
|
|
|
|
fig1.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["Actual_spend"],
|
|
name="Actual",
|
|
text=summary_df_sorted["Actual_spend"].apply(format_number) + " "
|
|
|
|
|
|
|
|
,
|
|
textposition="outside",
|
|
marker_color=light_blue,
|
|
)
|
|
)
|
|
|
|
fig1.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["Optimized_spend"],
|
|
name="Optimized",
|
|
text=summary_df_sorted["Optimized_spend"].apply(format_number) + " "
|
|
|
|
|
|
,
|
|
textposition="outside",
|
|
marker_color=light_orange,
|
|
)
|
|
)
|
|
|
|
fig1.update_xaxes(title_text="Channels")
|
|
fig1.update_yaxes(title_text="Spends ($)")
|
|
fig1.update_layout(
|
|
title="Actual vs. Optimized Spends", margin=dict(t=40, b=40, l=40, r=40)
|
|
)
|
|
|
|
|
|
|
|
|
|
fig2 = go.Figure()
|
|
fig2.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["Old_sales"],
|
|
name="Actual Contribution",
|
|
text=summary_df_sorted["Old_sales"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_blue,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig2.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["New_sales"],
|
|
name="Optimized Contribution",
|
|
text=summary_df_sorted["New_sales"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_orange,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig2.update_yaxes(title_text="Contribution")
|
|
fig2.update_xaxes(title_text="Channels")
|
|
fig2.update_layout(
|
|
title="Actual vs. Optimized Contributions",
|
|
margin=dict(t=40, b=40, l=40, r=40),
|
|
|
|
)
|
|
|
|
|
|
|
|
fig3 = go.Figure()
|
|
summary_df_sorted_p = summary_df_sorted[
|
|
summary_df_sorted["Channel_name"] != "Panel"
|
|
]
|
|
fig3.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted_p["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted_p["old_efficiency"],
|
|
name="Actual Efficiency",
|
|
text=summary_df_sorted_p["old_efficiency"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_blue,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
fig3.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted_p["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted_p["new_efficiency"],
|
|
name="Optimized Efficiency",
|
|
text=summary_df_sorted_p["new_efficiency"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_orange,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig3.update_xaxes(title_text="Channels")
|
|
fig3.update_yaxes(title_text="Efficiency")
|
|
fig3.update_layout(
|
|
title="Actual vs. Optimized Efficiency",
|
|
margin=dict(t=40, b=40, l=40, r=40),
|
|
|
|
)
|
|
|
|
|
|
return fig1, fig2, fig3
|
|
|
|
|
|
def scenario_planner_plots():
|
|
with st.expander("Actual vs. Optimized Comparison"):
|
|
|
|
|
|
|
|
import plotly.graph_objects as go
|
|
from plotly.subplots import make_subplots
|
|
|
|
|
|
import plotly.graph_objects as go
|
|
from plotly.subplots import make_subplots
|
|
|
|
st.empty()
|
|
|
|
spends_data = pd.read_excel("Overview_data_test.xlsx")
|
|
|
|
with open("summary_df.pkl", "rb") as file:
|
|
summary_df_sorted = pickle.load(file).copy()
|
|
|
|
|
|
|
|
summary_df_sorted = summary_df_sorted.sort_values(
|
|
by=["Optimized_spend"], ascending=False
|
|
)
|
|
summary_df_sorted["old_efficiency"] = (
|
|
summary_df_sorted["Old_sales"] / summary_df_sorted["Old_sales"].sum()
|
|
) / (
|
|
summary_df_sorted["Actual_spend"] / summary_df_sorted["Actual_spend"].sum()
|
|
)
|
|
summary_df_sorted["new_efficiency"] = (
|
|
summary_df_sorted["New_sales"] / summary_df_sorted["New_sales"].sum()
|
|
) / (
|
|
summary_df_sorted["Optimized_spend"]
|
|
/ summary_df_sorted["Optimized_spend"].sum()
|
|
)
|
|
|
|
summary_df_sorted["old_roi"] = (
|
|
summary_df_sorted["Old_sales"] / summary_df_sorted["Actual_spend"]
|
|
)
|
|
summary_df_sorted["new_roi"] = (
|
|
summary_df_sorted["New_sales"] / summary_df_sorted["Optimized_spend"]
|
|
)
|
|
|
|
total_actual_spend = summary_df_sorted["Actual_spend"].sum()
|
|
total_optimized_spend = summary_df_sorted["Optimized_spend"].sum()
|
|
|
|
actual_spend_percentage = (
|
|
summary_df_sorted["Actual_spend"] / total_actual_spend
|
|
) * 100
|
|
optimized_spend_percentage = (
|
|
summary_df_sorted["Optimized_spend"] / total_optimized_spend
|
|
) * 100
|
|
|
|
light_blue = "rgba(0, 31, 120, 0.7)"
|
|
light_orange = "rgba(0, 181, 219, 0.7)"
|
|
light_green = "rgba(240, 61, 20, 0.7)"
|
|
light_red = "rgba(250, 110, 10, 0.7)"
|
|
light_purple = "rgba(255, 191, 69, 0.7)"
|
|
|
|
fig1 = go.Figure()
|
|
|
|
|
|
fig1.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["Actual_spend"],
|
|
name="Actual",
|
|
text=summary_df_sorted["Actual_spend"].apply(format_number) + " "
|
|
|
|
|
|
|
|
,
|
|
textposition="outside",
|
|
marker_color=light_blue,
|
|
)
|
|
)
|
|
|
|
fig1.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["Optimized_spend"],
|
|
name="Optimized",
|
|
text=summary_df_sorted["Optimized_spend"].apply(format_number) + " "
|
|
|
|
|
|
,
|
|
textposition="outside",
|
|
marker_color=light_orange,
|
|
)
|
|
)
|
|
|
|
fig1.update_xaxes(title_text="Channels")
|
|
fig1.update_yaxes(title_text="Spends ($)")
|
|
fig1.update_layout(
|
|
title="Spends", margin=dict(t=40, b=40, l=40, r=40)
|
|
)
|
|
|
|
st.plotly_chart(fig1, use_container_width=True)
|
|
|
|
|
|
fig2 = go.Figure()
|
|
fig2.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["Old_sales"],
|
|
name="Actual Contribution",
|
|
text=summary_df_sorted["Old_sales"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_blue,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig2.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted["New_sales"],
|
|
name="Optimized Contribution",
|
|
text=summary_df_sorted["New_sales"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_orange,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig2.update_yaxes(title_text="Revenue ($)")
|
|
fig2.update_xaxes(title_text="Channels")
|
|
fig2.update_layout(
|
|
title="Revenue",
|
|
margin=dict(t=40, b=40, l=40, r=40),
|
|
|
|
)
|
|
st.plotly_chart(fig2, use_container_width=True)
|
|
|
|
|
|
fig3 = go.Figure()
|
|
summary_df_sorted_p = summary_df_sorted[
|
|
summary_df_sorted["Channel_name"] != "Panel"
|
|
]
|
|
|
|
|
|
summary_df_sorted_p["Actual_spend"].replace(0, np.nan, inplace=True)
|
|
summary_df_sorted_p["Optimized_spend"].replace(0, np.nan, inplace=True)
|
|
|
|
summary_df_sorted_p["old_roi"] = summary_df_sorted_p["Old_sales"] / summary_df_sorted_p["Actual_spend"]
|
|
summary_df_sorted_p["new_roi"] = summary_df_sorted_p["New_sales"] / summary_df_sorted_p["Optimized_spend"]
|
|
|
|
|
|
fig3.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted_p["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted_p["old_roi"],
|
|
name="Actual ROI",
|
|
text=summary_df_sorted_p["old_roi"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_blue,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig3.add_trace(
|
|
go.Bar(
|
|
x=summary_df_sorted_p["Channel_name"].apply(channel_name_formating),
|
|
y=summary_df_sorted_p["new_roi"],
|
|
name="Optimized ROI",
|
|
text=summary_df_sorted_p["new_roi"].apply(format_number),
|
|
textposition="outside",
|
|
marker_color=light_orange,
|
|
showlegend=True,
|
|
)
|
|
)
|
|
|
|
fig3.update_xaxes(title_text="Channels")
|
|
fig3.update_yaxes(title_text="ROI")
|
|
fig3.update_layout(
|
|
title="ROI",
|
|
margin=dict(t=40, b=40, l=40, r=40),
|
|
|
|
)
|
|
|
|
st.plotly_chart(fig3, use_container_width=True)
|
|
return fig1, fig2, fig3
|
|
|
|
|
|
def give_demo():
|
|
def get_file_bytes(file_path):
|
|
with open(file_path, "rb") as file:
|
|
return file.read()
|
|
|
|
|
|
file_path = "input_data_example.xlsx"
|
|
|
|
|
|
st.download_button(
|
|
label="Download Input File Format",
|
|
data=get_file_bytes(file_path),
|
|
file_name=file_path,
|
|
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
)
|
|
|
|
|
|
if auth_status == True:
|
|
authenticator.logout("Logout", "main")
|
|
st.header("Scenario Planner")
|
|
|
|
data_selected = st.selectbox(
|
|
"Select base data for optimisation",
|
|
options=["Optimise Actual Spends", "Optimise Uploaded Spends"],
|
|
key="data_upload_key",
|
|
index=1,
|
|
)
|
|
|
|
|
|
|
|
directory = "metrics_level_data"
|
|
metrics_list = get_excel_names(directory)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
metrics_selected = "prospects"
|
|
|
|
target = name_formating(metrics_selected)
|
|
|
|
|
|
|
|
|
|
file_selected = None
|
|
if data_selected == "Optimise Uploaded Spends":
|
|
give_demo()
|
|
st.write("Select a file to upload")
|
|
|
|
uploaded_file = st.file_uploader("Choose an Excel file", type=["xlsx", "xls"])
|
|
|
|
if uploaded_file:
|
|
try:
|
|
|
|
df = pd.read_excel(uploaded_file, engine="openpyxl")
|
|
upload_file_format(df)
|
|
file_selected = "Overview_data_uploaded.xlsx"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
st.error(f"Error reading the file: {e}")
|
|
|
|
elif data_selected == "Optimise Actual Spends":
|
|
file_selected = f"Overview_data_test_panel@#{metrics_selected}.xlsx"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else:
|
|
st.write("")
|
|
|
|
if file_selected:
|
|
st.session_state["file_selected"] = file_selected
|
|
|
|
panel_list = panel_fetch(file_selected)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
panel_selected = "Total Market"
|
|
st.session_state["selected_markets"] = panel_selected
|
|
|
|
if "update_rcs" in st.session_state:
|
|
updated_rcs = st.session_state["update_rcs"]
|
|
else:
|
|
updated_rcs = None
|
|
|
|
if "first_time" not in st.session_state:
|
|
st.session_state["first_time"] = True
|
|
|
|
|
|
is_state_initiaized = st.session_state.get("initialized", False)
|
|
if not is_state_initiaized or st.session_state["first_time"]:
|
|
initialize_data(
|
|
target_file=file_selected,
|
|
panel=panel_selected,
|
|
updated_rcs=updated_rcs,
|
|
metrics=metrics_selected,
|
|
)
|
|
st.session_state["initialized"] = True
|
|
st.session_state["first_time"] = False
|
|
save_scenario("current scenario")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channels_list = st.session_state["channels_list"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
main_header = st.columns((2, 2))
|
|
sub_header = st.columns((1, 1, 1, 1))
|
|
_scenario = st.session_state["scenario"]
|
|
|
|
if "total_spends_change" not in st.session_state:
|
|
st.session_state.total_spends_change = 0
|
|
|
|
if "total_sales_change" not in st.session_state:
|
|
st.session_state.total_sales_change = 0
|
|
|
|
if "total_spends_change_abs" not in st.session_state:
|
|
st.session_state["total_spends_change_abs"] = numerize(
|
|
_scenario.actual_total_spends, 1
|
|
)
|
|
|
|
|
|
if "total_sales_change_abs" not in st.session_state:
|
|
st.session_state["total_sales_change_abs"] = numerize(
|
|
_scenario.actual_total_sales, 1
|
|
)
|
|
|
|
if "total_spends_change_abs_slider" not in st.session_state:
|
|
st.session_state.total_spends_change_abs_slider = numerize(
|
|
_scenario.actual_total_spends, 1
|
|
)
|
|
|
|
if "total_sales_change_abs_slider" not in st.session_state:
|
|
st.session_state.total_sales_change_abs_slider = numerize(
|
|
_scenario.actual_total_sales, 1
|
|
)
|
|
|
|
if "lower_bound_key" not in st.session_state:
|
|
st.session_state["lower_bound_key"] = 10
|
|
|
|
if "upper_bound_key" not in st.session_state:
|
|
st.session_state["upper_bound_key"] = 100
|
|
|
|
|
|
header_df = pd.DataFrame(
|
|
index=["Actual", "Simulated", "Change", "Percent Change"],
|
|
columns=["Spends", "Prospects"],
|
|
)
|
|
header_df["Spends"]["Actual"] = format_numbers(_scenario.actual_total_spends)
|
|
header_df["Spends"]["Simulated"] = format_numbers(
|
|
_scenario.modified_total_spends
|
|
)
|
|
header_df["Spends"]["Change"] = format_numbers(
|
|
_scenario.delta_spends
|
|
)
|
|
header_df["Spends"]["Percent Change"] = (
|
|
numerize(100 * (_scenario.delta_spends / _scenario.actual_total_spends))
|
|
+ "%"
|
|
)
|
|
|
|
header_df["Prospects"]["Actual"] = format_numbers_f(
|
|
float(_scenario.actual_total_sales)
|
|
)
|
|
header_df["Prospects"]["Simulated"] = format_numbers_f(
|
|
float(_scenario.modified_total_sales)
|
|
)
|
|
header_df["Prospects"]["Change"] = format_numbers_f(_scenario.delta_sales)
|
|
header_df["Prospects"]["Percent Change"] = (
|
|
numerize(100 * (_scenario.delta_sales / _scenario.actual_total_sales), 1)
|
|
+ "%"
|
|
)
|
|
|
|
st.markdown("""<hr class="spends-heading-seperator">""", unsafe_allow_html=True)
|
|
_columns = st.columns((1, 1, 1, 1, 1))
|
|
st.markdown(
|
|
"""
|
|
<style>
|
|
.custom-text_head {
|
|
font-size: 24px; /* Adjust font size */
|
|
color: 'blue' ; /* Adjust text color */
|
|
|
|
font-weight: bold;
|
|
}
|
|
</style>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
with _columns[0]:
|
|
st.markdown(
|
|
f'<p class="custom-text_head">{"Metrics"}</p>', unsafe_allow_html=True
|
|
)
|
|
|
|
with _columns[1]:
|
|
st.markdown(
|
|
f'<p class="custom-text_head">{"Actual"}</p>', unsafe_allow_html=True
|
|
)
|
|
|
|
with _columns[2]:
|
|
st.markdown(
|
|
f'<p class="custom-text_head">{"Simulated"}</p>', unsafe_allow_html=True
|
|
)
|
|
|
|
with _columns[3]:
|
|
st.markdown(
|
|
f'<p class="custom-text_head">{"Change"}</p>', unsafe_allow_html=True
|
|
)
|
|
|
|
with _columns[4]:
|
|
st.markdown(
|
|
f'<p class="custom-text_head">{"Change Percent"}</p>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
st.markdown("""<hr class="spends-heading-seperator">""", unsafe_allow_html=True)
|
|
|
|
_columns = st.columns((1, 1, 1, 1, 1))
|
|
with _columns[0]:
|
|
st.markdown("""<h4>Spends</h4>""", unsafe_allow_html=True)
|
|
|
|
with _columns[1]:
|
|
st.markdown(
|
|
f"""<h4>{header_df["Spends"]["Actual"]}</h4>""", unsafe_allow_html=True
|
|
)
|
|
|
|
with _columns[2]:
|
|
st.markdown(
|
|
f"""<h4>{header_df["Spends"]["Simulated"]}</h4>""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
if _scenario.delta_spends < 0:
|
|
st.markdown(
|
|
"""
|
|
<style>
|
|
.custom-text {
|
|
font-size: 24px; /* Adjust font size */
|
|
color: #6bbf6b ; /* Adjust text color */
|
|
}
|
|
</style>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
else:
|
|
st.markdown(
|
|
"""
|
|
<style>
|
|
.custom-text {
|
|
font-size: 24px; /* Adjust font size */
|
|
color: #ff6868; /* Adjust text color */
|
|
}
|
|
</style>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
with _columns[3]:
|
|
|
|
|
|
st.markdown(
|
|
f'<h4 class="custom-text">{header_df["Spends"]["Change"]}</h4>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
with _columns[4]:
|
|
|
|
|
|
st.markdown(
|
|
f'<h4 class="custom-text">{header_df["Spends"]["Percent Change"]}</h4>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
st.markdown(
|
|
"""<hr class="spends-child-seperator">""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
_columns = st.columns((1, 1, 1, 1, 1))
|
|
with _columns[0]:
|
|
|
|
st.markdown("""<h4>Revenue</h4>""", unsafe_allow_html=True)
|
|
with _columns[1]:
|
|
st.markdown(
|
|
f"""<h4>$ {header_df["Prospects"]["Actual"]}</h4>""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
with _columns[2]:
|
|
st.markdown(
|
|
f"""<h4>$ {header_df["Prospects"]["Simulated"]}</h4>""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
if _scenario.delta_sales >= 0:
|
|
st.markdown(
|
|
"""
|
|
<style>
|
|
.custom-text {
|
|
font-size: 24px; /* Adjust font size */
|
|
color:#6bbf6b ; /* Adjust text color */
|
|
}
|
|
</style>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
else:
|
|
st.markdown(
|
|
"""<style>.custom-text {font-size: 24px; /* Adjust font size */color: #ff6868; /* Adjust text color */}</style>""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
with _columns[3]:
|
|
|
|
st.markdown(
|
|
f'<h4 class="custom-text">$ {header_df["Prospects"]["Change"]}</h4>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
with _columns[4]:
|
|
|
|
|
|
st.markdown(
|
|
f'<h4 class="custom-text">{header_df["Prospects"]["Percent Change"]}</h4>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
st.markdown(
|
|
"""<hr class="spends-child-seperator">""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
_columns = st.columns((1, 1, 1, 1, 1))
|
|
|
|
ef1 = _scenario.actual_total_sales/ _scenario.actual_total_spends
|
|
ef2 = _scenario.modified_total_sales/ _scenario.modified_total_spends
|
|
with _columns[0]:
|
|
st.markdown("""<h4>ROI</h4>""", unsafe_allow_html=True)
|
|
|
|
with _columns[1]:
|
|
st.markdown(f"""<h4>{numerize(ef1, 2)}</h4>""", unsafe_allow_html=True)
|
|
|
|
with _columns[2]:
|
|
st.markdown(f"""<h4>{numerize(ef2, 2)}</h4>""", unsafe_allow_html=True)
|
|
|
|
|
|
if ef2 >= ef1:
|
|
st.markdown(
|
|
"""
|
|
<style>
|
|
.custom-text1 {
|
|
font-size: 24px; /* Adjust font size */
|
|
color:#6bbf6b ; /* Adjust text color */
|
|
}
|
|
</style>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
else:
|
|
st.markdown(
|
|
"""
|
|
<style>
|
|
.custom-text1 {
|
|
font-size: 24px; /* Adjust font size */
|
|
color: #ff6868; /* Adjust text color */
|
|
}
|
|
</style>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
with _columns[3]:
|
|
|
|
|
|
st.markdown(
|
|
f'<h4 class="custom-text1">{numerize(ef2-ef1, 2)}</h4>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
with _columns[4]:
|
|
|
|
|
|
st.markdown(
|
|
f'<h4 class="custom-text1">{round((ef2-ef1)/ef1*100, 2)}%</h4>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
st.markdown("""<hr class="spends-child-seperator">""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with st.expander("Channel Spends Simulator", expanded=True):
|
|
_columns1 = st.columns((1, 1, 1, 1))
|
|
with _columns1[0]:
|
|
optimization_selection = st.selectbox(
|
|
"Optimize", options=["Media Spends"], key="optimization_key"
|
|
)
|
|
|
|
with _columns1[1]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.write("#")
|
|
st.checkbox(
|
|
label="Optimize all Channels",
|
|
key="optimze_all_channels",
|
|
value=False,
|
|
on_change=select_all_channels_for_optimization,
|
|
)
|
|
|
|
with _columns1[2]:
|
|
st.write("####")
|
|
optimize_placeholder = st.empty()
|
|
|
|
with _columns1[3]:
|
|
st.write("####")
|
|
st.button(
|
|
"Reset",
|
|
on_click=reset_scenario,
|
|
args=(panel_selected, file_selected, updated_rcs),
|
|
use_container_width=True,
|
|
)
|
|
|
|
|
|
_columns2 = st.columns((2, 2, 2, 2))
|
|
if st.session_state["optimization_key"] == "Media Spends":
|
|
|
|
|
|
with _columns2[2]:
|
|
overall_lower_bound = st.number_input(
|
|
"Overall Lower Bound for Spends",
|
|
value=30.0,
|
|
min_value=0.0,
|
|
max_value=30.0,
|
|
key="overall_lower_bound",
|
|
|
|
)
|
|
with _columns2[3]:
|
|
overall_upper_bound = st.number_input(
|
|
"Overall Upper Bound for Spends",
|
|
value=30.0,
|
|
min_value=0.0,
|
|
max_value=30.0,
|
|
key="overall_upper_bound",
|
|
|
|
)
|
|
|
|
min_value = round(
|
|
_scenario.actual_total_spends * (1 - overall_lower_bound / 100)
|
|
)
|
|
max_value = round(
|
|
_scenario.actual_total_spends * (1 - overall_upper_bound / 100)
|
|
)
|
|
with _columns2[0]:
|
|
spend_input = st.text_input(
|
|
"Absolute",
|
|
key="total_spends_change_abs",
|
|
|
|
on_change=update_all_spends_abs,
|
|
)
|
|
|
|
|
|
|
|
with _columns2[1]:
|
|
st.number_input(
|
|
"Percent Change",
|
|
key="total_spends_change",
|
|
|
|
|
|
min_value=-overall_lower_bound,
|
|
max_value=overall_upper_bound,
|
|
step=0.01,
|
|
value=0.00,
|
|
on_change=update_spends,
|
|
)
|
|
|
|
st.session_state["total_spends_change_abs_slider_options"] = [
|
|
numerize(value, 1)
|
|
for value in range(min_value, max_value + 1, int(1e4))
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif st.session_state["optimization_key"] == target:
|
|
|
|
with _columns2[0]:
|
|
sales_input = st.text_input(
|
|
"Absolute",
|
|
key="total_sales_change_abs",
|
|
on_change=update_sales_abs,
|
|
)
|
|
|
|
with _columns2[1]:
|
|
st.number_input(
|
|
"Percent aaChange",
|
|
key="total_sales_change",
|
|
min_value=-50.00,
|
|
max_value=50.00,
|
|
step=0.01,
|
|
value=0.00,
|
|
on_change=update_sales,
|
|
)
|
|
with _columns2[2]:
|
|
overall_lower_bound = st.number_input(
|
|
"Overall Lower Bound for Spends", value=50
|
|
)
|
|
with _columns2[3]:
|
|
|
|
overall_upper_bound = st.number_input(
|
|
"Overall Upper Bound for Spends", value=50
|
|
)
|
|
|
|
min_value = round(
|
|
_scenario.actual_total_sales * (1 - overall_lower_bound / 100)
|
|
)
|
|
max_value = round(
|
|
_scenario.actual_total_sales * (1 + overall_upper_bound / 100)
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (
|
|
not st.session_state["allow_sales_update"]
|
|
and optimization_selection == target
|
|
):
|
|
st.warning("Invalid Input")
|
|
|
|
if (
|
|
not st.session_state["allow_spends_update"]
|
|
and optimization_selection == "Media Spends"
|
|
):
|
|
st.warning("Invalid Input")
|
|
|
|
status_placeholder = st.empty()
|
|
|
|
|
|
|
|
|
|
|
|
optimize_placeholder.button(
|
|
"Optimize",
|
|
on_click=optimize,
|
|
args=(st.session_state["optimization_key"], status_placeholder),
|
|
use_container_width=True,
|
|
)
|
|
|
|
st.markdown(
|
|
"""<hr class="spends-heading-seperator">""", unsafe_allow_html=True
|
|
)
|
|
_columns = st.columns((2, 1.5, 3, 3, 1))
|
|
with _columns[0]:
|
|
generate_spending_header("Channel")
|
|
with _columns[1]:
|
|
generate_spending_header("Spends Input")
|
|
with _columns[2]:
|
|
generate_spending_header("Spends")
|
|
with _columns[3]:
|
|
generate_spending_header("Revenue" if target == "Prospects" else target)
|
|
with _columns[4]:
|
|
generate_spending_header("Optimize")
|
|
|
|
st.markdown(
|
|
"""<hr class="spends-heading-seperator">""", unsafe_allow_html=True
|
|
)
|
|
|
|
if "acutual_predicted" not in st.session_state:
|
|
st.session_state["acutual_predicted"] = {
|
|
"Channel_name": [],
|
|
"Actual_spend": [],
|
|
"Optimized_spend": [],
|
|
"Delta": [],
|
|
"New_sales": [],
|
|
"Old_sales": [],
|
|
}
|
|
for i, channel_name in enumerate(channels_list):
|
|
|
|
if channel_name == "Email":
|
|
continue
|
|
|
|
|
|
_channel_class = st.session_state["scenario"].channels[channel_name]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_columns = st.columns((2, 1.5, 3, 3, 1))
|
|
response_curve_params = pd.read_excel(
|
|
"response_curves_parameters.xlsx", index_col="channel"
|
|
)
|
|
param_dicts = {
|
|
col: response_curve_params[col].to_dict()
|
|
for col in response_curve_params.columns
|
|
}
|
|
|
|
with _columns[0]:
|
|
st.write(
|
|
channel_name_formating(channel_name).replace(
|
|
"Connected & OTTTV", "Connected & OTT TV"
|
|
)
|
|
)
|
|
bin_placeholder = st.container()
|
|
|
|
with _columns[1]:
|
|
channel_bounds = _channel_class.bounds
|
|
|
|
channel_spends = float(_channel_class.actual_total_spends)
|
|
channel_bounds_min = float(_channel_class.channel_bounds_min)
|
|
channel_bounds_max = float(_channel_class.channel_bounds_max)
|
|
min_value = float((1 - channel_bounds_min / 100) * channel_spends)
|
|
max_value = float((1 + channel_bounds_max / 100) * channel_spends)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_columns_min = st.columns(1)
|
|
with _columns_min[0]:
|
|
spend_input = st.text_input(
|
|
"Absolute",
|
|
key=channel_name,
|
|
|
|
on_change=partial(update_data, channel_name),
|
|
)
|
|
channel_name_lower_bound = f"{channel_name}_lower_bound"
|
|
|
|
if channel_name_lower_bound not in st.session_state:
|
|
st.session_state[channel_name_lower_bound] = str(
|
|
round(
|
|
param_dicts["x_min"][channel_name]
|
|
* 10400
|
|
/ param_dicts["current_spends"][channel_name]
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channel_bounds_min = 30.0
|
|
|
|
|
|
|
|
if not validate_input(spend_input):
|
|
st.error("Invalid input")
|
|
|
|
def calc_min_value():
|
|
return float(st.session_state[channel_name_upper_bound])
|
|
|
|
channel_name_current = f"{channel_name}_change"
|
|
with _columns_min[0]:
|
|
channel_name_upper_bound = f"{channel_name}_upper_bound"
|
|
if channel_name_upper_bound not in st.session_state:
|
|
st.session_state[channel_name_upper_bound] = str(100)
|
|
|
|
st.number_input(
|
|
"Percent Change",
|
|
key=channel_name_current,
|
|
step=1.00,
|
|
value=0.00,
|
|
on_change=partial(update_data_by_percent, channel_name),
|
|
max_value=st.session_state["overall_upper_bound"],
|
|
min_value=-st.session_state["overall_lower_bound"],
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
channel_bounds_max = 30.0
|
|
|
|
with _columns[2]:
|
|
|
|
current_channel_spends = float(
|
|
_channel_class.modified_total_spends
|
|
|
|
)
|
|
actual_channel_spends = float(
|
|
_channel_class.actual_total_spends
|
|
|
|
)
|
|
spends_delta = float(
|
|
|
|
_channel_class.delta_spends
|
|
)
|
|
st.session_state["acutual_predicted"]["Channel_name"].append(
|
|
channel_name
|
|
)
|
|
st.session_state["acutual_predicted"]["Actual_spend"].append(
|
|
actual_channel_spends
|
|
)
|
|
st.session_state["acutual_predicted"]["Optimized_spend"].append(
|
|
current_channel_spends
|
|
)
|
|
st.session_state["acutual_predicted"]["Delta"].append(spends_delta)
|
|
_spend_cols = st.columns(2)
|
|
with _spend_cols[0]:
|
|
|
|
st.markdown(
|
|
f"<p> Actual Spends<h5>{format_numbers(actual_channel_spends)}</h5> </p>",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.markdown(
|
|
f"<p>Spends Change<h5>{format_numbers(spends_delta)}</h5> </p>",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with _spend_cols[1]:
|
|
st.markdown(
|
|
f"<p>Simulated Spends<h5>{format_numbers(current_channel_spends)}</h5> </p>",
|
|
unsafe_allow_html=True,
|
|
)
|
|
st.markdown(
|
|
f'<p>Spends Percent<h5>{numerize(( spends_delta/actual_channel_spends)*100,0) +"%"}</h5> </p>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with _columns[3]:
|
|
|
|
current_channel_sales = float(_channel_class.modified_total_sales)
|
|
actual_channel_sales = float(_channel_class.actual_total_sales)
|
|
sales_delta = float(_channel_class.delta_sales)
|
|
st.session_state["acutual_predicted"]["Old_sales"].append(
|
|
actual_channel_sales
|
|
)
|
|
st.session_state["acutual_predicted"]["New_sales"].append(
|
|
current_channel_sales
|
|
)
|
|
|
|
|
|
|
|
_prospect_cols = st.columns(2)
|
|
with _prospect_cols[0]:
|
|
|
|
st.markdown(
|
|
f"<p>Actual Revenue<h5>$ {format_numbers_f(actual_channel_sales)}</h5> </p>",
|
|
unsafe_allow_html=True,
|
|
)
|
|
st.markdown(
|
|
f"<p>Revenue Change<h5>$ {format_numbers_f(sales_delta)}</h5> </p>",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with _prospect_cols[1]:
|
|
st.markdown(
|
|
f"<p>Simulated Revenue<h5>$ {format_numbers_f(current_channel_sales)}</h5> </p>",
|
|
unsafe_allow_html=True,
|
|
)
|
|
st.markdown(
|
|
f'<p>Revenue Percent<h5>{numerize(( _channel_class.delta_sales/actual_channel_sales)*100,0) +"%"}</h5> </p>',
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with _columns[4]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.checkbox(
|
|
label="select for optimization",
|
|
key=f"{channel_name}_selected",
|
|
value=False,
|
|
on_change=partial(
|
|
select_channel_for_optimization, channel_name
|
|
),
|
|
label_visibility="collapsed",
|
|
)
|
|
|
|
st.markdown(
|
|
"""<hr class="spends-child-seperator">""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
col = channels_list[i]
|
|
x_actual = st.session_state["scenario"].channels[col].actual_spends
|
|
x_modified = st.session_state["scenario"].channels[col].modified_spends
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x_total = x_modified.sum()
|
|
power = np.ceil(np.log(x_actual.max()) / np.log(10)) - 3
|
|
|
|
updated_rcs_key = (
|
|
f"{metrics_selected}#@{panel_selected}#@{channel_name}"
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
summary_df = pd.DataFrame(st.session_state["acutual_predicted"])
|
|
|
|
|
|
summary_df.drop_duplicates(
|
|
subset="Channel_name", keep="last", inplace=True
|
|
)
|
|
|
|
|
|
summary_df_sorted = summary_df.sort_values(by="Delta", ascending=False)
|
|
summary_df_sorted["Delta_percent"] = np.round(
|
|
(
|
|
(
|
|
summary_df_sorted["Optimized_spend"]
|
|
/ summary_df_sorted["Actual_spend"]
|
|
)
|
|
- 1
|
|
)
|
|
* 100,
|
|
2,
|
|
)
|
|
|
|
summary_df_sorted = summary_df_sorted.sort_values(
|
|
by=["Optimized_spend"], ascending=False
|
|
)
|
|
summary_df_sorted["old_efficiency"] = (
|
|
summary_df_sorted["Old_sales"]
|
|
/ summary_df_sorted["Actual_spend"].sum()
|
|
) / (
|
|
summary_df_sorted["Actual_spend"]
|
|
/ summary_df_sorted["Actual_spend"].sum()
|
|
)
|
|
summary_df_sorted["new_efficiency"] = (
|
|
summary_df_sorted["New_sales"]
|
|
/ summary_df_sorted["Optimized_spend"].sum()
|
|
) / (
|
|
summary_df_sorted["Optimized_spend"]
|
|
/ summary_df_sorted["Optimized_spend"].sum()
|
|
)
|
|
|
|
a = (
|
|
summary_df_sorted[summary_df_sorted["Channel_name"] == col]
|
|
).reset_index()["new_efficiency"][0]
|
|
b = (
|
|
summary_df_sorted[summary_df_sorted["Channel_name"] == col]
|
|
).reset_index()["old_efficiency"][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with bin_placeholder:
|
|
if a > 1:
|
|
fill_color_box = "#6bbf6b"
|
|
elif a <= 1:
|
|
fill_color_box = "#ff6868"
|
|
else:
|
|
fill_color_box = "#ff6868"
|
|
st.markdown(
|
|
f"""
|
|
<div style="
|
|
border-radius: 12px;
|
|
background-color: {fill_color_box};
|
|
padding: 10px;
|
|
text-align: center;
|
|
color: {'black'};
|
|
">
|
|
<p style="margin: 0; font-size: 16px;">Simulated ROI: {round(a,2)} </br> Actual ROI: {round(b,2)} </p>
|
|
<!--<p style="margin: 0; font-size: 16px;">Marginal ROI: {round(b,1)}</p>-->
|
|
</div>
|
|
""",
|
|
unsafe_allow_html=True,
|
|
)
|
|
|
|
|
|
|
|
with st.expander("See Response Curves", expanded=True):
|
|
fig = plot_response_curves(summary_df_sorted)
|
|
|
|
|
|
|
|
summary_df = pd.DataFrame(st.session_state["acutual_predicted"])
|
|
|
|
summary_df.drop_duplicates(subset="Channel_name", keep="last", inplace=True)
|
|
|
|
|
|
summary_df_sorted = summary_df.sort_values(by="Delta", ascending=False)
|
|
summary_df_sorted["Delta_percent"] = np.round(
|
|
(
|
|
(
|
|
summary_df_sorted["Optimized_spend"]
|
|
/ summary_df_sorted["Actual_spend"]
|
|
)
|
|
- 1
|
|
)
|
|
* 100,
|
|
2,
|
|
)
|
|
|
|
with open("summary_df.pkl", "wb") as f:
|
|
pickle.dump(summary_df_sorted, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
scenario_planner_plots()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_columns = st.columns(2)
|
|
|
|
st.subheader("Save Scenario")
|
|
scenario_name = st.text_input(
|
|
"Scenario name",
|
|
key="scenario_input",
|
|
placeholder="Scenario name",
|
|
label_visibility="collapsed",
|
|
)
|
|
st.button(
|
|
"Save",
|
|
on_click=lambda: save_scenario(scenario_name),
|
|
disabled=len(st.session_state["scenario_input"])
|
|
== 0,
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if st.button("Prepare Analysis Download"):
|
|
fig1, fig2, fig3 = scenario_planner_plots2()
|
|
ppt_file = save_ppt_file(summary_df_sorted, fig1, fig2, fig3)
|
|
|
|
try:
|
|
|
|
st.download_button(
|
|
label="Download Response Curves And Optimised Spends Overview",
|
|
data=ppt_file,
|
|
file_name="MMM_Scenario_Planner_Presentation.pptx",
|
|
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
)
|
|
except:
|
|
st.write("")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elif auth_status == False:
|
|
st.error("Username/Password is incorrect")
|
|
|
|
if auth_status != True:
|
|
try:
|
|
username_forgot_pw, email_forgot_password, random_password = (
|
|
authenticator.forgot_password("Forgot password")
|
|
)
|
|
if username_forgot_pw:
|
|
st.session_state["config"]["credentials"]["usernames"][username_forgot_pw][
|
|
"password"
|
|
] = stauth.Hasher([random_password]).generate()[0]
|
|
send_email(email_forgot_password, random_password)
|
|
st.success("New password sent securely")
|
|
|
|
elif username_forgot_pw == False:
|
|
st.error("Username not found")
|
|
except Exception as e:
|
|
st.error(e)
|
|
|