constructelligence commited on
Commit
0e1b9dd
·
verified ·
1 Parent(s): 4fc57ba

Upload test_completion.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. test_completion.py +71 -0
test_completion.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Tests for completion.py (numpy + scipy). Run: python3 test_completion.py"""
3
+ import numpy as np
4
+
5
+ from completion import completion_summary
6
+
7
+
8
+ def wall_scene():
9
+ """80x80 frame, wall everywhere but a 10 px border; all painted at P=0.97."""
10
+ wall = np.zeros((80, 80), bool)
11
+ wall[10:70, 10:70] = True
12
+ painted = wall.copy()
13
+ probability = np.where(wall, 0.97, 0.0)
14
+ return wall, painted, probability
15
+
16
+
17
+ def test_fully_painted_wall_is_complete():
18
+ wall, painted, probability = wall_scene()
19
+ summary, confident, touch_up = completion_summary(wall, painted, probability, threshold=0.9)
20
+ assert summary["status"] == "complete" and summary["confident_painted_fraction"] == 1.0
21
+ assert summary["touch_up_regions"] == [] and confident.sum() == wall.sum() and not touch_up.any()
22
+
23
+
24
+ def test_hesitant_paint_is_not_done_and_becomes_a_touch_up_region():
25
+ wall, painted, probability = wall_scene()
26
+ probability[20:40, 20:40] = 0.6 # argmax says painted, but not confidently
27
+ summary, confident, touch_up = completion_summary(wall, painted, probability, threshold=0.9)
28
+ assert summary["argmax_painted_fraction"] == 1.0 # the old number would sign this off
29
+ assert summary["status"] == "incomplete"
30
+ region = summary["touch_up_regions"][0]
31
+ assert region["bbox_xyxy"] == [20, 20, 39, 39] and region["pixels"] == 400
32
+ assert region["low_confidence_painted_px"] == 400 and region["unpainted_px"] == 0
33
+ assert touch_up.sum() == 400 and not confident[20:40, 20:40].any()
34
+
35
+
36
+ def test_regions_sorted_largest_first_and_small_specks_dropped():
37
+ wall, painted, probability = wall_scene()
38
+ painted[12:30, 50:68] = False # 324 px unpainted
39
+ painted[50:60, 15:25] = False # 100 px unpainted
40
+ painted[40, 40] = False # 1 px speck
41
+ uncertain = np.zeros_like(wall)
42
+ uncertain[50:55, 15:25] = True # half of the second region is "unsure"
43
+ summary, _, _ = completion_summary(wall, painted, probability, threshold=0.9, uncertain=uncertain)
44
+ sizes = [r["pixels"] for r in summary["touch_up_regions"]]
45
+ assert sizes == [324, 100] and summary["touch_up_regions_total"] == 2
46
+ assert summary["touch_up_regions"][1]["uncertain_px"] == 50
47
+ assert summary["touch_up_regions"][1]["unpainted_px"] == 50
48
+
49
+
50
+ def test_without_threshold_nothing_is_signed_off():
51
+ wall, painted, probability = wall_scene()
52
+ summary, confident, _ = completion_summary(wall, painted, probability, threshold=None)
53
+ assert summary["status"] == "not_calibrated" and not confident.any()
54
+
55
+
56
+ def test_no_wall():
57
+ empty = np.zeros((20, 20), bool)
58
+ summary, confident, touch_up = completion_summary(empty, empty, np.zeros((20, 20)), threshold=0.9)
59
+ assert summary["status"] == "no_wall_detected" and not confident.any() and not touch_up.any()
60
+
61
+
62
+ def main():
63
+ tests = [value for name, value in sorted(globals().items()) if name.startswith("test_")]
64
+ for test in tests:
65
+ test()
66
+ print(f"ok {test.__name__}")
67
+ print(f"{len(tests)} tests passed")
68
+
69
+
70
+ if __name__ == "__main__":
71
+ main()