Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.
// SPDX-License-Identifier: MIT
// Copyright contributors to the kepler.gl project
import test from 'tape';
import {
resetFilterGpuMode,
assignGpuChannel,
assignGpuChannels,
getDatasetFieldIndexForFilter,
getGpuFilterProps
} from '@kepler.gl/table';
import {FILTER_TYPES} from '@kepler.gl/constants';
test('gpuFilterUtils -> resetFilterGpuMode', t => {
const testFilters = [
{id: '1', dataId: ['smoothie'], gpu: true},
{id: '2', dataId: ['smoothie'], gpu: true},
{id: '3', dataId: ['smoothie'], gpu: false},
{id: '4', dataId: ['smoothie'], gpu: true},
{id: '5', dataId: ['smoothie'], gpu: true},
{id: '6', dataId: ['smoothie'], gpu: true},
{id: '7', dataId: ['milkshake'], gpu: true},
{id: '8', dataId: ['milkshake'], gpu: false}
];
const expectedFilters = [
{id: '1', dataId: ['smoothie'], gpu: true},
{id: '2', dataId: ['smoothie'], gpu: true},
{id: '3', dataId: ['smoothie'], gpu: false},
{id: '4', dataId: ['smoothie'], gpu: true},
{id: '5', dataId: ['smoothie'], gpu: true},
{id: '6', dataId: ['smoothie'], gpu: false},
{id: '7', dataId: ['milkshake'], gpu: true},
{id: '8', dataId: ['milkshake'], gpu: false}
];
const result = resetFilterGpuMode(testFilters);
t.deepEqual(result, expectedFilters, 'should reset gpu mode');
t.end();
});
test('gpuFilterUtils -> assignGpuChannel', t => {
const testCases = [
{
gpuFilter: {id: '3', dataId: ['a'], gpu: true},
filters: [
{id: '1', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [0]}
],
result: {
id: '3',
dataId: ['a'],
gpu: true,
gpuChannel: [1]
}
},
{
gpuFilter: {id: '3', dataId: ['a'], gpu: true, gpuChannel: [1]},
filters: [
{id: '3', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '1', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [0]}
],
result: {
id: '3',
dataId: ['a'],
gpu: true,
gpuChannel: [1]
}
},
{
gpuFilter: {id: '3', dataId: ['a'], gpu: true},
filters: [
{id: '1', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [1]}
],
result: {id: '3', dataId: ['a'], gpu: true, gpuChannel: [0]}
},
{
gpuFilter: {id: '5', dataId: ['a', 'b'], gpu: true},
filters: [
{id: '1', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [3]},
{id: '3', dataId: ['a'], gpu: true, gpuChannel: [0]},
{id: '4', dataId: ['a'], gpu: true, gpuChannel: [2]}
],
result: {id: '5', dataId: ['a', 'b'], gpu: true, gpuChannel: [1, 1]}
},
{
gpuFilter: {id: '5', dataId: ['a', 'b'], gpu: true, gpuChannel: [1]},
filters: [
{id: '1', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [3]},
{id: '3', dataId: ['a', 'b'], gpu: true, gpuChannel: [0, 2]},
{id: '4', dataId: ['a'], gpu: true, gpuChannel: [2]}
],
result: {id: '5', dataId: ['a', 'b'], gpu: true, gpuChannel: [1, 1]}
},
{
gpuFilter: {id: '6', dataId: ['a'], gpu: true},
filters: [
{id: '1', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [3]},
{id: '3', dataId: ['a'], gpu: true, gpuChannel: [2]},
{id: '4', dataId: ['a'], gpu: true, gpuChannel: [0]},
{id: '5', dataId: ['a'], gpu: true, gpuChannel: [1]}
],
result: {id: '6', dataId: ['a'], gpu: false}
}
];
testCases.forEach(tc => {
t.deepEqual(
assignGpuChannel(tc.gpuFilter, tc.filters),
tc.result,
'should assign correct channel'
);
});
t.end();
});
test('gpuFilterUtils -> assignGpuChannel requires a free channel for every shared dataset', t => {
const occupied = [0, 1, 2, 3].map(channel => ({
id: `occupied-${channel}`,
dataId: ['full'],
gpu: true,
gpuChannel: [channel]
}));
const cases = [
{dataId: ['available', 'full'], gpuChannel: [0]},
{dataId: ['full', 'available'], gpuChannel: []},
{dataId: ['available', 'full'], gpuChannel: [0, 0]}
];
cases.forEach(({dataId, gpuChannel}) => {
const filter = {id: 'shared', dataId, gpu: true, gpuChannel};
const originalChannels = gpuChannel.slice();
const result = assignGpuChannel(filter, occupied);
t.equal(result.gpu, false, 'falls back for a missing, sparse, or conflicting channel');
t.deepEqual(filter.gpuChannel, originalChannels, 'preserves the input filter channels');
});
const available = assignGpuChannel(
{id: 'shared', dataId: ['available', 'full'], gpu: true, gpuChannel: [0]},
occupied.slice(0, 3)
);
t.equal(available.gpu, true, 'keeps GPU filtering when every dataset has room');
t.deepEqual(available.gpuChannel, [0, 3], 'uses the remaining channel in the second dataset');
t.end();
});
test('gpuFilterUtils -> assignGpuChannels', t => {
const testCases = [
{
filters: [
{id: '1', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '2', dataId: ['a'], gpu: true},
{id: '3', dataId: ['b'], gpu: true},
{id: '4', dataId: ['b'], gpu: false}
],
result: [
{id: '1', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [0]},
{id: '3', dataId: ['b'], gpu: true, gpuChannel: [0]},
{id: '4', dataId: ['b'], gpu: false}
]
},
{
filters: [
{id: '1', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '2', dataId: ['b'], gpu: true, gpuChannel: [1]},
{id: '3', dataId: ['a'], gpu: true, gpuChannel: [2]},
{id: '4', dataId: ['b'], gpu: false},
{id: '5', dataId: ['a'], gpu: true},
{id: '6', dataId: ['b'], gpu: true},
{id: '7', dataId: ['b'], gpu: true, gpuChannel: [0]}
],
result: [
{id: '1', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '2', dataId: ['b'], gpu: true, gpuChannel: [1]},
{id: '3', dataId: ['a'], gpu: true, gpuChannel: [2]},
{id: '4', dataId: ['b'], gpu: false},
{id: '5', dataId: ['a'], gpu: true, gpuChannel: [0]},
{id: '6', dataId: ['b'], gpu: true, gpuChannel: [2]},
{id: '7', dataId: ['b'], gpu: true, gpuChannel: [0]}
]
},
{
filters: [
{id: '1', dataId: ['a', 'b'], gpu: true, gpuChannel: [1, 0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '3', dataId: ['b', 'a'], gpu: true},
{id: '4', dataId: ['b'], gpu: true}
],
result: [
{id: '1', dataId: ['a', 'b'], gpu: true, gpuChannel: [0, 0]},
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '3', dataId: ['b', 'a'], gpu: true, gpuChannel: [1, 2]},
{id: '4', dataId: ['b'], gpu: true, gpuChannel: [2]}
]
}
];
testCases.forEach(tc => {
t.deepEqual(assignGpuChannels(tc.filters), tc.result, 'should assign correct channel');
});
t.end();
});
test('gpuFilterUtils -> getDatasetFieldIndexForFilter', t => {
const dataId = 'test-this-id';
let fieldIndex = getDatasetFieldIndexForFilter(dataId, {
dataId: [dataId],
fieldIdx: [3]
});
t.equal(fieldIndex, 3, 'FieldIndex should be 3');
fieldIndex = getDatasetFieldIndexForFilter(dataId, {
dataId: ['different-id', dataId],
fieldIdx: [3, 5]
});
t.equal(fieldIndex, 5, 'FieldIndex should be 5');
fieldIndex = getDatasetFieldIndexForFilter(dataId, {dataId: ['different-id']});
t.equal(fieldIndex, -1, 'FieldIndex should be -1');
t.end();
});
test('gpuFilterUtils -> assignGpuChannel interval time filter', t => {
const assigned = assignGpuChannel(
{
id: '3',
dataId: ['a'],
gpu: true,
type: FILTER_TYPES.timeRange,
endName: ['end']
},
[{id: '2', dataId: ['a'], gpu: true, gpuChannel: [0]}]
);
t.deepEqual(
assigned,
{
id: '3',
dataId: ['a'],
gpu: true,
type: FILTER_TYPES.timeRange,
endName: ['end'],
gpuChannel: [1],
gpuEndChannel: [2]
},
'should reserve two channels for start and end timestamps'
);
const noRoom = assignGpuChannel(
{
id: '6',
dataId: ['a'],
gpu: true,
type: FILTER_TYPES.timeRange,
endName: ['end']
},
[
{id: '2', dataId: ['a'], gpu: true, gpuChannel: [0]},
{id: '3', dataId: ['a'], gpu: true, gpuChannel: [1]},
{id: '4', dataId: ['a'], gpu: true, gpuChannel: [2]}
]
);
t.deepEqual(
noRoom,
{
id: '6',
dataId: ['a'],
gpu: false,
type: FILTER_TYPES.timeRange,
endName: ['end']
},
'should fall back to CPU when two channels are not available'
);
t.end();
});
test('gpuFilterUtils -> getGpuFilterProps unchanged for single-channel filters', t => {
const filters = [
{
id: 'f1',
gpu: true,
type: FILTER_TYPES.timeRange,
dataId: ['ds'],
name: ['timestamp'],
fieldIdx: [0],
gpuChannel: [0],
domain: [1000, 5000],
value: [2000, 3000]
},
{
id: 'f2',
gpu: true,
type: FILTER_TYPES.range,
dataId: ['ds'],
name: ['value'],
fieldIdx: [1],
gpuChannel: [2],
domain: [0, 10],
value: [2, 8]
}
];
const fields = [
{name: 'timestamp', filterProps: {mappedValue: [1000, 2000]}, valueAccessor: d => d.timestamp},
{name: 'value', valueAccessor: d => d.value}
];
const gpu = getGpuFilterProps(filters, 'ds', fields);
t.deepEqual(gpu.filterRange[0], [1000, 2000], 'timeRange channel stays [v0-domain0, v1-domain0]');
t.deepEqual(gpu.filterRange[1], [0, 0], 'unused channel stays [0, 0]');
t.deepEqual(gpu.filterRange[2], [2, 8], 'range channel stays [v0-domain0, v1-domain0]');
t.equal(gpu.filterValueUpdateTriggers.gpuFilter_0.name, 'timestamp');
t.equal(gpu.filterValueUpdateTriggers.gpuFilter_2.name, 'value');
t.equal(gpu.filterValueUpdateTriggers.gpuFilter_1, null);
t.equal(Object.prototype.hasOwnProperty.call(filters[0], 'gpuEndChannel'), false);
const row = {index: 1, value: 6};
const activeValues = gpu.filterValueAccessor(null)()(row);
filters.forEach((filter, index) => {
const channel = filter.gpuChannel[0];
const disabledFilters = filters.map((f, i) => (i === index ? {...f, enabled: false} : f));
const disabled = getGpuFilterProps(disabledFilters, 'ds', fields, gpu);
t.deepEqual(
disabled.filterRange,
gpu.filterRange.map((range, i) => (i === channel ? [0, 0] : range)),
`${filter.type}: disabling leaves the other filter range intact`
);
t.equal(disabled.filterValueUpdateTriggers[`gpuFilter_${channel}`], null);
t.deepEqual(
disabled.filterValueAccessor(null)()(row),
activeValues.map((value, i) => (i === channel ? 0 : value)),
`${filter.type}: the disabled channel passes every row`
);
const reenabled = getGpuFilterProps(
disabledFilters.map((f, i) => (i === index ? {...f, enabled: true} : f)),
'ds',
fields,
disabled
);
t.deepEqual(reenabled.filterRange, gpu.filterRange, `${filter.type}: restores saved ranges`);
t.deepEqual(reenabled.filterValueUpdateTriggers, gpu.filterValueUpdateTriggers);
t.deepEqual(reenabled.filterValueAccessor(null)()(row), activeValues);
});
t.end();
});
test('gpuFilterUtils -> getGpuFilterProps interval overlap ranges', t => {
const filters = [
{
id: 'f1',
gpu: true,
type: FILTER_TYPES.timeRange,
dataId: ['ds'],
name: ['start'],
endName: ['end'],
fieldIdx: [0],
endFieldIdx: [1],
gpuChannel: [0],
gpuEndChannel: [1],
domain: [1000, 5000],
value: [2000, 3000]
}
];
const fields = [
{name: 'start', filterProps: {mappedValue: [1000, 2000]}, valueAccessor: d => d.start},
{name: 'end', filterProps: {mappedValue: [3000, 4000]}, valueAccessor: d => d.end}
];
const gpu = getGpuFilterProps(filters, 'ds', fields);
t.deepEqual(gpu.filterRange[0], [0, 2000], 'start channel should be [0, windowEnd - domain0]');
t.deepEqual(
gpu.filterRange[1],
[1000, 4000],
'end channel should be [windowStart - domain0, domain1 - domain0]'
);
t.equal(
gpu.filterValueUpdateTriggers.gpuFilter_0.name,
'start',
'start trigger uses start field'
);
t.equal(gpu.filterValueUpdateTriggers.gpuFilter_1.name, 'end', 'end trigger uses end field');
const disabled = getGpuFilterProps([{...filters[0], enabled: false}], 'ds', fields, gpu);
t.deepEqual(
disabled.filterRange,
[
[0, 0],
[0, 0],
[0, 0],
[0, 0]
],
'disabling an interval neutralizes both start and end ranges'
);
t.equal(disabled.filterValueUpdateTriggers.gpuFilter_0, null);
t.equal(disabled.filterValueUpdateTriggers.gpuFilter_1, null);
const row = {index: 1};
t.deepEqual(disabled.filterValueAccessor(null)()(row), [0, 0, 0, 0]);
const reenabled = getGpuFilterProps([{...filters[0], enabled: true}], 'ds', fields, disabled);
t.deepEqual(reenabled.filterRange, gpu.filterRange, 're-enabling restores the interval window');
t.deepEqual(reenabled.filterValueUpdateTriggers, gpu.filterValueUpdateTriggers);
t.deepEqual(
reenabled.filterValueAccessor(null)()(row),
[1000, 3000, 0, 0],
're-enabling restores both timestamp accessors'
);
t.end();
});