Spaces:
Sleeping
Sleeping
Delete statistics.py
Browse files- statistics.py +0 -265
statistics.py
DELETED
|
@@ -1,265 +0,0 @@
|
|
| 1 |
-
# pages/statistics.py
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import plotly.express as px
|
| 5 |
-
import plotly.graph_objects as go
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
|
| 8 |
-
st.set_page_config(
|
| 9 |
-
page_title="Statistics - Medical AI Librarian",
|
| 10 |
-
page_icon="📊",
|
| 11 |
-
layout="wide"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
st.markdown("# 📊 Statistics Dashboard")
|
| 15 |
-
st.markdown("Analytics for medical cataloging activities")
|
| 16 |
-
|
| 17 |
-
# Initialize session state
|
| 18 |
-
if 'search_history' not in st.session_state:
|
| 19 |
-
st.session_state.search_history = []
|
| 20 |
-
|
| 21 |
-
# Metrics
|
| 22 |
-
col1, col2, col3, col4 = st.columns(4)
|
| 23 |
-
|
| 24 |
-
with col1:
|
| 25 |
-
total_analyses = len(st.session_state.search_history)
|
| 26 |
-
st.metric("Total Analyses", total_analyses)
|
| 27 |
-
|
| 28 |
-
with col2:
|
| 29 |
-
if total_analyses > 0:
|
| 30 |
-
# Calculate success rate
|
| 31 |
-
successful = sum(1 for item in st.session_state.search_history
|
| 32 |
-
if item.get('nlm_class') not in ['W 1', ''])
|
| 33 |
-
success_rate = (successful / total_analyses) * 100
|
| 34 |
-
st.metric("Success Rate", f"{success_rate:.1f}%")
|
| 35 |
-
else:
|
| 36 |
-
st.metric("Success Rate", "0%")
|
| 37 |
-
|
| 38 |
-
with col3:
|
| 39 |
-
if total_analyses > 0:
|
| 40 |
-
# Average confidence
|
| 41 |
-
if st.session_state.get('analysis_data'):
|
| 42 |
-
ai_data = st.session_state.analysis_data.get('ai_analysis', {})
|
| 43 |
-
confidence = ai_data.get('nlm_class_confidence', 'Medium')
|
| 44 |
-
confidence_score = {'High': 100, 'Medium': 66, 'Low': 33}.get(confidence, 50)
|
| 45 |
-
st.metric("Avg Confidence", f"{confidence_score}%")
|
| 46 |
-
else:
|
| 47 |
-
st.metric("Avg Confidence", "N/A")
|
| 48 |
-
|
| 49 |
-
with col4:
|
| 50 |
-
if total_analyses > 0:
|
| 51 |
-
# Unique classifications
|
| 52 |
-
unique_nlm = set(item.get('nlm_class', '') for item in st.session_state.search_history)
|
| 53 |
-
unique_nlm.discard('')
|
| 54 |
-
st.metric("Unique NLM Codes", len(unique_nlm))
|
| 55 |
-
else:
|
| 56 |
-
st.metric("Unique NLM Codes", "0")
|
| 57 |
-
|
| 58 |
-
# Charts Section
|
| 59 |
-
st.markdown("## 📈 Analysis Trends")
|
| 60 |
-
|
| 61 |
-
if total_analyses > 0:
|
| 62 |
-
# Create DataFrame from history
|
| 63 |
-
history_data = []
|
| 64 |
-
for item in st.session_state.search_history:
|
| 65 |
-
history_data.append({
|
| 66 |
-
'Date': datetime.fromisoformat(item['timestamp']).strftime('%Y-%m-%d'),
|
| 67 |
-
'Title': item.get('title', 'Unknown'),
|
| 68 |
-
'ISBN': item.get('isbn', ''),
|
| 69 |
-
'NLM': item.get('nlm_class', 'Unknown')
|
| 70 |
-
})
|
| 71 |
-
|
| 72 |
-
df = pd.DataFrame(history_data)
|
| 73 |
-
|
| 74 |
-
# Group by date for trend analysis
|
| 75 |
-
if not df.empty and 'Date' in df.columns:
|
| 76 |
-
daily_counts = df['Date'].value_counts().sort_index()
|
| 77 |
-
|
| 78 |
-
col1, col2 = st.columns(2)
|
| 79 |
-
|
| 80 |
-
with col1:
|
| 81 |
-
# Daily analyses chart
|
| 82 |
-
fig1 = px.line(
|
| 83 |
-
x=daily_counts.index,
|
| 84 |
-
y=daily_counts.values,
|
| 85 |
-
title='📅 Daily Analyses Trend',
|
| 86 |
-
labels={'x': 'Date', 'y': 'Number of Analyses'},
|
| 87 |
-
markers=True,
|
| 88 |
-
line_shape='spline'
|
| 89 |
-
)
|
| 90 |
-
fig1.update_layout(
|
| 91 |
-
xaxis_title="Date",
|
| 92 |
-
yaxis_title="Analyses",
|
| 93 |
-
hovermode='x unified',
|
| 94 |
-
plot_bgcolor='rgba(240, 240, 240, 0.1)'
|
| 95 |
-
)
|
| 96 |
-
st.plotly_chart(fig1, use_container_width=True)
|
| 97 |
-
|
| 98 |
-
with col2:
|
| 99 |
-
# NLM classification distribution
|
| 100 |
-
if 'NLM' in df.columns:
|
| 101 |
-
nlm_counts = df['NLM'].value_counts().head(10)
|
| 102 |
-
|
| 103 |
-
if not nlm_counts.empty:
|
| 104 |
-
fig2 = px.bar(
|
| 105 |
-
x=nlm_counts.index,
|
| 106 |
-
y=nlm_counts.values,
|
| 107 |
-
title='🏷️ Top 10 NLM Classifications',
|
| 108 |
-
labels={'x': 'NLM Code', 'y': 'Count'},
|
| 109 |
-
color=nlm_counts.values,
|
| 110 |
-
color_continuous_scale='Viridis'
|
| 111 |
-
)
|
| 112 |
-
fig2.update_layout(
|
| 113 |
-
xaxis_title="NLM Classification",
|
| 114 |
-
yaxis_title="Frequency",
|
| 115 |
-
xaxis_tickangle=45,
|
| 116 |
-
plot_bgcolor='rgba(240, 240, 240, 0.1)'
|
| 117 |
-
)
|
| 118 |
-
st.plotly_chart(fig2, use_container_width=True)
|
| 119 |
-
|
| 120 |
-
# Recent Activity Table
|
| 121 |
-
st.markdown("## 📋 Recent Activity")
|
| 122 |
-
|
| 123 |
-
# Prepare enhanced table data
|
| 124 |
-
table_data = []
|
| 125 |
-
for item in reversed(st.session_state.search_history[-10:]):
|
| 126 |
-
table_data.append({
|
| 127 |
-
'Timestamp': datetime.fromisoformat(item['timestamp']).strftime('%Y-%m-%d %H:%M'),
|
| 128 |
-
'ISBN': item.get('isbn', ''),
|
| 129 |
-
'Title': item.get('title', 'Unknown')[:40] + ('...' if len(item.get('title', '')) > 40 else ''),
|
| 130 |
-
'NLM Classification': item.get('nlm_class', 'Not classified')
|
| 131 |
-
})
|
| 132 |
-
|
| 133 |
-
if table_data:
|
| 134 |
-
table_df = pd.DataFrame(table_data)
|
| 135 |
-
st.dataframe(
|
| 136 |
-
table_df,
|
| 137 |
-
use_container_width=True,
|
| 138 |
-
hide_index=True,
|
| 139 |
-
column_config={
|
| 140 |
-
"Timestamp": st.column_config.DatetimeColumn(
|
| 141 |
-
"Timestamp",
|
| 142 |
-
format="YYYY-MM-DD HH:mm"
|
| 143 |
-
),
|
| 144 |
-
"ISBN": "ISBN",
|
| 145 |
-
"Title": "Title",
|
| 146 |
-
"NLM Classification": "NLM Code"
|
| 147 |
-
}
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
# Export option
|
| 151 |
-
csv_data = table_df.to_csv(index=False)
|
| 152 |
-
st.download_button(
|
| 153 |
-
label="📥 Export History as CSV",
|
| 154 |
-
data=csv_data,
|
| 155 |
-
file_name="cataloging_history.csv",
|
| 156 |
-
mime="text/csv",
|
| 157 |
-
use_container_width=True
|
| 158 |
-
)
|
| 159 |
-
else:
|
| 160 |
-
st.info("No recent activity to display.")
|
| 161 |
-
|
| 162 |
-
# NLM Classification Insights
|
| 163 |
-
st.markdown("## 🎯 NLM Classification Insights")
|
| 164 |
-
|
| 165 |
-
col1, col2 = st.columns(2)
|
| 166 |
-
|
| 167 |
-
with col1:
|
| 168 |
-
# Classification confidence gauge
|
| 169 |
-
if st.session_state.get('analysis_data'):
|
| 170 |
-
ai_data = st.session_state.analysis_data.get('ai_analysis', {})
|
| 171 |
-
confidence = ai_data.get('nlm_class_confidence', 'Medium')
|
| 172 |
-
|
| 173 |
-
# Map confidence to value
|
| 174 |
-
confidence_value = {'High': 90, 'Medium': 60, 'Low': 30}.get(confidence, 50)
|
| 175 |
-
|
| 176 |
-
fig3 = go.Figure(go.Indicator(
|
| 177 |
-
mode="gauge+number+delta",
|
| 178 |
-
value=confidence_value,
|
| 179 |
-
title={'text': "Current Classification Confidence"},
|
| 180 |
-
domain={'x': [0, 1], 'y': [0, 1]},
|
| 181 |
-
gauge={
|
| 182 |
-
'axis': {'range': [None, 100]},
|
| 183 |
-
'bar': {'color': "#2563eb"},
|
| 184 |
-
'steps': [
|
| 185 |
-
{'range': [0, 40], 'color': "#ef4444"},
|
| 186 |
-
{'range': [40, 70], 'color': "#f59e0b"},
|
| 187 |
-
{'range': [70, 100], 'color': "#10b981"}
|
| 188 |
-
],
|
| 189 |
-
'threshold': {
|
| 190 |
-
'line': {'color': "red", 'width': 4},
|
| 191 |
-
'thickness': 0.75,
|
| 192 |
-
'value': 80
|
| 193 |
-
}
|
| 194 |
-
}
|
| 195 |
-
))
|
| 196 |
-
|
| 197 |
-
fig3.update_layout(
|
| 198 |
-
height=300,
|
| 199 |
-
font={'color': "darkblue", 'family': "Arial"}
|
| 200 |
-
)
|
| 201 |
-
st.plotly_chart(fig3, use_container_width=True)
|
| 202 |
-
|
| 203 |
-
with col2:
|
| 204 |
-
# Most common classifications
|
| 205 |
-
if 'NLM' in df.columns and not df.empty:
|
| 206 |
-
common_nlm = df['NLM'].value_counts().head(5)
|
| 207 |
-
|
| 208 |
-
if not common_nlm.empty:
|
| 209 |
-
st.markdown("### 🏆 Most Common Classifications")
|
| 210 |
-
|
| 211 |
-
for nlm_code, count in common_nlm.items():
|
| 212 |
-
col_a, col_b = st.columns([1, 3])
|
| 213 |
-
with col_a:
|
| 214 |
-
st.markdown(f"**{nlm_code}**")
|
| 215 |
-
with col_b:
|
| 216 |
-
st.progress(count / total_analyses, text=f"{count} book{'s' if count > 1 else ''}")
|
| 217 |
-
|
| 218 |
-
st.caption(f"Based on {total_analyses} total analyses")
|
| 219 |
-
else:
|
| 220 |
-
st.info("No classification data available.")
|
| 221 |
-
|
| 222 |
-
# Analysis summary
|
| 223 |
-
st.markdown("### 📊 Analysis Summary")
|
| 224 |
-
st.metric("Total Books Cataloged", total_analyses)
|
| 225 |
-
if total_analyses > 0:
|
| 226 |
-
today = datetime.now().strftime('%Y-%m-%d')
|
| 227 |
-
today_analyses = sum(1 for item in st.session_state.search_history
|
| 228 |
-
if datetime.fromisoformat(item['timestamp']).strftime('%Y-%m-%d') == today)
|
| 229 |
-
st.metric("Analyses Today", today_analyses)
|
| 230 |
-
|
| 231 |
-
else:
|
| 232 |
-
st.info("""
|
| 233 |
-
📊 **No analysis history yet!**
|
| 234 |
-
|
| 235 |
-
Start by analyzing some medical books:
|
| 236 |
-
1. Go to the main page
|
| 237 |
-
2. Enter an ISBN number
|
| 238 |
-
3. Click "Analyze Book"
|
| 239 |
-
4. Your analyses will appear here
|
| 240 |
-
""")
|
| 241 |
-
|
| 242 |
-
# Performance Metrics
|
| 243 |
-
if total_analyses >= 3:
|
| 244 |
-
st.markdown("## ⚡ Performance Metrics")
|
| 245 |
-
|
| 246 |
-
col1, col2, col3 = st.columns(3)
|
| 247 |
-
|
| 248 |
-
with col1:
|
| 249 |
-
st.metric("Cover Success Rate", "95%+", "+5%")
|
| 250 |
-
|
| 251 |
-
with col2:
|
| 252 |
-
st.metric("Date Accuracy", "85%+", "+10%")
|
| 253 |
-
|
| 254 |
-
with col3:
|
| 255 |
-
st.metric("NLM Accuracy", "90%+", "+15%")
|
| 256 |
-
|
| 257 |
-
# Footer
|
| 258 |
-
st.markdown("---")
|
| 259 |
-
st.markdown("""
|
| 260 |
-
<div style='text-align: center; color: #666; font-size: 0.9rem; padding: 20px;'>
|
| 261 |
-
<p>📈 <strong>Medical AI Librarian Statistics Dashboard</strong> - Enhanced Edition</p>
|
| 262 |
-
<p>Data is stored locally in your session • Last updated: {}</p>
|
| 263 |
-
<p style='margin-top: 20px; font-style: italic;'>🏥 Transforming Medical Library Management with AI</p>
|
| 264 |
-
</div>
|
| 265 |
-
""".format(datetime.now().strftime('%Y-%m-%d %H:%M')), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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