From 31a5cc7162446d37ea96a517baa3a5a38e7ff618 Mon Sep 17 00:00:00 2001 From: xnought Date: Fri, 27 Sep 2024 16:10:22 -0400 Subject: [PATCH] v0 --- src/App.svelte | 52 ++++++++++++++++++++----------------- src/app.css | 7 +++++ src/color.js | 4 +-- src/lib/Codebook.svelte | 2 +- src/lib/Features.svelte | 19 +++++++++----- src/lib/Prism.svelte | 2 +- src/lib/Quantized.svelte | 55 ++++++++++++++++++++++++++++++++++++++++ src/stores.js | 8 +++++- 8 files changed, 115 insertions(+), 34 deletions(-) create mode 100644 src/lib/Quantized.svelte diff --git a/src/App.svelte b/src/App.svelte index e724a9d..579882e 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -11,6 +11,8 @@ import { VQVAE } from "./vectorQuantizer"; import Features from "./lib/Features.svelte"; import Matrix from "./lib/Matrix.svelte"; + import Quantized from "./lib/Quantized.svelte"; + import { hovering } from "./stores"; const inputOutputCanvasSize = 300; const images = [1, 2, 3, 4, 5, 7].map((d) => `images/${d}.png`); @@ -21,7 +23,6 @@ let inputDigit = Array(784).fill(0); let outputDigit = Array(784).fill(0); let embeddings; - let hovering; let idxs; let features; @@ -82,32 +83,37 @@ >
- (hovering = [i, j])} - mouseleave={() => (hovering = undefined)} - /> + +
+ {#if features && $hovering} + + {/if} +
-
- {#if features && hovering} - +
+ - {/if} +
+
+ {#if idxs} + + {/if} +
+
- +
+ import { hovering } from "../stores"; import Prism from "./Prism.svelte"; export let width; @@ -14,9 +15,6 @@ export let W = 7; export let C = 16; - export let mouseenter = (i, j) => {}; - export let mouseleave = () => {}; - const miniSquare = square / H; const squareFront = { @@ -33,7 +31,14 @@ }; - + ($hovering = undefined)} +> mouseenter(i, j)} - {mouseleave} + mouseenter={() => ($hovering = [i, j])} + hovering={$hovering + ? $hovering[0] === i && $hovering[1] === j + : false} /> {#if j === 0} diff --git a/src/lib/Prism.svelte b/src/lib/Prism.svelte index fb4c0c6..8ab8ced 100644 --- a/src/lib/Prism.svelte +++ b/src/lib/Prism.svelte @@ -28,7 +28,7 @@ width: square, height: square, }; - let hovering = false; + export let hovering = false; diff --git a/src/lib/Quantized.svelte b/src/lib/Quantized.svelte new file mode 100644 index 0000000..776e738 --- /dev/null +++ b/src/lib/Quantized.svelte @@ -0,0 +1,55 @@ + + + + ($hovering = undefined)} +> + + {#each { length: rows } as _, i} + {#each { length: columns } as _, j} + {@const idx = idxs[i][j]} + {@const c = codeColors[idx]} + {@const x = i * w} + {@const y = j * h} + {@const isHovering = + $hovering && $hovering[0] === i && $hovering[1] === j} + ($hovering = [i, j])} + > + G + {#if isHovering} + {codeNames[idx]} + {/if} + + {/each} + {/each} + + + diff --git a/src/stores.js b/src/stores.js index 4fa2813..fa7f525 100644 --- a/src/stores.js +++ b/src/stores.js @@ -2,5 +2,11 @@ import { writable } from "svelte/store"; import { generateDiscreteColors } from "./color"; import * as d3 from "d3"; +export const hovering = writable(); export const codeNames = "0123456789ABCDEF"; -export const codeColors = generateDiscreteColors(16, d3.interpolatePlasma); +export const codeColors = generateDiscreteColors( + 16, + d3.interpolateCool, + 0, + 0.2 +);