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), audit_kfold=True).run(df)
print(result.report())
print(result.verdict)                      # "use_cavity", "adds_noise" or "no_cavity_effect"

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

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/ 131 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.1.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.1.0
File Size Uploaded Provenance Actions
cavity_encoding_ml-0.1.0.tar.gz 35.7 kB With provenance Details for cavity_encoding_ml-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 66.6 kB

Release files / cavity_encoding_ml-0.1.0.tar.gz

Download URL cavity_encoding_ml-0.1.0.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
46e54b72c43beb6d683c1c82b18dd19de33d1d88e494f84fd3fb78c1782b0049
BLAKE2b-256 checksum
How to use checksums
f1f39bb774e7246aec49c6cf0523a522b1be6166e787612ed7dbb7ef9942b5b8
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 2, 2026.

Transparency log

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

Download URL cavity_encoding_ml-0.1.0-py3-none-any.whl
Size 30.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a50a7cd2d6ea8efea24f3e981ce650879f3920d58ae55d77dead5d1e260c61a8
BLAKE2b-256 checksum
How to use checksums
c2ef237cb3aafa6e9f8f187cf9650d10092da8a305d18f76ef1005aeab72fda8
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 2, 2026.

Transparency log

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