Skip to main content

Apply Nelson rules

Project description

nelson_rules

Python 3.12 NumPy

Table of Contents
  1. Background
  2. About the package
  3. Prerequisit
  4. How to install
  5. How to import
  6. How to run with default settings
  7. How to run with other settings
  8. How to access results
  9. How to access further information

Background

The 8 Nelson rules can be used in process control to detect potential anomalies during production. Those rules were defined by Lloyd S. Nelson in the 1980s. The rules and their exemplary visualizations are available on wikipedia.

(back to top)

About the package

The package was built with Numpy and tested with pytest.

By the use of this package, your data is checked for all 8 rules. As input data, a 1D NumPy Array is preferred, but a list of numeric values is also possible. As result, a dictionary is provided, that contains numpy arrays as values for each rule. It furthermore contains the input data and the evaluated zscores.

Results on each Nelson rule are available as 1D arrays with binary values (0, 1). Those results could e.g. be displayed in visuals with potential anomalies (1) being colored in red.

(back to top)

Prerequisit

The input data must be sorted by time - before it is processed by this package.

(back to top)

How to install

pip install nelson-rules

(back to top)

How to import

from nelson_rules import NelsonRules

(back to top)

How to run with default settings

Default settings refer to the values that are stated in the rules above. E.g. for rule 1, we'd check if at least one point is more than 3 standard deviations from the mean. The default values are encoded as follows:

d_rule_settings = {
    "rule1": {"f_std": 3.0},
    "rule2": {"i_points": 9},
    "rule3": {"i_points": 6},
    "rule4": {"i_points": 14},
    "rule5": {"i_points": 3, "i_points_window": 2, "f_std": 2.0},
    "rule6": {"i_points": 5, "i_points_window": 4, "f_std": 1.0},
    "rule7": {"i_points": 15, "f_std": 1.0},
    "rule8": {"i_points": 8, "f_std": 1.0},
}

To check for data points following Nelson rules, the rule setting dictionary is not needed as a parameter, because it is already set as default. Therefore, simply run the following on your data:

nr = NelsonRules(<your_data_in_1D_numpy_array>)
d_results = nr.apply_rules()

(back to top)

How to run with other settings

In case you'd like to run the code with other settings, adapt d_rule_settings to your needs and run:

nr = NelsonRules(<your_data_in_1D_numpy_array>, d_rule_settings)
d_results = nr.apply_rules()

(back to top)

How to access results

The results are available in a dictionary with the following structure:

d_results = {
    "input_data":    [...],
    "zscore":        [...],
    "rule1":         [...],
    "rule2":         [...],
    "rule3":         [...],
    "rule4":         [...],
    "rule5_points":  [...],
    "rule5_windows": [...],
    "rule6_points":  [...],
    "rule6_windows": [...],
    "rule7":         [...],
    "rule8":         [...]
}

You could load the dictionary into a pandas DataFrame (as mentioned above).
Or you could access the data of interest like this:

d_results["rule4"]

For rules 5 and 6, points and windows are available. By this, you could visualize both: the n data points out of m points (window) that fulfill the rule. You can access this data as follows:

d_results["rule5_points"]
d_results["rule5_windows"]

The program is set up with NumPy. However, if you'd like to get the results in a pandas DataFrame, run:

df = pd.DataFrame(data=d_results)

(back to top)

How to access further information

Zscores are available as numpy array and can be accessed as follows:

d_results["zscore"]

Mean and x standard deviations are available as single values each. They can be accessed as follows:

## mean
nr_mean = nr.f_mean

## 1 standard deviation
nr_1std = nr.f_std

## 2 standard deviations
nr_2std = 2 * nr.f_std

## 3 standard deviations
nr_3std = 3 * nr.f_std

(back to top)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nelson_rules-0.1.1.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nelson_rules-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file nelson_rules-0.1.1.tar.gz.

File metadata

  • Download URL: nelson_rules-0.1.1.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for nelson_rules-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7be4db280d36d81820e852939ff4229f3f939c99b313dda1cfff4d85d1ee6292
MD5 cf9a4befcfdd364eb1ee7564b0635992
BLAKE2b-256 af44cca3c46aae294ae55567c470611d5b2ef498679a47960bdc5c909f3d2f0b

See more details on using hashes here.

File details

Details for the file nelson_rules-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: nelson_rules-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for nelson_rules-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25f045127febd0c190c0383e3766ebcad700c65192b2bfa6791a5a89b02c6506
MD5 de3db1abbe0289f854f4e1a557d4e6f3
BLAKE2b-256 a14e8a19441508bb12690ed36c4c9138976460abb46b7c6ddd8e4360afccafee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page