Skip to content

Commit

Permalink
Merge pull request #60 from worldcoin/yichen1-ai-5169-add-validator-e…
Browse files Browse the repository at this point in the history
…rrors-for-specific-various-pupil-to-iris

finetuning P2I validator errors into dilation, constriction and offcenter
  • Loading branch information
ycbiometrics authored Jan 16, 2025
2 parents 37ad778 + 3d93a4c commit e0e4054
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
15 changes: 15 additions & 0 deletions src/iris/io/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ class PupilIrisPropertyEstimationError(Exception):

pass

class Pupil2IrisValidatorErrorDilation(Exception):
"""Pupil2IrisValidatorErrorDilation error class."""

pass

class Pupil2IrisValidatorErrorConstriction(Exception):
"""Pupil2IrisValidatorErrorConstriction error class."""

pass

class Pupil2IrisValidatorErrorOffcenter(Exception):
"""Pupil2IrisValidatorErrorOffcenter error class."""

pass

class GeometryEstimationError(Exception):
"""GeometryEstimation module Error class."""
Expand Down Expand Up @@ -122,3 +136,4 @@ class IRISPipelineError(Exception):
"""IRIS Pipeline module Error class."""

pass

24 changes: 14 additions & 10 deletions src/iris/nodes/validators/object_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,22 @@ def run(self, val_arguments: PupilToIrisProperty) -> None:
p2i_property (PupilToIrisProperty): Computation result.
Raises:
E.PupilIrisPropertyEstimationError: Raised if result isn't without previously specified boundaries.
E.Pupil2IrisValidatorErrorConstriction: Raised if pupil is constricted.
E.Pupil2IrisValidatorErrorDilation: Raised if pupil is dilated.
E.Pupil2IrisValidatorErrorOffcenter: Raised if pupil and iris are offcenter.
"""
if not (
self.params.min_allowed_diameter_ratio
<= val_arguments.pupil_to_iris_diameter_ratio
<= self.params.max_allowed_diameter_ratio
):
raise E.PupilIrisPropertyEstimationError(
f"p2i_property={val_arguments.pupil_to_iris_diameter_ratio} is not within [{self.params.min_allowed_diameter_ratio}, {self.params.max_allowed_diameter_ratio}]."

if val_arguments.pupil_to_iris_diameter_ratio < self.params.min_allowed_diameter_ratio:
raise E.Pupil2IrisValidatorErrorConstriction(
f"p2i_property={val_arguments.pupil_to_iris_diameter_ratio} is below min threshold {self.params.min_allowed_diameter_ratio}. Pupil is too constricted."
)
if val_arguments.pupil_to_iris_diameter_ratio > self.params.max_allowed_diameter_ratio:
raise E.Pupil2IrisValidatorErrorDilation(
f"p2i_property={val_arguments.pupil_to_iris_diameter_ratio} is above max threshold {self.params.max_allowed_diameter_ratio}. Pupil is too dilated."
)
if val_arguments.pupil_to_iris_center_dist_ratio > self.params.max_allowed_center_dist_ratio:
raise E.PupilIrisPropertyEstimationError(
f"p2i_property={val_arguments.pupil_to_iris_center_dist_ratio} exceeds {self.params.max_allowed_center_dist_ratio}."
raise E.Pupil2IrisValidatorErrorOffcenter(
f"p2i_property={val_arguments.pupil_to_iris_center_dist_ratio} exceeds {self.params.max_allowed_center_dist_ratio}. Pupil and iris are off-center."
)

def on_execute_end(self, result: PupilToIrisProperty) -> None:
Expand Down Expand Up @@ -408,3 +411,4 @@ def on_execute_end(self, input_template: IrisTemplate, *args, **kwargs) -> None:
input_template (IrisTemplate): input IrisTemplate to be validated.
"""
self.run(input_template)

14 changes: 7 additions & 7 deletions tests/unit_tests/nodes/validators/test_object_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,21 @@ def test_pupil_to_iris_property_validator(p2i_property: float) -> None:


@pytest.mark.parametrize(
"p2i_property",
"p2i_property, expected_error",
[
PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.9, pupil_to_iris_center_dist_ratio=0.1),
PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.19, pupil_to_iris_center_dist_ratio=0.1),
PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.51, pupil_to_iris_center_dist_ratio=0.1),
PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.2, pupil_to_iris_center_dist_ratio=0.8),
(PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.9, pupil_to_iris_center_dist_ratio=0.1), E.Pupil2IrisValidatorErrorDilation),
(PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.19, pupil_to_iris_center_dist_ratio=0.1), E.Pupil2IrisValidatorErrorConstriction),
(PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.51, pupil_to_iris_center_dist_ratio=0.1), E.Pupil2IrisValidatorErrorDilation),
(PupilToIrisProperty(pupil_to_iris_diameter_ratio=0.2, pupil_to_iris_center_dist_ratio=0.8), E.Pupil2IrisValidatorErrorOffcenter),
],
ids=["simple", "edge case: left boundary", "edge case: right boundary", "center distance too big"],
)
def test_pupil_to_iris_property_validator_raise_exception1(p2i_property: float) -> None:
def test_pupil_to_iris_property_validator_raise_exception1(p2i_property: float, expected_error: Exception) -> None:
validator = obj_v.Pupil2IrisPropertyValidator(
min_allowed_diameter_ratio=0.2, max_allowed_diameter_ratio=0.5, max_allowed_center_dist_ratio=0.5
)

with pytest.raises(E.PupilIrisPropertyEstimationError):
with pytest.raises(expected_error):
validator(p2i_property)


Expand Down

0 comments on commit e0e4054

Please sign in to comment.