From 74ba473c75f14be9e3dc4ce03d4beb05bd7c1d3d Mon Sep 17 00:00:00 2001 From: acxz <17132214+acxz@users.noreply.github.com> Date: Sun, 12 Jun 2022 20:37:18 -0400 Subject: [PATCH] add geomean test --- test/jasmine/tests/calcdata_test.js | 17 +++++++++++++++++ test/jasmine/tests/lib_test.js | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/test/jasmine/tests/calcdata_test.js b/test/jasmine/tests/calcdata_test.js index 801aaa96acd..2bb185fca05 100644 --- a/test/jasmine/tests/calcdata_test.js +++ b/test/jasmine/tests/calcdata_test.js @@ -1154,6 +1154,23 @@ describe('calculated data and points', function() { checkAggregatedValue(baseMock, expectedAgg, false, done); }); + it('takes the geomean of all values per category across traces of type ' + trace.type, function(done) { + var type = trace.type; + var data = [7, 2, 3]; + var data2 = [5, 4, 2]; + var baseMock = { data: [makeData(type, axName, cat, data), makeData(type, axName, cat, data2)], layout: {}}; + baseMock.layout[axName] = { type: 'category', categoryorder: 'geomean ascending'}; + + var expectedAgg = [['a', Math.sqrt(data[0] * data2[0])], ['b', Math.sqrt(data[1] * data2[1])], ['c', Math.sqrt(data[2] * data2[2])]]; + // TODO: how to actually calc these? what do these even mean? + if(type === 'histogram') expectedAgg = [['a', 2], ['b', 1], ['c', 1]]; + if(type === 'histogram2d') expectedAgg = [['a', 2 / 3], ['b', 1 / 3], ['c', 1 / 3]]; + if(type === 'contour' || type === 'heatmap') expectedAgg = [['a', expectedAgg[0][1] / 3], ['b', expectedAgg[1][1] / 3], ['c', expectedAgg[2][1] / 3]]; + if(type === 'histogram2dcontour') expectedAgg = [['a', 2 / 4], ['b', 1 / 4], ['c', 1 / 4]]; + + checkAggregatedValue(baseMock, expectedAgg, false, done); + }); + it('takes the median of all values per category across traces of type ' + trace.type, function(done) { var type = trace.type; var data = [7, 2, 3]; diff --git a/test/jasmine/tests/lib_test.js b/test/jasmine/tests/lib_test.js index 647c9c4d5b9..bae5f206192 100644 --- a/test/jasmine/tests/lib_test.js +++ b/test/jasmine/tests/lib_test.js @@ -126,6 +126,24 @@ describe('Test lib.js:', function() { }); }); + describe('geomean() should', function() { + it('toss out non-numerics (strings)', function() { + var input = [1, 2, 'apple', 'orange']; + var res = Lib.geomean(input); + expect(res).toBeCloseTo(1.414, 3); + }); + it('toss out non-numerics (NaN)', function() { + var input = [1, 2, NaN]; + var res = Lib.geomean(input); + expect(res).toBeCloseTo(1.414, 3); + }); + it('evaluate numbers which are passed around as text strings:', function() { + var input = ['1', '2']; + var res = Lib.geomean(input); + expect(res).toBeCloseTo(1.414, 3); + }); + }); + describe('midRange() should', function() { it('should calculate the arithmetic mean of the maximum and minimum value of a given array', function() { var input = [1, 5.5, 6, 15, 10, 13];