Created
May 22, 2022 12:13
-
-
Save doclrogers/c9da9740d52a902542a8856ec7900839 to your computer and use it in GitHub Desktop.
Create an Interactive Control Chart from a Data Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from plotly.subplots import make_subplots | |
import plotly.graph_objects as go | |
# Inputs | |
HOSTNAME = "https://statistical-process-control1.p.rapidapi.com" | |
HEADERS = headers: { | |
'X-RapidAPI-Host': 'statistical-process-control1.p.rapidapi.com', | |
'X-RapidAPI-Key': 'YOUR-KEY" | |
} | |
data = [82,84,75,79,84,81,81,105,80,106,74,76,73,82] | |
N = 1 | |
# Endpoints | |
URL_SPC = f"{HOSTNAME}/spc/control/variable/{N}" | |
URL_NELSON = f"{HOSTNAME}/spc/rules/nelson" | |
# Compute SPC | |
resp_spc = requests.request("POST", URL_SPC, data={'data': ",".join([str(x) for x in data])}) | |
data_spc = resp_spc.json() | |
print(data_spc) | |
# Compute Nelson Rules | |
resp_nelson = requests.request("POST", URL_NELSON, data={'data': ",".join([str(x) for x in data])}) | |
data_nelson = resp_nelson.json() | |
print(data_nelson) | |
# Plot | |
fig = make_subplots(rows=2, shared_xaxes=True) | |
fig.update_layout(title_text=f"{data_spc['kind']} Chart with Nelson Rules Overlay", | |
hovermode="x") | |
# X | |
fig.add_trace(go.Scatter(y=data, name='X', line={'color':'black'}), row=1, col=1) | |
fig.add_trace(go.Scatter(y=[data_spc['x_bar']]*data_spc['k'], name='X̄', mode='lines', line={'color': 'blue', 'width': 2}), row=1, col=1) | |
fig.add_trace(go.Scatter(y=[data_spc['x_ucl']]*data_spc['k'], name='X UCL', mode='lines', line={'color': 'red', 'width': 2}), row=1, col=1) | |
fig.add_trace(go.Scatter(y=[data_spc['x_lcl']]*data_spc['k'], name='X LCL', mode='lines', line={'color': 'red', 'width': 2}), row=1, col=1) | |
colors_nelson = ['darkred', 'firebrick', 'maroon', 'crimson', 'indianred', 'salmon', 'lightcoral', 'darksalmon'] | |
for idx, fail in enumerate(data_nelson['failed_rules']): | |
if fail: | |
#print(data_nelson['failed_indices'][idx], [data[i] for i in data_nelson['failed_indices'][idx]]) | |
fig.add_trace(go.Scatter(x=data_nelson['failed_indices'][idx], y=[data[i] for i in data_nelson['failed_indices'][idx]], | |
name=f'X Nelson Rule {idx+1}', mode='markers', marker={'size': (idx+1)*2, 'color': colors_nelson[idx]}), row=1, col=1) | |
fig['layout']['xaxis']['title'] = 'Observation' | |
fig['layout']['yaxis']['title'] = 'Individual Value' | |
# Moving Range | |
fig.add_trace(go.Scatter(y=data_spc['mr'], name='MR', line={'color': 'darkslategrey'}), row=2, col=1) | |
fig.add_trace(go.Scatter(y=[data_spc['mr_bar']]*data_spc['k'], name='M̅R̅', mode='lines', line={'color': 'royalblue', 'width': 2}), row=2, col=1) | |
fig.add_trace(go.Scatter(y=[data_spc['mr_ucl']]*data_spc['k'], name='MR UCL', mode='lines', line={'color': 'darkred', 'width': 2}), row=2, col=1) | |
fig.add_trace(go.Scatter(y=[data_spc['mr_lcl']]*data_spc['k'], name='MR LCL', mode='lines', line={'color': 'darkred', 'width': 2}), row=2, col=1) | |
fig['layout']['xaxis2']['title'] = 'Observation' | |
fig['layout']['yaxis2']['title'] = 'Moving Range' | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment