Update app.py
Browse files
app.py
CHANGED
@@ -130,14 +130,24 @@ ENGAGED_STR = 'Engaged (Respect, Responsibility, Effort)'
|
|
130 |
PARTIALLY_ENGAGED_STR = 'Partially Engaged (about 50%)'
|
131 |
NOT_ENGAGED_STR = 'Not Engaged (less than 50%)'
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
def safe_convert_to_time(series, format_str='%I:%M %p'):
|
134 |
try:
|
135 |
-
|
136 |
-
converted = pd.to_datetime(series, format='%H:%M:%S', errors='coerce').dt.time
|
137 |
-
|
138 |
if format_str:
|
139 |
-
|
140 |
-
return pd.Series([time.strftime(format_str) if time is not None else None for time in converted])
|
141 |
return converted
|
142 |
except Exception as e:
|
143 |
print(f"Error converting series to time: {e}")
|
@@ -346,7 +356,7 @@ def compute_student_metrics(df):
|
|
346 |
|
347 |
# Attendance (%)
|
348 |
attendance_pct = (sessions_attended / intervention_sessions_held) * 100 if intervention_sessions_held > 0 else 0
|
349 |
-
attendance_pct = round(attendance_pct
|
350 |
|
351 |
# For engagement calculation, include only sessions where attendance is not 'Absent'
|
352 |
valid_engagement_indices = attendance_values[attendance_values == 1].index
|
@@ -364,12 +374,18 @@ def compute_student_metrics(df):
|
|
364 |
|
365 |
# Engagement (%)
|
366 |
engagement_pct = (sum_engagement_values / number_sessions_attended) * 100 if number_sessions_attended > 0 else 0
|
367 |
-
engagement_pct = round(engagement_pct
|
|
|
|
|
|
|
|
|
|
|
|
|
368 |
|
369 |
-
# Store metrics
|
370 |
student_metrics[student_name] = {
|
371 |
-
'Attendance (%)': attendance_pct,
|
372 |
-
'Engagement (%)': engagement_pct
|
373 |
}
|
374 |
|
375 |
# Create a DataFrame from student_metrics
|
@@ -377,9 +393,45 @@ def compute_student_metrics(df):
|
|
377 |
student_metrics_df.rename(columns={'index': 'Student'}, inplace=True)
|
378 |
return student_metrics_df
|
379 |
|
380 |
-
def plot_student_metrics(student_metrics_df):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
381 |
# Create the figure and axis
|
382 |
-
fig, ax = plt.subplots()
|
383 |
|
384 |
# Width for the bars and offset for overlapping effect
|
385 |
bar_width = 0.4
|
@@ -391,16 +443,17 @@ def plot_student_metrics(student_metrics_df):
|
|
391 |
engagement_bars = ax.bar([i + bar_width/2 for i in index], student_metrics_df['Engagement (%)'],
|
392 |
width=bar_width, label='Engagement (%)', color='#3AB0FF', alpha=0.7)
|
393 |
|
394 |
-
# Add labels to each bar
|
|
|
395 |
for bar in attendance_bars:
|
396 |
height = bar.get_height()
|
397 |
ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
|
398 |
-
ha='center', va='bottom', color='
|
399 |
|
400 |
for bar in engagement_bars:
|
401 |
height = bar.get_height()
|
402 |
ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
|
403 |
-
ha='center', va='bottom', color='black')
|
404 |
|
405 |
# Set labels, title, and legend
|
406 |
ax.set_xlabel('Student')
|
@@ -408,12 +461,16 @@ def plot_student_metrics(student_metrics_df):
|
|
408 |
ax.set_title('Student Attendance and Engagement Metrics')
|
409 |
ax.legend()
|
410 |
ax.set_xticks(index)
|
411 |
-
ax.set_xticklabels(student_metrics_df['Student'], rotation=45)
|
|
|
|
|
|
|
412 |
|
413 |
# Display the plot
|
414 |
st.pyplot(fig)
|
415 |
|
416 |
return fig
|
|
|
417 |
|
418 |
# def plot_student_metrics(student_metrics_df):
|
419 |
# # Create the figure and axis
|
|
|
130 |
PARTIALLY_ENGAGED_STR = 'Partially Engaged (about 50%)'
|
131 |
NOT_ENGAGED_STR = 'Not Engaged (less than 50%)'
|
132 |
|
133 |
+
# def safe_convert_to_time(series, format_str='%I:%M %p'):
|
134 |
+
# try:
|
135 |
+
# # Attempt to parse the time in 24-hour format
|
136 |
+
# converted = pd.to_datetime(series, format='%H:%M:%S', errors='coerce').dt.time
|
137 |
+
|
138 |
+
# if format_str:
|
139 |
+
# # Format the time to the specified format (hours and minutes only, with AM/PM)
|
140 |
+
# return pd.Series([time.strftime(format_str) if time is not None else None for time in converted])
|
141 |
+
# return converted
|
142 |
+
# except Exception as e:
|
143 |
+
# print(f"Error converting series to time: {e}")
|
144 |
+
# return series
|
145 |
+
|
146 |
def safe_convert_to_time(series, format_str='%I:%M %p'):
|
147 |
try:
|
148 |
+
converted = pd.to_datetime(series, format='%H:%M:%S', errors='coerce')
|
|
|
|
|
149 |
if format_str:
|
150 |
+
return converted.dt.strftime(format_str)
|
|
|
151 |
return converted
|
152 |
except Exception as e:
|
153 |
print(f"Error converting series to time: {e}")
|
|
|
356 |
|
357 |
# Attendance (%)
|
358 |
attendance_pct = (sessions_attended / intervention_sessions_held) * 100 if intervention_sessions_held > 0 else 0
|
359 |
+
attendance_pct = round(attendance_pct) # Round to whole number
|
360 |
|
361 |
# For engagement calculation, include only sessions where attendance is not 'Absent'
|
362 |
valid_engagement_indices = attendance_values[attendance_values == 1].index
|
|
|
374 |
|
375 |
# Engagement (%)
|
376 |
engagement_pct = (sum_engagement_values / number_sessions_attended) * 100 if number_sessions_attended > 0 else 0
|
377 |
+
engagement_pct = round(engagement_pct) # Round to whole number
|
378 |
+
|
379 |
+
# # Store metrics
|
380 |
+
# student_metrics[student_name] = {
|
381 |
+
# 'Attendance (%)': attendance_pct,
|
382 |
+
# 'Engagement (%)': engagement_pct
|
383 |
+
# }
|
384 |
|
385 |
+
# Store metrics with percentage sign
|
386 |
student_metrics[student_name] = {
|
387 |
+
'Attendance (%)': f"{attendance_pct}%", # Add percentage sign
|
388 |
+
'Engagement (%)': f"{engagement_pct}%" # Add percentage sign
|
389 |
}
|
390 |
|
391 |
# Create a DataFrame from student_metrics
|
|
|
393 |
student_metrics_df.rename(columns={'index': 'Student'}, inplace=True)
|
394 |
return student_metrics_df
|
395 |
|
396 |
+
# def plot_student_metrics(student_metrics_df):
|
397 |
+
# # Create the figure and axis
|
398 |
+
# fig, ax = plt.subplots()
|
399 |
+
|
400 |
+
# # Width for the bars and offset for overlapping effect
|
401 |
+
# bar_width = 0.4
|
402 |
+
# index = range(len(student_metrics_df))
|
403 |
+
|
404 |
+
# # Plot Attendance and Engagement bars side by side with transparency
|
405 |
+
# attendance_bars = ax.bar([i - bar_width/2 for i in index], student_metrics_df['Attendance (%)'],
|
406 |
+
# width=bar_width, label='Attendance (%)', color='#005288', alpha=0.7)
|
407 |
+
# engagement_bars = ax.bar([i + bar_width/2 for i in index], student_metrics_df['Engagement (%)'],
|
408 |
+
# width=bar_width, label='Engagement (%)', color='#3AB0FF', alpha=0.7)
|
409 |
+
|
410 |
+
# # Add labels to each bar
|
411 |
+
# for bar in attendance_bars:
|
412 |
+
# height = bar.get_height()
|
413 |
+
# ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
|
414 |
+
# ha='center', va='bottom', color='black')
|
415 |
+
|
416 |
+
# for bar in engagement_bars:
|
417 |
+
# height = bar.get_height()
|
418 |
+
# ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
|
419 |
+
# ha='center', va='bottom', color='black')
|
420 |
+
|
421 |
+
# # Set labels, title, and legend
|
422 |
+
# ax.set_xlabel('Student')
|
423 |
+
# ax.set_ylabel('Percentage (%)')
|
424 |
+
# ax.set_title('Student Attendance and Engagement Metrics')
|
425 |
+
# ax.legend()
|
426 |
+
# ax.set_xticks(index)
|
427 |
+
# ax.set_xticklabels(student_metrics_df['Student'], rotation=45)
|
428 |
+
|
429 |
+
# # Display the plot
|
430 |
+
# st.pyplot(fig)
|
431 |
+
|
432 |
+
def plot_student_metrics(student_metrics_df):
|
433 |
# Create the figure and axis
|
434 |
+
fig, ax = plt.subplots(figsize=(10, 6)) # Increased figure size for better readability
|
435 |
|
436 |
# Width for the bars and offset for overlapping effect
|
437 |
bar_width = 0.4
|
|
|
443 |
engagement_bars = ax.bar([i + bar_width/2 for i in index], student_metrics_df['Engagement (%)'],
|
444 |
width=bar_width, label='Engagement (%)', color='#3AB0FF', alpha=0.7)
|
445 |
|
446 |
+
# Add labels to each bar with customized font size and color
|
447 |
+
font_size = 8 # Reduced font size for better fit
|
448 |
for bar in attendance_bars:
|
449 |
height = bar.get_height()
|
450 |
ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
|
451 |
+
ha='center', va='bottom', color='white', fontsize=font_size)
|
452 |
|
453 |
for bar in engagement_bars:
|
454 |
height = bar.get_height()
|
455 |
ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
|
456 |
+
ha='center', va='bottom', color='black', fontsize=font_size)
|
457 |
|
458 |
# Set labels, title, and legend
|
459 |
ax.set_xlabel('Student')
|
|
|
461 |
ax.set_title('Student Attendance and Engagement Metrics')
|
462 |
ax.legend()
|
463 |
ax.set_xticks(index)
|
464 |
+
ax.set_xticklabels(student_metrics_df['Student'], rotation=45, ha='right') # Improved xticklabel alignment
|
465 |
+
|
466 |
+
# Tight layout to ensure all elements fit within the figure
|
467 |
+
fig.tight_layout()
|
468 |
|
469 |
# Display the plot
|
470 |
st.pyplot(fig)
|
471 |
|
472 |
return fig
|
473 |
+
return fig
|
474 |
|
475 |
# def plot_student_metrics(student_metrics_df):
|
476 |
# # Create the figure and axis
|