Skip to content

Fix date histograms #1186

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

Merged
merged 2 commits into from
Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 13 additions & 3 deletions src/traces/histogram/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ module.exports = function calc(gd, trace) {
}

var binspec = trace[maindata + 'bins'],
allbins = typeof binspec.size === 'string',
bins = allbins ? [] : binspec,
nonuniformBins = typeof binspec.size === 'string',
bins = nonuniformBins ? [] : binspec,
// make the empty bin array
i2,
binend,
Expand Down Expand Up @@ -85,13 +85,23 @@ module.exports = function calc(gd, trace) {
size.push(sizeinit);
// nonuniform bins (like months) we need to search,
// rather than straight calculate the bin we're in
if(allbins) bins.push(i);
if(nonuniformBins) bins.push(i);
// nonuniform bins also need nonuniform normalization factors
if(densitynorm) inc.push(1 / (i2 - i));
if(doavg) counts.push(0);
i = i2;
}

// for date axes we need bin bounds to be calcdata. For nonuniform bins
// we already have this, but uniform with start/end/size they're still strings.
if(!nonuniformBins && pa.type === 'date') {
bins = {
start: pa.r2c(bins.start),
end: pa.r2c(bins.end),
size: bins.size
};
}

var nMax = size.length;
// bin the data
for(i = 0; i < pos0.length; i++) {
Expand Down
35 changes: 27 additions & 8 deletions src/traces/histogram2d/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ module.exports = function calc(gd, trace) {
z = [];
var onecol = [],
zerocol = [],
xbins = (typeof(trace.xbins.size) === 'string') ? [] : trace.xbins,
ybins = (typeof(trace.xbins.size) === 'string') ? [] : trace.ybins,
nonuniformBinsX = (typeof(trace.xbins.size) === 'string'),
nonuniformBinsY = (typeof(trace.ybins.size) === 'string'),
xbins = nonuniformBinsX ? [] : trace.xbins,
ybins = nonuniformBinsY ? [] : trace.ybins,
total = 0,
n,
m,
Expand Down Expand Up @@ -103,10 +105,10 @@ module.exports = function calc(gd, trace) {

for(i = binStart; i < binEnd; i = Axes.tickIncrement(i, binspec.size)) {
onecol.push(sizeinit);
if(Array.isArray(xbins)) xbins.push(i);
if(nonuniformBinsX) xbins.push(i);
if(doavg) zerocol.push(0);
}
if(Array.isArray(xbins)) xbins.push(i);
if(nonuniformBinsX) xbins.push(i);

var nx = onecol.length;
x0 = trace.xbins.start;
Expand All @@ -121,10 +123,10 @@ module.exports = function calc(gd, trace) {

for(i = binStart; i < binEnd; i = Axes.tickIncrement(i, binspec.size)) {
z.push(onecol.concat());
if(Array.isArray(ybins)) ybins.push(i);
if(nonuniformBinsY) ybins.push(i);
if(doavg) counts.push(zerocol.concat());
}
if(Array.isArray(ybins)) ybins.push(i);
if(nonuniformBinsY) ybins.push(i);

var ny = z.length;
y0 = trace.ybins.start;
Expand All @@ -134,15 +136,32 @@ module.exports = function calc(gd, trace) {

if(densitynorm) {
xinc = onecol.map(function(v, i) {
if(Array.isArray(xbins)) return 1 / (xbins[i + 1] - xbins[i]);
if(nonuniformBinsX) return 1 / (xbins[i + 1] - xbins[i]);
return 1 / dx;
});
yinc = z.map(function(v, i) {
if(Array.isArray(ybins)) return 1 / (ybins[i + 1] - ybins[i]);
if(nonuniformBinsY) return 1 / (ybins[i + 1] - ybins[i]);
return 1 / dy;
});
}

// for date axes we need bin bounds to be calcdata. For nonuniform bins
// we already have this, but uniform with start/end/size they're still strings.
if(!nonuniformBinsX && xa.type === 'date') {
xbins = {
start: xa.r2c(xbins.start),
end: xa.r2c(xbins.end),
size: xbins.size
};
}
if(!nonuniformBinsY && ya.type === 'date') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need this here? Lib.findBin is called below.

Copy link
Collaborator Author

@alexcjohnson alexcjohnson Nov 22, 2016

Choose a reason for hiding this comment

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

Described above:

At its root, the issue is that Lib.findBin doesn't know anything about the axis it's binning onto, so needs bins specified entirely by numbers, either {start, end, size} or [edge0, edge1, edge2, ... edgeN], but in the new date system, start and end are date strings. For performance it's important that these be turned into numbers only once, so rather than hack date conversion into findBin we convert the bins object everywhere it gets called.

We could refactor findBin to be a higher-order function:

var findBin = Lib.binFinder(bins, ax);

for(i...) findBin(x[i]);

That would actually have some performance benefits, but it's a little bit of a bigger project because of all the places this gets called.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. Sorry I thought that first patched file ⏫ was lib/search.js. Time to grab another ☕

ybins = {
start: ya.r2c(ybins.start),
end: ya.r2c(ybins.end),
size: ybins.size
};
}


// put data into bins
for(i = 0; i < serieslen; i++) {
Expand Down
51 changes: 51 additions & 0 deletions test/jasmine/tests/histogram2d_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var Plots = require('@src/plots/plots');
var Lib = require('@src/lib');

var supplyDefaults = require('@src/traces/histogram2d/defaults');
var calc = require('@src/traces/histogram2d/calc');


describe('Test histogram2d', function() {
Expand Down Expand Up @@ -56,4 +60,51 @@ describe('Test histogram2d', function() {

});


describe('calc', function() {
function _calc(opts) {
var base = { type: 'histogram2d' },
trace = Lib.extendFlat({}, base, opts),
gd = { data: [trace] };

Plots.supplyDefaults(gd);
var fullTrace = gd._fullData[0];

var out = calc(gd, fullTrace);
delete out.trace;
return out;
}

// remove tzOffset when we move to UTC
var oneDay = 24 * 3600000;

it('should handle both uniform and nonuniform date bins', function() {
var out = _calc({
x: ['1970-01-01', '1970-01-01', '1970-01-02', '1970-01-04'],
nbinsx: 4,
y: ['1970-01-01', '1970-01-01', '1971-01-01', '1973-01-01'],
nbinsy: 4
});

expect(out.x0).toBe('1970-01-01');
expect(out.dx).toBe(oneDay);

// TODO: even though the binning is done on non-uniform bins,
// the display makes them linear (using only y0 and dy)
// when we sort out https://github.com/plotly/plotly.js/issues/1151
// lets also make it display the bins with nonuniform size,
// and ensure we don't generate an extra bin on the end (see
// first row of z below)
expect(out.y0).toBe('1969-07-02 15:24');
expect(out.dy).toBe(365.2 * oneDay);

expect(out.z).toEqual([
[0, 0, 0, 0],
[2, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 1]
]);
});
});
});
73 changes: 73 additions & 0 deletions test/jasmine/tests/histogram_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var Plots = require('@src/plots/plots');
var Lib = require('@src/lib');

var supplyDefaults = require('@src/traces/histogram/defaults');
var calc = require('@src/traces/histogram/calc');


describe('Test histogram', function() {
Expand Down Expand Up @@ -127,4 +131,73 @@ describe('Test histogram', function() {

});


describe('calc', function() {
function _calc(opts) {
var base = { type: 'histogram' },
trace = Lib.extendFlat({}, base, opts),
gd = { data: [trace] };

Plots.supplyDefaults(gd);
var fullTrace = gd._fullData[0];

var out = calc(gd, fullTrace);
delete out[0].trace;
return out;
}

// remove tzOffset when we move to UTC
var tzOffset = (new Date(1970, 0, 1)).getTimezoneOffset() * 60000,
oneDay = 24 * 3600000;

it('should handle auto dates with nonuniform (month) bins', function() {
var out = _calc({
x: ['1970-01-01', '1970-01-01', '1971-01-01', '1973-01-01'],
nbinsx: 4
});

// TODO: https://github.com/plotly/plotly.js/issues/1151
// these bins should shift when we implement that

// note that x1-x0 = 365 days, but the others are 365.5 days

// ALSO: this gives half-day gaps between all but the first two
// bars. Now that we have explicit per-bar positioning, perhaps
// we should fill the space, rather than insisting on equal-width
// bars?
var x0 = tzOffset + 15768000000,
x1 = x0 + oneDay * 365,
x2 = x1 + oneDay * 365.5,
x3 = x2 + oneDay * 365.5;

expect(out).toEqual([
// full calcdata has x and y too (and t in the first one),
// but those come later from setPositions.
{b: 0, p: x0, s: 2},
{b: 0, p: x1, s: 1},
{b: 0, p: x2, s: 0},
{b: 0, p: x3, s: 1}
]);
});

it('should handle auto dates with uniform (day) bins', function() {
var out = _calc({
x: ['1970-01-01', '1970-01-01', '1970-01-02', '1970-01-04'],
nbinsx: 4
});

var x0 = tzOffset,
x1 = x0 + oneDay,
x2 = x1 + oneDay,
x3 = x2 + oneDay;

expect(out).toEqual([
{b: 0, p: x0, s: 2},
{b: 0, p: x1, s: 1},
{b: 0, p: x2, s: 0},
{b: 0, p: x3, s: 1}
]);
});

});
});