-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
532 lines (430 loc) · 19.7 KB
/
Copy pathanalysis.py
File metadata and controls
532 lines (430 loc) · 19.7 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
"""
RCA/CAPA Analysis Pipeline - analysis.py
Automatically generates actionable insights for manufacturing teams by analyzing
predicted failures and historical CAPA/RCA data to improve product design and
reduce recurring defects.
Features:
- LLM-powered summarization (Qwen/Qwen2.5-3B-Instruct)
- Complaint clustering for pattern detection
- Rich visualizations (8 chart types)
- Manufacturing-focused actionable insights
- Professional PDF report generation
Usage:
python analysis.py # Run with default settings
python analysis.py --test # Run with sample test data
"""
import os
import sys
import time
from datetime import datetime
from pathlib import Path
import numpy as np
import pandas as pd
from tqdm import tqdm
# Local modules
from config import config, Config
from llm_utils import llm, LLMAnalyzer
from clustering import create_clusterer
from visualizations import create_visualizer
# PDF Generation
from fpdf import FPDF
class AnalysisPDFReport(FPDF):
"""Professional PDF report with clean styling - no blank pages."""
def __init__(self):
super().__init__()
self.set_auto_page_break(auto=True, margin=20)
def header(self):
# Simple header
self.set_font("Arial", "B", 16)
self.set_text_color(31, 97, 141)
self.cell(0, 10, "RCA/CAPA Analysis Report", 0, 1, "C")
self.set_font("Arial", "", 9)
self.set_text_color(100, 100, 100)
self.cell(0, 5, f"Generated: {datetime.now().strftime('%B %d, %Y at %H:%M')}", 0, 1, "C")
self.set_draw_color(31, 97, 141)
self.line(10, 25, 200, 25)
self.ln(8)
def footer(self):
self.set_y(-12)
self.set_font("Arial", "I", 8)
self.set_text_color(128, 128, 128)
self.cell(0, 10, f"Page {self.page_no()} - Confidential", 0, 0, "C")
def section_header(self, title: str, color: tuple = (31, 97, 141)):
"""Add a colored section header."""
self.set_font("Arial", "B", 13)
self.set_fill_color(*color)
self.set_text_color(255, 255, 255)
self.cell(0, 8, f" {title}", 0, 1, "L", True)
self.set_text_color(0, 0, 0)
self.ln(3)
def add_text(self, text: str, size: int = 10, bold: bool = False):
"""Add formatted text."""
self.set_font("Arial", "B" if bold else "", size)
self.multi_cell(0, 5, text)
self.ln(1)
def add_metric_row(self, label: str, value: str):
"""Add a key-value metric row."""
self.set_font("Arial", "B", 10)
self.cell(60, 6, label + ":")
self.set_font("Arial", "", 10)
self.cell(0, 6, str(value), 0, 1)
def add_severity_badge(self, severity: str):
"""Add a colored severity badge."""
colors = {
'CRITICAL': (192, 57, 43),
'HIGH': (211, 84, 0),
'MEDIUM': (243, 156, 18),
'LOW': (39, 174, 96)
}
color = colors.get(severity, (127, 140, 141))
self.set_fill_color(*color)
self.set_text_color(255, 255, 255)
self.set_font("Arial", "B", 9)
self.cell(22, 5, severity, 0, 0, "C", True)
self.set_text_color(0, 0, 0)
self.cell(5, 5, "", 0, 0)
def add_image_safe(self, path: str, width: int = 180):
"""Safely add image if it exists."""
if os.path.exists(path):
try:
# Check if we need a new page
if self.get_y() > 200:
self.add_page()
self.image(path, x=15, w=width)
self.ln(5)
return True
except Exception as e:
print(f"[WARN] Could not add image {path}: {e}")
return False
class AnalysisPipeline:
"""Main RCA/CAPA analysis pipeline."""
def __init__(self):
self.df = None
self.results = {}
self.cluster_data = []
self.charts = []
self.clusterer = create_clusterer()
self.visualizer = create_visualizer()
def run(self):
"""Execute the full analysis pipeline."""
start_time = time.time()
print("\n" + "=" * 70)
print(" RCA/CAPA ANALYSIS PIPELINE")
print(" Manufacturing Insights & Defect Reduction")
print("=" * 70)
# Validate config
Config.validate()
# Pipeline steps
self._step1_load_data()
self._step2_analyze_severity()
self._step3_cluster_complaints()
self._step4_generate_insights()
self._step5_create_visualizations()
self._step6_generate_pdf()
elapsed = time.time() - start_time
print(f"\n{'='*70}")
print(f"[COMPLETE] Analysis finished in {elapsed:.1f} seconds")
print(f"[OUTPUT] Report: {config.OUTPUT_DIR / f'{config.REPORT_NAME}.pdf'}")
print("=" * 70)
def _step1_load_data(self):
"""Load complaint data."""
print("\n[STEP 1/6] Loading Data...")
data_path = Path(__file__).parent / config.LOCAL_DATA_PATH
if not data_path.exists():
print(f"[ERROR] Data file not found: {data_path}")
sys.exit(1)
self.df = pd.read_csv(data_path)
print(f" Loaded {len(self.df)} complaint records")
# Ensure required columns
for col in ['Description', 'Component', 'Make', 'Model']:
if col not in self.df.columns:
self.df[col] = 'UNKNOWN'
def _step2_analyze_severity(self):
"""Analyze severity of each complaint."""
print("\n[STEP 2/6] Severity Analysis...")
results = []
for idx, row in tqdm(self.df.iterrows(), total=len(self.df), desc=" Processing"):
sev, score, factors = self._calc_severity(row)
results.append({'Severity': sev, 'Risk_Score': score, 'Risk_Factors': factors})
result_df = pd.DataFrame(results)
self.df['Severity'] = result_df['Severity']
self.df['Risk_Score'] = result_df['Risk_Score']
self.df['Risk_Factors'] = result_df['Risk_Factors']
# Refine components
self.df['Component'] = self.df.apply(
lambda r: self._refine_component(r.get('Description', ''), r.get('Component', '')), axis=1
)
# Store summary
sev_counts = self.df['Severity'].value_counts()
self.results = {
'total': len(self.df),
'critical': sev_counts.get('CRITICAL', 0),
'high': sev_counts.get('HIGH', 0),
'medium': sev_counts.get('MEDIUM', 0),
'low': sev_counts.get('LOW', 0),
'top_components': self.df['Component'].value_counts().head(5).index.tolist(),
'top_makes': self.df['Make'].value_counts().head(5).index.tolist() if 'Make' in self.df.columns else []
}
print(f" Critical: {self.results['critical']} | High: {self.results['high']} | Medium: {self.results['medium']} | Low: {self.results['low']}")
def _calc_severity(self, row) -> tuple:
"""Calculate severity for a single complaint."""
score = 0
factors = []
text = str(row.get('Description', '')).lower()
# Safety flags
if str(row.get('Crash', '')).upper() == 'YES':
score += 40
factors.append("Crash")
if str(row.get('Fire', '')).upper() == 'YES':
score += 50
factors.append("Fire")
deaths = pd.to_numeric(row.get('Deaths', 0), errors='coerce') or 0
injuries = pd.to_numeric(row.get('Injured', 0), errors='coerce') or 0
if deaths > 0:
score += 60
factors.append("Fatality")
if injuries > 0:
score += 25
factors.append(f"Injuries: {int(injuries)}")
# Keyword analysis
critical_kw = ['fire', 'death', 'fatal', 'explosion', 'rollover', 'ejection']
high_kw = ['crash', 'crash', 'failed', 'lost control', 'collision', 'stalled highway']
for kw in critical_kw:
if kw in text:
score += 20
break
for kw in high_kw:
if kw in text:
score += 10
break
# Determine level
if score >= 70:
severity = 'CRITICAL'
elif score >= 40:
severity = 'HIGH'
elif score >= 20:
severity = 'MEDIUM'
else:
severity = 'LOW'
return severity, score, "; ".join(factors) if factors else "Standard Report"
def _refine_component(self, desc: str, current: str) -> str:
"""Refine component classification."""
if current and current not in ['UNKNOWN', 'OTHER', '']:
return current
text = str(desc).lower()
mappings = {
'BRAKES': ['brake', 'rotor', 'pedal', 'abs', 'stopping'],
'ENGINE': ['engine', 'motor', 'stall', 'oil', 'overheat'],
'STEERING': ['steering', 'wheel locked', 'power steering'],
'AIRBAGS': ['airbag', 'air bag', 'srs', 'deploy'],
'POWERTRAIN': ['transmission', 'cvt', 'axle', 'drivetrain'],
'ELECTRICAL': ['electrical', 'battery', 'wiring', 'sensor'],
'FUEL_SYSTEM': ['fuel', 'throttle', 'accelerat'],
'TIRES_WHEELS': ['tire', 'wheel', 'blowout'],
'SUSPENSION': ['suspension', 'strut', 'shock'],
'BODY_STRUCTURE': ['door', 'hood', 'latch', 'sunroof', 'window']
}
for comp, keywords in mappings.items():
if any(kw in text for kw in keywords):
return comp
return current if current else 'OTHER'
def _step3_cluster_complaints(self):
"""Cluster similar complaints."""
print("\n[STEP 3/6] Clustering Analysis...")
if not config.ENABLE_CLUSTERING or len(self.df) < 5:
print(" Skipping clustering (disabled or insufficient data)")
return
descriptions = self.df['Description'].fillna('').tolist()
labels = self.clusterer.fit_transform(descriptions)
self.df['cluster'] = labels
self.cluster_data = self.clusterer.analyze_clusters(self.df)
print(f" Created {len(self.cluster_data)} clusters")
# Generate cluster labels with LLM
if config.ENABLE_LLM and llm.enabled:
print(" Generating cluster labels...")
for cluster in self.cluster_data:
cluster['label'] = llm.summarize_cluster(
cluster.get('descriptions', []),
cluster.get('keywords', [])
)
def _step4_generate_insights(self):
"""Generate insights using LLM."""
print("\n[STEP 4/6] Generating Insights...")
# Executive summary
summary_data = {
'total_complaints': self.results['total'],
'critical_count': self.results['critical'],
'high_count': self.results['high'],
'top_components': self.results['top_components'],
'top_makes': self.results['top_makes'],
'date_range': self._get_date_range(),
'key_patterns': ', '.join([c.get('label', '')[:30] for c in self.cluster_data[:3]])
}
self.results['executive_summary'] = llm.generate_executive_summary(summary_data)
print(" Executive summary generated")
# CAPA recommendations for top issues
top_issues = self.df.nlargest(5, 'Risk_Score')
self.results['top_issues'] = []
for idx, row in top_issues.iterrows():
issue_data = {
'component': row.get('Component', 'Unknown'),
'severity': row.get('Severity', 'Unknown'),
'count': 1,
'safety_impact': row.get('Risk_Factors', '')
}
root_cause = llm.generate_root_cause_hypothesis(
issue_data['component'],
[str(row.get('Description', ''))],
[]
)
recommendations = llm.generate_capa_recommendations(issue_data)
self.results['top_issues'].append({
'data': row.to_dict(),
'root_cause': root_cause,
'recommendations': recommendations
})
print(f" Generated CAPA for {len(self.results['top_issues'])} top issues")
# Manufacturing insights
if self.cluster_data:
self.results['manufacturing_insights'] = llm.generate_manufacturing_insights(self.cluster_data)
print(f" Generated {len(self.results['manufacturing_insights'])} manufacturing insights")
def _get_date_range(self) -> str:
"""Get date range from data."""
for col in ['Date_Complaint', 'Timestamp']:
if col in self.df.columns:
dates = pd.to_datetime(self.df[col], errors='coerce').dropna()
if len(dates) > 0:
return f"{dates.min().strftime('%Y-%m-%d')} to {dates.max().strftime('%Y-%m-%d')}"
return "N/A"
def _step5_create_visualizations(self):
"""Generate all charts."""
print("\n[STEP 5/6] Creating Visualizations...")
if not config.ENABLE_VISUALIZATIONS:
print(" Visualizations disabled")
return
self.charts = self.visualizer.generate_all_charts(self.df, self.cluster_data)
print(f" Created {len(self.charts)} charts")
def _step6_generate_pdf(self):
"""Generate the final PDF report."""
print("\n[STEP 6/6] Generating PDF Report...")
pdf = AnalysisPDFReport()
output_dir = config.OUTPUT_DIR
# === PAGE 1: Executive Summary ===
pdf.add_page()
pdf.section_header("Executive Summary")
summary = self.results.get('executive_summary', 'Analysis complete. See details below.')
pdf.add_text(summary, size=10)
pdf.ln(5)
# Key Metrics Table
pdf.section_header("Key Metrics", (46, 134, 193))
pdf.add_metric_row("Total Complaints", str(self.results['total']))
pdf.add_metric_row("Critical Issues", str(self.results['critical']))
pdf.add_metric_row("High Severity", str(self.results['high']))
pdf.add_metric_row("Medium Severity", str(self.results['medium']))
pdf.add_metric_row("Low Severity", str(self.results['low']))
pdf.add_metric_row("Top Components", ", ".join(self.results['top_components'][:3]))
pdf.add_metric_row("Date Range", self._get_date_range())
pdf.ln(5)
# Severity Distribution Chart
pdf.add_image_safe(str(output_dir / 'severity_distribution.png'), 160)
# === PAGE 2: Top Issues & CAPA ===
pdf.add_page()
pdf.section_header("Top Priority Issues & CAPA Recommendations", (192, 57, 43))
for i, issue in enumerate(self.results.get('top_issues', [])[:5], 1):
data = issue['data']
pdf.set_font("Arial", "B", 11)
pdf.set_text_color(50, 50, 50)
make = data.get('Make', 'N/A')
model = data.get('Model', 'N/A')
comp = data.get('Component', 'N/A')
pdf.cell(0, 6, f"Issue #{i}: {make} {model} - {comp}", 0, 1)
pdf.set_text_color(0, 0, 0)
pdf.add_severity_badge(data.get('Severity', 'UNKNOWN'))
pdf.set_font("Arial", "", 9)
pdf.cell(0, 5, f" Risk Score: {data.get('Risk_Score', 0)}", 0, 1)
# Description snippet
desc = str(data.get('Description', ''))[:150] + "..."
pdf.set_font("Arial", "I", 9)
pdf.multi_cell(0, 4, desc)
pdf.ln(1)
# Root cause
pdf.set_font("Arial", "B", 9)
pdf.set_text_color(0, 100, 0)
pdf.cell(0, 5, "Root Cause:", 0, 1)
pdf.set_text_color(0, 0, 0)
pdf.set_font("Arial", "", 9)
root = str(issue.get('root_cause', 'Under investigation'))[:200]
pdf.multi_cell(0, 4, root)
# CAPA Actions
pdf.set_font("Arial", "B", 9)
pdf.set_text_color(0, 0, 150)
pdf.cell(0, 5, "CAPA Actions:", 0, 1)
pdf.set_text_color(0, 0, 0)
pdf.set_font("Arial", "", 9)
for rec in issue.get('recommendations', [])[:3]:
pdf.cell(5, 4, "", 0, 0)
pdf.multi_cell(0, 4, f"- {rec[:100]}")
pdf.ln(4)
# === PAGE 3: Manufacturing Insights ===
pdf.add_page()
pdf.section_header("Manufacturing Insights & Recommendations", (39, 174, 96))
insights = self.results.get('manufacturing_insights', [])
if insights:
for i, insight in enumerate(insights[:7], 1):
pdf.set_font("Arial", "B", 10)
pdf.cell(8, 6, f"{i}.", 0, 0)
pdf.set_font("Arial", "", 10)
pdf.multi_cell(0, 5, insight[:200])
pdf.ln(2)
else:
pdf.add_text("Enable LLM integration for AI-generated manufacturing insights.", 10)
pdf.ln(3)
# Component failures chart
pdf.add_image_safe(str(output_dir / 'component_failures.png'), 175)
# === PAGE 4: Visual Analytics ===
pdf.add_page()
pdf.section_header("Visual Analytics Dashboard", (155, 89, 182))
# Monthly trend
pdf.add_image_safe(str(output_dir / 'monthly_trend.png'), 175)
# Heatmap
pdf.add_image_safe(str(output_dir / 'severity_heatmap.png'), 175)
# === PAGE 5: More Charts ===
pdf.add_page()
pdf.section_header("Safety & Geographic Analysis", (52, 73, 94))
pdf.add_image_safe(str(output_dir / 'safety_summary.png'), 175)
pdf.add_image_safe(str(output_dir / 'geographic_distribution.png'), 175)
# === PAGE 6: Cluster Analysis ===
if self.cluster_data:
pdf.add_page()
pdf.section_header("Complaint Pattern Clustering", (142, 68, 173))
pdf.add_text("Complaints grouped by similarity to identify systematic failure patterns:", 10)
pdf.ln(3)
for cluster in self.cluster_data[:6]:
label = cluster.get('label', f"Cluster {cluster['cluster_id']}")[:40]
pdf.set_font("Arial", "B", 10)
pdf.cell(0, 5, label, 0, 1)
pdf.set_font("Arial", "", 9)
info = f" Count: {cluster['count']} | Components: {', '.join(cluster.get('components', [])[:2])} | Crashes: {cluster.get('crashes', 0)}"
pdf.cell(0, 4, info, 0, 1)
keywords = ", ".join(cluster.get('keywords', [])[:6])
pdf.set_font("Arial", "I", 8)
pdf.cell(0, 4, f" Keywords: {keywords}", 0, 1)
pdf.ln(2)
pdf.ln(3)
pdf.add_image_safe(str(output_dir / 'cluster_analysis.png'), 175)
# === PAGE 7: Make Distribution ===
pdf.add_page()
pdf.section_header("Manufacturer Analysis", (230, 126, 34))
pdf.add_image_safe(str(output_dir / 'make_distribution.png'), 175)
# Save PDF
output_path = output_dir / f"{config.REPORT_NAME}.pdf"
pdf.output(str(output_path))
print(f" Report saved: {output_path}")
def main():
"""Entry point."""
if '--test' in sys.argv:
os.environ['USE_LOCAL_DATA'] = 'true'
pipeline = AnalysisPipeline()
pipeline.run()
if __name__ == "__main__":
main()