Skip to content

Commit 447021b

Browse files
committed
replace geomean with geometric mean or geometricMean
fix geometricMean computation
1 parent 9da8b07 commit 447021b

File tree

7 files changed

+18
-18
lines changed

7 files changed

+18
-18
lines changed

draftlogs/6223_add.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
- Add geomean functionality and 'geomean ascending' + 'geomean descending' to `category_order` on cartesian axes [[#6223](https://github.com/plotly/plotly.js/pull/6223)]
1+
- Add geometric mean functionality and 'geometric mean ascending' + 'geometric mean descending' to `category_order` on cartesian axes [[#6223](https://github.com/plotly/plotly.js/pull/6223)]
22
with thanks to @acxz and @prabhathc for the contribution!

src/lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ var statsModule = require('./stats');
114114
lib.aggNums = statsModule.aggNums;
115115
lib.len = statsModule.len;
116116
lib.mean = statsModule.mean;
117-
lib.geoMean = statsModule.geoMean;
117+
lib.geometricMean = statsModule.geometricMean;
118118
lib.median = statsModule.median;
119119
lib.midRange = statsModule.midRange;
120120
lib.variance = statsModule.variance;

src/lib/stats.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ exports.mean = function(data, len) {
4747
return exports.aggNums(function(a, b) { return a + b; }, 0, data) / len;
4848
};
4949

50-
exports.geoMean = function(data, len) {
50+
exports.geometricMean = function(data, len) {
5151
if(!len) len = exports.len(data);
52-
return exports.aggNums(function(a, b) { return a * b; }, 0, data) ** (1 / len);
53-
}
52+
return Math.pow(exports.aggNums(function(a, b) { return a * b; }, 1, data), 1 / len);
53+
};
5454

5555
exports.midRange = function(numArr) {
5656
if(numArr === undefined || numArr.length === 0) return undefined;

src/plots/cartesian/layout_attributes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,7 @@ module.exports = {
10121012
'max ascending', 'max descending',
10131013
'sum ascending', 'sum descending',
10141014
'mean ascending', 'mean descending',
1015-
'geomean ascending', 'geomean descending',
1015+
'geometric mean ascending', 'geometric mean descending',
10161016
'median ascending', 'median descending'
10171017
],
10181018
dflt: 'trace',
@@ -1027,7 +1027,7 @@ module.exports = {
10271027
'the *trace* mode. The unspecified categories will follow the categories in `categoryarray`.',
10281028
'Set `categoryorder` to *total ascending* or *total descending* if order should be determined by the',
10291029
'numerical order of the values.',
1030-
'Similarly, the order can be determined by the min, max, sum, mean, geomean or median of all the values.'
1030+
'Similarly, the order can be determined by the min, max, sum, mean, geometric mean or median of all the values.'
10311031
].join(' ')
10321032
},
10331033
categoryarray: {

src/plots/plots.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3092,7 +3092,7 @@ plots.doCalcdata = function(gd, traces) {
30923092
Registry.getComponentMethod('errorbars', 'calc')(gd);
30933093
};
30943094

3095-
var sortAxisCategoriesByValueRegex = /(total|sum|min|max|mean|median) (ascending|descending)/;
3095+
var sortAxisCategoriesByValueRegex = /(total|sum|min|max|mean|geometric mean|median) (ascending|descending)/;
30963096

30973097
function sortAxisCategoriesByValue(axList, gd) {
30983098
var affectedTraces = [];
@@ -3127,7 +3127,7 @@ function sortAxisCategoriesByValue(axList, gd) {
31273127
'sum': function(values) {return Lib.aggNums(function(a, b) { return a + b;}, null, values);},
31283128
'total': function(values) {return Lib.aggNums(function(a, b) { return a + b;}, null, values);},
31293129
'mean': function(values) {return Lib.mean(values);},
3130-
'geomean': function(values) {return Lib.geoMean(values);},
3130+
'geometric mean': function(values) {return Lib.geometricMean(values);},
31313131
'median': function(values) {return Lib.median(values);}
31323132
};
31333133

test/jasmine/tests/calcdata_test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1154,19 +1154,19 @@ describe('calculated data and points', function() {
11541154
checkAggregatedValue(baseMock, expectedAgg, false, done);
11551155
});
11561156

1157-
it('takes the geomean of all values per category across traces of type ' + trace.type, function(done) {
1157+
it('takes the geometric mean of all values per category across traces of type ' + trace.type, function(done) {
11581158
var type = trace.type;
11591159
var data = [7, 2, 3];
11601160
var data2 = [5, 4, 2];
11611161
var baseMock = { data: [makeData(type, axName, cat, data), makeData(type, axName, cat, data2)], layout: {}};
1162-
baseMock.layout[axName] = { type: 'category', categoryorder: 'geomean ascending'};
1162+
baseMock.layout[axName] = { type: 'category', categoryorder: 'geometric mean ascending'};
11631163

11641164
var expectedAgg = [['a', Math.sqrt(data[0] * data2[0])], ['b', Math.sqrt(data[1] * data2[1])], ['c', Math.sqrt(data[2] * data2[2])]];
11651165
// TODO: how to actually calc these? what do these even mean?
11661166
if(type === 'histogram') expectedAgg = [['a', 2], ['b', 1], ['c', 1]];
1167-
if(type === 'histogram2d') expectedAgg = [['a', 2 / 3], ['b', 1 / 3], ['c', 1 / 3]];
1168-
if(type === 'contour' || type === 'heatmap') expectedAgg = [['a', expectedAgg[0][1] / 3], ['b', expectedAgg[1][1] / 3], ['c', expectedAgg[2][1] / 3]];
1169-
if(type === 'histogram2dcontour') expectedAgg = [['a', 2 / 4], ['b', 1 / 4], ['c', 1 / 4]];
1167+
if(type === 'histogram2d') expectedAgg = [['a', 0], ['b', 0], ['c', 0]];
1168+
if(type === 'contour' || type === 'heatmap') expectedAgg = [['a', 0], ['b', 0], ['c', 0]];
1169+
if(type === 'histogram2dcontour') expectedAgg = [['a', 0], ['b', 0], ['c', 0]];
11701170

11711171
checkAggregatedValue(baseMock, expectedAgg, false, done);
11721172
});

test/jasmine/tests/lib_test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,20 @@ describe('Test lib.js:', function() {
126126
});
127127
});
128128

129-
describe('geomean() should', function() {
129+
describe('geometricMean() should', function() {
130130
it('toss out non-numerics (strings)', function() {
131131
var input = [1, 2, 'apple', 'orange'];
132-
var res = Lib.geomean(input);
132+
var res = Lib.geometricMean(input);
133133
expect(res).toBeCloseTo(1.414, 3);
134134
});
135135
it('toss out non-numerics (NaN)', function() {
136136
var input = [1, 2, NaN];
137-
var res = Lib.geomean(input);
137+
var res = Lib.geometricMean(input);
138138
expect(res).toBeCloseTo(1.414, 3);
139139
});
140140
it('evaluate numbers which are passed around as text strings:', function() {
141141
var input = ['1', '2'];
142-
var res = Lib.geomean(input);
142+
var res = Lib.geometricMean(input);
143143
expect(res).toBeCloseTo(1.414, 3);
144144
});
145145
});

0 commit comments

Comments
 (0)