To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

aggr.py 7.08 KB
Newer Older
1
import os
2
3
import logging
import csv
4
import jinja2
5
6
7
import pandas as pd
from .utils import logging as logutils

8
9
10
DEFAULT_CLUSTER_FILES_DIR="_clusters"
DEFAULT_CLUSTERS_MATCHES_CSV_FILE="clusters-matches.csv"
DEFAULT_CLUSTER_STUDENTS_CSV_FILE_PATTERN="cluster-students-{}.csv"
11
12
13
DEFAULT_CX_COURSE_STUDENTS_CSV_FILE="cx_students.csv"

def main(
14
15
16
    cluster_files_dir=DEFAULT_CLUSTER_FILES_DIR,
    clusters_matches_csv_file=DEFAULT_CLUSTERS_MATCHES_CSV_FILE,
    cluster_students_csv_file_pattern=DEFAULT_CLUSTER_STUDENTS_CSV_FILE_PATTERN,
17
18
    cx_course_students_csv_file=DEFAULT_CX_COURSE_STUDENTS_CSV_FILE):

scmalte's avatar
scmalte committed
19
20
  logutils.configure_level_and_format()

21
22
  if not os.path.isfile(clusters_matches_csv_file):
    raise RuntimeError("CSV file {} with matches per clusters doesn't exist. Should have been created by mu-cluster.".format(clusters_matches_csv_file))
23
24
25
26

  if not os.path.isfile(cx_course_students_csv_file):
    raise RuntimeError("Code Expert course data CSV file {} doesn't exist. Download it from Code Expert as follows: My Courses -> Students -> Export to CSV.".format(cx_course_students_csv_file))

27
  clusters_csv: pd.DataFrame = pd.read_csv(clusters_matches_csv_file)
28
  
scmalte's avatar
scmalte committed
29
  # Read CX course data, reduce to relevant columns, truncate TotalScore (which are floats), set index column
30
  relevant_course_columns = ["Legi", "Lastname", "Firstname", "Email", "Gender", "TotalScore"]
31
  course_csv: pd.DataFrame = pd.read_csv(cx_course_students_csv_file)
32
  course_csv = course_csv[relevant_course_columns]
scmalte's avatar
scmalte committed
33
  course_csv["TotalScore"] = course_csv["TotalScore"].round(0)
34
  course_csv.set_index("Legi", inplace=True)
35
  ## TODO: Remove staff from course_csv
36

37
38
39
40
41
  ## TODO: Make eDoz files configurable
  ## TODO: Make eDoz files optional
  ## TODO: Could integrate eDoz data "Leistungskontrollen" to get information whether
  ##       or not a student is a repeater  

42
  # Analogous for eDoz course data
43
  relevant_edoz_columns = ["Nummer", "Departement"]
44
  edoz1_csv: pd.DataFrame = pd.read_csv("edoz-252083200L.csv", sep="\t")
45
  edoz1_csv = edoz1_csv[relevant_edoz_columns]
46
47
48
49
50
51
  edoz1_csv.rename(columns={"Nummer": "Legi"}, inplace=True)
  edoz1_csv.set_index("Legi", inplace=True)
  # print(edoz1_csv)
  # print("edoz1_csv.index.is_unique = {}".format(edoz1_csv.index.is_unique))
  
  edoz2_csv: pd.DataFrame = pd.read_csv("edoz-252084800L.csv", sep="\t")
52
  edoz2_csv = edoz2_csv[relevant_edoz_columns]
53
54
55
56
  edoz2_csv.rename(columns={"Nummer": "Legi"}, inplace=True)
  edoz2_csv.set_index("Legi", inplace=True)
  # print(edoz2_csv.index)
  # print("edoz2_csv.index.is_unique = {}".format(edoz2_csv.index.is_unique))
57

58
59
60
  # Vertically concat eDoz data. Since students may be enrolled into multiple
  # courses, duplicated rows are afterwards dropped.
  edoz_csv: pd.DataFrame = pd.concat([edoz1_csv, edoz2_csv])
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  # print("========== edoz_csv [initial]")
  # print(edoz_csv.shape)
  # print(edoz_csv)
  # edoz_csv.drop_duplicates(inplace=True) # Not applicable here since indices are ignored
  edoz_csv = edoz_csv.loc[~edoz_csv.index.duplicated(keep='first')] # Get rows not in the set of duplicated indices
  # print("========== edoz_csv [unique]")
  # print(edoz_csv.shape)
  # print(edoz_csv)


  ## TODO: Add "Departement" column to course_csv, by joining with edoz_csv


  ### Aggregate course overview statistics
  edoz_departements: pd.DataFrame = edoz_csv["Departement"].value_counts()
  course_genders: pd.DataFrame = course_csv["Gender"].value_counts()

78
79
80
81
  assert edoz_csv.index.is_unique, "Expected unique indices (= legis) in edoz_csv"
  # # Show rows with non-unique indices (https://stackoverflow.com/questions/20199129) 
  # print(edoz_csv[edoz_csv.index.duplicated(keep=False)])
  
82
83
84

  jinja2_file_loader = jinja2.FileSystemLoader(".")
  jinja2_env = jinja2.Environment(loader=jinja2_file_loader)
85
  template = jinja2_env.get_template("./_static/clusters.html.jinja")
86
87
88
89
90
91

  # output = template.render(colors=colors)
  # print(output)

  jinja2_rows = []

92
  cluster_groups: pd.DataFrameGroupBy = clusters_csv.groupby("cluster_id")
93
  for cluster_id, cluster in cluster_groups: # cluster: pd.DataFrame
94
    # print("-"*60)
95
96
    # Get all ids (= legis) participating in a cluster
    ids_values: numpy.ndarray = pd.concat([cluster["id1"], cluster["id2"]]).unique()
97
    
98
    # ids = pd.Series(ids_values, name="Legi", index=ids_values)
99
100
    # # Performs an inner join on the keys; here: legis
    # # https://pandas.pydata.org/pandas-docs/stable/getting_started/comparison/comparison_with_sql.html#compare-with-sql-join
101
    # join = pd.merge(ids, course_csv, left_index=True, right_index=True)
102

103
    cluster_course_rows: pd.DataFrame = course_csv.loc[ids_values]
104

105
106
107
108
109
110
111
112
113
114
115
116
117
    # print("========== cluster ")
    # print(cluster.shape)
    # print(cluster)
    # print("========== ids_values ")
    # print(ids_values.shape)
    # print(ids_values)
    # print("========== course_csv")
    # print(course_csv)
    # print("========== cluster_course_rows")
    # print(cluster_course_rows.shape)
    # print(cluster_course_rows)
    # print("========== edoz_csv")
    # print(edoz_csv.shape)
118
119
    # print(edoz_csv)

120
121
    cluster_rows: pd.DataFrame = cluster_course_rows.join(edoz_csv)

122
123
124
125
126
127
128
    students_per_clusters_file = os.path.join(
        cluster_files_dir, 
        cluster_students_csv_file_pattern.format(cluster_id))
    
    logging.info("Writing students per clusters to file {}".format(students_per_clusters_file))
    cluster_rows.to_csv(students_per_clusters_file)

129
130
131
    # print("========== cluster_rows")
    # print(cluster_rows.shape)
    # print(cluster_rows)
132
    # print(name)
133
134
135
136
137
138

    # print(cluster)
    # print(cluster["svg_file"].iat[0])

    jinja2_rows.append((cluster, cluster_rows))

scmalte's avatar
scmalte committed
139
140
141
142
143
144
145
  
  ## TODO: Support sorting clusters by max (or average) involved percentage


  plagiarist_count = 0
  for (_, cluster_rows) in jinja2_rows:
    plagiarist_count += cluster_rows.shape[0]
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186


  department_counts = {}
  for (cluster, cluster_rows) in jinja2_rows:
    for index, value in cluster_rows["Departement"].value_counts().iteritems():
      if index in department_counts:
        department_counts[index] += value
      else:
        department_counts[index] = value

  # print(department_counts)

  department_percentage = {}
  for dep in department_counts:
    department_percentage[dep] = department_counts[dep] / edoz_departements[dep] * 100
  
  # print(department_percentage)


  gender_counts = {}
  for (cluster, cluster_rows) in jinja2_rows:
    for index, value in cluster_rows["Gender"].value_counts().iteritems():
      if index in gender_counts:
        gender_counts[index] += value
      else:
        gender_counts[index] = value

  # print(gender_counts)

  gender_percentage = {}
  for dep in gender_counts:
    gender_percentage[dep] = gender_counts[dep] / course_genders[dep] * 100
  
  # print(gender_percentage)

  percentages = {**department_percentage, **gender_percentage}
  for key, value in percentages.items():
    percentages[key] = round(value, 1)

  # print(percentages)

187

188
189
190
191
192
  template.stream(
    title="Clusters",
    clusters=jinja2_rows,
    edoz_count=edoz_csv.shape[0],
    course_count=course_csv.shape[0],
scmalte's avatar
scmalte committed
193
    plagiarist_count=plagiarist_count,
194
195
    percentages=percentages
  ).dump("clusters.html")
196

197
198
199

if __name__ == "__main__":
  main()