hoops_ai.ml.context_layer.ContextPredictor
- class hoops_ai.ml.context_layer.ContextPredictor(context_provider, default_categorical_rule=None, default_numerical_rule=None, per_key_rules=None)
Bases:
objectPredicts engineering context (metadata) for a query part from its nearest neighbors.
Given a list of
VectorHitobjects (fromCADSearch.search_by_shape) and a set of metadata keys to infer, this class fetches per-hit metadata from aContextProvider, extracts evidence per key, and dispatches each key to anAggregationRule.Dispatch precedence:
per_key_rules[key]if present.default_numerical_ruleifkey in context_provider.list_numeric_keys().default_categorical_ruleotherwise.
Defaults are constructed lazily:
CategoricalRule()andNumericWeightedRule()if you do not pass them.- Parameters:
context_provider (ContextProvider) – A
ContextProvidersubclass providing the metadata for indexed parts. Must declare its numeric keys viaContextProvider.list_numeric_keys()if thedefault_numerical_rulepath should fire.default_categorical_rule (AggregationRule | None) – Aggregation rule for non-numeric keys. Defaults to a fresh
CategoricalRule.default_numerical_rule (AggregationRule | None) – Aggregation rule for numeric keys (as declared by
context_provider.list_numeric_keys()). Defaults to a freshNumericWeightedRulewith auto-fittedRelevanceWeighter.per_key_rules (Mapping[str, AggregationRule] | None) – Optional mapping
{key: AggregationRule}that overrides both defaults on a per-key basis.
Example
- from hoops_ai.ml.context_layer import (
ContextPredictor, InMemoryContextProvider, CategoricalRule, NumericWeightedRule,
)
- store = InMemoryContextProvider(
{“part-a”: {“Material”: “Steel”, “Cost”: 12.5}}, numeric_keys=[“Cost”],
) predictor = ContextPredictor(context_provider=store) predictions = predictor.infer(hits, keys=[“Material”, “Cost”]) # “Material” uses CategoricalRule (default), “Cost” uses NumericWeightedRule (default).
- infer(hits, keys, query_context=None, *, return_evidence=False, status_policy=<object object>)
Infer context values from a list of search hits.
Metadata is fetched from the configured
ContextProviderin a single batched call;VectorHit.metadatais not consulted.Every key in
keysis mapped to aContextPrediction— the result is neverNone. When the underlying rule cannot produce a value (margin guard fired, no usable evidence, empty hit list) the returnedContextPredictioncarriesvalue=Noneandstatus=STATUS_INSUFFICIENTso callers can branch on a single, well-typed object.status_policycontrols how status is gated:Not passed →
DEFAULT_STATUS_POLICYis applied. Lenient defaults give every non-abstaining predictionSTATUS_READY; abstentions areSTATUS_INSUFFICIENT.Mapping → the caller’s policy is used (tighten any of
min_observed_hits,min_observed_score_weight,min_top_share,min_margin).Explicitly ``None`` → opt out of status evaluation entirely; predictions are still returned (never
None) but theirstatusisNoneandreasonsis empty.
When
keysmixes categorical and numeric entries, categoricals are inferred first and any prediction reachingSTATUS_READY(ready_to_propose) is merged into thequery_contextpassed to numeric rules. NumericContextPredictionobjects carry the merged context on theirinjected_contextfield so callers can see what conditioned the numeric estimate. The output dict preserves the caller’s key order.
- property context_provider: ContextProvider
- property default_categorical_rule: AggregationRule
- property default_numerical_rule: AggregationRule
- property per_key_rules: Mapping[str, AggregationRule]