|
| 1 | +import csv |
| 2 | +from itertools import cycle |
| 3 | +import os |
| 4 | +import time |
| 5 | +from pathlib import Path |
| 6 | +from typing import List, Dict, Iterator |
| 7 | + |
| 8 | +import pandas as pd |
| 9 | + |
| 10 | +from api.ai.external_algorithms.group_matcher_algorithm.custom_dataclasses import ( |
| 11 | + GroupMatcherStudent, |
| 12 | +) |
| 13 | +from api.ai.interfaces.algorithm import Algorithm |
| 14 | +from api.ai.interfaces.algorithm_config import GroupMatcherAlgorithmConfig |
| 15 | +from api.ai.interfaces.algorithm_options import GroupMatcherAlgorithmOptions |
| 16 | +from api.ai.interfaces.team_generation_options import TeamGenerationOptions |
| 17 | +from api.dataclasses.student import Student |
| 18 | +from api.dataclasses.team import Team |
| 19 | +from api.dataclasses.team_set import TeamSet |
| 20 | + |
| 21 | + |
| 22 | +class GroupMatcherAlgorithm(Algorithm): |
| 23 | + """ |
| 24 | + From paper: https://sigcse2023.sigcse.org/details/sigcse-ts-2023-papers/163/Inclusive-study-group-formation-at-scale |
| 25 | + """ |
| 26 | + |
| 27 | + student_trace: Dict[int, Student] |
| 28 | + team_trace: Dict[int, Team] |
| 29 | + team_cycler: Iterator[Team] |
| 30 | + |
| 31 | + group_matcher_input_data_file_path: Path |
| 32 | + group_matcher_output_data_file_path: Path |
| 33 | + group_matcher_config_path: Path |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + algorithm_options: GroupMatcherAlgorithmOptions, |
| 38 | + team_generation_options: TeamGenerationOptions, |
| 39 | + algorithm_config: GroupMatcherAlgorithmConfig, |
| 40 | + ): |
| 41 | + super().__init__(algorithm_options, team_generation_options, algorithm_config) |
| 42 | + self.group_matcher_input_data_file_path = Path(algorithm_config.csv_input_path) |
| 43 | + self.group_matcher_run_path = algorithm_config.group_matcher_run_path |
| 44 | + |
| 45 | + self.prepare_file_environment() |
| 46 | + |
| 47 | + self.team_trace = { |
| 48 | + team_idx + 1: team for team_idx, team in enumerate(self.teams) |
| 49 | + } |
| 50 | + self.team_cycler = cycle(self.teams) |
| 51 | + |
| 52 | + def prepare_file_environment(self): |
| 53 | + class_size = int(self.group_matcher_input_data_file_path.stem.split("-")[0]) |
| 54 | + self.group_matcher_run_path = self.group_matcher_run_path |
| 55 | + self.group_matcher_output_data_file_path = ( |
| 56 | + Path.cwd() / f"out-private-{class_size}.csv" |
| 57 | + ) |
| 58 | + if self.group_matcher_output_data_file_path.exists(): |
| 59 | + self.group_matcher_output_data_file_path.unlink() |
| 60 | + self.group_matcher_config_path = ( |
| 61 | + Path(self.group_matcher_run_path).parent / "example_config.py" |
| 62 | + ) |
| 63 | + if not self.group_matcher_input_data_file_path.parent.exists(): |
| 64 | + self.group_matcher_input_data_file_path.parent.mkdir(parents=True) |
| 65 | + |
| 66 | + def export_students_data_to_group_matcher_format_csv( |
| 67 | + self, students: List[Student] |
| 68 | + ) -> None: |
| 69 | + student_data = [ |
| 70 | + GroupMatcherStudent(student).get_formatted_data() for student in students |
| 71 | + ] |
| 72 | + self.student_trace = {student.id: student for student in students} |
| 73 | + with open(self.group_matcher_input_data_file_path, "w") as csvfile: |
| 74 | + writer = csv.DictWriter( |
| 75 | + csvfile, fieldnames=student_data[0].keys(), delimiter=";" |
| 76 | + ) |
| 77 | + writer.writeheader() |
| 78 | + writer.writerows(student_data) |
| 79 | + |
| 80 | + def generate(self, students: List[Student]) -> TeamSet: |
| 81 | + self.export_students_data_to_group_matcher_format_csv(students) |
| 82 | + |
| 83 | + # Run the group matcher algorithm |
| 84 | + cmd = f"python3 {self.group_matcher_run_path} {self.group_matcher_config_path} {self.group_matcher_input_data_file_path}" |
| 85 | + os.system(cmd) |
| 86 | + |
| 87 | + # This only happens when class size is small and the system I/O speed is not as fast as the runtime |
| 88 | + while not self.group_matcher_output_data_file_path.exists(): |
| 89 | + print("Not found file " + str(self.group_matcher_output_data_file_path)) |
| 90 | + time.sleep(1) |
| 91 | + # Read the output csv file and create a TeamSet |
| 92 | + df = pd.read_csv(self.group_matcher_output_data_file_path) |
| 93 | + |
| 94 | + return GroupMatcherStudent.transform_output_data_to_team_set( |
| 95 | + df, self.team_trace, self.student_trace, self.team_cycler |
| 96 | + ) |
0 commit comments