ProfessorLeVesseur commited on
Commit
ffd39e7
1 Parent(s): 823b52c

Create visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +78 -0
visualization.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import io
3
+ import streamlit as st
4
+
5
+ class Visualization:
6
+ def plot_intervention_statistics(self, intervention_stats):
7
+ sessions_held = intervention_stats['Intervention Sessions Held'].values[0]
8
+ sessions_not_held = intervention_stats['Intervention Sessions Not Held'].values[0]
9
+
10
+ fig, ax = plt.subplots()
11
+ ax.bar(['Intervention Sessions'], [sessions_held], label='Held', color='#358E66')
12
+ ax.bar(['Intervention Sessions'], [sessions_not_held], bottom=[sessions_held], label='Not Held', color='#91D6B8')
13
+
14
+ ax.text(0, sessions_held / 2, str(sessions_held), ha='center', va='center', color='white', fontweight='bold', fontsize=14)
15
+ ax.text(0, sessions_held + sessions_not_held / 2, str(sessions_not_held), ha='center', va='center', color='black', fontweight='bold', fontsize=14)
16
+
17
+ ax.set_ylabel('Frequency')
18
+ ax.set_title('Intervention Sessions Held vs Not Held', fontsize=16)
19
+ handles, labels = ax.get_legend_handles_labels()
20
+ ax.legend(handles[::-1], labels[::-1])
21
+
22
+ ax.spines['top'].set_visible(False)
23
+ ax.spines['right'].set_visible(False)
24
+
25
+ st.pyplot(fig)
26
+ return fig
27
+
28
+ def plot_student_metrics(self, student_metrics_df, attendance_avg_stats, engagement_avg_stats):
29
+ fig, ax = plt.subplots(figsize=(10, 6))
30
+ bar_width = 0.35
31
+ index = range(len(student_metrics_df))
32
+
33
+ attendance_bars = ax.bar(
34
+ [i - bar_width / 2 for i in index],
35
+ student_metrics_df['Attendance (%)'],
36
+ width=bar_width, label='Attendance (%)',
37
+ color='#005288', alpha=0.7
38
+ )
39
+ engagement_bars = ax.bar(
40
+ [i + bar_width / 2 for i in index],
41
+ student_metrics_df['Engagement (%)'],
42
+ width=bar_width, label='Engagement (%)',
43
+ color='#3AB0FF', alpha=0.7
44
+ )
45
+
46
+ for bar in attendance_bars:
47
+ height = bar.get_height()
48
+ ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.0f}%', ha='center', va='bottom', color='black')
49
+
50
+ for bar in engagement_bars:
51
+ height = bar.get_height()
52
+ ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.0f}%', ha='center', va='bottom', color='black')
53
+
54
+ ax.axhline(y=attendance_avg_stats, color='#005288', linestyle='--', linewidth=1.5, label=f'Attendance Average: {attendance_avg_stats}%')
55
+ ax.axhline(y=engagement_avg_stats, color='#3AB0FF', linestyle='--', linewidth=1.5, label=f'Engagement Average: {engagement_avg_stats}%')
56
+
57
+ ax.set_xlabel('Student')
58
+ ax.set_ylabel('Percentage (%)')
59
+ ax.set_title('Student Attendance and Engagement Metrics')
60
+ ax.legend(loc='upper right', frameon=False)
61
+ ax.set_xticks(index)
62
+ ax.set_xticklabels(student_metrics_df['Student'], rotation=0, ha='right')
63
+ ax.set_ylim(0, 119)
64
+ ax.yaxis.set_ticks(range(0, 119, 20))
65
+
66
+ ax.spines['top'].set_visible(False)
67
+ ax.spines['right'].set_visible(False)
68
+
69
+ plt.tight_layout()
70
+ st.pyplot(fig)
71
+ return fig
72
+
73
+ def download_chart(self, fig, filename):
74
+ buffer = io.BytesIO()
75
+ fig.savefig(buffer, format='png')
76
+ buffer.seek(0)
77
+ st.download_button(label="Download Chart", data=buffer, file_name=filename, mime='image/png', icon="📊", use_container_width=True)
78
+