Developer ToolsFree generatorPDF, Excel, Word exportSemiconductors

Western Electric Rules SPC Python Script Generator for Fab Data

This generator produces a short, dependency-free Python script that reads a column of process measurements from a CSV and flags points that break the four Western Electric run rules. It is a practical starting point for fab SPC automation when you need results outside a commercial SPC package.

Fill in

Generated code
Download:

western_electric_check.py

import csv

CSV_PATH = 'thickness_measurements.csv'
VALUE_COL = 'oxide_thickness_nm'
CENTER = 100.0
SIGMA = 1.2
PARAM = 'Oxide thickness (nm)'


def load_values(path, col):
    values = []
    with open(path, newline='') as handle:
        for row in csv.DictReader(handle):
            values.append(float(row[col]))
    return values


def western_electric(values, center, sigma):
    z = [(v - center) / sigma for v in values]
    flags = []
    for i in range(len(z)):
        if abs(z[i]) > 3:
            flags.append((i, 'Rule 1: point beyond 3 sigma'))
        if i >= 2:
            window = z[i - 2:i + 1]
            for sign in (1, -1):
                if sign * z[i] > 2 and sum(1 for a in window if sign * a > 2) >= 2:
                    flags.append((i, 'Rule 2: 2 of 3 beyond 2 sigma, same side'))
        if i >= 4:
            window = z[i - 4:i + 1]
            for sign in (1, -1):
                if sign * z[i] > 1 and sum(1 for a in window if sign * a > 1) >= 4:
                    flags.append((i, 'Rule 3: 4 of 5 beyond 1 sigma, same side'))
        if i >= 7:
            window = z[i - 7:i + 1]
            if all(a > 0 for a in window) or all(a < 0 for a in window):
                flags.append((i, 'Rule 4: 8 in a row on one side of center'))
    return flags


if __name__ == '__main__':
    data = load_values(CSV_PATH, VALUE_COL)
    print('Parameter:', PARAM, '| points:', len(data))
    results = western_electric(data, CENTER, SIGMA)
    if not results:
        print('No rule violations found.')
    for index, message in results:
        print('Point', index + 1, 'value', data[index], '-', message)

Use control limits from a stable baseline period, not from the data you are testing. The script uses a single point per sample (individuals chart). For subgroup data, compute subgroup means and use the sigma of the means. Rules 2 and 3 also flag the current point only when it is itself in the zone, which matches common implementation practice. Review the rule set your fab specifies, since some sites use Nelson or custom rules.

SPC alarms tied to your ERP lots

Optional: explore linking control chart alarms to lot and work order records.

Optional. The tool and every export work without this. No spam.

How to use it

Enter the CSV path, the column that holds your measurement, and the center line and sigma from your control chart baseline. Copy the script into a file and run it with Python 3. It prints each point that violates a rule, with the sample number, value and rule name. Add the output to a daily review or schedule it after each lot measurement import.

  • Use baseline mean and sigma, not those of the test window
  • Keep rows in time order, oldest first
  • Run it per parameter and per tool for useful signals

The four rules

Rule 1 flags any point beyond three sigma. Rule 2 flags two of three consecutive points beyond two sigma on the same side. Rule 3 flags four of five consecutive points beyond one sigma on the same side. Rule 4 flags eight consecutive points on one side of the center line. Together they catch sudden shifts and slow drifts that a three sigma limit alone misses.

Worked example

With a center of 100 nm and sigma of 1.2 nm, a reading of 103.8 nm is 3.17 sigma and triggers Rule 1. Three readings of 102.6, 100.4 and 102.7 nm have two points beyond 2 sigma (102.4 nm) on the high side and trigger Rule 2 on the third point. Eight consecutive readings all above 100 nm trigger Rule 4 even though none is individually alarming.

Common mistakes

Recomputing limits from the data you are checking hides shifts. Running rules on unsorted data gives false flags. More rules mean more false alarms, so tune the rule set to your false alarm tolerance, and always document the action plan for each alarm, such as an out of control action plan (OCAP).

Frequently Asked Questions

Is this the same as the Nelson rules?

No. The Western Electric rules are a four rule set and the Nelson rules are an eight rule set that overlaps with them. Use the set your quality procedure specifies.

Does the script need any libraries?

No. It uses only the Python standard library csv module, so it runs on any machine with Python 3.

Can I use it for subgroup X-bar charts?

Yes, if you first compute subgroup means and set the center and sigma to the mean and standard error of those means. The script then treats each subgroup mean as one point.

How do I choose center and sigma?

Use the mean and standard deviation from a stable baseline period, or the limits from your released control chart. Avoid using the same data you are evaluating.

If you want SPC alarms tied to ERP lots, tools and work orders, Netray can help connect the data on your own infrastructure.