-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharts.py
More file actions
282 lines (227 loc) · 9.55 KB
/
charts.py
File metadata and controls
282 lines (227 loc) · 9.55 KB
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
#!/usr/bin/env python3
"""
Demographic Charts Generator for Attendance System
Creates visualizations of member demographics from VexU attendance data.
"""
import matplotlib.pyplot as plt
from collections import Counter
from datetime import date
from typing import Dict, List, Optional
from sheets_client import GoogleSheetsClient
from data_models import MembershipRecord
from new_format import NewFormatParser
from old_format import OldFormatParser
def get_active_members(
system: NewFormatParser,
activity: Optional[str] = None,
start_date: Optional[date] = None,
end_date: Optional[date] = None
) -> Dict[str, MembershipRecord]:
"""
Get active members matching criteria with their full membership info.
Args:
system: Initialized NewFormatParser with loaded data
activity: Filter by activity name (e.g., "VexU"). None = all activities
start_date: Only include attendance on or after this date
end_date: Only include attendance on or before this date
Returns:
Dictionary mapping EID -> MembershipRecord
"""
matching_eids = set()
for attendance_date, eid, att_activity, _ in system.unique_attendance:
if activity is not None and att_activity.lower() != activity.lower():
continue
if start_date is not None and attendance_date < start_date:
continue
if end_date is not None and attendance_date > end_date:
continue
matching_eids.add(eid)
members_map = {}
for eid in matching_eids:
member = system.get_member_by_eid(eid)
if member:
members_map[eid] = member
return members_map
def create_pie_chart(ax, data: Counter, title: str, colors=None):
"""Create a pie chart on the given axes."""
if not data:
ax.text(0.5, 0.5, 'No Data', ha='center', va='center', fontsize=14)
ax.set_title(title, fontsize=14, fontweight='bold')
return
labels = list(data.keys())
sizes = list(data.values())
# Clean up empty labels
cleaned = [(l if l.strip() else 'Not Specified', s) for l, s in zip(labels, sizes)]
labels, sizes = zip(*cleaned)
wedges, texts, autotexts = ax.pie(
sizes,
labels=labels,
autopct=lambda pct: f'{pct:.1f}%\n({int(pct/100*sum(sizes))})',
startangle=90,
colors=colors
)
ax.set_title(title, fontsize=14, fontweight='bold')
def create_bar_chart(ax, data: Counter, title: str, color='steelblue', max_items: int = 15):
"""Create a horizontal bar chart on the given axes."""
if not data:
ax.text(0.5, 0.5, 'No Data', ha='center', va='center', fontsize=14)
ax.set_title(title, fontsize=14, fontweight='bold')
return
# Get top items
items = data.most_common(max_items)
labels = [l if l.strip() else 'Not Specified' for l, _ in items]
values = [v for _, v in items]
# Reverse for horizontal bar chart (top item at top)
labels = labels[::-1]
values = values[::-1]
bars = ax.barh(labels, values, color=color)
ax.set_xlabel('Count')
ax.set_title(title, fontsize=14, fontweight='bold')
# Add value labels on bars
for bar, val in zip(bars, values):
ax.text(val + 0.1, bar.get_y() + bar.get_height()/2, str(val),
va='center', fontsize=10)
def generate_demographic_charts(members: Dict[str, MembershipRecord], title_prefix: str = ""):
"""
Generate demographic charts for the given members.
Args:
members: Dictionary mapping EID -> MembershipRecord
title_prefix: Optional prefix for chart titles
"""
if not members:
print("No members to chart!")
return
member_list = list(members.values())
# Collect data
gender_counts = Counter(m.gender for m in member_list)
major_counts = Counter(m.major for m in member_list)
grad_semester_counts = Counter(m.grad_semester for m in member_list)
race_counts = Counter(m.race for m in member_list)
hispanic_counts = Counter(m.hispanic for m in member_list)
discord_counts = Counter(m.joined_discord for m in member_list)
# Parse committees (comma-separated)
committee_counts = Counter()
for m in member_list:
if m.committees:
for committee in m.committees.split(','):
committee = committee.strip()
if committee:
committee_counts[committee] += 1
# Create figure with subplots
fig = plt.figure(figsize=(18, 14))
fig.suptitle(f'{title_prefix}Member Demographics (n={len(members)})',
fontsize=18, fontweight='bold', y=0.98)
# Color palettes
gender_colors = ['#FF6B6B', '#4ECDC4', '#95E1D3', '#F7DC6F', '#BB8FCE']
discord_colors = ['#7289DA', '#99AAB5'] # Discord brand colors
# Gender distribution (pie chart)
ax1 = fig.add_subplot(2, 3, 1)
create_pie_chart(ax1, gender_counts, 'Gender Distribution', colors=gender_colors)
# Graduation semester (bar chart)
ax2 = fig.add_subplot(2, 3, 2)
create_bar_chart(ax2, grad_semester_counts, 'Graduation Semester', color='#3498DB')
# Discord status (pie chart)
ax3 = fig.add_subplot(2, 3, 3)
create_pie_chart(ax3, discord_counts, 'Joined Discord?', colors=discord_colors)
# Major distribution (bar chart)
ax4 = fig.add_subplot(2, 3, 4)
create_bar_chart(ax4, major_counts, 'Major Distribution', color='#2ECC71', max_items=12)
# Race/Ethnicity (bar chart)
ax5 = fig.add_subplot(2, 3, 5)
create_bar_chart(ax5, race_counts, 'Race/Ethnicity', color='#9B59B6')
# Committee membership (bar chart)
ax6 = fig.add_subplot(2, 3, 6)
create_bar_chart(ax6, committee_counts, 'Committee Membership', color='#E74C3C')
plt.tight_layout(rect=[0, 0, 1, 0.96])
# Create filename from title prefix
filename = title_prefix.strip().replace(' ', '_').replace('-', '').lower() + '_demographics.png'
plt.savefig(filename, dpi=150, bbox_inches='tight')
print(f"✓ Saved {filename}")
plt.close()
# Print summary statistics
print("\n" + "=" * 60)
print("DEMOGRAPHIC SUMMARY")
print("=" * 60)
print(f"\nTotal Members: {len(members)}")
print("\nGender Breakdown:")
for gender, count in gender_counts.most_common():
pct = count / len(members) * 100
print(f" {gender or 'Not Specified'}: {count} ({pct:.1f}%)")
print("\nTop 5 Majors:")
for major, count in major_counts.most_common(5):
pct = count / len(members) * 100
print(f" {major or 'Not Specified'}: {count} ({pct:.1f}%)")
print("\nCommittee Participation:")
for committee, count in committee_counts.most_common():
pct = count / len(members) * 100
print(f" {committee}: {count} ({pct:.1f}%)")
def main():
"""Main function to generate demographic charts."""
# Initialize shared client
client = GoogleSheetsClient()
print("Authenticating with Google Sheets...")
client.authenticate()
print("✓ Authentication successful\n")
# Initialize system with shared client
system = NewFormatParser(client)
# Also load old format data
old_parser = OldFormatParser(client)
# Load new format data
print("=" * 60)
print("Loading NEW FORMAT data")
print("=" * 60)
print("Loading membership data...")
system.get_membership_records()
print(f"✓ Loaded {len(system.members_by_eid)} members from new format\n")
print("Loading attendance data...")
system.get_attendance_records()
print(f"✓ Loaded {len(system.unique_attendance)} unique attendance entries from new format\n")
# Load old format data
print("=" * 60)
print("Loading OLD FORMAT data")
print("=" * 60)
old_memberships, old_attendances = old_parser.load_data()
# Merge old format into system's data structures
print("\nMerging old format data into system...")
for eid, member in old_parser.members_by_eid.items():
if eid not in system.members_by_eid:
system.members_by_eid[eid] = member
for attendance_tuple in old_parser.unique_attendance:
system.unique_attendance.add(attendance_tuple)
print(f"✓ Combined members: {len(system.members_by_eid)}")
print(f"✓ Combined unique attendance: {len(system.unique_attendance)}\n")
# Define time windows
time_windows = {
"All Time": {
"start": None,
"end": None,
"title_prefix": "VexU - All Time"
},
"Nov 2025 - Present": {
"start": date(2025, 11, 1),
"end": date(2026, 1, 31),
"title_prefix": "VexU - Nov 2025 to Present"
},
"Spring 2026 (Current Semester)": {
"start": date(2026, 1, 1),
"end": date(2026, 5, 31),
"title_prefix": "VexU - Spring 2026"
}
}
# Generate charts for each time window
for window_name, window_config in time_windows.items():
print(f"\n{'='*60}")
print(f"Fetching VexU members: {window_name}")
print(f"{'='*60}")
vexu_members = get_active_members(
system,
activity="VexU",
start_date=window_config["start"],
end_date=window_config["end"]
)
print(f"✓ Found {len(vexu_members)} VexU members\n")
# Generate charts
print(f"Generating demographic charts for {window_name}...")
generate_demographic_charts(vexu_members, title_prefix=window_config["title_prefix"] + " ")
if __name__ == "__main__":
main()