|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""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'^\s*(\d{4}[-/.]\d{2}[-/.]\d{2}(?:[ T]\d{2}[:]\d{2}(?:[:]\d{2}(?:[.,]\d+)?)?(?:Z|[+-]\d{2}[:]\d{2})?)?)\s*' |
|
timestamp_pattern = re.compile(timestamp_regex, re.MULTILINE) |
|
sacrebleu = evaluate.load("sacrebleu") |
|
|
|
|
|
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, sacrebleu): |
|
ref = ref.strip(' \t\n\r') |
|
pred = pred.strip(' \t\n\r') |
|
|
|
|
|
|
|
pred_split_log = self.timestamp_pattern.split(pred) |
|
|
|
ref_split_log = self.timestamp_pattern.split(ref) |
|
|
|
|
|
|
|
assert(len(pred_split_log) % 2 == len(ref_split_log) % 2 == 1) |
|
|
|
|
|
pred_logentries = [] |
|
ref_logentries = [] |
|
|
|
|
|
for i in range(1, len(pred_split_log), 2): |
|
pred_logentries.append((pred_split_log[i],pred_split_log[i+1])) |
|
|
|
for i in range(1, len(ref_split_log), 2): |
|
ref_logentries.append((ref_split_log[i],ref_split_log[i+1])) |
|
|
|
|
|
max_logentries = max(len(pred_logentries), len(ref_logentries)) |
|
min_logentries = min(len(pred_logentries), len(ref_logentries)) |
|
|
|
|
|
|
|
|
|
if (len(pred_logentries) == 0 and len(ref_logentries) == 0): |
|
|
|
assert(pred == "") |
|
|
|
logmsg_score = 100.0 if pred == ref else 0.0 |
|
return 0.3 * 100.0 + 0.7 * logmsg_score |
|
|
|
|
|
if (len(pred_logentries) == 0 or len(ref_logentries) == 0): |
|
|
|
return 0.0 |
|
|
|
|
|
|
|
|
|
pred_timestring_pattern = re.sub(r'\d', r'\\d', re.escape(pred_logentries[0][0])) |
|
|
|
matchesPatternScore = 100.0 |
|
monotonicallyIncreasingScore = 100.0 |
|
|
|
|
|
logmessage_scores = [] |
|
|
|
|
|
prev_datetime = None |
|
|
|
|
|
for i in range(min_logentries): |
|
ts, pred_lm = pred_logentries[i] |
|
_, ref_lm = ref_logentries[i] |
|
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): |
|
matchesPatternScore = 0.0 |
|
if (not monotonicallyIncreasing): |
|
monotonicallyIncreasingScore = 0.0 |
|
|
|
except Exception as e: |
|
|
|
matchesPatternScore = 0.0 |
|
monotonicallyIncreasingScore = 0.0 |
|
|
|
logmessage_scores.append(sacrebleu.compute(predictions=[pred_lm], references=[ref_lm])["score"]) |
|
|
|
|
|
assert(len(logmessage_scores) == min_logentries) |
|
|
|
logmessage_aggregated_score = ((min_logentries / max_logentries) * np.mean(logmessage_scores)) |
|
|
|
return 0.2 * monotonicallyIncreasingScore + 0.1 * matchesPatternScore + 0.7 * logmessage_aggregated_score |
|
|
|
def _compute(self, predictions, references): |
|
"""Returns the scores""" |
|
|
|
|
|
|
|
t_before_logmetric = time.perf_counter() |
|
timestamp_score = np.mean([self.getLogMetric(p,r, self.sacrebleu) 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, |
|
} |
|
|