Gage R&R Python Script Generator
This generator produces a standard-library Python script that reads a CSV of gage R&R readings and prints repeatability, reproducibility, part variation, percent GRR and ndc. Use it when you want a repeatable calculation outside a spreadsheet.
Fill in
Columns: operator, part, trial, value
In the same unit as the readings
gage_rr_average_range.py
# Gage R&R, average and range method. Standard library only.
# Input CSV columns: operator, part, trial, value
import csv
import math
from collections import defaultdict
CSV_PATH = 'grr_study.csv'
TOLERANCE = 0.050
UNIT = 'mm'
# 5.15-sigma (99 percent) constants from the AIAG MSA average and range method
K1 = {2: 4.56, 3: 3.05}
K2 = {2: 3.65, 3: 2.70}
K3 = {2: 3.65, 3: 2.70, 4: 2.30, 5: 2.08, 6: 1.93, 7: 1.82, 8: 1.74, 9: 1.67, 10: 1.62}
cells = defaultdict(list)
with open(CSV_PATH, newline='') as f:
for row in csv.DictReader(f):
cells[(row['operator'], row['part'])].append(float(row['value']))
operators = sorted({k[0] for k in cells})
parts = sorted({k[1] for k in cells})
trials = len(next(iter(cells.values())))
n, o, r = len(parts), len(operators), trials
if r not in K1 or o not in K2 or n not in K3:
raise SystemExit('Layout not covered by the constants table: %d parts, %d operators, %d trials' % (n, o, r))
# Equipment variation (repeatability)
op_rbar = []
for op in operators:
ranges = [max(cells[(op, p)]) - min(cells[(op, p)]) for p in parts]
op_rbar.append(sum(ranges) / len(ranges))
rbar_bar = sum(op_rbar) / len(op_rbar)
ev = rbar_bar * K1[r]
# Appraiser variation (reproducibility)
op_mean = []
for op in operators:
vals = [v for p in parts for v in cells[(op, p)]]
op_mean.append(sum(vals) / len(vals))
x_diff = max(op_mean) - min(op_mean)
av_sq = (x_diff * K2[o]) ** 2 - (ev ** 2) / (n * r)
av = math.sqrt(av_sq) if av_sq > 0 else 0.0
grr = math.sqrt(ev ** 2 + av ** 2)
# Part variation
part_mean = []
for p in parts:
vals = [v for op in operators for v in cells[(op, p)]]
part_mean.append(sum(vals) / len(vals))
rp = max(part_mean) - min(part_mean)
pv = rp * K3[n]
tv = math.sqrt(grr ** 2 + pv ** 2)
ndc = int(1.41 * pv / grr) if grr > 0 else 0
print('Layout: %d parts, %d operators, %d trials' % (n, o, r))
print('EV (repeatability) : %.5f %s' % (ev, UNIT))
print('AV (reproducibility) : %.5f %s' % (av, UNIT))
print('GRR : %.5f %s' % (grr, UNIT))
print('PV (part variation) : %.5f %s' % (pv, UNIT))
print('TV (total variation) : %.5f %s' % (tv, UNIT))
print('%%GRR of total variation: %.1f %%' % (100 * grr / tv))
print('%%GRR of tolerance : %.1f %%' % (100 * grr / TOLERANCE))
print('ndc : %d' % ndc)grr_study.csv (example layout)
operator,part,trial,value A,1,1,0.652 A,1,2,0.648 A,2,1,0.701 A,2,2,0.705 B,1,1,0.650 B,1,2,0.655 B,2,1,0.698 B,2,2,0.703
Verify the output against a hand calculation or your QMS software on one study before relying on it. The script covers 2 or 3 trials, 2 or 3 operators and 2 to 10 parts, with balanced data. It does not do ANOVA or interaction. Check which MSA edition and spread your customer requires.
Automate gage studies from shop floor data
Optional: discuss pulling measurement data into automated studies tied to your ERP.
Optional. The tool and every export work without this. No spam.
How to use it
Export your study to a CSV with four columns: operator, part, trial and value. Enter the file name, the tolerance and your unit, then copy the script into a file and run it with Python 3. It groups readings by operator and part, applies the K constants for your layout and prints the results. Keep the script and the CSV together as a record of the study.
What the script calculates
EV is the average range multiplied by K1. AV comes from the spread of operator averages and K2, with the repeatability share removed. GRR combines the two. PV is the range of part averages times K3, and TV combines GRR and PV. ndc is 1.41 times PV divided by GRR. Because the constants give a 5.15-sigma spread, percent GRR against tolerance is simply GRR divided by the tolerance.
- Negative values under the square root for AV are set to zero
- ndc is truncated to a whole number, as is customary
- The script exits with a message if the layout has no constant
Limits and checks
The script handles the classic balanced layout only. If any operator has a missing reading, the cell lists will differ in length and the results will be wrong, so check your data first. It does not replace ANOVA, which also estimates the operator by part interaction. Compare against another tool on your first study to confirm the numbers agree.
Frequently Asked Questions
Which Python version is needed?
Any current Python 3 release works. The script only imports csv, math and collections from the standard library, so there is nothing to install.
Does it do ANOVA gage R&R?
No. It implements the average and range method with published constants. ANOVA needs an F-test and variance components, and is better done in dedicated statistical software or a library you validate yourself.
How should the CSV be arranged?
One row per reading with columns operator, part, trial and value. Every operator must measure every part the same number of times. Header names must match exactly as written.
Can I use it for audits?
You can, provided you validate the output against a known worked example and keep the script version with the study record. Many quality systems require validation of software used for acceptance decisions.
If you want gage studies to be run from data in your ERP or a measurement device feed, we can help build that pipeline.
Related Tools
Gage R&R Average and Range Constants (K1, K2, K3) Reference
The K1, K2 and K3 constants for the 5.15-sigma (99 percent) average and range gage R&R method, by number of trials, operators and parts.
Quality & InspectionGage R&R Acceptance Criteria Reference
Look up the usual pass/fail thresholds for %GRR, number of distinct categories, resolution and kappa, with how each is normally read.
Developer ToolsCalibration Due List SQL Query Generator
Fill in your table and column names and get SQL for overdue, due soon and never-calibrated gages.
Quality & InspectionMeasurement System and Calibration Audit Checklist
A practical checklist for internal and supplier audits of gage control, calibration records, MSA studies and traceability.
More Developer Tools tools
See all- OEE SQL Query Generator
- OEE Power BI DAX Measures Generator
- Dimensional Inspection Out-of-Tolerance SQL Query Generator
- SAP Sales Order ABAP SELECT Generator (VBAK, VBAP, VBFA)
- SAP Sales Order Create BAPI ABAP Generator (BAPI_SALESORDER_CREATEFROMDAT2)
- SAP ABAP Open Items SELECT Generator
- SAP ACDOCA SQL Query Generator
- SAP QM Inspection Lot ABAP SELECT Generator
- SAP Quality Notification Pareto SQL Query Generator