-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyseComments.py
More file actions
116 lines (96 loc) · 3.32 KB
/
Copy pathanalyseComments.py
File metadata and controls
116 lines (96 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import numpy as np
import pandas as pd
from typing import Dict, List, Mapping, Optional, Tuple
def sum_by_student(df: pd.DataFrame, column: str) -> Dict[str, int]:
df2 = df.pivot(columns="Student")[column]
sums: Dict[str, int] = {}
for student in df2.columns:
sums[student] = np.nansum(df2[student])
return sums
def weight_comments(df: pd.DataFrame) -> pd.DataFrame:
date_diffs = (df['Date'] - max(df['Date'])).apply(lambda x: x.days)
df['Weight'] = date_diffs.apply(lambda x: np.exp(x / 10.))
df.loc[df["Sentiment"] < 1, "Weight"] = 0
return df
def count_dnf_by_student(df: pd.DataFrame) -> Dict[str, int]:
return sum_by_student(df, "DNF")
def count_dnf_greater_than(df: pd.DataFrame, cut_off: float = 0) -> Dict[str, int]:
df = count_dnf_by_student(df)
df_positive: Dict[str, int] = {}
for k, v in df.items():
if v > cut_off:
df_positive[k] = v
return df_positive
def sum_weights_by_student(df: pd.DataFrame, students: List[str]) -> Dict[str, float]:
weights = dict.fromkeys(students, 0)
non_zero_weights = sum_by_student(df, "Weight")
v: int
for student, v in non_zero_weights.items():
if student in students:
weights[student] = v
return weights
def students_by_least_weight(weights: Dict[str, float]) -> List[str]:
return [k for k, v in sorted(weights.items(), key=lambda item: item[1])]
def comments_needed(df: pd.DataFrame, students: List[str]) -> List[str]:
if df.empty:
return students
else:
df = weight_comments(df)
weights = sum_weights_by_student(df, students)
return students_by_least_weight(weights)
def latex_comments(df: pd.DataFrame, student: str) -> str:
df = df[df['Student'].isin([student])]
if len(df) == 0:
return "No comments yet\n"
df = df[["Date", "Info"]]
df['Date'] = df['Date'].dt.strftime('%d%b%Y').astype(str)
return df.style.hide(axis="index").to_latex()
def latex_student_page(
outline: str,
student: str,
name: str,
course: str,
comments: str,
sentiment_graph: str = "",
exam_graph: str = "",
) -> str:
text = outline
keywords = [
"STUDENTCODE",
"STUDENTNAME",
"COURSE",
"STUDENTCOMMENTS",
"SENTIMENTGRAPH",
"EXAMGRAPH",
]
for (before, after) in zip(
keywords,
[student, name, course, comments, sentiment_graph, exam_graph],
):
text = text.replace(before, after)
return text
def latex_student_pages(
df: pd.DataFrame,
outline: str,
students: List[str],
given_names: List[str],
courses: List[str],
sentiment_graphs: Optional[Mapping[Tuple[str, str], str]] = None,
exam_graphs: Optional[Mapping[Tuple[str, str], str]] = None,
) -> str:
sentiment_graphs = sentiment_graphs or {}
exam_graphs = exam_graphs or {}
pages = []
for student, name, course in zip(students, given_names, courses):
comments = latex_comments(df, student)
this_student_latex_page = latex_student_page(
outline,
student,
name,
course,
comments,
sentiment_graphs.get((course, student), ""),
exam_graphs.get((course, student), ""),
)
pages.append(this_student_latex_page)
return "\\newpage\n\n".join(pages)