Source code for pyvcad_attribute_resolver.modules.j750_shore_hardness

"""J750 shore-hardness to volume-fractions conversion registrations."""

import pyvcad as pv

from pyvcad_attribute_resolver.registry import register_conversion

from .fitted_model import (
    J750_AGILUS_FRACTION_EXPRESSION,
    J750_AGILUS_FRACTION_RUNTIME_EXPRESSION,
    J750_VERO_FRACTION_RUNTIME_EXPRESSION,
)


def _first_material_id_with_prefix(material_defs, prefix):
    for material_id in range(material_defs.num_materials() + 1):
        try:
            name = material_defs.name(material_id)
        except Exception:
            continue

        if name.startswith(prefix):
            return material_id

    raise ValueError("No material found with prefix '{}'".format(prefix))


def _resolve_material_ids(material_defs, agilus_material, vero_material):
    if agilus_material is None:
        agilus_id = _first_material_id_with_prefix(material_defs, "Agilus30")
    else:
        agilus_id = material_defs.id(agilus_material)

    if vero_material is None:
        vero_id = _first_material_id_with_prefix(material_defs, "Vero")
    else:
        vero_id = material_defs.id(vero_material)

    return agilus_id, vero_id


def _curve_fit_factory(agilus_id, vero_id):
    def _factory():
        return pv.VolumeFractionsExpressionConverter(
            [pv.DefaultAttributes.SHORE_HARDNESS],
            [agilus_id, vero_id],
            [
                J750_AGILUS_FRACTION_RUNTIME_EXPRESSION,
                J750_VERO_FRACTION_RUNTIME_EXPRESSION,
            ],
        )

    return _factory


[docs] def fit_j750_shore_hardness_curve(polynomial_degree=6): """Fit the source CSV and return the expression data. The bundled J750 measurements of Shore hardness against Agilus30 fraction are fitted with an endpoint-constrained polynomial. This is an offline helper for regenerating the fitted model; the registered converter uses the stored model, so adapting a design never imports NumPy or reads the CSV. Arguments: polynomial_degree (int): Degree of the fitted polynomial. Must be at least 2. Returns: dict: The fit, with keys ``polynomial_degree``, ``min_hardness``, ``max_hardness``, ``min_fraction``, ``max_fraction``, ``coefficients``, ``metrics`` (``max_abs_error`` and ``rmse``) and ``expression`` (the Agilus30 fraction as an expression string). Raises: ValueError: If ``polynomial_degree`` is less than 2. """ from .fit_curve import build_expression from .fit_curve import fit_j750_shore_hardness_curve as _fit_curve fit = _fit_curve(polynomial_degree) fit["expression"] = build_expression(fit) return fit
[docs] def generate_j750_shore_hardness_fit_plot(output_path=None, polynomial_degree=6): """Generate the out-of-loop J750 hardness fit SVG figure. Plots the measured Shore hardness against the rigid Vero fraction, with the fitted curve. Arguments: output_path (str | Path, optional): SVG file to write. Parent directories are created. Defaults to ``.tmp/j750_shore_hardness_rigid_fraction_fit.svg`` under the working directory. polynomial_degree (int): Degree of the fitted polynomial. Must be at least 2. Returns: Path: The path of the written figure. """ from .fit_curve import generate_j750_shore_hardness_fit_plot as _generate_plot return _generate_plot(output_path, polynomial_degree)
[docs] def register_j750_shore_hardness_conversions( material_defs=None, agilus_material=None, vero_material=None, ): """Register the J750 shore-hardness to volume-fractions conversion. By default this resolves the first J750 materials whose names begin with ``Agilus30`` and ``Vero``. Callers can override the representative material names when they want a specific color variant. """ if material_defs is None: material_defs = pv.j750_materials agilus_id, vero_id = _resolve_material_ids( material_defs, agilus_material, vero_material, ) register_conversion( source="shore_hardness", target="volume_fractions", converter_factory=_curve_fit_factory(agilus_id, vero_id), name="j750_shore_hardness_to_volume_fractions", priority=0, tags=["j750_shore_hardness"], )
__all__ = [ "J750_AGILUS_FRACTION_EXPRESSION", "fit_j750_shore_hardness_curve", "generate_j750_shore_hardness_fit_plot", "register_j750_shore_hardness_conversions", ]