-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
333 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from sqlalchemy import alias, select | ||
from sqlalchemy.orm import Session | ||
|
||
from valor_api import enums, schemas | ||
from valor_api.backend import core, models | ||
from valor_api.backend.metrics.metric_utils import ( | ||
create_metric_mappings, | ||
get_or_create_row, | ||
log_evaluation_duration, | ||
log_evaluation_item_counts, | ||
prepare_filter_for_evaluation, | ||
validate_computation, | ||
) | ||
|
||
|
||
def _compute_embedding_metrics( | ||
db: Session, | ||
): | ||
queries = select(models.Embedding).where().subquery() | ||
ref = select(models.Embedding).where().subquery() | ||
ref_alias = alias(ref) | ||
|
||
ref_dist = db.scalars( | ||
select(ref.c.value.cosine_distance(ref_alias.c.value)) | ||
.select_from(ref) | ||
.join(ref_alias, ref_alias.c.id != ref.c.id) | ||
) | ||
query_dist = db.scalars( | ||
select(ref.c.value.cosine_distance(queries.c.value)) | ||
.select_from(ref) | ||
.join( | ||
queries, | ||
isouter=True, | ||
) | ||
) | ||
|
||
|
||
@validate_computation | ||
def compute_embedding_metrics( | ||
*, | ||
db: Session, | ||
evaluation_id: int, | ||
) -> int: | ||
""" | ||
Create classification metrics. This function is intended to be run using FastAPI's `BackgroundTasks`. | ||
Parameters | ||
---------- | ||
db : Session | ||
The database Session to query against. | ||
evaluation_id : int | ||
The job ID to create metrics for. | ||
Returns | ||
---------- | ||
int | ||
The evaluation job id. | ||
""" | ||
|
||
# fetch evaluation | ||
evaluation = core.fetch_evaluation_from_id(db, evaluation_id) | ||
|
||
# unpack filters and params | ||
parameters = schemas.EvaluationParameters(**evaluation.parameters) | ||
groundtruth_filter, prediction_filter = prepare_filter_for_evaluation( | ||
db=db, | ||
filters=schemas.Filter(**evaluation.filters), | ||
dataset_names=evaluation.dataset_names, | ||
model_name=evaluation.model_name, | ||
task_type=parameters.task_type, | ||
label_map=parameters.label_map, | ||
) | ||
|
||
log_evaluation_item_counts( | ||
db=db, | ||
evaluation=evaluation, | ||
prediction_filter=prediction_filter, | ||
groundtruth_filter=groundtruth_filter, | ||
) | ||
|
||
if parameters.metrics_to_return is None: | ||
raise RuntimeError("Metrics to return should always be defined here.") | ||
|
||
metrics = _compute_embedding_metrics( | ||
db=db, | ||
prediction_filter=prediction_filter, | ||
groundtruth_filter=groundtruth_filter, | ||
label_map=parameters.label_map, | ||
pr_curve_max_examples=( | ||
parameters.pr_curve_max_examples | ||
if parameters.pr_curve_max_examples | ||
else 0 | ||
), | ||
metrics_to_return=parameters.metrics_to_return, | ||
) | ||
|
||
metric_mappings = create_metric_mappings( | ||
db=db, | ||
metrics=metrics, | ||
evaluation_id=evaluation.id, | ||
) | ||
|
||
for mapping in metric_mappings: | ||
# ignore value since the other columns are unique identifiers | ||
# and have empirically noticed value can slightly change due to floating | ||
# point errors | ||
get_or_create_row( | ||
db, | ||
models.Metric, | ||
mapping, | ||
columns_to_ignore=["value"], | ||
) | ||
|
||
log_evaluation_duration( | ||
evaluation=evaluation, | ||
db=db, | ||
) | ||
|
||
return evaluation_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.