Spaces:
Runtime error
Runtime error
File size: 5,856 Bytes
1dd7971 | 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 | '''import pandas as pd
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
import io
def calculate_stats(df):
"""Calculate summary statistics from the dataframe."""
if df.empty:
return {
"total": 0,
"defect_count": 0,
"good_count": 0,
"defect_rate": 0.0,
"most_frequent_defect": "None"
}
total = len(df)
defect_count = len(df[df['status'] == 'Defect'])
good_count = len(df[df['status'] == 'Good'])
defect_rate = (defect_count / total) * 100 if total > 0 else 0
# Most frequent defect (excluding 'None' or 'Normal')
defects_only = df[df['status'] == 'Defect']
if not defects_only.empty:
most_frequent_defect = defects_only['defect_type'].mode()[0]
else:
most_frequent_defect = "None"
return {
"total": total,
"defect_count": defect_count,
"good_count": good_count,
"defect_rate": defect_rate,
"most_frequent_defect": most_frequent_defect
}
def get_defect_distribution_chart(df):
"""Generate a pie chart for defect distribution."""
if df.empty:
return None
defects_only = df[df['status'] == 'Defect']
if defects_only.empty:
return None
counts = defects_only['defect_type'].value_counts()
fig, ax = plt.subplots()
ax.pie(counts, labels=counts.index, autopct='%1.1f%%', startangle=90, colors=sns.color_palette("pastel"))
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
ax.set_title("Defect Type Distribution")
return fig
def get_status_distribution_chart(df):
"""Generate a bar chart for Good vs Defect."""
if df.empty:
return None
counts = df['status'].value_counts()
fig, ax = plt.subplots()
sns.barplot(x=counts.index, y=counts.values, ax=ax, hue=counts.index, palette=['#FF9999', '#66B2FF'], legend=False)
ax.set_title("Inspection Status (Good vs Defect)")
ax.set_ylabel("Count")
return fig
def get_trend_chart(df):
"""Generate a line chart for inspections over time."""
if df.empty:
return None
# Resample by hour or day depending on data volume, for now simple plot
# Let's plot inspections over time (index) or bucket by date
df_sorted = df.sort_values('timestamp')
fig, ax = plt.subplots(figsize=(10, 4))
# Plot simply the cumulative count or just localized count?
# Let's plot counts per day/hour
# Group by date-hour
# Just a scatter plot of scores over time is also interesting for QA
sns.lineplot(data=df_sorted, x='timestamp', y='score', hue='status', marker="o", ax=ax)
plt.xticks(rotation=45)
ax.set_title("Anomaly Scores Over Time")
plt.tight_layout()
return fig
'''
import pandas as pd
import matplotlib
matplotlib.use("Agg") # Required for Streamlit Cloud
import matplotlib.pyplot as plt
# ==============================
# STATISTICS
# ==============================
def calculate_stats(df):
"""Calculate summary statistics from the dataframe."""
if df.empty:
return {
"total": 0,
"defect_count": 0,
"good_count": 0,
"defect_rate": 0.0,
"most_frequent_defect": "None"
}
total = len(df)
defect_count = len(df[df["status"] == "Defect"])
good_count = len(df[df["status"] == "Good"])
defect_rate = (defect_count / total) * 100 if total > 0 else 0
defects_only = df[df["status"] == "Defect"]
most_frequent_defect = (
defects_only["defect_type"].mode()[0]
if not defects_only.empty
else "None"
)
return {
"total": total,
"defect_count": defect_count,
"good_count": good_count,
"defect_rate": defect_rate,
"most_frequent_defect": most_frequent_defect
}
# ==============================
# DEFECT DISTRIBUTION (PIE)
# ==============================
def get_defect_distribution_chart(df):
"""Generate a pie chart for defect type distribution."""
defects_only = df[df["status"] == "Defect"]
if defects_only.empty:
return None
counts = defects_only["defect_type"].value_counts()
fig, ax = plt.subplots(figsize=(5, 5))
ax.pie(
counts.values,
labels=counts.index,
autopct="%1.1f%%",
startangle=90
)
ax.axis("equal")
ax.set_title("Defect Type Distribution")
plt.tight_layout()
return fig
# ==============================
# STATUS DISTRIBUTION (BAR)
# ==============================
def get_status_distribution_chart(df):
"""Generate a bar chart for Good vs Defect."""
if df.empty:
return None
counts = df["status"].value_counts()
fig, ax = plt.subplots(figsize=(5, 4))
counts.plot(kind="bar", ax=ax, color=["#66B2FF", "#FF9999"])
ax.set_title("Inspection Status (Good vs Defect)")
ax.set_xlabel("Status")
ax.set_ylabel("Count")
plt.tight_layout()
return fig
# ==============================
# TREND CHART (LINE)
# ==============================
def get_trend_chart(df):
"""Generate a line chart of anomaly scores over time."""
if df.empty or "timestamp" not in df.columns:
return None
df_sorted = df.sort_values("timestamp")
fig, ax = plt.subplots(figsize=(10, 4))
for status in df_sorted["status"].unique():
subset = df_sorted[df_sorted["status"] == status]
ax.plot(
subset["timestamp"],
subset["score"],
marker="o",
label=status
)
ax.set_title("Anomaly Scores Over Time")
ax.set_xlabel("Time")
ax.set_ylabel("Anomaly Score")
ax.legend()
plt.xticks(rotation=45)
plt.tight_layout()
return fig
|