Refactor radar system
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
"""SAR3D projection rendering and visible processor failures, without live resources."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from types import SimpleNamespace
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
import numpy as np
|
||||
|
||||
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
||||
from PyQt6.QtCore import Qt # noqa: E402
|
||||
from PyQt6.QtWidgets import QApplication, QStackedWidget # noqa: E402
|
||||
|
||||
from python_app.gui.controllers.app_window_pipeline_mixin import AppWindowPipelineMixin # noqa: E402
|
||||
from python_app.gui.controllers.app_window_config.live_processing_mixin import AppWindowLiveProcessingMixin # noqa: E402
|
||||
from python_app.gui.controllers.app_window_plot_mixin import AppWindowPlotMixin # noqa: E402
|
||||
from python_app.gui.controllers.app_window_plot.sar3d_plot_mixin import SAR3D_VIEWS, sar3d_status # noqa: E402
|
||||
from python_app.gui.controllers.app_window_ui_mixin import AppWindowUiMixin # noqa: E402
|
||||
from python_app.models.dataset_model import ResultCollection, ResultPayload # noqa: E402
|
||||
|
||||
|
||||
def _status(code: int, message: str) -> ResultPayload:
|
||||
return ResultPayload(processing_name="sar3d_status", kind=4,
|
||||
table=np.array([[code, *message.encode("utf-8")]], dtype=np.float32))
|
||||
|
||||
|
||||
def _collection(*payloads: ResultPayload) -> ResultCollection:
|
||||
return ResultCollection(collection_id=5, monotonic_ns=1500, collection_payloads=list(payloads))
|
||||
|
||||
|
||||
def _projections() -> ResultCollection:
|
||||
x, y, z = np.array([-1, 0, 1]), np.array([-0.5, 0.5]), np.array([1, 2, 3])
|
||||
axes = [(x, y), (z, y), (x, z), (x, y)]
|
||||
images = [ResultPayload(processing_name=name, kind=3, image_x_axis=xx, image_y_axis=yy,
|
||||
image=np.ones((len(yy), len(xx)), dtype=np.float32))
|
||||
for name, (xx, yy) in zip(SAR3D_VIEWS, axes)]
|
||||
return _collection(*images, _status(0, "CUDA"),
|
||||
ResultPayload(processing_name="sar3d_oblique_basis", kind=4,
|
||||
table=np.array([[1, 0, 0], [0, 1, 0]], dtype=np.float32)),
|
||||
ResultPayload(processing_name="sar3d_peak", kind=4,
|
||||
table=np.array([[2, 0.5, -0.2, 2]], dtype=np.float32)))
|
||||
|
||||
|
||||
class _View(AppWindowPlotMixin, AppWindowUiMixin, AppWindowPipelineMixin, AppWindowLiveProcessingMixin):
|
||||
def __init__(self) -> None:
|
||||
self._processing_mode = SimpleNamespace(currentText=lambda: "sar3d")
|
||||
self._plot_stack = QStackedWidget()
|
||||
self._build_sar3d_plot_page()
|
||||
|
||||
|
||||
class Sar3dPlotTest(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls) -> None:
|
||||
cls.app = QApplication.instance() or QApplication([])
|
||||
|
||||
def setUp(self) -> None:
|
||||
self.view = _View()
|
||||
self.addCleanup(self.view._plot_stack.close)
|
||||
|
||||
def test_all_four_projections_have_visible_images_and_correct_peak_axes(self) -> None:
|
||||
self.assertTrue(self.view._draw_results(_projections()))
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), "SAR3D: ready — CUDA")
|
||||
positions = {"sar3d_front": (0.5, -0.2), "sar3d_side": (2, -0.2),
|
||||
"sar3d_top": (0.5, 2), "sar3d_oblique": (0.5, -0.2)}
|
||||
for name, position in positions.items():
|
||||
image = self.view._sar3d_images[name]
|
||||
self.assertTrue(image.isVisible(), name)
|
||||
self.assertGreater(image.boundingRect().width(), 0)
|
||||
self.assertGreater(image.boundingRect().height(), 0)
|
||||
xx, yy = self.view._sar3d_peaks[name].getData()
|
||||
np.testing.assert_allclose([xx[0], yy[0]], position)
|
||||
self.assertTrue(all(label.isVisible() for label in self.view._sar3d_axis_labels.values()))
|
||||
|
||||
def test_unavailable_warning_clears_previous_images_and_labels_and_shows_reason(self) -> None:
|
||||
self.view._draw_results(_projections())
|
||||
message = "CUDA unavailable: драйвер <libcuda>"
|
||||
self.assertFalse(self.view._draw_results(_collection(_status(3, message))))
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), f"SAR3D: warning — {message}")
|
||||
self.assertEqual(self.view._sar3d_status_label.textFormat(), Qt.TextFormat.PlainText)
|
||||
self.assertTrue(all(not image.isVisible() for image in self.view._sar3d_images.values()))
|
||||
self.assertTrue(all(not label.isVisible() for label in self.view._sar3d_axis_labels.values()))
|
||||
|
||||
def test_no_signal_and_missing_projections_explain_empty_panels(self) -> None:
|
||||
self.assertFalse(self.view._draw_results(_collection(_status(1, "No signal in selected range"))))
|
||||
self.assertIn("no signal", self.view._sar3d_status_label.text())
|
||||
self.assertFalse(self.view._draw_results(_collection()))
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), "SAR3D: waiting for projection data")
|
||||
|
||||
def test_ready_cuda_status_and_clear_restores_waiting(self) -> None:
|
||||
self.assertTrue(self.view._draw_results(_projections()))
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), "SAR3D: ready — CUDA")
|
||||
self.view._clear_sar3d_plot()
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), "SAR3D: waiting for measurements")
|
||||
|
||||
def test_malformed_status_is_a_visible_error_instead_of_a_qt_exception(self) -> None:
|
||||
for table in (np.empty((0, 0)), np.array([[2, np.nan]]), np.array([[2, 256]]),
|
||||
np.array([[2, 0.5]]), np.array([[99]]), np.array([2])):
|
||||
with self.subTest(table=table):
|
||||
payload = _status(2, "failure")
|
||||
payload.table = table
|
||||
self.assertEqual(sar3d_status(_collection(payload)), (2, "Invalid SAR3D status payload"))
|
||||
self.assertFalse(self.view._draw_results(_collection(payload)))
|
||||
|
||||
def test_single_capture_finishes_reported_failure_without_losing_its_result(self) -> None:
|
||||
for code in (1, 2, 3):
|
||||
with self.subTest(code=code):
|
||||
result = _collection(_status(code, "processor explanation"))
|
||||
self.view._result_history = [result]
|
||||
self.view._single_capture_active = True
|
||||
self.view._single_capture_start_ns = 1000
|
||||
self.view._single_capture_seen_raw = True
|
||||
self.view._single_capture_target_collection_id = 5
|
||||
self.view._log = mock.Mock()
|
||||
self.view._log_error = mock.Mock()
|
||||
self.view._log_warning = mock.Mock()
|
||||
self.view._status_label = mock.Mock()
|
||||
self.view._stop_run = mock.Mock(side_effect=lambda: setattr(self.view, "_single_capture_active", False))
|
||||
self.assertTrue(self.view._finish_single_capture_if_ready())
|
||||
self.assertFalse(self.view._single_capture_active)
|
||||
self.assertEqual(self.view._result_history, [result])
|
||||
self.assertIn("processor explanation", self.view._sar3d_status_label.text())
|
||||
state = {1: "no signal in selected range", 2: "error", 3: "warning"}[code]
|
||||
self.view._status_label.setText.assert_called_once_with(f"Status: {state}")
|
||||
log = self.view._log_error if code == 2 else self.view._log_warning
|
||||
log.assert_called_once_with(
|
||||
f"Single capture: SAR3D {state}: processor explanation")
|
||||
|
||||
def test_live_edit_retains_failure_reason_then_recovers_to_ready_images(self) -> None:
|
||||
self.view._result_history = []
|
||||
self.view._write_live_processing_config = mock.Mock()
|
||||
self.view._show_exception = mock.Mock()
|
||||
for result, expected in ((_collection(_status(2, "CUDA kernel failure")), "error"),
|
||||
(_collection(_status(3, "Driver initialization unavailable")), "warning"),
|
||||
(_collection(_status(2, "fewer than two frequency points")), "error"),
|
||||
(_collection(_status(1, "No signal in selected depth range")), "no signal"),
|
||||
(_projections(), "ready — CUDA")):
|
||||
self.view._drain_results_until_quiet = mock.Mock(side_effect=[None, result])
|
||||
self.view._on_processing_live_settings_changed()
|
||||
self.assertIn(expected, self.view._sar3d_status_label.text())
|
||||
self.view._show_exception.assert_not_called()
|
||||
self.assertTrue(all(image.isVisible() for image in self.view._sar3d_images.values()))
|
||||
|
||||
def test_history_redraw_preserves_error_but_stale_revision_clears_old_images(self) -> None:
|
||||
self.view._result_history = [_collection(_status(2, "processor error"))]
|
||||
self.view._redraw_after_history_deletion()
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), "SAR3D: error — processor error")
|
||||
self.view._draw_results(_projections())
|
||||
self.view._required_processing_revision = 10
|
||||
self.view._redraw_after_history_deletion()
|
||||
self.assertEqual(self.view._sar3d_status_label.text(), "SAR3D: waiting for measurements")
|
||||
self.assertTrue(all(not image.isVisible() for image in self.view._sar3d_images.values()))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user