Skip to content
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
2 changes: 1 addition & 1 deletion Granny/Models/Values/BoolValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, name: str, label: str, help: str):
super().__init__(name, label, help)
self.type = bool

def validate(self) -> bool:
def validate(self, value=None) -> bool:
"""
{@inheritdoc}
"""
Expand Down
96 changes: 94 additions & 2 deletions tests/test_Analyses/test_BlushColor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,107 @@
import numpy as np
import pytest
from Granny.Analyses.BlushColor import BlushColor


def test_BlushColorInstantiation():
"""Test that BlushColor can be instantiated"""
analysis = BlushColor()
assert analysis is not None
assert analysis.__analysis_name__ == "blush"


def test_BlushColorInputImages():
"""Test that BlushColor input_images can be set"""
analysis = BlushColor()
analysis.input_images.setValue("demo/pear_images/full_masked_images")
assert analysis.input_images.getValue() == "demo/pear_images/full_masked_images"


# ---------------------------------------------------------------------------
# Default parameter values
# ---------------------------------------------------------------------------

def test_default_threshold_is_148():
assert BlushColor().threshold.getValue() == 148


def test_default_fruit_threshold_is_140():
assert BlushColor().fruit_threshold.getValue() == 140


def test_default_blush_color():
a = BlushColor()
assert a.blush_color_r.getValue() == 150
assert a.blush_color_g.getValue() == 55
assert a.blush_color_b.getValue() == 50


def test_default_text_position():
a = BlushColor()
assert a.text_x.getValue() == 20
assert a.text_y.getValue() == 50


def test_default_font_scale_is_1():
assert BlushColor().font_scale.getValue() == pytest.approx(1.0)


def test_default_text_thickness_is_3():
assert BlushColor().text_thickness.getValue() == 3


def test_threshold_boundary_values():
a = BlushColor()
a.threshold.setValue(0)
assert a.threshold.getValue() == 0
a.threshold.setValue(255)
assert a.threshold.getValue() == 255


# ---------------------------------------------------------------------------
# _calculateBlush with synthetic images
# ---------------------------------------------------------------------------

def _make_bgr(r, g, b, size=50):
img = np.zeros((size, size, 3), dtype=np.uint8)
img[:, :, 0] = b
img[:, :, 1] = g
img[:, :, 2] = r
return img


def test_calculate_blush_returns_ratio_between_0_and_1():
a = BlushColor()
img = _make_bgr(200, 100, 100)
ratio, _ = a._calculateBlush(img)
assert 0.0 <= ratio <= 1.0


def test_calculate_blush_output_shape_matches_input():
a = BlushColor()
img = _make_bgr(200, 100, 100)
_, result = a._calculateBlush(img)
assert result.shape == img.shape


def test_calculate_blush_threshold_affects_ratio():
img = _make_bgr(200, 150, 100)
low_a = BlushColor()
low_a.threshold.setValue(50)
high_a = BlushColor()
high_a.threshold.setValue(200)
low_ratio, _ = low_a._calculateBlush(img)
high_ratio, _ = high_a._calculateBlush(img)
assert low_ratio != high_ratio


def test_calculate_blush_all_black_image():
a = BlushColor()
img = np.zeros((50, 50, 3), dtype=np.uint8)
ratio, result = a._calculateBlush(img)
assert result.shape == img.shape


def test_calculate_blush_returns_float():
a = BlushColor()
img = _make_bgr(180, 100, 80)
ratio, _ = a._calculateBlush(img)
assert isinstance(ratio, float)
92 changes: 90 additions & 2 deletions tests/test_Analyses/test_PeelColor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,103 @@
import numpy as np
import pytest
from Granny.Analyses.PeelColor import PeelColor


def test_PeelColorInstantiation():
"""Test that PeelColor can be instantiated"""
analysis = PeelColor()
assert analysis is not None
assert analysis.__analysis_name__ == "color"


def test_PeelColorInputImages():
"""Test that PeelColor input_images can be set"""
analysis = PeelColor()
analysis.input_images.setValue("demo/pear_images/full_masked_images")
assert analysis.input_images.getValue() == "demo/pear_images/full_masked_images"


# ---------------------------------------------------------------------------
# Default parameter values
# ---------------------------------------------------------------------------

def test_default_purple_threshold_is_126():
assert PeelColor().purple_threshold.getValue() == 126


def test_default_lightness_range():
a = PeelColor()
assert a.lightness_min.getValue() == 0
assert a.lightness_max.getValue() == 255


def test_default_green_range():
a = PeelColor()
assert a.green_min.getValue() == 0
assert a.green_max.getValue() == 128


def test_default_yellow_range():
a = PeelColor()
assert a.yellow_min.getValue() == 128
assert a.yellow_max.getValue() == 255


def test_default_normalize_lightness_is_50():
assert PeelColor().normalize_lightness.getValue() == 50


def test_mean_values_length_match():
a = PeelColor()
assert len(a.MEAN_VALUES_A) == len(a.MEAN_VALUES_B) == len(a.SCORE)


def test_line_points_are_2d():
a = PeelColor()
assert a.LINE_POINT_1.shape == (2,)
assert a.LINE_POINT_2.shape == (2,)


# ---------------------------------------------------------------------------
# remove_purple
# ---------------------------------------------------------------------------

def test_remove_purple_output_shape():
a = PeelColor()
img = np.full((50, 50, 3), 128, dtype=np.uint8)
result = a.remove_purple(img)
assert result.shape == img.shape


def test_remove_purple_does_not_modify_original():
a = PeelColor()
img = np.full((50, 50, 3), 100, dtype=np.uint8)
original = img.copy()
a.remove_purple(img)
np.testing.assert_array_equal(img, original)


# ---------------------------------------------------------------------------
# calculate_bin_distance
# ---------------------------------------------------------------------------

def test_calculate_bin_distance_euclidean_returns_valid_bin():
a = PeelColor()
bin_num, dist = a.calculate_bin_distance([-20.0, 70.0], method="Euclidean")
assert 1 <= bin_num <= len(a.SCORE)


def test_calculate_bin_distance_score_method():
a = PeelColor()
bin_num, dist = a.calculate_bin_distance([0.6], method="Score")
assert 1 <= bin_num <= len(a.SCORE)


def test_calculate_bin_distance_x_component():
a = PeelColor()
bin_num, _ = a.calculate_bin_distance([-20.0, 70.0], method="X-component")
assert 1 <= bin_num <= len(a.SCORE)


def test_calculate_bin_distance_y_component():
a = PeelColor()
bin_num, _ = a.calculate_bin_distance([-20.0, 70.0], method="Y-component")
assert 1 <= bin_num <= len(a.SCORE)
63 changes: 62 additions & 1 deletion tests/test_Analyses/test_Segmentation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
import pytest
from Granny.Analyses.Segmentation import Segmentation


def test_performAnalysis():
analysis = Segmentation()



# ---------------------------------------------------------------------------
# Default parameter values
# ---------------------------------------------------------------------------

def test_default_conf_threshold_is_0_7():
assert Segmentation().conf_threshold.getValue() == pytest.approx(0.7)


def test_default_iou_threshold():
assert Segmentation().iou_threshold.getValue() == pytest.approx(0.45)


def test_default_font_scale():
s = Segmentation()
assert s.font_scale.getValue() == pytest.approx(2.0)


def test_default_text_thickness_is_3():
assert Segmentation().text_thickness.getValue() == 3


def test_default_text_color_is_black():
s = Segmentation()
assert s.text_color_r.getValue() == 0
assert s.text_color_g.getValue() == 0
assert s.text_color_b.getValue() == 0


def test_text_color_boundary_values():
s = Segmentation()
s.text_color_r.setValue(255)
s.text_color_g.setValue(255)
s.text_color_b.setValue(255)
assert s.text_color_r.getValue() == 255
assert s.text_color_g.getValue() == 255
assert s.text_color_b.getValue() == 255


def test_conf_threshold_range():
s = Segmentation()
s.conf_threshold.setValue(0.0)
assert s.conf_threshold.getValue() == pytest.approx(0.0)
s.conf_threshold.setValue(1.0)
assert s.conf_threshold.getValue() == pytest.approx(1.0)


def test_qr_detector_initialized():
from Granny.Utils.QRCodeDetector import QRCodeDetector
s = Segmentation()
assert isinstance(s.qr_detector, QRCodeDetector)


def test_variety_info_initially_none():
assert Segmentation().variety_info is None


def test_analysis_name_is_segmentation():
assert Segmentation().__analysis_name__ == "segmentation"
Loading
Loading