# Gage R&R Python Script Generator

> Source: https://netray.co/tools/gage-rr-python-script-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in the file name, tolerance, and get a plain Python script that computes the average and range gage R&R.

## Category

developer

## Industry

Discrete Manufacturing

## Audience

- Quality engineer
- Manufacturing engineer
- Data analyst

## Tool

**Kind:** template

**Output:** code

**Language:** python

**Fields:**

#### CSV file path

**Key:** csv_path

**Type:** text

**Default:** grr_study.csv

**Help:** Columns: operator, part, trial, value

#### Tolerance (USL minus LSL)

**Key:** tolerance

**Type:** number

**Default:** 0.050

**Help:** In the same unit as the readings

#### Measurement unit label

**Key:** unit

**Type:** text

**Default:** mm

**Blocks:**

#### gage_rr_average_range.py

**Body:** # 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 = '{{csv_path}}'
TOLERANCE = {{tolerance}}
UNIT = '{{unit}}'

# 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)

**Body:** 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

**Notes:** 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.

## Content

**Intro:** 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.

**Sections:**

#### How to use it

**Body:** 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

**Body:** 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.

**Bullets:**

- 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

**Body:** 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.

**Cta:** If you want gage studies to be run from data in your ERP or a measurement device feed, we can help build that pipeline.

**Faq:**

#### Which Python version is needed?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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?

**Answer:** 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.

## Search terms

gage r&r python, gauge r&r python script, gage r&r csv calculation, average and range method python, msa python code, calculate ndc python, gage r&r automation, grr script

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
