File size: 14,593 Bytes
46d2500
 
 
 
 
85d2c7e
fef7f72
46d2500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fef7f72
46d2500
 
 
 
 
f4e26b8
46d2500
 
 
 
 
f7bb281
46d2500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d2c7e
 
f4e26b8
85d2c7e
 
 
 
f4e26b8
 
 
fef7f72
 
 
 
 
 
 
 
85d2c7e
fef7f72
 
85d2c7e
 
fef7f72
 
85d2c7e
a7b3ed8
 
 
 
 
 
 
 
 
 
 
85d2c7e
fef7f72
 
 
 
 
 
85d2c7e
 
 
 
 
 
a7b3ed8
85d2c7e
f4e26b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d2c7e
fef7f72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85d2c7e
 
 
fef7f72
85d2c7e
 
 
 
f4e26b8
85d2c7e
 
 
 
 
 
 
 
 
 
f4e26b8
 
85d2c7e
 
 
 
 
 
 
 
 
a7b3ed8
 
 
 
46d2500
85d2c7e
 
46d2500
85d2c7e
 
 
 
 
46d2500
85d2c7e
46d2500
85d2c7e
 
46d2500
85d2c7e
46d2500
85d2c7e
 
 
 
46d2500
85d2c7e
 
46d2500
 
85d2c7e
46d2500
85d2c7e
 
 
 
46d2500
 
 
85d2c7e
 
 
46d2500
 
85d2c7e
 
 
 
f7bb281
85d2c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46d2500
85d2c7e
 
46d2500
85d2c7e
 
46d2500
 
85d2c7e
 
 
 
46d2500
85d2c7e
 
46d2500
85d2c7e
46d2500
85d2c7e
46d2500
 
 
 
 
 
 
 
 
 
 
 
 
 
85d2c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7b3ed8
85d2c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

import streamlit as st
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import pickle
import Streamlit_functions as sf
from utilities import (load_authenticator)

from utilities_with_panel import (set_header,
                                  overview_test_data_prep_panel,
                                  overview_test_data_prep_nonpanel,
                                  initialize_data,
                                  load_local_css,
                                  create_channel_summary,
                                  create_contribution_pie,
                                  create_contribuion_stacked_plot,
                                  create_channel_spends_sales_plot,
                                  format_numbers,
                                  channel_name_formating)

import plotly.graph_objects as go
import streamlit_authenticator as stauth
import yaml
from yaml import SafeLoader
import time
from datetime import datetime,timedelta

st.set_page_config(layout='wide')
load_local_css('styles.css')
set_header()

st.title("Model Result Overview")

def get_random_effects(media_data, panel_col, mdf):
    random_eff_df = pd.DataFrame(columns=[panel_col, "random_effect"])

    for i, market in enumerate(media_data[panel_col].unique()):
        # print(i, end='\r')
        intercept = mdf.random_effects[market].values[0]
        random_eff_df.loc[i, 'random_effect'] = intercept
        random_eff_df.loc[i, panel_col] = market

    return random_eff_df


def process_train_and_test(train, test, features, panel_col, target_col):
    X1 = train[features]

    ss = MinMaxScaler()
    X1 = pd.DataFrame(ss.fit_transform(X1), columns=X1.columns)

    X1[panel_col] = train[panel_col]
    X1[target_col] = train[target_col]

    if test is not None:
        X2 = test[features]
        X2 = pd.DataFrame(ss.transform(X2), columns=X2.columns)
        X2[panel_col] = test[panel_col]
        X2[target_col] = test[target_col]
        return X1, X2
    return X1

def mdf_predict(X_df, mdf, random_eff_df) :
    X=X_df.copy()
    X=pd.merge(X, random_eff_df[[panel_col,'random_effect']], on=panel_col, how='left')
    X['pred_fixed_effect'] = mdf.predict(X)

    X['pred'] = X['pred_fixed_effect'] + X['random_effect']
    X.to_csv('Test/merged_df_contri.csv',index=False)
    X.drop(columns=['pred_fixed_effect', 'random_effect'], inplace=True)

    return X


target_col='Prospects'
target='Prospects'

# is_panel=False
# is_panel = st.session_state['is_panel']
#panel_col = [col.lower().replace('.','_').replace('@','_').replace(" ", "_").replace('-', '').replace(':', '').replace("__", "_") for col in  st.session_state['bin_dict']['Panel Level 1']  ] [0]# set the panel column
panel_col='Panel'
date_col = 'date'

#st.write(media_data)

is_panel = True 

# panel_col='markets'
date_col = 'date'
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

authenticator = st.session_state.get('authenticator')

if authenticator is None:
    authenticator = load_authenticator()
    
name, authentication_status, username = authenticator.login('Login', 'main')
auth_status = st.session_state['authentication_status']

if auth_status:
    authenticator.logout('Logout', 'main')
    
    is_state_initiaized = st.session_state.get('initialized',False)
    if not is_state_initiaized:
        a=1
    
    # st.header("")
    # st.markdown("<h5 style='font-weight: normal;'>MMM Readout for Selected Period</h5>", unsafe_allow_html=True)
    #### Input Select Start and End Date  
    
    # Create two columns for start date and end date input
    col1, col2 = st.columns(2)

    # now = datetime.now()
    # us_format = now.strftime("%m/%d/%Y")

    # Define the minimum and maximum dates
    min_date,max_date = sf.get_date_range()
    # st.write(min_date,max_date)
    # min_date = datetime(2023, 1, 1)
    # max_date = datetime(2024, 12, 31)
    default_date1,default_date2 = sf.get_default_dates()   
    # st.write(default_date1,default_date2)                                                       
    with col1:
        start_date = st.date_input("Start Date: ",value=default_date1,min_value=min_date,
                                    max_value=max_date)
        
    with col2:
        end_date = st.date_input("End Date: ",value = default_date2,min_value=min_date,
                                    max_value=max_date)
    
    # col1, col2 = st.columns(2)
    # with col1:
    #     fig = sf.pie_spend(start_date,end_date)
    #     st.plotly_chart(fig,use_container_width=True)
    # with col2:
    #     fig = sf.pie_contributions(start_date,end_date)
    #     st.plotly_chart(fig,use_container_width=True)
    # st.header("Distribution of Spends and Contributions")
    fig = sf.pie_charts(start_date,end_date)
    st.plotly_chart(fig,use_container_width=True)

    # Dropdown menu options
    st.markdown("<h1 style='font-size:28px;'>Change in MMM Estimated Prospect Contributions</h1>", unsafe_allow_html=True)
    # st.header("Change in MMM Estimated Prospect Contributions")




    options = [
        "Month on Month",
        "Year on Year"]
    col1, col2 = st.columns(2)
         # Create a dropdown menu
    with col1:
        selected_option = st.selectbox('Select a comparison', options)
    with col2:
        st.markdown("""</br>""",unsafe_allow_html=True)
        if selected_option == "Month on Month" :

            st.markdown(
                f"""

                <div style="padding: 5px; border-radius: 5px; background-color: #FFFFE0; width: fit-content; display: inline-block;">

                    <strong> Comparision of current month spends to previous month spends</strong>

                </div>

                """,
                unsafe_allow_html=True
            )
        else :
            st.markdown(
                f"""

                <div style="padding: 5px; border-radius: 5px; background-color: #FFFFE0; width: fit-content; display: inline-block;">

                    <strong> Comparision of current month spends to the same month in previous year</strong>

                </div>

                """,
                unsafe_allow_html=True
            )
        # Waterfall chart
    
    def get_month_year_list(start_date, end_date):
        # Generate a range of dates from start_date to end_date with a monthly frequency
        dates = pd.date_range(start=start_date, end=end_date, freq='MS')  # 'MS' is month start frequency
        
        # Extract month and year from each date and create a list of tuples
        month_year_list = [(date.month, date.year) for date in dates]
        
        return month_year_list
    def get_start_end_dates(month, year):
        start_date = datetime(year, month, 1).date()
        
        if month == 12:
            end_date = datetime(year + 1, 1, 1).date() - timedelta(days=1)
        else:
            end_date = datetime(year, month + 1, 1).date() - timedelta(days=1)
        
        return start_date, end_date
    
    month_year_list = get_month_year_list(start_date, end_date)
    dropdown_options = [f"{date.strftime('%B %Y')}" for date in pd.date_range(start=start_date, end=end_date, freq='MS')]
    waterfall_option = st.selectbox("Select a month:", dropdown_options)
    waterfall_date = datetime.strptime(waterfall_option, "%B %Y")
    waterfall_month = waterfall_date.month 
    waterfall_year = waterfall_date.year 
    waterfall_start_date, waterfall_end_date = get_start_end_dates(waterfall_month, waterfall_year)

    fig = sf.waterfall(waterfall_start_date,waterfall_end_date,selected_option)
    st.plotly_chart(fig,use_container_width=True)
        
    # Waterfall table
    shares_df = sf.shares_df_func(waterfall_start_date,waterfall_end_date)
    st.table(sf.waterfall_table_func(shares_df).style.format("{:.0%}"))

    ## Channel Contribution Bar Chart
    st.plotly_chart(sf.channel_contribution(start_date,end_date),use_container_width=True)
    st.plotly_chart(sf.chanel_spends(start_date,end_date),use_container_width=True)
    # Format first three rows in percentage format
    # styled_df = sf.shares_table_func(shares_df)
    # # styled_df = styled_df.round(0).astype(int)
    # styled_df.iloc[:3] = (styled_df.iloc[:3]).astype(int)

        # # Round next two rows to two decimal places
        # styled_df.iloc[3:5] = styled_df.iloc[3:5].round(0).astype(str)

        # st.table(styled_df)
    st.dataframe(sf.shares_table_func(shares_df),use_container_width=True)
    
    st.dataframe(sf.eff_table_func(shares_df).style.format({"TOTAL SPEND": "{:,.0f}", "TOTAL SUPPORT": "{:,.0f}", "TOTAL CONTRIBUTION": "{:,.0f}"}),use_container_width=True)

        ### CPP CHART
    st.plotly_chart(sf.cpp(start_date,end_date),use_container_width=True)
        
        ### Base decomp CHART
    st.plotly_chart(sf.base_decomp(),use_container_width=True)

        ### Media decomp CHART
    st.plotly_chart(sf.media_decomp(),use_container_width=True)
     
    
    # st.write(fig.columns)
    # st.dataframe(fig)

    # def panel_fetch(file_selected):
    #     raw_data_mmm_df = pd.read_excel(file_selected, sheet_name="RAW DATA MMM")

    #     if "Panel" in raw_data_mmm_df.columns:
    #         panel = list(set(raw_data_mmm_df["Panel"]))
    #     else:
    #         raw_data_mmm_df = None
    #         panel = None

    #     return panel

    # def rerun():
    #     st.rerun()

    # metrics_selected='prospects'

    # file_selected = (
    #         f"Overview_data_test_panel@#{metrics_selected}.xlsx"
    #     )
    # panel_list = panel_fetch(file_selected)

    # if "selected_markets" not in st.session_state:
    #     st.session_state['selected_markets']='DMA1'


    # st.header('Overview of previous spends')

    # selected_market= st.selectbox(
    #         "Select Markets",
    #         ["Total Market"] + panel_list
    #     )



    # initialize_data(target_col,selected_market)
    # scenario = st.session_state['scenario']
    # raw_df = st.session_state['raw_df']
    # st.write(scenario.actual_total_spends)
    # st.write(scenario.actual_total_sales)
    # columns = st.columns((1,1,3))

    # with columns[0]:
    #     st.metric(label='Spends', value=format_numbers(float(scenario.actual_total_spends)))
    # #### print(f"##################### {scenario.actual_total_sales} ##################")
    # with columns[1]:
    #     st.metric(label=target, value=format_numbers(float(scenario.actual_total_sales),include_indicator=False))


    # actual_summary_df = create_channel_summary(scenario)
    # actual_summary_df['Channel'] = actual_summary_df['Channel'].apply(channel_name_formating) 

    # columns = st.columns((2,1))
    # #with columns[0]:
    # with st.expander('Channel wise overview'):
    #     st.markdown(actual_summary_df.style.set_table_styles(
    #     [{
    #         'selector': 'th',
    #         'props': [('background-color', '#FFFFF')]
    #     },
    #         {
    #         'selector' : 'tr:nth-child(even)',
    #         'props' : [('background-color', '#FFFFF')]
    #         }]).to_html(), unsafe_allow_html=True)
            
    # st.markdown("<hr>",unsafe_allow_html=True)
    # ##############################

    # st.plotly_chart(create_contribution_pie(scenario),use_container_width=True)
    # st.markdown("<hr>",unsafe_allow_html=True)


    # ################################3
    # st.plotly_chart(create_contribuion_stacked_plot(scenario),use_container_width=True)
    # st.markdown("<hr>",unsafe_allow_html=True)
    # #######################################

    # selected_channel_name = st.selectbox('Channel', st.session_state['channels_list'] + ['non media'], format_func=channel_name_formating)
    # selected_channel = scenario.channels.get(selected_channel_name,None)

    # st.plotly_chart(create_channel_spends_sales_plot(selected_channel), use_container_width=True)

    # st.markdown("<hr>",unsafe_allow_html=True)

# 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.success('New password sent securely')
#             # Random password to be transferred to user securely
#         elif username_forgot_pw == False:
#             st.error('Username not found')
#     except Exception as e:
#         st.error(e)
# st.header("")
# st.markdown("<h5 style='font-weight: normal;'>MMM Readout for Selected Period</h5>", unsafe_allow_html=True)
# #### Input Select Start and End Date
    
# # Create two columns for start date and end date input
# col1, col2 = st.columns(2)
    
# with col1:
#     start_date = st.date_input("Start Date: ")
    
# with col2:
#     end_date = st.date_input("End Date: ")
# # Dropdown menu options
# options = [
#     "Month on Month",
#     "Year on Year"]
# col1, col2 = st.columns(2)
#      # Create a dropdown menu
# with col1:
#     selected_option = st.selectbox('Select a comparison', options)
# with col2:
#     st.write("")
#     # Waterfall chart
# fig = sf.waterfall(start_date,end_date,selected_option)
# st.plotly_chart(fig)
    
# # Waterfall table
# shares_df = sf.shares_df_func(start_date,end_date)
# st.table(sf.waterfall_table_func(shares_df).style.format("{:.0%}"))

# ## Channel Contribution Bar Chart
# st.plotly_chart(sf.channel_contribution(start_date,end_date))
# # Format first three rows in percentage format
# # styled_df = sf.shares_table_func(shares_df)
# # # styled_df = styled_df.round(0).astype(int)
# # styled_df.iloc[:3] = (styled_df.iloc[:3]).astype(int)

#     # # Round next two rows to two decimal places
#     # styled_df.iloc[3:5] = styled_df.iloc[3:5].round(0).astype(str)

#     # st.table(styled_df)
# st.dataframe(sf.shares_table_func(shares_df))

# st.dataframe(sf.eff_table_func(shares_df))

#     ### CPP CHART
# st.plotly_chart(sf.cpp(start_date,end_date))
    
#     ### Base decomp CHART
# st.plotly_chart(sf.base_decomp())

#     ### Media decomp CHART
#     st.plotly_chart(sf.media_decomp())