Skip to content

Commit

Permalink
fix(transform): stattistics
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxue committed Feb 26, 2019
1 parent bd12008 commit 49d8f60
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .torch.compile.opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module.exports = {
include: [
'src/**/*.js',
'test/**/*.js',
'node_modules/three/**/*.js'
'node_modules/three/**/*.js',
'node_modules/simple-statistics/src/*.js'
],
exclude: [
'node_modules/@babel/**/*.js'
Expand Down
3 changes: 3 additions & 0 deletions src/source/transform/hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export function pointToHexbin(data, option) {
const columns = getColumn(hex, option.field);
hex[option.method] = statistics[option.method](columns);
}
const item = {};
item[option.method] = hex[option.method];
return {
...item,
coordinates: unProjectFlat([ hex.x, hex.y ]),
id: index + 1
};
Expand Down
84 changes: 75 additions & 9 deletions src/source/transform/statistics.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
/* 支持 'max', 'mean', 'median', 'min', 'mode', 'product', 'standardDeviation',
* 'sum', 'sumSimple', 'variance', 'count', 'distinct'
*/
export { default as min } from 'simple-statistics/src/min';
export { default as max } from 'simple-statistics/src/max';
export { default as mean } from 'simple-statistics/src/mean';
export { default as sum } from 'simple-statistics/src/sum';
export { default as median } from 'simple-statistics/src/median';
export { default as standardDeviation } from 'simple-statistics/src/standard_deviation';
function max(x) {
if (x.length === 0) {
throw new Error('max requires at least one data point');
}

let value = x[0];
for (let i = 1; i < x.length; i++) {
// On the first iteration of this loop, max is
// undefined and is thus made the maximum element in the array
if (x[i] > value) {
value = x[i];
}
}
return value;
}

function min(x) {
if (x.length === 0) {
throw new Error('min requires at least one data point');
}

let value = x[0];
for (let i = 1; i < x.length; i++) {
// On the first iteration of this loop, min is
// undefined and is thus made the minimum element in the array
if (x[i] < value) {
value = x[i];
}
}
return value;
}

function sum(x) {
// If the array is empty, we needn't bother computing its sum
if (x.length === 0) {
return 0;
}

// Initializing the sum as the first number in the array
let sum = x[0];

// Keeping track of the floating-point error correction
let correction = 0;

let transition;

for (let i = 1; i < x.length; i++) {
transition = sum + x[i];

// Here we need to update the correction in a different fashion
// if the new absolute value is greater than the absolute sum
if (Math.abs(sum) >= Math.abs(x[i])) {
correction += sum - transition + x[i];
} else {
correction += x[i] - transition + sum;
}

sum = transition;
}

// Returning the corrected sum
return sum + correction;
}
function mean(x) {
if (x.length === 0) {
throw new Error('mean requires at least one data point');
}
return sum(x) / x.length;
}

export {
sum,
max,
min,
mean
};
4 changes: 3 additions & 1 deletion test/unit/source/transfrom/hexagon.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('hexagon Test', function() {
const data = {
dataArray
};
const hexgonGrid = pointToHexbin(data, { size: 100, field: 'count', method: 'sum' });
const hexgonGrid = pointToHexbin(data, { size: 100, field: 'v', method: 'sum' });
expect(hexgonGrid.dataArray.length).eql(567);
console.log(hexgonGrid);
});
});

0 comments on commit 49d8f60

Please sign in to comment.