koa_middleware.selector_base

Classes

CalibrationSelector(*args[, origin])

Base class for calibration selectors.

class koa_middleware.selector_base.CalibrationSelector(*args, origin: str | None = None, **kwargs)[source]

Bases: object

Base class for calibration selectors.

This abstract base class defines the interface for selecting one or more calibrations based on input data and a database of available calibrations. Subclasses are expected to implement specific selection logic by overriding methods like get_candidates and select_best.

Required methods to implement:
  • get_candidates: Retrieve a list of candidate calibrations from the database. Optionally, this method can also return a single candidate if desired.

Optional methods to override:
  • select_fallback: Provide a fallback selection mechanism if no candidates are found.

  • select_best: Choose the best calibration(s) from the list of candidates. See method for default behavior.

  • _select: Customize the overall selection workflow by combining candidate retrieval and best selection. This does not call select_fallback; that is handled in select.

get_candidates(input, db: LocalCalibrationDB) list[dict] | dict[source]

Primary method called to retrieve an initial set of candidate calibrations from the local DB. Subclasses must implement this method.

Parameters:
  • input – The input object for which a calibration is to be selected. The exact type depends on the specific selector implementation.

  • db (LocalCalibrationDB) – The database instance for querying.

Returns:

A list of candidate calibration metadata records, or a single candidate records.

Return type:

list[dict] | dict

select(input, db: LocalCalibrationDB) dict | None[source]

Selects the best calibration for the given input data.

This is the primary entry point for calibration selection. It does the following:

  1. Calls the internal _select method to perform the main selection logic, which calls:
    • get_candidates to retrieve candidate calibrations.

    • select_best to choose the best calibration from the candidates.

  2. If no suitable calibration is found, it calls select_fallback to attempt a fallback selection.

Parameters:
  • input – The input object for which a calibration is to be selected. The exact type depends on the specific selector implementation.

  • db (LocalCalibrationDB) – The database instance for querying.

Returns:

result – The selected calibration metadata dictionary. Returns None if no suitable calibration is found, even after fallback.

Return type:

dict | None

select_best(input, candidates: list[dict] | dict) dict | None[source]

Select the best calibration(s) based on the candidates. The default implementation simply returns the first candidate if candidates is a list, or the candidate itself if it’s a dict.

Parameters:
  • input – The input object for which a calibration is to be selected. The exact type depends on the specific selector implementation.

  • candidates (list[dict] | dict) – Candidate calibrations returned from get_candidates().

Returns:

Selected calibration metadata record, or None if no candidates available.

Return type:

dict | None

select_fallback(input, db: LocalCalibrationDB) dict | None[source]

Select a fallback calibration if no suitable candidates are found. Default implementation returns None. This must be overridden by subclasses if needed.

Parameters:
  • input – The input object for which a fallback calibration is to be selected. The exact type depends on the specific selector implementation.

  • db (LocalCalibrationDB) – Database instance for querying.

Returns:

Fallback calibration metadata record, or None if not available.

Return type:

dict | None