junaid-simamdigital commited on
Commit
f5abc9f
·
verified ·
1 Parent(s): d1f380c

Test support-aware fusion pruning

Browse files
Files changed (1) hide show
  1. tests/test_core.py +362 -0
tests/test_core.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from pathlib import Path
3
+ from types import SimpleNamespace
4
+
5
+ from simam3d_core import (
6
+ analyze_light_field,
7
+ classify_scene_hypothesis,
8
+ classify_fusion_support,
9
+ deterministic_demo_depth,
10
+ estimate_depth_light,
11
+ filter_fusion_by_support,
12
+ default_intrinsics,
13
+ depth_to_camera_points,
14
+ fuse_point_views,
15
+ fusion_metrics,
16
+ input_diversity_metrics,
17
+ gaussian_initialization,
18
+ normalize_map,
19
+ normalize_prediction_contract,
20
+ next_view_plan,
21
+ orbit_project_points,
22
+ project_world_points,
23
+ reprojection_consistency,
24
+ prepare_intrinsics,
25
+ reveal_uncertainty,
26
+ synthetic_orbit_poses,
27
+ write_confidence_ply,
28
+ write_gaussian_ply,
29
+ )
30
+
31
+
32
+ def test_input_diversity_flags_duplicate_views_without_rejecting_batch():
33
+ duplicate = input_diversity_metrics(["a", "a", "b"])
34
+ assert duplicate["has_duplicate_input_views"] is True
35
+ assert duplicate["unique_input_view_count"] == 2
36
+ assert duplicate["duplicate_input_view_count"] == 1
37
+ assert duplicate["near_duplicate_pair_count"] == 0
38
+ assert duplicate["input_diversity_status"] == "duplicate_inputs"
39
+
40
+ distinct = input_diversity_metrics(["a", "b"])
41
+ assert distinct["has_duplicate_input_views"] is False
42
+ assert distinct["input_diversity_status"] == "distinct_inputs"
43
+ near = input_diversity_metrics(["a", "b"], near_duplicate_pair_count=1)
44
+ assert near["input_diversity_status"] == "near_duplicate_inputs"
45
+
46
+
47
+ def test_fusion_support_classification_is_explicit_about_view_count_and_agreement():
48
+ assert classify_fusion_support({}, 1) == "single_view_baseline"
49
+ assert classify_fusion_support({"multi_view_voxel_fraction": 0.9}, 2, 1) == "duplicate_input_support"
50
+ assert classify_fusion_support({"multi_view_voxel_fraction": 0.9}, 2, 2, "depth_anything_3", 1) == "near_duplicate_input_support"
51
+ assert classify_fusion_support({"multi_view_voxel_fraction": 0.9}, 2, 2, "synthetic_orbit_prior") == "synthetic_prior_support"
52
+ assert classify_fusion_support({"multi_view_voxel_fraction": 0.05}, 3) == "low_multi_view_support"
53
+ assert classify_fusion_support({"multi_view_voxel_fraction": 0.25}, 3) == "partial_multi_view_support"
54
+ assert classify_fusion_support({"multi_view_voxel_fraction": 0.75}, 3) == "strong_multi_view_support"
55
+
56
+
57
+ def test_light_field_detector_returns_bright_side_and_bounded_confidence():
58
+ image = np.zeros((8, 8, 3), dtype=np.uint8)
59
+ image[:, 6:] = 255
60
+ result = analyze_light_field(image)
61
+ assert result["bright_centroid_x"] > 0.2
62
+ assert result["detector_confidence"] >= 0.0
63
+ assert result["detector_confidence"] <= 1.0
64
+ assert "directional" in result["lighting_label"]
65
+
66
+
67
+ def test_depth_light_estimate_returns_normalized_camera_direction():
68
+ depth = np.tile(np.linspace(0.0, 1.0, 8, dtype=np.float32), (8, 1))
69
+ colors = np.zeros((8, 8, 3), dtype=np.uint8)
70
+ colors[:, 5:] = 255
71
+ result = estimate_depth_light(depth, colors)
72
+ direction = np.array([result["light_direction_x"], result["light_direction_y"], result["light_direction_z"]])
73
+ assert np.isfinite(direction).all()
74
+ assert np.isclose(np.linalg.norm(direction), 1.0)
75
+ assert 0.0 <= result["photometric_confidence"] <= 1.0
76
+
77
+
78
+ def test_deterministic_demo_depth_is_shape_stable_and_explicitly_numeric():
79
+ colors = np.array([[[0, 0, 0], [255, 255, 255]]], dtype=np.uint8)
80
+ depth = deterministic_demo_depth(colors)
81
+ assert depth.shape == (1, 2)
82
+ assert np.allclose(depth, [[255.0, 0.0]])
83
+
84
+
85
+ def test_scene_hypothesis_is_explicit_and_transparent():
86
+ result = classify_scene_hypothesis("I think a garden is behind the portrait")
87
+ assert result["user_guess"].startswith("I think")
88
+ assert "outdoor" in result["semantic_cues"]
89
+ assert "nature" in result["semantic_cues"]
90
+ assert "not verified" in result["status"]
91
+
92
+
93
+ def test_reveal_uncertainty_preserves_shape_and_confidence_signal():
94
+ depth = np.tile(np.linspace(0.0, 1.0, 8, dtype=np.float32), (8, 1))
95
+ confidence = np.ones_like(depth)
96
+ result = reveal_uncertainty(depth, confidence)
97
+ assert result.shape == depth.shape
98
+ assert np.isfinite(result).all()
99
+ assert float(result.max()) <= 1.0
100
+ assert float(result.mean()) < 0.1
101
+
102
+
103
+ def test_next_view_plan_is_deterministic_and_prefers_gaps():
104
+ plan = next_view_plan(1)
105
+ assert len(plan) == 5
106
+ assert plan[0]["gap_from_existing_deg"] >= plan[-1]["gap_from_existing_deg"]
107
+ assert all(item["reason"] == "largest unsupported azimuth" for item in plan)
108
+
109
+
110
+ def test_orbit_projection_rotates_points_without_nan():
111
+ points = np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]], dtype=np.float32)
112
+ xy, depth = orbit_project_points(points, 90.0)
113
+ assert xy.shape == (2, 2)
114
+ assert depth.shape == (2,)
115
+ assert np.isfinite(xy).all()
116
+ assert np.isfinite(depth).all()
117
+ assert np.all(np.abs(xy) <= 1.0)
118
+
119
+
120
+ def test_prediction_contract_normalizes_single_view_and_optional_fields():
121
+ prediction = SimpleNamespace(depth=np.ones((4, 5), dtype=np.float32), conf=None)
122
+ depth, confidence, extrinsics, intrinsics, confidence_source, pose_source = normalize_prediction_contract(prediction, 1)
123
+ assert depth.shape == (1, 4, 5)
124
+ assert confidence.shape == depth.shape
125
+ assert extrinsics.shape == (1, 4, 4)
126
+ assert intrinsics.shape == (1, 3, 3)
127
+ assert confidence_source == "baseline_uniform"
128
+ assert pose_source == "synthetic_orbit_prior"
129
+
130
+
131
+ def test_prediction_contract_rejects_mismatched_confidence():
132
+ prediction = SimpleNamespace(
133
+ depth=np.ones((2, 4, 5), dtype=np.float32),
134
+ conf=np.ones((1, 4, 5), dtype=np.float32),
135
+ )
136
+ try:
137
+ normalize_prediction_contract(prediction, 2)
138
+ except ValueError as exc:
139
+ assert "confidence" in str(exc)
140
+ else:
141
+ raise AssertionError("mismatched DA3 confidence should be rejected")
142
+
143
+
144
+ def test_world_projection_reports_in_frame_points():
145
+ points = np.array([[0.0, 0.0, 1.0], [2.0, 0.0, 1.0]], dtype=np.float32)
146
+ pixels, depths, inside = project_world_points(points, None, None, 10, 10)
147
+ assert pixels.shape == (2, 2)
148
+ assert np.allclose(pixels[0], [5.0, 5.0])
149
+ assert np.isclose(depths[0], 1.0)
150
+ assert inside.tolist() == [True, False]
151
+
152
+
153
+ def test_reprojection_consistency_detects_self_consistent_depth():
154
+ depth = np.tile(np.linspace(0.0, 1.0, 6, dtype=np.float32), (6, 1))
155
+ points, _ = depth_to_camera_points(depth)
156
+ metrics = reprojection_consistency(points, depth, None, None)
157
+ assert metrics["coverage"] > 0.8
158
+ assert metrics["mean_absolute_error"] < 0.05
159
+
160
+
161
+ def test_normalize_map_handles_constant_and_nan_values():
162
+ assert np.all(normalize_map(np.ones((2, 2))) == 0)
163
+ normalized = normalize_map(np.array([[np.nan, 1.0], [2.0, 3.0]], dtype=np.float32))
164
+ assert normalized.shape == (2, 2)
165
+ assert np.isfinite(normalized).all()
166
+ assert normalized.max() == 1.0
167
+
168
+
169
+ def test_depth_projection_has_expected_shape():
170
+ depth = np.ones((4, 5), dtype=np.float32)
171
+ points, weights = depth_to_camera_points(depth, default_intrinsics(5, 4))
172
+ assert points.shape == (20, 3)
173
+ assert weights.shape == (20,)
174
+ assert np.isfinite(points).all()
175
+
176
+
177
+ def test_relative_depth_projection_matches_exporter_convention():
178
+ depth = np.array([[0.0, 1.0]], dtype=np.float32)
179
+ points, _ = depth_to_camera_points(depth)
180
+ assert points[0, 2] > points[1, 2]
181
+
182
+
183
+ def test_fusion_is_deterministic_and_voxel_reduces_duplicates():
184
+ depth = np.ones((3, 3), dtype=np.float32)
185
+ colors = np.zeros((3, 3, 3), dtype=np.uint8)
186
+ views = [(depth, np.ones_like(depth), None, None, colors), (depth, np.ones_like(depth), None, None, colors)]
187
+ first = fuse_point_views(views, voxel_size=0.1)
188
+ second = fuse_point_views(views, voxel_size=0.1)
189
+ assert len(first.points) < 18
190
+ assert np.array_equal(first.points, second.points)
191
+ assert fusion_metrics(first)["point_count"] == len(first.points)
192
+ assert fusion_metrics(first)["dominant_source_view_count"] == 1
193
+ assert fusion_metrics(first)["supported_source_view_count"] == 2
194
+ assert fusion_metrics(first)["multi_view_voxel_fraction"] == 1.0
195
+
196
+
197
+ def test_filter_fusion_by_support_prunes_weak_lineage_and_preserves_default():
198
+ depth = np.ones((3, 3), dtype=np.float32)
199
+ colors = np.zeros((3, 3, 3), dtype=np.uint8)
200
+ views = [(depth, np.ones_like(depth), None, None, colors), (depth, np.ones_like(depth), None, None, colors)]
201
+ result = fuse_point_views(views, voxel_size=0.1)
202
+ assert filter_fusion_by_support(result, 1) is result
203
+ filtered = filter_fusion_by_support(result, 2)
204
+ assert len(filtered.points) == len(result.points)
205
+ assert all(int(mask).bit_count() >= 2 for mask in filtered.source_masks)
206
+
207
+
208
+ def test_extrinsics_move_camera_points_into_world_coordinates():
209
+ depth = np.ones((2, 2), dtype=np.float32)
210
+ colors = np.full((2, 2, 3), 127, dtype=np.uint8)
211
+ translation = np.eye(4, dtype=np.float32)
212
+ translation[0, 3] = 2.0
213
+ result = fuse_point_views([(depth, None, None, translation, colors)], voxel_size=0.0)
214
+ camera_points, _ = depth_to_camera_points(depth)
215
+ assert np.allclose(result.points[:, 0], camera_points[:, 0] - 2.0)
216
+
217
+
218
+ def test_zero_confidence_points_are_excluded():
219
+ depth = np.ones((2, 2), dtype=np.float32)
220
+ colors = np.zeros((2, 2, 3), dtype=np.uint8)
221
+ confidence = np.array([[1.0, 0.0], [0.0, 1.0]], dtype=np.float32)
222
+ result = fuse_point_views([(depth, confidence, None, None, colors)], voxel_size=0.0)
223
+ assert len(result.points) == 2
224
+
225
+
226
+ def test_constant_positive_confidence_remains_usable():
227
+ depth = np.ones((2, 2), dtype=np.float32)
228
+ colors = np.zeros((2, 2, 3), dtype=np.uint8)
229
+ result = fuse_point_views([(depth, np.ones_like(depth), None, None, colors)], voxel_size=0.0)
230
+ assert len(result.points) == 4
231
+ assert np.all(result.weights == 1.0)
232
+
233
+
234
+ def test_synthetic_orbit_poses_are_valid_and_distinct():
235
+ assert np.allclose(synthetic_orbit_poses(1)[0], np.eye(4, dtype=np.float32))
236
+ poses = synthetic_orbit_poses(4)
237
+ assert poses.shape == (4, 4, 4)
238
+ assert np.isfinite(poses).all()
239
+ assert not np.allclose(poses[0], poses[1])
240
+ assert np.allclose(poses[:, 3, :], np.array([[0, 0, 0, 1]] * 4, dtype=np.float32))
241
+
242
+
243
+ def test_gaussian_initialization_tracks_confidence():
244
+ opacity, log_scale = gaussian_initialization(np.array([0.0, 0.5, 1.0]), 3)
245
+ assert np.all(np.diff(opacity) > 0)
246
+ assert np.all(np.diff(log_scale) < 0)
247
+ assert np.isclose(opacity[0], 0.05)
248
+ assert np.isclose(opacity[-1], 0.95)
249
+
250
+
251
+ def test_voxel_fusion_uses_confidence_weighted_centroid_and_color():
252
+ depth = np.ones((1, 2), dtype=np.float32)
253
+ colors = np.array([[[255, 0, 0], [0, 0, 255]]], dtype=np.uint8)
254
+ intrinsics = np.array([[2.0, 0.0, -1.0], [0.0, 2.0, 0.5], [0.0, 0.0, 1.0]], dtype=np.float32)
255
+ first = fuse_point_views(
256
+ [(depth, np.array([[1.0, 0.5]], dtype=np.float32), intrinsics, None, colors)],
257
+ voxel_size=10.0,
258
+ )
259
+ assert len(first.points) == 1
260
+ assert first.colors[0, 0] > first.colors[0, 2]
261
+ assert np.isclose(first.weights[0], 0.75)
262
+ assert first.source_views[0] == 0
263
+ assert first.source_masks[0] == 1
264
+ assert fusion_metrics(first)["multi_view_voxel_fraction"] == 0.0
265
+
266
+
267
+ def test_confidence_ply_retains_per_point_evidence(tmp_path=None):
268
+ path = Path("confidence_test.ply")
269
+ try:
270
+ write_confidence_ply(
271
+ path,
272
+ np.array([[0.0, 0.0, 1.0]], dtype=np.float32),
273
+ np.array([[10, 20, 30]], dtype=np.uint8),
274
+ np.array([0.75], dtype=np.float32),
275
+ )
276
+ contents = path.read_text(encoding="utf-8")
277
+ assert "property float confidence" in contents
278
+ assert "property int source_view" in contents
279
+ assert "property int source_mask" in contents
280
+ assert "0 0 1 10 20 30 0.75 -1 0" in contents
281
+ finally:
282
+ if path.exists():
283
+ path.unlink()
284
+
285
+
286
+ def test_gaussian_ply_contains_3dgs_and_evidence_fields():
287
+ path = Path("gaussian_test.ply")
288
+ try:
289
+ write_gaussian_ply(
290
+ path,
291
+ np.array([[0.0, 0.0, 1.0]], dtype=np.float32),
292
+ np.array([[10, 20, 30]], dtype=np.uint8),
293
+ np.array([0.75], dtype=np.float32),
294
+ np.array([2], dtype=np.int32),
295
+ )
296
+ contents = path.read_text(encoding="utf-8")
297
+ assert "property float opacity" in contents
298
+ assert "property float confidence" in contents
299
+ assert "property int source_view" in contents
300
+ assert "property int source_mask" in contents
301
+ assert contents.rstrip().endswith("0.75 2 4")
302
+ finally:
303
+ if path.exists():
304
+ path.unlink()
305
+
306
+
307
+ def test_gaussian_ply_preserves_direct_parameters():
308
+ path = Path("gaussian_direct_test.ply")
309
+ try:
310
+ write_gaussian_ply(
311
+ path,
312
+ np.array([[0.0, 0.0, 1.0]], dtype=np.float32),
313
+ np.array([[100, 120, 140]], dtype=np.uint8),
314
+ scales=np.array([[0.1, 0.2, 0.3]], dtype=np.float32),
315
+ rotations=np.array([[0.0, 0.0, 0.0, 2.0]], dtype=np.float32),
316
+ opacities=np.array([0.8], dtype=np.float32),
317
+ )
318
+ contents = path.read_text(encoding="utf-8")
319
+ assert "property float scale_2" in contents
320
+ assert "property float rot_3" in contents
321
+ assert "0.8 -2.302585 -1.609438 -1.203973" in contents
322
+ assert contents.rstrip().endswith("1 -1 0")
323
+ finally:
324
+ path.unlink(missing_ok=True)
325
+
326
+
327
+ def test_normalized_intrinsics_are_converted_to_pixels():
328
+ normalized = np.array([[0.8, 0.0, 0.5], [0.0, 0.8, 0.5], [0.0, 0.0, 1.0]], dtype=np.float32)
329
+ pixels = prepare_intrinsics(normalized, 100, 50)
330
+ assert np.allclose(pixels, [[80, 0, 50], [0, 40, 25], [0, 0, 1]])
331
+
332
+
333
+ def test_identity_intrinsics_use_centered_default():
334
+ pixels = prepare_intrinsics(np.eye(3, dtype=np.float32), 100, 50)
335
+ assert np.allclose(pixels, default_intrinsics(100, 50))
336
+
337
+
338
+ def test_multiview_demo_contract_exports_reproducible_evidence(tmp_path):
339
+ depth = np.tile(np.linspace(0.1, 0.9, 12, dtype=np.float32), (10, 1))
340
+ views = []
341
+ for index, pose in enumerate(synthetic_orbit_poses(3)):
342
+ colors = np.zeros((10, 12, 3), dtype=np.uint8)
343
+ colors[..., index] = 180
344
+ confidence = np.full_like(depth, 1.0 - 0.15 * index)
345
+ views.append((depth, confidence, None, pose, colors))
346
+ result = fuse_point_views(views, voxel_size=0.03, max_points=5000)
347
+ assert len(result.points) > 0
348
+ metrics = fusion_metrics(result)
349
+ assert metrics["supported_source_view_count"] == 3
350
+ confidence_path = Path("fused_integration_test.ply")
351
+ gaussian_path = Path("gaussians_integration_test.ply")
352
+ try:
353
+ write_confidence_ply(confidence_path, result.points, result.colors, result.weights, result.source_views, result.source_masks)
354
+ write_gaussian_ply(gaussian_path, result.points, result.colors, result.weights, result.source_views, result.source_masks)
355
+ confidence_text = confidence_path.read_text(encoding="utf-8")
356
+ gaussian_text = gaussian_path.read_text(encoding="utf-8")
357
+ assert "property int source_mask" in confidence_text
358
+ assert "property float opacity" in gaussian_text
359
+ assert "property int source_mask" in gaussian_text
360
+ finally:
361
+ confidence_path.unlink(missing_ok=True)
362
+ gaussian_path.unlink(missing_ok=True)