Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions mteb/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections import defaultdict
from collections.abc import Sequence
from pathlib import Path
from typing import cast
from typing import Any, cast

from mteb.abstasks import AbsTask
from mteb.models import ModelMeta
Expand Down Expand Up @@ -465,7 +465,7 @@ def _filter_paths_by_task(
def load_results(
self,
models: Sequence[str] | Sequence[ModelMeta] | None = None,
tasks: Sequence[str] | Sequence[AbsTask] | None = None,
tasks: Sequence[str] | Sequence[AbsTask] | Any | None = None,
Comment thread
ayush1298 marked this conversation as resolved.
Outdated
require_model_meta: bool = True,
include_remote: bool = True,
validate_and_filter: bool = False,
Expand Down Expand Up @@ -497,6 +497,13 @@ def load_results(
... require_model_meta=True,
... )
"""
from mteb.benchmarks.benchmark import Benchmark
Comment thread
Samoed marked this conversation as resolved.
Outdated

benchmark = None
if tasks is not None and isinstance(tasks, Benchmark):
benchmark = tasks
tasks = benchmark.tasks
Comment thread
Samoed marked this conversation as resolved.
Outdated

paths = self.get_cache_paths(
models=models,
tasks=tasks,
Expand Down Expand Up @@ -546,6 +553,7 @@ def load_results(

benchmark_results = BenchmarkResults(
model_results=models_results,
benchmark=benchmark,
Comment thread
Samoed marked this conversation as resolved.
Outdated
Comment thread
Samoed marked this conversation as resolved.
Outdated
)

return benchmark_results
76 changes: 75 additions & 1 deletion mteb/results/benchmark_results.py
Comment thread
Samoed marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import warnings
from collections.abc import Callable, Iterable, Iterator, Sequence
from pathlib import Path
from typing import Any, Literal
from typing import TYPE_CHECKING, Any, Literal

import pandas as pd
from packaging.version import InvalidVersion, Version
from pydantic import BaseModel, ConfigDict
from typing_extensions import Self

if TYPE_CHECKING:
pass

Comment thread
ayush1298 marked this conversation as resolved.
Outdated
from mteb.abstasks.abstask import AbsTask
from mteb.abstasks.task_metadata import (
TaskDomain,
Expand Down Expand Up @@ -39,6 +42,7 @@ class BenchmarkResults(BaseModel):
"""

model_results: list[ModelResult]
benchmark: Any | None = None
Comment thread
ayush1298 marked this conversation as resolved.
Outdated
model_config = (
ConfigDict( # to free up the name model_results which is otherwise protected
protected_namespaces=(),
Expand Down Expand Up @@ -362,6 +366,65 @@ def to_dataframe(
format=format,
)

def get_benchmark_result(self) -> dict[str, float]:
Comment thread
Samoed marked this conversation as resolved.
Outdated
"""Get aggregated scores for each model in the benchmark.

Uses the benchmark's summary table creation method to compute scores.

Returns:
A dictionary mapping model names to their benchmark scores.

Raises:
ValueError: If no benchmark is associated with these results.

Examples:
>>> results = cache.load_results(
... models=["intfloat/e5-small"],
... tasks=mteb.get_benchmark("MTEB(eng)")
... )
>>> scores = results.get_benchmark_result()
>>> # {"intfloat/e5-small": 0.5}
Comment thread
ayush1298 marked this conversation as resolved.
Outdated
"""
if self.benchmark is None:
raise ValueError(
"No benchmark associated with these results. "
Comment thread
Samoed marked this conversation as resolved.
Outdated
"To get benchmark results, load results with a Benchmark object."
Comment thread
ayush1298 marked this conversation as resolved.
Outdated
)

summary_table = self.benchmark._create_summary_table(self)
if (
"No results" in summary_table.columns
or summary_table.empty
or "Model" not in summary_table.columns
):
return {}

model_scores = {}

score_column = None

if "Mean (Task)" in summary_table.columns:
score_column = "Mean (Task)"
elif "Mean (TaskType)" in summary_table.columns:
score_column = "Mean (TaskType)"
elif "Mean (Public)" in summary_table.columns:
score_column = "Mean (Public)"
elif "Mean (Subset)" in summary_table.columns:
score_column = "Mean (Subset)"

if score_column is None:
return {}

for _, row in summary_table.iterrows():
model_name = row["Model"]
if isinstance(model_name, str) and "[" in model_name:
import re
Comment thread
ayush1298 marked this conversation as resolved.
Outdated

match = re.match(r"\[([^\]]+)\]", model_name)
if match:
model_name = match.group(1)
Comment thread
Samoed marked this conversation as resolved.
Outdated
Comment thread
Samoed marked this conversation as resolved.
Outdated
return model_scores
Comment thread
Samoed marked this conversation as resolved.
Outdated

def __iter__(self) -> Iterator[ModelResult]:
return iter(self.model_results)

Expand Down Expand Up @@ -493,3 +556,14 @@ def model_revisions(self) -> list[dict[str, str | None]]:
{"model_name": model_res.model_name, "revision": model_res.model_revision}
for model_res in self.model_results
]

@property
def benchmark_name(self) -> str | None:
Comment thread
ayush1298 marked this conversation as resolved.
Outdated
"""Get the name of the benchmark if one is associated.

Returns:
The benchmark name or None if no benchmark is associated.
"""
if self.benchmark is None:
return None
return self.benchmark.name
Comment thread
ayush1298 marked this conversation as resolved.
Outdated
Loading