File size: 1,123 Bytes
0161e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from dataclasses import dataclass
from typing import Any, Callable, Protocol

from .._types import DEComparison, MetricBestValue, MetricType, PerturbationAnndataPair


class Metric(Protocol):
    """Protocol for metric functions."""

    def __call__(
        self,
        data: PerturbationAnndataPair | DEComparison,
        **kwargs,
    ) -> float | dict[str, float]: ...


@dataclass
class MetricResult:
    """Result of a metric computation."""

    name: str
    value: float | str
    perturbation: str | None = None

    def to_dict(self) -> dict[str, float | str]:
        """Convert result to dictionary."""
        return {
            "perturbation": self.perturbation,  # type: ignore
            "metric": self.name,
            "value": self.value,
        }


@dataclass
class MetricInfo:
    """Information about a registered metric."""

    name: str
    type: MetricType
    func: Callable
    description: str
    best_value: MetricBestValue
    is_class: bool = False
    kwargs: dict[str, Any] | None = None

    def __post_init__(self):
        if self.kwargs is None:
            self.kwargs = {}