elbow_helper.preprocessing module
Phase 1 — cleaning, robust normalization, and global-shape screening.
Turns raw (x, y) into a PreparedCurve on the
unit square or raises Abstain with a reason code when the data are
unusable or globally incompatible with the requested curve/direction.
- exception elbow_helper.preprocessing.Abstain(reason, **diagnostics)[source]
Bases:
ExceptionInternal control-flow signal carrying a reason code and diagnostics.
- elbow_helper.preprocessing.infer_curve_direction(x_norm, y_scaled)[source]
Infer
(curve, direction)from a cleaned, normalized curve.directionis the sign of the Spearman correlation betweenx_normandy_scaled: positive means increasing, negative means decreasing.curveis read off the sign of the (lightly smoothed) curve’s average signed deviation from the chord connecting its first and last point: a curve lying above its chord is concave (the mathematical definition, e.g. a square-root-shaped knee); a curve lying below its chord is convex (e.g. a k-means inertia elbow). Both reads are direction-agnostic, so they combine independently into all four concave/convex x increasing/decreasing cases.- Parameters:
x_norm (numpy.ndarray) – A cleaned, normalized curve on the unit square.
y_scaled (numpy.ndarray) – A cleaned, normalized curve on the unit square.
- Returns:
(curve, direction), each one of the values accepted byprepare_curve().- Return type:
- elbow_helper.preprocessing.prepare_curve(x, y, curve, direction, config)[source]
Clean, sort, deduplicate, normalize, and screen a curve.
- Parameters:
x (array-like) – Raw input coordinates of equal length.
y (array-like) – Raw input coordinates of equal length.
curve (str or None) –
"concave"or"convex"(passed through to the locator). IfNone, inferred from the data viainfer_curve_direction().direction (str or None) –
"increasing"or"decreasing". IfNone, inferred from the data viainfer_curve_direction().config (RobustKneeConfig) – Thresholds;
min_samples,min_spearman_absandmax_direction_violation_rateare consulted here.
- Returns:
The normalized curve plus inverse-transform metadata, with
curveanddirectionresolved to the actual values used (neverNone).- Return type:
- Raises:
Abstain – With
INVALID_INPUT,INSUFFICIENT_DATA,ZERO_RANGEorINCOMPATIBLE_GLOBAL_SHAPEwhen the data cannot be processed.
- elbow_helper.preprocessing.prepare_curve_unconstrained(x, y, min_samples)[source]
Clean, sort, deduplicate and normalize a curve with no shape assumption.
Used by
elbow_helper.robust_knees(): a multi-breakpoint search has no single globalcurve/directionto check against (segments may alternate slope sign freely), so this skips the shape-compatibility gate inprepare_curve()entirely.curve/directionon the returnedPreparedCurveare set to"n/a"and unused.