File size: 4,427 Bytes
ffdff5d |
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 |
// Data transformation utilities for ChartRenderer
/**
* Chart data transformations and calculations
*/
export class ChartTransforms {
/**
* Process metric data and calculate domains
*/
static processMetricData(metricData, metricKey, normalizeLoss) {
const runs = Object.keys(metricData || {});
const hasAny = runs.some(r => (metricData[r] || []).length > 0);
if (!hasAny) {
return {
runs: [],
hasData: false,
minStep: 0,
maxStep: 0,
minVal: 0,
maxVal: 1,
yDomain: [0, 1],
stepSet: new Set(),
hoverSteps: []
};
}
// Calculate data bounds
let minStep = Infinity, maxStep = -Infinity, minVal = Infinity, maxVal = -Infinity;
runs.forEach(r => {
(metricData[r] || []).forEach(pt => {
minStep = Math.min(minStep, pt.step);
maxStep = Math.max(maxStep, pt.step);
minVal = Math.min(minVal, pt.value);
maxVal = Math.max(maxVal, pt.value);
});
});
// Determine Y domain based on metric type
const isAccuracy = /accuracy/i.test(metricKey);
const isLoss = /loss/i.test(metricKey);
let yDomain;
if (isAccuracy) {
yDomain = [0, 1];
} else if (isLoss && normalizeLoss) {
yDomain = [0, 1];
} else {
yDomain = [minVal, maxVal];
}
// Collect all steps for hover interactions
const stepSet = new Set();
runs.forEach(r => (metricData[r] || []).forEach(v => stepSet.add(v.step)));
const hoverSteps = Array.from(stepSet).sort((a, b) => a - b);
return {
runs,
hasData: true,
minStep,
maxStep,
minVal,
maxVal,
yDomain,
stepSet,
hoverSteps,
isAccuracy,
isLoss
};
}
/**
* Setup scales based on data and scale type
*/
static setupScales(svgManager, processedData, logScaleX) {
const { hoverSteps, yDomain } = processedData;
const { x: xScale, y: yScale, line: lineGen } = svgManager.getScales();
// Update scales
yScale.domain(yDomain).nice();
let stepIndex = null;
if (logScaleX) {
const minStep = Math.max(1, Math.min(...hoverSteps));
const maxStep = Math.max(...hoverSteps);
xScale.domain([minStep, maxStep]);
lineGen.x(d => xScale(d.step));
} else {
stepIndex = new Map(hoverSteps.map((s, i) => [s, i]));
xScale.domain([0, Math.max(0, hoverSteps.length - 1)]);
lineGen.x(d => xScale(stepIndex.get(d.step)));
}
return { stepIndex };
}
/**
* Create normalization function for Y values
*/
static createNormalizeFunction(processedData, normalizeLoss) {
const { isLoss, minVal, maxVal } = processedData;
return (v) => {
if (isLoss && normalizeLoss) {
return ((maxVal > minVal) ? (v - minVal) / (maxVal - minVal) : 0);
}
return v;
};
}
/**
* Validate and clean data values
*/
static validateData(metricData) {
const cleanedData = {};
Object.keys(metricData || {}).forEach(run => {
const values = metricData[run] || [];
cleanedData[run] = values.filter(pt =>
pt &&
typeof pt.step === 'number' &&
typeof pt.value === 'number' &&
Number.isFinite(pt.step) &&
Number.isFinite(pt.value)
);
});
return cleanedData;
}
/**
* Calculate chart dimensions based on content
*/
static calculateOptimalDimensions(dataCount, containerWidth) {
// Suggest optimal dimensions based on data density
const minHeight = 120;
const maxHeight = 300;
const baseHeight = 150;
// More data points = slightly taller chart for better readability
const heightMultiplier = Math.min(1.5, 1 + (dataCount / 1000) * 0.5);
const suggestedHeight = Math.min(maxHeight, Math.max(minHeight, baseHeight * heightMultiplier));
return {
width: containerWidth || 800,
height: suggestedHeight
};
}
/**
* Prepare hover step data for interactions
*/
static prepareHoverSteps(processedData, logScaleX) {
const { hoverSteps } = processedData;
if (!hoverSteps.length) return { hoverSteps: [], stepIndex: null };
let stepIndex = null;
if (!logScaleX) {
stepIndex = new Map(hoverSteps.map((s, i) => [s, i]));
}
return { hoverSteps, stepIndex };
}
}
|