Spaces:
Runtime error
Runtime error
File size: 10,120 Bytes
a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 35a28f9 a84678a 88b0888 a84678a 88b0888 a84678a |
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
import evaluate
import datasets
import numpy as np
# Add BibTeX citation
_CITATION = """\
@Paper{
author = {Bruno Taillé, Vincent Guigue, Geoffrey Scoutheeten, Patrick Gallinari},
title = {Let's Stop Incorrect Comparisons in End-to-end Relation Extraction!},
year = {2020},
link = https://arxiv.org/abs/2009.10684
}
"""
# Add description of the module here
_DESCRIPTION = """\
This metric is used for evaluating the quality of relation extraction output. By calculating the Micro and Macro F1 score of every relation extraction outputs to ensure the quality.
"""
# Add description of the arguments of the module here
_KWARGS_DESCRIPTION = """
Calculates how good are predictions given some references, using Micro and Macro F1 scores
Args:
predictions: list of list of dictionary, including relation and its type
dictionary should be key value pair like entity name link to its type
references: list of list of dictionary, including relation and its type
dictionary should be entity name pair like entity name link to its type
Returns:
evaluation result:
- **sell** (`dictionary`): score of type sell
- **tp** : true positive count
- **fp** : false positive count
- **fn** : false negative count
- **p** : precision
- **r** : recall
- **f1** : micro f1 score
- **ALL** (`dictionary`): score of all of the type (sell and belongs to)
- **tp** : true positive count
- **fp** : false positive count
- **fn** : false negative count
- **p** : precision
- **r** : recall
- **f1** : micro f1 score
- **Macro_f1** : macro f1 score
- **Macro_p** : macro precision
- **Macro_r** : macro recall
Examples:
>>> metric_path = "Ikala-allen/relation_extraction"
>>> module = evaluate.load(metric_path)
>>> references = [
... [
... {"head": "phip igments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
... {"head": "tinadaviespigments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
... ]
... ]
>>> predictions = [
... [
... {"head": "phipigments", "head_type": "product", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
... {"head": "tinadaviespigments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
... ]
... ]
>>> evaluation_scores = module.compute(predictions=predictions, references=references)
>>> print(evaluation_scores)
{'sell': {'tp': 1, 'fp': 1, 'fn': 1, 'p': 50.0, 'r': 50.0, 'f1': 50.0}, 'ALL': {'tp': 1, 'fp': 1, 'fn': 1, 'p': 50.0, 'r': 50.0, 'f1': 50.0, 'Macro_f1': 50.0, 'Macro_p': 50.0, 'Macro_r': 50.0}}
"""
def convert_format(data:list):
"""
Args:
data (list) : list of dictionaries with different entity elements
e.g
[
{'head': ['phipigments', 'tinadaviespigments'...],
'head_type': ['product', 'brand'...],
'type': ['sell', 'sell'...],
'tail': ['國際認證之色乳', '國際認證之色乳'...],
'tail_type': ['product', 'product'...]}, # first element
{'head': ['SABONTAIWAN', 'SNTAIWAN'...],
'head_type': ['brand', 'brand'...],
'type': ['sell', 'sell'...],
'tail': ['大馬士革玫瑰有機光燦系列', '大馬士革玫瑰有機光燦系列'...],
'tail_type': ['product', 'product'...]} # second element
...
]
"""
predictions = []
for item in data:
prediction_group = []
for i in range(len(item['head'])):
prediction = {
'head': item['head'][i],
'head_type': item['head_type'][i],
'type': item['type'][i],
'tail': item['tail'][i],
'tail_type': item['tail_type'][i],
}
prediction_group.append(prediction)
predictions.append(prediction_group)
return predictions
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
class relation_extraction(evaluate.Metric):
"""
evaluation metric of relation extraction
inputs:
predictions : (`list` of `list`s of `dictionary`s) about relation and its type of prediction
references : (`list` of `list`s of `dictionary`s) about references for each relation and its type.
"""
def _info(self):
# TODO: Specifies the evaluate.EvaluationModuleInfo object
return evaluate.MetricInfo(
# This is the description that will appear on the modules page.
module_type="metric",
description=_DESCRIPTION,
citation=_CITATION,
inputs_description=_KWARGS_DESCRIPTION,
# This defines the format of each prediction and reference
features=datasets.Features({
'predictions': datasets.Sequence({
"head": datasets.Value("string"),
"head_type": datasets.Value("string"),
"type": datasets.Value("string"),
"tail": datasets.Value("string"),
"tail_type": datasets.Value("string"),
}),
'references': datasets.Sequence({
"head": datasets.Value("string"),
"head_type": datasets.Value("string"),
"type": datasets.Value("string"),
"tail": datasets.Value("string"),
"tail_type": datasets.Value("string"),
}),
}),
# Homepage of the module for documentation
homepage="http://module.homepage",
# Additional links to the codebase or references
codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
reference_urls=["http://path.to.reference.url/new_module"]
)
def _download_and_prepare(self, dl_manager):
"""Optional: download external resources useful to compute the scores"""
# TODO: Download external resources if needed
pass
def _compute(self, predictions, references, mode="strict", relation_types=[]):
"""Returns the scores"""
# TODO: Compute the different scores of the module
predictions = convert_format(predictions)
references = convert_format(references)
assert mode in ["strict", "boundaries"]
# construct relation_types from ground truth if not given
if len(relation_types) == 0:
for triplets in references:
for triplet in triplets:
relation = triplet["type"]
if relation not in relation_types:
relation_types.append(relation)
scores = {rel: {"tp": 0, "fp": 0, "fn": 0} for rel in relation_types + ["ALL"]}
# Count GT relations and Predicted relations
n_sents = len(references)
n_rels = sum([len([rel for rel in sent]) for sent in references])
n_found = sum([len([rel for rel in sent]) for sent in predictions])
# Count TP, FP and FN per type
for pred_sent, gt_sent in zip(predictions, references):
for rel_type in relation_types:
# strict mode takes argument types into account
if mode == "strict":
pred_rels = {(rel["head"], rel["head_type"], rel["tail"], rel["tail_type"]) for rel in pred_sent if
rel["type"] == rel_type}
gt_rels = {(rel["head"], rel["head_type"], rel["tail"], rel["tail_type"]) for rel in gt_sent if
rel["type"] == rel_type}
# boundaries mode only takes argument spans into account
elif mode == "boundaries":
pred_rels = {(rel["head"], rel["tail"]) for rel in pred_sent if rel["type"] == rel_type}
gt_rels = {(rel["head"], rel["tail"]) for rel in gt_sent if rel["type"] == rel_type}
scores[rel_type]["tp"] += len(pred_rels & gt_rels)
scores[rel_type]["fp"] += len(pred_rels - gt_rels)
scores[rel_type]["fn"] += len(gt_rels - pred_rels)
# Compute per entity Precision / Recall / F1
for rel_type in scores.keys():
if scores[rel_type]["tp"]:
scores[rel_type]["p"] = 100 * scores[rel_type]["tp"] / (scores[rel_type]["fp"] + scores[rel_type]["tp"])
scores[rel_type]["r"] = 100 * scores[rel_type]["tp"] / (scores[rel_type]["fn"] + scores[rel_type]["tp"])
else:
scores[rel_type]["p"], scores[rel_type]["r"] = 0, 0
if not scores[rel_type]["p"] + scores[rel_type]["r"] == 0:
scores[rel_type]["f1"] = 2 * scores[rel_type]["p"] * scores[rel_type]["r"] / (
scores[rel_type]["p"] + scores[rel_type]["r"])
else:
scores[rel_type]["f1"] = 0
# Compute micro F1 Scores
tp = sum([scores[rel_type]["tp"] for rel_type in relation_types])
fp = sum([scores[rel_type]["fp"] for rel_type in relation_types])
fn = sum([scores[rel_type]["fn"] for rel_type in relation_types])
if tp:
precision = 100 * tp / (tp + fp)
recall = 100 * tp / (tp + fn)
f1 = 2 * precision * recall / (precision + recall)
else:
precision, recall, f1 = 0, 0, 0
scores["ALL"]["p"] = precision
scores["ALL"]["r"] = recall
scores["ALL"]["f1"] = f1
scores["ALL"]["tp"] = tp
scores["ALL"]["fp"] = fp
scores["ALL"]["fn"] = fn
# Compute Macro F1 Scores
scores["ALL"]["Macro_f1"] = np.mean([scores[ent_type]["f1"] for ent_type in relation_types])
scores["ALL"]["Macro_p"] = np.mean([scores[ent_type]["p"] for ent_type in relation_types])
scores["ALL"]["Macro_r"] = np.mean([scores[ent_type]["r"] for ent_type in relation_types])
return scores |