Spaces:
Sleeping
Sleeping
File size: 5,949 Bytes
af73265 | 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 185 186 | import { describe, it, expect } from 'vitest';
import {
signedUnit,
sharedNoiseSimilarity,
relativeDifference,
oppositeConflictBalance,
cancelAmountFromMetric,
hasGroupsForDimContrast,
computeDimRelationMetrics,
paintWeightsForDim,
applyGroupDimPaint,
buildPointGroupPaintAttributes,
} from '../src/visualizer/groupDimContrast.js';
describe('groupDimContrast math', () => {
it('signedUnit treats 0 as positive', () => {
expect(signedUnit(0)).toBe(1);
expect(signedUnit(0.002)).toBe(1);
expect(signedUnit(-0.018)).toBe(-1);
});
it('sharedNoiseSimilarity is formula (b)', () => {
// a=0.002, b=0.018 → 1 - 0.016/0.020 = 0.2
expect(sharedNoiseSimilarity(0.002, 0.018)).toBeCloseTo(0.2, 5);
expect(sharedNoiseSimilarity(0.1, 0.1)).toBeCloseTo(1, 5);
expect(sharedNoiseSimilarity(0, 0)).toBe(1);
});
it('relativeDifference complements similarity on same magnitudes', () => {
expect(relativeDifference(0.002, 0.018)).toBeCloseTo(0.8, 5);
expect(relativeDifference(0.1, -0.1)).toBeCloseTo(1, 5);
});
it('cancelAmountFromMetric mirrors zero-coverage on high metric', () => {
expect(cancelAmountFromMetric(0.4, 0)).toBe(0);
expect(cancelAmountFromMetric(0.4, 0.5)).toBe(0); // floor=0.5
expect(cancelAmountFromMetric(0.75, 0.5)).toBeCloseTo(0.5, 5);
expect(cancelAmountFromMetric(1, 0.5)).toBeCloseTo(1, 5);
});
});
describe('computeDimRelationMetrics', () => {
it('requires ≥2 groups', () => {
expect(hasGroupsForDimContrast([{ groupId: 'G1', embedding: [1] }])).toBe(false);
expect(computeDimRelationMetrics([{ groupId: 'G1', embedding: [1] }])).toEqual([]);
});
it('uses group means and classifies same vs opposite sign', () => {
const items = [
{ groupId: 'G1', embedding: [0.002, 0.1, -0.05] },
{ groupId: 'G1', embedding: [0.002, 0.1, -0.05] },
{ groupId: 'G2', embedding: [0.018, -0.1, -0.05] },
{ groupId: 'G2', embedding: [0.018, -0.1, -0.05] },
];
const m = computeDimRelationMetrics(items);
expect(m).toHaveLength(3);
expect(m[0].sameSign).toBe(true);
expect(m[0].similarity).toBeCloseTo(0.2, 5);
expect(m[1].sameSign).toBe(false);
expect(m[1].difference).toBeCloseTo(1, 5);
expect(m[2].sameSign).toBe(true);
expect(m[2].similarity).toBeCloseTo(1, 5);
});
});
describe('paintWeightsForDim / applyGroupDimPaint', () => {
it('same-sign cancel only when enabled', () => {
const metric = {
meanA: 0.1,
meanB: 0.1,
sameSign: true,
similarity: 1,
difference: 0,
};
expect(paintWeightsForDim(metric, { sameSignCancelEnabled: false, sameSignCancelCoverage: 90 })).toEqual({
cancel: 0,
highlight: 0,
});
const on = paintWeightsForDim(metric, { sameSignCancelEnabled: true, sameSignCancelCoverage: 90 });
expect(on.cancel).toBeGreaterThan(0.9);
expect(on.highlight).toBe(0);
});
it('opposite highlight scales by strength × conflict balance', () => {
const metric = {
meanA: 0.1,
meanB: -0.1,
sameSign: false,
similarity: 0,
difference: 1,
conflictBalance: 1,
};
const w = paintWeightsForDim(metric, {
oppositeHighlightEnabled: true,
oppositeHighlightStrength: 50,
oppositeCancelCoverage: 0,
});
expect(w.highlight).toBeCloseTo(0.5, 5);
expect(w.cancel).toBe(0);
});
it('conflict cover fades opposite dims linearly (not all-or-nothing)', () => {
const metric = {
meanA: 0.1,
meanB: -0.1,
sameSign: false,
similarity: 0,
difference: 1,
conflictBalance: 1,
};
const at0 = paintWeightsForDim(metric, {
oppositeHighlightEnabled: true,
oppositeHighlightStrength: 100,
oppositeCancelCoverage: 0,
});
expect(at0.cancel).toBe(0);
expect(at0.highlight).toBeCloseTo(1, 5);
const at45 = paintWeightsForDim(metric, {
oppositeHighlightEnabled: true,
oppositeHighlightStrength: 100,
oppositeCancelCoverage: 45,
});
expect(at45.cancel).toBeCloseTo(0.5, 5);
expect(at45.highlight).toBeCloseTo(1, 5);
const at90 = paintWeightsForDim(metric, {
oppositeHighlightEnabled: true,
oppositeHighlightStrength: 100,
oppositeCancelCoverage: 90,
});
expect(at90.cancel).toBeCloseTo(1, 5);
});
it('oppositeConflictBalance is 1 for equal mags, lower when unbalanced', () => {
expect(oppositeConflictBalance(0.1, -0.1)).toBeCloseTo(1, 5);
expect(oppositeConflictBalance(0.1, -0.01)).toBeCloseTo(2 * 0.01 / 0.11, 5);
});
it('replaces toward highlight then blackens', () => {
const base = { r: 1, g: 1, b: 0, alpha: 1 };
const metric = {
meanA: 1,
meanB: -1,
sameSign: false,
similarity: 0,
difference: 1,
};
const painted = applyGroupDimPaint(
base,
metric,
{
oppositeHighlightEnabled: true,
oppositeHighlightStrength: 100,
oppositeCancelCoverage: 0,
oppositeHighlightColor: '#00E5FF',
},
{ r: 0, g: 0, b: 0 },
{ r: 0, g: 229 / 255, b: 1 }
);
expect(painted.r).toBeCloseTo(0, 5);
expect(painted.b).toBeCloseTo(1, 5);
});
it('buildPointGroupPaintAttributes uses meta.dim', () => {
const metrics = computeDimRelationMetrics([
{ groupId: 'G1', embedding: [0.1, -0.1] },
{ groupId: 'G2', embedding: [0.1, 0.1] },
]);
const points = [
{ meta: { dim: 0 } },
{ meta: { dim: 1 } },
];
const { cancel, highlight } = buildPointGroupPaintAttributes(points, metrics, {
sameSignCancelEnabled: true,
sameSignCancelCoverage: 90,
oppositeHighlightEnabled: true,
oppositeHighlightStrength: 100,
oppositeCancelCoverage: 0,
});
expect(cancel[0]).toBeGreaterThan(0.5); // same-sign dim0
expect(highlight[0]).toBe(0);
expect(highlight[1]).toBeGreaterThan(0.5); // opposite dim1
});
});
|