| 1 |
import * as _ from 'underscore'; |
| 2 |
import isTypedArray from 'is-typedarray'; |
| 3 |
|
| 4 |
const typesToArray = { |
| 5 |
int8: Int8Array, |
| 6 |
int16: Int16Array, |
| 7 |
int32: Int32Array, |
| 8 |
uint8: Uint8Array, |
| 9 |
uint16: Uint16Array, |
| 10 |
uint32: Uint32Array, |
| 11 |
float32: Float32Array, |
| 12 |
float64: Float64Array, |
| 13 |
}; |
| 14 |
|
| 15 |
const arrayToTypes = { |
| 16 |
Int8Array: 'int8', |
| 17 |
Int16Array: 'int16', |
| 18 |
Int32Array: 'int32', |
| 19 |
Uint8Array: 'uint8', |
| 20 |
Uint16Array: 'uint16', |
| 21 |
Uint32Array: 'uint32', |
| 22 |
Float32Array: 'float32', |
| 23 |
Float64Array: 'float64', |
| 24 |
}; |
| 25 |
|
| 26 |
function deserialize_typed_array(data) { |
| 27 |
const type = typesToArray[data.dtype]; |
| 28 |
|
| 29 |
if (data == null || !data.value || !data.value.buffer) { |
| 30 |
throw new Error('Failed to deserialize data'); |
| 31 |
} |
| 32 |
|
| 33 |
const ar = new type(data.value.buffer); |
| 34 |
ar.type = data.type; |
| 35 |
if (data.shape && data.shape.length >= 2) { |
| 36 |
if (data.shape.length > 2) { |
| 37 |
throw new Error('only arrays with rank 1 or 2 supported'); |
| 38 |
} |
| 39 |
const arrays = []; |
| 40 |
// slice the 1d typed arrays in multiple arrays and put them in a |
| 41 |
// regular array |
| 42 |
for (let i = 0; i < data.shape[0]; i++) { |
| 43 |
arrays.push(ar.slice(i * data.shape[1], (i + 1) * data.shape[1])); |
| 44 |
} |
| 45 |
return arrays; |
| 46 |
} else { |
| 47 |
return ar; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
function serialize_typed_array(ar) { |
| 52 |
if (ar == null) { |
| 53 |
console.log('data is null'); |
| 54 |
} |
| 55 |
if (!ar.buffer) { |
| 56 |
console.log('ar.buffer is null or not defined'); |
| 57 |
} |
| 58 |
const dtype = arrayToTypes[ar.constructor.name]; |
| 59 |
const type = ar.type || null; |
| 60 |
const wire = { |
| 61 |
dtype: dtype, |
| 62 |
value: new DataView(ar.buffer), |
| 63 |
shape: [ar.length], |
| 64 |
type: type, |
| 65 |
}; |
| 66 |
return wire; |
| 67 |
} |
| 68 |
|
| 69 |
function deserialize_array_or_json(data, manager) { |
| 70 |
if (!_.isArray(data) && !_.isObject(data)) { |
| 71 |
return data; |
| 72 |
} |
| 73 |
|
| 74 |
if (_.isArray(data)) { |
| 75 |
return _.map(data, (subdata) => { |
| 76 |
return deserialize_array_or_json(subdata, manager); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
if (data.value && data.dtype) { |
| 81 |
return deserialize_typed_array(data); |
| 82 |
} |
| 83 |
|
| 84 |
throw new Error('Failed to deserialize data'); |
| 85 |
} |
| 86 |
|
| 87 |
function serialize_array_or_json(data, manager) { |
| 88 |
if (!_.isArray(data) && !_.isObject(data)) { |
| 89 |
return data; |
| 90 |
} |
| 91 |
|
| 92 |
if (_.isArray(data)) { |
| 93 |
return _.map(data, serialize_array_or_json); |
| 94 |
} |
| 95 |
|
| 96 |
if (isTypedArray(data)) { |
| 97 |
return serialize_typed_array(data); |
| 98 |
} |
| 99 |
|
| 100 |
throw new Error('Failed to serialize data'); |
| 101 |
} |
| 102 |
|
| 103 |
export const array_or_json_serializer = { |
| 104 |
deserialize: deserialize_array_or_json, |
| 105 |
serialize: serialize_array_or_json, |
| 106 |
}; |