hoops_ai.ml.context_layer.NearestNeighborRule

class hoops_ai.ml.context_layer.NearestNeighborRule(threshold=0.95)

Bases: AggregationRule

Use the top-similarity hit’s value when shape similarity is high enough.

Useful for keys whose value is essentially intrinsic to the geometry — number of internal features, hole count, surface area, bounding-box dimensions, … — and therefore better borrowed from the closest shape match than averaged out of a neighborhood. When the top-ranked hit’s score is at or above threshold, this rule returns that hit’s value with confidence equal to the top score. Otherwise it abstains so the predictor returns an STATUS_INSUFFICIENT sentinel.

Pairs naturally with ContextPredictor’s cross-key injection: install this rule for an intrinsic geometric key (InternalFeatures) via per_key_rules, and the resulting STATUS_READY prediction is forwarded into the query_context of every downstream numeric key (Cost, Weight, …) so their RelevanceWeighter can re-rank neighbors with a matching feature count.

Parameters:

threshold (float) – Minimum top-hit similarity required to trust the borrowed value, in [0.0, 1.0]. Default 0.95.

Example

from hoops_ai.ml.context_layer import (

ContextPredictor, NearestNeighborRule, NumericWeightedRule,

)

predictor = ContextPredictor(

provider, per_key_rules={

“InternalFeatures”: NearestNeighborRule(threshold=0.95),

},

)

predict(values, scores, key)

Predict a context value from neighbor evidence.

Parameters:
  • values (list[Any]) – The context values collected from hits for this key, ordered by relevance (best match first).

  • scores (list[float]) – Similarity scores corresponding 1-to-1 with values (higher = more similar).

  • key (str) – The context key being inferred.

Returns:

ContextPrediction or None if insufficient evidence.

Return type:

ContextPrediction | None

predict_with_context(values, scores, key, query_context=None, *, hits=None)

Predict with optional query context and per-hit metadata.

Default implementation ignores query_context and hits and delegates to predict(). Override in subclasses that benefit from one or both.

Parameters:
Return type:

ContextPrediction | None

property threshold: float