Spaces:
Running
Running
File size: 5,659 Bytes
c6afb12 614eca7 c6afb12 236e9a2 c6afb12 | 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 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 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 | import type { ToolsDataset, ToolsMetricMap, ToolsScatterRowId } from '$lib/types/tools-data';
import { adaptBucketRanking } from './adapt-ranking';
import type {
BucketLatencyMs,
BucketMetricMap,
BucketScatterRowId,
BucketVisualizationSnapshot
} from './types';
function modelKeyById(dataset: ReturnType<typeof adaptBucketRanking>) {
return new Map(
dataset.models.map((model) => [model.modelId ?? model.name, model.modelId ?? model.name])
);
}
function groupKeyById(snapshot: BucketVisualizationSnapshot) {
return new Map(snapshot.catalog.groups.map((group) => [group.group_id, group.label.en]));
}
function modelKey(id: string, keys: ReadonlyMap<string, string>) {
return keys.get(id) ?? id;
}
function groupKey(id: string, keys: ReadonlyMap<string, string>) {
return keys.get(id) ?? id;
}
function latencyMs(latency: BucketLatencyMs) {
return {
p50: latency.p50,
p95: latency.p95,
p99: latency.p99
};
}
function remapMetricMap(
map: BucketMetricMap,
rowKey: (id: string) => string,
modelKeyForId: (id: string) => string
): ToolsMetricMap {
return Object.fromEntries(
Object.entries(map).map(([rowId, row]) => [
rowKey(rowId),
Object.fromEntries(
Object.entries(row).map(([modelId, value]) => [modelKeyForId(modelId), value])
)
])
);
}
function remapScatterValues(
values: Record<BucketScatterRowId, Record<string, number | null>>,
modelKeyForId: (id: string) => string
) {
return Object.fromEntries(
Object.entries(values).map(([rowId, row]) => [
rowId,
Object.fromEntries(
Object.entries(row).map(([modelId, value]) => [modelKeyForId(modelId), value])
)
])
) as Record<ToolsScatterRowId, Record<string, number | null>>;
}
export function adaptBucketTools(snapshot: BucketVisualizationSnapshot): ToolsDataset {
const ranking = adaptBucketRanking(snapshot);
const modelKeys = modelKeyById(ranking);
const groupKeys = groupKeyById(snapshot);
const modelKeyForId = (id: string) => modelKey(id, modelKeys);
const groupKeyForId = (id: string) => groupKey(id, groupKeys);
const datasetRefs = snapshot.catalog.datasets.map((dataset) => ({
id: dataset.dataset_id,
groupId: dataset.group_id,
groupKey: groupKeyForId(dataset.group_id),
label: dataset.label,
language: dataset.language
}));
return {
...ranking,
datasets: datasetRefs,
visualizations: {
radar: {
title: snapshot.radar.title,
subtitle: snapshot.radar.subtitle,
modelKeys: snapshot.radar.model_ids.map(modelKeyForId),
groupKeys: snapshot.radar.group_ids.map(groupKeyForId),
defaultModelKeys: snapshot.radar.default_model_ids.map(modelKeyForId),
values: remapMetricMap(snapshot.radar.values, groupKeyForId, modelKeyForId),
f1Values: remapMetricMap(snapshot.radar.f1_values, groupKeyForId, modelKeyForId)
},
scatter: {
title: snapshot.scatter.title,
xAxis: snapshot.scatter.x_axis,
yAxis: snapshot.scatter.y_axis,
points: snapshot.scatter.points.map((point) => ({
modelKey: modelKeyForId(point.model_id),
fpr: point.fpr,
fnr: point.fnr,
tooltip: point.tooltip
})),
rowIds: snapshot.scatter.row_ids,
values: remapScatterValues(snapshot.scatter.values, modelKeyForId)
},
heatmap: {
title: snapshot.heatmap.title,
subtitle: snapshot.heatmap.subtitle,
metric: snapshot.heatmap.metric,
rows: datasetRefs.filter((dataset) => snapshot.heatmap.row_ids.includes(dataset.id)),
columnModelKeys: snapshot.heatmap.column_ids.map(modelKeyForId),
values: remapMetricMap(snapshot.heatmap.values, (id) => id, modelKeyForId)
},
groupedBars: {
title: snapshot.grouped_bars.title,
subtitle: snapshot.grouped_bars.subtitle,
groupKeys: snapshot.grouped_bars.group_ids.map(groupKeyForId),
models: snapshot.grouped_bars.models.map((model) => ({
modelKey: modelKeyForId(model.model_id),
groups: Object.fromEntries(
Object.entries(model.groups).map(([groupId, metrics]) => [
groupKeyForId(groupId),
metrics
])
)
}))
},
pareto: {
title: snapshot.pareto.title,
subtitle: snapshot.pareto.subtitle,
xAxis: snapshot.pareto.x_axis,
yAxis: snapshot.pareto.y_axis,
byModelKey: Object.fromEntries(
Object.entries(snapshot.pareto.by_model_id).map(([modelId, item]) => [
modelKeyForId(modelId),
{
latencyMs: latencyMs(item.latency_ms),
integral: item.integral,
fpr: item.fpr
}
])
)
},
performance: {
title: snapshot.performance.title,
rows: snapshot.performance.rows.map((row) => ({
modelKey: modelKeyForId(row.model_id),
rank: row.rank,
latencyMs: latencyMs(row.latency_ms),
errorRate: row.error_rate,
integral: row.integral
}))
},
robustness: {
title: snapshot.robustness.title,
rows: snapshot.robustness.rows.map((row) => ({
modelKey: modelKeyForId(row.model_id),
scoreReal: row.score_real,
scoreRobust: row.score_robust,
deltaScore: row.delta_score,
fnrReal: row.fnr_real,
fnrRobust: row.fnr_robust,
deltaFnr: row.delta_fnr,
fprReal: row.fpr_real,
fprRobust: row.fpr_robust,
deltaFpr: row.delta_fpr,
categoryLabel: row.category_label
}))
}
},
drilldownIndex: {
heatmap: snapshot.drilldownIndex.heatmap.map((row) => ({
modelKey: modelKeyForId(row.model_id),
groupKey: groupKeyForId(row.group_id),
datasetIds: row.dataset_ids,
selectedMetric: row.selected_metric,
metrics: row.metrics,
description: row.description,
promptViewerAvailable: row.prompt_viewer_available,
promptViewerPath: row.prompt_viewer_path
}))
}
};
}
|