Metadata-Version: 2.4
Name: dimstack
Version: 0.8.0
Author-email: phcreery <phcreery@gmail.com>
License: MIT
Requires-Python: >=3.8
Requires-Dist: jinja2>=3.1.2
Requires-Dist: pandas>=1.5.2
Requires-Dist: plotly>=5.14.1
Requires-Dist: rich>=13.7.0
Requires-Dist: scipy>=1.8.1
Description-Content-Type: text/markdown

# dimstack

Python library for 1D statistical tolerancing analysis and design.

![PyPI - Version](https://img.shields.io/pypi/v/dimstack)
![PyPI - Downloads](https://img.shields.io/pypi/dm/dimstack)
https://pypi.org/project/dimstack/

## Example (MIT Calc Demonstration Analysis)

```python
import dimstack as ds

ds.display.mode("rich")

k = 0.25
target_process_sigma = 6
stdev = 0.036 / target_process_sigma
m1 = ds.dim.Statistical(
    nom=208,
    tol=ds.tol.SymmetricBilateral(0.036),
    distribution=ds.dist.Normal(208 + k * target_process_sigma * stdev, stdev),
    target_process_sigma=target_process_sigma,
    name="a",
    desc="Shaft",
)
m2 = ds.dim.Statistical(
    nom=-1.75,
    tol=ds.tol.UnequalBilateral(0, 0.06),
    target_process_sigma=3,
    name="b",
    desc="Retainer ring",
)
m3 = ds.dim.Statistical(nom=-23, tol=ds.tol.UnequalBilateral(0, 0.12), target_process_sigma=3, name="c", desc="Bearing")
m4 = ds.dim.Statistical(
    nom=20,
    tol=ds.tol.SymmetricBilateral(0.026),
    target_process_sigma=3,
    name="d",
    desc="Bearing Sleeve",
)
m5 = ds.dim.Statistical(nom=-200, tol=ds.tol.SymmetricBilateral(0.145), target_process_sigma=3, name="e", desc="Case")
m6 = ds.dim.Basic(
    nom=20,
    tol=ds.tol.SymmetricBilateral(0.026),
    # target_process_sigma=3,
    name="f",
    desc="Bearing Sleeve",
)
m7 = ds.dim.Statistical(nom=-23, tol=ds.tol.UnequalBilateral(0, 0.12), target_process_sigma=3, name="g", desc="Bearing")
items = [m1, m2, m3, m4, m5, m6, m7]

stack = ds.Stack(name="stacks on stacks", dims=items)

stack.show()
ds.calc.Closed(stack).show()
ds.calc.WC(stack).show()
ds.calc.RSS(stack).show()
ds.calc.MRSS(stack).show()
designed_for = ds.calc.SixSigma(stack, at=4.5)
designed_for.show()

spec = ds.Spec("stack spec", "", distribution=designed_for.distribution, LL=0.05, UL=0.8)
spec.show()

ds.plot.StackPlot().add(stack).add(stack.RSS).show()
```

Returns:

```
                              DIMENSION STACK: stacks on stacks
┏━━━━┳━━━━━━┳━━━━━━━━━━━━━━━━┳━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━┓
┃ ID ┃ Name ┃ Desc.          ┃ ± ┃ Nom.  ┃ Tol.           ┃ Sens. (a) ┃ Abs. Bounds          ┃
┡━━━━╇━━━━━━╇━━━━━━━━━━━━━━━━╇━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━┩
│ 0  │ a    │ Shaft          │ + │ 208.0 │ ± 0.036        │ 1         │ [207.964, 208.036]   │
│ 1  │ b    │ Retainer ring  │ - │ 1.75  │ +0.06 / +0     │ 1         │ [-1.81, -1.75]       │
│ 2  │ c    │ Bearing        │ - │ 23.0  │ +0.12 / +0     │ 1         │ [-23.12, -23]        │
│ 3  │ d    │ Bearing Sleeve │ + │ 20.0  │ ± 0.026        │ 1         │ [19.974, 20.026]     │
│ 4  │ e    │ Case           │ - │ 200.0 │ ± 0.145        │ 1         │ [-200.145, -199.855] │
│ 6  │ g    │ Bearing        │ - │ 23.0  │ +0.12 / +0     │ 1         │ [-23.12, -23]        │
└────┴──────┴────────────────┴───┴───────┴────────────────┴───────────┴──────────────────────┘

                                                                 REVIEWED DIMENSION STACK: stacks on stacks
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━┳━━━━━━┳━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Dim.                                              ┃ Dist.                            ┃ Shift (k) ┃ C_p ┃ C_pk ┃ μ_eff  ┃ σ_eff   ┃ Eff. Sigma ┃ Yield Prob. ┃ Reject PPM ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━╇━━━━━━╇━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ 0: a Shaft                     +   208 ± 0.036    │ Normal Dist. μ=208.009, σ=0.012  │ 0.25      │ 1.0 │ 0.75 │ 208.0  │ 0.016   │ ± 2.25σ    │ 98.76871101 │ 12312.89   │
│ 1: b Retainer ring             -  1.75 +0.06 / +0 │ Normal Dist. μ=-1.78, σ=0.01     │ 0.0       │ 1.0 │ 1.0  │ -1.78  │ 0.01    │ ± 3.0σ     │ 99.73002039 │ 2699.8     │
│ 2: c Bearing                   -    23 +0.12 / +0 │ Normal Dist. μ=-23.06, σ=0.02    │ 0.0       │ 1.0 │ 1.0  │ -23.06 │ 0.02    │ ± 3.0σ     │ 99.73002039 │ 2699.8     │
│ 3: d Bearing Sleeve            +    20 ± 0.026    │ Normal Dist. μ=20.0, σ=0.00867   │ 0.0       │ 1.0 │ 1.0  │ 20.0   │ 0.00867 │ ± 3.0σ     │ 99.73002039 │ 2699.8     │
│ 4: e Case                      -   200 ± 0.145    │ Normal Dist. μ=-200.0, σ=0.04833 │ 0.0       │ 1.0 │ 1.0  │ -200.0 │ 0.04833 │ ± 3.0σ     │ 99.73002039 │ 2699.8     │
│ 6: g Bearing                   -    23 +0.12 / +0 │ Normal Dist. μ=-23.06, σ=0.02    │ 0.0       │ 1.0 │ 1.0  │ -23.06 │ 0.02    │ ± 3.0σ     │ 99.73002039 │ 2699.8     │
└───────────────────────────────────────────────────┴──────────────────────────────────┴───────────┴─────┴──────┴────────┴─────────┴────────────┴─────────────┴────────────┘

                                  DIMENSION: stacks on stacks - Closed Analysis
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃ ID ┃ Name                               ┃ Desc. ┃ ± ┃ Nom.  ┃ Tol.            ┃ Sens. (a) ┃ Abs. Bounds        ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━╇━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ 7  │ stacks on stacks - Closed Analysis │       │ - │ 19.75 │ +0.507 / -0.207 │ 1         │ [-20.257, -19.543] │
└────┴────────────────────────────────────┴───────┴───┴───────┴─────────────────┴───────────┴────────────────────┘

                                 DIMENSION: stacks on stacks - WC Analysis
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━┳━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
┃ ID ┃ Name                           ┃ Desc. ┃ ± ┃ Nom. ┃ Tol.           ┃ Sens. (a) ┃ Abs. Bounds        ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━╇━━━╇━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
│ 8  │ stacks on stacks - WC Analysis │       │ - │ 19.9 │ ± 0.357        │ 1         │ [-20.257, -19.543] │
└────┴────────────────────────────────┴───────┴───┴──────┴────────────────┴───────────┴────────────────────┘

                                                         DIMENSION: stacks on stacks - RSS Analysis
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━┳━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ID ┃ Name                            ┃ Desc.                                            ┃ ± ┃ Nom. ┃ Tol.           ┃ Sens. (a) ┃ Abs. Bounds            ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━╇━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩
│ 9  │ stacks on stacks - RSS Analysis │ (assuming inputs with Normal Dist. & uniform SD) │ - │ 19.9 │ ± 0.17634      │ 1         │ [-20.07634, -19.72366] │
└────┴─────────────────────────────────┴──────────────────────────────────────────────────┴───┴──────┴────────────────┴───────────┴────────────────────────┘

                                                         DIMENSION: stacks on stacks - MRSS Analysis
┏━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━┳━━━━━━┳━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ID ┃ Name                             ┃ Desc.                                            ┃ ± ┃ Nom. ┃ Tol.           ┃ Sens. (a) ┃ Abs. Bounds            ┃
┡━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━╇━━━━━━╇━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩
│ 10 │ stacks on stacks - MRSS Analysis │ (assuming inputs with Normal Dist. & uniform SD) │ - │ 19.9 │ ± 0.23866      │ 1         │ [-20.13866, -19.66134] │
└────┴──────────────────────────────────┴──────────────────────────────────────────────────┴───┴──────┴────────────────┴───────────┴────────────────────────┘

                                                        REVIEWED DIMENSION: stacks on stacks - '6 Sigma' Analysis
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━┳━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Dim.                                             ┃ Dist.                           ┃ Shift (k) ┃ C_p ┃ C_pk ┃ μ_eff ┃ σ_eff   ┃ Eff. Sigma ┃ Yield Prob. ┃ Reject PPM ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━╇━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ 11: stacks on stacks - '6...   -  19.9 ± 0.26877 │ Normal Dist. μ=-19.9, σ=0.05973 │ 0.0       │ 1.5 │ 1.5  │ -19.9 │ 0.05973 │ ± 4.5σ     │ 99.99932047 │ 6.8        │
└──────────────────────────────────────────────────┴─────────────────────────────────┴───────────┴─────┴──────┴───────┴─────────┴────────────┴─────────────┴────────────┘

                                          REQUIREMENT: stack spec
┏━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Name       ┃ Desc. ┃ Distribution                    ┃ Median ┃ Spec. Limits ┃ Yield Prob. ┃ Reject PPM ┃
┡━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ stack spec │       │ Normal Dist. μ=-19.9, σ=0.05973 │ 0.425  │ [0.05, 0.8]  │ 0.0         │ 1000000.0  │
└────────────┴───────┴─────────────────────────────────┴────────┴──────────────┴─────────────┴────────────┘

```

![](./docs/newplot.png)
![](./docs/newplot2.png)

## Usage

dimstack works great as a library in a python script, in REPL, or in JupyterLab.

### and Notebook setup

```
%pip install -q dimstack
```

## Development

### Testing

```
uv run python -m unittest
```

### Documenting

```
python -m mkdocs build
python -m mkdocs serve
python -m mkdocs gh-deploy
```

```
uv run mkdocs build
uv run mkdocs serve
uv run mkdocs gh-deploy
```

### Deploying

First bump version in pyproject.toml, then

```
uv build
uv publish
cp '.\\dist\\*.whl' '.\\notebooks\\pypi\\'
```

# Acknowledgements

- https://d2t1xqejof9utc.cloudfront.net/files/147765/Dimensioning%20and%20Tolerancing%20Handbook.pdf?1541238602
- http://www.newconceptzdesign.com/stackups/
  - https://web.archive.org/web/20221206235926/http://www.newconceptzdesign.com/tutorial/Tutorial-My_first_stackup.html
- https://github.com/slightlynybbled/tol-stack
- https://www.mitcalc.com/doc/tolanalysis1d/help/en/tolanalysis1d.htm
- https://clas.iusb.edu/math-compsci/_prior-thesis/YFeng_thesis.pdf
- https://ris.utwente.nl/ws/portalfiles/portal/6632975/Salomons96computer1.pdf
- https://ris.utwente.nl/ws/files/6632926/Salomons96computer2.pdf
