|
| 1 | +import numpy as np |
| 2 | +import pytest |
1 | 3 | from Granny.Analyses.BlushColor import BlushColor |
2 | 4 |
|
3 | 5 |
|
4 | 6 | def test_BlushColorInstantiation(): |
5 | | - """Test that BlushColor can be instantiated""" |
6 | 7 | analysis = BlushColor() |
7 | 8 | assert analysis is not None |
8 | 9 | assert analysis.__analysis_name__ == "blush" |
9 | 10 |
|
10 | 11 |
|
11 | 12 | def test_BlushColorInputImages(): |
12 | | - """Test that BlushColor input_images can be set""" |
13 | 13 | analysis = BlushColor() |
14 | 14 | analysis.input_images.setValue("demo/pear_images/full_masked_images") |
15 | 15 | assert analysis.input_images.getValue() == "demo/pear_images/full_masked_images" |
| 16 | + |
| 17 | + |
| 18 | +# --------------------------------------------------------------------------- |
| 19 | +# Default parameter values |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | + |
| 22 | +def test_default_threshold_is_148(): |
| 23 | + assert BlushColor().threshold.getValue() == 148 |
| 24 | + |
| 25 | + |
| 26 | +def test_default_fruit_threshold_is_140(): |
| 27 | + assert BlushColor().fruit_threshold.getValue() == 140 |
| 28 | + |
| 29 | + |
| 30 | +def test_default_blush_color(): |
| 31 | + a = BlushColor() |
| 32 | + assert a.blush_color_r.getValue() == 150 |
| 33 | + assert a.blush_color_g.getValue() == 55 |
| 34 | + assert a.blush_color_b.getValue() == 50 |
| 35 | + |
| 36 | + |
| 37 | +def test_default_text_position(): |
| 38 | + a = BlushColor() |
| 39 | + assert a.text_x.getValue() == 20 |
| 40 | + assert a.text_y.getValue() == 50 |
| 41 | + |
| 42 | + |
| 43 | +def test_default_font_scale_is_1(): |
| 44 | + assert BlushColor().font_scale.getValue() == pytest.approx(1.0) |
| 45 | + |
| 46 | + |
| 47 | +def test_default_text_thickness_is_3(): |
| 48 | + assert BlushColor().text_thickness.getValue() == 3 |
| 49 | + |
| 50 | + |
| 51 | +def test_threshold_boundary_values(): |
| 52 | + a = BlushColor() |
| 53 | + a.threshold.setValue(0) |
| 54 | + assert a.threshold.getValue() == 0 |
| 55 | + a.threshold.setValue(255) |
| 56 | + assert a.threshold.getValue() == 255 |
| 57 | + |
| 58 | + |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | +# _calculateBlush with synthetic images |
| 61 | +# --------------------------------------------------------------------------- |
| 62 | + |
| 63 | +def _make_bgr(r, g, b, size=50): |
| 64 | + img = np.zeros((size, size, 3), dtype=np.uint8) |
| 65 | + img[:, :, 0] = b |
| 66 | + img[:, :, 1] = g |
| 67 | + img[:, :, 2] = r |
| 68 | + return img |
| 69 | + |
| 70 | + |
| 71 | +def test_calculate_blush_returns_ratio_between_0_and_1(): |
| 72 | + a = BlushColor() |
| 73 | + img = _make_bgr(200, 100, 100) |
| 74 | + ratio, _ = a._calculateBlush(img) |
| 75 | + assert 0.0 <= ratio <= 1.0 |
| 76 | + |
| 77 | + |
| 78 | +def test_calculate_blush_output_shape_matches_input(): |
| 79 | + a = BlushColor() |
| 80 | + img = _make_bgr(200, 100, 100) |
| 81 | + _, result = a._calculateBlush(img) |
| 82 | + assert result.shape == img.shape |
| 83 | + |
| 84 | + |
| 85 | +def test_calculate_blush_threshold_affects_ratio(): |
| 86 | + img = _make_bgr(200, 150, 100) |
| 87 | + low_a = BlushColor() |
| 88 | + low_a.threshold.setValue(50) |
| 89 | + high_a = BlushColor() |
| 90 | + high_a.threshold.setValue(200) |
| 91 | + low_ratio, _ = low_a._calculateBlush(img) |
| 92 | + high_ratio, _ = high_a._calculateBlush(img) |
| 93 | + assert low_ratio != high_ratio |
| 94 | + |
| 95 | + |
| 96 | +def test_calculate_blush_all_black_image(): |
| 97 | + a = BlushColor() |
| 98 | + img = np.zeros((50, 50, 3), dtype=np.uint8) |
| 99 | + ratio, result = a._calculateBlush(img) |
| 100 | + assert result.shape == img.shape |
| 101 | + |
| 102 | + |
| 103 | +def test_calculate_blush_returns_float(): |
| 104 | + a = BlushColor() |
| 105 | + img = _make_bgr(180, 100, 80) |
| 106 | + ratio, _ = a._calculateBlush(img) |
| 107 | + assert isinstance(ratio, float) |
0 commit comments