Skip to content

Commit

Permalink
refactor: docs and make dataselections rely on dataset, not scatterplot
Browse files Browse the repository at this point in the history
  • Loading branch information
bmschmidt committed Apr 1, 2024
1 parent 4b43c7a commit 58c5595
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 68 deletions.
68 changes: 34 additions & 34 deletions dev/Main.svelte
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
<script>
import { Scatterplot } from '../src/deepscatter';
import { onMount } from 'svelte'
import SwitchPositions from './svelte/SwitchPositions.svelte';
import { Scatterplot } from '../src/deepscatter';
import { onMount } from 'svelte';
import SwitchPositions from './svelte/SwitchPositions.svelte';
import ColorChange from './svelte/ColorChange.svelte';
const prefs = {
source_url: '/tiles',
max_points: 1000000,
alpha: 35, // Target saturation for the full page.
zoom_balance: 0.22, // Rate at which points increase size. https://observablehq.com/@bmschmidt/zoom-strategies-for-huge-scatterplots-with-three-js
point_size: 2, // Default point size before application of size scaling
background_color: '#EEEDDE',
encoding: {
color: {
field: 'class',
range: 'category10',
},
x: {
field: 'x',
transform: 'literal',
},
y: {
field: 'y',
transform: 'literal',
},
import SizeSlider from './svelte/SizeSlider.svelte';
const startSize = 2;
const prefs = {
source_url: '/tiles',
max_points: 1000000,
alpha: 35, // Target saturation for the full page.
zoom_balance: 0.22, // Rate at which points increase size. https://observablehq.com/@bmschmidt/zoom-strategies-for-huge-scatterplots-with-three-js
point_size: startSize, // Default point size before application of size scaling
background_color: '#EEEDDE',
encoding: {
color: {
field: 'class',
range: 'category10',
},
x: {
field: 'x',
transform: 'literal',
},
};
y: {
field: 'y',
transform: 'literal',
},
},
};
let scatterplot = null;
onMount( () => {
onMount(() => {
scatterplot = new Scatterplot('#deepscatter');
window.scatterplot = scatterplot;
scatterplot.plotAPI(prefs)
})
scatterplot.plotAPI(prefs);
});
</script>

<div id="overlay">
<SwitchPositions {scatterplot}>
</SwitchPositions>
<SwitchPositions {scatterplot}></SwitchPositions>
<ColorChange {scatterplot}></ColorChange>
<SizeSlider size={startSize} {scatterplot}></SizeSlider>
</div>

<div id="deepscatter">

</div>
<div id="deepscatter"></div>

<style>
#overlay {
Expand All @@ -57,4 +57,4 @@
width: 100vw;
height: 100vh;
}
</style>
</style>
21 changes: 21 additions & 0 deletions dev/svelte/SizeSlider.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
export let scatterplot;
export let value;
let min = Math.sqrt(0.5);
let max = Math.sqrt(20);
function changeSize() {
scatterplot
.plotAPI({
duration: 0,
point_size: value,
})
.then(() => {
scatterplot.plotAPI({
duration: 1000,
});
});
}
</script>

<input type="range" bind:value {min} {max} step="0.01" on:change={changeSize} />
82 changes: 82 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"terser": "^5.28.1",
"typedoc": "^0.25.8",
"typescript": "^5.3.3",
"uvu": "^0.5.6",
"vite": "^5.1.4"
}
}
1 change: 1 addition & 0 deletions src/Dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { wrapArrowTable } from './wrap_arrow';
type Key = string;

type ArrowBuildable = DS.ArrowBuildable;

type Transformation = DS.Transformation;

// Some variables are universally available.
Expand Down
2 changes: 1 addition & 1 deletion src/scatterplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ export class Scatterplot {
return old_version.selection;
}
}
const selection = new DataSelection(this, params);
const selection = new DataSelection(this.dataset, params);
this.selection_history.push({
selection,
name: selection.name,
Expand Down
44 changes: 14 additions & 30 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ type BinaryOperation = 'AND' | 'OR' | 'XOR';
type UnaryOperation = 'NOT';

type CompArgs = DataSelection | Composition;

/**
* A composition represents an operation on selections. The syntax is basically
* a lisp-like list of operations and selections. The first element is the operation,
* and the rest of the elements are the arguments.
*/
type Composition =
| [UnaryOperation, CompArgs]
| [BinaryOperation, CompArgs, CompArgs]
Expand Down Expand Up @@ -329,15 +335,14 @@ export class DataSelection {
private events: { [key: string]: Array<(args) => void> } = {};

constructor(
plot: Scatterplot,
dataset: Dataset,
params:
| IdSelectParams
| BooleanColumnParams
| FunctionSelectParams
| CompositeSelectParams,
) {
this.plot = plot;
this.dataset = plot.dataset;
this.dataset = dataset;
this.name = params.name;
let markReady = function () {};
this.ready = new Promise((resolve) => {
Expand Down Expand Up @@ -424,7 +429,7 @@ export class DataSelection {
* selection that is the union of the two.
*/
union(other: DataSelection, name: string | undefined): DataSelection {
return new DataSelection(this.plot, {
return new DataSelection(this.dataset, {
name: name || this.name + ' union ' + other.name,
composition: ['OR', this, other],
});
Expand All @@ -438,7 +443,7 @@ export class DataSelection {
* disjunctive normal form constructor.
*/
intersection(other: DataSelection, name: string | undefined): DataSelection {
return new DataSelection(this.plot, {
return new DataSelection(this.dataset, {
name: name || this.name + ' intersection ' + other.name,
composition: ['AND', this, other],
});
Expand Down Expand Up @@ -606,7 +611,7 @@ export class DataSelection {
}
return mask.to_arrow();
};
const selection = new DataSelection(this.plot, {
const selection = new DataSelection(this.dataset, {
name: newName,
tileFunction,
});
Expand Down Expand Up @@ -648,7 +653,7 @@ export class DataSelection {
if (this.dataset.has_column(name)) {
throw new Error(`Column ${name} already exists, can't create`);
}
this.plot.dataset.transformations[name] =
this.dataset.transformations[name] =
this.wrapWithSelectionMetadata(tileFunction);
// Await the application to the root tile, which may be necessary
await this.dataset.root_tile.apply_transformation(name);
Expand Down Expand Up @@ -766,43 +771,22 @@ export class DataSelection {
}
if (typeof codes[0] === 'string') {
const matcher = stringmatcher(key_field, codes as string[]);
this.plot.dataset.transformations[name] =
this.dataset.transformations[name] =
this.wrapWithSelectionMetadata(matcher);
await this.dataset.root_tile.apply_transformation(name);
} else if (typeof codes[0] === 'bigint') {
const matcher = bigintmatcher(key_field, codes as bigint[]);
this.plot.dataset.transformations[name] =
this.dataset.transformations[name] =
this.wrapWithSelectionMetadata(matcher);
await this.dataset.root_tile.apply_transformation(name);
} else {
console.error('Unable to match type', typeof codes[0]);
}
if (options.plot_after) {
return this.apply_to_foreground({});
}
}

async add_boolean_column(name: string, field: string): Promise<void> {
throw new Error('Method not implemented.');
}

apply_to_foreground(params: DS.BackgroundOptions): Promise<void> {
const field = this.name;
const background_options: DS.BackgroundOptions = {
size: [0.5, 10],
...params,
};
return this.plot.plotAPI({
background_options,
encoding: {
foreground: {
field,
op: 'gt',
a: 0,
},
},
});
}
}

function bigintmatcher(field: string, matches: bigint[]) {
Expand Down
Loading

0 comments on commit 58c5595

Please sign in to comment.