From 5f99622646b3f6d3a842aebef91ff8e082072cd6 Mon Sep 17 00:00:00 2001 From: Julius Unverfehrt Date: Mon, 12 Sep 2022 08:47:02 +0200 Subject: [PATCH] RED-5202 add per class customizable max image to page quotient setting for signatures, default is 0.4. Can be overwritten by , set to null to use default value or set to value that should be used. --- config.yaml | 3 +++ image_prediction/config.py | 6 ++++++ .../transformer/transformers/response.py | 12 ++++++++++- test/unit_tests/config_test.py | 12 ++++++++++- test/unit_tests/response_transformer_test.py | 21 +++++++++++++++++++ 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/unit_tests/response_transformer_test.py diff --git a/config.yaml b/config.yaml index 6a6111a..23c9268 100644 --- a/config.yaml +++ b/config.yaml @@ -18,6 +18,9 @@ filters: image_to_page_quotient: # Image size to page size ratio (ratio of geometric means of areas) min: $MIN_REL_IMAGE_SIZE|0.05 # Minimum permissible max: $MAX_REL_IMAGE_SIZE|0.75 # Maximum permissible + customized: # Customized settings per class (RED-5202) + max: + signature: $MAX_REL_SIGNATURE_SIZE|0.4 image_width_to_height_quotient: # Image width to height ratio min: $MIN_IMAGE_FORMAT|0.1 # Minimum permissible diff --git a/image_prediction/config.py b/image_prediction/config.py index 4696191..26295dc 100644 --- a/image_prediction/config.py +++ b/image_prediction/config.py @@ -15,6 +15,12 @@ class DotIndexable: def __init__(self, x): self.x = x + def get(self, item, default=None): + try: + return _get_item_and_maybe_make_dotindexable(self.x, item) + except (KeyError, TypeError): + return default + def __getattr__(self, item): return _get_item_and_maybe_make_dotindexable(self.x, item) diff --git a/image_prediction/transformer/transformers/response.py b/image_prediction/transformer/transformers/response.py index 3e35104..b67825f 100644 --- a/image_prediction/transformer/transformers/response.py +++ b/image_prediction/transformer/transformers/response.py @@ -27,7 +27,9 @@ def build_image_info(data: dict) -> dict: quotient = round(compute_geometric_quotient(), 4) min_image_to_page_quotient_breached = bool(quotient < CONFIG.filters.image_to_page_quotient.min) - max_image_to_page_quotient_breached = bool(quotient > CONFIG.filters.image_to_page_quotient.max) + max_image_to_page_quotient_breached = is_max_image_to_page_quotient_breached( + quotient, data["classification"]["label"] + ) min_image_width_to_height_quotient_breached = bool( width / height < CONFIG.filters.image_width_to_height_quotient.min ) @@ -73,3 +75,11 @@ def build_image_info(data: dict) -> dict: } return image_info + + +def is_max_image_to_page_quotient_breached(quotient, label): + default_max_quotient = CONFIG.filters.image_to_page_quotient.max + customized_entries = CONFIG.filters.image_to_page_quotient.customized.max + max_quotient = customized_entries.get(label, default_max_quotient) + max_quotient = max_quotient if max_quotient else default_max_quotient + return bool(quotient > max_quotient) diff --git a/test/unit_tests/config_test.py b/test/unit_tests/config_test.py index 8c16cf8..e2a2c5e 100644 --- a/test/unit_tests/config_test.py +++ b/test/unit_tests/config_test.py @@ -3,7 +3,7 @@ import tempfile import pytest import yaml -from image_prediction.config import Config +from image_prediction.config import Config, DotIndexable @pytest.fixture @@ -36,3 +36,13 @@ def test_dot_access_key_does_not_exists(config): def test_access_key_does_not_exists(config): assert config["B"] is None + + +def test_get_method_returns_key_if_key_does_exist(config): + dot_indexable = config.D.E + assert dot_indexable.get("F", "default_value") == True + + +def test_get_method_returns_default_if_key_does_not_exist(config): + dot_indexable = config.D.E + assert dot_indexable.get("X", "default_value") == "default_value" diff --git a/test/unit_tests/response_transformer_test.py b/test/unit_tests/response_transformer_test.py new file mode 100644 index 0000000..59fb72d --- /dev/null +++ b/test/unit_tests/response_transformer_test.py @@ -0,0 +1,21 @@ +import pytest + +from image_prediction.transformer.transformers.response import is_max_image_to_page_quotient_breached + + +@pytest.fixture +def expected_is_breached(quotient, label): + if label == "signature" and quotient < 0.4: + return False + elif label == "signature" and quotient >= 0.4: + return True + elif quotient < 0.7: + return False + else: + return True + + +@pytest.mark.parametrize("quotient", [0.1, 0.5]) +@pytest.mark.parametrize("label", ["logo", "signature"]) +def test_customized_per_label_ratio_breach(quotient, label, expected_is_breached): + assert is_max_image_to_page_quotient_breached(quotient, label) == expected_is_breached