Skip to main content

cavity-encoding-ml

Cavity encoding and leakage-safe validation for machine learning on multi-cavity injection molding data.

Tests License: MIT

A multi-cavity mold forms several parts in every machine cycle (a shot). When you predict part quality from process data, three questions come up.

  1. Do the cavities differ enough that the cavity label should enter the model?
  2. How should the label be encoded?
  3. How do you validate the model when all parts of a shot share the same machine settings?

cavity-encoding-ml answers them in four steps. It implements the method of Boakye, Hafizh, Lohar and Abdul Majeed (accepted, Journal of Intelligent Manufacturing, 2026) and works with any machine learning library that offers fit and predict.

Step What the package does
1. Diagnostic F test of the cavity effect. It also reports the floor separation, the largest RMSE reduction that cavity information can deliver.
2. Encoding One-hot (default), target or integer encoding of the cavity label, refitted inside every fold.
3. Validation Shot-grouped folds. A LeakageWarning is raised when ordinary K-fold separates the parts of one shot.
4. Decision Compares the floor separation with the fold-to-fold jitter of the cavity-blind model. If the jitter is similar or larger, it reports that the cavity information adds noise.

Install

pip install cavity-encoding-ml

Before the release is on PyPI you can install from GitHub:

pip install "git+https://github.com/nanaboak/cavity-encoding-ml.git"

Python 3.9 or newer. The core needs only NumPy, pandas, SciPy and scikit-learn. Figures need pip install matplotlib.

Try it in one line

cavity-encoding-ml example

This runs the four steps on synthetic tip diameter data that is shipped with the package.

Quick start

from sklearn.ensemble import RandomForestRegressor
from cavity_encoding_ml import CavityConfig, CavityWorkflow
import pandas as pd

df = pd.read_csv("my_parts.csv")          # one row per part

config = CavityConfig(
    shot="ShotID",                         # column that identifies the machine cycle
    cavity="CavityNumber",                 # column with the cavity number of the part
    response="TipDiameter",                # column with the measured characteristic
    shared=["InjPressure", "CoolFlow"],    # machine features, equal for all parts of a shot
    sensors={                              # one cavity sensor type, one column per cavity
        "Max": ["Cav1Max", "Cav2Max", "Cav3Max", "Cav4Max"],
    },
    cavities=[1, 2, 3, 4],
)

result = CavityWorkflow(config, RandomForestRegressor(random_state=0)).run(df)
print(result.report())
print(result.verdict)                      # "use_cavity", "adds_noise" or "no_cavity_effect"

The workflow trains the model three times on identical shot-grouped folds: cavity-blind (R0), with the cavity sensor readings only (R1) and with the cavity label. The report explains in plain sentences how much the readings and the label each lower the error, whether that gain is larger than the normal spread between folds, and whether you should keep the label. Use arms=("blind", "encoded") to skip R1 and save one training run, and audit_kfold=True to also measure how much ordinary K-fold would flatter the result.

If your data have no cavity sensors, leave sensors empty. The package then compares the model with and without the cavity label alone.

Only train a model with the cavity label

If you only want the library to build the features and cross-validate one model, without any comparison:

from cavity_encoding_ml import validate_cavity_encoding

v = validate_cavity_encoding(df, config, RandomForestRegressor(random_state=0),
                             encoding="onehot", arms=("encoded",))
print(v.encoded.rmse, v.encoded.r2)        # one model, shot-grouped folds, leakage check included

Any machine learning library

from cavity_encoding_ml import validate_cavity_encoding, FunctionModel

validate_cavity_encoding(df, config, XGBRegressor())          # any object with fit and predict
validate_cavity_encoding(df, config, lambda: LGBMRegressor()) # or a function that returns one
validate_cavity_encoding(df, config, FunctionModel(my_fit, my_predict))  # PyTorch, TensorFlow, JAX

What is in this repository

Folder or file Content
src/cavity_encoding_ml/ The package
tests/ 138 automatic tests
docs/ Beginner guide, tutorial, method and API reference
examples/ Six small scripts that run as they are
scripts/ Verification against the original code, reproduction on private data, synthetic data, figure
paper/ The JOSS paper (paper.md, paper.bib, figure1.png)

Verification

python scripts/verify_paper3_pipeline.py compares the package with a literal copy of the fold and representation code of the original study. For the random forest the pooled RMSE is exactly equal. For gradient boosting, support vector regression and the multilayer perceptron it agrees to machine precision. On the confidential tip diameter and tensile strength data, the F test reproduces F(3, 285) = 326.4 and F(3, 1513) = 11.4, and the decision matches the original study. The production data are not part of this repository. scripts/reproduce_paper3.py repeats the analysis if you have the files.

Cite

Use the button Cite this repository on GitHub, which reads CITATION.cff, and cite the original study.

Contribute and license

See CONTRIBUTING.md and CODE_OF_CONDUCT.md. The package is released under the MIT license.

Metadata

Release files for cavity-encoding-ml 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cavity-encoding-ml 0.3.0
File Size Uploaded Provenance Actions
cavity_encoding_ml-0.3.0.tar.gz 39.0 kB With provenance Details for cavity_encoding_ml-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cavity-encoding-ml 0.3.0
File Interpreter ABI Platform Actions
cavity_encoding_ml-0.3.0-py3-none-any.whl Python 3 none any Details for cavity_encoding_ml-0.3.0-py3-none-any.whl

Total release size: 72.6 kB

Release files / cavity_encoding_ml-0.3.0.tar.gz

Download URL cavity_encoding_ml-0.3.0.tar.gz
Size 39.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4b73d3de632313f16624ae0c0dc72d965fe3b2a4d780b86b49fe23efbf2d7378
BLAKE2b-256 checksum
How to use checksums
7bfbac71d8407506b0d7b2b4b7a16b333a4a0c555d7ce700fc8a21157d19c09d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Release files / cavity_encoding_ml-0.3.0-py3-none-any.whl

Download URL cavity_encoding_ml-0.3.0-py3-none-any.whl
Size 33.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be381e6684de1df7cff1abaaae76962ec10d90bd53ef6056779baaf8f86a72fc
BLAKE2b-256 checksum
How to use checksums
595ab1e24959555c257dcea73698c11a56c3d11ee4640b1bdb12eb3636e78739
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

Security for cavity-encoding-ml 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for cavity_encoding_ml-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page