Skip to content

Commit

Permalink
include return value
Browse files Browse the repository at this point in the history
  • Loading branch information
bmschmidt committed Oct 2, 2024
1 parent d025f62 commit 60d27f4
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 27 deletions.
87 changes: 76 additions & 11 deletions dev/svelte/SwitchPositions.svelte
Original file line number Diff line number Diff line change
@@ -1,23 +1,88 @@
<script>
import {
makeData,
Float32,
Vector,
vectorFromArray,
Struct,
makeVector,
Field,
} from 'apache-arrow';
export let scatterplot;
let positionNum = 0;
async function click() {
console.log(scatterplot.prefs.encoding.x)
for (let i = 0; i < 10; i++) {
if (scatterplot.deeptable.transformations['struct' + i]) {
continue;
}
scatterplot.deeptable.transformations['struct' + i] = async function (
tile,
) {
// Create a nested struct with a change.
const x = (await tile.get_column('x')).toArray();
const y = (await tile.get_column('y')).toArray();
const x_ = new Float32Array(x.length);
const y_ = new Float32Array(y.length);
for (let i = 0; i < x.length; i++) {
const r = (Math.random() + Math.random()) / 3;
const theta = Math.random() * Math.PI * 2;
x_[i] = x[i] + Math.cos(theta) * r;
y_[i] = y[i] + Math.sin(theta) * r;
}
const d = makeData({
type: new Struct([
new Field('x', new Float32()),
new Field('y', new Float32()),
]),
children: [vectorFromArray(x_).data[0], vectorFromArray(y_).data[0]],
});
const r = new Vector([d]);
return r;
};
scatterplot.deeptable.map((d) => d.get_column('struct' + i));
}
await new Promise((resolve) => {
setTimeout(() => resolve());
}, 100);
let r = 'struct' + (positionNum++ % 10);
await scatterplot.plotAPI({
duration: 1000,
encoding: {
x: {
field: scatterplot.prefs.encoding.x.field === 'x' ? 'y' : 'x',
transform: scatterplot.prefs.encoding.x.field === 'x' ? 'linear': 'literal'
field: r,
subfield: ['x'],
transform: 'literal',
domain: [-10, 10],
},
y: {
field: scatterplot.prefs.encoding.y.field === 'y' ? 'x' : 'y',
transform: scatterplot.prefs.encoding.y.field === 'y' ? 'linear': 'literal'
}
}
})
field: r,
subfield: ['y'],
transform: 'literal',
domain: [-10, 10],
},
},
});
// await scatterplot.plotAPI({
// encoding: {
// x: {
// field: scatterplot.prefs.encoding.x.field === 'x' ? 'y' : 'x',
// transform:
// scatterplot.prefs.encoding.x.field === 'x' ? 'linear' : 'literal',
// },
// y: {
// field: scatterplot.prefs.encoding.y.field === 'y' ? 'x' : 'y',
// transform:
// scatterplot.prefs.encoding.y.field === 'y' ? 'linear' : 'literal',
// },
// },
// });
}
</script>

<button on:click={click}>
Switch positions
</button>
<button on:click={click}> Switch positions </button>
41 changes: 31 additions & 10 deletions src/Deeptable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Int32,
Int8,
tableToIPC,
Struct,
} from 'apache-arrow';
import { Scatterplot } from './scatterplot';
import { wrapArrowTable } from './wrap_arrow';
Expand All @@ -34,6 +35,8 @@ import type {
IdSelectParams,
} from './selection';
import { DataSelection } from './selection';
import { Some, TupleMap } from './utilityFunctions';
import { getNestedVector } from './regl_rendering';

type TransformationStatus = 'queued' | 'in progress' | 'complete' | 'failed';

Expand Down Expand Up @@ -70,7 +73,8 @@ export class Deeptable {
...defaultTransformations,
};
public _plot: Scatterplot | null;
private extents: Record<string, [number, number] | [Date, Date]> = {};
private extents: TupleMap<string, [number, number] | [Date, Date]> =
new TupleMap();
// A 3d identifier for the tile. Usually [z, x, y]
private _extent?: Rectangle;
public _ix_seed = 0;
Expand Down Expand Up @@ -344,13 +348,24 @@ export class Deeptable {

domain<T extends [number, number] | [string, Date] = [number, number]>(
columnName: string,
subfield?: string[],
): [T[1], T[1]] {
if (this.extents[columnName]) {
return this.extents[columnName];
const key = [columnName, ...(subfield || [])] as Some<string>;
if (this.extents.get(key)) {
return this.extents.get(key);
}

// First -- look at the schema metadata.
let dim = this._schema?.fields.find((d) => d.name === columnName);
for (const sub in subfield) {
if (dim === undefined) {
continue;
}
console.log({ dim });
dim = (dim as Field<Struct<any>>).type.children.find(
(d) => d.name === sub,
);
}
const dim = this._schema?.fields.find(
(d) => d.name === columnName,
) as Field<DS.SupportedArrowTypes>;
if (dim !== undefined) {
let min: T[0] | undefined = undefined;
let max: T[0] | undefined = undefined;
Expand All @@ -373,24 +388,30 @@ export class Deeptable {
'Date field extents in metadata must be passed as strings',
);
}
return (this.extents[columnName] = [new Date(min), new Date(max)]);
this.extents.set(key, [new Date(min), new Date(max)]);
return this.extents.get(key);
}
if (typeof max === 'string') {
throw new Error('Failed to parse min-max as numbers');
}
if (min !== undefined) {
return (this.extents[columnName] = [min as T[1], max as T[1]] as
this.extents.set(key, [min as T[1], max as T[1]] as
| [number, number]
| [Date, Date]);
return this.extents.get(key);
}
}

const vectors: Vector[] = this.map((tile) => tile)
.filter((d) => d.hasLoadedColumn(columnName))
.map((d) => d.record_batch.getChild(columnName) as Vector<Float32>);
.map((d) => getNestedVector(d, [columnName, ...(subfield || [])]));

const extented = extent([...new Vector(vectors)]) as [T[1], T[1]] as
| [number, number]
| [Date, Date];
return (this.extents[columnName] = extented);

this.extents.set(key, extented);
return this.extents.get(key);
}

*points(bbox: Rectangle | undefined, max_ix = 1e99) {
Expand Down
5 changes: 3 additions & 2 deletions src/aesthetics/Aesthetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ export abstract class Aesthetic<
for (let i = 0; i < this.subfield.length; i++) {
v = v[this.subfield[i]] as Input['domainType'];
}
return v;
// Needs a default perhaps?
return null;
}
// Needs a default perhaps?
return null;
}

get map_position() {
Expand Down
2 changes: 1 addition & 1 deletion src/aesthetics/ScaledAesthetic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export abstract class ScaledAesthetic<
Input['domainType'],
];
} else {
return this.scatterplot.deeptable.domain(this.field);
return this.scatterplot.deeptable.domain(this.field, this.subfield);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/regl_rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ export class BufferManager {
}

// TODO: Build this out in next PR.
function getNestedVector(
export function getNestedVector(
tile: Tile,
key: string[],
): Vector<DS.SupportedArrowTypes> {
Expand Down Expand Up @@ -1336,7 +1336,6 @@ class MultipurposeBufferSet {
byte_size: items * bytes_per_item,
};

// add a listener for GC on the value.
this.pointer += items * bytes_per_item;
return value;
}
Expand Down
2 changes: 1 addition & 1 deletion src/scatterplot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ export class Scatterplot {
}

this.plot_queue = this.unsafe_plotAPI(prefs);
console.log('C');

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const [_, hook] of Object.entries(this.hooks)) {
hook();
Expand Down

0 comments on commit 60d27f4

Please sign in to comment.