Spaces:
Sleeping
Sleeping
Download src/utils.py from notha99y/timetable_selection: direct link, hf CLI and curl.
- Browser
- Download file 6.85 kB
-
https://huggingface.co/spaces/notha99y/timetable_selection/resolve/main/src/utils.py
- Command line
-
hf download hf://spaces/notha99y/timetable_selection/src/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/notha99y/timetable_selection/resolve/main/src/utils.py
6.85 kB
| import re | |
| from datetime import datetime | |
| def get_day(timeslot): | |
| """ | |
| assume timeslot is Day starttime to endtime | |
| """ | |
| if type(timeslot) != str: | |
| # assume it is na | |
| return None | |
| else: | |
| return timeslot.split(" ")[0] | |
| def refine_time_parsing(df, time_column): | |
| """ | |
| Improves the parsing of time ranges by handling edge cases and filling in missing values. | |
| """ | |
| def time_to_24h(t): | |
| """Convert 12-hour time format to 24-hour time.""" | |
| return ( | |
| datetime.strptime(t, "%I%p").time() | |
| if "." not in t | |
| else datetime.strptime(t, "%I.%M%p").time() | |
| ) | |
| starts, ends = [], [] | |
| for entry in df[time_column]: | |
| match = re.search(r"(\d+\.?\d*\w+)\s*to\s*(\d+\.?\d*\w+)", str(entry)) | |
| if match: | |
| starts.append(time_to_24h(match.group(1))) | |
| ends.append(time_to_24h(match.group(2))) | |
| else: | |
| starts.append(None) | |
| ends.append(None) | |
| df[f"{time_column}_Start"] = starts | |
| df[f"{time_column}_End"] = ends | |
| return df | |
| def check_overlap(first_class, second_class, restrict_back_to_back=False): | |
| """ | |
| class: (day, start_time, end_time) | |
| """ | |
| # require both classes to have a valid day string | |
| if type(first_class[0]) != str or type(second_class[0]) != str: | |
| return False | |
| # require both classes to have start and end times | |
| if ( | |
| first_class[1] is None | |
| or first_class[2] is None | |
| or second_class[1] is None | |
| or second_class[2] is None | |
| ): | |
| return False | |
| # same day check | |
| if first_class[0] != second_class[0]: | |
| return False | |
| # If restricting back-to-back, treat end == start as overlap | |
| if restrict_back_to_back: | |
| # treat touching endpoints as overlap as well | |
| return not (first_class[2] < second_class[1] or second_class[2] < first_class[1]) | |
| # Default: strict overlap (interval intersection) | |
| return first_class[1] < second_class[2] and second_class[1] < first_class[2] | |
| def check_conflict(current_schedule, new_class, restrict_back_to_back=False): | |
| def is_touching(a, b): | |
| # a and b are (day, start, end) | |
| if a[0] != b[0]: | |
| return False | |
| if a[1] is None or a[2] is None or b[1] is None or b[2] is None: | |
| return False | |
| return a[2] == b[1] or b[2] == a[1] | |
| def count_touching_pairs(schedule_rows, day): | |
| # build list of intervals for the given day from schedule rows | |
| intervals = [] | |
| for r in schedule_rows: | |
| # lecture | |
| if r.get("lecture_day") == day and r.get("Lecture_Start") is not None: | |
| intervals.append((r.get("Lecture_Start"), r.get("Lecture_End"))) | |
| # tutorial | |
| if r.get("tutorial_day") == day and r.get("Tutorial_Start") is not None: | |
| intervals.append((r.get("Tutorial_Start"), r.get("Tutorial_End"))) | |
| # count touching pairs | |
| cnt = 0 | |
| for i in range(len(intervals)): | |
| for j in range(i + 1, len(intervals)): | |
| if intervals[i][1] == intervals[j][0] or intervals[j][1] == intervals[i][0]: | |
| cnt += 1 | |
| return cnt | |
| for s in current_schedule: | |
| # check lecture with lecture | |
| class_1 = (s["lecture_day"], s["Lecture_Start"], s["Lecture_End"]) | |
| class_2 = ( | |
| new_class["lecture_day"], | |
| new_class["Lecture_Start"], | |
| new_class["Lecture_End"], | |
| ) | |
| # Check overlap normally | |
| if check_overlap(class_1, class_2, restrict_back_to_back): | |
| # If overlap is a touching endpoint and we are restricting back-to-back, | |
| # allow up to 2 touching pairs per day. | |
| if restrict_back_to_back and is_touching(class_1, class_2): | |
| day = class_1[0] | |
| existing_touches = count_touching_pairs(current_schedule, day) | |
| # if adding this touch would make more than 2 touches, reject | |
| if existing_touches >= 2: | |
| return True | |
| # otherwise allow this touching pair (do not treat as conflict) | |
| else: | |
| return True | |
| # check lecture with tutorial | |
| class_1 = (s["lecture_day"], s["Lecture_Start"], s["Lecture_End"]) | |
| class_2 = ( | |
| new_class["tutorial_day"], | |
| new_class["Tutorial_Start"], | |
| new_class["Tutorial_End"], | |
| ) | |
| if check_overlap(class_1, class_2, restrict_back_to_back): | |
| if restrict_back_to_back and is_touching(class_1, class_2): | |
| day = class_1[0] | |
| existing_touches = count_touching_pairs(current_schedule, day) | |
| if existing_touches >= 2: | |
| return True | |
| else: | |
| return True | |
| # check tutorial with lecture | |
| class_1 = (s["tutorial_day"], s["Tutorial_Start"], s["Tutorial_End"]) | |
| class_2 = ( | |
| new_class["lecture_day"], | |
| new_class["Lecture_Start"], | |
| new_class["Lecture_End"], | |
| ) | |
| if check_overlap(class_1, class_2, restrict_back_to_back): | |
| if restrict_back_to_back and is_touching(class_1, class_2): | |
| day = class_1[0] | |
| existing_touches = count_touching_pairs(current_schedule, day) | |
| if existing_touches >= 2: | |
| return True | |
| else: | |
| return True | |
| # check tutorial with tutorial | |
| class_1 = (s["tutorial_day"], s["Tutorial_Start"], s["Tutorial_End"]) | |
| class_2 = ( | |
| new_class["tutorial_day"], | |
| new_class["Tutorial_Start"], | |
| new_class["Tutorial_End"], | |
| ) | |
| if check_overlap(class_1, class_2, restrict_back_to_back): | |
| if restrict_back_to_back and is_touching(class_1, class_2): | |
| day = class_1[0] | |
| existing_touches = count_touching_pairs(current_schedule, day) | |
| if existing_touches >= 2: | |
| return True | |
| else: | |
| return True | |
| return False | |
| def check_solution_is_valid(solution, num_combi, num_units): | |
| if len(solution) != num_combi: | |
| return False | |
| for s in solution: | |
| if len(s) != num_units: | |
| return False | |
| return True | |
| def print_solution(solution, show_selected_days=True): | |
| for s in solution: | |
| selected_days = [] | |
| for c in s: | |
| print( | |
| f"{c['Module']} | {c['Class']} | {c['Lecture']} | {c['Tutorial']}" | |
| ) | |
| selected_days.append(c["lecture_day"]) | |
| selected_days.append(c["tutorial_day"]) | |
| if show_selected_days: | |
| selected_days = list(set(selected_days)) | |
| print("selected days: ", selected_days) | |
| print("-" * 88) | |