Skip to content

Utilities

evalica.Winner

Bases: Enum

The outcome of the pairwise comparison.

Source code in evalica/evalica.pyi
11
12
13
14
15
16
17
18
19
20
class Winner(Enum):
    """The outcome of the pairwise comparison."""
    X = ...
    """The first element won."""
    Y = ...
    """The second element won."""
    Draw = ...
    """There is a tie."""
    Ignore = ...
    """The comparison should be ignored."""

Draw = ... class-attribute instance-attribute

There is a tie.

Ignore = ... class-attribute instance-attribute

The comparison should be ignored.

X = ... class-attribute instance-attribute

The first element won.

Y = ... class-attribute instance-attribute

The second element won.

evalica.WINNERS = [Winner.X, Winner.Y, Winner.Draw, Winner.Ignore] module-attribute

Known values of Winner.

evalica.indexing(xs, ys, index=None)

Map the input elements into their numerical representations.

Parameters:

Name Type Description Default
xs Collection[T]

The left-hand side elements.

required
ys Collection[T]

The right-hand side elements.

required
index dict[T, int] | None

The pre-computed index.

None

Returns:

Type Description
tuple[list[int], list[int], dict[T, int]]

The tuple containing the numerical representations of the input elements and the corresponding index.

Source code in evalica/__init__.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def indexing(
        xs: Collection[T],
        ys: Collection[T],
        index: dict[T, int] | None = None,
) -> tuple[list[int], list[int], dict[T, int]]:
    """
    Map the input elements into their numerical representations.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        index: The pre-computed index.

    Returns:
        The tuple containing the numerical representations of the input elements and the corresponding index.

    """
    if index is None:
        index = {}
        xy_index = index
    else:
        xy_index = MappingProxyType(index)  # type: ignore[assignment]

    def get_index(x: T) -> int:
        if (idx := xy_index.get(x)) is None:
            idx = xy_index[x] = len(xy_index)

        return idx

    xs_indexed = [get_index(x) for x in xs]
    ys_indexed = [get_index(y) for y in ys]

    return xs_indexed, ys_indexed, index

evalica.matrices(xs_indexed, ys_indexed, ws, index)

Build win and tie matrices from the given elements.

Parameters:

Name Type Description Default
xs_indexed ArrayLike

The left-hand side elements.

required
ys_indexed ArrayLike

The right-hand side elements.

required
ws Collection[Winner]

The winner elements.

required
index dict[T, int]

The index.

required

Returns:

Type Description
MatricesResult[T]

The win and tie matrices.

Source code in evalica/__init__.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
def matrices(
        xs_indexed: npt.ArrayLike,
        ys_indexed: npt.ArrayLike,
        ws: Collection[Winner],
        index: dict[T, int],
) -> MatricesResult[T]:
    """
    Build win and tie matrices from the given elements.

    Args:
        xs_indexed: The left-hand side elements.
        ys_indexed: The right-hand side elements.
        ws: The winner elements.
        index: The index.

    Returns:
        The win and tie matrices.

    """
    win_matrix, tie_matrix = matrices_pyo3(xs_indexed, ys_indexed, ws, len(index))

    return MatricesResult(
        win_matrix=win_matrix,
        tie_matrix=tie_matrix,
        index=index,
    )

evalica.MatricesResult dataclass

Bases: Generic[T]

The win and tie matrices.

Attributes:

Name Type Description
win_matrix NDArray[int64]

The matrix representing wins between the elements.

tie_matrix NDArray[int64]

The matrix representing ties between the elements; it is always symmetric.

index dict[T, int]

The index.

Source code in evalica/__init__.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
@dataclass(frozen=True)
class MatricesResult(Generic[T]):
    """
    The win and tie matrices.

    Attributes:
        win_matrix: The matrix representing wins between the elements.
        tie_matrix: The matrix representing ties between the elements; it is always symmetric.
        index: The index.

    """

    win_matrix: npt.NDArray[np.int64]
    tie_matrix: npt.NDArray[np.int64]
    index: dict[T, int]

evalica.pairwise_frame(scores)

Create a data frame out of the estimated pairwise scores.

Parameters:

Name Type Description Default
scores Series[T]

The element scores.

required

Returns:

Type Description
DataFrame

The data frame representing pairwise scores between the elements.

Source code in evalica/__init__.py
791
792
793
794
795
796
797
798
799
800
801
802
def pairwise_frame(scores: pd.Series[T]) -> pd.DataFrame:  # type: ignore[type-var]
    """
    Create a data frame out of the estimated pairwise scores.

    Args:
        scores: The element scores.

    Returns:
        The data frame representing pairwise scores between the elements.

    """
    return pd.DataFrame(pairwise_scores(scores.to_numpy()), index=scores.index, columns=scores.index)

evalica.pairwise_scores(scores, solver='pyo3')

Estimate the pairwise scores.

Parameters:

Name Type Description Default
scores NDArray[float64]

The element scores.

required
solver Literal['naive', 'pyo3']

The solver.

'pyo3'

Returns:

Type Description
NDArray[float64]

The matrix representing pairwise scores between the elements.

Source code in evalica/__init__.py
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
def pairwise_scores(
    scores: npt.NDArray[np.float64],
    solver: Literal["naive", "pyo3"] = "pyo3",
) -> npt.NDArray[np.float64]:
    """
    Estimate the pairwise scores.

    Args:
        scores: The element scores.
        solver: The solver.

    Returns:
        The matrix representing pairwise scores between the elements.

    """
    if scores.ndim != 1:
        raise ScoreDimensionError(scores.ndim)

    if solver == "naive":
        return pairwise_scores_naive(scores)

    return pairwise_scores_pyo3(scores)

evalica.__version__ = ... module-attribute

The version of Evalica.