Spaces:
Sleeping
Sleeping
File size: 12,564 Bytes
be4456f |
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 |
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import datetime
from utilities import set_header,initialize_data,load_local_css
from scipy.optimize import curve_fit
import statsmodels.api as sm
from plotly.subplots import make_subplots
st.set_page_config(
page_title="Data Validation",
page_icon=":shark:",
layout="wide",
initial_sidebar_state='collapsed'
)
load_local_css('styles.css')
set_header()
def format_numbers(x):
if abs(x) >= 1e6:
# Format as millions with one decimal place and commas
return f'{x/1e6:,.1f}M'
elif abs(x) >= 1e3:
# Format as thousands with one decimal place and commas
return f'{x/1e3:,.1f}K'
else:
# Format with one decimal place and commas for values less than 1000
return f'{x:,.1f}'
def format_axis(x):
if isinstance(x, tuple):
x = x[0] # Extract the numeric value from the tuple
if abs(x) >= 1e6:
return f'{x / 1e6:.0f}M'
elif abs(x) >= 1e3:
return f'{x / 1e3:.0f}k'
else:
return f'{x:.0f}'
attributred_app_installs=pd.read_csv("attributed_app_installs.csv")
attributred_app_installs_tactic=pd.read_excel('attributed_app_installs_tactic.xlsx')
data=pd.read_excel('Channel_wise_imp_click_spends.xlsx')
data['Date']=pd.to_datetime(data['Date'])
st.header('Saturation Curves')
# st.dataframe(data.head(2))
st.markdown('Data QC')
st.markdown('Channel wise summary')
summary_df=data.groupby(data['Date'].dt.strftime('%B %Y')).sum()
summary_df=summary_df.sort_index(key=lambda x: pd.to_datetime(x, format='%B %Y'))
st.dataframe(summary_df.applymap(format_numbers))
def line_plot_target(df,target,title):
df=df
df['Date_unix'] = df['Date'].apply(lambda x: x.timestamp())
# Perform polynomial fitting
coefficients = np.polyfit(df['Date_unix'], df[target], 1)
# st.dataframe(df)
coefficients = np.polyfit(df['Date'].view('int64'), df[target], 1)
trendline = np.poly1d(coefficients)
fig = go.Figure()
fig.add_trace(go.Scatter(x=df['Date'], y=df[target], mode='lines', name=target,line=dict(color='#11B6BD')))
trendline_x = df['Date']
trendline_y = trendline(df['Date'].view('int64'))
fig.add_trace(go.Scatter(x=trendline_x, y=trendline_y, mode='lines', name='Trendline', line=dict(color='#739FAE')))
fig.update_layout(
title=title,
xaxis=dict(type='date')
)
for year in df['Date'].dt.year.unique()[1:]:
january_1 = pd.Timestamp(year=year, month=1, day=1)
fig.add_shape(
go.layout.Shape(
type="line",
x0=january_1,
x1=january_1,
y0=0,
y1=1,
xref="x",
yref="paper",
line=dict(color="grey", width=1.5, dash="dash"),
)
)
return fig
channels_d= data.columns[:28]
channels=list(set([col.replace('_impressions','').replace('_clicks','').replace('_spend','') for col in channels_d if col.lower()!='date']))
channel= st.selectbox('Select Channel_name',channels)
target_column = st.selectbox('Select Channel)',[col for col in data.columns if col.startswith(channel)])
fig=line_plot_target(data, target=str(target_column), title=f'{str(target_column)} Over Time')
st.plotly_chart(fig, use_container_width=True)
# st.markdown('## Saturation Curve')
st.header('Build saturation curve')
# Your data
# st.write(len(attributred_app_installs))
# st.write(len(data))
# col=st.columns(3)
# with col[0]:
col=st.columns(2)
with col[0]:
if st.checkbox('Cap Outliers'):
x = data[target_column]
x.index=data['Date']
# st.write(x)
result = sm.tsa.seasonal_decompose(x, model='additive')
x_resid=result.resid
# fig = make_subplots(rows=1, cols=1, shared_xaxes=True, vertical_spacing=0.02)
# trace_x = go.Scatter(x=data['Date'], y=x, mode='lines', name='x')
# fig.add_trace(trace_x)
# trace_x_resid = go.Scatter(x=data['Date'], y=x_resid, mode='lines', name='x_resid', yaxis='y2',line=dict(color='orange'))
# fig.add_trace(trace_x_resid)
# fig.update_layout(title='',
# xaxis=dict(title='Date'),
# yaxis=dict(title='x', side='left'),
# yaxis2=dict(title='x_resid', side='right'))
# st.title('')
# st.plotly_chart(fig)
# x=result.resid
# x=x.fillna(0)
x_mean = np.mean(x)
x_std = np.std(x)
x_scaled = (x - x_mean) / x_std
lower_threshold = -2.0
upper_threshold = 2.0
x_scaled = np.clip(x_scaled, lower_threshold, upper_threshold)
else:
x = data[target_column]
x_mean = np.mean(x)
x_std = np.std(x)
x_scaled = (x - x_mean) / x_std
with col[1]:
if st.checkbox('Attributed'):
column=[col for col in attributred_app_installs.columns if col in target_column]
data['app_installs_appsflyer']=attributred_app_installs[column]
y=data['app_installs_appsflyer']
title='Attributed-App_installs_appsflyer'
# st.dataframe(y)
# st.dataframe(x)
# st.dataframe(x_scaled)
else:
y=data["app_installs_appsflyer"]
title='App_installs_appsflyer'
# st.write(len(y))
# Curve fitting function
def sigmoid(x, K, a, x0):
return K / (1 + np.exp(-a * (x - x0)))
initial_K = np.max(y)
initial_a = 1
initial_x0 = 0
columns=st.columns(3)
with columns[0]:
K = st.number_input('K (Amplitude)', min_value=0.01, max_value=2.0 * np.max(y), value=float(initial_K), step=5.0)
with columns[1]:
a = st.number_input('a (Slope)', min_value=0.01, max_value=5.0, value=float(initial_a), step=0.5)
with columns[2]:
x0 = st.number_input('x0 (Center)', min_value=float(min(x_scaled)), max_value=float(max(x_scaled)), value=float(initial_x0), step=2.0)
params, _ = curve_fit(sigmoid, x_scaled, y, p0=[K, a, x0], maxfev=20000)
x_slider = st.slider('X Value', min_value=float(min(x)), max_value=float(max(x))+1, value=float(x_mean), step=1.)
# Calculate the corresponding value on the fitted curve
x_slider_scaled = (x_slider - x_mean) / x_std
y_slider_fit = sigmoid(x_slider_scaled, *params)
# Display the corresponding value
st.write(f'{target_column}: {format_numbers(x_slider)}')
st.write(f'Corresponding App_installs: {format_numbers(y_slider_fit)}')
# Scatter plot of your data
fig = px.scatter(data_frame=data, x=x_scaled, y=y, labels={'x': f'{target_column}', 'y': 'App Installs'}, title=title)
# Add the fitted sigmoid curve to the plot
x_fit = np.linspace(min(x_scaled), max(x_scaled), 100) # Generate x values for the curve
y_fit = sigmoid(x_fit, *params)
fig.add_trace(px.line(x=x_fit, y=y_fit).data[0])
fig.data[1].update(line=dict(color='orange'))
fig.add_vline(x=x_slider_scaled, line_dash='dash', line_color='red', annotation_text=f'{format_numbers(x_slider)}')
x_tick_labels = {format_axis(x_scaled[i]): format_axis(x[i]) for i in range(len(x_scaled))}
num_points = 30 # Number of points you want to select
keys = list(x_tick_labels.keys())
values = list(x_tick_labels.values())
spacing = len(keys) // num_points # Calculate the spacing
if spacing==0:
spacing=15
selected_keys = keys[::spacing]
selected_values = values[::spacing]
else:
selected_keys = keys[::spacing]
selected_values = values[::spacing]
# Update the x-axis ticks with the selected keys and values
fig.update_xaxes(tickvals=selected_keys, ticktext=selected_values)
fig.update_xaxes(tickvals=list(x_tick_labels.keys()), ticktext=list(x_tick_labels.values()))
# Show the plot using st.plotly_chart
fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False)
fig.update_layout(
width=600, # Adjust the width as needed
height=600 # Adjust the height as needed
)
st.plotly_chart(fig)
st.markdown('Tactic level')
if channel=='paid_social':
tactic_data=pd.read_excel("Tatcic_paid.xlsx",sheet_name='paid_social_impressions')
else:
tactic_data=pd.read_excel("Tatcic_paid.xlsx",sheet_name='digital_app_display_impressions')
target_column = st.selectbox('Select Channel)',[col for col in tactic_data.columns if col!='Date' and col!='app_installs_appsflyer'])
fig=line_plot_target(tactic_data, target=str(target_column), title=f'{str(target_column)} Over Time')
st.plotly_chart(fig, use_container_width=True)
if st.checkbox('Cap Outliers',key='tactic1'):
x = tactic_data[target_column]
x_mean = np.mean(x)
x_std = np.std(x)
x_scaled = (x - x_mean) / x_std
lower_threshold = -2.0
upper_threshold = 2.0
x_scaled = np.clip(x_scaled, lower_threshold, upper_threshold)
else:
x = tactic_data[target_column]
x_mean = np.mean(x)
x_std = np.std(x)
x_scaled = (x - x_mean) / x_std
if st.checkbox('Attributed',key='tactic2'):
column=[col for col in attributred_app_installs_tactic.columns if col in target_column]
tactic_data['app_installs_appsflyer']=attributred_app_installs_tactic[column]
y=tactic_data['app_installs_appsflyer']
title='Attributed-App_installs_appsflyer'
# st.dataframe(y)
# st.dataframe(x)
# st.dataframe(x_scaled)
else:
y=data["app_installs_appsflyer"]
title='App_installs_appsflyer'
# st.write(len(y))
# Curve fitting function
def sigmoid(x, K, a, x0):
return K / (1 + np.exp(-a * (x - x0)))
# Curve fitting
# st.dataframe(x_scaled.head(3))
# # y=y.astype(float)
# st.dataframe(y.head(3))
initial_K = np.max(y)
initial_a = 1
initial_x0 = 0
K = st.number_input('K (Amplitude)', min_value=0.01, max_value=2.0 * np.max(y), value=float(initial_K), step=5.0,key='tactic3')
a = st.number_input('a (Slope)', min_value=0.01, max_value=5.0, value=float(initial_a), step=2.0,key='tactic41')
x0 = st.number_input('x0 (Center)', min_value=float(min(x_scaled)), max_value=float(max(x_scaled)), value=float(initial_x0), step=2.0,key='tactic4')
params, _ = curve_fit(sigmoid, x_scaled, y, p0=[K, a, x0], maxfev=20000)
# Slider to vary x
x_slider = st.slider('X Value', min_value=float(min(x)), max_value=float(max(x)), value=float(x_mean), step=1.,key='tactic7')
# Calculate the corresponding value on the fitted curve
x_slider_scaled = (x_slider - x_mean) / x_std
y_slider_fit = sigmoid(x_slider_scaled, *params)
# Display the corresponding value
st.write(f'{target_column}: {format_axis(x_slider)}')
st.write(f'Corresponding App_installs: {format_axis(y_slider_fit)}')
# Scatter plot of your data
fig = px.scatter(data_frame=data, x=x_scaled, y=y, labels={'x': f'{target_column}', 'y': 'App Installs'}, title=title)
# Add the fitted sigmoid curve to the plot
x_fit = np.linspace(min(x_scaled), max(x_scaled), 100) # Generate x values for the curve
y_fit = sigmoid(x_fit, *params)
fig.add_trace(px.line(x=x_fit, y=y_fit).data[0])
fig.data[1].update(line=dict(color='orange'))
fig.add_vline(x=x_slider_scaled, line_dash='dash', line_color='red', annotation_text=f'{format_numbers(x_slider)}')
x_tick_labels = {format_axis((x_scaled[i],0)): format_axis(x[i]) for i in range(len(x_scaled))}
num_points = 50 # Number of points you want to select
keys = list(x_tick_labels.keys())
values = list(x_tick_labels.values())
spacing = len(keys) // num_points # Calculate the spacing
if spacing==0:
spacing=2
selected_keys = keys[::spacing]
selected_values = values[::spacing]
else:
selected_keys = keys[::spacing]
selected_values = values[::spacing]
# Update the x-axis ticks with the selected keys and values
fig.update_xaxes(tickvals=selected_keys, ticktext=selected_values)
# Round the x-axis and y-axis tick values to zero decimal places
fig.update_xaxes(tickformat=".f") # Format x-axis ticks to zero decimal places
fig.update_yaxes(tickformat=".f") # Format y-axis ticks to zero decimal places
# Show the plot using st.plotly_chart
fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False)
fig.update_layout(
width=600, # Adjust the width as needed
height=600 # Adjust the height as needed
)
st.plotly_chart(fig) |