Spaces:
Sleeping
Sleeping
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/book/EventTracker/11_Andon.ipynb. | |
# %% auto 0 | |
__all__ = ['df', 'farms', 'selected_farms', 'filter_df', 'generate_discrete_colors', 'get_discrete_colormap', 'get_summary', | |
'get_event_metrics', 'get_algo_performance', 'get_pie', 'get_data', 'get_farms', 'kpi'] | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 2 | |
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
import plotly.express as px | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 6 | |
st.set_page_config( | |
page_title="Andon", | |
layout='wide' | |
) | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 11 | |
def generate_discrete_colors(n): | |
colors = px.colors.qualitative.Plotly | |
return [colors[i % len(colors)] for i in range(n)] | |
generate_discrete_colors(5) | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 12 | |
def get_discrete_colormap(classes): | |
colors = generate_discrete_colors(len(classes)) | |
color_discrete_map = dict(zip(classes, colors)) | |
return color_discrete_map | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 13 | |
def get_summary(df, col_a='Type of Alert', col_b='Ground Truth'): | |
cal_df = df[[col_a, col_b]].groupby([col_a, col_b]).value_counts().reset_index() | |
color_discrete_map = get_discrete_colormap(df[col_b].dropna().unique()) | |
fig = px.bar(cal_df, y=col_a, | |
x='count', | |
color=col_b, | |
color_discrete_map=color_discrete_map, | |
orientation='h', | |
title=f'Summary: {col_a}|{col_b}') | |
fig.update_layout( | |
margin=dict(l=10, r=10, t=40, b=40), | |
yaxis=dict(automargin=True) | |
) | |
return fig | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 16 | |
def get_event_metrics(validated_df, | |
event_type_cols=['Heat', 'Missed Heat'], | |
event_type='Heat'): | |
val_event_df = validated_df[validated_df['Type of Alert'].isin(event_type_cols)] | |
a = val_event_df['Ground Truth'].value_counts() | |
recall = a.get('TRUE', 0)*100/(a.get('TRUE', 0)+a.get('MISSED', np.finfo(float).eps)) | |
precision = a.get('TRUE', 0)*100/(a.get('TRUE', 0)+a.get('FALSE', np.finfo(float).eps)) | |
return { | |
f'Validated_{event_type}_Recall': recall, | |
f'Validated_{event_type}_Precision': precision, | |
} | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 17 | |
def get_algo_performance(df): | |
confirmed_df = df[df['Ground Truth'].isin(['TRUE','FALSE', 'MISSED', 'EXCLUDE', 'OTHER'])] | |
validated_df = df[df['Ground Truth'].isin(['TRUE','FALSE', 'MISSED'])] | |
missed_df = df[df['Ground Truth'].isin(['MISSED'])] | |
heat_metrics = get_event_metrics(validated_df, | |
event_type_cols=['Heat', 'Missed Heat'], | |
event_type='Heat') | |
health_metrics = get_event_metrics(validated_df, | |
event_type_cols=['Health', 'Missed Health'], | |
event_type='Health') | |
data_coverage_metrics = {'Total':len(df), | |
'Confirmed': len(confirmed_df), | |
'Validated': len(validated_df), | |
'Missed': len(missed_df), | |
'Confirmed_Percentage': len(confirmed_df)*100/len(df), | |
'Validated_Percentage': len(validated_df)*100/len(df), | |
'Val_On_Confirm_Percentage': len(validated_df)*100/len(confirmed_df), | |
'Missed_On_Val_Percentage': len(missed_df)*100/len(validated_df) } | |
performance_metrics = {**data_coverage_metrics,**heat_metrics, **health_metrics} | |
return performance_metrics | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 20 | |
def get_pie(df, colname): | |
df_dist = df[colname].value_counts().reset_index() | |
fig = px.pie(df_dist, values='count', | |
names=colname, | |
title=f'{colname}', | |
color = colname, | |
color_discrete_map = get_discrete_colormap(df[colname].dropna().unique()) | |
) | |
return fig | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 35 | |
def get_data(url="https://docs.google.com/spreadsheets/d/1TbqmmSzXtY8DwolVo9rN7M1QdZxLG30nro2cqrvn2CA/export?format=csv&#gid=294763682"): | |
df = pd.read_csv(url).dropna(how='all') | |
df['Alert Date'] = df['Alert Date'].str.replace("//", "-") | |
df = df.dropna(subset=['Alert Date']) | |
df['Type of Alert'] = df['Type of Alert'].str.strip() | |
return df | |
# colors | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 36 | |
def get_farms(df): | |
return df['Dairy Farm'].dropna().unique().tolist() | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 39 | |
def kpi(df, ncols = 7): | |
metrics = get_algo_performance(df) | |
dcols = st.columns(ncols) | |
for (i, (k, v)) in enumerate(metrics.items()): | |
with dcols[i%ncols]: | |
st.metric(k, np.round(v, 2)) | |
# %% ../../nbs/book/EventTracker/11_Andon.ipynb 40 | |
#|eval: false | |
df = get_data() | |
farms = get_farms(df) | |
selected_farms = st.sidebar.multiselect('Select farms', farms, max_selections=len(farms)) | |
filter_df = df | |
if selected_farms: | |
str_farms = "|".join(selected_farms) | |
st.write(f"Selected_farms: {str_farms}") | |
filter_df = df[df['Dairy Farm'].isin(selected_farms)] | |
else: | |
st.write("Selected_farms: All") | |
# st.write(farms) | |
kpi(filter_df,ncols=4) | |
st.plotly_chart(get_pie(filter_df, colname = 'Type of Alert')) | |
st.plotly_chart(get_pie(filter_df, colname = 'Ground Truth')) | |
st.plotly_chart(get_pie(filter_df, colname = 'Dairy Farm')) | |
st.plotly_chart(get_summary(filter_df, col_a='Type of Alert', col_b='Ground Truth')) | |
st.plotly_chart(get_summary(filter_df, col_b='Type of Alert', col_a='Dairy Farm')) | |
st.plotly_chart(get_summary(filter_df, col_b='Ground Truth', col_a='Dairy Farm')) | |
# | |