Spaces:
Sleeping
Sleeping
File size: 6,724 Bytes
8fa21e1 | 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 | import { useCallback, useMemo, useState } from 'react';
import { ScatterChart, Scatter, XAxis, YAxis, Tooltip, Cell, ResponsiveContainer } from 'recharts';
const CLUSTER_COLORS = [
'#f59e0b', '#3b82f6', '#22c55e', '#ef4444',
'#a855f7', '#06b6d4', '#f97316', '#ec4899',
'#84cc16', '#14b8a6',
];
const ANOMALY_COLOR = '#4b4b4b';
const MAX_POINTS = 500;
function stratifiedSample(clusterPoints, anomalyPoints, max) {
const total = clusterPoints.length + anomalyPoints.length;
if (total <= max) return { points: [...clusterPoints, ...anomalyPoints], sampled: false };
const budgetForClusters = max - anomalyPoints.length;
const ratio = budgetForClusters / clusterPoints.length;
const sampledClusters = ratio >= 1
? clusterPoints
: clusterPoints.filter(() => Math.random() < ratio);
return {
points: [...sampledClusters, ...anomalyPoints],
sampled: true,
};
}
export default function ScatterPlot({ clusters, anomalies }) {
const [hiddenClusters, setHiddenClusters] = useState(new Set());
const safeClusters = useMemo(() => Array.isArray(clusters) ? clusters : [], [clusters]);
const safeAnomalies = useMemo(() => Array.isArray(anomalies) ? anomalies : [], [anomalies]);
function toggleCluster(id) {
setHiddenClusters(prev => {
const next = new Set(prev);
next.has(id) ? next.delete(id) : next.add(id);
return next;
});
}
const clusterPoints = useMemo(() =>
safeClusters
.filter(c => !hiddenClusters.has(c.id))
.flatMap(c =>
(c.points ?? []).map(p => ({
...p,
clusterId: c.id,
clusterLabel: c.label,
color: CLUSTER_COLORS[c.id % CLUSTER_COLORS.length],
isAnomaly: false,
}))
),
[safeClusters, hiddenClusters]);
const anomalyPoints = useMemo(() =>
safeAnomalies.map(p => ({
...p,
clusterId: -1,
clusterLabel: 'Anomaly',
color: ANOMALY_COLOR,
isAnomaly: true,
})),
[safeAnomalies]);
const { points: allPoints, sampled } = useMemo(
() => stratifiedSample(clusterPoints, anomalyPoints, MAX_POINTS),
[clusterPoints, anomalyPoints]
);
const totalPoints = clusterPoints.length + anomalyPoints.length;
const CustomTooltip = useCallback(({ active, payload }) => {
if (!active || !payload?.length) return null;
const d = payload[0].payload;
return (
<div className="tooltip-box">
<div style={{ color: d.color, marginBottom: 4, letterSpacing: '0.1em' }}>
{d.isAnomaly ? '⚠ ANOMALY' : d.clusterLabel}
</div>
{d.raw
? <div style={{ color: 'var(--text-mid)' }}>{d.raw}</div>
: <div style={{ color: 'var(--text-dim)' }}>{d.error_type} · {d.module}</div>
}
</div>
);
}, []);
return (
<div className="scatter-wrapper fade-in">
<div className="scatter-header">
<span className="scatter-title">CLUSTER MAP — UMAP 2D PROJECTION</span>
{sampled && (
<span style={{
fontSize: 10,
fontFamily: 'var(--font-mono)',
color: 'var(--text-dim)',
marginLeft: 12,
}}>
showing {allPoints.length} of {totalPoints} points
</span>
)}
<div className="scatter-legend">
{safeClusters.map(c => (
<div
key={c.id}
className="legend-item"
onClick={() => toggleCluster(c.id)}
style={{
cursor: 'pointer',
opacity: hiddenClusters.has(c.id) ? 0.3 : 1,
transition: 'opacity 0.2s',
}}
>
<div
className="legend-dot"
style={{ background: CLUSTER_COLORS[c.id % CLUSTER_COLORS.length] }}
/>
{c.label}
</div>
))}
{safeAnomalies.length > 0 && (
<div className="legend-item">
<div className="legend-dot" style={{ background: ANOMALY_COLOR }} />
Anomalies
</div>
)}
</div>
</div>
<ResponsiveContainer width="100%" height={400}>
<ScatterChart margin={{ top: 20, right: 20, bottom: 20, left: 20 }}>
<XAxis
dataKey="x"
type="number"
domain={['auto', 'auto']}
tick={{ fill: 'var(--text-dim)', fontSize: 10 }}
axisLine={{ stroke: 'var(--border)' }}
tickLine={false}
label={{ value: 'UMAP-1', position: 'insideBottom', offset: -10, fill: 'var(--text-dim)', fontSize: 9 }}
/>
<YAxis
dataKey="y"
type="number"
domain={['auto', 'auto']}
tick={{ fill: 'var(--text-dim)', fontSize: 10 }}
axisLine={{ stroke: 'var(--border)' }}
tickLine={false}
label={{ value: 'UMAP-2', angle: -90, position: 'insideLeft', fill: 'var(--text-dim)', fontSize: 9 }}
/>
<Tooltip content={<CustomTooltip />} />
<Scatter
data={allPoints}
isAnimationActive={allPoints.length < 200}
>
{allPoints.map((point, i) => (
<Cell
key={i}
fill={point.color}
opacity={point.isAnomaly ? 0.5 : 0.85}
r={point.isAnomaly ? 4 : 6}
/>
))}
</Scatter>
</ScatterChart>
</ResponsiveContainer>
</div>
);
} |