|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""TODO: Add a description here.""" |
|
|
|
import evaluate |
|
import datasets |
|
import re |
|
import dateutil.parser |
|
import numpy as np |
|
|
|
import time |
|
|
|
|
|
|
|
_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} |
|
""" |
|
|
|
|
|
BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt" |
|
|
|
|
|
@evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION) |
|
class LogMetric(evaluate.Metric): |
|
"""TODO: Short description of my evaluation module.""" |
|
|
|
|
|
timestamp_regex = r'(^\d{4}[-/.]\d{2}[-/.]\d{2}(?:[ T]\d{2}[:]\d{2}(?:[:]\d{2}(?:[.,]\d+)?)?(?:Z|[+-]\d{2}[:]\d{2})?)?)' |
|
timestamp_pattern = re.compile(timestamp_regex, re.MULTILINE) |
|
|
|
def _info(self): |
|
|
|
return evaluate.MetricInfo( |
|
|
|
module_type="metric", |
|
description=_DESCRIPTION, |
|
citation=_CITATION, |
|
inputs_description=_KWARGS_DESCRIPTION, |
|
|
|
|
|
features=datasets.Features({ |
|
"predictions": datasets.Value("string", id="sequence"), |
|
"references": datasets.Value("string", id="sequence"), |
|
}), |
|
|
|
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""" |
|
|
|
pass |
|
|
|
def getLogMetric(self, pred : str, ref : str): |
|
ref = ref.strip(' \t\n\r') |
|
pred = pred.strip(' \t\n\r') |
|
|
|
|
|
pred_timestrings = self.timestamp_pattern.findall(pred) |
|
ref_timestrings = self.timestamp_pattern.findall(ref) |
|
|
|
|
|
if(len(pred_timestrings) != len(ref_timestrings)): |
|
return 0.0 |
|
|
|
|
|
if (len(pred_timestrings) == 0): |
|
return 1.0 |
|
|
|
|
|
|
|
pred_timestring_pattern = re.sub(r'\d', r'\\d', re.escape(pred_timestrings[0])) |
|
|
|
|
|
prev_datetime = None |
|
|
|
for ts in pred_timestrings: |
|
try: |
|
|
|
matchesPattern = re.fullmatch(pred_timestring_pattern, ts) is not None |
|
|
|
cur_datetime = dateutil.parser.parse(ts) |
|
monotonicallyIncreasing = True if prev_datetime == None else prev_datetime <= cur_datetime |
|
prev_datetime = cur_datetime |
|
|
|
if not (matchesPattern and monotonicallyIncreasing): |
|
|
|
return 0.0 |
|
|
|
except Exception as e: |
|
|
|
return 0.0 |
|
|
|
|
|
return 1.0 |
|
|
|
def _compute(self, predictions, references): |
|
"""Returns the scores""" |
|
|
|
t_before_logmetric = time.perf_counter() |
|
timestamp_score = np.mean([self.getLogMetric(p,r) for p,r in zip(predictions,references)]) |
|
t_after_logmetric = time.perf_counter() |
|
|
|
logmetric_duration = f" {t_after_logmetric - t_before_logmetric:0.10f}" |
|
|
|
return { |
|
"score": timestamp_score, |
|
"duration": logmetric_duration, |
|
} |
|
|