# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/book/LabellingTracker/14_Model.ipynb. # %% auto 0 __all__ = ['df', 'get_model_grp_data', 'get_model_summary', 'get_model_hist', 'get_step_df', 'get_gantt'] # %% ../../nbs/book/LabellingTracker/14_Model.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/14_Model.ipynb 6 st.set_page_config( page_title="Model", page_icon="👋", layout='wide' ) # %% ../../nbs/book/LabellingTracker/14_Model.ipynb 8 # st.sidebar.success("Select a demo above.") # %% ../../nbs/book/LabellingTracker/14_Model.ipynb 11 def get_model_grp_data(df): grp_df = df.loc[df['TAG']=='MODEL', ['Trial_Num', 'AccountNumber', 'AccountName', 'CattleFolder/Frame','SubFolder', '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', 'SubFolder' ], 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/14_Model.ipynb 14 def get_model_summary(df, kind='sum'): fig, ax = plt.subplots() grp_df = get_model_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','AccountName', 'CattleFolder/Frame', 'Recording','Waiting4Video', 'Waiting4Assignment', 'Labelling', 'Waiting4Labels', 'Waiting4Verification','Waiting4Completion']].groupby(['AccountNumber', 'AccountName', 'CattleFolder/Frame'], as_index=False).agg(kind).set_index(['AccountNumber', 'AccountName', 'CattleFolder/Frame']).plot(kind='barh', stacked=True, ax=ax, color=colors, legend=False); fig.legend(loc='upper right') return fig # %% ../../nbs/book/LabellingTracker/14_Model.ipynb 16 def get_model_hist(df, col): grp_df = get_model_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/14_Model.ipynb 17 def get_step_df(grp_df, start_step, end_step, step_name): df_e = grp_df[['AccountNumber', 'AccountName','Assigned_sum', 'CattleFolder/Frame', 'SubFolder']].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/14_Model.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_model_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_concat[['AccountNumber', 'AccountName', 'CattleFolder/Frame']].apply(lambda row: "_".join(row), axis=1) 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='label', color='Step', color_discrete_map=colors, hover_data=['Assigned_sum', 'AccountName', 'AccountNumber']) # fig.update_layout(legend=dict( # orientation="h", # )) return fig # %% ../../nbs/book/LabellingTracker/14_Model.ipynb 23 df = None st.write("# Model 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'] st.markdown("## Summary Model Durations") st.markdown(" Below plot includes a summary of all the frames in different model farms. Process includes recording and sharing video everyday with non sequential sharing schedule") with st.container(border=True): tab_sum, tab_mean = st.tabs(['Sum', 'Mean']) with tab_sum: st.pyplot(get_model_summary(df)) with tab_mean: st.pyplot(get_model_summary(df, kind='mean')) 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_model_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)