|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""TODO: Add a description here.""" |
|
|
|
import evaluate |
|
import datasets |
|
import ham |
|
import os |
|
import isco |
|
import json |
|
|
|
|
|
|
|
_CITATION = """\ |
|
@InProceedings{huggingface:module, |
|
title = {A great new module}, |
|
authors={huggingface, Inc.}, |
|
year={2020} |
|
} |
|
""" |
|
|
|
|
|
_DESCRIPTION = """\ |
|
This new module is designed to solve this great ML task and is crafted with a lot of care. |
|
""" |
|
|
|
|
|
|
|
_KWARGS_DESCRIPTION = """ |
|
Calculates how good are predictions given some references, using certain scores |
|
Args: |
|
predictions: list of predictions to score. Each predictions |
|
should be a string with tokens separated by spaces. |
|
references: list of reference for each prediction. Each |
|
reference should be a string with tokens separated by spaces. |
|
Returns: |
|
accuracy: description of the first score, |
|
another_score: description of the second score, |
|
Examples: |
|
Examples should be written in doctest format, and should illustrate how |
|
to use the function. |
|
|
|
>>> my_new_module = evaluate.load("my_new_module") |
|
>>> results = my_new_module.compute(references=[0, 1], predictions=[0, 1]) |
|
>>> print(results) |
|
{'accuracy': 1.0} |
|
""" |
|
|
|
|
|
ISCO_CSV_URL = "https://storage.googleapis.com/isco-public/tables/ISCO_structure.csv" |
|
ISCO_JSON_URL = "https://storage.googleapis.com/isco-public/tables/isco_structure.json" |
|
|
|
|
|
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION) |
|
class MetricTemplate1(evaluate.Metric): |
|
"""TODO: Short description of my evaluation module.""" |
|
|
|
def _info(self): |
|
|
|
return evaluate.MetricInfo( |
|
|
|
module_type="metric", |
|
description=_DESCRIPTION, |
|
citation=_CITATION, |
|
inputs_description=_KWARGS_DESCRIPTION, |
|
|
|
features=datasets.Features( |
|
{ |
|
"predictions": datasets.Value("string"), |
|
"references": datasets.Value("string"), |
|
} |
|
), |
|
|
|
homepage="http://module.homepage", |
|
|
|
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""" |
|
|
|
|
|
|
|
isco_csv = dl_manager.download_and_extract(ISCO_CSV_URL) |
|
print(f"ISCO CSV file downloaded") |
|
self.isco_hierarchy = isco.create_hierarchy_dict(isco_csv) |
|
print("ISCO hierarchy dictionary created") |
|
print(self.isco_hierarchy) |
|
|
|
def _compute(self, predictions, references): |
|
"""Returns the scores""" |
|
|
|
|
|
|
|
predictions = [str(p) for p in predictions] |
|
references = [str(r) for r in references] |
|
|
|
|
|
accuracy = sum(i == j for i, j in zip(predictions, references)) / len( |
|
predictions |
|
) |
|
|
|
|
|
|
|
|
|
|
|
hierarchy = self.isco_hierarchy |
|
hP, hR = ham.calculate_hierarchical_precision_recall( |
|
references, predictions, hierarchy |
|
) |
|
hF = ham.hierarchical_f_measure(hP, hR) |
|
print( |
|
f"Hierarchical Precision: {hP}, Hierarchical Recall: {hR}, Hierarchical F-measure: {hF}" |
|
) |
|
|
|
return { |
|
"accuracy": accuracy, |
|
"hierarchical_precision": hP, |
|
"hierarchical_recall": hR, |
|
"hierarchical_fmeasure": hF, |
|
} |
|
|