Files
radar_system/python_app/gui/controllers/sections/processing_section.py
T
Ayzen b88d704126 Port the SAR3D background gate, CLEAN and object boxes from Multi_depthnorm_clean
- BG_GATE_ENABLE: frame peak over its median background (sar3d_background); the
  GUI scales the frame with bg_gate_gain and its cloud threshold.
- SAR_CLEAN_ENABLE: CLEAN of a one-frame aperture on a 4x10x4 cm search grid over
  the whole scene; objects in their own scale (sar3d_objects).
- SAR_MARK_ENABLE: boxes by class around object groups (sar3d_marks), with the
  legend under the views.
- SAR_VOXEL_M as three steps X/Y/Z with the notebook defaults 0.025/0.02/0.02.
- GPU backend: image/download/load/project API, per-plane block reduction for the
  maximum, depth gate without FMA; sar3d host code built with -ffp-contract=off.
- References regenerated from notebook cells 1 and 5, with new CLEAN scenes and a
  synthetic box-rule case; docs updated.
2026-10-16 12:08:57 +03:00

351 lines
20 KiB
Python

"""Builder for processing mode and live-parameter section."""
from __future__ import annotations
from PyQt6.QtCore import QRegularExpression, Qt
from PyQt6.QtGui import QRegularExpressionValidator
from PyQt6.QtWidgets import (
QCheckBox,
QDoubleSpinBox,
QFormLayout,
QGroupBox,
QLineEdit,
QSlider,
QSizePolicy,
QStackedWidget,
QVBoxLayout,
QWidget,
)
from python_app.gui.controllers.sections.layout_helpers import FormRow, build_two_column_form_widget
from python_app.gui.antenna_geometry_editor import AntennaGeometryEditor
from python_app.gui.controllers.sections.views import (
ProcessingView, SectionView, choice, double_spin, integer_spin, write_setting,
)
from python_app.models.gui_profile_schema import GuiStateModel
from python_app.models.run_config_schema import RunConfigModel
from python_app.models.setting_fields import GUI_FIELDS
from python_app.models.run_config_validation import POSITION_CSV_PATTERN
# Same semantics on every GPR-like page; the data_processor applies them.
_IGNORE_SOCKET_SPEED_TOOLTIP = (
"Checked: the speed below drives the motion correction. Unchecked: the speed a locator "
"socket client streams ('vlc') does, and the field below only stands in while no client "
"has sent one in the last 5 s."
)
def _build_processing_mode_page(parent: QWidget, rows: list[FormRow], *, split_index: int | None = None) -> QWidget:
"""Create one processing-mode page with a two-column form body."""
page = QWidget(parent)
page.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
page_layout = QVBoxLayout(page)
page_layout.setContentsMargins(0, 0, 0, 0)
page_layout.setSpacing(0)
page_layout.addWidget(build_two_column_form_widget(page, rows, split_index=split_index))
return page
def build_processing_group(config: RunConfigModel, gui: GuiStateModel) -> ProcessingView:
"""Create processing mode section with pass-through, B-scan, GPR, and 3D SAR pages."""
view = SectionView(QGroupBox("Processing"))
group = view.group
form = QFormLayout(group)
form.setFieldGrowthPolicy(QFormLayout.FieldGrowthPolicy.AllNonFixedFieldsGrow)
processing_mode = view.add("_processing_mode", choice(['pass_through', 'bscan', 'gpr', 'sar3d']))
processing_mode_pages = QStackedWidget(group)
processing_mode_pages.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Fixed)
show_magnitude_checkbox = view.add("_show_magnitude_checkbox", QCheckBox("Show magnitude"))
show_phase_checkbox = view.add("_show_phase_checkbox", QCheckBox("Show phase"))
unwrap_phase_checkbox = view.add("_unwrap_phase_checkbox", QCheckBox("Unwrap phase"))
unwrap_phase_checkbox.setToolTip(
"Plot the cumulative (unwrapped) phase instead of wrapping to ±180°; "
"the phase axis auto-scales to the unwrapped range."
)
pass_through_combo_filter_input = view.add("_pass_through_combo_filter_input", QLineEdit())
pass_through_combo_filter_input.setPlaceholderText("empty = all, e.g. 0:0,1:0")
pass_through_fixed_y_enabled = view.add("_pass_through_fixed_y_enabled", QCheckBox("Fix magnitude Y range"))
pass_through_y_min_db = view.add("_pass_through_y_min_db", double_spin(-240.0, 240.0, 1, 1.0))
pass_through_y_max_db = view.add("_pass_through_y_max_db", double_spin(-240.0, 240.0, 1, 1.0))
pass_through_page = _build_processing_mode_page(
processing_mode_pages,
[
show_magnitude_checkbox,
show_phase_checkbox,
unwrap_phase_checkbox,
("Switch combos", pass_through_combo_filter_input),
pass_through_fixed_y_enabled,
("Y min (dB)", pass_through_y_min_db),
("Y max (dB)", pass_through_y_max_db),
],
split_index=5,
)
processing_mode_pages.addWidget(pass_through_page)
bscan_axis = view.add("_bscan_axis", choice(['abs', 'real', 'phase']))
bscan_cut_m = view.add("_bscan_cut_m", double_spin(0.0, 2.0, 3, 0.001))
bscan_max_depth_m = view.add("_bscan_max_depth_m", double_spin(0.1, 20.0, 1, 0.1))
bscan_gain = view.add("_bscan_gain", double_spin(0.0, 3.0, 1, 0.1))
bscan_start_freq_mhz = view.add("_bscan_start_freq_mhz", double_spin(100.0, 8800.0, 1, 10.0))
bscan_stop_freq_mhz = view.add("_bscan_stop_freq_mhz", double_spin(100.0, 8800.0, 1, 10.0))
bscan_subtract_mean_ascan = view.add("_bscan_subtract_mean_ascan", QCheckBox("Subtract mean A-scan"))
bscan_history_window = view.add("_bscan_history_window", integer_spin(1, 1000))
bscan_history_window.setToolTip(
"How many past sweeps the B-scan shows once acquisition is stopped. While "
"running, the window stays clamped to the C++ ring capacity."
)
bscan_page = _build_processing_mode_page(
processing_mode_pages,
[
("Axis", bscan_axis),
("Cut (m)", bscan_cut_m),
("Max depth (m)", bscan_max_depth_m),
("Gain", bscan_gain),
("Start (MHz)", bscan_start_freq_mhz),
("Stop (MHz)", bscan_stop_freq_mhz),
("Scans to show (stopped)", bscan_history_window),
bscan_subtract_mean_ascan,
],
split_index=4,
)
processing_mode_pages.addWidget(bscan_page)
gpr_defaults = config.gpr
gpr_tx_geometry_input = view.add("_gpr_tx_geometry_input", AntennaGeometryEditor(
"output_pos", gpr_defaults.tx_geometry, position_count=config.output_switch.positions,
))
gpr_rx_geometry_input = view.add("_gpr_rx_geometry_input", AntennaGeometryEditor(
"input_pos", gpr_defaults.rx_geometry, position_count=config.input_switch.positions,
))
gpr_common_page = _build_processing_mode_page(
group,
[
("Tx geometry", gpr_tx_geometry_input),
("Rx geometry", gpr_rx_geometry_input),
],
split_index=1,
)
gpr_input_positions_input = view.add("_gpr_input_positions_input", QLineEdit())
gpr_input_positions_input.setPlaceholderText("0,1,2")
gpr_output_positions_input = view.add("_gpr_output_positions_input", QLineEdit())
gpr_output_positions_input.setPlaceholderText("0,1")
gpr_min_depth_m = view.add("_gpr_min_depth_m", double_spin(0.0, 50.0, 2, 0.1))
gpr_max_depth_m = view.add("_gpr_max_depth_m", double_spin(0.1, 50.0, 2, 0.1))
gpr_range_comp_power = view.add("_gpr_range_comp_power", double_spin(0.0, 5.0, 3, 0.01))
gpr_object_min_frac = view.add("_gpr_object_min_frac", double_spin(0.0, 1.0, 2, 0.05))
gpr_object_min_frac.setToolTip(
"Object detection stops once a peak falls below this fraction of the global maximum."
)
gpr_score_mode = view.add("_gpr_score_mode", choice(['peak', 'combined']))
gpr_motion_mode = view.add("_gpr_motion_mode", choice(['int_minus', 'int_focus']))
gpr_motion_mode.setToolTip(
"Intra-sweep speed correction before the IFFT. int_minus: full correction; "
"int_focus: focusing residual only (no net Z shift)."
)
gpr_look_angle_deg = view.add("_gpr_look_angle_deg", double_spin(-90.0, 90.0, 2, 0.5))
gpr_look_angle_deg.setToolTip(
"Radar look angle vs the range axis (deg). Used by intra-sweep motion correction."
)
gpr_direction_sign = view.add("_gpr_direction_sign", double_spin(-1.0, 1.0, 0, 2.0))
gpr_direction_sign.setToolTip(
"Motion direction along range: +1 (later frequencies deeper) or -1 (shallower)."
)
gpr_ignore_socket_speed_enabled = view.add("_gpr_ignore_socket_speed_enabled", QCheckBox("Ignore socket speed"))
gpr_ignore_socket_speed_enabled.setToolTip(_IGNORE_SOCKET_SPEED_TOOLTIP)
gpr_speed_m_s = view.add("_gpr_speed_m_s", double_spin(-100.0, 100.0, 3, 0.05))
gpr_speed_m_s.setToolTip(
"Radar speed (m/s) used by intra-sweep motion correction when 'Ignore socket speed' is on."
)
gpr_max_detected_objects_to_draw = view.add("_gpr_max_detected_objects_to_draw", integer_spin(0, 10000))
gpr_draw_top_m_objects = view.add("_gpr_draw_top_m_objects", integer_spin(0, 10000))
gpr_object_approach_min_frames = view.add("_gpr_object_approach_min_frames", integer_spin(1, 100))
gpr_object_approach_min_frames.setToolTip(
"Show an object only after it persists as a motion-consistent track this many "
"consecutive frames (1 disables the approach filter)."
)
gpr_start_freq_mhz = view.add("_gpr_start_freq_mhz", double_spin(100.0, 8800.0, 1, 10.0))
gpr_stop_freq_mhz = view.add("_gpr_stop_freq_mhz", double_spin(100.0, 8800.0, 1, 10.0))
gpr_background_subtract_enabled = view.add("_gpr_background_subtract_enabled", QCheckBox("Subtract mean of previous collections"))
gpr_background_mean_count = view.add("_gpr_background_mean_count", integer_spin(0, 10000))
gpr_remove_sidelobe_objects_enabled = view.add("_gpr_remove_sidelobe_objects_enabled", QCheckBox("Remove sidelobe objects"))
gpr_render_mode = view.add("_gpr_render_mode", choice(['heatmap', 'objects_only']))
gpr_visible_x_min_m = view.add("_gpr_visible_x_min_m", double_spin(-100.0, 100.0, 2, 0.1))
gpr_visible_x_max_m = view.add("_gpr_visible_x_max_m", double_spin(-100.0, 100.0, 2, 0.1))
gpr_visible_z_min_m = view.add("_gpr_visible_z_min_m", double_spin(0.0, 100.0, 2, 0.1))
gpr_visible_z_max_m = view.add("_gpr_visible_z_max_m", double_spin(0.0, 100.0, 2, 0.1))
gpr_bp_ground_depth_m = view.add("_gpr_bp_ground_depth_m", double_spin(0.0, 50.0, 3, 0.05))
gpr_bp_ground_depth_m.setToolTip("Height of the radar origin above the ground; the BP slice follows the ground and radar tilt.")
gpr_pair_depth_equalize = view.add("_gpr_pair_depth_equalize", QCheckBox("PAIR_DEPTH_EQUALIZE"))
gpr_page = _build_processing_mode_page(
processing_mode_pages,
[
("Input positions", gpr_input_positions_input),
("Output positions", gpr_output_positions_input),
("Min depth (m)", gpr_min_depth_m),
("Max depth (m)", gpr_max_depth_m),
("Range compensation power", gpr_range_comp_power),
("Object threshold fraction", gpr_object_min_frac),
("Score mode", gpr_score_mode),
("Motion mode", gpr_motion_mode),
("Look angle (°)", gpr_look_angle_deg),
("Direction sign", gpr_direction_sign),
gpr_ignore_socket_speed_enabled,
("Speed (m/s)", gpr_speed_m_s),
("Render mode", gpr_render_mode),
("Max detected objects", gpr_max_detected_objects_to_draw),
("Draw top M objects", gpr_draw_top_m_objects),
("Approach min frames", gpr_object_approach_min_frames),
("Start (MHz)", gpr_start_freq_mhz),
("Stop (MHz)", gpr_stop_freq_mhz),
("Radar height (m)", gpr_bp_ground_depth_m),
("Visible X min (m)", gpr_visible_x_min_m),
("Visible X max (m)", gpr_visible_x_max_m),
("Visible Z min (m)", gpr_visible_z_min_m),
("Visible Z max (m)", gpr_visible_z_max_m),
gpr_background_subtract_enabled,
("Mean count", gpr_background_mean_count),
gpr_pair_depth_equalize,
gpr_remove_sidelobe_objects_enabled,
],
split_index=8,
)
processing_mode_pages.addWidget(gpr_page)
# 3D SAR page. The labels are the names of the reference notebook
# Multi_depthnorm_clean.ipynb; the shared gpr_* fields get their own
# widgets here, like on the GPR page.
sar3d_input_positions_input = view.add("_sar3d_input_positions_input", QLineEdit())
sar3d_input_positions_input.setPlaceholderText("0,1,2")
sar3d_output_positions_input = view.add("_sar3d_output_positions_input", QLineEdit())
sar3d_output_positions_input.setPlaceholderText("0,1")
sar3d_start_freq_mhz = view.add("_sar3d_start_freq_mhz", double_spin(100.0, 8800.0, 1, 10.0))
sar3d_stop_freq_mhz = view.add("_sar3d_stop_freq_mhz", double_spin(100.0, 8800.0, 1, 10.0))
sar3d_min_depth_m = view.add("_sar3d_min_depth_m", double_spin(0.0, 50.0, 2, 0.1))
sar3d_min_depth_m.setToolTip("Radar depth gate start (m); the scene grid along Z follows the gate.")
sar3d_max_depth_m = view.add("_sar3d_max_depth_m", double_spin(0.1, 50.0, 2, 0.1))
sar3d_max_depth_m.setToolTip("Radar depth gate end (m); the scene grid along Z follows the gate.")
sar3d_voxel_x_m = view.add("_sar3d_voxel_x_m", double_spin(0.01, 0.1, 3, 0.005))
sar3d_voxel_y_m = view.add("_sar3d_voxel_y_m", double_spin(0.01, 0.1, 3, 0.005))
sar3d_voxel_z_m = view.add("_sar3d_voxel_z_m", double_spin(0.01, 0.1, 3, 0.005))
for axis, step in zip("XYZ", (sar3d_voxel_x_m, sar3d_voxel_y_m, sar3d_voxel_z_m)):
step.setToolTip(f"Voxel step along {axis} (m). Smaller is finer and slower: halving all three "
"steps makes 8 times more voxels.")
sar3d_look_angle_deg = view.add("_sar3d_look_angle_deg", double_spin(-90.0, 90.0, 2, 0.5))
sar3d_look_angle_deg.setToolTip(
"Downward tilt of the radar (deg): motion correction along range and the ground scene axes."
)
sar3d_ignore_socket_speed_enabled = view.add("_sar3d_ignore_socket_speed_enabled", QCheckBox("Ignore socket speed"))
sar3d_ignore_socket_speed_enabled.setToolTip(_IGNORE_SOCKET_SPEED_TOOLTIP)
sar3d_speed_m_s = view.add("_sar3d_speed_m_s", double_spin(-100.0, 100.0, 3, 0.05))
sar3d_frame_radius = view.add("_sar3d_frame_radius", integer_spin(0, 24))
sar3d_frame_radius.setToolTip(
"Causal aperture: current and r previous frames. 0: the current frame only (needs a vertical antenna "
"baseline); >0 adds aperture from motion and needs an accurate speed. The scene follows the current frame."
)
sar3d_bp_ground_depth_m = view.add("_sar3d_bp_ground_depth_m", double_spin(0.0, 10.0, 2, 0.05))
sar3d_bp_ground_depth_m.setToolTip("Height of the lower antenna row above the ground (m); scene Y = 0 is the ground.")
sar3d_comp_range_power = view.add("_sar3d_comp_range_power", double_spin(0.0, 2.0, 2, 0.05))
sar3d_comp_range_power.setToolTip("Geometric spreading compensation (Rtx*Rrx)^power, capped at 5.")
sar3d_use_compensation = view.add("_sar3d_use_compensation", QCheckBox("SAR_USE_COMPENSATION"))
sar3d_normalize_each_frame = view.add("_sar3d_normalize_each_frame", QCheckBox("SAR_NORMALIZE_EACH_FRAME"))
sar3d_normalize_each_frame.setToolTip("Scale every Tx/Rx A-scan by its median inside the depth gate.")
sar3d_pair_depth_equalize = view.add("_sar3d_pair_depth_equalize", QCheckBox("PAIR_DEPTH_EQUALIZE"))
sar3d_background_subtract_enabled = view.add("_sar3d_background_subtract_enabled", QCheckBox("Subtract mean of previous collections"))
sar3d_background_mean_count = view.add("_sar3d_background_mean_count", integer_spin(0, 10000))
sar3d_bg_gate_enabled = view.add("_sar3d_bg_gate_enabled", QCheckBox("BG_GATE_ENABLE"))
sar3d_bg_gate_enabled.setToolTip(
"Show the frame relative to its background, the median of the volume: below 6 backgrounds the "
"frame is empty, at 6 its maximum appears at SAR_CLOUD_THRESHOLD, from 8.5 the scale is full."
)
sar3d_clean_enabled = view.add("_sar3d_clean_enabled", QCheckBox("SAR_CLEAN_ENABLE"))
sar3d_clean_enabled.setToolTip(
"CLEAN: subtract the strongest responses one by one, so weak objects show beside strong ones; "
"each object is drawn in its own 0-1 scale. Runs on a one-frame aperture: always with "
"SAR_FRAME_RADIUS = 0, otherwise while no previous frame is available, as after Start."
)
sar3d_x_size_m = view.add("_sar3d_x_size_m", double_spin(0.0, 100.0, 2, 0.1))
sar3d_y_min_m = view.add("_sar3d_y_min_m", double_spin(-50.0, 50.0, 2, 0.1))
sar3d_y_max_m = view.add("_sar3d_y_max_m", double_spin(-50.0, 50.0, 2, 0.1))
sar3d_z_size_m = view.add("_sar3d_z_size_m", double_spin(0.0, 100.0, 2, 0.1))
sar3d_cloud_threshold = view.add("_sar3d_cloud_threshold", double_spin(0.0, 1.0, 2, 0.05))
sar3d_zoom_percent = view.add("_sar3d_zoom_percent", QSlider(Qt.Orientation.Horizontal))
sar3d_zoom_percent.setRange(100, 1000)
sar3d_zoom_percent.setToolTip("100% shows the full scene; higher values zoom towards its brightest object.")
sar3d_mark_enabled = view.add("_sar3d_mark_enabled", QCheckBox("SAR_MARK_ENABLE"))
sar3d_mark_enabled.setToolTip(
"Box the CLEAN objects by height: red very low (like a mine), orange on the ground, blue above it, "
"dashed grey at the scene boundary (not yet decidable). Off: numbered objects instead."
)
sar3d_page = _build_processing_mode_page(
processing_mode_pages,
[
("INPUT_IDX", sar3d_input_positions_input),
("OUTPUT_IDX", sar3d_output_positions_input),
("F_START MHz", sar3d_start_freq_mhz),
("F_STOP MHz", sar3d_stop_freq_mhz),
("MIN_DEPTH", sar3d_min_depth_m),
("MAX_DEPTH", sar3d_max_depth_m),
("SAR_VOXEL_M X", sar3d_voxel_x_m),
("SAR_VOXEL_M Y", sar3d_voxel_y_m),
("SAR_VOXEL_M Z", sar3d_voxel_z_m),
("LOOK_ANGLE_DEG", sar3d_look_angle_deg),
sar3d_ignore_socket_speed_enabled,
("SPEED_M_S", sar3d_speed_m_s),
("SAR_FRAME_RADIUS", sar3d_frame_radius),
("BP_GROUND_DEPTH_M", sar3d_bp_ground_depth_m),
("COMP_RANGE_POWER", sar3d_comp_range_power),
sar3d_use_compensation,
sar3d_normalize_each_frame,
sar3d_pair_depth_equalize,
sar3d_background_subtract_enabled,
("Mean count", sar3d_background_mean_count),
sar3d_bg_gate_enabled,
sar3d_clean_enabled,
("SAR_X_SIZE_M (0 = auto)", sar3d_x_size_m),
("SAR_Y_RANGE_M min", sar3d_y_min_m),
("SAR_Y_RANGE_M max", sar3d_y_max_m),
("SAR_Z_SIZE_M (0 = auto)", sar3d_z_size_m),
("SAR_CLOUD_THRESHOLD", sar3d_cloud_threshold),
("Zoom (%)", sar3d_zoom_percent),
sar3d_mark_enabled,
],
)
processing_mode_pages.addWidget(sar3d_page)
form.addRow(processing_mode_pages)
form.addRow(gpr_common_page)
band = config.radar.rph_radar.band if config.is_rph_radar else config.radar.sweep
for binding in GUI_FIELDS:
if binding.path[0] == "processing":
widget = view.fields[binding.spec.widget]
if isinstance(widget, AntennaGeometryEditor):
raise TypeError("Geometry is not a scalar GUI setting")
if binding.path[-1] in {"start_freq_mhz", "stop_freq_mhz"}:
assert isinstance(widget, QDoubleSpinBox)
widget.setRange(band.start_hz / 1e6, band.stop_hz / 1e6)
if binding.spec.live in {"gpr_input_positions", "gpr_output_positions"}:
assert isinstance(widget, QLineEdit)
widget.setValidator(QRegularExpressionValidator(QRegularExpression(POSITION_CSV_PATTERN), widget))
write_setting(widget, binding.read(gui))
return ProcessingView(
group=group, fields=view.fields, mode=processing_mode, pages=processing_mode_pages,
geometry_page=gpr_common_page, geometries=(gpr_tx_geometry_input, gpr_rx_geometry_input),
fixed_y=pass_through_fixed_y_enabled, y_bounds=(pass_through_y_min_db, pass_through_y_max_db),
speed_controls=((gpr_ignore_socket_speed_enabled, gpr_speed_m_s),
(sar3d_ignore_socket_speed_enabled, sar3d_speed_m_s)),
trace_controls=(show_magnitude_checkbox, show_phase_checkbox, unwrap_phase_checkbox,
pass_through_combo_filter_input, pass_through_fixed_y_enabled,
pass_through_y_min_db, pass_through_y_max_db),
visual_controls=(gpr_render_mode, sar3d_cloud_threshold, sar3d_zoom_percent, sar3d_mark_enabled),
locator_controls=(gpr_max_detected_objects_to_draw, gpr_draw_top_m_objects,
gpr_visible_x_min_m, gpr_visible_x_max_m,
gpr_visible_z_min_m, gpr_visible_z_max_m),
frequency_controls=(bscan_start_freq_mhz, bscan_stop_freq_mhz, gpr_start_freq_mhz, gpr_stop_freq_mhz,
sar3d_start_freq_mhz, sar3d_stop_freq_mhz),
)