-
Notifications
You must be signed in to change notification settings - Fork 233
/
serialize.ts
255 lines (234 loc) · 8.7 KB
/
serialize.ts
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
import * as widgets from "@jupyter-widgets/base";
import { isArray, isNumber } from "lodash";
import ndarray from "ndarray";
import * as THREE from "three";
import * as utils from "./utils";
function ascii_decode(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
function read_uint16_LE(buffer) {
const view = new DataView(buffer);
let val = view.getUint8(0);
// tslint:disable-next-line: no-bitwise
val |= view.getUint8(1) << 8;
return val;
}
function numpy_buffer_to_ndarray(buf) {
const magic = ascii_decode(buf.slice(0, 6));
if (magic.slice(1, 6) !== "NUMPY") {
throw new Error("unknown file type");
}
const version = new Uint8Array(buf.slice(6, 8));
const headerLength = read_uint16_LE(buf.slice(8, 10));
const headerStr = ascii_decode(buf.slice(10, 10 + headerLength));
const offsetBytes = 10 + headerLength;
// rest = buf.slice(10+headerLength); XXX -- This makes a copy!!! https://www.khronos.org/registry/typedarray/specs/latest/#5
const info = JSON.parse(headerStr.toLowerCase().replace("(", "[").replace(",),", "]").replace("),", "]").replace(/'/g, "\""));
// Intepret the bytes according to the specified dtype
let data;
if (info.descr === "|u1") {
data = new Uint8Array(buf, offsetBytes);
} else if (info.descr === "|i1") {
data = new Int8Array(buf, offsetBytes);
} else if (info.descr === "<u2") {
data = new Uint16Array(buf, offsetBytes);
} else if (info.descr === "<i2") {
data = new Int16Array(buf, offsetBytes);
} else if (info.descr === "<u4") {
data = new Uint32Array(buf, offsetBytes);
} else if (info.descr === "<i4") {
data = new Int32Array(buf, offsetBytes);
} else if (info.descr === "<f4") {
data = new Float32Array(buf, offsetBytes);
} else if (info.descr === "<f8") {
data = new Float64Array(buf, offsetBytes);
} else {
throw new Error("unknown numeric dtype");
}
return ndarray(data, info.shape);
}
// see https://github.com/jovyan/pythreejs/pull/80/files
// should go to a seperate package/module
const typesToArray = {
int8: Int8Array,
int16: Int16Array,
int32: Int32Array,
uint8: Uint8Array,
uint16: Uint16Array,
uint32: Uint32Array,
float32: Float32Array,
float64: Float64Array,
};
const arrayToTypes = {
Int8Array: "int8",
Int16Array: "int16",
Int32Array: "int32",
Uint8Array: "uint8",
Uint16Array: "uint16",
Uint32Array: "uint32",
Float32Array: "float32",
Float64Array: "float64",
};
function deserialize_typed_array(data, manager) {
const type = typesToArray[data.dtype];
if (data == null) {
console.log("data is null");
}
if (!data.data) {
console.log("data.data is null");
}
if (!data.data.buffer) {
console.log("data.data.buffer is null");
}
return new type(data.data.buffer); //
}
export
function deserialize_array_or_json(data, manager) {
if (data == null) {
return null;
}
let arrays = null;
if (isNumber(data)) { // plain number
return data;
} else { // should be an array of buffer+dtype+shape
arrays = data.map((element) => deserialize_typed_array(element, manager));
}
arrays.original_data = data;
return arrays;
/*
if(_.isArray(data) && !data.buffer) { // plain json, or list of buffers
if(data.length == 0) {
arrays = []
} else
if(!data[0].buffer) {
// plain json
if(_.isArray(data[0])) {
arrays = _.map(data, function(array1d) { return new Float32Array(array1d)})
} else {
arrays = [new Float32Array(data)]
}
} else {
arrays = _.map(data, function(data) { return new Float32Array(data.buffer)});
}
} else {
nd = numpy_buffer_to_ndarray(data.buffer)
if (nd.shape.length == 2) {
arrays = []
for(var i = 0; i < nd.shape[0]; i++) {
var ar = nd.data.slice(i*nd.stride[0], (i+1)*nd.stride[0])
arrays.push(ar)
}
} else { // assume 1d... ?
arrays = [nd.data];
}
}
arrays.original_data = data;
return arrays;*/
}
export
function deserialize_color_or_json(data, manager) {
if (data == null) {
return null;
}
let arrays = null;
// It can deals with these 6 cases: which are threated in the same order
// shape is 0 dim, and it's a string, interpret as color
// shape is 1 dim, items are strings, seperate color for each item
// shape is 2 dim, items are strings, sequence of the above
// shape is 1 dim, items are floats, it should be of length 3 -> rgb values
// shape is 2 dim, items are float, it should be of shape (len(x), 3) -> rgb values
// shape is 3 dim, items are float, it should be (sequence_length, len(x), 3) -> rgb values
function string_array_to_rgb(string_array) {
const rgbs = new Float32Array(string_array.length * 3);
for (let i = 0; i < string_array.length; i++) {
const color = new THREE.Color(string_array[i]);
rgbs[i * 3 + 0] = color.r;
rgbs[i * 3 + 1] = color.g;
rgbs[i * 3 + 2] = color.b;
}
return rgbs;
}
function rgb_array_to_rgb(rgb_array) {
const rgbs = new Float32Array(rgb_array.length * 3);
for (let i = 0; i < rgb_array.length; i++) {
rgbs[i * 3 + 0] = rgb_array[i][0];
rgbs[i * 3 + 1] = rgb_array[i][1];
rgbs[i * 3 + 2] = rgb_array[i][2];
}
return rgbs;
}
if (typeof data === "string") { // single color
// var color = new THREE.Color(data)
// arrays = new Float32Array([color.r, color.g, color.b]) // no sequence, scalar
return data; // special case, if we keep it a string, we can control it via colorppicker
} else {
if (typeof data.dtype !== "undefined") { // we have a list of ndarrays
arrays = [deserialize_typed_array(data, manager)];
} else if (typeof data[0].dtype !== "undefined") { // we have a list of ndarrays
arrays = data.map((element) => deserialize_typed_array(element, manager));
} else {
const dimension = utils.get_array_dimension(data);
// must be a plain list of string, or list of list
if (dimension === 1 && typeof data[0] === "string") {
arrays = string_array_to_rgb(data);
} else
if (dimension === 2 && typeof data[0][0] === "string") {
arrays = data.map(string_array_to_rgb);
} else {
console.error("don't understand color type");
}
}
}
arrays.original_data = data;
return arrays;
}
export
function serialize_array_or_json(obj, manager) {
if (isNumber(obj)) { return obj; } // return numbers directly
if (obj != null) {
if (typeof obj.original_data === "undefined") { // if someone modified the data from javascript land, we don't have this
return obj;
} else {
return obj.original_data;
} // ftm we just feed back the original data, we don't modify currently
} else {
return null;
}
}
function deserialize_texture(data, manager) {
if (typeof data === "string") {
if (data.startsWith("IPY_MODEL_")) {
return widgets.unpack_models(data, manager);
}
}
return data;
}
function deserialize_ndarray(data, manager) {
if (data === null) {
return null;
}
return ndarray(deserialize_typed_array(data, manager), data.shape);
}
function serialize_ndarray(data, manager) {
if (data === null) {
return null;
}
let ar = data;
if (isArray(data) && !(data as any).data) { // plain list of list
ar = require("ndarray-pack")(data);
}
const data_json = { data: ar.data.buffer, dtype: arrayToTypes[ar.data.constructor.name], shape: ar.shape };
return data_json;
}
function serialize_texture(data, manager) {
return data;
}
// (window as any).ndarray = ndarray;
export const texture = { deserialize: deserialize_texture, serialize: serialize_texture };
// export const serialize_array_or_json = serialize_array_or_json;
// export const deserialize_array_or_json = deserialize_array_or_json;
// export const deserialize_color_or_json = deserialize_color_or_json;
export const array_or_json = { deserialize: deserialize_array_or_json, serialize: serialize_array_or_json };
export const color_or_json = { deserialize: deserialize_color_or_json, serialize: serialize_array_or_json };
const _ndarray = {deserialize: deserialize_ndarray, serialize: serialize_ndarray };
export {_ndarray as ndarray};