Skip to content

Counting

evalica.counting(xs, ys, ws, index=None, win_weight=1.0, tie_weight=0.5, solver='pyo3')

Count individual elements.

Parameters:

Name Type Description Default
xs Collection[T]

The left-hand side elements.

required
ys Collection[T]

The right-hand side elements.

required
ws Collection[Winner]

The winner elements.

required
index dict[T, int] | None

The index.

None
win_weight float

The win weight.

1.0
tie_weight float

The tie weight.

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

The solver.

'pyo3'

Returns:

Type Description
CountingResult[T]

The counting result.

Source code in evalica/__init__.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def counting(
        xs: Collection[T],
        ys: Collection[T],
        ws: Collection[Winner],
        index: dict[T, int] | None = None,
        win_weight: float = 1.,
        tie_weight: float = .5,
        solver: Literal["naive", "pyo3"] = "pyo3",
) -> CountingResult[T]:
    """
    Count individual elements.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        ws: The winner elements.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.

    Returns:
        The counting result.

    """
    assert np.isfinite(win_weight), "win_weight must be finite"
    assert np.isfinite(tie_weight), "tie_weight must be finite"

    xs_indexed, ys_indexed, index = indexing(xs, ys, index)

    assert index is not None, "index is None"

    if solver == "pyo3":
        scores = counting_pyo3(xs_indexed, ys_indexed, ws, len(index), win_weight, tie_weight)
    else:
        scores = counting_naive(xs_indexed, ys_indexed, ws, len(index), win_weight, tie_weight)

    return CountingResult(
        scores=pd.Series(scores, index=index, name=counting.__name__).sort_values(ascending=False, kind="stable"),
        index=index,
        win_weight=win_weight,
        tie_weight=tie_weight,
        solver=solver,
    )

evalica.CountingResult dataclass

Bases: Generic[T]

The counting result.

Attributes:

Name Type Description
scores Series[float]

The element scores.

index dict[T, int]

The index.

win_weight float

The win weight.

tie_weight float

The tie weight.

solver str

The solver.

Source code in evalica/__init__.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
@dataclass(frozen=True)
class CountingResult(Generic[T]):
    """
    The counting result.

    Attributes:
        scores: The element scores.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.

    """

    scores: pd.Series[float]
    index: dict[T, int]
    win_weight: float
    tie_weight: float
    solver: str

evalica.average_win_rate(xs, ys, ws, index=None, win_weight=1.0, tie_weight=0.5, solver='pyo3')

Count pairwise win rates between the elements and average per element.

Parameters:

Name Type Description Default
xs Collection[T]

The left-hand side elements.

required
ys Collection[T]

The right-hand side elements.

required
ws Collection[Winner]

The winner elements.

required
index dict[T, int] | None

The index.

None
win_weight float

The win weight.

1.0
tie_weight float

The tie weight.

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

The solver.

'pyo3'

Returns:

Type Description
AverageWinRateResult[T]

The average win rate result.

Source code in evalica/__init__.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
def average_win_rate(
        xs: Collection[T],
        ys: Collection[T],
        ws: Collection[Winner],
        index: dict[T, int] | None = None,
        win_weight: float = 1.,
        tie_weight: float = .5,
        solver: Literal["naive", "pyo3"] = "pyo3",
) -> AverageWinRateResult[T]:
    """
    Count pairwise win rates between the elements and average per element.

    Args:
        xs: The left-hand side elements.
        ys: The right-hand side elements.
        ws: The winner elements.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.

    Returns:
        The average win rate result.

    """
    assert np.isfinite(win_weight), "win_weight must be finite"
    assert np.isfinite(tie_weight), "tie_weight must be finite"

    xs_indexed, ys_indexed, index = indexing(xs, ys, index)

    assert index is not None, "index is None"

    if solver == "pyo3":
        scores = average_win_rate_pyo3(xs_indexed, ys_indexed, ws, len(index), win_weight, tie_weight)
    else:
        _matrices = matrices(xs_indexed, ys_indexed, ws, index)

        matrix = (win_weight * _matrices.win_matrix + tie_weight * _matrices.tie_matrix).astype(float)

        with np.errstate(invalid="ignore"):
            matrix /= matrix + matrix.T

        with warnings.catch_warnings():
            warnings.filterwarnings("ignore", "Mean of empty slice")

            scores = np.nan_to_num(np.nanmean(matrix, axis=1), copy=False)

    return AverageWinRateResult(
        scores=pd.Series(scores, index=index, name=average_win_rate.__name__).sort_values(
            ascending=False, kind="stable"),
        index=index,
        win_weight=win_weight,
        tie_weight=tie_weight,
        solver=solver,
    )

evalica.AverageWinRateResult dataclass

Bases: Generic[T]

The average win rate result.

Attributes:

Name Type Description
scores Series[float]

The element scores.

index dict[T, int]

The index.

win_weight float

The win weight.

tie_weight float

The tie weight.

solver str

The solver.

Source code in evalica/__init__.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@dataclass(frozen=True)
class AverageWinRateResult(Generic[T]):
    """
    The average win rate result.

    Attributes:
        scores: The element scores.
        index: The index.
        win_weight: The win weight.
        tie_weight: The tie weight.
        solver: The solver.

    """

    scores: pd.Series[float]
    index: dict[T, int]
    win_weight: float
    tie_weight: float
    solver: str