File size: 8,659 Bytes
796891d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from comparison import ModelEvaluator, ModelComparison
import matplotlib.pyplot as plt
import seaborn as sns
import io
import os
import base64

st.set_page_config(
    page_title="Nexar Dashcam Leaderboard",
    page_icon="nexar_logo.png",
    layout="wide"
)

st.markdown("""
    <style>
    .main { padding: 2rem; }
    .stTabs [data-baseweb="tab-list"] { gap: 8px; }
    .stTabs [data-baseweb="tab"] {
        padding: 8px 16px;
        border-radius: 4px;
    }
    .metric-card {
        background-color: #f8f9fa;
        padding: 20px;
        border-radius: 10px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    </style>
""", unsafe_allow_html=True)

col1, col2 = st.columns([0.15, 0.85])
with col1:
    st.image("nexar_logo.png", width=600)
with col2:
    st.title("Nexar Dashcam Leaderboard")

@st.cache_data
def load_data(directory='results', labels_filename='Labels.csv'):
    labels_path = os.path.join(directory, labels_filename)
    df_labels = pd.read_csv(labels_path)

    evaluators = []

    for filename in os.listdir(directory):
        if filename.endswith('.csv') and filename != labels_filename:
            model_name = os.path.splitext(filename)[0]
            df_model = pd.read_csv(os.path.join(directory, filename))
            evaluator = ModelEvaluator(df_labels, df_model, model_name)
            evaluators.append(evaluator)

    model_comparison = ModelComparison(evaluators)

    return model_comparison

if 'model_comparison' not in st.session_state:
    st.session_state.model_comparison = load_data()
    st.session_state.leaderboard_df = st.session_state.model_comparison.transform_to_leaderboard()
    st.session_state.combined_df = st.session_state.model_comparison.combined_df

tab1, tab2, tab3, tab4 = st.tabs([
    "πŸ“ˆ Leaderboard", 
    "🎯 Category Analysis",
    "πŸ“Š Class Performance",
    "πŸ” Detailed Metrics"
])

def style_dataframe(df):
    numeric_cols = df.select_dtypes(include=['float64']).columns
    
    def background_gradient(s):
        normalized = (s - s.min()) / (s.max() - s.min())
        normalized = normalized.fillna(0)  # Handle NaN values
        return ['background: linear-gradient(90deg, rgba(52, 152, 219, 0.2) {}%, transparent {}%)'.format(
            int(val * 100), int(val * 100)) for val in normalized]
    
    def highlight_max(s):
        is_max = s == s.max()
        return ['font-weight: bold; color: #2ecc71' if v else '' for v in is_max]
    
    styled = df.style\
        .format({col: '{:.2f}%' for col in numeric_cols})\
        .apply(background_gradient, subset=numeric_cols)\
        .apply(highlight_max, subset=numeric_cols)\
        .set_properties(**{
            'background-color': '#f8f9fa',
            'padding': '10px',
            'border': '1px solid #dee2e6',
            'text-align': 'center'
        })\
        .set_table_styles([
            {'selector': 'th', 'props': [
                ('background-color', '#4a90e2'),
                ('color', 'white'),
                ('font-weight', 'bold'),
                ('padding', '10px'),
                ('text-align', 'center')
            ]},
            {'selector': 'tr:hover', 'props': [
                ('background-color', '#edf2f7')
            ]}
        ])
    
    return styled

with tab1:
    st.subheader("Model Performance Leaderboard")
    
    sort_col = st.selectbox(
        "Sort by metric:",
        options=[col for col in st.session_state.leaderboard_df.columns if col not in ['Rank', 'Model']],
        key='leaderboard_sort'
    )
    
    sorted_df = st.session_state.leaderboard_df.sort_values(by=sort_col, ascending=False)
    st.dataframe(
        style_dataframe(sorted_df),
        use_container_width=True,
        height=400
    )
    
    # Category performance bar plot
    metrics = ['F1 Score', 'Precision', 'Recall']
    selected_metric = st.selectbox("Select Metric for Category Analysis:", metrics)
    
    category_data = st.session_state.combined_df[
        st.session_state.combined_df['Class'].str.contains('Overall')
    ]
    
    fig = px.bar(
        category_data,
        x='Category',
        y=selected_metric,
        color='Model',
        barmode='group',
        title=f'Category-level {selected_metric} by Model',
    )
    
    fig.update_layout(
        xaxis_title="Category",
        yaxis_title=selected_metric,
        legend_title="Model"
    )
    
    st.plotly_chart(fig, use_container_width=True)

with tab2:
    st.subheader("Category-level Analysis")
    
    categories = st.session_state.combined_df['Category'].unique()
    selected_category = st.selectbox("Select Category:", categories)
    
    col1, col2 = st.columns(2)
    
    with col1:
        category_data = st.session_state.combined_df[
            st.session_state.combined_df['Class'].str.contains('Overall')
        ]
        
        fig = px.bar(
            category_data,
            x='Category',
            y=selected_metric,
            color='Model',
            barmode='group',
            title=f'{selected_metric} by Category'
        )
        st.plotly_chart(fig, use_container_width=True)
    
    with col2:
        cat_data = st.session_state.combined_df[
            (st.session_state.combined_df['Category'] == selected_category) &
            (~st.session_state.combined_df['Class'].str.contains('Overall'))
        ]
        
        fig = go.Figure()
        
        for model in cat_data['Model'].unique():
            model_data = cat_data[cat_data['Model'] == model]
            fig.add_trace(go.Scatterpolar(
                r=model_data[selected_metric],
                theta=model_data['Class'],
                name=model,
                fill='toself'
            ))
        
        fig.update_layout(
            polar=dict(
                radialaxis=dict(
                    visible=True,
                    range=[0, 1]
                )
            ),
            showlegend=True,
            title=f'{selected_metric} Distribution for {selected_category}'
        )
        st.plotly_chart(fig, use_container_width=True)

with tab3:
    st.subheader("Class-level Performance")
    
    col1, col2, col3 = st.columns(3)
    with col1:
        selected_category = st.selectbox(
            "Select Category:",
            categories,
            key='class_category'
        )
    with col2:
        selected_metric = st.selectbox(
            "Select Metric:",
            metrics,
            key='class_metric'
        )
    with col3:
        selected_models = st.multiselect(
            "Select Models:",
            st.session_state.combined_df['Model'].unique(),
            default=st.session_state.combined_df['Model'].unique()
        )
    
    class_data = st.session_state.combined_df[
        (st.session_state.combined_df['Category'] == selected_category) &
        (~st.session_state.combined_df['Class'].str.contains('Overall')) &
        (st.session_state.combined_df['Model'].isin(selected_models))
    ]
    
    fig = px.bar(
        class_data,
        x='Class',
        y=selected_metric,
        color='Model',
        barmode='group',
        title=f'{selected_metric} by Class for {selected_category}'
    )
    st.plotly_chart(fig, use_container_width=True)
    
    fig = px.scatter(
        class_data,
        x='Precision',
        y='Recall',
        color='Model',
        size='Support',
        hover_data=['Class'],
        title=f'Precision vs Recall for {selected_category}'
    )
    fig.update_traces(marker=dict(sizeref=2.*max(class_data['Support'])/40.**2))
    st.plotly_chart(fig, use_container_width=True)

with tab4:
    st.subheader("Detailed Metrics Analysis")
    
    selected_model = st.selectbox(
        "Select Model for Detailed Analysis:",
        st.session_state.combined_df['Model'].unique()
    )
    
    model_data = st.session_state.combined_df[
        st.session_state.combined_df['Model'] == selected_model
    ]
    
    st.markdown("### Detailed Metrics Table")
    detailed_metrics = model_data.pivot_table(
        index='Category',
        columns='Class',
        values=['F1 Score', 'Precision', 'Recall']
    ).round(4)
    
    st.dataframe(style_dataframe(detailed_metrics), use_container_width=True)
    
    csv = detailed_metrics.to_csv().encode()
    st.download_button(
        "Download Detailed Metrics",
        csv,
        f"detailed_metrics_{selected_model}.csv",
        "text/csv",
        key='download-csv'
    )

st.markdown("---")
st.markdown("Dashboard created for model evaluation and comparison.")