This repository has been archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
significantDimension.js
75 lines (71 loc) · 2.52 KB
/
significantDimension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import tailLength from './tailLength.js';
import round from './round.js';
import { uniq, isFinite } from 'underscore';
/**
* computes the significant dimension for a list of numbers
* That's the number of decimals to which we can round the numbers
* without loosing information
*
* @exports significantDimension
* @kind function
*
* @example
* import {significantDimension} from '@datawrapper/shared/significantDimension';
* significantDimension([0,10,20,30]); // -1
*
* @param {number[]} values - list of input numbers
* @param {number} tolerance - percent of input values that we allow to "collide"
* @returns {number} - number of significant dimensions (= the number of decimals)
*/
export default function significantDimension(values, tolerance = 0.1) {
let result = [];
let decimals = 0;
const uniqValues = uniq(values.filter(isFinite));
const totalUniq = uniqValues.length;
let check, diff;
const accepted = Math.floor(totalUniq * (1 - tolerance));
if (uniqValues.length < 3) {
// special case if there are only 2 unique values
return Math.round(
uniqValues.reduce(function (acc, cur) {
if (!cur) return acc;
const exp = Math.log(Math.abs(cur)) / Math.LN10;
if (exp < 8 && exp > -3) {
// use tail length for normal numbers
return acc + Math.min(3, tailLength(uniqValues[0]));
} else {
return acc + (exp > 0 ? (exp - 1) * -1 : exp * -1);
}
}, 0) / uniqValues.length
);
}
if (uniq(uniqValues.map(currentRound)).length > accepted) {
// we seem to have enough precision, but maybe it's too much?
check = function () {
return uniq(result).length === totalUniq;
};
diff = -1;
} else {
// if we end up here it means we're loosing too much information
// due to rounding, we need to increase precision
check = function () {
return uniq(result).length <= accepted;
};
diff = +1;
}
let maxIter = 100;
do {
result = uniqValues.map(currentRound);
decimals += diff;
} while (check() && maxIter-- > 0);
if (maxIter < 10) {
console.warn('maximum iteration reached', values, result, decimals);
}
if (diff < 0) decimals += 2;
else decimals--;
/* rounds to the current number of decimals */
function currentRound(v) {
return round(v, decimals);
}
return decimals;
}