#!/usr/bin/env python3
"""Pareto analysis of defects: counts or cost/severity-weighted, vital few, 'Other' grouping, stratification,
before/after comparison and an optional SVG chart.
Input: a defect log CSV with a category column (--category, default 'defect'), and optionally a quantity
column (--count) and a cost column (--cost). Each row is one record; without --count each row counts as 1.
Usage:
python pareto.py defects.csv
python pareto.py defects.csv --count qty --cost unit_cost --weight cost
python pareto.py defects.csv --by line # one Pareto per line (stratified)
python pareto.py defects.csv --period month --compare 2026-08 2026-10
python pareto.py defects.csv --other 0.03 --svg > pareto.svg
Warnings:
PA001 'Other' is bigger than the top category: categories are too fine or too vague
PA002 flat Pareto (top 20% of categories < 50% of the total): stratify by another factor
PA003 count and cost Paretos disagree on the top category: decide which matters to the business
Exit codes: 0 printed, 2 usage or data error.
"""
import argparse
import csv
import sys
from collections import Counter, defaultdict
from html import escape
def tally(rows, cat, count, cost, weight):
t = Counter()
for r in rows:
k = (r.get(cat) or "").strip() or "(blank)"
q = float(r[count]) if count and (r.get(count) or "").strip() else 1.0
if weight == "cost":
q *= float(r[cost]) if (r.get(cost) or "").strip() else 0.0
t[k] += q
return t
def group_other(t, frac):
total = sum(t.values()) or 1
keep = Counter({k: v for k, v in t.items() if v / total >= frac})
rest = sum(v for k, v in t.items() if k not in keep)
return keep, rest
def show(t, frac, cut, unit):
keep, other = group_other(t, frac)
total = sum(t.values()) or 1
items = keep.most_common() + ([("Other", other)] if other else [])
print(f"{'Category':<30}{unit:>12}{'%':>7}{'Cum %':>8}")
cum, vital = 0.0, []
for k, v in items:
prev = cum
cum += v
if k != "Other" and prev < cut * total:
vital.append(k)
print(f"{k[:29]:<30}{v:>12,.0f}{100 * v / total:>6.1f}%{100 * cum / total:>7.1f}%" + (" *" if k in vital else ""))
print(f"\nVital few (* up to {cut:.0%}): {', '.join(vital)} = {len(vital)} of {len(t)} categories")
warn = []
if other and keep and other > keep.most_common(1)[0][1]:
warn.append("PA001 'Other' is bigger than the top category: split vague categories or lower --other")
n20 = max(1, round(0.2 * len(t)))
if len(t) >= 5 and sum(v for _, v in t.most_common(n20)) < 0.5 * total:
warn.append("PA002 flat Pareto: no vital few by this category - stratify by line, shift, supplier or part")
return items, warn
def svg(items, title, unit):
W, H, M = 760, 380, 60
total = sum(v for _, v in items) or 1
bw = (W - 2 * M) / max(1, len(items))
top = max(v for _, v in items) or 1
out = [f'<svg xmlns="http://www.w3.org/2000/svg" width="{W}" height="{H}"><rect width="{W}" height="{H}" fill="#fff"/>',
f'<text x="{M}" y="24" font-size="14" font-weight="600">{escape(title)}</text>']
cum, pts = 0, []
for i, (k, v) in enumerate(items):
h = (H - 2 * M - 30) * v / top
x = M + i * bw
out.append(f'<rect x="{x + 4:.1f}" y="{H - M - h:.1f}" width="{bw - 8:.1f}" height="{h:.1f}" fill="#3a6ea5"><title>{escape(k)}: {v:,.0f}</title></rect>')
out.append(f'<text x="{x + bw / 2:.1f}" y="{H - M + 14}" font-size="10" text-anchor="middle">{escape(k[:12])}</text>')
cum += v
pts.append(f"{x + bw / 2:.1f},{H - M - (H - 2 * M - 30) * cum / total:.1f}")
y80 = H - M - (H - 2 * M - 30) * 0.8
out.append(f'<line x1="{M}" x2="{W - M}" y1="{y80:.1f}" y2="{y80:.1f}" stroke="#c0392b" stroke-dasharray="5 4"/>')
out.append(f'<text x="{W - M + 4}" y="{y80 + 4:.1f}" font-size="10">80%</text>')
out.append(f'<polyline points="{" ".join(pts)}" fill="none" stroke="#e67e22" stroke-width="2"/>')
out.append(f'<text x="{M}" y="{H - 12}" font-size="11">{escape(unit)}; line = cumulative %</text></svg>')
return "".join(out)
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
ap.add_argument("csv")
ap.add_argument("--category", default="defect")
ap.add_argument("--count", help="quantity column (default: each row = 1)")
ap.add_argument("--cost", help="cost per unit column (for --weight cost)")
ap.add_argument("--weight", choices=["count", "cost"], default="count")
ap.add_argument("--by", help="stratify: one Pareto per value of this column")
ap.add_argument("--period", help="period column for --compare")
ap.add_argument("--compare", nargs=2, metavar=("BEFORE", "AFTER"))
ap.add_argument("--other", type=float, default=0.0, help="group categories below this share into Other, e.g. 0.03")
ap.add_argument("--cut", type=float, default=0.8)
ap.add_argument("--svg", action="store_true")
a = ap.parse_args()
try:
with open(a.csv, newline="", encoding="utf-8-sig") as fh:
rows = list(csv.DictReader(fh))
if not rows or a.category not in rows[0]:
raise ValueError(f"no '{a.category}' column (use --category)")
if a.weight == "cost" and not a.cost:
raise ValueError("--weight cost needs --cost")
t = tally(rows, a.category, a.count, a.cost, a.weight)
except (OSError, ValueError, KeyError) as e:
print(f"error: {e}", file=sys.stderr)
return 2
unit = "Cost" if a.weight == "cost" else "Count"
if a.svg:
keep, other = group_other(t, a.other)
print(svg(keep.most_common() + ([("Other", other)] if other else []), f"Pareto of {a.category}", unit))
return 0
if a.compare:
if not a.period:
print("error: --compare needs --period", file=sys.stderr)
return 2
b = tally([r for r in rows if r.get(a.period) == a.compare[0]], a.category, a.count, a.cost, a.weight)
f = tally([r for r in rows if r.get(a.period) == a.compare[1]], a.category, a.count, a.cost, a.weight)
tb, tf = sum(b.values()) or 1, sum(f.values()) or 1
print(f"{'Category':<30}{a.compare[0]:>12}{a.compare[1]:>12}{'Change':>9}")
for k in sorted(set(b) | set(f), key=lambda k: -(b[k] + f[k])):
ch = (f[k] - b[k]) / b[k] if b[k] else float("inf")
print(f"{k[:29]:<30}{b[k]:>12,.0f}{f[k]:>12,.0f}{('new' if b[k] == 0 else f'{ch:+.0%}'):>9}")
print(f"{'Total':<30}{tb:>12,.0f}{tf:>12,.0f}{(tf - tb) / tb:>+9.0%}")
return 0
print(f"Pareto of {a.category} by {unit.lower()} ({len(rows)} records)\n")
items, warn = show(t, a.other, a.cut, unit)
if a.cost and a.weight == "count":
tc = tally(rows, a.category, a.count, a.cost, "cost")
if tc and tc.most_common(1)[0][0] != t.most_common(1)[0][0]:
warn.append(f"PA003 top by count is '{t.most_common(1)[0][0]}' but top by cost is '{tc.most_common(1)[0][0]}'")
if a.by:
strata = defaultdict(list)
for r in rows:
strata[(r.get(a.by) or "(blank)").strip()].append(r)
for s, rs in sorted(strata.items()):
ts = tally(rs, a.category, a.count, a.cost, a.weight)
top = ts.most_common(3)
tot = sum(ts.values()) or 1
print(f" {a.by} {s}: total {tot:,.0f}; top " + ", ".join(f"{k} {100 * v / tot:.0f}%" for k, v in top))
for w in warn:
print(w)
return 0
if __name__ == "__main__":
sys.exit(main())
8d-problem-solving-report-writer
Write and review 8D problem-solving reports for customer complaints and serious nonconformities (automotive and aerospace practice: Ford Global 8D, VDA, GM, Stellantis formats): D0 emergency response, D1 cross-functional team and champion, D2 problem description with 5W2H and is / is-not and no cause in the statement, D3 interim containment within 24 hours with verification and clean point, D4 verified occurrence and escape (non-detection) root causes plus systemic cause, D5 permanent corrective actions for each cause verified before implementation, D6 implementation, validation with production data and removal of containment, D7 PFMEA, control plan and read-across to similar products, D8 closure and recognition. Checks an 8D discipline by discipline for missing content, human-error root causes, missing escape cause or action, late containment and dates out of order.
acceptance-sampling-plan-selector
Select and evaluate acceptance sampling plans for incoming, in-process and final inspection: ANSI/ASQ Z1.4 / ISO 2859-1 single sampling under normal inspection (code letter from lot size and inspection level I, II, III, S-1 to S-4; sample size, Ac and Re from AQL, following the table's arrows to the correct plan; 100% when n reaches the lot size; AQLs above 10 per 100 units), operating characteristic curves (Pa at any quality, producer's quality level, indifference point, LTPD, AOQ and AOQL, binomial or hypergeometric), plans designed from producer and consumer risk, and c = 0 zero-acceptance plans. Covers AQL choice by defect class, switching rules between normal, tightened and reduced inspection, when sampling is not appropriate, and random sampling.
approved-supplier-list-manager
Manage and audit an approved supplier list (ASL) / external provider register for ISO 9001 8.4.1, IATF 16949 8.4 and AS9100 8.4.1: supplier status (approved, conditional, probation, pending, disqualified), scope of approval by commodity and process, risk class, certification and expiry, re-evaluation intervals by risk, conditions with end dates, single-source contingency, approver and customer-directed sources. Audits the list for expired or expiring certificates, overdue re-evaluations, conditional or probation status past its decision date, high-risk suppliers without the required certification, single-source suppliers without backup and missing approvers, and checks purchase orders for suppliers that are off-list, disqualified or pending, or buying outside the approved scope. Covers ERP blocks, PO requirements, off-list and emergency purchases.
apqp-phase-planner
Plan and track Advanced Product Quality Planning (AIAG APQP 3rd edition, IATF 16949) for a new or changed automotive part: back-schedule the five phases (plan and define, product design, process design, product and process validation, launch and feedback) from the start-of-production date by program complexity, with overlap, PPAP lead and safe-launch period; list the deliverables and gate for each phase (DFMEA, DVP&R, feasibility, process flow, PFMEA, control plans, MSA, Ppk, run at rate, PPAP, safe launch, lessons learned); adjust for build-to-print suppliers; flag compressed timelines and late or untracked deliverables against a status list. Covers gate reviews, red/yellow/green status reporting, run at rate and safe-launch exit criteria.
as9100-gap-analyzer
Run an AS9100D / EN 9100:2018 gap analysis for aviation, space and defence suppliers on the requirements added to ISO 9001: on-time delivery and conformity focus, management representative, ethics and human-factors awareness, operational planning and operational risk management, configuration management, product safety, counterfeit-part prevention, special requirements, critical items and key characteristics, external provider register and flow-down, FOD prevention, tooling and NC program control, special process validation, first article inspection (AS9102), traceability, in-service data and nonconforming product control. Generates and scores a worksheet, grades missing core controls as major, and checks invalid n/a claims. Covers AS9101/AS9104, OASIS, AS9102 FAI forms, counterfeit standards (AS5553, AS6174, AS6081), NADCAP and the planned IA9100 transition.
audit-report-writer
Write internal, supplier and pre-certification audit reports with the content ISO 19011:2018 6.5 expects: audit objectives, scope, criteria with revisions, dates, team and auditees, method and sampling statement, specific strengths, findings table and three-part finding details (requirement, evidence, statement), conclusion on the degree of fulfilment of criteria, diverging opinions, follow-up deadlines and distribution. Builds a Markdown report from a JSON audit record, generates the conclusion from the findings (major / minor / none) and flags reports missing required content or findings without clause or evidence. Covers conclusion wording, closing meetings, response expectations and verifying corrective actions.