File size: 9,286 Bytes
f8d9c8a 4d9a2f5 f8d9c8a 4d9a2f5 626c449 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 626c449 4d9a2f5 626c449 4d9a2f5 f731ddb 4d9a2f5 f731ddb 4d9a2f5 cc39e44 f731ddb cc39e44 f731ddb cc39e44 f731ddb cc39e44 f731ddb cc39e44 f731ddb 626c449 f731ddb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
import gradio as gr
import numpy as np
import plotly.graph_objects as go
from scipy.stats import norm
class ToolMetricsDisplay:
def __init__(self):
self.df = None
self.pos_color = '#2CFCFF'
self.ori_color = '#ff8508'
self.plots = []
@staticmethod
def gauge(df, type=None, cote=None):
width = 205
height = 150
margin = dict(l=30, r=50, t=50, b=0)
if df is None or df.empty:
fig = go.Figure()
fig.update_layout(
template='plotly_dark',
width=width,
height=height,
margin=margin,
annotations=[dict(
text="No data",
showarrow=False,
font=dict(size=16, color="white")
)]
)
return fig
column = f"{cote}_rolling_{type}"
idx = df['Timestamp'].idxmax()
value = df.loc[idx, column]
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=value,
number={'font': {'color': 'white', 'size': 20}},
domain={'x': [0, 1], 'y': [0, 1]},
title={'text': type, 'font': {'color': 'white', 'size': 16}},
gauge={'axis': {'range': [0, 3]},
'bar': {'color': 'black', 'thickness': 0.4},
'steps': [
{'range': [0, 1.33], 'color': "red"},
{'range': [1.33, 2], 'color': "yellow"},
{'range': [2, 3], 'color': "green"}],
'threshold': {
'line': {'color': 'black', 'width': 3},
'thickness': 0.8,
'value': value}}
))
fig.update_layout(
template='plotly_dark',
width=width,
height=height,
margin=margin,
)
return fig
def control_graph(self, df):
width = 832
height = 400
margin = dict(l=70, r=100, t=30, b=70)
if df is None or df.empty:
fig = go.Figure()
fig.update_layout(
template='plotly_dark',
xaxis_title='Timestamp',
yaxis_title='Values',
showlegend=True,
width=width,
height=height,
margin=margin,
annotations=[dict(
text="No data",
showarrow=False,
font=dict(size=20, color="white")
)]
)
return fig
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=df['Timestamp'], y=[0.3] * len(df),
mode='lines',
line=dict(dash='dot', color=self.pos_color, width=1),
name='lsl pos'
)
)
fig.add_trace(
go.Scatter(
x=df['Timestamp'], y=[0.5] * len(df),
mode='lines',
line=dict(dash='dot', color=self.pos_color, width=1),
name='usl pos'
)
)
fig.add_trace(
go.Scatter(
x=df['Timestamp'], y=[0.2] * len(df),
mode='lines',
line=dict(dash='dot', color=self.ori_color, width=1),
name='lsl ori'
)
)
fig.add_trace(
go.Scatter(
x=df['Timestamp'], y=[0.6] * len(df),
mode='lines',
line=dict(dash='dot', color=self.ori_color, width=1),
name='usl ori'
)
)
fig.add_trace(
go.Scatter(
x=df['Timestamp'], y=df['Position'],
mode='lines+markers', line=dict(color=self.pos_color),
name='pos'
)
)
fig.add_trace(
go.Scatter(
x=df['Timestamp'], y=df['Orientation'],
mode='lines+markers', line=dict(color=self.ori_color),
name='ori'
)
)
fig.update_layout(
template='plotly_dark',
xaxis_title='Timestamp',
yaxis_title='Valeurs',
showlegend=True,
width=width,
height=height,
margin=margin
)
return fig
def normal_curve(self, df, cote=None):
width = 410
height= 250
margin= dict(l=20, r=70, t=20, b=20)
if df is None or df.empty:
fig = go.Figure()
fig.update_layout(
template='plotly_dark',
showlegend=False,
width=width,
height=height,
margin=margin,
annotations=[dict(
text="No data",
showarrow=False,
font=dict(size=16, color="white")
)]
)
return fig
if cote == 'pos':
color = self.pos_color
lsl = 0.3
usl = 0.5
else:
color = self.ori_color
lsl = 0.2
usl = 0.6
mu_column = f"{cote}_rolling_mean"
std_column = f"{cote}_rolling_std"
idx = df['Timestamp'].idxmax()
mu, std = df.loc[idx, [mu_column, std_column]]
x = np.linspace(mu - 3 * std, mu + 3 * std, 100)
y = norm.pdf(x, mu, std)
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=x, y=y,
mode='lines',
name='Normal Curve',
line=dict(color=color)
)
)
fig.add_shape(
type="line", x0=usl, y0=0, x1=usl, y1=max(y),
line=dict(color="red", width=1, dash="dot"),
name='usl'
)
fig.add_shape(
type="line", x0=lsl, y0=0, x1=lsl, y1=max(y),
line=dict(color="red", width=1, dash="dot"),
name='lsl'
)
fig.update_layout(
template='plotly_dark',
showlegend=False,
width=width,
height=height,
margin=margin
)
return fig
def tool_block(self, df, id=1):
header = f"Tool {id}"
html_content = f"""
<div style="display: flex; align-items: center; justify-content: flex-start; width: 100%;">
<div style="flex: 0 0 2%; border-top: 1px solid white;"></div>
<h2 style="flex: 0 0 auto; margin: 0 10px;">{header}</h2>
<div style="flex: 1; border-top: 1px solid white;"></div>
</div>
"""
gr.HTML(html_content)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### `Position`")
with gr.Group():
with gr.Row(height=250):
pos_normal_plot = gr.Plot(
self.normal_curve(
df=df, cote='pos'
)
)
with gr.Row(height=150):
pos_cp_gauge = gr.Plot(
self.gauge(
df=df, type='cp', cote='pos'
)
)
pos_cpk_gauge = gr.Plot(
self.gauge(
df=df, type='cpk', cote='pos'
)
)
with gr.Column(scale=1):
gr.Markdown("### `Orientation`")
with gr.Group():
with gr.Row(height=250):
ori_normal_plot = gr.Plot(
self.normal_curve(
df=df, cote='ori'
)
)
with gr.Row(height=150):
ori_cp_gauge = gr.Plot(
self.gauge(
df=df, type='cp', cote='ori'
)
)
ori_cpk_gauge = gr.Plot(
self.gauge(
df=df, type='cpk', cote='ori'
)
)
with gr.Column(scale=2):
gr.Markdown("### `Control card`")
with gr.Row(height=400):
control_plot = gr.Plot(
self.control_graph(
df=df
)
)
self.plots = [
pos_normal_plot, pos_cp_gauge, pos_cpk_gauge,
ori_normal_plot, ori_cp_gauge, ori_cpk_gauge,
control_plot
]
return self.plots
def refresh(self, df):
return [
self.normal_curve(df, cote='pos'),
self.gauge(df, type='cp', cote='pos'),
self.gauge(df, type='cpk', cote='pos'),
self.normal_curve(df, cote='ori'),
self.gauge(df, type='cp', cote='ori'),
self.gauge(df, type='cpk', cote='ori'),
self.control_graph(df),
] |