Refactor DataSource classes and enhance testing framework
- Removed outdated comments and documentation from CsvSessionSource and DemoSource classes. - Introduced new unit tests for CsvSource and DataSource functionalities, ensuring robust validation of CSV loading and session handling. - Added UI smoke tests to verify window instantiation and menu actions without hardware dependencies. - Implemented VisionEngine tests to validate demo snapshots and alignment state logic.
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
"""Tests for CsvSource CSV loader."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
import sys
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
||||
|
||||
from wafer_edge.Calibration import BaselineCalibration
|
||||
from wafer_edge.CsvSource import find_session_files, load_camera_records_from_csv, parse_csv_filename
|
||||
|
||||
|
||||
BASELINE = BaselineCalibration()
|
||||
MASTER_SD = Path(__file__).resolve().parents[1] / "MASTER_SD"
|
||||
DATA_DIR = Path(__file__).resolve().parents[1] / "DATA" / "0610"
|
||||
|
||||
|
||||
class CsvSourceTests(unittest.TestCase):
|
||||
"""Test CSV loader correctness and robustness."""
|
||||
|
||||
def test_point_count_matches_header(self) -> None:
|
||||
"""Test 1: load S10519P0.CSV, assert parser reads all data rows."""
|
||||
# Read raw point count from file header (before filtering)
|
||||
with open(MASTER_SD / "S10519P0.CSV") as f:
|
||||
for line in f:
|
||||
if line.startswith("# points,"):
|
||||
raw_count = int(line.split(",")[1].strip())
|
||||
break
|
||||
# After filter_ring_outliers, non-wafer edges are stripped
|
||||
# but a significant fraction should remain for a good capture
|
||||
paths = {"cam-1": MASTER_SD / "S10519P0.CSV"}
|
||||
records = load_camera_records_from_csv(paths, BASELINE)
|
||||
self.assertEqual(len(records), 1)
|
||||
self.assertEqual(records[0].camera_id, "cam-1")
|
||||
filtered = len(records[0].points)
|
||||
self.assertGreater(filtered, 0, "No points loaded from CSV")
|
||||
self.assertGreaterEqual(raw_count, 100, f"Expected >= 100 raw points, got {raw_count}")
|
||||
# Filtered should be a reasonable fraction (at least 5% of raw)
|
||||
self.assertGreaterEqual(filtered, raw_count * 0.05,
|
||||
f"Filtered {filtered} << raw {raw_count}")
|
||||
|
||||
def test_mm_values_in_reasonable_range(self) -> None:
|
||||
"""Test 2: assert all mm values are within ±100 mm (physically reasonable)."""
|
||||
paths = {"cam-1": MASTER_SD / "S10519P0.CSV"}
|
||||
records = load_camera_records_from_csv(paths, BASELINE)
|
||||
pts = np.array(records[0].points)
|
||||
# All coordinates should be within ±100 mm for a 75mm radius wafer
|
||||
self.assertTrue(np.all(np.abs(pts) < 100.0),
|
||||
f"Out-of-range values: min={pts.min():.1f}, max={pts.max():.1f}")
|
||||
|
||||
def test_four_cameras_returns_four_records(self) -> None:
|
||||
"""Test 3: 4 paths → 4 records with correct camera IDs."""
|
||||
paths = {
|
||||
"cam-1": MASTER_SD / "S10540P0.CSV",
|
||||
"cam-2": MASTER_SD / "S20540P0.CSV",
|
||||
"cam-3": MASTER_SD / "R10540P0.CSV",
|
||||
"cam-4": MASTER_SD / "R20539P0.CSV",
|
||||
}
|
||||
records = load_camera_records_from_csv(paths, BASELINE)
|
||||
self.assertEqual(len(records), 4)
|
||||
record_ids = {r.camera_id for r in records}
|
||||
self.assertEqual(record_ids, {"cam-1", "cam-2", "cam-3", "cam-4"})
|
||||
# All should have points and ok status
|
||||
for r in records:
|
||||
self.assertGreater(len(r.points), 0, f"{r.camera_id} has no points")
|
||||
self.assertEqual(r.status, "ok")
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# find_session_files tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def test_find_session_files_complete(self) -> None:
|
||||
"""All 4 cameras present → dict with 4 entries, correct labels."""
|
||||
result = find_session_files(DATA_DIR)
|
||||
self.assertEqual(len(result), 4)
|
||||
self.assertIn("cam-1 (S1)", result)
|
||||
self.assertIn("cam-2 (S2)", result)
|
||||
self.assertIn("cam-3 (R1)", result)
|
||||
self.assertIn("cam-4 (R2)", result)
|
||||
for label, path in result.items():
|
||||
self.assertTrue(path.exists(), f"{label} → {path}")
|
||||
|
||||
def test_find_session_files_partial(self) -> None:
|
||||
"""Only 2 cameras present → returns dict with 2 entries, no error."""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmpdir = Path(tmp)
|
||||
for name in ("S10610P0.CSV", "R10610P0.CSV"):
|
||||
(tmpdir / name).write_text(
|
||||
(DATA_DIR / name).read_text()
|
||||
)
|
||||
result = find_session_files(tmpdir)
|
||||
self.assertEqual(len(result), 2)
|
||||
self.assertIn("cam-1 (S1)", result)
|
||||
self.assertIn("cam-3 (R1)", result)
|
||||
self.assertNotIn("cam-2 (S2)", result)
|
||||
self.assertNotIn("cam-4 (R2)", result)
|
||||
|
||||
def test_find_session_files_empty_folder(self) -> None:
|
||||
"""No CSVs at all → empty dict, no exception."""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
result = find_session_files(Path(tmp))
|
||||
self.assertEqual(result, {})
|
||||
|
||||
def test_find_session_files_bad_filename(self) -> None:
|
||||
"""Non-NanoBerry file is silently skipped."""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmpdir = Path(tmp)
|
||||
(tmpdir / "README.TXT").write_text("hello")
|
||||
(tmpdir / "S10610P0.CSV").write_text(
|
||||
(DATA_DIR / "S10610P0.CSV").read_text()
|
||||
)
|
||||
result = find_session_files(tmpdir)
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertIn("cam-1 (S1)", result)
|
||||
|
||||
def test_find_session_files_new_format(self) -> None:
|
||||
"""Underscore-style filenames (R1_0540_P0.CSV) are also discovered."""
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
tmpdir = Path(tmp)
|
||||
(tmpdir / "S1_0610_P0.CSV").write_text(
|
||||
(DATA_DIR / "S10610P0.CSV").read_text()
|
||||
)
|
||||
result = find_session_files(tmpdir)
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertIn("cam-1 (S1)", result)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,34 @@
|
||||
"""DataSource TODO anchors for future headless tests."""
|
||||
|
||||
from __future__ import annotations
|
||||
import unittest
|
||||
|
||||
from wafer_edge.DataSources import DemoSource, CsvSessionSource
|
||||
from wafer_edge.VisionEngine import VisionEngine
|
||||
from wafer_edge.Calibration import BaselineCalibration
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import MASTER_SD
|
||||
MASTER_SD = Path(__file__).resolve().parents[1] / "MASTER_SD"
|
||||
|
||||
class DataSourcesTest(unittest.TestCase):
|
||||
def test_demo_source_snapshot(self) -> None:
|
||||
sources = DemoSource(VisionEngine())
|
||||
snapshot = sources.latest_snapshot(BaselineCalibration())
|
||||
self.assertEqual(len(snapshot.camera_readings), 4)
|
||||
self.assertEqual(snapshot.source_label, "User App - Simulated Data")
|
||||
self.assertLess(snapshot.sigma_mm, 0.01)
|
||||
|
||||
def test_csv_session_source_snapshot(self) -> None:
|
||||
source = CsvSessionSource(MASTER_SD, VisionEngine())
|
||||
snapshot = source.latest_snapshot(BaselineCalibration())
|
||||
|
||||
self.assertEqual(len(snapshot.camera_readings), 4)
|
||||
self.assertEqual(snapshot.system_status, "Ready")
|
||||
self.assertEqual(snapshot.source_label, "Session: MASTER_SD (4 cams)")
|
||||
|
||||
def test_csv_session_source_missing_or_invalid_seed(self)-> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
CsvSessionSource(Path(tmp), VisionEngine())
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
try:
|
||||
from PySide6 import QtWidgets
|
||||
|
||||
PYSIDE6_IMPORT_ERROR = None
|
||||
except Exception as exc:
|
||||
QtWidgets = None
|
||||
PYSIDE6_IMPORT_ERROR = exc
|
||||
|
||||
|
||||
@unittest.skipIf(PYSIDE6_IMPORT_ERROR is not None, "PySide6 is not installed")
|
||||
class UiSmokeTests(unittest.TestCase):
|
||||
def test_windows_instantiate_without_hardware(self) -> None:
|
||||
from wafer_edge.App import UserWindow
|
||||
|
||||
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])
|
||||
|
||||
user = UserWindow()
|
||||
|
||||
self.assertEqual(user.windowTitle(), "Wafer Position Monitor")
|
||||
self.assertEqual(user.source_label.text(), "User App")
|
||||
app.quit()
|
||||
|
||||
def test_file_menu_has_open_session_action(self) -> None:
|
||||
"""Verify File → Open Session… action exists in the menu bar."""
|
||||
from wafer_edge.App import UserWindow
|
||||
|
||||
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])
|
||||
|
||||
user = UserWindow()
|
||||
menu_bar = user.menuBar()
|
||||
self.assertIsNotNone(menu_bar)
|
||||
|
||||
file_menu = None
|
||||
for action in menu_bar.actions():
|
||||
menu = action.menu()
|
||||
if menu is not None and menu.title() in ("File", "&File"):
|
||||
file_menu = menu
|
||||
break
|
||||
|
||||
self.assertIsNotNone(file_menu, "File menu should exist in the menu bar")
|
||||
|
||||
# Check that "Open Session…" is one of the menu actions.
|
||||
action_texts = {a.text() for a in file_menu.actions()}
|
||||
self.assertIn(
|
||||
"Open Session…",
|
||||
action_texts,
|
||||
"File menu should contain an 'Open Session…' action",
|
||||
)
|
||||
app.quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,80 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from wafer_edge.Calibration import BaselineCalibration, load_baseline, save_baseline
|
||||
from wafer_edge.VisionEngine import AlignmentThresholds, VisionEngine, fit_circle
|
||||
|
||||
class VisionEngineTests(unittest.TestCase):
|
||||
def test_demo_snapshot_matches_beta_values(self) -> None:
|
||||
snapshot = VisionEngine().demo_snapshot(BaselineCalibration())
|
||||
|
||||
self.assertEqual(snapshot.source_label, "User App - Simulated Data")
|
||||
self.assertEqual(snapshot.system_status, "Ready")
|
||||
self.assertAlmostEqual(snapshot.x_offset_mm, 2.3, places=6)
|
||||
self.assertAlmostEqual(snapshot.y_offset_mm, -1.1, places=6)
|
||||
self.assertAlmostEqual(snapshot.theta_offset_deg, 0.5, places=1)
|
||||
self.assertLess(snapshot.sigma_mm, 0.001)
|
||||
self.assertEqual(len(snapshot.camera_readings), 4)
|
||||
self.assertEqual(snapshot.camera_readings[2].status, "ok")
|
||||
|
||||
def test_fit_circle_from_edge_points(self) -> None:
|
||||
circle = fit_circle([(1.0, 0.0), (0.0, 1.0), (-1.0, 0.0), (0.0, -1.0)])
|
||||
|
||||
self.assertAlmostEqual(circle.center_mm[0], 0.0)
|
||||
self.assertAlmostEqual(circle.center_mm[1], 0.0)
|
||||
self.assertAlmostEqual(circle.radius_mm, 1.0)
|
||||
|
||||
def test_calibration_round_trip(self) -> None:
|
||||
baseline = BaselineCalibration(
|
||||
center_mm=(1.0, 2.0), radius_mm=3.0, theta_arm_mm=4.0
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
path = Path(temp_dir) / "baseline.json"
|
||||
save_baseline(baseline, path)
|
||||
loaded = load_baseline(path)
|
||||
|
||||
self.assertEqual(loaded, baseline)
|
||||
|
||||
class AlignmentStateTests(unittest.TestCase):
|
||||
def test_ready_state(self)-> None:
|
||||
engine = VisionEngine()
|
||||
# offset ~ 0.07mm from (0.05, 0.05), sigma = 0.01
|
||||
state = engine.get_alignment_state(0.05, 0.05, 0.01)
|
||||
self.assertEqual(state, "ready")
|
||||
|
||||
def test_aligning_state(self)-> None:
|
||||
engine = VisionEngine()
|
||||
state = engine.get_alignment_state(0.20, 0.0, 0.10)
|
||||
self.assertEqual(state, "aligning")
|
||||
|
||||
def test_fault_by_offset(self) -> None:
|
||||
engine = VisionEngine()
|
||||
state = engine.get_alignment_state(0.60, 0.0, 0.01)
|
||||
self.assertEqual(state, "fault")
|
||||
def test_fault_by_sigma(self) -> None:
|
||||
engine = VisionEngine()
|
||||
state = engine.get_alignment_state(0.05, 0.0, 0.25)
|
||||
self.assertEqual(state, "fault")
|
||||
def test_boundary_ready(self) -> None:
|
||||
engine = VisionEngine()
|
||||
state =engine.get_alignment_state(0.10, 0.0, 0.05)
|
||||
self.assertEqual(state, "ready")
|
||||
def test_demo_snapshot_alignment_state(self) -> None:
|
||||
engine = VisionEngine()
|
||||
snapshot = engine.demo_snapshot(BaselineCalibration())
|
||||
self.assertEqual(snapshot.alignment_state, "fault")
|
||||
|
||||
class AlignmentThresholdInjectionTest(unittest.TestCase):
|
||||
def test_loose_thresholds_make_demo_ready(self) -> None:
|
||||
engine = VisionEngine(thresholds = AlignmentThresholds(
|
||||
ready_offset_mm = 5.0, ready_sigma_mm = 5.0
|
||||
))
|
||||
snapshot = engine.demo_snapshot(BaselineCalibration())
|
||||
self.assertEqual(snapshot.alignment_state, "ready")
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user