File size: 9,277 Bytes
9938325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fde220d
 
9938325
fde220d
 
9938325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# Importing necessary libraries
import streamlit as st

st.set_page_config(
    page_title="Saved Scenarios",
    page_icon="⚖️",
    layout="wide",
    initial_sidebar_state="collapsed",
)

import io
import os
import json
import pickle
import sqlite3
import zipfile
import numpy as np
import pandas as pd
from classes import numerize
from openpyxl import Workbook
from collections import OrderedDict
from utilities import (
    project_selection,
    initialize_data,
    set_header,
    load_local_css,
    name_formating,
)

# Styling
load_local_css("styles.css")
set_header()

# Create project_dct
if "project_dct" not in st.session_state:

    project_selection()
    st.stop()

    database_file = r"DB\User.db"

    conn = sqlite3.connect(
        database_file, check_same_thread=False
    )  # connection with sql db
    c = conn.cursor()

# Display project info
col_project_data = st.columns([2, 1])
with col_project_data[0]:
    st.markdown(f"**Welcome {st.session_state['username']}**")
with col_project_data[1]:
    st.markdown(f"**Current Project: {st.session_state['project_name']}**")

# Page Title
st.title("Saved Scenarios")
scenarios_name_placeholder = st.empty()


# Function to get saved scenarios dictionary
def get_saved_scenarios_dict():
    # Path to the saved scenarios file
    saved_scenarios_dict_path = os.path.join(
        st.session_state["project_path"], "saved_scenarios.pkl"
    )

    # Load existing scenarios if the file exists
    if os.path.exists(saved_scenarios_dict_path):
        with open(saved_scenarios_dict_path, "rb") as f:
            saved_scenarios_dict = pickle.load(f)
    else:
        saved_scenarios_dict = OrderedDict()

    return saved_scenarios_dict


# Function to format values based on their size
def format_value(value):
    return round(value, 4) if value < 1 else round(value, 1)


# Function to recursively convert non-serializable types to serializable ones
def convert_to_serializable(obj):
    if isinstance(obj, np.ndarray):
        return obj.tolist()
    elif isinstance(obj, dict):
        return {key: convert_to_serializable(value) for key, value in obj.items()}
    elif isinstance(obj, list):
        return [convert_to_serializable(element) for element in obj]
    elif isinstance(obj, (int, float, str, bool, type(None))):
        return obj
    else:
        # Fallback: convert the object to a string
        return str(obj)


# Function to generate zip file of current scenario
@st.cache_data(show_spinner=False)
def download_as_zip(
    df,
    scenario_data,
    excel_name="optimization_results.xlsx",
    json_name="scenario_params.json",
):
    # Create an in-memory bytes buffer for the ZIP file
    buffer = io.BytesIO()

    # Create a ZipFile object in memory
    with zipfile.ZipFile(buffer, "w") as zip_file:
        # Save the DataFrame to an Excel file in the zip using openpyxl
        excel_buffer = io.BytesIO()
        workbook = Workbook()
        sheet = workbook.active
        sheet.title = "Results"

        # Write DataFrame headers
        for col_num, column_title in enumerate(df.columns, 1):
            sheet.cell(row=1, column=col_num, value=column_title)

        # Write DataFrame data
        for row_num, row_data in enumerate(df.values, 2):
            for col_num, cell_value in enumerate(row_data, 1):
                sheet.cell(row=row_num, column=col_num, value=cell_value)

        # Save the workbook to the in-memory buffer
        workbook.save(excel_buffer)
        excel_buffer.seek(0)  # Rewind the buffer to the beginning
        zip_file.writestr(excel_name, excel_buffer.getvalue())

        # Save the dictionary to a JSON file in the zip
        json_buffer = io.BytesIO()
        json_buffer.write(
            json.dumps(convert_to_serializable(scenario_data), indent=4).encode("utf-8")
        )
        json_buffer.seek(0)  # Rewind the buffer to the beginning
        zip_file.writestr(json_name, json_buffer.getvalue())

    buffer.seek(0)  # Rewind the buffer to the beginning

    return buffer


# Function to delete the selected scenario from the saved scenarios dictionary
def delete_selected_scenarios(selected_scenario):
    # Path to the saved scenarios file
    saved_scenarios_dict_path = os.path.join(
        st.session_state["project_path"], "saved_scenarios.pkl"
    )

    # Load existing scenarios if the file exists
    if os.path.exists(saved_scenarios_dict_path):
        with open(saved_scenarios_dict_path, "rb") as f:
            saved_scenarios_dict = pickle.load(f)
    else:
        saved_scenarios_dict = OrderedDict()

    try:
        # Attempt to delete the selected scenario
        del saved_scenarios_dict[selected_scenario]

        # Save the updated dictionary back to the file
        with open(saved_scenarios_dict_path, "wb") as f:
            pickle.dump(saved_scenarios_dict, f)

    except KeyError:
        # If the scenario is not found in the dictionary, ignore the error
        pass


# Get saved scenarios dictionary and scenario name list
saved_scenarios_dict = get_saved_scenarios_dict()
scenarios_list = list(saved_scenarios_dict.keys())

# Check if the list of saved scenarios is empty
if len(scenarios_list) == 0:
    # Display a warning message if no scenarios are saved
    st.warning("No scenarios saved. Please save a scenario to load.", icon="⚠️")
    st.stop()

# Display a dropdown saved scenario list
selected_scenario = st.selectbox(
    "Pick a Scenario", sorted(scenarios_list), key="selected_scenario"
)
selected_scenario_data = saved_scenarios_dict[selected_scenario]

# Scenarios Name
metrics_name = selected_scenario_data["metrics_selected"]
panel_name = selected_scenario_data["panel_selected"]
optimization_name = selected_scenario_data["optimization"]

# Display the scenario details with bold "Metric," "Panel," and "Optimization"
scenarios_name_placeholder.markdown(
    f"**Metric**: {name_formating(metrics_name)}; **Panel**: {name_formating(panel_name)}; **Optimization**: {name_formating(optimization_name)}"
)

# Create columns for download and delete buttons
download_col, delete_col = st.columns(2)


channels_list = list(selected_scenario_data["channels"].keys())

# List to hold data for all channels
channels_data = []

# Iterate through each channel and gather required data
for channel in channels_list:
    channel_conversion_rate = selected_scenario_data["channels"][channel][
        "conversion_rate"
    ]
    channel_actual_spends = (
        selected_scenario_data["channels"][channel]["actual_total_spends"]
        * channel_conversion_rate
    )
    channel_optimized_spends = (
        selected_scenario_data["channels"][channel]["modified_total_spends"]
        * channel_conversion_rate
    )

    channel_actual_metrics = selected_scenario_data["channels"][channel][
        "actual_total_sales"
    ]
    channel_optimized_metrics = selected_scenario_data["channels"][channel][
        "modified_total_sales"
    ]

    channel_roi_mroi_data = selected_scenario_data["channel_roi_mroi"][channel]

    # Extract the ROI and MROI data
    actual_roi = channel_roi_mroi_data["actual_roi"]
    optimized_roi = channel_roi_mroi_data["optimized_roi"]
    actual_mroi = channel_roi_mroi_data["actual_mroi"]
    optimized_mroi = channel_roi_mroi_data["optimized_mroi"]

    # Calculate spends per metric
    spends_per_metrics_actual = channel_actual_spends / channel_actual_metrics
    spends_per_metrics_optimized = channel_optimized_spends / channel_optimized_metrics

    # Append the collected data as a dictionary to the list
    channels_data.append(
        {
            "Channel Name": channel,
            "Spends Actual": numerize(channel_actual_spends),
            "Spends Optimized": numerize(channel_optimized_spends),
            f"{name_formating(metrics_name)} Actual": numerize(channel_actual_metrics),
            f"{name_formating(metrics_name)} Optimized": numerize(
                channel_optimized_metrics
            ),
            "ROI Actual": format_value(actual_roi),
            "ROI Optimized": format_value(optimized_roi),
            "MROI Actual": format_value(actual_mroi),
            "MROI Optimized": format_value(optimized_mroi),
            f"Spends per {name_formating(metrics_name)} Actual": round(
                spends_per_metrics_actual, 2
            ),
            f"Spends per {name_formating(metrics_name)} Optimized": round(
                spends_per_metrics_optimized, 2
            ),
        }
    )

# Create a DataFrame from the collected data
df = pd.DataFrame(channels_data)

# Display the DataFrame
st.dataframe(df, hide_index=True)

# Generate download able data for selected scenario
buffer = download_as_zip(
    df,
    selected_scenario_data,
    excel_name="optimization_results.xlsx",
    json_name="scenario_params.json",
)

# Provide the buffer as a downloadable ZIP file in Streamlit
download_col.download_button(
    label="Download",
    data=buffer,
    file_name=f"{selected_scenario}_scenario_data.zip",
    mime="application/zip",
    use_container_width=True,
)

# Button to trigger the deletion of the selected scenario
delete_col.button(
    "Delete",
    use_container_width=True,
    on_click=delete_selected_scenarios,
    args=(selected_scenario,),
)