Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Evaluation Response Performance #661

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Changes from all commits
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
117 changes: 55 additions & 62 deletions api/valor_api/backend/core/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

from valor_api import api_utils, enums, exceptions, schemas
from valor_api.backend import core, models
from valor_api.backend.metrics.metric_utils import (
prepare_filter_for_evaluation,
)
from valor_api.backend.query import generate_query
from valor_api.schemas import migrations

Expand Down Expand Up @@ -127,35 +130,6 @@ def _create_bulk_expression(
return expr


def _convert_db_metric_to_pydantic_metric(
db: Session,
metric: models.Metric,
) -> schemas.Metric:
"""Apply schemas.Metric to a metric from the database"""

label_row = (
db.query(
select(models.Label)
.where(models.Label.id == metric.label_id)
.subquery()
).one_or_none()
if metric.label_id
else None
)
label = (
schemas.Label(key=label_row.key, value=label_row.value)
if label_row
else None
)

return schemas.Metric(
type=metric.type,
value=metric.value,
label=label,
parameters=metric.parameters,
)


def validate_request(
db: Session,
job_request: schemas.EvaluationRequest,
Expand Down Expand Up @@ -268,10 +242,6 @@ def _validate_evaluation_filter(
parameters = schemas.EvaluationParameters(**evaluation.parameters)

# generate filters
from valor_api.backend.metrics.metric_utils import (
prepare_filter_for_evaluation,
)

groundtruth_filter, prediction_filter = prepare_filter_for_evaluation(
db=db,
filters=filters,
Expand Down Expand Up @@ -314,44 +284,67 @@ def _create_response(
**kwargs,
) -> schemas.EvaluationResponse:
"""Converts a evaluation row into a response schema."""
metrics = db.query(
select(models.Metric)
.where(
and_(
models.Metric.evaluation_id == evaluation.id,
models.Metric.type.in_(
evaluation.parameters["metrics_to_return"]
),

metrics = [
schemas.Metric(
type=mtype,
value=mvalue,
label=(
schemas.Label(key=lkey, value=lvalue)
if lkey and lvalue
else None
),
parameters=mparam,
)
for mtype, mvalue, mparam, lkey, lvalue in (
db.query(
models.Metric.type,
models.Metric.value,
models.Metric.parameters,
models.Label.key,
models.Label.value,
)
.select_from(models.Metric)
.join(
models.Label,
models.Label.id == models.Metric.label_id,
isouter=True,
)
.where(
and_(
models.Metric.evaluation_id == evaluation.id,
models.Metric.type.in_(
evaluation.parameters["metrics_to_return"]
),
)
)
.all()
)
.subquery()
).all()
confusion_matrices = db.query(
select(models.ConfusionMatrix)
.where(models.ConfusionMatrix.evaluation_id == evaluation.id)
.subquery()
).all()
]

confusion_matrices = [
schemas.ConfusionMatrixResponse(
label_key=matrix.label_key,
entries=[
schemas.ConfusionMatrixEntry(**entry) for entry in matrix.value
],
)
for matrix in (
db.query(models.ConfusionMatrix)
.where(models.ConfusionMatrix.evaluation_id == evaluation.id)
.all()
)
]

return schemas.EvaluationResponse(
id=evaluation.id,
dataset_names=evaluation.dataset_names,
model_name=evaluation.model_name,
filters=evaluation.filters,
parameters=evaluation.parameters,
status=enums.EvaluationStatus(evaluation.status),
metrics=[
_convert_db_metric_to_pydantic_metric(db, metric)
for metric in metrics
],
confusion_matrices=[
schemas.ConfusionMatrixResponse(
label_key=matrix.label_key,
entries=[
schemas.ConfusionMatrixEntry(**entry)
for entry in matrix.value
],
)
for matrix in confusion_matrices
],
metrics=metrics,
confusion_matrices=confusion_matrices,
created_at=evaluation.created_at.replace(tzinfo=timezone.utc),
meta=evaluation.meta,
**kwargs,
Expand Down
Loading