File size: 5,422 Bytes
0c651e4 7ee4f6a 0c651e4 7ee4f6a 0c651e4 7ee4f6a | 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 {
buildRankingRows,
compareRankingRows,
rankingRowsToCsv,
rankingScopeGroups
} from '$lib/data/ranking';
import { toolsDataGroups, toolsRowsToCsv } from '$lib/data/tools';
import {
buildMatrixGroups,
buildMatrixRows,
detailMatrixRowsToCsv,
detailMetricValue,
visibleDetailMatrixDatasets
} from '$lib/data/details';
import type { DetailsDataset, DetailsDatasetMatrixCell } from '$lib/types/details-data';
import type { DetailMetric, MatrixDataset } from '$lib/types/details';
import type { LocalizedString } from '$lib/types/i18n';
import type { GuardModel } from '$lib/types/leaderboard';
import type { RankingDataset } from '$lib/types/ranking-data';
import type { ToolsDataset } from '$lib/types/tools-data';
const rankingScope = 'all';
const rankingSortKey = 'integral';
const rankingSortDirection = 'desc';
const detailsMatrixMetric: DetailMetric = 'score';
const detailsMatrixSortDataset = null;
const detailsMatrixSortDirection = -1;
function localizedEn(value: LocalizedString | null | undefined, fallback: string) {
return value?.en ?? fallback;
}
function modelKey(model: GuardModel) {
return model.modelId ?? model.name;
}
function csvNumberValue(value: number | null | undefined, digits = 4) {
return typeof value === 'number' && Number.isFinite(value) ? Number(value.toFixed(digits)) : null;
}
function exportMatrixDatasets(dataset: DetailsDataset): MatrixDataset[] {
return dataset.datasetMatrix.datasets.map((item) => ({
id: item.id,
name: item.id,
group: localizedEn(item.groupLabel, item.groupId),
sourceGroup: localizedEn(item.groupLabel, item.groupId),
label: localizedEn(item.label, item.id),
language: item.language
}));
}
function exportMatrixGroups(dataset: DetailsDataset) {
const matrixDatasets = exportMatrixDatasets(dataset);
const visibleDatasets = visibleDetailMatrixDatasets(matrixDatasets, dataset.allGroups, 'any');
return buildMatrixGroups(visibleDatasets);
}
function exportMatrixRows(dataset: DetailsDataset) {
return buildMatrixRows(
'',
detailsMatrixMetric,
detailsMatrixSortDataset,
detailsMatrixSortDirection,
dataset.models,
exportMatrixDatasets(dataset),
dataset.datasetMatrix.cellsByModelId
);
}
export function rankingExportRows(dataset: RankingDataset) {
const groups = rankingScopeGroups(rankingScope, dataset);
const rows = buildRankingRows(groups, true, dataset.models, dataset.allGroups.length);
return [...rows].sort((a, b) => compareRankingRows(a, b, rankingSortKey, rankingSortDirection));
}
export function rankingExportCsv(dataset: RankingDataset) {
return rankingRowsToCsv(rankingExportRows(dataset));
}
export function rankingExportJson(dataset: RankingDataset) {
return {
snapshotId: dataset.snapshotId,
snapshotDate: dataset.snapshotDate,
generatedAt: dataset.generatedAt,
scope: rankingScope,
sort: {
key: rankingSortKey,
direction: rankingSortDirection
},
rows: rankingExportRows(dataset).map((row, index) => ({
rank: index + 1,
model: row.model.name,
size: row.size,
integral: csvNumberValue(row.integral),
min_group: csvNumberValue(row.minGroup),
overall_fpr: csvNumberValue(row.fpr),
overall_fnr: csvNumberValue(row.fnr),
overall_f1: csvNumberValue(row.f1),
p95_ms: csvNumberValue(row.p95, 1),
run_date: row.model.runDate
}))
};
}
export function detailsMatrixExportCsv(dataset: DetailsDataset) {
return detailMatrixRowsToCsv(
exportMatrixRows(dataset),
exportMatrixGroups(dataset),
detailsMatrixMetric,
dataset.datasetMatrix.cellsByModelId
);
}
export function detailsMatrixExportJson(dataset: DetailsDataset) {
const groups = exportMatrixGroups(dataset);
const matrixRows = exportMatrixRows(dataset);
const matrixDatasets = groups.flatMap((group) => group.datasets);
const cellsByModelId: Record<string, Record<string, DetailsDatasetMatrixCell>> = dataset
.datasetMatrix.cellsByModelId;
return {
snapshotId: dataset.snapshotId,
snapshotDate: dataset.snapshotDate,
generatedAt: dataset.generatedAt,
metric: detailsMatrixMetric,
datasets: matrixDatasets.map((item) => ({
id: item.id,
group: item.group,
label: item.label,
language: item.language
})),
rows: matrixRows.map((model, index) => ({
rank: index + 1,
model: model.name,
organization: model.org,
size: model.params,
integral: csvNumberValue(model.integral),
metric: detailsMatrixMetric,
values: Object.fromEntries(
matrixDatasets.map((item) => [
item.id,
csvNumberValue(
detailMetricValue(cellsByModelId[modelKey(model)]?.[item.id], detailsMatrixMetric)
)
])
)
}))
};
}
function toolsExportGroups(dataset: ToolsDataset) {
return toolsDataGroups(dataset.models, dataset.allGroups);
}
export function toolsExportCsv(dataset: ToolsDataset) {
return toolsRowsToCsv(dataset.models, toolsExportGroups(dataset));
}
export function toolsExportJson(dataset: ToolsDataset) {
const groups = toolsExportGroups(dataset);
return {
snapshotId: dataset.snapshotId,
snapshotDate: dataset.snapshotDate,
generatedAt: dataset.generatedAt,
groups,
rows: dataset.models.flatMap((model) =>
groups.map((group) => {
const cell = model.groups[group] ?? null;
return {
model: model.name,
group,
score: csvNumberValue(cell?.score),
fnr: csvNumberValue(cell?.fnr),
fpr: csvNumberValue(cell?.fpr),
f1: csvNumberValue(cell?.f1)
};
})
)
};
}
|