Compare commits

..

2 Commits

View File

@ -1,13 +1,20 @@
from typing import Optional
from typing import Optional, Any, NamedTuple
import pandas as pd
from PyQt5.QtWidgets import QWidget, QVBoxLayout
import pyqtgraph as pg
import numpy as np
from numpy import floating
from base.base import BasePlotWidget
class ProcessStage(NamedTuple):
mean_value: floating[Any]
start_index: int
finish_index: int
class PlotWidget(BasePlotWidget):
def _create_stage_region(self,
@ -26,10 +33,18 @@ class PlotWidget(BasePlotWidget):
return region
return None
def _find_mid_in_stage(self,
stage: str,
dataframe: pd.DataFrame,
signal_name: str) -> float:
@staticmethod
def _init_plot_widget(title: str) -> (pg.PlotWidget, pg.LegendItem):
plot_widget = pg.PlotWidget(title=title)
plot_widget.showGrid(x=True, y=True)
legend = pg.LegendItem((80, 60), offset=(70, 20))
legend.setParentItem(plot_widget.graphicsItem())
return plot_widget, legend
def get_stage_info(self,
stage: str,
dataframe: pd.DataFrame,
signal_name: str) -> Optional[ProcessStage]:
if stage in self._stages:
stage_diff = np.diff(dataframe[stage])
start_index = np.where(stage_diff == 1)[0]
@ -42,8 +57,8 @@ class PlotWidget(BasePlotWidget):
finish = finish_index[0] if finish_index.size else (len(data) - 1)
data_slice = data[start:finish]
mean = np.mean(data_slice)
return mean
return 0.0
return ProcessStage(mean_value=mean, start_index=int(start), finish_index=int(finish))
return None
def _build_widget(self, dataframe: pd.DataFrame) -> QWidget:
widget = QWidget()
@ -53,37 +68,54 @@ class PlotWidget(BasePlotWidget):
dataframe_headers = dataframe.columns.tolist()
for channel, description in self._plt_channels.items():
plot_widget = pg.PlotWidget(title=channel)
plot_widget.showGrid(x=True, y=True)
legend = pg.LegendItem((80, 60), offset=(70, 20))
legend.setParentItem(plot_widget.graphicsItem())
settings = description["Settings"]
plot_widget, legend = self._init_plot_widget(title=channel)
settings = description["Settings"]
if settings["stages"] and all([stage in dataframe_headers for stage in self._stages]):
for stage in self._stages:
region = self._create_stage_region(stage, time_axis, dataframe)
if region:
plot_widget.addItem(region)
if settings["zoom"] and max(time_axis) < 5.0:
means_raw = [self._find_mid_in_stage("Welding",
dataframe,
signal["name"]) for signal in description["Signals"]]
mean = max(means_raw)
region_stable = pg.LinearRegionItem([mean - mean * 0.015,
mean + mean * 0.015],
orientation='horizontal')
region_stable.setBrush(pg.mkBrush([250, 250, 0, 25]))
plot_widget.addItem(region_stable)
if settings["zoom"]:
if max(time_axis) < 5.0:
stages = [self.get_stage_info("Welding",
dataframe,
signal["name"]) for signal in description["Signals"]]
if stages:
means_raw = [stage.mean_value for stage in stages]
mean = max(means_raw)
start = time_axis[stages[0].start_index]
finish = time_axis[stages[0].finish_index]
region_overshoot = pg.LinearRegionItem([mean - mean * 0.05,
mean + mean * 0.05],
orientation='horizontal')
region_overshoot.setBrush(pg.mkBrush([0, 250, 0, 30]))
plot_widget.addItem(region_overshoot)
overshoot = pg.BarGraphItem(x0=0,
y0=mean - mean * 0.05,
height=mean * 0.05 * 2,
width=start,
brush=pg.mkBrush([0, 250, 0, 100]))
plot_widget.addItem(overshoot)
plot_widget.setYRange(mean - 260, mean + 260)
plot_widget.setInteractive(False)
stable = pg.BarGraphItem(x0=start,
y0=mean - mean * 0.015,
height=mean * 0.015 * 2,
width=finish - start,
brush=pg.mkBrush([0, 250, 0, 100]))
plot_widget.addItem(stable)
plot_widget.setYRange(mean - 260, mean + 260)
plot_widget.setInteractive(False)
else:
max_value = min([max(dataframe[signal["name"]]) for signal in description["Signals"]])
region = pg.LinearRegionItem([max_value - max_value * 0.015,
max_value + max_value * 0.015],
movable=False,
orientation="horizontal")
region.setBrush(pg.mkBrush([0, 250, 0, 100]))
plot_widget.setYRange(max_value - 200, max_value + 200)
plot_widget.setXRange(3.5, 4.5)
plot_widget.addItem(region)
plot_widget.setInteractive(False)
for signal in description["Signals"]:
if signal["name"] in dataframe_headers: