Skip to content

adds confidence chart #10

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

Merged
merged 7 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion business_objects/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def commit() -> None:
session.commit()



def remove_and_refresh_session(
session_token: Any, request_new: bool = False
) -> Union[Any, None]:
Expand Down Expand Up @@ -70,6 +69,10 @@ def execute_distinct_count(count_sql: str) -> int:
return session.execute(count_sql).first().distinct_count


def set_seed(seed: float = 0) -> None:
execute(f"SELECT setseed({seed});")


def get_bind() -> Any:
return session.get_bind()

Expand Down
63 changes: 61 additions & 2 deletions business_objects/project.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from typing import List, Optional, Any, Dict, Union
from sqlalchemy.sql import func

from . import general

from .. import enums
from ..session import session
from ..models import Project
from ..models import (
DataSliceRecordAssociation,
LabelingTask,
LabelingTaskLabel,
Project,
RecordLabelAssociation,
)


def get(project_id: str) -> Project:
Expand Down Expand Up @@ -60,6 +68,57 @@ def get_label_distribution(
return values[0]


def get_confidence_distribution(
project_id: str,
labeling_task_id: str,
data_slice_id: Optional[str] = None,
num_samples: Optional[int] = None,
) -> List[float]:
query_filter = (
session.query(RecordLabelAssociation.confidence)
.join(
LabelingTaskLabel,
(RecordLabelAssociation.labeling_task_label_id == LabelingTaskLabel.id)
& (LabelingTaskLabel.project_id == RecordLabelAssociation.project_id),
)
.join(
LabelingTask,
(LabelingTask.id == LabelingTaskLabel.labeling_task_id)
& (LabelingTask.project_id == LabelingTaskLabel.project_id),
)
.filter(
RecordLabelAssociation.project_id == project_id,
LabelingTask.id == labeling_task_id,
RecordLabelAssociation.source_type
== enums.LabelSource.WEAK_SUPERVISION.value,
RecordLabelAssociation.project_id == project_id,
)
)

if data_slice_id is not None:
query_filter = query_filter.join(
DataSliceRecordAssociation,
(DataSliceRecordAssociation.record_id == RecordLabelAssociation.record_id)
& (
DataSliceRecordAssociation.project_id
== RecordLabelAssociation.project_id
),
).filter(
DataSliceRecordAssociation.data_slice_id == data_slice_id,
)

if num_samples is not None:
query_filter = query_filter.order_by(func.random()).limit(num_samples)
general.set_seed(0)
confidence_scores = [confidence for confidence, in (query_filter.all())]
confidence_scores = sorted(confidence_scores)
else:
query_filter = query_filter.order_by(RecordLabelAssociation.confidence.asc())
confidence_scores = [confidence for confidence, in (query_filter.all())]

return confidence_scores


def get_confusion_matrix(
project_id: str,
labeling_task_id: str,
Expand Down Expand Up @@ -130,7 +189,7 @@ def create(
created_by: str,
created_at: Optional[str] = None,
with_commit: bool = False,
status: enums.ProjectStatus = enums.ProjectStatus.INIT_UPLOAD
status: enums.ProjectStatus = enums.ProjectStatus.INIT_UPLOAD,
) -> Project:
project: Project = Project(
name=name,
Expand Down