Skip to content

Commit 712a124

Browse files
committed
WIP of decoding in calc.
Works for initial render, but typed array is overwritten when supplyDefaults is run again.
1 parent 67ccac9 commit 712a124

File tree

4 files changed

+129
-44
lines changed

4 files changed

+129
-44
lines changed

src/lib/array.js

+112-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88

99
'use strict';
10+
var b64 = require('base64-arraybuffer');
11+
var isPlainObject = require('./is_plain_object');
1012

1113
var isArray = Array.isArray;
1214

@@ -39,7 +41,7 @@ exports.isArrayOrTypedArray = isArrayOrTypedArray;
3941
* not consistent we won't figure that out here.
4042
*/
4143
function isArray1D(a) {
42-
return !isArrayOrTypedArray(a[0]);
44+
return !(isArrayOrTypedArray(a[0]) || (isTypedArraySpec(a) && a.ndims === 1));
4345
}
4446
exports.isArray1D = isArray1D;
4547

@@ -63,6 +65,113 @@ exports.ensureArray = function(out, n) {
6365
return out;
6466
};
6567

68+
var typedArrays = {
69+
int8: typeof Int8Array !== 'undefined' ? Int8Array : null,
70+
uint8: typeof Uint8Array !== 'undefined' ? Uint8Array : null,
71+
uint8clamped: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : null,
72+
int16: typeof Int16Array !== 'undefined' ? Int16Array : null,
73+
uint16: typeof Uint16Array !== 'undefined' ? Uint16Array : null,
74+
int32: typeof Int32Array !== 'undefined' ? Int32Array : null,
75+
uint32: typeof Uint32Array !== 'undefined' ? Uint32Array : null,
76+
float32: typeof Float32Array !== 'undefined' ? Float32Array : null,
77+
float64: typeof Float64Array !== 'undefined' ? Float64Array : null,
78+
bigint64: typeof BigInt64Array !== 'undefined' ? BigInt64Array : null,
79+
biguint64: typeof BigUint64Array !== 'undefined' ? BigUint64Array : null
80+
};
81+
exports.typedArrays = typedArrays;
82+
83+
84+
exports.decodeTypedArraySpec = function(v) {
85+
// Assume processed by coerceTypedArraySpec
86+
var T = typedArrays[v.dtype];
87+
var buffer;
88+
if(v.bvals.constructor === ArrayBuffer) {
89+
// Already an ArrayBuffer
90+
buffer = v.bvals;
91+
} else {
92+
// Decode, assuming a string
93+
buffer = b64.decode(v.bvals);
94+
}
95+
96+
// Check if 1d shape. If so, we're done
97+
if(v.ndims === 1) {
98+
// Construct single Typed array over entire buffer
99+
return new T(buffer);
100+
} else {
101+
// Reshape into nested plain arrays with innermost
102+
// level containing typed arrays
103+
// We could eventually adopt an ndarray library
104+
105+
// Build cumulative product of dimensions
106+
var cumulativeShape = v.shape.map(function(a, i) {
107+
return a * (v.shape[i - 1] || 1);
108+
});
109+
110+
// Loop of dimensions in reverse order
111+
var nestedArray = [];
112+
for(var dimInd = v.ndims - 1; dimInd > 0; dimInd--) {
113+
var subArrayLength = v.shape[dimInd];
114+
var numSubArrays = cumulativeShape[dimInd - 1];
115+
var nextArray = [];
116+
117+
if(dimInd === v.ndims - 1) {
118+
// First time through, we build the
119+
// inner most typed arrays
120+
for(var typedInd = 0; typedInd < numSubArrays; typedInd++) {
121+
var typedOffset = typedInd * subArrayLength;
122+
nextArray.push(
123+
new T(buffer, typedOffset * T.BYTES_PER_ELEMENT, subArrayLength)
124+
);
125+
}
126+
} else {
127+
// Following times through, build
128+
// next layer of nested arrays
129+
for(var i = 0; i < numSubArrays; i++) {
130+
var offset = i * subArrayLength;
131+
nextArray.push(nextArray.slice(offset, offset + subArrayLength - 1));
132+
}
133+
}
134+
135+
// Update nested array with next nesting level
136+
nestedArray = nextArray;
137+
}
138+
139+
return nestedArray;
140+
}
141+
};
142+
143+
function isTypedArraySpec(v) {
144+
// Assume v has not passed through
145+
return isPlainObject(v) && typedArrays[v.dtype] && v.bvals && (
146+
Number.isInteger(v.shape) ||
147+
(isArrayOrTypedArray(v.shape) &&
148+
v.shape.length > 0 &&
149+
v.shape.every(function(d) { return Number.isInteger(d); }))
150+
);
151+
}
152+
exports.isTypedArraySpec = isTypedArraySpec;
153+
154+
function coerceTypedArraySpec(v) {
155+
// Assume isTypedArraySpec passed
156+
var coerced = {dtype: v.dtype, bvals: v.bvals};
157+
158+
// Normalize shape to a list
159+
if(Number.isInteger(v.shape)) {
160+
coerced.shape = [v.shape];
161+
} else {
162+
coerced.shape = v.shape;
163+
}
164+
165+
// Add length property
166+
coerced.length = v.shape.reduce(function(a, b) { return a * b; });
167+
168+
// Add ndims
169+
coerced.ndims = v.shape.length;
170+
171+
return coerced;
172+
}
173+
exports.coerceTypedArraySpec = coerceTypedArraySpec;
174+
66175
/*
67176
* TypedArray-compatible concatenation of n arrays
68177
* if all arrays are the same type it will preserve that type,
@@ -150,6 +259,8 @@ function _rowLength(z, fn, len0) {
150259
} else {
151260
return z.length;
152261
}
262+
} else if(isTypedArraySpec(z)) {
263+
return z.shape[z.shape.length - 1];
153264
}
154265
return 0;
155266
}

src/lib/coerce.js

+5-20
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,10 @@ var DESELECTDIM = require('../constants/interactions').DESELECTDIM;
1818
var nestedProperty = require('./nested_property');
1919
var counterRegex = require('./regex').counter;
2020
var modHalf = require('./mod').modHalf;
21-
var isPlainObject = require('./is_plain_object');
2221
var isArrayOrTypedArray = require('./array').isArrayOrTypedArray;
22+
var isTypedArraySpec = require('./array').isTypedArraySpec;
23+
var coerceTypedArraySpec = require('./array').coerceTypedArraySpec;
2324

24-
var typedArrays = {
25-
int8: typeof Int8Array !== 'undefined' ? 1 : 0,
26-
uint8: typeof Uint8Array !== 'undefined' ? 1 : 0,
27-
uint8clamped: typeof Uint8ClampedArray !== 'undefined' ? 1 : 0,
28-
int16: typeof Int16Array !== 'undefined' ? 1 : 0,
29-
uint16: typeof Uint16Array !== 'undefined' ? 1 : 0,
30-
int32: typeof Int32Array !== 'undefined' ? 1 : 0,
31-
uint32: typeof Uint32Array !== 'undefined' ? 1 : 0,
32-
float32: typeof Float32Array !== 'undefined' ? 1 : 0,
33-
float64: typeof Float64Array !== 'undefined' ? 1 : 0,
34-
bigint64: typeof BigInt64Array !== 'undefined' ? 1 : 0,
35-
biguint64: typeof BigUint64Array !== 'undefined' ? 1 : 0
36-
};
3725

3826
exports.valObjectMeta = {
3927
data_array: {
@@ -53,12 +41,9 @@ exports.valObjectMeta = {
5341
if(isArrayOrTypedArray(v)) {
5442
propOut.set(v);
5543
wasSet = true;
56-
} else if(isPlainObject(v)) {
57-
var T = typedArrays[v.dtype];
58-
if(T) {
59-
propOut.set(v);
60-
wasSet = true;
61-
}
44+
} else if(isTypedArraySpec(v)) {
45+
propOut.set(coerceTypedArraySpec(v));
46+
wasSet = true;
6247
}
6348
if(!wasSet && dflt !== undefined) propOut.set(dflt);
6449
}

src/plots/plots.js

+10-23
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
var d3 = require('d3');
1212
var timeFormatLocale = require('d3-time-format').timeFormatLocale;
1313
var isNumeric = require('fast-isnumeric');
14-
var b64 = require('base64-arraybuffer');
14+
var decodeTypedArraySpec = require('../lib/array').decodeTypedArraySpec;
15+
var isTypedArraySpec = require('../lib/array').isTypedArraySpec;
1516

1617
var Registry = require('../registry');
1718
var PlotSchema = require('../plot_api/plot_schema');
@@ -2849,40 +2850,26 @@ function _transition(gd, transitionOpts, opts) {
28492850
return transitionStarting.then(function() { return gd; });
28502851
}
28512852

2852-
var typedArrays = {
2853-
int8: typeof Int8Array !== 'undefined' ? Int8Array : null,
2854-
uint8: typeof Uint8Array !== 'undefined' ? Uint8Array : null,
2855-
uint8clamped: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : null,
2856-
int16: typeof Int16Array !== 'undefined' ? Int16Array : null,
2857-
uint16: typeof Uint16Array !== 'undefined' ? Uint16Array : null,
2858-
int32: typeof Int32Array !== 'undefined' ? Int32Array : null,
2859-
uint32: typeof Uint32Array !== 'undefined' ? Uint32Array : null,
2860-
float32: typeof Float32Array !== 'undefined' ? Float32Array : null,
2861-
float64: typeof Float64Array !== 'undefined' ? Float64Array : null,
2862-
bigint64: typeof BigInt64Array !== 'undefined' ? BigInt64Array : null,
2863-
biguint64: typeof BigUint64Array !== 'undefined' ? BigUint64Array : null
2864-
};
2865-
28662853
function _decode(cont) {
2867-
if(cont.dtype && cont.bvals) {
2868-
var T = typedArrays[cont.dtype];
2869-
if(T) {
2870-
return new T(b64.decode(cont.bvals));
2871-
}
2854+
if(isTypedArraySpec(cont)) {
2855+
return decodeTypedArraySpec(cont);
28722856
}
2873-
28742857
for(var prop in cont) {
28752858
if(prop[0] !== '_' && cont.hasOwnProperty(prop)) {
28762859
var item = cont[prop];
28772860
if(Lib.isPlainObject(item)) {
28782861
var r = _decode(item);
28792862
if(r !== undefined) cont[prop] = r;
2863+
} else if(Array.isArray(cont) && cont.length > 0 && Lib.isPlainObject(cont[0])) {
2864+
for(var i = 0; i < cont.length; i++) {
2865+
_decode(cont[i]);
2866+
}
28802867
}
28812868
}
28822869
}
28832870
}
28842871

2885-
function decodeB64Arrays(gd) {
2872+
function decodeTypedArrays(gd) {
28862873
for(var i = 0; i < gd._fullData.length; i++) {
28872874
_decode(gd._fullData[i]);
28882875
}
@@ -2891,7 +2878,7 @@ function decodeB64Arrays(gd) {
28912878
}
28922879

28932880
plots.doCalcdata = function(gd, traces) {
2894-
decodeB64Arrays(gd);
2881+
decodeTypedArrays(gd);
28952882

28962883
var axList = axisIDs.list(gd);
28972884
var fullData = gd._fullData;

src/traces/isosurface/defaults.js

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function supplyIsoDefaults(traceIn, traceOut, defaultColor, layout, coerce) {
3838
var z = coerce('z');
3939
var value = coerce('value');
4040

41+
console.log(["x", x]);
42+
4143
if(
4244
!x || !x.length ||
4345
!y || !y.length ||

0 commit comments

Comments
 (0)