Skip to content
v0.1.3latest release121 skills4 categories6 sub-categories247 tested scripts397 reference docsContact us
scripts/pfmea_ap.py
#!/usr/bin/env python3
"""Rate a PFMEA with AIAG-VDA Action Priority (AP), legacy RPN, and check the ratings for common errors.

CSV columns: step, function, failure_mode, effect, S, cause, prevention, O, detection, D,
             special (CC/SC/blank), action, owner, due

Checks:
  FM001 high    AP = H without a recommended action (AIAG-VDA: action required or documented justification)
  FM002 high    severity 9-10 with no prevention or detection control
  FM003 high    rating missing or outside 1-10
  FM004 medium  D <= 3 claimed without an error-proofing / automatic detection control (rating inflation)
  FM005 medium  O <= 2 claimed without a prevention control
  FM006 medium  severity 9-10 not marked as a special characteristic (CC) - confirm with the customer's rules
  FM007 low     action without owner or due date

Usage:
    python pfmea_ap.py pfmea.csv
    python pfmea_ap.py pfmea.csv --sort rpn
    python pfmea_ap.py pfmea.csv --csv > pfmea_rated.csv      # adds AP and RPN columns

Exit codes: 0 no high findings, 1 high findings, 2 read error.
"""

import argparse
import csv
import re
import sys

# AIAG-VDA FMEA Handbook (2019) action priority, rows by S band and O band; columns D 7-10, 5-6, 2-4, 1
AP_TABLE = {
    "9-10": {"8-10": "HHHH", "6-7": "HHHH", "4-5": "HHHM", "2-3": "HMLL", "1": "LLLL"},
    "7-8": {"8-10": "HHHH", "6-7": "HHHM", "4-5": "HMMM", "2-3": "MMLL", "1": "LLLL"},
    "4-6": {"8-10": "HHMM", "6-7": "MMML", "4-5": "MLLL", "2-3": "LLLL", "1": "LLLL"},
    "2-3": {"8-10": "MMLL", "6-7": "LLLL", "4-5": "LLLL", "2-3": "LLLL", "1": "LLLL"},
    "1": {"8-10": "LLLL", "6-7": "LLLL", "4-5": "LLLL", "2-3": "LLLL", "1": "LLLL"},
}
ERROR_PROOF = re.compile(r"poka|error[- ]?proof|mistake[- ]?proof|sensor|interlock|automatic|automated|vision|in-?line gauge|100%.*(auto|machine)|go/?no-?go fixture|presence detect", re.I)


def band(v, cuts):
    for label, lo, hi in cuts:
        if lo <= v <= hi:
            return label
    raise ValueError(v)


def action_priority(s, o, d):
    sb = band(s, [("9-10", 9, 10), ("7-8", 7, 8), ("4-6", 4, 6), ("2-3", 2, 3), ("1", 1, 1)])
    ob = band(o, [("8-10", 8, 10), ("6-7", 6, 7), ("4-5", 4, 5), ("2-3", 2, 3), ("1", 1, 1)])
    col = 0 if d >= 7 else 1 if d >= 5 else 2 if d >= 2 else 3
    return AP_TABLE[sb][ob][col]


def rating(v):
    try:
        n = int(str(v).strip())
        return n if 1 <= n <= 10 else None
    except ValueError:
        return None


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("csv", help="PFMEA CSV, or - for stdin")
    ap.add_argument("--sort", choices=["ap", "rpn", "row"], default="ap")
    ap.add_argument("--csv", dest="as_csv", action="store_true", help="print the PFMEA with AP and RPN columns")
    a = ap.parse_args()
    try:
        fh = sys.stdin if a.csv == "-" else open(a.csv, newline="", encoding="utf-8-sig")
        with fh:
            rows = list(csv.DictReader(fh))
    except (OSError, csv.Error) as e:
        print(f"error: {e}", file=sys.stderr)
        return 2
    if not rows or not {"failure_mode", "S", "O", "D"} <= set(rows[0]):
        print("error: PFMEA CSV needs failure_mode, S, O and D columns", file=sys.stderr)
        return 2
    findings, rated = [], []
    for i, r in enumerate(rows, 1):
        g = lambda k: (r.get(k) or "").strip()
        s, o, d = rating(r["S"]), rating(r["O"]), rating(r["D"])
        label = f"row {i} {g('step')} / {g('failure_mode')} / {g('cause')}".replace(" /  /", " /")
        if None in (s, o, d):
            findings.append(("FM003", "high", label, f"S/O/D must be 1-10 (got {r['S']}/{r['O']}/{r['D']})"))
            continue
        prio = action_priority(s, o, d)
        rpn = s * o * d
        rated.append((i, r, s, o, d, prio, rpn))
        if prio == "H" and not g("action"):
            findings.append(("FM001", "high", label, f"AP H (S{s} O{o} D{d}) needs an action or a documented justification"))
        if s >= 9 and not (g("prevention") or g("detection")):
            findings.append(("FM002", "high", label, f"severity {s} with no prevention or detection control"))
        if d <= 3 and not ERROR_PROOF.search(g("detection")):
            findings.append(("FM004", "medium", label, f"D{d} claimed for '{g('detection') or 'no control'}': D 1-3 needs error-proofing or automatic detection"))
        if o <= 2 and not g("prevention"):
            findings.append(("FM005", "medium", label, f"O{o} claimed with no prevention control"))
        if s >= 9 and g("special").upper() not in ("CC", "SC", "KC", "CRITICAL"):
            findings.append(("FM006", "medium", label, f"severity {s}: mark as special characteristic (CC) if the customer's rules require"))
        if g("action") and not (g("owner") and g("due")):
            findings.append(("FM007", "low", label, "action without owner or due date"))
    order = {"H": 0, "M": 1, "L": 2}
    if a.sort == "ap":
        rated.sort(key=lambda x: (order[x[5]], -x[2], -x[6]))
    elif a.sort == "rpn":
        rated.sort(key=lambda x: -x[6])
    if a.as_csv:
        w = csv.writer(sys.stdout, lineterminator="\n")
        cols = list(rows[0].keys())
        w.writerow(cols + ["AP", "RPN"])
        for _, r, *_rest, prio, rpn in rated:
            w.writerow([r.get(c, "") for c in cols] + [prio, rpn])
    else:
        print(f"{'AP':<3}{'S':>3}{'O':>3}{'D':>3}{'RPN':>5}  {'Step':<14}{'Failure mode':<28}Cause")
        for i, r, s, o, d, prio, rpn in rated:
            print(f"{prio:<3}{s:>3}{o:>3}{d:>3}{rpn:>5}  {(r.get('step') or '')[:13]:<14}{(r.get('failure_mode') or '')[:27]:<28}{(r.get('cause') or '')[:40]}")
        counts = {k: sum(x[5] == k for x in rated) for k in "HML"}
        print(f"\nAction priority: H {counts['H']}, M {counts['M']}, L {counts['L']}  ({len(rated)} rated rows)\n")
    sev = {"high": 0, "medium": 1, "low": 2}
    for code, s_, label, msg in sorted(findings, key=lambda f: sev[f[1]]):
        print(f"{code} [{s_}] {label}: {msg}", file=sys.stderr if a.as_csv else sys.stdout)
    return 1 if any(f[1] == "high" for f in findings) else 0


if __name__ == "__main__":
    sys.exit(main())
More in Quality Management & ISO 9001

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.