Skip to content
v0.1.3latest release121 skills4 categories6 sub-categories247 tested scripts397 reference docsContact us

gage_rr_anova.py

Helper script for gage-rr-msa-analyzer · 152 lines

Manufacturing & Industrial Operations / Quality Management & ISO 9001v1.0.0Updated OCT 25 20262 references4 scripts
scripts/gage_rr_anova.py
#!/usr/bin/env python3
"""Crossed Gage R&R by the ANOVA method (AIAG MSA 4th ed.): variance components, %contribution, %study
variation, %tolerance, number of distinct categories (ndc), and the operator x part interaction test.

The interaction term is pooled into repeatability when its p-value exceeds --alpha-interaction (default 0.25,
as in AIAG and Minitab). Study variation uses 6 sigma (--k 5.15 for the older 99% convention).

CSV columns: part, operator, value (one row per measurement; balanced: every operator measures every part the
same number of times). Use --part/--operator/--value to rename.

Usage:
    python gage_rr_anova.py grr.csv --tolerance 0.018
    python gage_rr_anova.py grr.csv --lsl 12.000 --usl 12.018 --json

Exit codes: 0 acceptable, 1 conditional or not acceptable, 2 usage or data error.
"""

import argparse
import json
import math
import sys
from statistics import mean

from _msa import load_crossed, verdict


def betacf(a, b, x):
    qab, qap, qam = a + b, a + 1, a - 1
    c, d = 1.0, 1 - qab * x / qap
    d = 1 / (d if abs(d) > 1e-300 else 1e-300)
    h = d
    for m in range(1, 300):
        m2 = 2 * m
        aa = m * (b - m) * x / ((qam + m2) * (a + m2))
        d = 1 + aa * d
        d = 1 / (d if abs(d) > 1e-300 else 1e-300)
        c = 1 + aa / c if abs(1 + aa / c) > 1e-300 else 1e-300
        h *= d * c
        aa = -(a + m) * (qab + m) * x / ((a + m2) * (qap + m2))
        d = 1 + aa * d
        d = 1 / (d if abs(d) > 1e-300 else 1e-300)
        c = 1 + aa / c if abs(1 + aa / c) > 1e-300 else 1e-300
        delta = d * c
        h *= delta
        if abs(delta - 1) < 1e-12:
            break
    return h


def betainc(a, b, x):
    if x <= 0:
        return 0.0
    if x >= 1:
        return 1.0
    lbt = math.lgamma(a + b) - math.lgamma(a) - math.lgamma(b) + a * math.log(x) + b * math.log(1 - x)
    if x < (a + 1) / (a + b + 2):
        return math.exp(lbt) * betacf(a, b, x) / a
    return 1 - math.exp(lbt) * betacf(b, a, 1 - x) / b


def f_pvalue(f, d1, d2):
    return 1 - betainc(d1 / 2, d2 / 2, d1 * f / (d1 * f + d2)) if f > 0 else 1.0


def anova(data, parts, ops, r):
    p, o = len(parts), len(ops)
    allv = [v for pt in parts for op in ops for v in data[pt][op]]
    gm = mean(allv)
    pm = {pt: mean(v for op in ops for v in data[pt][op]) for pt in parts}
    om = {op: mean(v for pt in parts for v in data[pt][op]) for op in ops}
    cm = {(pt, op): mean(data[pt][op]) for pt in parts for op in ops}
    ss_p = o * r * sum((pm[x] - gm) ** 2 for x in parts)
    ss_o = p * r * sum((om[x] - gm) ** 2 for x in ops)
    ss_po = r * sum((cm[(pt, op)] - pm[pt] - om[op] + gm) ** 2 for pt in parts for op in ops)
    ss_e = sum((v - cm[(pt, op)]) ** 2 for pt in parts for op in ops for v in data[pt][op])
    df = {"part": p - 1, "operator": o - 1, "interaction": (p - 1) * (o - 1), "repeat": p * o * (r - 1)}
    ms = {"part": ss_p / df["part"], "operator": ss_o / df["operator"], "interaction": ss_po / df["interaction"],
          "repeat": ss_e / df["repeat"]}
    f_po = ms["interaction"] / ms["repeat"] if ms["repeat"] else float("inf")
    return ss_p, ss_o, ss_po, ss_e, df, ms, f_po


def main():
    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("csv")
    ap.add_argument("--part", default="part")
    ap.add_argument("--operator", default="operator")
    ap.add_argument("--value", default="value")
    ap.add_argument("--tolerance", type=float, help="USL - LSL")
    ap.add_argument("--lsl", type=float)
    ap.add_argument("--usl", type=float)
    ap.add_argument("--k", type=float, default=6.0, help="study variation multiplier (6, or 5.15)")
    ap.add_argument("--alpha-interaction", type=float, default=0.25)
    ap.add_argument("--json", action="store_true")
    a = ap.parse_args()
    try:
        data, parts, ops, r = load_crossed(a.csv, a.part, a.operator, a.value)
    except (OSError, ValueError) as e:
        print(f"error: {e}", file=sys.stderr)
        return 2
    tol = a.tolerance if a.tolerance else (a.usl - a.lsl if a.usl is not None and a.lsl is not None else None)
    p, o = len(parts), len(ops)
    ss_p, ss_o, ss_po, ss_e, df, ms, f_po = anova(data, parts, ops, r)
    p_po = f_pvalue(f_po, df["interaction"], df["repeat"])
    pooled = p_po > a.alpha_interaction
    if pooled:
        mse = (ss_po + ss_e) / (df["interaction"] + df["repeat"])
        var = {"repeatability": mse, "operator": max(0.0, (ms["operator"] - mse) / (p * r)), "interaction": 0.0,
               "part": max(0.0, (ms["part"] - mse) / (o * r))}
    else:
        var = {"repeatability": ms["repeat"], "operator": max(0.0, (ms["operator"] - ms["interaction"]) / (p * r)),
               "interaction": max(0.0, (ms["interaction"] - ms["repeat"]) / r),
               "part": max(0.0, (ms["part"] - ms["interaction"]) / (o * r))}
    var["reproducibility"] = var["operator"] + var["interaction"]
    var["grr"] = var["repeatability"] + var["reproducibility"]
    var["total"] = var["grr"] + var["part"]
    rows = []
    for k in ("grr", "repeatability", "reproducibility", "operator", "interaction", "part", "total"):
        sd = math.sqrt(var[k])
        rows.append((k, var[k], 100 * var[k] / var["total"], 100 * sd / math.sqrt(var["total"]),
                     100 * a.k * sd / tol if tol else None))
    ndc = int(1.41 * math.sqrt(var["part"]) / math.sqrt(var["grr"])) if var["grr"] else 99
    pct = rows[0][4] if tol else rows[0][3]
    v = verdict(pct, ndc)
    if a.json:
        print(json.dumps({"parts": p, "operators": o, "trials": r, "interaction_p": p_po, "interaction_pooled": pooled,
                          "components": {k: {"variance": vv, "pct_contribution": c, "pct_study_var": s, "pct_tolerance": t}
                                         for k, vv, c, s, t in rows}, "ndc": ndc, "verdict": v}, indent=2))
        return 0 if v == "ACCEPTABLE" else 1
    print(f"Crossed Gage R&R (ANOVA): {p} parts x {o} operators x {r} trials\n")
    print(f"Operator x part interaction: F = {f_po:.3f}, p = {p_po:.3f} -> " +
          ("pooled into repeatability" if pooled else "significant, kept in the model"))
    print(f"\n{'Source':<17}{'Variance':>12}{'%Contrib':>10}{'%StudyVar':>11}" + (f"{'%Tolerance':>12}" if tol else ""))
    names = {"grr": "Total Gage R&R", "repeatability": "  Repeatability", "reproducibility": "  Reproducibility",
             "operator": "    Operator", "interaction": "    Oper x Part", "part": "Part-to-part", "total": "Total variation"}
    for k, vv, c, s, t in rows:
        if k == "interaction" and pooled:
            continue
        print(f"{names[k]:<17}{vv:>12.4g}{c:>9.1f}%{s:>10.1f}%" + (f"{t:>11.1f}%" if t is not None else ""))
    print(f"\nNumber of distinct categories (ndc): {ndc}")
    basis = "%Tolerance" if tol else "%Study variation"
    print(f"Verdict on {basis} {pct:.1f}% and ndc {ndc}: {v}")
    if var["repeatability"] > 2 * var["reproducibility"]:
        print("Repeatability dominates: the gauge itself (fixture, resolution, wear, method) needs attention.")
    elif var["reproducibility"] > 2 * var["repeatability"]:
        print("Reproducibility dominates: operators measure differently - standardise the method and train.")
    return 0 if v == "ACCEPTABLE" else 1


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.