-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathhistogram_test.js
355 lines (310 loc) · 12.4 KB
/
histogram_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
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() {
'use strict';
describe('supplyDefaults', function() {
var traceIn,
traceOut;
beforeEach(function() {
traceOut = {};
});
it('should set visible to false when x or y is empty', function() {
traceIn = {
x: []
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.visible).toBe(false);
traceIn = {
y: []
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.visible).toBe(false);
});
it('should set visible to false when type is histogram2d and x or y are empty', function() {
traceIn = {
type: 'histogram2d',
x: [],
y: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.visible).toBe(false);
traceIn = {
type: 'histogram2d',
x: [1, 2, 2],
y: []
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.visible).toBe(false);
traceIn = {
type: 'histogram2d',
x: [],
y: []
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.visible).toBe(false);
traceIn = {
type: 'histogram2dcontour',
x: [],
y: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.visible).toBe(false);
});
it('should set orientation to v by default', function() {
traceIn = {
x: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.orientation).toBe('v');
traceIn = {
x: [1, 2, 2],
y: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.orientation).toBe('v');
});
it('should set orientation to h when only y is supplied', function() {
traceIn = {
y: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.orientation).toBe('h');
});
// coercing bin attributes got moved to calc because it needs
// axis type - so here we just test that it's NOT happening
it('should not coerce autobinx regardless of xbins', function() {
traceIn = {
x: [1, 2, 2],
xbins: {
start: 1,
end: 3,
size: 1
}
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.autobinx).toBeUndefined();
traceIn = {
x: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.autobinx).toBeUndefined();
});
it('should not coerce autobiny regardless of ybins', function() {
traceIn = {
y: [1, 2, 2],
ybins: {
start: 1,
end: 3,
size: 1
}
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.autobiny).toBeUndefined();
traceIn = {
y: [1, 2, 2]
};
supplyDefaults(traceIn, traceOut, '', {});
expect(traceOut.autobiny).toBeUndefined();
});
it('should inherit layout.calendar', function() {
traceIn = {
x: [1, 2, 3]
};
supplyDefaults(traceIn, traceOut, '', {calendar: 'islamic'});
// we always fill calendar attributes, because it's hard to tell if
// we're on a date axis at this point.
// size axis calendar is weird, but *might* be able to happen if
// we're using histfunc=min or max (does this work?)
expect(traceOut.xcalendar).toBe('islamic');
expect(traceOut.ycalendar).toBe('islamic');
});
it('should take its own calendars', function() {
traceIn = {
x: [1, 2, 3],
xcalendar: 'coptic',
ycalendar: 'nepali'
};
supplyDefaults(traceIn, traceOut, '', {calendar: 'islamic'});
expect(traceOut.xcalendar).toBe('coptic');
expect(traceOut.ycalendar).toBe('nepali');
});
});
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;
}
var oneDay = 24 * 3600000;
it('should handle auto dates with nonuniform (month) bins', function() {
// All data on exact years: shift so bin center is an
// exact year, except on leap years
var out = _calc({
x: ['1970-01-01', '1970-01-01', '1971-01-01', '1973-01-01'],
nbinsx: 4
});
// TODO: 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?
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: Date.UTC(1970, 0, 1), s: 2},
{b: 0, p: Date.UTC(1971, 0, 1), s: 1},
{b: 0, p: Date.UTC(1972, 0, 1, 12), s: 0},
{b: 0, p: Date.UTC(1973, 0, 1), s: 1}
]);
// All data on exact months: shift so bin center is on (31-day months)
// or in (shorter months) that month
out = _calc({
x: ['1970-01-01', '1970-01-01', '1970-02-01', '1970-04-01'],
nbinsx: 4
});
expect(out).toEqual([
{b: 0, p: Date.UTC(1970, 0, 1), s: 2},
{b: 0, p: Date.UTC(1970, 1, 1), s: 1},
{b: 0, p: Date.UTC(1970, 2, 2, 12), s: 0},
{b: 0, p: Date.UTC(1970, 3, 1), s: 1}
]);
// data on exact days: shift so each bin goes from noon to noon
// even though this gives kind of odd bin centers since the bins
// are months... but the important thing is it's unambiguous which
// bin any given day is in.
out = _calc({
x: ['1970-01-02', '1970-01-31', '1970-02-13', '1970-04-19'],
nbinsx: 4
});
expect(out).toEqual([
// dec 31 12:00 -> jan 31 12:00, middle is jan 16
{b: 0, p: Date.UTC(1970, 0, 16), s: 2},
// jan 31 12:00 -> feb 28 12:00, middle is feb 14 12:00
{b: 0, p: Date.UTC(1970, 1, 14, 12), s: 1},
{b: 0, p: Date.UTC(1970, 2, 16), s: 0},
{b: 0, p: Date.UTC(1970, 3, 15, 12), 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 = 0,
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}
]);
});
describe('cumulative distribution functions', function() {
var base = {
x: [0, 5, 10, 15, 5, 10, 15, 10, 15, 15],
y: [2, 2, 2, 14, 6, 6, 6, 10, 10, 2]
};
it('makes the right base histogram', function() {
var baseOut = _calc(base);
expect(baseOut).toEqual([
{b: 0, p: 2, s: 1},
{b: 0, p: 7, s: 2},
{b: 0, p: 12, s: 3},
{b: 0, p: 17, s: 4},
]);
});
var CDFs = [
{p: [2, 7, 12, 17], s: [1, 3, 6, 10]},
{
direction: 'decreasing',
p: [2, 7, 12, 17], s: [10, 9, 7, 4]
},
{
currentbin: 'exclude',
p: [7, 12, 17, 22], s: [1, 3, 6, 10]
},
{
direction: 'decreasing', currentbin: 'exclude',
p: [-3, 2, 7, 12], s: [10, 9, 7, 4]
},
{
currentbin: 'half',
p: [2, 7, 12, 17, 22], s: [0.5, 2, 4.5, 8, 10]
},
{
direction: 'decreasing', currentbin: 'half',
p: [-3, 2, 7, 12, 17], s: [10, 9.5, 8, 5.5, 2]
},
{
direction: 'decreasing', currentbin: 'half', histnorm: 'percent',
p: [-3, 2, 7, 12, 17], s: [100, 95, 80, 55, 20]
},
{
currentbin: 'exclude', histnorm: 'probability',
p: [7, 12, 17, 22], s: [0.1, 0.3, 0.6, 1]
},
{
// behaves the same as without *density*
direction: 'decreasing', currentbin: 'half', histnorm: 'density',
p: [-3, 2, 7, 12, 17], s: [10, 9.5, 8, 5.5, 2]
},
{
// behaves the same as without *density*, only *probability*
direction: 'decreasing', currentbin: 'half', histnorm: 'probability density',
p: [-3, 2, 7, 12, 17], s: [1, 0.95, 0.8, 0.55, 0.2]
},
{
currentbin: 'half', histfunc: 'sum',
p: [2, 7, 12, 17, 22], s: [1, 6, 19, 44, 60]
},
{
currentbin: 'half', histfunc: 'sum', histnorm: 'probability',
p: [2, 7, 12, 17, 22], s: [0.5 / 30, 0.1, 9.5 / 30, 22 / 30, 1]
},
{
direction: 'decreasing', currentbin: 'half', histfunc: 'max', histnorm: 'percent',
p: [-3, 2, 7, 12, 17], s: [100, 3100 / 32, 2700 / 32, 1900 / 32, 700 / 32]
},
{
direction: 'decreasing', currentbin: 'half', histfunc: 'min', histnorm: 'density',
p: [-3, 2, 7, 12, 17], s: [8, 7, 5, 3, 1]
},
{
currentbin: 'exclude', histfunc: 'avg', histnorm: 'probability density',
p: [7, 12, 17, 22], s: [0.1, 0.3, 0.6, 1]
}
];
CDFs.forEach(function(CDF) {
var p = CDF.p,
s = CDF.s;
it('handles direction=' + CDF.direction + ', currentbin=' + CDF.currentbin +
', histnorm=' + CDF.histnorm + ', histfunc=' + CDF.histfunc, function() {
var traceIn = Lib.extendFlat({}, base, {
cumulative: {
enabled: true,
direction: CDF.direction,
currentbin: CDF.currentbin
},
histnorm: CDF.histnorm,
histfunc: CDF.histfunc
});
var out = _calc(traceIn);
expect(out.length).toBe(p.length);
out.forEach(function(outi, i) {
expect(outi.p).toBe(p[i]);
expect(outi.s).toBeCloseTo(s[i], 6);
expect(outi.b).toBe(0);
});
});
});
});
});
});