This repository was archived by the owner on Mar 9, 2025. It is now read-only.
forked from ArturoAmaya/ExploratoryCurricularAnalytics
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcollege_ges.py
More file actions
155 lines (139 loc) · 4.73 KB
/
Copy pathcollege_ges.py
File metadata and controls
155 lines (139 loc) · 4.73 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
"""
python3 college_ges.py > college_ges.csv
python3 college_ges.py 2022 html > reports/output/college-ge-units-fragment.html
python3 college_ges.py <year> debug <major> <college>
python3 college_ges.py <year> (html)
"""
import sys
from parse import major_codes, major_plans
from university import university
from util import partition
if len(sys.argv) <= 1:
raise ValueError("Need year: python3 college_ges.py <year> (html|debug) ...")
year = int(sys.argv[1])
if len(sys.argv) > 2 and sys.argv[2] == "debug":
if len(sys.argv) != 5:
raise ValueError(
"Need major code and college: python3 college_ges.py <year> debug <major> <college>"
)
_, _, _, major_code, college = sys.argv
courses = partition(
(
(
"MAJOR"
if course.for_major
else "ELECTIVE" if course.course_title.upper() == "ELECTIVE" else "GE"
),
(
course.course_title
if course.units == 4
else f"{course.course_title} ({course.units})"
),
)
for course in major_plans(int(year))[major_code].plan(college)
)
print("[Major]")
print(", ".join(courses.get("MAJOR") or []) or "(none)")
print()
print("[GE]")
print(", ".join(courses.get("GE") or []) or "(none)")
print()
print("[Padding]")
print(", ".join(courses.get("ELECTIVE") or []) or "(none)")
exit()
html = len(sys.argv) > 2 and sys.argv[2] == "html"
all_extra_ge_units = {
(major_code, college): sum(
course.units
for course in plans.plan(college)
if course.course_title.upper() != "ELECTIVE" and not course.for_major
)
for major_code, plans in major_plans(year).items()
for college in plans.colleges
}
min_ge = min(units for units in all_extra_ge_units.values() if units > 0)
max_ge = max(all_extra_ge_units.values())
print(f"min={min_ge} max={max_ge}", file=sys.stderr)
class ColorScale:
# Google Sheets color scale colors
_GREEN = 87, 187, 138
_YELLOW = 255, 214, 102
_RED = 230, 124, 115
@staticmethod
def _interpolate(t: float, a: float, b: float) -> str:
return str(a + (b - a) * t)
@classmethod
def color_scale(cls, t: float) -> str:
# green: rgb(87,187,138)
# yellow: rgb(255,214,102)
# red: rgb(230,124,115)
channels = ",".join(
(
cls._interpolate(t * 2, cls._GREEN[i], cls._YELLOW[i])
if t < 0.5
else cls._interpolate(t * 2 - 1, cls._YELLOW[i], cls._RED[i])
)
for i in range(3)
)
return f"rgb({channels}, var(--fill-opacity))"
if html:
college_headers = "".join(
f'<th className="college-header">{university.college_names[college]}</th>'
for college in university.college_codes
)
print(
f'<table><tr className="header"><th className="major">Major</th>{college_headers}</tr>'
)
else:
print(
"Major,"
+ ",".join(
university.college_names[college] for college in university.college_codes
)
)
sums = {college: 0.0 for college in university.college_codes}
major_count = 0
for major_code in major_plans(year).keys():
if major_code.startswith("UN"):
continue
major_count += 1
if html:
print(
f'<tr className="row" id="{major_code}"><th scope="col" className="major">'
)
print(
f'<span className="major-code">{major_code}</span><span className="major-name">: {major_codes()[major_code].name}</span></th>'
)
else:
print(major_code, end="")
for college in university.college_codes:
if (major_code, college) not in all_extra_ge_units:
if html:
print("<td></td>")
else:
print(",", end="")
continue
extra_ge_units = all_extra_ge_units[major_code, college]
sums[college] += extra_ge_units
if html:
color = (
ColorScale.color_scale((extra_ge_units - min_ge) / (max_ge - min_ge))
if extra_ge_units >= min_ge
else "transparent"
)
print(f'<td style="--color: {color};">{extra_ge_units: .0f}</td>')
else:
print(f",{extra_ge_units}", end="")
if html:
print(f"</tr>")
else:
print()
if html:
print('<tr className="average"><th scope="col" className="major">Average</th>')
for college in university.college_codes:
average = sums[college] / major_count
color = ColorScale.color_scale((average - min_ge) / (max_ge - min_ge))
print(f'<td style="--color: {color};">{average: .0f}</td>')
print(f"</tr>")
if html:
print("</table>")