-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
402 lines (331 loc) · 16.5 KB
/
main.py
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QFrame, QFileDialog, QLineEdit, QHBoxLayout
from PyQt6 import QtCore
import pandas as pd
import subprocess
import copy
import sys
import os
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle("CIS-DEIB")
self.setGeometry(200, 200, 400, 300)
# Main widget
main_widget = QWidget(self)
self.setCentralWidget(main_widget)
# Layout
self.layout = QVBoxLayout(main_widget)
# Upload panel
self.upload = QPushButton("Upload Excel")
self.upload.setMaximumWidth(200)
self.upload.setMinimumWidth(100)
self.upload.clicked.connect(self.upload_file)
self.upload_panel = QWidget()
self.upload_layout = QVBoxLayout(self.upload_panel)
self.upload_layout.addWidget(self.upload)
self.upload_layout.setAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.layout.addWidget(self.upload_panel)
# Horizontal line
line1 = QFrame()
line1.setFrameShape(QFrame.Shape.HLine)
line1.setFrameShadow(QFrame.Shadow.Sunken)
self.layout.addWidget(line1)
# Cohort panel
self.cohort_panel = QWidget()
self.cohort_layout = QVBoxLayout(self.cohort_panel)
self.layout.addWidget(self.cohort_panel)
# Horizontal line
line2 = QFrame()
line2.setFrameShape(QFrame.Shape.HLine)
line2.setFrameShadow(QFrame.Shadow.Sunken)
self.layout.addWidget(line2)
# Semester panel
self.semester_panel = QWidget()
self.semester_layout = QVBoxLayout(self.semester_panel)
self.layout.addWidget(self.semester_panel)
# Horizontal line
line3 = QFrame()
line3.setFrameShape(QFrame.Shape.HLine)
line3.setFrameShadow(QFrame.Shadow.Sunken)
self.layout.addWidget(line3)
# Option panel
self.option_panel = QWidget()
self.option_layout = QVBoxLayout(self.option_panel)
self.layout.addWidget(self.option_panel)
def clear_panels(self):
# Clear all panels
self.semester = None
for i in reversed(range(self.cohort_layout.count())):
widget = self.cohort_layout.itemAt(i).widget()
widget.setParent(None)
for i in reversed(range(self.semester_layout.count())):
widget = self.semester_layout.itemAt(i).widget()
widget.setParent(None)
for i in reversed(range(self.option_layout.count())):
widget = self.option_layout.itemAt(i).widget()
widget.setParent(None)
def upload_file(self):
# handle file dialog
file, _ = QFileDialog.getOpenFileName(self, "Upload Excel", "", "Excel Files (*.xlsx *.xls)")
try:
if file and file.endswith(('.xlsx', '.xls')):
self.process_file(file)
else:
raise Exception("Not an Excel file")
except Exception as e:
# Print exceptions in panel
self.clear_panels()
error_message = f"{type(e).__name__}: {str(e)}"
err = QLineEdit(error_message)
err.setReadOnly(True)
self.cohort_layout.addWidget(err)
def process_file(self, file):
self.clear_panels()
# Load in Excel
self.file_path = file
self.file = pd.ExcelFile(file)
assert len(self.file.sheet_names) > 1
# Set cohort buttons
if len(self.file.sheet_names) > 1:
for i in range(1, len(self.file.sheet_names)):
button = QPushButton("COHORT " + self.file.sheet_names[i])
button.clicked.connect(self.cohort_download)
self.cohort_layout.addWidget(button)
# Set semester buttons
if len(self.file.sheet_names) > 1:
for i in range(1, len(self.file.sheet_names)):
button = QPushButton(self.file.sheet_names[i])
button.setCheckable(True)
button.clicked.connect(lambda _, b=button: self.semester_button_click(b))
self.semester_layout.addWidget(button)
# Set option buttons
option_widget = QWidget()
option_layouts = QHBoxLayout(option_widget)
for option in ["First-Year", "Retention", "Changed Major", "Graduates"]:
button = QPushButton(option)
button.clicked.connect(self.option_download)
option_layouts.addWidget(button)
self.option_layout.addWidget(option_widget)
# Set window title
self.setWindowTitle("FILE: " + file)
def semester_button_click(self, button):
# Select semester and unselect other semester buttons
for i in range(self.semester_layout.count()):
widget = self.semester_layout.itemAt(i).widget()
if isinstance(widget, QPushButton):
widget.setChecked(False)
button.setChecked(True)
self.semester = button.text()
def option_download(self):
try:
# Read option and semester
option = self.sender().text()
if not self.semester:
return
semester = self.file.sheet_names.index(self.semester)
assert semester > 0
# Get output path
file_name = os.path.splitext(os.path.basename(self.file_path))[0]
downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads")
output_path = os.path.join(downloads_dir, f"{file_name}_{self.semester}_{option}.xlsx".replace(" ", "_"))
# filter non-citizen, non-undergrad, rename 'Academic Plan' to 'Major'
dfs = [pd.read_excel(self.file, sheet_name=s) for s in self.file.sheet_names]
for idx, df in enumerate(dfs):
if 'US Citizen' in df.columns:
df = df[df['US Citizen'].fillna('').eq('Citizen')]
if 'Class/Level' in df.columns:
df = df[~df['Class/Level'].str.contains('grad', case=False, na=False)]
if 'Academic Plan' in df.columns:
df = df.rename(columns={'Academic Plan': 'Major'})
dfs[idx] = df
# get only relevant dfs
prev_df = dfs[semester - 1]
curr_df = dfs[semester]
# see what option is cho
match option:
case "First-Year":
# get netids of students from previous semester with CS major
prev_ids = prev_df[prev_df['Major'].str.contains('Computer Science', case=False, na=False)]['Netid']
df = curr_df[
# currently in CS major
(curr_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# was not in CS major in the previous data
(~curr_df['Netid'].isin(prev_ids))
]
case "Retention":
# get netids of students from previous semester with CS major
prev_ids = prev_df[prev_df['Major'].str.contains('Computer Science', case=False, na=False)]['Netid']
df = curr_df[
# currently in CS major
(curr_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# was also in CS major in the previous data
(curr_df['Netid'].isin(prev_ids))
]
case "Changed Major":
# get netids of students from previous semester with CS major
prev_ids = prev_df[prev_df['Major'].str.contains('Computer Science', case=False, na=False)]['Netid']
df = curr_df[
# currently not in CS major
(~curr_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# was in CS major in the previous data
(curr_df['Netid'].isin(prev_ids))
]
case "Graduates":
# get netids of current students
curr_ids = curr_df['Netid']
df = prev_df[
# was in CS major in the previous data
(prev_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# no longer a student this data cycle
(~prev_df['Netid'].isin(curr_ids))
]
# write to output
with pd.ExcelWriter(output_path, engine="xlsxwriter") as writer:
overview = df.groupby(["Ethnic CU", "Gender"]).size().unstack(fill_value=0)
overview.to_excel(writer, sheet_name="Overview")
df.to_excel(writer, sheet_name="All", index=False)
for group in df["Ethnic CU"].unique():
group_df = df[df["Ethnic CU"] == group]
group_df.to_excel(writer, sheet_name=f"Group - {group}", index=False)
subprocess.run(["osascript", "-e", f'tell application "Finder" to reveal POSIX file "{os.path.expanduser(output_path)}"'])
except Exception as e:
# Print exceptions in middle panel
self.clear_panels()
error_message = f"{type(e).__name__}: {str(e)}"
err = QLineEdit(error_message)
err.setReadOnly(True)
self.cohort_layout.addWidget(err)
def cohort_download(self):
try:
# Read option and semester
cohort = self.sender().text()
semester = " ".join(cohort.split(" ")[1:])
semester = self.file.sheet_names.index(semester)
# Get output path
file_name = os.path.splitext(os.path.basename(self.file_path))[0]
downloads_dir = os.path.join(os.path.expanduser("~"), "Downloads")
output_path = os.path.join(downloads_dir, f"{file_name}_{cohort}.xlsx".replace(" ", "_"))
# filter non-citizen, non-undergrad, rename 'Academic Plan' to 'Major'
dfs = [pd.read_excel(self.file, sheet_name=s) for s in self.file.sheet_names]
for idx, df in enumerate(dfs):
if 'US Citizen' in df.columns:
df = df[df['US Citizen'].fillna('').eq('Citizen')]
if 'Class/Level' in df.columns:
df = df[~df['Class/Level'].str.contains('grad', case=False, na=False)]
if 'Academic Plan' in df.columns:
df = df.rename(columns={'Academic Plan': 'Major'})
dfs[idx] = df
save_dfs = []
save_dfs_names = []
# get first semesters
prev_df = dfs[semester - 1]
curr_df = dfs[semester]
# get first-years
prev_ids = prev_df[prev_df['Major'].str.contains('Computer Science', case=False, na=False)]['Netid']
df = curr_df[
# currently in CS major
(curr_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# was not in CS major in the previous data
(~curr_df['Netid'].isin(prev_ids))
]
# save a copy of the dataframe
save_dfs.append(copy.deepcopy(df))
save_dfs_names.append("FirstYear " + self.file.sheet_names[semester])
# make sure all dfs only have the current first years
first_ids = set(df['Netid']) if 'Netid' in df.columns else {}
dfs = [df[df["Netid"].isin(first_ids)] for df in dfs]
while(semester + 1 < len(self.file.sheet_names)):
semester += 1
prev_df = dfs[semester - 1]
curr_df = dfs[semester]
# Retention
df = curr_df[
# still in CS major
(curr_df['Major'].str.contains('Computer Science', case=False, na=False))
]
save_dfs.append(copy.deepcopy(df))
save_dfs_names.append("Retention " + self.file.sheet_names[semester])
# ChangeMajor
prev_ids = prev_df[prev_df['Major'].str.contains('Computer Science', case=False, na=False)]['Netid']
df = curr_df[
(~curr_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# was in CS major in the previous data
(curr_df['Netid'].isin(prev_ids))
]
save_dfs.append(copy.deepcopy(df))
save_dfs_names.append("ChangedMajor " + self.file.sheet_names[semester])
# Gradutes
curr_ids = curr_df['Netid']
df = prev_df[
(prev_df['Major'].str.contains('Computer Science', case=False, na=False)) &
# no longer a student this data cycle
(~prev_df['Netid'].isin(curr_ids))
]
save_dfs.append(copy.deepcopy(df))
save_dfs_names.append("Gradutes " + self.file.sheet_names[semester])
# anomoly detector
# detect the following:
# 1. student not following typical path
# Example: retained --> graduated --> retained
# 2. student appearing in multiple sheet per semester
# Example: appeared as both retained and graduated in one semester
anomoly=[]
for netids in first_ids:
sheets = []
for idx, save_df in enumerate(save_dfs):
if save_df['Netid'].eq(netids).any():
sheets.append(save_dfs_names[idx])
options = [i.split(" ")[0] for i in sheets]
semesters = [" ".join(i.split(" ")[1:]) for i in sheets]
if len(semesters) != len(set(semesters)):
anomoly.append(f"{netids}: Student appears in multiple sheets in the same semester/year.")
state = 0 # 0: FirstYear, 1: Retention, 2: final (ChangedMajor or Gradutes)
for o in options:
if state == 0:
if o == "FirstYear":
continue
elif o == "Retention":
state = 1
elif o in ("ChangedMajor", "Gradutes"):
state = 2
else:
anomoly.append(f"{netids}: [Error] Unknown sheet name encountered.")
break
elif state == 1:
if o == "Retention":
continue
elif o in ("ChangedMajor", "Gradutes"):
state = 2
else:
anomoly.append(f"{netids}: [Error] Invalid sequence encountered.")
break
elif state == 2:
anomoly.append(f"{netids}: [Error] Student was considered graduated but came back as undergrad or reappeared.")
break
save_dfs.insert(1, pd.DataFrame(anomoly, columns=['Anomoly']))
save_dfs_names.insert(1, 'Anomoly')
# write to output
with pd.ExcelWriter(output_path, engine="xlsxwriter") as writer:
for i in range(len(save_dfs)):
sdf = save_dfs[i]
if "Ethnic CU" in sdf.columns and "Gender" in sdf.columns:
overview = sdf.groupby(["Ethnic CU", "Gender"]).size().unstack(fill_value=0)
overview.to_excel(writer, sheet_name="OV " + save_dfs_names[i])
sdf.to_excel(writer, sheet_name=save_dfs_names[i], index=False)
else:
sdf.to_excel(writer, sheet_name=save_dfs_names[i], index=False)
subprocess.run(["osascript", "-e", f'tell application "Finder" to reveal POSIX file "{os.path.expanduser(output_path)}"'])
except Exception as e:
# Print exceptions in middle panel
self.clear_panels()
error_message = f"{type(e).__name__}: {str(e)}"
err = QLineEdit(error_message)
err.setReadOnly(True)
self.cohort_layout.addWidget(err)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())