|
| 1 | +# -*- coding: ascii -*- |
| 2 | +""" |
| 3 | +Module for computing aggregated metrics over specific dataset transitions (bisegments). |
| 4 | +""" |
| 5 | + |
| 6 | +__author__ = "Your Name" |
| 7 | +__copyright__ = "Copyright (c) 2026 PySATL project" |
| 8 | +__license__ = "SPDX-License-Identifier: MIT" |
| 9 | + |
| 10 | +from collections.abc import Sequence |
| 11 | +from typing import Any, cast |
| 12 | + |
| 13 | +from pysatl_cpd.benchmark.metrics.aggregation_metric import AggregationMetric |
| 14 | +from pysatl_cpd.benchmark.metrics.multiple_run_metric import MultipleRunMetric |
| 15 | +from pysatl_cpd.core.data_providers.dataset import PandasLabeledDataProvider, SegmentFilter |
| 16 | +from pysatl_cpd.core.online.online_detection_trace import OnlineDetectionTrace |
| 17 | + |
| 18 | + |
| 19 | +class SegmentAggregationMetric[TraceT: OnlineDetectionTrace[Any], ResultInT, ResultOutT]( |
| 20 | + MultipleRunMetric[TraceT, PandasLabeledDataProvider, dict[str, ResultOutT]] |
| 21 | +): |
| 22 | + """ |
| 23 | + Evaluates an aggregation metric exclusively on specific transition types (bisegments). |
| 24 | +
|
| 25 | + This metric slices both the input data providers and their corresponding |
| 26 | + detection traces based on user-provided transition filters. It then groups |
| 27 | + these slices by transition type and computes the underlying base metric for |
| 28 | + each group independently. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + base_agg_metric : AggregationMetric[TraceT, PandasLabeledDataProvider, ResultInT, ResultOutT] |
| 33 | + The underlying metric to compute (e.g., F1Metric, MeanDelayMetric) for each group. |
| 34 | + transition_filters : dict[str, SegmentFilter] |
| 35 | + A mapping where keys are human-readable transition names (e.g., 'A -> B') |
| 36 | + and values are callable predicates that filter bisegments. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__( |
| 40 | + self, |
| 41 | + base_agg_metric: AggregationMetric[TraceT, PandasLabeledDataProvider, ResultInT, ResultOutT], |
| 42 | + transition_filters: dict[str, SegmentFilter], |
| 43 | + ) -> None: |
| 44 | + self._base_agg_metric = base_agg_metric |
| 45 | + self._transition_filters = transition_filters |
| 46 | + |
| 47 | + @property |
| 48 | + def base_agg_metric(self) -> AggregationMetric[TraceT, PandasLabeledDataProvider, ResultInT, ResultOutT]: |
| 49 | + """ |
| 50 | + Returns the underlying aggregation metric instance. |
| 51 | + """ |
| 52 | + |
| 53 | + return self._base_agg_metric |
| 54 | + |
| 55 | + def evaluate(self, runs: Sequence[tuple[TraceT, PandasLabeledDataProvider]]) -> dict[str, ResultOutT]: |
| 56 | + """ |
| 57 | + Evaluate the metric grouped by segment transitions. |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + runs : Sequence[tuple[TraceT, PandasLabeledDataProvider]] |
| 62 | + The full benchmark execution results. |
| 63 | +
|
| 64 | + Returns |
| 65 | + ------- |
| 66 | + dict[str, Rout] |
| 67 | + A dictionary mapping the transition name to the computed metric result. |
| 68 | + If a transition filter matches no segments, it is omitted from the output. |
| 69 | + """ |
| 70 | + |
| 71 | + grouped_runs: dict[str, list[tuple[TraceT, PandasLabeledDataProvider]]] = { |
| 72 | + name: [] for name in self._transition_filters |
| 73 | + } |
| 74 | + |
| 75 | + for trace, provider in runs: |
| 76 | + for trans_name, filter_fn in self._transition_filters.items(): |
| 77 | + sub_providers = provider.query_bisegments(filter_fn) |
| 78 | + sub_indices = provider.query_bisegments_indexes(filter_fn) |
| 79 | + |
| 80 | + for sub_prov, (g_start, _, g_end) in zip(sub_providers, sub_indices, strict=False): |
| 81 | + sub_trace = cast(TraceT, trace.slice(g_start, g_end)) |
| 82 | + grouped_runs[trans_name].append((sub_trace, sub_prov)) |
| 83 | + |
| 84 | + results: dict[str, ResultOutT] = {} |
| 85 | + for trans_name, sub_runs in grouped_runs.items(): |
| 86 | + if sub_runs: |
| 87 | + results[trans_name] = self._base_agg_metric.evaluate(sub_runs) |
| 88 | + |
| 89 | + return results |
0 commit comments