Skip to content

Commit

Permalink
reformat historical probabilities util fn
Browse files Browse the repository at this point in the history
  • Loading branch information
el-agua committed Apr 8, 2024
1 parent 6ef7189 commit 7620241
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 27 deletions.
5 changes: 3 additions & 2 deletions backend/courses/management/commands/recompute_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ def recompute_historical_semester_probabilities(current_semester, verbose=False)
print("Recomputing historical probabilities for all topics...")
topics = Topic.objects.all()
# Iterate over each Topic
for i, topic in tqdm(enumerate(topics)):
for i, topic in tqdm(enumerate(topics), disable=not verbose, total=topics.count()):
# Calculate historical_year_probability for the current topic
ordered_courses = topic.courses.all().order_by("semester")
historical_prob = historical_semester_probability(current_semester, ordered_courses)
ordered_semester = [course.semester for course in ordered_courses]
historical_prob = historical_semester_probability(current_semester, ordered_semester)
# Update the historical_probabilities field for the current topic
topic.historical_probabilities_spring = historical_prob[0]
topic.historical_probabilities_summer = historical_prob[1]
Expand Down
57 changes: 32 additions & 25 deletions backend/courses/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ def get_semesters(semesters: str = None) -> list[str]:
return sorted(semesters)


def historical_semester_probability(current, courses):
def historical_semester_probability(current_semester: str, semesters: list[str]):
"""
:param current: The current semester represented in the 20XX(A|B|C) format.
:type current: str
Expand All @@ -724,34 +724,41 @@ def historical_semester_probability(current, courses):
taking a course in each semester.
:rtype: list
"""
prob_distribution = [0.4, 0.3, 0.15, 0.1, 0.05]
PROB_DISTRIBUTION = [0.4, 0.3, 0.15, 0.1, 0.05]

def normalize_and_round(prob, i):
"""Modifies the probability distribution to account for the
fact that the last course was taken i semesters ago."""
truncate = prob_distribution[:i]
fact that the last course was taken i years ago."""
truncate = PROB_DISTRIBUTION[:i]
total = sum(truncate)
return list(map(lambda x: round(x / total, 3), truncate))

current_index = int(translate_semester(current)) // 10
min_index = current_index - 60
max_index = current_index - 10
p = [0, 0, 0]
if courses == []:
return p
semester_probabilities = {"A": 0.0, "B": 0.0, "C": 0.0}
current_year = int(current_semester[:-1])
semesters = [
semester
for semester in semesters
if semester < str(current_year) and semester > str(current_year - 5)
]
if not semesters:
return [0, 0, 0]
if current_year - int(semesters[0][:-1]) < 5:
# If the class hasn't been offered in the last 5 years,
# we make sure the resulting probabilities sum to 1
modified_prob_distribution = normalize_and_round(
PROB_DISTRIBUTION, current_year - int(semesters[0][:-1])
)
else:
last_index = int(translate_semester(courses[0].semester)) // 10
if last_index > min_index:
prob_distribution = normalize_and_round(
prob_distribution, ((current_index - last_index) + 9) // 10
)
for c in courses:
index = int(translate_semester(c.semester)) // 10
if index < min_index or index > max_index:
continue
# Diff is the number of years ago the course was taken
diff = (current_index - index) // 10 - 1
if diff >= len(prob_distribution):
diff = len(prob_distribution) - 1
p[index % 10 - 1] += prob_distribution[diff]
return list(map(lambda x: min(round(x, 2), 1.00), p))
modified_prob_distribution = PROB_DISTRIBUTION
for historical_semester in semesters:
historical_year = int(historical_semester[:-1])
sem_char = historical_semester[-1].upper() # A, B, C
semester_probabilities[sem_char] += modified_prob_distribution[
current_year - historical_year - 1
]
return list(
map(
lambda x: min(round(x, 2), 1.00),
[semester_probabilities["A"], semester_probabilities["B"], semester_probabilities["C"]],
)
)

0 comments on commit 7620241

Please sign in to comment.