Skip to content

Commit

Permalink
Verify metrics prediction_type without loading metric (#1519)
Browse files Browse the repository at this point in the history
* Verify metrics prediction_type without loading metric

Signed-off-by: elronbandel <[email protected]>

* Refactor metric retrieval to use get_metrics_artifact_without_load for efficiency

Signed-off-by: elronbandel <[email protected]>

---------

Signed-off-by: elronbandel <[email protected]>
  • Loading branch information
elronbandel authored Jan 16, 2025
1 parent 5506f9c commit 4d50008
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/unitxt/task.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import warnings
from functools import lru_cache
from typing import Any, Dict, List, Optional, Union

from .artifact import fetch_artifact
from .deprecation_utils import deprecation
from .error_utils import Documentation, UnitxtError, UnitxtWarning
from .logging_utils import get_logger
from .metrics import MetricsList
from .operator import InstanceOperator
from .operators import ArtifactFetcherMixin
from .settings_utils import get_constants
from .settings_utils import get_constants, get_settings
from .templates import Template
from .type_utils import (
Type,
Expand All @@ -25,6 +25,7 @@

constants = get_constants()
logger = get_logger()
settings = get_settings()


@deprecation(
Expand Down Expand Up @@ -213,17 +214,17 @@ def process_data_before_dump(self, data):
return data

@classmethod
@lru_cache(maxsize=None)
def get_metrics_artifacts(cls, metric_id: str):
metric = cls.get_artifact(metric_id)
def get_metrics_artifact_without_load(cls, metric_id: str):
with settings.context(skip_artifacts_prepare_and_verify=True):
metric, _ = fetch_artifact(metric_id)
if isinstance(metric, MetricsList):
return metric.items
return [metric]

def check_metrics_type(self) -> None:
prediction_type = self.prediction_type
for metric_id in self.metrics:
metric_artifacts_list = Task.get_metrics_artifacts(metric_id)
metric_artifacts_list = Task.get_metrics_artifact_without_load(metric_id)
for metric_artifact in metric_artifacts_list:
metric_prediction_type = metric_artifact.prediction_type
if (
Expand Down
4 changes: 2 additions & 2 deletions tests/library/test_artifact.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,12 @@ def test_artifact_link_from_within_overwrites(self):
actual_metrics_of_task_as_is = []
for metric_id in task_as_is.metrics:
actual_metrics_of_task_as_is.extend(
Task.get_metrics_artifacts(metric_id)
Task.get_metrics_artifact_without_load(metric_id)
)
actual_metrics_of_task_indirect = []
for metric_id in task_indirect.metrics:
actual_metrics_of_task_indirect.extend(
Task.get_metrics_artifacts(metric_id)
Task.get_metrics_artifact_without_load(metric_id)
)

actual_metrics_to_dict_of_task_as_is = [
Expand Down

0 comments on commit 4d50008

Please sign in to comment.