Skip to content

Commit

Permalink
Merge pull request #111 from nomic-ai/imagelabels
Browse files Browse the repository at this point in the history
improve labels
  • Loading branch information
bmschmidt authored May 23, 2024
2 parents 95fd2d3 + b921fbe commit 7c36cf3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 31 deletions.
20 changes: 13 additions & 7 deletions dev/Main.svelte
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
<script>
import { onMount } from 'svelte';
// Each test file is a svelte component -- routing
// here is just grabbing the URL and instantiating that
// component.
import FourClasses from './FourClasses.svelte';
import SinglePoint from './SinglePoint.svelte';
import LabelMaker from './submodules/LabelMaker.svelte';
const modes = {
FourClasses: FourClasses,
SinglePoint: SinglePoint,
FourClasses,
SinglePoint,
LabelMaker,
};
$: mode = '';
onMount(() => {
mode = window.location.hash.slice(1);
// window.onhashchange(() => {
// mode = window.location.hash.slice(1);
// });
mode = window.location.pathname.slice(1);
});
</script>

{#if mode in modes}
<svelte:component this={modes[mode]} />
{:else}
Current mode, {mode} is not in the list of modes.
<h1>Put a load mode from the list in the hash.</h1>
<div>
{#each Object.keys(modes) as modename}
<a href="#{modename}">{modename}</a><br />
<a href="/{modename}">{modename}</a><br />
{/each}
</div>
{/if}
31 changes: 31 additions & 0 deletions dev/submodules/LabelMaker.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<script>
import { onMount } from 'svelte';
import { LabelMaker, Scatterplot } from '../../src/deepscatter';
onMount(() => {
const scatterplot = new Scatterplot('#labels');
const d = document.getElementById('labels');
d.ATTRIBUTE_NODE;
if (d === null) {
throw new Error('ComponentDidNotMount');
}
const maker = new LabelMaker(scatterplot, 'maker');
});
</script>

<div class="full" id="labels">
<canvas class="full fixed"> </canvas>
<svg class="full fixed"> </svg>
</div>

<style>
.full {
width: 100vw;
height: 100vh;
}
.fixed {
position: fixed;
top: 0;
left: 0;
}
</style>
20 changes: 9 additions & 11 deletions src/label_rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class LabelMaker extends Renderer {
public tree: DepthTree;
public timer?: Timer;
public label_key?: string;
// public svg: SVGElement;
public labelgroup: SVGGElement;
private hovered: undefined | string;
public options: DS.LabelOptions = {};
Expand All @@ -45,11 +44,7 @@ export class LabelMaker extends Renderer {
id_raw: string,
options: DS.LabelOptions = {},
) {
super(
scatterplot.div!.node() as HTMLDivElement,
scatterplot.deeptable,
scatterplot,
);
super(scatterplot.div!.node() as HTMLDivElement, scatterplot);
this.options = options;
this.canvas = scatterplot
.elements![2].selectAll('canvas')
Expand Down Expand Up @@ -99,8 +94,6 @@ export class LabelMaker extends Renderer {
this.timer.stop();
}

select(this.labelgroup).attr('display', 'inline');

this.timer = timer(() => {
this.render();
ticks -= 1;
Expand All @@ -120,7 +113,7 @@ export class LabelMaker extends Renderer {
stop() {
if (this.timer) {
this.timer.stop();
select(this.labelgroup).attr('display', 'none');
select(this.labelgroup).selectAll('rect.labelbbox').remove();
this.ctx.clearRect(0, 0, 4096, 4096);
this.timer = undefined;
}
Expand Down Expand Up @@ -198,11 +191,16 @@ export class LabelMaker extends Renderer {
const bboxes = select(this.labelgroup)
.selectAll('rect.labelbbox')
// Keyed by the coordinates.
.data(overlaps, (d: BBox) => String(d.minZ) + String(d.minX))
.data(
overlaps,
(d: BBox) =>
String(d.minZ) + String(d.minX) + (d.data as RawPoint).text,
)
.join((enter) =>
enter
.append('rect')
.attr('class', 'labellbox')
.attr('data-labelmaker-label', (d) => `${(d.data as RawPoint).text}`)
.style('opacity', RECT_DEFAULT_OPACITY),
);

Expand Down Expand Up @@ -308,7 +306,7 @@ export class LabelMaker extends Renderer {
.attr('display', (d: P3d) => {
return (d.data.properties.__display as string) || 'inline';
})
.on('mouseover', (event, d) => {
.on('mouseover', (event, d: P3d) => {
select(event.target).style('opacity', RECT_DEFAULT_OPACITY);
this.hovered = '' + d.minZ + d.minX;
event.stopPropagation();
Expand Down
2 changes: 1 addition & 1 deletion src/regl_rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class ReglRenderer extends Renderer {
tileSet: Deeptable,
scatterplot: Scatterplot,
) {
super(selector, tileSet, scatterplot);
super(selector, scatterplot);
const c = this.canvas;
if (this.canvas === undefined) {
throw new Error('No canvas found');
Expand Down
17 changes: 7 additions & 10 deletions src/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,12 @@ export class Renderer {
public aes?: AestheticSet;
public _zoom?: Zoom;
public render_props: RenderProps = new RenderProps();
constructor(
selector: string | Node,
tileSet: Deeptable,
scatterplot: Scatterplot,
) {
constructor(selector: string | Node, scatterplot: Scatterplot) {
this.scatterplot = scatterplot;
this.holder = select(selector as string);
this.canvas = select(
this.holder!.node()!.firstElementChild,
).node() as HTMLCanvasElement;
this.deeptable = tileSet;
this.width = +select(this.canvas).attr('width');
this.height = +select(this.canvas).attr('height');
this.deferred_functions = [];
Expand Down Expand Up @@ -181,7 +176,7 @@ export class Renderer {
const { aes } = this;
const needed = new Set<string>();
if (aes) {
for (const [_k, v] of Object.entries(aes.store)) {
for (const v of Object.values(aes.store)) {
if (v instanceof StatefulAesthetic) {
for (const f of v.neededFields) {
needed.add(f);
Expand Down Expand Up @@ -242,7 +237,7 @@ export class Renderer {
// yield the currently visible tiles based on the zoom state
// and a maximum index passed manually.
const { max_ix } = this;
const { deeptable: tileSet } = this;

// Materialize using a tileset method.

if (!this.aes) throw new Error('Aesthetic missing');
Expand All @@ -255,7 +250,7 @@ export class Renderer {
y.last.field == 'y';

const all_tiles = natural_display
? tileSet
? this.scatterplot.deeptable
.map((d: Tile) => d)
.filter((tile) => {
const visible = tile.is_visible(
Expand All @@ -264,7 +259,9 @@ export class Renderer {
);
return visible;
})
: tileSet.map((d) => d).filter((tile) => tile.min_ix < this.max_ix);
: this.scatterplot.deeptable
.map((d) => d)
.filter((tile) => tile.min_ix < this.max_ix);
all_tiles.sort((a, b) => a.min_ix - b.min_ix);
return all_tiles;
}
Expand Down
2 changes: 0 additions & 2 deletions src/scatterplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,7 @@ export class Scatterplot {
const { prefs } = this;

await this.deeptable.ready;
console.log('HERE');
await this.deeptable.root_tile.get_column('x');
console.log('HERE 2');

this._renderer = new ReglRenderer(
'#container-for-webgl-canvas',
Expand Down

0 comments on commit 7c36cf3

Please sign in to comment.