Manuscript Psy03-D@1 · Psy03-D-v1.tex · listing · 3e18951d

Listing: Listing

Matching records and interpolation allowances · lines 2569–2651

Back to manuscript
def matching_record(rows, target, tolerance, curvature=None):
    target, tolerance = float(target), float(tolerance)
    if not math.isfinite(target) or target < 0 \
            or not math.isfinite(tolerance) or tolerance < 0:
        raise ValueError("invalid matching target")
    if curvature is not None:
        curvature = float(curvature)
        if not math.isfinite(curvature) or curvature < 0:
            raise ValueError("invalid curvature certificate")
    checked = []
    for row in rows:
        d = float(row["displacement"])
        if not math.isfinite(d) or d < 0:
            raise ValueError("invalid executed displacement")
        checked.append((d, str(row["candidate_id"]), row))
    if not checked:
        return {"status": "unsupported_target", "target": target}

    nearest = min(
        checked, key=lambda item: (abs(item[0]-target), item[1])
    )
    residual = abs(nearest[0] - target)
    if residual <= tolerance:
        return {
            "status": "executed_match",
            "target": target,
            "candidate": nearest[2],
            "actual_displacement": nearest[0],
            "residual": residual,
            "tolerance": tolerance,
        }

    ordered = sorted(checked, key=lambda item: (item[0], item[1]))
    brackets = []
    for left, right in zip(ordered[:-1], ordered[1:]):
        d0, _, r0 = left
        d1, _, r1 = right
        same_path = r0.get("path_id") is not None \
            and r0.get("path_id") == r1.get("path_id")
        if same_path and d0 < target < d1:
            brackets.append((d1-d0, left, right))
    if not brackets:
        return {
            "status": "unsupported_target",
            "target": target,
            "nearest_candidate_id": nearest[1],
            "nearest_residual": residual,
        }

    _, left, right = min(brackets, key=lambda item: item[0])
    d0, _, r0 = left
    d1, _, r1 = right
    w1 = (target-d0)/(d1-d0)
    w0 = 1.0-w1
    estimate = w0 * float(r0["endpoint"]) + w1 * float(r1["endpoint"])
    allowance = None
    if curvature is not None and r0.get("radius") is not None \
            and r1.get("radius") is not None:
        allowance = (
            0.5 * curvature * (target-d0) * (d1-target)
            + w0 * float(r0["radius"]) + w1 * float(r1["radius"])
        )
    return {
        "status": "bracketed_surrogate",
        "target": target,
        "estimate": estimate,
        "allowance": allowance,
        "endpoint_candidate_ids": (r0["candidate_id"], r1["candidate_id"]),
        "path_id": r0["path_id"],
        "executed_target_checkpoint": False,
        "warning": "path and curvature certificates are caller obligations",
    }

def isoacute_eligible(estimate, radius, target, tolerance):
    values = tuple(map(float, (estimate, radius, target, tolerance)))
    if not all(math.isfinite(v) for v in values):
        raise ValueError("finite calibration values required")
    estimate, radius, target, tolerance = values
    if radius < 0 or tolerance < 0:
        raise ValueError("negative calibration allowance")
    return abs(estimate-target) + radius <= tolerance

Nearby in this section