KPI¶
Overview¶
Key Performance Indicator (KPI) framework for scenario evaluation.
This module defines the foundational classes and enums used to define, compute,
and present KPIs within the Algomancy framework. It integrates with the
algomancy_utils.unit system to provide smart, unit-aware formatting for
performance metrics.
Core concepts:¶
BaseKPI: An abstract base class for all KPIs. It handles the metadata (name, improvement direction), unit scaling, and threshold comparisons.
ImprovementDirection: An enum defining what “better” means for a given metric (e.g., HIGHER is better for throughput, LOWER is better for latency).
Binary vs. Continuous KPIs: KPIs can be simple numeric trackers (continuous) or they can have a threshold (binary), where they are either “successful” or “failed” based on whether they met the threshold.
Why this exists:¶
Scenario results often contain raw data that needs to be distilled into meaningful metrics. This module provides a consistent way to define these metrics, automatically handle their units, and determine if they meet predefined success criteria, making it easy to generate summary reports.
Quick start:¶
Define a concrete KPI class by inheriting from
BaseKPIand implementing thecomputemethod.Instantiate the KPI with a name, improvement direction, and base unit.
Call
compute_and_check(result)to populate the KPI value from scenario results.Use
pretty()to get a human-readable string of the result.
Example
TODO Explain example
1from algomancy_scenario import BaseKPI, ImprovementDirection
2from algomancy_utils import QUANTITIES, BaseMeasurement
3
4class DurationKPI(BaseKPI):
5 def compute(self, result):
6 # Logic to extract duration from result
7 return 1250.5
8
9time = QUANTITIES["time"]
10# Expect duraction of at least 1000 seconds
11kpi = DurationKPI(
12 "System Duration",
13 ImprovementDirection.AT_LEAST,
14 BaseMeasurement(time["s"]),
15 threshold=1000.0
16)
17
18kpi.compute_and_check(some_result_object)
19print(kpi.pretty())
20print(kpi.details())
✓
1.25 ks
Note that the KPI class is normally passed to the app configuration and a separate instance of the KPI
is constructed every Scenario. Typical use of the DurationKPI class would therefore be:
1from algomancy_gui.configuration.appconfig import AppConfig
2from algomancy_scenario.core_configuration import CoreConfig
3
4config = AppConfig(
5 core_config=CoreConfig(
6 ...
7 kpis={"duration": DurationKPI, ...},
8 ...
9 ),
10 ...
11)
Reference¶
- class algomancy_scenario.keyperformanceindicator.ImprovementDirection(*values)[source]¶
Bases:
StrEnumDefines the direction of improvement or success criteria for a KPI.
Members:
HIGHER: The KPI is better when its value is higher (e.g., Accuracy).LOWER: The KPI is better when its value is lower (e.g., Latency).AT_LEAST: A binary success criterion; successful if value >= threshold.AT_MOST: A binary success criterion; successful if value <= threshold.
- HIGHER = 'higher'¶
- LOWER = 'lower'¶
- AT_LEAST = 'at_least'¶
- AT_MOST = 'at_most'¶
- exception algomancy_scenario.keyperformanceindicator.KpiError(message)[source]¶
Bases:
ExceptionException raised for errors during KPI computation or validation.
- Parameters:
message (str) – Explanation of the error.
- class algomancy_scenario.keyperformanceindicator.BaseKPI(name, better_when, base_measurement, threshold=None)[source]¶
Bases:
ABCAbstract base class for all Key Performance Indicators.
A BaseKPI encapsulates the logic for computing a metric from scenario results and formatting it for display. It supports both continuous tracking and binary success/failure checks via thresholds.
- Parameters:
name (str) – The human-readable name of the KPI.
better_when (ImprovementDirection) – The ImprovementDirection for this KPI.
base_measurement (BaseMeasurement) – The BaseMeasurement defining the unit and formatting preferences.
threshold (float | None) – An optional numeric threshold for binary KPIs. Required if better_when is AT_LEAST or AT_MOST.
Notes
Subclasses MUST implement the compute method.
The value of the KPI is typically set via compute_and_check.
- property measurement: Measurement¶
Returns the underlying Measurement object for the KPI value.
- property name: str¶
Returns the name of the KPI.
- property better_when: ImprovementDirection¶
Returns the improvement direction of the KPI.
- property is_binary_kpi: bool¶
Returns True if the KPI has a binary success/failure criterion.
A KPI is binary if its better_when is AT_LEAST or AT_MOST.
- property success: bool¶
Returns True if the KPI meets its threshold criteria.
- Returns:
Boolean indicating success.
- Raises:
ValueError – If the KPI is not binary or if no threshold is defined.
- property value: float | None¶
Returns the current numeric value of the KPI, or None if not yet computed.
- get_threshold_str(unit=None)[source]¶
Returns a formatted string of the KPI’s threshold.
- Parameters:
unit (Unit | None) – Optional unit to scale the threshold to before formatting.
- Returns:
Formatted threshold string.
- Return type:
str
- pretty(unit=None)[source]¶
Returns a human-friendly string of the KPI result.
For binary KPIs, returns “✓” or “✗”. For continuous KPIs, returns the value with its unit.
- Parameters:
unit (Unit | None) – Optional unit to scale the value to.
- Returns:
A string representation of the KPI state or value.
- Return type:
str
- get_pretty_unit()[source]¶
Returns the unit that would be used for pretty-printing the current value.
- details(unit=None)[source]¶
Returns a human-friendly string of the numeric KPI value and unit.
Unlike pretty(), this always returns the numeric value even for binary KPIs.
- Parameters:
unit (Unit | None) – Optional unit to scale the value to.
- Returns:
Formatted string of the value and unit.
- Return type:
str | None
- abstractmethod compute(result)[source]¶
Abstract method to compute the KPI value from scenario results.
- Parameters:
result (BASE_RESULT_BOUND) – The scenario result data.
- Returns:
The computed numeric value.
- Return type:
float
- compute_and_check(result)[source]¶
Computes the KPI value and updates the internal state.
This method calls compute(result), validates that the returned value is numeric, and updates the KPI’s value.
- Parameters:
result (BASE_RESULT_BOUND) – The scenario result data.
- Raises:
KpiError – If computation fails or returns a non-numeric value.