modified parsing. Now it is consistent with new shorter data

This commit is contained in:
2025-12-27 21:23:07 +03:00
parent 8544a44d60
commit e4b39e5cfa

View File

@ -4,7 +4,7 @@
Формат строк: Формат строк:
- "Sweep_start" — начало нового свипа (предыдущий считается завершённым) - "Sweep_start" — начало нового свипа (предыдущий считается завершённым)
- "stp X Y" — точка (индекс X, значение Y), все целые со знаком - "s X Y" — точка (индекс X, значение Y), все целые со знаком
Отрисовываются два графика: Отрисовываются два графика:
- Левый: последний полученный свип (Y vs X) - Левый: последний полученный свип (Y vs X)
@ -248,14 +248,17 @@ class SweepReader(threading.Thread):
self._stop = stop_event self._stop = stop_event
self._src: Optional[SerialLineSource] = None self._src: Optional[SerialLineSource] = None
self._fancy = bool(fancy) self._fancy = bool(fancy)
self._max_width: int = 0
def _finalize_current(self, xs, ys): def _finalize_current(self, xs, ys):
if not xs: if not xs:
return return
max_x = max(xs) max_x = max(xs)
width = max_x + 1 width = max_x + 1
self._max_width = max(self._max_width, width)
target_width = self._max_width if self._fancy else width
# Быстрый векторизованный путь # Быстрый векторизованный путь
sweep = np.full((width,), np.nan, dtype=np.float32) sweep = np.full((target_width,), np.nan, dtype=np.float32)
try: try:
idx = np.asarray(xs, dtype=np.int64) idx = np.asarray(xs, dtype=np.int64)
vals = np.asarray(ys, dtype=np.float32) vals = np.asarray(ys, dtype=np.float32)
@ -263,9 +266,9 @@ class SweepReader(threading.Thread):
except Exception: except Exception:
# Запасной путь # Запасной путь
for x, y in zip(xs, ys): for x, y in zip(xs, ys):
if 0 <= x < width: if 0 <= x < target_width:
sweep[x] = float(y) sweep[x] = float(y)
# Дополнительная обработка пропусков: при --fancy заполняем внутренние разрывы # Дополнительная обработка пропусков: при --fancy заполняем внутренние разрывы, края и дотягиваем до максимальной длины
if self._fancy: if self._fancy:
try: try:
known = ~np.isnan(sweep) known = ~np.isnan(sweep)
@ -276,6 +279,12 @@ class SweepReader(threading.Thread):
if i1 - i0 > 1: if i1 - i0 > 1:
avg = (sweep[i0] + sweep[i1]) * 0.5 avg = (sweep[i0] + sweep[i1]) * 0.5
sweep[i0 + 1 : i1] = avg sweep[i0 + 1 : i1] = avg
first_idx = int(known_idx[0])
last_idx = int(known_idx[-1])
if first_idx > 0:
sweep[:first_idx] = sweep[first_idx]
if last_idx < sweep.size - 1:
sweep[last_idx + 1 :] = sweep[last_idx]
except Exception: except Exception:
# В случае ошибки просто оставляем как есть # В случае ошибки просто оставляем как есть
pass pass
@ -342,10 +351,10 @@ class SweepReader(threading.Thread):
ys.clear() ys.clear()
continue continue
# stp X Y (оба целые со знаком). Разделяем по любым пробелам/табам. # s X Y (оба целые со знаком). Разделяем по любым пробелам/табам.
if len(line) >= 3: if len(line) >= 3:
parts = line.split() parts = line.split()
if len(parts) >= 3 and parts[0].lower() == b"stp": if len(parts) >= 3 and parts[0].lower() == b"s":
try: try:
x = int(parts[1], 10) x = int(parts[1], 10)
y = int(parts[2], 10) # поддержка знака: "+…" и "-…" y = int(parts[2], 10) # поддержка знака: "+…" и "-…"