|
| 1 | +import { evaluateChartEncodes } from '../helpers/evaluate-chart-encode.js'; |
| 2 | +import { readDataset, writeDataset } from '../helpers/read-dataset.js'; |
| 3 | +import _ from 'lodash' |
| 4 | + |
| 5 | +export const evaluateChartRecommend = async () => { |
| 6 | + const evalDatasetPath = `datastes/recommend/eval.json`; |
| 7 | + const testDataset = await readDataset(evalDatasetPath); |
| 8 | + console.log('datasets count: ', testDataset.length); |
| 9 | + console.log('Beginning eval datasets...'); |
| 10 | + const misMap = new Map(); |
| 11 | + const scoredData = testDataset.map(data => { |
| 12 | + const target = data.target?.[0] ?? data.target; |
| 13 | + const gen = data.generation?.[0] ?? data.generation; |
| 14 | + if(!gen || !target) { |
| 15 | + // 数据缺失 |
| 16 | + misMap.set('missing', (misMap.get('missing') ?? 0) + 1); |
| 17 | + return; |
| 18 | + } |
| 19 | + const chartTypeScore = gen.type === target.type ? 1 : 0; |
| 20 | + const encodeScore = evaluateChartEncodes(gen.encode, target.encode); |
| 21 | + if (!chartTypeScore) { |
| 22 | + const key = `${target.type}_to_${gen.type}`; |
| 23 | + if (misMap.has(key)) { |
| 24 | + misMap.set(key, misMap.get(key) + 1); |
| 25 | + } else { |
| 26 | + misMap.set(key, 1); |
| 27 | + } |
| 28 | + } |
| 29 | + return { |
| 30 | + ...data, |
| 31 | + correctness: chartTypeScore, |
| 32 | + encodeScore, |
| 33 | + } |
| 34 | + }).filter(data => data); |
| 35 | + // save evaluate result |
| 36 | + await writeDataset(`datastes/recommend/metrics.json`, scoredData); |
| 37 | + // output metrics |
| 38 | + let score = 0; |
| 39 | + let chartTypeScore = 0; |
| 40 | + let encodeScore = 0; |
| 41 | + let chartTypeScoreMap = {} |
| 42 | + scoredData.forEach(data => { |
| 43 | + chartTypeScore += data.correctness; |
| 44 | + encodeScore += data.encodeScore; |
| 45 | + const target = data.target?.[0] ?? data.target; |
| 46 | + const chartType = target?.type |
| 47 | + chartTypeScoreMap[chartType] = { |
| 48 | + chartTypeScore: (chartTypeScoreMap[chartType]?.chartTypeScore ?? 0) + data.correctness, |
| 49 | + encodeScore: (chartTypeScoreMap[chartType]?.encodeScore ?? 0) + data.encodeScore, |
| 50 | + count: (chartTypeScoreMap[chartType]?.count ?? 0) + 1, |
| 51 | + }; |
| 52 | + }); |
| 53 | + score /= testDataset.length; |
| 54 | + chartTypeScore /= testDataset.length; |
| 55 | + encodeScore /= testDataset.length; |
| 56 | + chartTypeScoreMap = _.mapValues(chartTypeScoreMap, ((score) => ({ |
| 57 | + chartTypeScore: score.chartTypeScore/score.count, |
| 58 | + encodeScore: score.encodeScore/score.count, |
| 59 | + count: score.count |
| 60 | + }))) |
| 61 | + console.log('scoredData.length', scoredData.length, 'datasets count: ', testDataset.length) |
| 62 | + console.log('chart type recommend accuracy:', chartTypeScore) |
| 63 | + console.log('chart encode score:', encodeScore) |
| 64 | + console.log('misclassified:', misMap); |
| 65 | + console.log('chartTypeScoreMap', chartTypeScoreMap) |
| 66 | +} |
| 67 | + |
| 68 | +evaluateChartRecommend().catch((error) => { |
| 69 | + console.error('Error evaluating chart recommendation:', error); |
| 70 | +}); |
0 commit comments