Skip to content

Commit d48543e

Browse files
authored
Add new generator (#341)
1 parent 20dbfd4 commit d48543e

File tree

1 file changed

+59
-33
lines changed

1 file changed

+59
-33
lines changed

scripts/variants_generation.py

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,63 @@
11
import csv
22
import numpy as np
33
from xlsxwriter.workbook import Workbook
4+
from pathlib import Path
45

5-
NUM_TASKS = 3
6-
NUM_STUDENTS = 29
7-
NUM_VARIANTS = 27
8-
csv_file = 'variants.csv'
9-
10-
list_of_tasks = []
11-
str_of_print = ""
12-
str_of_headers = ""
13-
for i in range(NUM_TASKS):
14-
list_of_variants = []
15-
shuffled_list_of_variants = []
16-
for j in range(int(NUM_STUDENTS / NUM_VARIANTS) + 1):
17-
list_of_variants.append(np.arange(NUM_VARIANTS) + 1)
18-
for variant in list_of_variants:
19-
np.random.shuffle(variant)
20-
shuffled_list_of_variants.append(variant)
21-
result_variants = np.concatenate(shuffled_list_of_variants)
22-
list_of_tasks.append(result_variants[:NUM_STUDENTS])
23-
str_of_print += '%d,'
24-
str_of_headers += 'Task ' + str(i + 1) + ','
25-
str_of_print = str_of_print[:-1]
26-
str_of_headers = str_of_headers[:-1]
27-
28-
np.savetxt(csv_file, np.dstack(list_of_tasks)[0], str_of_print, header=str_of_headers)
29-
30-
workbook = Workbook(csv_file[:-4] + '.xlsx')
31-
worksheet = workbook.add_worksheet()
32-
with open(csv_file, 'rt') as f:
33-
reader = csv.reader(f)
34-
for r, row in enumerate(reader):
35-
for c, col in enumerate(row):
36-
worksheet.write(r, c, col)
37-
workbook.close()
6+
7+
def get_project_path():
8+
script_path = Path(__file__).resolve() # Absolute path of the script
9+
script_dir = script_path.parent # Directory containing the script
10+
return script_dir.parent
11+
12+
13+
def generate_group_table(_num_tasks, _num_students, _num_variants, _csv_file):
14+
if _num_tasks != len(_num_variants):
15+
raise Exception(f"Count of students: {_num_tasks} != count of list of variants: {len(_num_variants)}")
16+
17+
list_of_tasks = []
18+
str_of_print = ""
19+
str_of_headers = ""
20+
for i, num_v in zip(range(_num_tasks), _num_variants):
21+
list_of_variants = []
22+
shuffled_list_of_variants = []
23+
for j in range(int(_num_students / num_v) + 1):
24+
list_of_variants.append(np.arange(num_v) + 1)
25+
for variant in list_of_variants:
26+
np.random.shuffle(variant)
27+
shuffled_list_of_variants.append(variant)
28+
result_variants = np.concatenate(shuffled_list_of_variants)
29+
list_of_tasks.append(result_variants[:_num_students])
30+
str_of_print += '%d,'
31+
str_of_headers += 'Task ' + str(i + 1) + ','
32+
str_of_print = str_of_print[:-1]
33+
str_of_headers = str_of_headers[:-1]
34+
35+
np.savetxt(_csv_file, np.dstack(list_of_tasks)[0], str_of_print, header=str_of_headers)
36+
37+
workbook = Workbook(_csv_file[:-4] + '.xlsx')
38+
worksheet = workbook.add_worksheet()
39+
with open(_csv_file, 'rt') as f:
40+
reader = csv.reader(f)
41+
for r, row in enumerate(reader):
42+
for c, col in enumerate(row):
43+
worksheet.write(r, c, col)
44+
workbook.close()
45+
46+
47+
if __name__ == "__main__":
48+
# Define the number of tasks
49+
num_tasks = 3
50+
51+
# List containing the number of students for each task
52+
list_students = [29, 10, 40]
53+
54+
# List containing the number of variants (versions) for each task
55+
num_variants = [27, 2, 9]
56+
57+
# Overall, `path_to_results` represents the file path leading to a csv's and xlsx's directory
58+
path_to_results = Path(get_project_path()) / "build" / "variants_results"
59+
path_to_results.mkdir(parents=True, exist_ok=True)
60+
61+
for num_students, index in zip(list_students, range(len(list_students))):
62+
csv_path = path_to_results / f'variants_group_{index + 1}.csv'
63+
generate_group_table(num_tasks, num_students, num_variants, csv_path.as_posix())

0 commit comments

Comments
 (0)