Skip to content

Commit

Permalink
type improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress committed Jun 10, 2024
1 parent d854670 commit 54ee4f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import type {Aggregator, AggregationProps} from '../aggregator';
import type {Aggregator, AggregationProps, AggregatedBin} from '../aggregator';
import {_deepEqual as deepEqual, BinaryAttribute} from '@deck.gl/core';
import {sortBins, packBinIds} from './sort';
import {aggregateChannel} from './aggregate';
import {VertexAccessor, evaluateVertexAccessor} from './vertex-accessor';

/** Settings used to construct a new CPUAggregator */
export type CPUAggregatorSettings = {
/** Options used to construct a new CPUAggregator */
export type CPUAggregatorOptions = {
/** Size of bin IDs */
dimensions: number;
/** Accessor to map each data point to a bin ID.
* If dimensions=1, bin ID should be a number;
* If dimensions>1, bin ID should be an array with [dimensions] elements;
* The data point will be skipped if bin ID is null.
*/
getBin: VertexAccessor<number | number[] | null, any>;
getBin: VertexAccessor<number[] | null, any>;
/** Accessor to map each data point to a weight value, defined per channel */
getValue: VertexAccessor<number>[];
};

/** Options used to run CPU aggregation, can be changed at any time */
/** Props used to run CPU aggregation, can be changed at any time */
export type CPUAggregationProps = AggregationProps & {};

export type Bin = {
id: number | number[];
id: number[];
index: number;
/** list of data point indices */
points: number[];
Expand All @@ -40,8 +40,8 @@ export class CPUAggregator implements Aggregator {
attributes: {}
};

protected getBinId: CPUAggregatorSettings['getBin'];
protected getValue: CPUAggregatorSettings['getValue'];
protected getBinId: CPUAggregatorOptions['getBin'];
protected getValue: CPUAggregatorOptions['getValue'];
/** Dirty flag
* If true, redo sorting
* If array, redo aggregation on the specified channel
Expand All @@ -52,7 +52,7 @@ export class CPUAggregator implements Aggregator {
protected binIds: Float32Array | null = null;
protected results: {value: Float32Array; domain: [min: number, max: number]}[] = [];

constructor({dimensions, getBin, getValue}: CPUAggregatorSettings) {
constructor({dimensions, getBin, getValue}: CPUAggregatorOptions) {
this.dimensions = dimensions;
this.numChannels = getValue.length;
this.getBinId = getBin;
Expand Down Expand Up @@ -164,16 +164,12 @@ export class CPUAggregator implements Aggregator {
}

/** Returns the information for a given bin. */
getBin(index: number): {
/** The original id */
id: number | number[];
/** Aggregated values by channel */
value: number[];
/** Count of data points in this bin */
count: number;
/** List of data point indices that fall into this bin. */
points?: number[];
} | null {
getBin(index: number):
| (AggregatedBin & {
/** List of data point indices that fall into this bin. */
points?: number[];
})
| null {
const bin = this.bins[index];
if (!bin) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function sortBins({
getBinId
}: {
pointCount: number;
getBinId: (index: number) => number | number[] | null;
getBinId: (index: number) => number[] | null;
}): Bin[] {
const binsById: Map<string, Bin> = new Map();

Expand Down
2 changes: 1 addition & 1 deletion modules/aggregation-layers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ export type {
} from './aggregation-layer-v9/gpu-aggregator/gpu-aggregator';
export type {
CPUAggregationProps,
CPUAggregatorSettings
CPUAggregatorOptions
} from './aggregation-layer-v9/cpu-aggregator/cpu-aggregator';
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('CPUAggregator#1D', t => {
dimensions: 1,
getBin: {
sources: ['age'],
getValue: ({age}, index, {ageGroupSize}) => Math.floor(age / ageGroupSize)
getValue: ({age}, index, {ageGroupSize}) => [Math.floor(age / ageGroupSize)]
},
getValue: [
{getValue: () => 1},
Expand Down Expand Up @@ -83,7 +83,7 @@ test('CPUAggregator#1D', t => {
// {age: 44, household: 4, income: 500, education: 4},
t.deepEqual(
aggregator.getBin(5),
{id: 8, count: 3, value: [3, 250, 5], points: [16, 17, 18]},
{id: [8], count: 3, value: [3, 250, 5], points: [16, 17, 18]},
'getBin()'
);

Expand All @@ -94,7 +94,7 @@ test('CPUAggregator#1D', t => {
t.end();
});

test.only('CPUAggregator#2D', t => {
test('CPUAggregator#2D', t => {
// An aggregator that calculates:
// [0] total count [1] average income, grouped by [age, education]
const aggregator = new CPUAggregator({
Expand Down

0 comments on commit 54ee4f1

Please sign in to comment.