From 9d153a403abc49cbfd9258579835d98608b2d1ed Mon Sep 17 00:00:00 2001 From: Brandon <132288221+brandon-groundlight@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:43:03 -0800 Subject: [PATCH] Internal function bugfix (#296) Considering a case where ask_async returned image queries have no result --------- Co-authored-by: Auto-format Bot --- pyproject.toml | 2 +- src/groundlight/internalapi.py | 4 ++++ test/unit/test_internalapi.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/unit/test_internalapi.py diff --git a/pyproject.toml b/pyproject.toml index 8bdb11d7..4e2eaebb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,7 +9,7 @@ packages = [ {include = "**/*.py", from = "src"}, ] readme = "README.md" -version = "0.21.0" +version = "0.21.1" [tool.poetry.dependencies] # For certifi, use ">=" instead of "^" since it upgrades its "major version" every year, not really following semver diff --git a/src/groundlight/internalapi.py b/src/groundlight/internalapi.py index 18f2f3a7..bff192cd 100644 --- a/src/groundlight/internalapi.py +++ b/src/groundlight/internalapi.py @@ -65,6 +65,8 @@ def iq_is_confident(iq: ImageQuery, confidence_threshold: float) -> bool: The only subtlety here is that currently confidence of None means human label, which is treated as confident. """ + if not iq.result: + return False return iq.result.confidence >= confidence_threshold # type: ignore @@ -72,6 +74,8 @@ def iq_is_answered(iq: ImageQuery) -> bool: """Returns True if the image query has a ML or human label. Placeholder and special labels (out of domain) have confidences exactly 0.5 """ + if not iq.result: + return False if (iq.result.source == Source.STILL_PROCESSING) or (iq.result.source is None): # Should never be None return False return True diff --git a/test/unit/test_internalapi.py b/test/unit/test_internalapi.py new file mode 100644 index 00000000..fc207509 --- /dev/null +++ b/test/unit/test_internalapi.py @@ -0,0 +1,14 @@ +from groundlight import ExperimentalApi +from groundlight.internalapi import iq_is_answered, iq_is_confident + + +def test_iq_is_confident(gl_experimental: ExperimentalApi): + det = gl_experimental.get_or_create_detector("Test", "test_query") + iq = gl_experimental.ask_async(det, image="test/assets/dog.jpeg", wait=10) + assert not iq_is_confident(iq, 0.9) + + +def test_iq_is_answered(gl_experimental: ExperimentalApi): + det = gl_experimental.get_or_create_detector("Test", "test_query") + iq = gl_experimental.ask_async(det, image="test/assets/dog.jpeg", wait=10) + assert not iq_is_answered(iq)