Create score_focal_labels_only.py
Browse files
scoring_scripts/score_focal_labels_only.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# imports
|
| 2 |
+
from sklearn.preprocessing import MultiLabelBinarizer
|
| 3 |
+
from sklearn.metrics import classification_report
|
| 4 |
+
|
| 5 |
+
# global param
|
| 6 |
+
label_list = ['Background', 'Motivation', 'Uses', 'Extends', 'Similarities', 'Differences', 'Compare/Contrast', 'Future Work', 'Unclear']
|
| 7 |
+
|
| 8 |
+
# start of function
|
| 9 |
+
def evaluate_FOCAL_labels(references_jsonl, predictions_jsonl, print_report=False):
|
| 10 |
+
'''
|
| 11 |
+
Computes precision, recall and f1-scores for the labels of citations,
|
| 12 |
+
without looking at the location of these labels in the paragraph,
|
| 13 |
+
between two datasets loaded from jsonl (list of dicts with same keys).
|
| 14 |
+
In plain English, this check that you correctly predicted the reason(s) a given citation was made,
|
| 15 |
+
without checking if you correctly find the parts of the paragraph that explain the function of the citation.
|
| 16 |
+
'''
|
| 17 |
+
|
| 18 |
+
# sort the refs and pred by unique ID
|
| 19 |
+
references_jsonl = sorted(references_jsonl, key=lambda x:x['Identifier'])
|
| 20 |
+
predictions_jsonl = sorted(predictions_jsonl, key=lambda x:x['Identifier'])
|
| 21 |
+
|
| 22 |
+
# assert that paragraphs match
|
| 23 |
+
ref_paragraphs = [e['Paragraph'] for e in references_jsonl]
|
| 24 |
+
pred_paragraphs = [e['Paragraph'] for e in predictions_jsonl]
|
| 25 |
+
assert(ref_paragraphs==pred_paragraphs)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# build y_true and y_pred
|
| 29 |
+
mlb = MultiLabelBinarizer(classes=label_list)
|
| 30 |
+
y_true = mlb.fit_transform([e['Functions Label'] for e in references_jsonl])
|
| 31 |
+
y_pred = mlb.fit_transform([e['Functions Label'] for e in predictions_jsonl])
|
| 32 |
+
|
| 33 |
+
# build report for printing
|
| 34 |
+
report_string = classification_report(y_true=y_true,
|
| 35 |
+
y_pred=y_pred,
|
| 36 |
+
target_names=label_list,
|
| 37 |
+
zero_division=0.0,
|
| 38 |
+
output_dict=False
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# return report as dict (can't do both at the same time? slight waste of compute)
|
| 42 |
+
report_dict = classification_report(y_true=y_true,
|
| 43 |
+
y_pred=y_pred,
|
| 44 |
+
target_names=label_list,
|
| 45 |
+
zero_division=0.0,
|
| 46 |
+
output_dict=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if print_report:
|
| 50 |
+
print(report_string)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
return(report_dict)
|