''' MMO Build Sprint 3 additions : contributions calculated using tuned Mixed LM model pending : contributions calculations using - 1. not tuned Mixed LM model, 2. tuned OLS model, 3. not tuned OLS model MMO Build Sprint 4 additions : response metrics selection pending : contributions calculations using - 1. not tuned Mixed LM model, 2. tuned OLS model, 3. not tuned OLS model ''' import streamlit as st import pandas as pd from sklearn.preprocessing import MinMaxScaler import pickle 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 st.set_page_config(layout='wide') load_local_css('styles.css') set_header() 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='Revenue' target='Revenue' # 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 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='revenue' file_selected = ( f"Overview_data_test_panel@#revenue.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))) 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("