Skip to content

violin: handle zero standard deviation #3625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions src/traces/violin/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,37 @@ module.exports = function calc(gd, trace) {
for(var i = 0; i < cd.length; i++) {
var cdi = cd[i];
var vals = cdi.pts.map(helpers.extractVal);

var bandwidth = cdi.bandwidth = calcBandwidth(trace, cdi, vals);
var span = cdi.span = calcSpan(trace, cdi, valAxis, bandwidth);

// step that well covers the bandwidth and is multiple of span distance
var dist = span[1] - span[0];
var n = Math.ceil(dist / (bandwidth / 3));
var step = dist / n;

if(!isFinite(step) || !isFinite(n)) {
Lib.error('Something went wrong with computing the violin span');
cd[0].t.empty = true;
return cd;
}

var kde = helpers.makeKDE(cdi, trace, vals);
cdi.density = new Array(n);

for(var k = 0, t = span[0]; t < (span[1] + step / 2); k++, t += step) {
var v = kde(t);
cdi.density[k] = {v: v, t: t};
maxKDE = Math.max(maxKDE, v);
var span;

if(cdi.min === cdi.max) {
cdi.spanZero = true;
span = cdi.span = [cdi.min, cdi.max];
cdi.density = [{v: 1, t: span[0]}];
cdi.bandwidth = 1;
maxKDE = Math.max(maxKDE, 1);
} else {
Copy link
Contributor Author

@antoinerg antoinerg Mar 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What follows is exactly the same code we had before.

var bandwidth = cdi.bandwidth = calcBandwidth(trace, cdi, vals);
span = cdi.span = calcSpan(trace, cdi, valAxis, bandwidth);

// step that well covers the bandwidth and is multiple of span distance
var dist = span[1] - span[0];
var n = Math.ceil(dist / (bandwidth / 3));
var step = dist / n;

if(!isFinite(step) || !isFinite(n)) {
Lib.error('Something went wrong with computing the violin span');
cd[0].t.empty = true;
return cd;
}

var kde = helpers.makeKDE(cdi, trace, vals);
cdi.density = new Array(n);

for(var k = 0, t = span[0]; t < (span[1] + step / 2); k++, t += step) {
var v = kde(t);
cdi.density[k] = {v: v, t: t};
maxKDE = Math.max(maxKDE, v);
}
}

maxCount = Math.max(maxCount, vals.length);
Expand Down
Binary file modified test/image/baselines/groups-over-matching-axes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/violin-offsetgroups.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/violin_only_zeroes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions test/image/mocks/violin_only_zeroes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": [{
"type": "violin",
"y": [0, 0, 0, 0],
"points": "all",
"pointpos": 0,
"spanmode": "hard",
"meanline": {
"visible": true
}
}, {
"type": "violin",
"y": [2, 2, 2, 2],
"points": "all",
"pointpos": 0,
"spanmode": "hard"
}, {
"type": "violin",
"y": [0, 1, 1.5, 2],
"points": "all",
"pointpos": 0
}]
}