BlendMMM commited on
Commit
2037919
1 Parent(s): 1e6110a

Update Model_Result_Overview.py

Browse files
Files changed (1) hide show
  1. Model_Result_Overview.py +124 -124
Model_Result_Overview.py CHANGED
@@ -38,175 +38,175 @@ load_local_css('styles.css')
38
  set_header()
39
 
40
 
41
- def get_random_effects(media_data, panel_col, mdf):
42
- random_eff_df = pd.DataFrame(columns=[panel_col, "random_effect"])
43
 
44
- for i, market in enumerate(media_data[panel_col].unique()):
45
- print(i, end='\r')
46
- intercept = mdf.random_effects[market].values[0]
47
- random_eff_df.loc[i, 'random_effect'] = intercept
48
- random_eff_df.loc[i, panel_col] = market
49
 
50
- return random_eff_df
51
 
52
 
53
- def process_train_and_test(train, test, features, panel_col, target_col):
54
- X1 = train[features]
55
 
56
- ss = MinMaxScaler()
57
- X1 = pd.DataFrame(ss.fit_transform(X1), columns=X1.columns)
58
 
59
- X1[panel_col] = train[panel_col]
60
- X1[target_col] = train[target_col]
61
 
62
- if test is not None:
63
- X2 = test[features]
64
- X2 = pd.DataFrame(ss.transform(X2), columns=X2.columns)
65
- X2[panel_col] = test[panel_col]
66
- X2[target_col] = test[target_col]
67
- return X1, X2
68
- return X1
69
 
70
- def mdf_predict(X_df, mdf, random_eff_df) :
71
- X=X_df.copy()
72
- X=pd.merge(X, random_eff_df[[panel_col,'random_effect']], on=panel_col, how='left')
73
- X['pred_fixed_effect'] = mdf.predict(X)
74
 
75
- X['pred'] = X['pred_fixed_effect'] + X['random_effect']
76
- X.to_csv('Test/merged_df_contri.csv',index=False)
77
- X.drop(columns=['pred_fixed_effect', 'random_effect'], inplace=True)
78
 
79
- return X
80
 
81
 
82
- target_col='Revenue'
83
- target='Revenue'
84
 
85
- # is_panel=False
86
- # is_panel = st.session_state['is_panel']
87
- #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
88
- panel_col='Panel'
89
- date_col = 'date'
90
 
91
- #st.write(media_data)
92
 
93
- is_panel = True
94
 
95
- # panel_col='markets'
96
- date_col = 'date'
97
- for k, v in st.session_state.items():
98
 
99
- if k not in ['logout', 'login','config'] and not k.startswith('FormSubmitter'):
100
- st.session_state[k] = v
101
 
102
- authenticator = st.session_state.get('authenticator')
103
 
104
- if authenticator is None:
105
- authenticator = load_authenticator()
106
 
107
- name, authentication_status, username = authenticator.login('Login', 'main')
108
- auth_status = st.session_state['authentication_status']
109
 
110
- if auth_status:
111
- authenticator.logout('Logout', 'main')
112
 
113
- is_state_initiaized = st.session_state.get('initialized',False)
114
- if not is_state_initiaized:
115
- a=1
116
 
117
- def panel_fetch(file_selected):
118
- raw_data_mmm_df = pd.read_excel(file_selected, sheet_name="RAW DATA MMM")
119
 
120
- if "Panel" in raw_data_mmm_df.columns:
121
- panel = list(set(raw_data_mmm_df["Panel"]))
122
- else:
123
- raw_data_mmm_df = None
124
- panel = None
125
 
126
- return panel
127
 
128
- def rerun():
129
- st.rerun()
130
 
131
- metrics_selected='revenue'
132
 
133
- file_selected = (
134
- f"Overview_data_test_panel@#{metrics_selected}.xlsx"
135
- )
136
- panel_list = panel_fetch(file_selected)
137
 
138
- if "selected_markets" not in st.session_state:
139
- st.session_state['selected_markets']='DMA1'
140
 
141
 
142
- st.header('Overview of previous spends')
143
 
144
- selected_market= st.selectbox(
145
- "Select Markets",
146
- ["Total Market"] + panel_list
147
- )
148
 
149
 
150
 
151
- initialize_data(target_col,selected_market)
152
- scenario = st.session_state['scenario']
153
- raw_df = st.session_state['raw_df']
154
- # st.write(scenario.actual_total_spends)
155
- # st.write(scenario.actual_total_sales)
156
- columns = st.columns((1,1,3))
157
 
158
- with columns[0]:
159
- st.metric(label='Spends', value=format_numbers(float(scenario.actual_total_spends)))
160
- ###print(f"##################### {scenario.actual_total_sales} ##################")
161
- with columns[1]:
162
- st.metric(label=target, value=format_numbers(float(scenario.actual_total_sales),include_indicator=False))
163
 
164
 
165
- actual_summary_df = create_channel_summary(scenario)
166
- actual_summary_df['Channel'] = actual_summary_df['Channel'].apply(channel_name_formating)
167
 
168
- columns = st.columns((2,1))
169
- #with columns[0]:
170
- with st.expander('Channel wise overview'):
171
- st.markdown(actual_summary_df.style.set_table_styles(
172
- [{
173
- 'selector': 'th',
174
- 'props': [('background-color', '#FFFFF')]
175
- },
176
- {
177
- 'selector' : 'tr:nth-child(even)',
178
- 'props' : [('background-color', '#FFFFF')]
179
- }]).to_html(), unsafe_allow_html=True)
180
 
181
- st.markdown("<hr>",unsafe_allow_html=True)
182
- ##############################
183
 
184
- st.plotly_chart(create_contribution_pie(scenario),use_container_width=True)
185
- st.markdown("<hr>",unsafe_allow_html=True)
186
 
187
 
188
- ################################3
189
- st.plotly_chart(create_contribuion_stacked_plot(scenario),use_container_width=True)
190
- st.markdown("<hr>",unsafe_allow_html=True)
191
- #######################################
192
 
193
- selected_channel_name = st.selectbox('Channel', st.session_state['channels_list'] + ['non media'], format_func=channel_name_formating)
194
- selected_channel = scenario.channels.get(selected_channel_name,None)
195
 
196
- st.plotly_chart(create_channel_spends_sales_plot(selected_channel), use_container_width=True)
197
 
198
- st.markdown("<hr>",unsafe_allow_html=True)
199
 
200
- # elif auth_status == False:
201
- # st.error('Username/Password is incorrect')
202
 
203
- # if auth_status != True:
204
- # try:
205
- # username_forgot_pw, email_forgot_password, random_password = authenticator.forgot_password('Forgot password')
206
- # if username_forgot_pw:
207
- # st.success('New password sent securely')
208
- # # Random password to be transferred to user securely
209
- # elif username_forgot_pw == False:
210
- # st.error('Username not found')
211
- # except Exception as e:
212
- # st.error(e)
 
38
  set_header()
39
 
40
 
41
+ # def get_random_effects(media_data, panel_col, mdf):
42
+ # random_eff_df = pd.DataFrame(columns=[panel_col, "random_effect"])
43
 
44
+ # for i, market in enumerate(media_data[panel_col].unique()):
45
+ # print(i, end='\r')
46
+ # intercept = mdf.random_effects[market].values[0]
47
+ # random_eff_df.loc[i, 'random_effect'] = intercept
48
+ # random_eff_df.loc[i, panel_col] = market
49
 
50
+ # return random_eff_df
51
 
52
 
53
+ # def process_train_and_test(train, test, features, panel_col, target_col):
54
+ # X1 = train[features]
55
 
56
+ # ss = MinMaxScaler()
57
+ # X1 = pd.DataFrame(ss.fit_transform(X1), columns=X1.columns)
58
 
59
+ # X1[panel_col] = train[panel_col]
60
+ # X1[target_col] = train[target_col]
61
 
62
+ # if test is not None:
63
+ # X2 = test[features]
64
+ # X2 = pd.DataFrame(ss.transform(X2), columns=X2.columns)
65
+ # X2[panel_col] = test[panel_col]
66
+ # X2[target_col] = test[target_col]
67
+ # return X1, X2
68
+ # return X1
69
 
70
+ # def mdf_predict(X_df, mdf, random_eff_df) :
71
+ # X=X_df.copy()
72
+ # X=pd.merge(X, random_eff_df[[panel_col,'random_effect']], on=panel_col, how='left')
73
+ # X['pred_fixed_effect'] = mdf.predict(X)
74
 
75
+ # X['pred'] = X['pred_fixed_effect'] + X['random_effect']
76
+ # X.to_csv('Test/merged_df_contri.csv',index=False)
77
+ # X.drop(columns=['pred_fixed_effect', 'random_effect'], inplace=True)
78
 
79
+ # return X
80
 
81
 
82
+ # target_col='Revenue'
83
+ # target='Revenue'
84
 
85
+ # # is_panel=False
86
+ # # is_panel = st.session_state['is_panel']
87
+ # #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
88
+ # panel_col='Panel'
89
+ # date_col = 'date'
90
 
91
+ # #st.write(media_data)
92
 
93
+ # is_panel = True
94
 
95
+ # # panel_col='markets'
96
+ # date_col = 'date'
97
+ # for k, v in st.session_state.items():
98
 
99
+ # if k not in ['logout', 'login','config'] and not k.startswith('FormSubmitter'):
100
+ # st.session_state[k] = v
101
 
102
+ # authenticator = st.session_state.get('authenticator')
103
 
104
+ # if authenticator is None:
105
+ # authenticator = load_authenticator()
106
 
107
+ # name, authentication_status, username = authenticator.login('Login', 'main')
108
+ # auth_status = st.session_state['authentication_status']
109
 
110
+ # if auth_status:
111
+ # authenticator.logout('Logout', 'main')
112
 
113
+ # is_state_initiaized = st.session_state.get('initialized',False)
114
+ # if not is_state_initiaized:
115
+ # a=1
116
 
117
+ # def panel_fetch(file_selected):
118
+ # raw_data_mmm_df = pd.read_excel(file_selected, sheet_name="RAW DATA MMM")
119
 
120
+ # if "Panel" in raw_data_mmm_df.columns:
121
+ # panel = list(set(raw_data_mmm_df["Panel"]))
122
+ # else:
123
+ # raw_data_mmm_df = None
124
+ # panel = None
125
 
126
+ # return panel
127
 
128
+ # def rerun():
129
+ # st.rerun()
130
 
131
+ # metrics_selected='revenue'
132
 
133
+ # file_selected = (
134
+ # f"Overview_data_test_panel@#{metrics_selected}.xlsx"
135
+ # )
136
+ # panel_list = panel_fetch(file_selected)
137
 
138
+ # if "selected_markets" not in st.session_state:
139
+ # st.session_state['selected_markets']='DMA1'
140
 
141
 
142
+ # st.header('Overview of previous spends')
143
 
144
+ # selected_market= st.selectbox(
145
+ # "Select Markets",
146
+ # ["Total Market"] + panel_list
147
+ # )
148
 
149
 
150
 
151
+ # initialize_data(target_col,selected_market)
152
+ # scenario = st.session_state['scenario']
153
+ # raw_df = st.session_state['raw_df']
154
+ # # st.write(scenario.actual_total_spends)
155
+ # # st.write(scenario.actual_total_sales)
156
+ # columns = st.columns((1,1,3))
157
 
158
+ # with columns[0]:
159
+ # st.metric(label='Spends', value=format_numbers(float(scenario.actual_total_spends)))
160
+ # ###print(f"##################### {scenario.actual_total_sales} ##################")
161
+ # with columns[1]:
162
+ # st.metric(label=target, value=format_numbers(float(scenario.actual_total_sales),include_indicator=False))
163
 
164
 
165
+ # actual_summary_df = create_channel_summary(scenario)
166
+ # actual_summary_df['Channel'] = actual_summary_df['Channel'].apply(channel_name_formating)
167
 
168
+ # columns = st.columns((2,1))
169
+ # #with columns[0]:
170
+ # with st.expander('Channel wise overview'):
171
+ # st.markdown(actual_summary_df.style.set_table_styles(
172
+ # [{
173
+ # 'selector': 'th',
174
+ # 'props': [('background-color', '#FFFFF')]
175
+ # },
176
+ # {
177
+ # 'selector' : 'tr:nth-child(even)',
178
+ # 'props' : [('background-color', '#FFFFF')]
179
+ # }]).to_html(), unsafe_allow_html=True)
180
 
181
+ # st.markdown("<hr>",unsafe_allow_html=True)
182
+ # ##############################
183
 
184
+ # st.plotly_chart(create_contribution_pie(scenario),use_container_width=True)
185
+ # st.markdown("<hr>",unsafe_allow_html=True)
186
 
187
 
188
+ # ################################3
189
+ # st.plotly_chart(create_contribuion_stacked_plot(scenario),use_container_width=True)
190
+ # st.markdown("<hr>",unsafe_allow_html=True)
191
+ # #######################################
192
 
193
+ # selected_channel_name = st.selectbox('Channel', st.session_state['channels_list'] + ['non media'], format_func=channel_name_formating)
194
+ # selected_channel = scenario.channels.get(selected_channel_name,None)
195
 
196
+ # st.plotly_chart(create_channel_spends_sales_plot(selected_channel), use_container_width=True)
197
 
198
+ # st.markdown("<hr>",unsafe_allow_html=True)
199
 
200
+ # # elif auth_status == False:
201
+ # # st.error('Username/Password is incorrect')
202
 
203
+ # # if auth_status != True:
204
+ # # try:
205
+ # # username_forgot_pw, email_forgot_password, random_password = authenticator.forgot_password('Forgot password')
206
+ # # if username_forgot_pw:
207
+ # # st.success('New password sent securely')
208
+ # # # Random password to be transferred to user securely
209
+ # # elif username_forgot_pw == False:
210
+ # # st.error('Username not found')
211
+ # # except Exception as e:
212
+ # # st.error(e)