File size: 8,422 Bytes
7b444b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/book/LabellingTracker/13_Floating.ipynb.

# %% auto 0
__all__ = ['df', '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 8
# st.sidebar.success("Select a demo above.")

# %% ../../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);
    return fig

# %% ../../nbs/book/LabellingTracker/13_Floating.ipynb 14
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 15
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 17
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 21
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")
    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)