LabellingTracker / floating.py
rahuketu86's picture
Upload floating.py
a66dc2e verified
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/book/LabellingTracker/13_Floating.ipynb.
# %% auto 0
__all__ = ['df', 'kpi', 'get_floating_grp_data', 'get_floating_summary', 'get_floating_hist', 'get_step_df', 'get_gantt']
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 2
import streamlit as st
import pandas as pd
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import seaborn as sns
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 6
st.set_page_config(
page_title="Floating",
page_icon="πŸ‘‹",
layout='wide'
)
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 7
# st.sidebar.success("Select a demo above.")
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 10
def kpi(df):
cattle_days = df.loc[df['TAG']=='FLOATING', ['CattleFolder/Frame', 'SubFolder']].groupby('CattleFolder/Frame').count().sum().values[0]
cattle_floating = df.loc[df['TAG']=='FLOATING', ['CattleFolder/Frame']].nunique().values[0]
accounts_floating = df.loc[df['TAG']=='FLOATING', 'AccountNumber'].nunique()
count_user_floating = len(set(df.loc[df['TAG']=='FLOATING', 'Assigned'].dropna().str.split('/').sum()))
count_valid_floating = (df.loc[df['TAG']=='FLOATING', ['CattleFolder/Frame', 'SubFolder']].groupby('CattleFolder/Frame').count() > 1)['SubFolder'].sum()
count_exceptional_floating =(df.loc[df['TAG']=='FLOATING', ['CattleFolder/Frame', 'SubFolder']].groupby('CattleFolder/Frame').count() > 10)['SubFolder'].sum()
col1, col2, col3, col4 = st.columns(4)
col1.metric('Floating Days/Cattle', f'{cattle_days}/{cattle_floating}[{accounts_floating}]')
col2.metric('Labellers Floating', count_user_floating)
col3.metric('Valid Cattles', f'{count_valid_floating}/{cattle_floating}')
col4.metric('Exceptional Cattles', f'{count_exceptional_floating}/{cattle_floating}')
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 11
def get_floating_grp_data(df):
grp_df = df.loc[df['TAG']=='FLOATING', ['Trial_Num', 'AccountNumber', 'AccountName', 'CattleFolder/Frame', 'TAG', 'Assigned',
'Recording_Date', 'Video_Reception_Date', 'Assignment_Date',
'Target_Date', 'Labelling_Received_Date','Verification_Date', 'Completion/Rejection_Date']].groupby(['Trial_Num',
'AccountNumber',
'AccountName',
'TAG',
'CattleFolder/Frame',
],
as_index=False).agg({'Recording_Date':['min','max'],
'Video_Reception_Date':'max',
'Assignment_Date':'min',
'Target_Date':'max',
'Labelling_Received_Date':'max',
'Verification_Date':'max',
'Completion/Rejection_Date':'max',
'Assigned': 'sum'})
flat_cols = ["_".join(i).rstrip('_') for i in grp_df.columns];# flat_cols
grp_df.columns = flat_cols
grp_df['Recording'] = (grp_df['Recording_Date_max'] - grp_df['Recording_Date_min']).dt.days+1
grp_df['Waiting4Video'] = (grp_df['Video_Reception_Date_max'] - grp_df['Recording_Date_max']).dt.days
grp_df['Waiting4Assignment'] = (grp_df['Assignment_Date_min'] - grp_df['Video_Reception_Date_max']).dt.days
grp_df['Labelling'] = (grp_df['Target_Date_max'] - grp_df['Assignment_Date_min']).dt.days
grp_df['Waiting4Labels'] = (grp_df['Labelling_Received_Date_max'] - grp_df['Target_Date_max']).dt.days
grp_df['Waiting4Verification'] = (grp_df['Verification_Date_max'] - grp_df['Labelling_Received_Date_max']).dt.days
grp_df['Waiting4Completion'] = (grp_df['Completion/Rejection_Date_max'] - grp_df['Verification_Date_max']).dt.days
grp_df['Labelling_Duration'] = grp_df['Labelling'] + grp_df['Waiting4Labels'].fillna(0)
return grp_df
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 13
def get_floating_summary(df):
fig, ax = plt.subplots()
grp_df = get_floating_grp_data(df)
states = ['Recording','Waiting4Video', 'Waiting4Assignment', 'Labelling', 'Waiting4Labels', 'Waiting4Verification','Waiting4Completion']
colors = dict(zip(states, ['blue', 'red', 'green', 'yellow', 'cyan', 'violet', 'pink']))
grp_df[['AccountNumber','Assigned_sum', 'AccountName', 'CattleFolder/Frame','Recording','Waiting4Video', 'Waiting4Assignment', 'Labelling', 'Waiting4Labels', 'Waiting4Verification','Waiting4Completion']].set_index(['AccountNumber', 'AccountName', 'CattleFolder/Frame']).plot(kind='barh', stacked=True, ax=ax, color=colors);
fig.tight_layout()
return fig
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 15
def get_floating_hist(df, col):
grp_df = get_floating_grp_data(df)
fig, ax = plt.subplots()
grp_df[col].plot(kind='hist', ax=ax, legend=True)
avg = grp_df[col].mean()
count = grp_df[col].count()
ax.axvline(avg, color='red', label=f'mean={avg}')
ax.set_title(f'{col}[ mean={avg:.2f}, count={count:.2f} ]')
return fig
# get_floating_hist(df, 'Recording_Duration')
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 17
def get_step_df(grp_df, start_step, end_step, step_name):
df_e = grp_df[['AccountNumber', 'AccountName','Assigned_sum', 'CattleFolder/Frame']].copy()
df_e['Start'] = grp_df[start_step]
df_e['End'] = grp_df[end_step]
df_e['Step'] = step_name
return df_e
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 19
def get_gantt(df):
steps = [
{'start_step': 'Recording_Date_min', 'end_step' :'Recording_Date_max', 'step_name' : 'Recording'},
{'start_step': 'Recording_Date_max', 'end_step' :'Video_Reception_Date_max', 'step_name' : 'Waiting4Video'},
{'start_step': 'Video_Reception_Date_max', 'end_step' :'Assignment_Date_min', 'step_name' : 'Waiting4Assignment'},
{'start_step': 'Assignment_Date_min', 'end_step' :'Target_Date_max', 'step_name' : 'Labelling'},
{'start_step': 'Target_Date_max', 'end_step' :'Labelling_Received_Date_max', 'step_name' : 'Waiting4Labels'},
{'start_step': 'Labelling_Received_Date_max', 'end_step' :'Verification_Date_max', 'step_name' : 'Waiting4Verification'},
{'start_step': 'Verification_Date_max', 'end_step' :'Completion/Rejection_Date_max', 'step_name' : 'Waiting4Completion'},
]
grp_df = get_floating_grp_data(df)
df_concat = pd.concat(get_step_df(grp_df, start_step=step['start_step'], end_step=step['end_step'], step_name=step['step_name']) for step in steps)
df_concat['label'] = df['AccountName'] +"_"+ df['CattleFolder/Frame']
df_concat.loc[df_concat['Step']=='Recording', 'Start'] = df_concat.loc[df_concat['Step']=='Recording', 'Start'] - pd.Timedelta(days=1)
# # df_concat
states = [s['step_name'] for s in steps]; # states
colors = dict(zip(states, ['blue', 'red', 'green', 'yellow', 'cyan', 'violet', 'pink']))
fig = px.timeline(data_frame=df_concat, x_start='Start', x_end='End', y='CattleFolder/Frame', color='Step', color_discrete_map=colors, hover_data=['Assigned_sum', 'AccountName', 'AccountNumber'])
# fig.update_layout(legend=dict(
# orientation="h",
# ))
return fig
# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 23
#|eval: false
df = None
st.write("# Floating Details")
if 'processed_df' not in st.session_state:
st.write("Please go to andon page and upload data")
else:
df = st.session_state['processed_df']
col_order = st.session_state['col_order']
colors = st.session_state['colors']
colors2= st.session_state['colors2']
st.markdown("## Summary Floating Durations")
kpi(df)
with st.container(border=True):
st.pyplot(get_floating_summary(df))
ncols = 3
dcols = st.columns(ncols)
for i, col in enumerate(['Recording','Waiting4Video', 'Waiting4Assignment', 'Labelling_Duration', 'Waiting4Verification', 'Waiting4Completion']):
with dcols[i%ncols]:
st.pyplot(get_floating_hist(df, col), use_container_width=True)
st.markdown("## Timeline")
with st.container(border=True):
st.plotly_chart(get_gantt(df), theme="streamlit", use_container_width=True)