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

Don't wait for a confident answer if we know there won't be one #307

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions src/groundlight/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ImageQuery,
PaginatedDetectorList,
PaginatedImageQueryList,
Source,
)
from urllib3.exceptions import InsecureRequestWarning

Expand Down Expand Up @@ -970,6 +971,8 @@ def wait_for_confident_result(
2. The timeout_sec is reached
3. An error occurs

If the image query was answered on the edge or is done_processing, the result is returned immediately.

**Example usage**::

gl = Groundlight()
Expand Down Expand Up @@ -1002,9 +1005,29 @@ def wait_for_confident_result(
:meth:`get_image_query` for checking result status without blocking
:meth:`wait_for_ml_result` for waiting until the first ML result is available
"""

def should_stop_waiting(image_query: ImageQuery) -> bool:
"""Checks if the image query should be returned immediately because no better answer is expected."""
if image_query.done_processing:
logger.debug(
"The image query has completed escalating and will receive no new results, so we are "
"returning it immediately."
)
return True
if image_query.result and image_query.result.source and image_query.result.source == Source.EDGE:
logger.debug(
"The image query was answered on the edge, so we are returning it immediately and not waiting for "
"a confident result."
)
return True
return False

if isinstance(image_query, str):
image_query = self.get_image_query(image_query)

if should_stop_waiting(image_query):
return image_query

if confidence_threshold is None:
confidence_threshold = self.get_detector(image_query.detector_id).confidence_threshold

Expand Down
20 changes: 20 additions & 0 deletions test/unit/test_wait.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from unittest.mock import patch

from groundlight import ExperimentalApi
from model import BinaryClassificationResult, ImageQuery, Label, Source


def test_wait_for_confident_result_returns_immediately_when_no_better_result_expected(
gl_experimental: ExperimentalApi, initial_iq: ImageQuery
):
with patch.object(gl_experimental, "_wait_for_result") as mock_wait_for_result:
# Shouldn't wait if the image query is done processing
initial_iq.done_processing = True
gl_experimental.wait_for_confident_result(initial_iq)
mock_wait_for_result.assert_not_called()

# Shouldn't wait if the result is from the edge
initial_iq.done_processing = False
initial_iq.result = BinaryClassificationResult(source=Source.EDGE, label=Label.YES)
gl_experimental.wait_for_confident_result(initial_iq)
mock_wait_for_result.assert_not_called()
Loading