@arizeai/phoenix-evals 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +39 -0
  2. package/dist/esm/code/classificationMetrics.d.ts +121 -0
  3. package/dist/esm/code/classificationMetrics.d.ts.map +1 -0
  4. package/dist/esm/code/classificationMetrics.js +269 -0
  5. package/dist/esm/code/classificationMetrics.js.map +1 -0
  6. package/dist/esm/code/createClassificationMetricEvaluator.d.ts +16 -0
  7. package/dist/esm/code/createClassificationMetricEvaluator.d.ts.map +1 -0
  8. package/dist/esm/code/createClassificationMetricEvaluator.js +23 -0
  9. package/dist/esm/code/createClassificationMetricEvaluator.js.map +1 -0
  10. package/dist/esm/code/createF1Evaluator.d.ts +23 -0
  11. package/dist/esm/code/createF1Evaluator.d.ts.map +1 -0
  12. package/dist/esm/code/createF1Evaluator.js +23 -0
  13. package/dist/esm/code/createF1Evaluator.js.map +1 -0
  14. package/dist/esm/code/createFBetaEvaluator.d.ts +23 -0
  15. package/dist/esm/code/createFBetaEvaluator.d.ts.map +1 -0
  16. package/dist/esm/code/createFBetaEvaluator.js +27 -0
  17. package/dist/esm/code/createFBetaEvaluator.js.map +1 -0
  18. package/dist/esm/code/createPrecisionEvaluator.d.ts +22 -0
  19. package/dist/esm/code/createPrecisionEvaluator.d.ts.map +1 -0
  20. package/dist/esm/code/createPrecisionEvaluator.js +25 -0
  21. package/dist/esm/code/createPrecisionEvaluator.js.map +1 -0
  22. package/dist/esm/code/createPrecisionRecallFScoreEvaluators.d.ts +23 -0
  23. package/dist/esm/code/createPrecisionRecallFScoreEvaluators.d.ts.map +1 -0
  24. package/dist/esm/code/createPrecisionRecallFScoreEvaluators.js +44 -0
  25. package/dist/esm/code/createPrecisionRecallFScoreEvaluators.js.map +1 -0
  26. package/dist/esm/code/createRecallEvaluator.d.ts +22 -0
  27. package/dist/esm/code/createRecallEvaluator.d.ts.map +1 -0
  28. package/dist/esm/code/createRecallEvaluator.js +25 -0
  29. package/dist/esm/code/createRecallEvaluator.js.map +1 -0
  30. package/dist/esm/code/index.d.ts +7 -0
  31. package/dist/esm/code/index.d.ts.map +1 -0
  32. package/dist/esm/code/index.js +7 -0
  33. package/dist/esm/code/index.js.map +1 -0
  34. package/dist/esm/index.d.ts +1 -0
  35. package/dist/esm/index.d.ts.map +1 -1
  36. package/dist/esm/index.js +1 -0
  37. package/dist/esm/index.js.map +1 -1
  38. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  39. package/dist/src/code/classificationMetrics.d.ts +121 -0
  40. package/dist/src/code/classificationMetrics.d.ts.map +1 -0
  41. package/dist/src/code/classificationMetrics.js +274 -0
  42. package/dist/src/code/classificationMetrics.js.map +1 -0
  43. package/dist/src/code/createClassificationMetricEvaluator.d.ts +16 -0
  44. package/dist/src/code/createClassificationMetricEvaluator.d.ts.map +1 -0
  45. package/dist/src/code/createClassificationMetricEvaluator.js +26 -0
  46. package/dist/src/code/createClassificationMetricEvaluator.js.map +1 -0
  47. package/dist/src/code/createF1Evaluator.d.ts +23 -0
  48. package/dist/src/code/createF1Evaluator.d.ts.map +1 -0
  49. package/dist/src/code/createF1Evaluator.js +26 -0
  50. package/dist/src/code/createF1Evaluator.js.map +1 -0
  51. package/dist/src/code/createFBetaEvaluator.d.ts +23 -0
  52. package/dist/src/code/createFBetaEvaluator.d.ts.map +1 -0
  53. package/dist/src/code/createFBetaEvaluator.js +30 -0
  54. package/dist/src/code/createFBetaEvaluator.js.map +1 -0
  55. package/dist/src/code/createPrecisionEvaluator.d.ts +22 -0
  56. package/dist/src/code/createPrecisionEvaluator.d.ts.map +1 -0
  57. package/dist/src/code/createPrecisionEvaluator.js +28 -0
  58. package/dist/src/code/createPrecisionEvaluator.js.map +1 -0
  59. package/dist/src/code/createPrecisionRecallFScoreEvaluators.d.ts +23 -0
  60. package/dist/src/code/createPrecisionRecallFScoreEvaluators.d.ts.map +1 -0
  61. package/dist/src/code/createPrecisionRecallFScoreEvaluators.js +47 -0
  62. package/dist/src/code/createPrecisionRecallFScoreEvaluators.js.map +1 -0
  63. package/dist/src/code/createRecallEvaluator.d.ts +22 -0
  64. package/dist/src/code/createRecallEvaluator.d.ts.map +1 -0
  65. package/dist/src/code/createRecallEvaluator.js +28 -0
  66. package/dist/src/code/createRecallEvaluator.js.map +1 -0
  67. package/dist/src/code/index.d.ts +7 -0
  68. package/dist/src/code/index.d.ts.map +1 -0
  69. package/dist/src/code/index.js +23 -0
  70. package/dist/src/code/index.js.map +1 -0
  71. package/dist/src/index.d.ts +1 -0
  72. package/dist/src/index.d.ts.map +1 -1
  73. package/dist/src/index.js +1 -0
  74. package/dist/src/index.js.map +1 -1
  75. package/dist/tsconfig.tsbuildinfo +1 -1
  76. package/docs/classification-metrics.mdx +103 -0
  77. package/docs/overview.mdx +4 -1
  78. package/package.json +24 -16
  79. package/src/code/classificationMetrics.ts +464 -0
  80. package/src/code/createClassificationMetricEvaluator.ts +49 -0
  81. package/src/code/createF1Evaluator.ts +32 -0
  82. package/src/code/createFBetaEvaluator.ts +41 -0
  83. package/src/code/createPrecisionEvaluator.ts +36 -0
  84. package/src/code/createPrecisionRecallFScoreEvaluators.ts +83 -0
  85. package/src/code/createRecallEvaluator.ts +36 -0
  86. package/src/code/index.ts +6 -0
  87. package/src/index.ts +1 -0
@@ -0,0 +1,121 @@
1
+ /**
2
+ * A label produced by a classifier. Must be a primitive so it can be compared
3
+ * for equality and used as a lookup key.
4
+ */
5
+ export type ClassificationLabel = string | number;
6
+ /**
7
+ * The strategy used to aggregate per-class precision/recall/F-score into a
8
+ * single number when there are more than two classes (or no `positiveLabel`
9
+ * is configured).
10
+ *
11
+ * - `"macro"`: unweighted mean across classes.
12
+ * - `"micro"`: pool true/false positives and false negatives across classes
13
+ * before computing the metric.
14
+ * - `"weighted"`: mean across classes, weighted by each class's support
15
+ * (number of true instances).
16
+ */
17
+ export type AverageType = "macro" | "micro" | "weighted";
18
+ /**
19
+ * The example shape expected by the classification-metric evaluators.
20
+ */
21
+ export interface ClassificationExample {
22
+ /** The ground-truth sequence of labels. */
23
+ expected: ClassificationLabel[];
24
+ /** The predicted sequence of labels, aligned by index with `expected`. */
25
+ output: ClassificationLabel[];
26
+ [key: string]: unknown;
27
+ }
28
+ /**
29
+ * Options shared by precision, recall, and F-score computations.
30
+ */
31
+ export interface PrecisionRecallFScoreOptions {
32
+ /**
33
+ * Weight of recall relative to precision in the F-score. Must be > 0.
34
+ * @defaultValue 1
35
+ */
36
+ beta?: number;
37
+ /**
38
+ * Aggregation strategy across classes. Ignored when `positiveLabel` is set
39
+ * (or auto-detected).
40
+ * @defaultValue "macro"
41
+ */
42
+ average?: AverageType;
43
+ /**
44
+ * Value substituted for a metric when it is undefined (e.g. 0/0).
45
+ * @defaultValue 0
46
+ */
47
+ zeroDivision?: number;
48
+ /**
49
+ * When set, compute binary precision/recall/F exclusively for this label
50
+ * (one-vs-rest). If not set, `average` is at its default `"macro"`, and
51
+ * the labels are the numeric set `{0, 1}`, the positive label defaults to
52
+ * `1`. Otherwise, multi-class averaging is used. The auto-detection is
53
+ * skipped whenever a non-default `average` is configured, so an explicit
54
+ * `average` is never silently overridden by the shape of the data.
55
+ */
56
+ positiveLabel?: ClassificationLabel;
57
+ }
58
+ /**
59
+ * The result of computing precision, recall, and F-score for a batch of
60
+ * predictions.
61
+ */
62
+ export interface PrecisionRecallFScoreResult {
63
+ precision: number;
64
+ recall: number;
65
+ fScore: number;
66
+ beta: number;
67
+ average: AverageType;
68
+ /** All labels observed in `expected` and `output`, in first-seen order. */
69
+ labels: ClassificationLabel[];
70
+ /** The label treated as positive in one-vs-rest mode, or `null` if multi-class averaging was used. */
71
+ positiveLabel: ClassificationLabel | null;
72
+ }
73
+ /**
74
+ * Computes precision, recall, and F-beta score for a batch of expected vs.
75
+ * predicted labels.
76
+ *
77
+ * `expected`/`output` are the full sequence of labels across an entire
78
+ * dataset, not a single row — this and the evaluators built on it are
79
+ * dataset-level, unlike the package's per-row LLM evaluators. Call it once
80
+ * over every row's collected labels rather than wiring it into a per-row
81
+ * pipeline (e.g. `runExperiment`'s per-row evaluators).
82
+ *
83
+ * Supports both binary classification (via `positiveLabel`, or
84
+ * auto-detected when `average` is at its default `"macro"` and the labels
85
+ * are the numeric set `{0, 1}`) and multi-class classification (via the
86
+ * `average` strategy).
87
+ *
88
+ * @example Multi-class (macro average)
89
+ * ```typescript
90
+ * computePrecisionRecallFScore({
91
+ * expected: ["cat", "dog", "cat", "bird"],
92
+ * output: ["cat", "cat", "cat", "bird"],
93
+ * });
94
+ * // { precision: 5/9, recall: 2/3, fScore: 0.6, beta: 1, average: "macro", ... }
95
+ * ```
96
+ *
97
+ * @example Binary with an explicit positive label
98
+ * ```typescript
99
+ * computePrecisionRecallFScore(
100
+ * { expected: ["spam", "ham", "spam"], output: ["spam", "spam", "ham"] },
101
+ * { beta: 0.5, positiveLabel: "spam" }
102
+ * );
103
+ * ```
104
+ */
105
+ export declare function computePrecisionRecallFScore({ expected, output }: Pick<ClassificationExample, "expected" | "output">, options?: PrecisionRecallFScoreOptions): PrecisionRecallFScoreResult;
106
+ /**
107
+ * Formats a beta value for use in metric names, e.g. `1` -> `"f1"`,
108
+ * `0.5` -> `"f0_5"`.
109
+ */
110
+ export declare function formatBetaForMetricName(beta: number): string;
111
+ /**
112
+ * The suffix appended to a metric name to reflect the aggregation strategy,
113
+ * e.g. `"precision"` vs. `"precision_micro"`. No suffix is used when
114
+ * `positiveLabel` is explicitly configured, since `average` is not
115
+ * applicable in that one-vs-rest binary mode. This mirrors
116
+ * `resolvePositiveLabel`'s auto-detection rule (only under the default
117
+ * `"macro"` average) so a constructed evaluator's static name always
118
+ * matches what `computePrecisionRecallFScore` actually computes.
119
+ */
120
+ export declare function getAverageMetricNameSuffix({ average, positiveLabel, }: PrecisionRecallFScoreOptions): string;
121
+ //# sourceMappingURL=classificationMetrics.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classificationMetrics.d.ts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AAI,EAAE,MAAM,GAAG,MAAM,CAQ5D;AAED;;;;;;;;GAQG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,OAAiB,EACjB,aAAa,GACd,EAAE,4BAA4B,GAAG,MAAM,CAKvC"}
@@ -0,0 +1,274 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.computePrecisionRecallFScore = computePrecisionRecallFScore;
4
+ exports.formatBetaForMetricName = formatBetaForMetricName;
5
+ exports.getAverageMetricNameSuffix = getAverageMetricNameSuffix;
6
+ function validateBeta(beta) {
7
+ if (!(beta > 0)) {
8
+ throw new Error("beta must be > 0");
9
+ }
10
+ }
11
+ function validateAverage(average) {
12
+ if (average !== "macro" && average !== "micro" && average !== "weighted") {
13
+ throw new Error('average must be one of "macro", "micro", or "weighted"');
14
+ }
15
+ }
16
+ function validateLabelSequences(expected, output) {
17
+ if (!Array.isArray(expected) || !Array.isArray(output)) {
18
+ throw new Error("expected and output must be arrays of labels");
19
+ }
20
+ if (expected.length !== output.length) {
21
+ throw new Error(`expected and output must have the same length. Got ${expected.length} and ${output.length}`);
22
+ }
23
+ if (expected.length === 0) {
24
+ throw new Error("expected and output must be non-empty");
25
+ }
26
+ }
27
+ function safeDivide(numerator, denominator, zeroDivision) {
28
+ if (denominator === 0) {
29
+ return zeroDivision;
30
+ }
31
+ return numerator / denominator;
32
+ }
33
+ function computeFScore(precision, recall, beta, zeroDivision) {
34
+ if (precision === 0 && recall === 0) {
35
+ return 0;
36
+ }
37
+ const betaSquared = beta * beta;
38
+ const numerator = (1 + betaSquared) * precision * recall;
39
+ const denominator = betaSquared * precision + recall;
40
+ return safeDivide(numerator, denominator, zeroDivision);
41
+ }
42
+ /** Collects all unique labels, ordered by first appearance in `expected` then `output`. */
43
+ function collectLabels(expected, output) {
44
+ const seen = new Set();
45
+ const ordered = [];
46
+ for (const label of [...expected, ...output]) {
47
+ if (!seen.has(label)) {
48
+ seen.add(label);
49
+ ordered.push(label);
50
+ }
51
+ }
52
+ return ordered;
53
+ }
54
+ /**
55
+ * Compares two labels the same way `Map`/`Set` key lookups do (SameValueZero),
56
+ * so a `NaN` label matches another `NaN` label instead of always mismatching
57
+ * under strict `===`.
58
+ */
59
+ function labelsAreEqual(a, b) {
60
+ return (a === b ||
61
+ (typeof a === "number" &&
62
+ typeof b === "number" &&
63
+ Number.isNaN(a) &&
64
+ Number.isNaN(b)));
65
+ }
66
+ function computeClassCounts(expected, output, labels) {
67
+ const countsByLabel = new Map();
68
+ for (const label of labels) {
69
+ countsByLabel.set(label, {
70
+ truePositive: 0,
71
+ falsePositive: 0,
72
+ falseNegative: 0,
73
+ });
74
+ }
75
+ for (const [index, expectedLabel] of expected.entries()) {
76
+ // `output` is validated to have the same length as `expected`.
77
+ const outputLabel = output[index];
78
+ // `labels` is derived from these same `expected`/`output` arrays, so both
79
+ // labels are always pre-populated keys in `countsByLabel`.
80
+ if (labelsAreEqual(expectedLabel, outputLabel)) {
81
+ const counts = countsByLabel.get(expectedLabel);
82
+ counts.truePositive += 1;
83
+ }
84
+ else {
85
+ const predictedCounts = countsByLabel.get(outputLabel);
86
+ predictedCounts.falsePositive += 1;
87
+ const expectedCounts = countsByLabel.get(expectedLabel);
88
+ expectedCounts.falseNegative += 1;
89
+ }
90
+ }
91
+ return countsByLabel;
92
+ }
93
+ /**
94
+ * Resolves the one-vs-rest positive label, if any. Auto-detection of a
95
+ * numeric `{0, 1}` label set only applies under the default `"macro"`
96
+ * average, so an explicitly configured `"micro"`/`"weighted"` average is
97
+ * never silently replaced by binary one-vs-rest scoring.
98
+ */
99
+ function resolvePositiveLabel(configuredPositiveLabel, labels, average) {
100
+ if (configuredPositiveLabel !== undefined) {
101
+ return configuredPositiveLabel;
102
+ }
103
+ if (average !== "macro") {
104
+ return null;
105
+ }
106
+ const uniqueLabels = new Set(labels);
107
+ if (uniqueLabels.size === 2 && uniqueLabels.has(0) && uniqueLabels.has(1)) {
108
+ return 1;
109
+ }
110
+ return null;
111
+ }
112
+ /**
113
+ * Aggregates per-class precision/recall/F-score across all classes.
114
+ *
115
+ * Mirrors scikit-learn's `average` semantics: for `"macro"`/`"weighted"` the
116
+ * F-score is computed per class and then averaged (not derived from the
117
+ * aggregated precision/recall). For `"micro"` the per-class confusion counts
118
+ * are pooled first, so the F-score is computed from the pooled
119
+ * precision/recall.
120
+ */
121
+ function aggregatePrecisionRecall(countsByLabel, average, beta, zeroDivision) {
122
+ if (average === "micro") {
123
+ let truePositive = 0;
124
+ let falsePositive = 0;
125
+ let falseNegative = 0;
126
+ for (const counts of countsByLabel.values()) {
127
+ truePositive += counts.truePositive;
128
+ falsePositive += counts.falsePositive;
129
+ falseNegative += counts.falseNegative;
130
+ }
131
+ const precision = safeDivide(truePositive, truePositive + falsePositive, zeroDivision);
132
+ const recall = safeDivide(truePositive, truePositive + falseNegative, zeroDivision);
133
+ return {
134
+ precision,
135
+ recall,
136
+ fScore: computeFScore(precision, recall, beta, zeroDivision),
137
+ };
138
+ }
139
+ const perClassMetrics = Array.from(countsByLabel.values()).map((counts) => {
140
+ const precision = safeDivide(counts.truePositive, counts.truePositive + counts.falsePositive, zeroDivision);
141
+ const recall = safeDivide(counts.truePositive, counts.truePositive + counts.falseNegative, zeroDivision);
142
+ return {
143
+ precision,
144
+ recall,
145
+ fScore: computeFScore(precision, recall, beta, zeroDivision),
146
+ support: counts.truePositive + counts.falseNegative,
147
+ };
148
+ });
149
+ if (average === "macro") {
150
+ return {
151
+ precision: mean(perClassMetrics.map((m) => m.precision)),
152
+ recall: mean(perClassMetrics.map((m) => m.recall)),
153
+ fScore: mean(perClassMetrics.map((m) => m.fScore)),
154
+ };
155
+ }
156
+ // weighted
157
+ const totalSupport = perClassMetrics.reduce((sum, m) => sum + m.support, 0);
158
+ if (totalSupport === 0) {
159
+ return {
160
+ precision: zeroDivision,
161
+ recall: zeroDivision,
162
+ fScore: zeroDivision,
163
+ };
164
+ }
165
+ return {
166
+ precision: weightedMean(perClassMetrics, "precision", totalSupport),
167
+ recall: weightedMean(perClassMetrics, "recall", totalSupport),
168
+ fScore: weightedMean(perClassMetrics, "fScore", totalSupport),
169
+ };
170
+ }
171
+ function mean(values) {
172
+ return values.reduce((sum, value) => sum + value, 0) / values.length;
173
+ }
174
+ function weightedMean(perClassMetrics, field, totalSupport) {
175
+ const weightedSum = perClassMetrics.reduce((sum, metrics) => sum + metrics[field] * metrics.support, 0);
176
+ return weightedSum / totalSupport;
177
+ }
178
+ /**
179
+ * Computes precision, recall, and F-beta score for a batch of expected vs.
180
+ * predicted labels.
181
+ *
182
+ * `expected`/`output` are the full sequence of labels across an entire
183
+ * dataset, not a single row — this and the evaluators built on it are
184
+ * dataset-level, unlike the package's per-row LLM evaluators. Call it once
185
+ * over every row's collected labels rather than wiring it into a per-row
186
+ * pipeline (e.g. `runExperiment`'s per-row evaluators).
187
+ *
188
+ * Supports both binary classification (via `positiveLabel`, or
189
+ * auto-detected when `average` is at its default `"macro"` and the labels
190
+ * are the numeric set `{0, 1}`) and multi-class classification (via the
191
+ * `average` strategy).
192
+ *
193
+ * @example Multi-class (macro average)
194
+ * ```typescript
195
+ * computePrecisionRecallFScore({
196
+ * expected: ["cat", "dog", "cat", "bird"],
197
+ * output: ["cat", "cat", "cat", "bird"],
198
+ * });
199
+ * // { precision: 5/9, recall: 2/3, fScore: 0.6, beta: 1, average: "macro", ... }
200
+ * ```
201
+ *
202
+ * @example Binary with an explicit positive label
203
+ * ```typescript
204
+ * computePrecisionRecallFScore(
205
+ * { expected: ["spam", "ham", "spam"], output: ["spam", "spam", "ham"] },
206
+ * { beta: 0.5, positiveLabel: "spam" }
207
+ * );
208
+ * ```
209
+ */
210
+ function computePrecisionRecallFScore({ expected, output }, options = {}) {
211
+ const { beta = 1, average = "macro", zeroDivision = 0, positiveLabel, } = options;
212
+ validateBeta(beta);
213
+ validateAverage(average);
214
+ validateLabelSequences(expected, output);
215
+ const labels = collectLabels(expected, output);
216
+ const countsByLabel = computeClassCounts(expected, output, labels);
217
+ const resolvedPositiveLabel = resolvePositiveLabel(positiveLabel, labels, average);
218
+ if (resolvedPositiveLabel !== null) {
219
+ const classCounts = countsByLabel.get(resolvedPositiveLabel);
220
+ if (!classCounts) {
221
+ throw new Error(`positiveLabel ${JSON.stringify(resolvedPositiveLabel)} not present in labels ${JSON.stringify(labels)}`);
222
+ }
223
+ const precision = safeDivide(classCounts.truePositive, classCounts.truePositive + classCounts.falsePositive, zeroDivision);
224
+ const recall = safeDivide(classCounts.truePositive, classCounts.truePositive + classCounts.falseNegative, zeroDivision);
225
+ return {
226
+ precision,
227
+ recall,
228
+ fScore: computeFScore(precision, recall, beta, zeroDivision),
229
+ beta,
230
+ average,
231
+ labels,
232
+ positiveLabel: resolvedPositiveLabel,
233
+ };
234
+ }
235
+ const { precision, recall, fScore } = aggregatePrecisionRecall(countsByLabel, average, beta, zeroDivision);
236
+ return {
237
+ precision,
238
+ recall,
239
+ fScore,
240
+ beta,
241
+ average,
242
+ labels,
243
+ positiveLabel: null,
244
+ };
245
+ }
246
+ /**
247
+ * Formats a beta value for use in metric names, e.g. `1` -> `"f1"`,
248
+ * `0.5` -> `"f0_5"`.
249
+ */
250
+ function formatBetaForMetricName(beta) {
251
+ if (beta <= 0) {
252
+ return "f";
253
+ }
254
+ if (Number.isInteger(beta)) {
255
+ return `f${beta}`;
256
+ }
257
+ return `f${beta.toString().replace(".", "_")}`;
258
+ }
259
+ /**
260
+ * The suffix appended to a metric name to reflect the aggregation strategy,
261
+ * e.g. `"precision"` vs. `"precision_micro"`. No suffix is used when
262
+ * `positiveLabel` is explicitly configured, since `average` is not
263
+ * applicable in that one-vs-rest binary mode. This mirrors
264
+ * `resolvePositiveLabel`'s auto-detection rule (only under the default
265
+ * `"macro"` average) so a constructed evaluator's static name always
266
+ * matches what `computePrecisionRecallFScore` actually computes.
267
+ */
268
+ function getAverageMetricNameSuffix({ average = "macro", positiveLabel, }) {
269
+ if (positiveLabel !== undefined) {
270
+ return "";
271
+ }
272
+ return average === "macro" ? "" : `_${average}`;
273
+ }
274
+ //# sourceMappingURL=classificationMetrics.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classificationMetrics.js","sourceRoot":"","sources":["../../../src/code/classificationMetrics.ts"],"names":[],"mappings":";;AA4WA,oEAkEC;AAMD,0DAQC;AAWD,gEAQC;AA5XD,SAAS,YAAY,CAAC,IAAY;IAChC,IAAI,CAAC,CAAC,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACtC,CAAC;AACH,CAAC;AAED,SAAS,eAAe,CAAC,OAAoB;IAC3C,IAAI,OAAO,KAAK,OAAO,IAAI,OAAO,KAAK,OAAO,IAAI,OAAO,KAAK,UAAU,EAAE,CAAC;QACzE,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;IAC5E,CAAC;AACH,CAAC;AAED,SAAS,sBAAsB,CAC7B,QAA+B,EAC/B,MAA6B;IAE7B,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,CAAC;QACvD,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,CAAC;QACtC,MAAM,IAAI,KAAK,CACb,sDAAsD,QAAQ,CAAC,MAAM,QAAQ,MAAM,CAAC,MAAM,EAAE,CAC7F,CAAC;IACJ,CAAC;IACD,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC;AAED,SAAS,UAAU,CACjB,SAAiB,EACjB,WAAmB,EACnB,YAAoB;IAEpB,IAAI,WAAW,KAAK,CAAC,EAAE,CAAC;QACtB,OAAO,YAAY,CAAC;IACtB,CAAC;IACD,OAAO,SA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;GAQG;AACH,SAAgB,0BAA0B,CAAC,EACzC,OAAO,GAAG,OAAO,EACjB,aAAa,GACgB;IAC7B,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;QAChC,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,OAAO,EAAE,CAAC;AAClD,CAAC"}
@@ -0,0 +1,16 @@
1
+ import type { EvaluatorBase } from "../core/EvaluatorBase";
2
+ import { type ClassificationExample, type PrecisionRecallFScoreOptions, type PrecisionRecallFScoreResult } from "./classificationMetrics";
3
+ /**
4
+ * Computes a {@link PrecisionRecallFScoreResult} for a batch of labels.
5
+ * Injectable so callers (e.g. {@link createPrecisionRecallFScoreEvaluators})
6
+ * can share one computed result across multiple metric evaluators instead of
7
+ * recomputing it per evaluator.
8
+ */
9
+ export type ClassificationMetricComputer = (example: Pick<ClassificationExample, "expected" | "output">, options: PrecisionRecallFScoreOptions) => PrecisionRecallFScoreResult;
10
+ /**
11
+ * Internal factory shared by `createPrecisionEvaluator`, `createRecallEvaluator`,
12
+ * and `createFBetaEvaluator` — each is a thin wrapper that only differs in
13
+ * which result field it reads and how its metric name is built.
14
+ */
15
+ export declare function createClassificationMetricEvaluator<RecordType extends ClassificationExample = ClassificationExample>(name: string, field: "precision" | "recall" | "fScore", options: PrecisionRecallFScoreOptions, compute?: ClassificationMetricComputer): EvaluatorBase<RecordType>;
16
+ //# sourceMappingURL=createClassificationMetricEvaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createClassificationMetricEvaluator.d.ts","sourceRoot":"","sources":["../../../src/code/createClassificationMetricEvaluator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAEL,KAAK,qBAAqB,EAC1B,KAAK,4BAA4B,EACjC,KAAK,2BAA2B,EACjC,MAAM,yBAAyB,CAAC;AAEjC;;;;;GAKG;AACH,MAAM,MAAM,4BAA4B,GAAG,CACzC,OAAO,EAAE,IAAI,CAAC,qBAAqB,EAAE,UAAU,GAAG,QAAQ,CAAC,EAC3D,OAAO,EAAE,4BAA4B,KAClC,2BAA2B,CAAC;AAEjC;;;;GAIG;AACH,wBAAgB,mCAAmC,CACjD,UAAU,SAAS,qBAAqB,GAAG,qBAAqB,EAEhE,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,WAAW,GAAG,QAAQ,GAAG,QAAQ,EACxC,OAAO,EAAE,4BAA4B,EACrC,OAAO,GAAE,4BAA2D,GACnE,aAAa,CAAC,UAAU,CAAC,CAgB3B"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClassificationMetricEvaluator = createClassificationMetricEvaluator;
4
+ const createEvaluator_1 = require("../helpers/createEvaluator");
5
+ const classificationMetrics_1 = require("./classificationMetrics");
6
+ /**
7
+ * Internal factory shared by `createPrecisionEvaluator`, `createRecallEvaluator`,
8
+ * and `createFBetaEvaluator` — each is a thin wrapper that only differs in
9
+ * which result field it reads and how its metric name is built.
10
+ */
11
+ function createClassificationMetricEvaluator(name, field, options, compute = classificationMetrics_1.computePrecisionRecallFScore) {
12
+ return (0, createEvaluator_1.createEvaluator)(
13
+ // Pass `example` through by reference (rather than destructuring and
14
+ // rebuilding a new object) so callers that share one computed result by
15
+ // caching on object identity (e.g. `createPrecisionRecallFScoreEvaluators`)
16
+ // actually get a cache hit.
17
+ (example) => {
18
+ const result = compute(example, options);
19
+ return { score: result[field] };
20
+ }, {
21
+ name,
22
+ kind: "CODE",
23
+ optimizationDirection: "MAXIMIZE",
24
+ });
25
+ }
26
+ //# sourceMappingURL=createClassificationMetricEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createClassificationMetricEvaluator.js","sourceRoot":"","sources":["../../../src/code/createClassificationMetricEvaluator.ts"],"names":[],"mappings":";;AAyBA,kFAuBC;AA/CD,gEAA6D;AAC7D,mEAKiC;AAajC;;;;GAIG;AACH,SAAgB,mCAAmC,CAGjD,IAAY,EACZ,KAAwC,EACxC,OAAqC,EACrC,UAAwC,oDAA4B;IAEpE,OAAO,IAAA,iCAAe;IACpB,qEAAqE;IACrE,wEAAwE;IACxE,4EAA4E;IAC5E,4BAA4B;IAC5B,CAAC,OAAO,EAAE,EAAE;QACV,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACzC,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;IAClC,CAAC,EACD;QACE,IAAI;QACJ,IAAI,EAAE,MAAM;QACZ,qBAAqB,EAAE,UAAU;KAClC,CACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { EvaluatorBase } from "../core/EvaluatorBase";
2
+ import type { ClassificationExample, PrecisionRecallFScoreOptions } from "./classificationMetrics";
3
+ export type F1EvaluatorOptions = Omit<PrecisionRecallFScoreOptions, "beta">;
4
+ /**
5
+ * Creates a code evaluator that computes the F1 score: the harmonic mean of
6
+ * precision and recall.
7
+ *
8
+ * Supports binary classification (via `positiveLabel`, or auto-detected when
9
+ * labels are the numeric set `{0, 1}`) and multi-class classification (via
10
+ * the `average` strategy).
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const f1 = createF1Evaluator();
15
+ * const result = await f1.evaluate({
16
+ * expected: ["cat", "dog", "cat", "bird"],
17
+ * output: ["cat", "cat", "cat", "bird"],
18
+ * });
19
+ * // { score: 0.6 }
20
+ * ```
21
+ */
22
+ export declare function createF1Evaluator<RecordType extends ClassificationExample = ClassificationExample>(options?: F1EvaluatorOptions): EvaluatorBase<RecordType>;
23
+ //# sourceMappingURL=createF1Evaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createF1Evaluator.d.ts","sourceRoot":"","sources":["../../../src/code/createF1Evaluator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EACV,qBAAqB,EACrB,4BAA4B,EAC7B,MAAM,yBAAyB,CAAC;AAGjC,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC,4BAA4B,EAAE,MAAM,CAAC,CAAC;AAE5E;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,iBAAiB,CAC/B,UAAU,SAAS,qBAAqB,GAAG,qBAAqB,EAChE,OAAO,GAAE,kBAAuB,GAAG,aAAa,CAAC,UAAU,CAAC,CAE7D"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createF1Evaluator = createF1Evaluator;
4
+ const createFBetaEvaluator_1 = require("./createFBetaEvaluator");
5
+ /**
6
+ * Creates a code evaluator that computes the F1 score: the harmonic mean of
7
+ * precision and recall.
8
+ *
9
+ * Supports binary classification (via `positiveLabel`, or auto-detected when
10
+ * labels are the numeric set `{0, 1}`) and multi-class classification (via
11
+ * the `average` strategy).
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const f1 = createF1Evaluator();
16
+ * const result = await f1.evaluate({
17
+ * expected: ["cat", "dog", "cat", "bird"],
18
+ * output: ["cat", "cat", "cat", "bird"],
19
+ * });
20
+ * // { score: 0.6 }
21
+ * ```
22
+ */
23
+ function createF1Evaluator(options = {}) {
24
+ return (0, createFBetaEvaluator_1.createFBetaEvaluator)(Object.assign(Object.assign({}, options), { beta: 1 }));
25
+ }
26
+ //# sourceMappingURL=createF1Evaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createF1Evaluator.js","sourceRoot":"","sources":["../../../src/code/createF1Evaluator.ts"],"names":[],"mappings":";;AA2BA,8CAIC;AA1BD,iEAA8D;AAI9D;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,iBAAiB,CAE/B,UAA8B,EAAE;IAChC,OAAO,IAAA,2CAAoB,kCAAkB,OAAO,KAAE,IAAI,EAAE,CAAC,IAAG,CAAC;AACnE,CAAC"}
@@ -0,0 +1,23 @@
1
+ import type { EvaluatorBase } from "../core/EvaluatorBase";
2
+ import type { ClassificationExample, PrecisionRecallFScoreOptions } from "./classificationMetrics";
3
+ /**
4
+ * Creates a code evaluator that computes the F-beta score: the weighted
5
+ * harmonic mean of precision and recall, where `beta` controls how much more
6
+ * weight recall gets relative to precision (`beta = 1` is the standard F1
7
+ * score).
8
+ *
9
+ * Supports binary classification (via `positiveLabel`, or auto-detected when
10
+ * `average` is at its default `"macro"` and labels are the numeric set
11
+ * `{0, 1}`) and multi-class classification (via the `average` strategy).
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * const f2 = createFBetaEvaluator({ beta: 2 });
16
+ * const result = await f2.evaluate({
17
+ * expected: ["cat", "dog", "cat", "bird"],
18
+ * output: ["cat", "cat", "cat", "bird"],
19
+ * });
20
+ * ```
21
+ */
22
+ export declare function createFBetaEvaluator<RecordType extends ClassificationExample = ClassificationExample>(options?: PrecisionRecallFScoreOptions): EvaluatorBase<RecordType>;
23
+ //# sourceMappingURL=createFBetaEvaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createFBetaEvaluator.d.ts","sourceRoot":"","sources":["../../../src/code/createFBetaEvaluator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAK3D,OAAO,KAAK,EACV,qBAAqB,EACrB,4BAA4B,EAC7B,MAAM,yBAAyB,CAAC;AAGjC;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,oBAAoB,CAClC,UAAU,SAAS,qBAAqB,GAAG,qBAAqB,EAChE,OAAO,GAAE,4BAAiC,GAAG,aAAa,CAAC,UAAU,CAAC,CAQvE"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createFBetaEvaluator = createFBetaEvaluator;
4
+ const classificationMetrics_1 = require("./classificationMetrics");
5
+ const createClassificationMetricEvaluator_1 = require("./createClassificationMetricEvaluator");
6
+ /**
7
+ * Creates a code evaluator that computes the F-beta score: the weighted
8
+ * harmonic mean of precision and recall, where `beta` controls how much more
9
+ * weight recall gets relative to precision (`beta = 1` is the standard F1
10
+ * score).
11
+ *
12
+ * Supports binary classification (via `positiveLabel`, or auto-detected when
13
+ * `average` is at its default `"macro"` and labels are the numeric set
14
+ * `{0, 1}`) and multi-class classification (via the `average` strategy).
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * const f2 = createFBetaEvaluator({ beta: 2 });
19
+ * const result = await f2.evaluate({
20
+ * expected: ["cat", "dog", "cat", "bird"],
21
+ * output: ["cat", "cat", "cat", "bird"],
22
+ * });
23
+ * ```
24
+ */
25
+ function createFBetaEvaluator(options = {}) {
26
+ const { beta = 1 } = options;
27
+ const suffix = (0, classificationMetrics_1.getAverageMetricNameSuffix)(options);
28
+ return (0, createClassificationMetricEvaluator_1.createClassificationMetricEvaluator)(`${(0, classificationMetrics_1.formatBetaForMetricName)(beta)}${suffix}`, "fScore", options);
29
+ }
30
+ //# sourceMappingURL=createFBetaEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createFBetaEvaluator.js","sourceRoot":"","sources":["../../../src/code/createFBetaEvaluator.ts"],"names":[],"mappings":";;AA8BA,oDAUC;AAvCD,mEAGiC;AAKjC,+FAA4F;AAE5F;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAgB,oBAAoB,CAElC,UAAwC,EAAE;IAC1C,MAAM,EAAE,IAAI,GAAG,CAAC,EAAE,GAAG,OAAO,CAAC;IAC7B,MAAM,MAAM,GAAG,IAAA,kDAA0B,EAAC,OAAO,CAAC,CAAC;IACnD,OAAO,IAAA,yEAAmC,EACxC,GAAG,IAAA,+CAAuB,EAAC,IAAI,CAAC,GAAG,MAAM,EAAE,EAC3C,QAAQ,EACR,OAAO,CACR,CAAC;AACJ,CAAC"}
@@ -0,0 +1,22 @@
1
+ import type { EvaluatorBase } from "../core/EvaluatorBase";
2
+ import type { ClassificationExample, PrecisionRecallFScoreOptions } from "./classificationMetrics";
3
+ /**
4
+ * Creates a code evaluator that computes precision: of the labels the model
5
+ * predicted as a given class, the fraction that were actually that class.
6
+ *
7
+ * Supports binary classification (via `positiveLabel`, or auto-detected when
8
+ * `average` is at its default `"macro"` and labels are the numeric set
9
+ * `{0, 1}`) and multi-class classification (via the `average` strategy).
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const precision = createPrecisionEvaluator();
14
+ * const result = await precision.evaluate({
15
+ * expected: ["cat", "dog", "cat", "bird"],
16
+ * output: ["cat", "cat", "cat", "bird"],
17
+ * });
18
+ * // { score: 5/9 }
19
+ * ```
20
+ */
21
+ export declare function createPrecisionEvaluator<RecordType extends ClassificationExample = ClassificationExample>(options?: PrecisionRecallFScoreOptions): EvaluatorBase<RecordType>;
22
+ //# sourceMappingURL=createPrecisionEvaluator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createPrecisionEvaluator.d.ts","sourceRoot":"","sources":["../../../src/code/createPrecisionEvaluator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,KAAK,EACV,qBAAqB,EACrB,4BAA4B,EAC7B,MAAM,yBAAyB,CAAC;AAGjC;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,wBAAwB,CACtC,UAAU,SAAS,qBAAqB,GAAG,qBAAqB,EAChE,OAAO,GAAE,4BAAiC,GAAG,aAAa,CAAC,UAAU,CAAC,CAOvE"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createPrecisionEvaluator = createPrecisionEvaluator;
4
+ const classificationMetrics_1 = require("./classificationMetrics");
5
+ const createClassificationMetricEvaluator_1 = require("./createClassificationMetricEvaluator");
6
+ /**
7
+ * Creates a code evaluator that computes precision: of the labels the model
8
+ * predicted as a given class, the fraction that were actually that class.
9
+ *
10
+ * Supports binary classification (via `positiveLabel`, or auto-detected when
11
+ * `average` is at its default `"macro"` and labels are the numeric set
12
+ * `{0, 1}`) and multi-class classification (via the `average` strategy).
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const precision = createPrecisionEvaluator();
17
+ * const result = await precision.evaluate({
18
+ * expected: ["cat", "dog", "cat", "bird"],
19
+ * output: ["cat", "cat", "cat", "bird"],
20
+ * });
21
+ * // { score: 5/9 }
22
+ * ```
23
+ */
24
+ function createPrecisionEvaluator(options = {}) {
25
+ const suffix = (0, classificationMetrics_1.getAverageMetricNameSuffix)(options);
26
+ return (0, createClassificationMetricEvaluator_1.createClassificationMetricEvaluator)(`precision${suffix}`, "precision", options);
27
+ }
28
+ //# sourceMappingURL=createPrecisionEvaluator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createPrecisionEvaluator.js","sourceRoot":"","sources":["../../../src/code/createPrecisionEvaluator.ts"],"names":[],"mappings":";;AA0BA,4DASC;AAlCD,mEAAqE;AAKrE,+FAA4F;AAE5F;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,wBAAwB,CAEtC,UAAwC,EAAE;IAC1C,MAAM,MAAM,GAAG,IAAA,kDAA0B,EAAC,OAAO,CAAC,CAAC;IACnD,OAAO,IAAA,yEAAmC,EACxC,YAAY,MAAM,EAAE,EACpB,WAAW,EACX,OAAO,CACR,CAAC;AACJ,CAAC"}