From b824d8eb06fc34a69660b4a03eefd7a36d187802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Etienne=20T=C3=A9treault-Pinard?= Date: Mon, 17 Jul 2017 15:57:38 -0400 Subject: [PATCH 1/2] compute histogram bin positions beyond length 5000 - to correctly handle (very) small bins specs - to avoid, infinite loops, break in while loop when tick increment does not converge. --- src/traces/histogram/calc.js | 4 +++- test/jasmine/tests/histogram_test.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/traces/histogram/calc.js b/src/traces/histogram/calc.js index 95f97930e72..fe0e525491a 100644 --- a/src/traces/histogram/calc.js +++ b/src/traces/histogram/calc.js @@ -106,7 +106,7 @@ module.exports = function calc(gd, trace) { // decrease end a little in case of rounding errors binend = pr2c(binspec.end) + (i - Axes.tickIncrement(i, binspec.size, false, calendar)) / 1e6; - while(i < binend && pos.length < 5000) { + while(i < binend) { i2 = Axes.tickIncrement(i, binspec.size, false, calendar); pos.push((i + i2) / 2); size.push(sizeinit); @@ -116,6 +116,8 @@ module.exports = function calc(gd, trace) { // nonuniform bins also need nonuniform normalization factors if(densitynorm) inc.push(1 / (i2 - i)); if(doavg) counts.push(0); + // break to avoid infinite loops + if(i2 <= i) break; i = i2; } diff --git a/test/jasmine/tests/histogram_test.js b/test/jasmine/tests/histogram_test.js index a9fed476675..8c257a9b12d 100644 --- a/test/jasmine/tests/histogram_test.js +++ b/test/jasmine/tests/histogram_test.js @@ -246,6 +246,19 @@ describe('Test histogram', function() { ]); }); + it('should handle very small bins', function() { + var out = _calc({ + x: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], + xbins: { + start: 0, + end: 10, + size: 0.001 + } + }); + + expect(out.length).toEqual(9001); + }); + describe('cumulative distribution functions', function() { var base = { x: [0, 5, 10, 15, 5, 10, 15, 10, 15, 15], From eef9158d82c1d307b479721b391d074415af46c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Etienne=20T=C3=A9treault-Pinard?= Date: Wed, 19 Jul 2017 13:34:53 -0400 Subject: [PATCH 2/2] add 1e6 upper bound to pos array length --- src/traces/histogram/calc.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/traces/histogram/calc.js b/src/traces/histogram/calc.js index fe0e525491a..f1bca0555e7 100644 --- a/src/traces/histogram/calc.js +++ b/src/traces/histogram/calc.js @@ -106,7 +106,7 @@ module.exports = function calc(gd, trace) { // decrease end a little in case of rounding errors binend = pr2c(binspec.end) + (i - Axes.tickIncrement(i, binspec.size, false, calendar)) / 1e6; - while(i < binend) { + while(i < binend && pos.length < 1e6) { i2 = Axes.tickIncrement(i, binspec.size, false, calendar); pos.push((i + i2) / 2); size.push(sizeinit);