-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotations.py
More file actions
63 lines (55 loc) · 2.46 KB
/
annotations.py
File metadata and controls
63 lines (55 loc) · 2.46 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
import pandas as pd
class Annotations():
"""Class for storing annotations (as a dataframe)"""
class AnnotatorError (RuntimeError):
"The annotator in some row doesn't fir self.annotator"
def __init__(self, annotator=None):
self.annotator = annotator
self.fields = ['hash', 'annotator',
'understand', 'purpose',
'bfc', 'bpc', 'prc', 'nfc', 'specification', 'asc', 'obvious',
'safety', 'timing', 'memory', 'info', 'safety_exp', 'time',
'see_commit_clicked', 'lore_clicked', 'lore_founded',
'is_merge_commit', 'is_part_patchset']
# These are the default values for the fields, but only that have an input in the form
self.fields_defaults = ['', self.annotator,
None, "",
None, None, None, None, None, None, None,
None, None, None, None, "",
False, False, False,
False
]
if annotator is None:
self.filename = None
self.df = pd.DataFrame(columns=self.fields)
else:
self.filename = f"annotations_{annotator}.csv"
try:
self.df = pd.read_csv(self.filename)
annotators = self.df['annotator'].to_numpy()
if not (self.annotator == annotators).all():
raise self.AnnotatorError
except FileNotFoundError:
self.df = pd.DataFrame(columns=self.fields)
def update(self, data):
self.annotator = data['annotator']
if any(self.df['hash'] == data['hash']):
self.df.loc[self.df['hash'] == data['hash'], data.keys()] = data.values()
else:
self.df.loc[len(self.df)] = data
def get(self, hash):
result_df = self.df[self.df['hash'] == hash]
if len(result_df) == 0:
results = None
else:
results = result_df.iloc[0].to_dict()
return results
def get_values(self, hash):
annotation = self.get(hash)
if annotation is None:
values = self.fields_defaults
else:
values = [annotation[key] for key in self.fields if key not in ['time', 'is_merge_commit']]
return values
def save(self):
self.df.to_csv(self.filename, index=False)