Ansaribinhyder commited on
Commit
3c118cd
·
1 Parent(s): 0db1a10
Files changed (2) hide show
  1. app.py +98 -0
  2. requirement.txt +5 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ import io
6
+
7
+ def plot_excel_data(golden_data_file, manipulated_data_file):
8
+ """
9
+ Reads two Excel files, plots the time-series data, and adds spec limits.
10
+ """
11
+ if golden_data_file is None:
12
+ raise gr.Error("Please upload the 'Golden Data' Excel file.")
13
+
14
+ try:
15
+ # Read first 3 rows to extract limits from the golden data
16
+ limits_df1 = pd.read_excel(golden_data_file.name, nrows=4)
17
+ limits_df1 = limits_df1.drop(0)
18
+
19
+ # Data (skip first 3 rows)
20
+ df1 = pd.read_excel(golden_data_file.name)
21
+ df1 = df1.drop([0, 1, 2, 3])
22
+ df1 = df1.apply(pd.to_numeric, errors="coerce")
23
+ except Exception as e:
24
+ raise gr.Error(f"Error processing 'Golden Data' file: {e}")
25
+
26
+ # Build limits dictionary per column
27
+ ignore_cols = ["SITE_NUM", "PART_ID", "PASSFG", "SOFT_BIN", "T_TIME", "TEST_NUM"]
28
+ cols_to_plot = [col for col in limits_df1.columns if "_" in col and col not in ignore_cols]
29
+ limits_df1 = limits_df1.drop(columns=ignore_cols)
30
+ limits = {
31
+ col: {"LL": limits_df1.iloc[0][col], "UL": limits_df1.iloc[1][col]}
32
+ for col in limits_df1.columns
33
+ }
34
+
35
+ # Initialize a second dataframe if a file is provided
36
+ df2 = None
37
+ if manipulated_data_file is not None:
38
+ try:
39
+ df2 = pd.read_excel(manipulated_data_file.name)
40
+ df2 = df2.drop([0, 1, 2, 3])
41
+ df2 = df2.apply(pd.to_numeric, errors="coerce")
42
+ except Exception as e:
43
+ raise gr.Error(f"Error processing 'Manipulated Data' file: {e}")
44
+
45
+ # Plotting logic
46
+ n_cols = 3
47
+ n_rows = (len(df1.columns) + n_cols - 1) // n_cols
48
+
49
+ fig, axes = plt.subplots(n_rows, n_cols, figsize=(n_cols * 5, n_rows * 3.5))
50
+ if n_rows * n_cols > len(df1.columns):
51
+ # Flatten axes array for easy iteration, then turn off unused subplots
52
+ for i in range(len(df1.columns), n_rows * n_cols):
53
+ axes.flatten()[i].axis('off')
54
+
55
+ for i, col in enumerate(cols_to_plot):
56
+ ax = axes.flatten()[i] if n_rows > 1 else axes[i]
57
+
58
+ # Golden data (Old)
59
+ x1 = np.arange(1, len(df1[col]) + 1)
60
+ y1 = pd.to_numeric(df1[col], errors="coerce").values
61
+ ax.plot(x1, y1, marker="o", linestyle="-", color="blue", label="Old")
62
+
63
+ # New data (if provided)
64
+ if df2 is not None and col in df2.columns:
65
+ x2 = np.arange(1, len(df2[col]) + 1)
66
+ y2 = pd.to_numeric(df2[col], errors="coerce").values
67
+ ax.plot(x2, y2, marker="s", linestyle="--", color="red", label="New")
68
+
69
+ # Spec limits
70
+ if col in limits:
71
+ ll, ul = limits[col]["LL"], limits[col]["UL"]
72
+ ax.axhline(ll, color="green", linestyle="--", linewidth=2, label="LL")
73
+ ax.axhline(ul, color="orange", linestyle="--", linewidth=2, label="UL")
74
+
75
+ ax.set_title(f"{col}")
76
+ ax.set_xlabel("Part # (sequence)")
77
+ ax.set_ylabel("Value")
78
+ ax.set_xticks(x1)
79
+ ax.grid(True, linestyle="--", alpha=0.7)
80
+ ax.legend()
81
+
82
+ plt.tight_layout()
83
+ return fig
84
+
85
+ # Gradio Interface
86
+ iface = gr.Interface(
87
+ fn=plot_excel_data,
88
+ inputs=[
89
+ gr.File(label="Upload IPM_Golden_Data.xlsx (Required)"),
90
+ gr.File(label="Upload IPM_Golden_Data_Manipulated.xlsx (Optional)"),
91
+ ],
92
+ outputs=gr.Plot(label="Comparison Plots"),
93
+ title="Time-Series Data Comparison",
94
+ description="Upload two Excel files to compare time-series data and visualize specification limits. The first file (Golden Data) is required and will be used to extract the limits."
95
+ )
96
+
97
+ if __name__ == "__main__":
98
+ iface.launch()
requirement.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ matplotlib
4
+ numpy
5
+ openpyxl