File size: 3,993 Bytes
7bc9ae5
 
33d5431
e5c8dbf
 
33d5431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bc9ae5
 
 
 
 
33d5431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7bc9ae5
 
 
 
e5c8dbf
33d5431
 
7bc9ae5
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
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os

# --------- Load three datasets ----------
df_t1 = pd.read_excel("tester#1.xlsx")
df_t2 = pd.read_excel("tester#2.xlsx")
df_gold = pd.read_excel("Golden_Data.xlsx")

# --------- Get parameter list ----------
df_new = df_gold.head(3).dropna(axis=1, how="all")
df_param = df_new.drop(["T_TIME", "SITE_NUM"], axis=1)
parameters = list(df_param.columns)

# --------- Build master dictionary ----------
data = {
    param: {
        "Tester1": pd.to_numeric(df_t1[param].iloc[3:], errors="coerce").dropna().tolist(),
        "Tester2": pd.to_numeric(df_t2[param].iloc[3:], errors="coerce").dropna().tolist(),
        "Golden":  pd.to_numeric(df_gold[param].iloc[3:], errors="coerce").dropna().tolist(),
        "LimitL": df_new[param].iloc[1],
        "LimitU": df_new[param].iloc[2],
        "Unit": df_new[param].iloc[0]
    }
    for param in parameters
}

# --------- Plot comparison SPC ----------
os.makedirs("./charts_compare", exist_ok=True)

for param in parameters:

    t1 = np.array(data[param]["Tester1"])
    t2 = np.array(data[param]["Tester2"])
    gold = np.array(data[param]["Golden"])

    limit_l = data[param]["LimitL"]
    limit_u = data[param]["LimitU"]
    unit = data[param]["Unit"]

    # Golden reference control band
    mean = gold.mean()
    t1_mean = t1.mean()
    t2_mean = t2.mean()
    std = gold.std(ddof=1)
    UCL = mean + 3 * std
    LCL = mean - 3 * std

    # ---- Fix max length ----
    max_len = max(len(t1), len(t2), len(gold))
    xaxis = np.arange(1, max_len + 1)

    # ---- Correlation ----
    min_len_t1 = min(len(t1), len(gold))
    min_len_t2 = min(len(t2), len(gold))

    corr_t1 = np.corrcoef(t1[:min_len_t1], gold[:min_len_t1])[0,1]
    corr_t2 = np.corrcoef(t2[:min_len_t2], gold[:min_len_t2])[0,1]
    corr_t1_t2 = np.corrcoef(t1[:min_len_t1], t2[:min_len_t2])[0,1]

    # ---- CPK (based on Golden dataset) ----
    cpk = min((mean - limit_l) / (3 * std), (limit_u - mean) / (3 * std))

    plt.figure(figsize=(10,6))

    # --- Plot all datasets ---
    plt.plot(range(1, len(t1)+1), t1, marker='o', label="Tester #1")
    plt.plot(range(1, len(t2)+1), t2, marker='o', label="Tester #2")
    plt.plot(range(1, len(gold)+1), gold, marker='o', label="Gold Reference", linewidth=3)

    # --- SPC lines ---
    plt.axhline(mean, linestyle='--', color='black', label=f"Mean (Gold)")
    plt.axhline(UCL, linestyle='-.', color='red', label="UCL (Mean + 3σ)")
    plt.axhline(LCL, linestyle='-.', color='red', label="LCL (Mean – 3σ)")

    # --- Spec Limits ---
    plt.axhline(limit_l, linestyle=':', color='orange', label="Lower Spec")
    plt.axhline(limit_u, linestyle=':', color='orange', label="Upper Spec")

    # ---- Fix X-axis ----
    # plt.xticks(np.arange(1, max_len + 1, step=1))
    # plt.xlim(1, max_len)

    # ---- Fix X-axis with whole numbers + padding ----
    max_len = max(len(t1), len(t2), len(gold))

    plt.xticks(np.arange(1, max_len + 1, 1))  # whole numbers
    plt.xlim(0, max_len + 1)  # space before 1 and after last point


    # ---- Add CPK + Correlation text box ----
    textstr = (
        f"Gold Mean = {mean:.4f}\n"
        f"Std Dev = {std:.4f}\n"
        f"CPK = {cpk:.4f}\n"
        f"Corr T1–Gold = {corr_t1:.4f}\n"
        f"Corr T2–Gold = {corr_t2:.4f}\n"
        f"Corr T1-T2 = {corr_t1_t2:.4f}\n"
        f"Tester#1 Mean = {t1_mean:.4f}\n"
        f"Tester#2 Mean = {t2_mean:4f}\n"
    )

    plt.gca().text(
        0.02, 0.98, textstr,
        transform=plt.gca().transAxes,
        fontsize=10,
        verticalalignment='top',
        bbox=dict(boxstyle="round,pad=0.4", facecolor="white", alpha=0.8)
    )

    # Labels
    plt.title(f"SPC Comparison - {param} ({unit})")
    plt.xlabel("Sample Index")
    plt.ylabel(f"Value ({unit})")
    plt.grid(True)
    plt.legend()
    plt.tight_layout()

    # Save chart
    plt.savefig(f"./charts_compare/SPC_compare_{param}.png", dpi=300)
    plt.close()