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 twice on identical shot-grouped folds, without and with the cavity label, and then decides. Add arms=("blind", "readings", "encoded") to also see the cavity readings without the label (R1), and audit_kfold=True to also measure how much ordinary K-fold would flatter the result. Both are optional and cost extra training time.

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.2.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.2.0
File Size Uploaded Provenance Actions
cavity_encoding_ml-0.2.0.tar.gz 37.0 kB With provenance Details for cavity_encoding_ml-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 68.8 kB

Release files / cavity_encoding_ml-0.2.0.tar.gz

Download URL cavity_encoding_ml-0.2.0.tar.gz
Size 37.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d29d2ee6428c2e234a61147ca1ef58f41454a0faf1eb0e0cb4547f65b8392e79
BLAKE2b-256 checksum
How to use checksums
381200152b00ab6690e6b928bf7e7152742cac27203efdf19e321823b8535d27
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 3, 2026.

Transparency log

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

Download URL cavity_encoding_ml-0.2.0-py3-none-any.whl
Size 31.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b4015334c59131566496fbba325c7ed3309d504e4e32ebb3c2bdceaa374748ce
BLAKE2b-256 checksum
How to use checksums
7e21ff6f5be58729f6a22517dbe8f481dbeca8b8bdf71d73ddb332ae63ad8a93
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 3, 2026.

Transparency log

Security for cavity-encoding-ml 0.2.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.2.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 3, 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