Spaces:
Sleeping
Sleeping
Commit
·
7bc9ae5
1
Parent(s):
bc46226
Adding SPC Engine
Browse files- .gitignore +3 -1
- SPC_System/spc_engine.py +132 -0
.gitignore
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
data/
|
| 2 |
test.ipynb
|
| 3 |
*.csv
|
| 4 |
-
*.xlsx
|
|
|
|
|
|
|
|
|
| 1 |
data/
|
| 2 |
test.ipynb
|
| 3 |
*.csv
|
| 4 |
+
*.xlsx
|
| 5 |
+
*.json
|
| 6 |
+
charts/*.png
|
SPC_System/spc_engine.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
file = input("Select the file to analyze or type in the file name (Should be .xlsx)")
|
| 7 |
+
INPUT_EXCEL = file
|
| 8 |
+
|
| 9 |
+
df = pd.read_excel(INPUT_EXCEL)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
################DICTIONARY BASED ON THE PARAMETER###########################
|
| 13 |
+
import json
|
| 14 |
+
|
| 15 |
+
df_new = df.head(3)
|
| 16 |
+
##To drop the extra columns
|
| 17 |
+
df_new = df_new.dropna(axis=1, how='all')
|
| 18 |
+
df_param = df_new.drop(["T_TIME","SITE_NUM"],axis=1)
|
| 19 |
+
parameter = list(df_param.columns)
|
| 20 |
+
|
| 21 |
+
##Convert the parameter to dictionary
|
| 22 |
+
new_dict = {param:{"values":[]} for param in parameter}
|
| 23 |
+
for prm in parameter:
|
| 24 |
+
temp = dict(zip(df_new['SITE_NUM'],df_new[prm]))
|
| 25 |
+
new_dict[prm].update(temp)
|
| 26 |
+
|
| 27 |
+
for prm in parameter:
|
| 28 |
+
df = df.dropna(axis=1, how='all')
|
| 29 |
+
__temp = df[prm].values[4:].tolist()
|
| 30 |
+
new_dict[prm]['values'].extend(__temp)
|
| 31 |
+
|
| 32 |
+
######To extract the Limits##############
|
| 33 |
+
df_new = df.head(3)
|
| 34 |
+
##To drop the extra columns
|
| 35 |
+
df_new = df_new.dropna(axis=1, how='all')
|
| 36 |
+
df_param = df_new.drop(["T_TIME","SITE_NUM"],axis=1)
|
| 37 |
+
parameter = list(df_param.columns)
|
| 38 |
+
|
| 39 |
+
##Convert the parameter to dictionary
|
| 40 |
+
new_dict = {param:{"values":[]} for param in parameter}
|
| 41 |
+
for prm in parameter:
|
| 42 |
+
temp = dict(zip(df_new['SITE_NUM'],df_new[prm]))
|
| 43 |
+
new_dict[prm].update(temp)
|
| 44 |
+
|
| 45 |
+
for prm in parameter:
|
| 46 |
+
df = df.dropna(axis=1, how='all')
|
| 47 |
+
__temp = df[prm].values[4:].tolist()
|
| 48 |
+
new_dict[prm]['values'].extend(__temp)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
#TODO: No need to store json. If in case if we want to maintian history then we can store them
|
| 52 |
+
# json_data = json.dumps(new_dict)
|
| 53 |
+
# print(json_data)
|
| 54 |
+
|
| 55 |
+
# with open("output.json", "w") as f:
|
| 56 |
+
# json.dump(new_dict, f, indent=4)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
#############Plotting###############
|
| 61 |
+
|
| 62 |
+
data = new_dict
|
| 63 |
+
|
| 64 |
+
##TODO: To plot for all the paramter. But need to check the way to handle it in the front end.
|
| 65 |
+
for param in parameter:
|
| 66 |
+
|
| 67 |
+
values = data[param]["values"]
|
| 68 |
+
limit_l = data[param]["LimitL"]
|
| 69 |
+
limit_u = data[param]["LimitU"]
|
| 70 |
+
unit = data[param]["Unit"]
|
| 71 |
+
|
| 72 |
+
import numpy as np
|
| 73 |
+
|
| 74 |
+
values = np.array(values)
|
| 75 |
+
mean = np.mean(values)
|
| 76 |
+
std = np.std(values, ddof=1) # Sample standard deviation
|
| 77 |
+
|
| 78 |
+
UCL = mean + 3*std
|
| 79 |
+
LCL = mean - 3*std
|
| 80 |
+
|
| 81 |
+
plt.figure(figsize=(7, 5))
|
| 82 |
+
|
| 83 |
+
# --- Box Plot ---
|
| 84 |
+
plt.boxplot(values, vert=True, patch_artist=True)
|
| 85 |
+
|
| 86 |
+
# --- Jittered Scatter Plot (spread raw values) ---
|
| 87 |
+
y = values
|
| 88 |
+
x = np.random.normal(1, 0.04, size=len(values)) # jitter around x=1
|
| 89 |
+
plt.scatter(x, y, alpha=0.6)
|
| 90 |
+
|
| 91 |
+
# --- Reference Lines ---
|
| 92 |
+
plt.axhline(mean, color='green', linestyle='--', label='Mean')
|
| 93 |
+
plt.axhline(UCL, color='red', linestyle='-.', label='UCL (Mean + 3σ)')
|
| 94 |
+
plt.axhline(LCL, color='red', linestyle='--', label='LCL (Mean - 3σ)')
|
| 95 |
+
|
| 96 |
+
# Optional spec limits
|
| 97 |
+
plt.axhline(limit_l, color='orange', linestyle=':', label='Lower Spec Limit')
|
| 98 |
+
plt.axhline(limit_u, color='orange', linestyle=':', label='Upper Spec Limit')
|
| 99 |
+
|
| 100 |
+
# Labels & Styling
|
| 101 |
+
plt.title(f"Box Plot with All Data Points - {param} ({unit})")
|
| 102 |
+
plt.ylabel(f"Value ({unit})")
|
| 103 |
+
plt.grid(True)
|
| 104 |
+
plt.legend()
|
| 105 |
+
|
| 106 |
+
plt.tight_layout()
|
| 107 |
+
# plt.show()
|
| 108 |
+
chart_path = f"./charts/control_chart_{param}.png"
|
| 109 |
+
plt.savefig(chart_path, dpi=300, bbox_inches='tight')
|
| 110 |
+
plt.close()
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
####line plot
|
| 114 |
+
# x_axis = range(1, len(values)+1)
|
| 115 |
+
# plt.figure(figsize=(10,5))
|
| 116 |
+
# plt.plot(x_axis,values, marker='o', linestyle='-', label='Measurements')
|
| 117 |
+
# plt.axhline(mean, color='green', linestyle='--', label='Mean')
|
| 118 |
+
# plt.axhline(UCL, color='red', linestyle='-.', label='UCL (Mean + 3σ)')
|
| 119 |
+
# plt.axhline(LCL, color='red', linestyle='--', label='LCL (Mean - 3σ)')
|
| 120 |
+
# plt.axhline(limit_l, color='orange', linestyle=':', label='Lower Spec Limit')
|
| 121 |
+
# plt.axhline(limit_u, color='orange', linestyle=':', label='Upper Spec Limit')
|
| 122 |
+
|
| 123 |
+
# plt.xticks(x_axis)
|
| 124 |
+
# plt.title(f"SPC Chart - {param} ({unit})")
|
| 125 |
+
# plt.xlabel("Sample Index")
|
| 126 |
+
# plt.ylabel(f"Value ({unit})")
|
| 127 |
+
# plt.legend()
|
| 128 |
+
# plt.grid(True)
|
| 129 |
+
# plt.tight_layout()
|
| 130 |
+
# plt.show()
|
| 131 |
+
|
| 132 |
+
|