Skip to content

Commit

Permalink
new: experimental binary transfer of numpy data, requires jupyter-wid…
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Mar 8, 2017
1 parent aa3c0c1 commit d008749
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 15 additions & 0 deletions ipyvolume/serialize.py
Expand Up @@ -6,6 +6,8 @@
from base64 import b64encode
import warnings

binary = False # set this to True for experimental binary transfer, requires:
# https://github.com/ipython/ipywidgets/pull/1194

def cube_to_png(grid, vmin, vmax, file):
image_width = 2048
Expand Down Expand Up @@ -76,12 +78,25 @@ def from_json(value, obj=None):
def array_to_json(ar, obj=None):
return ar.tolist() if ar is not None else None

def array_to_binary_or_json(ar, obj=None):
if binary:
if ar is not None:
ar = ar.astype(np.float64)
mv = memoryview(ar)
return {'data': mv, 'shape': ar.shape}
else:
return None
else:
return array_to_json(ar, obj=obj)


def from_json_to_array(value, obj=None):
return np.array(value) if value else None

array_cube_png_serialization = dict(to_json=cube_to_json, from_json=from_json)
array_rgba_png_serialization = dict(to_json=rgba_to_json, from_json=from_json)
array_serialization = dict(to_json=array_to_json, from_json=from_json_to_array)
array_binary_serialization = dict(to_json=array_to_binary_or_json, from_json=from_json_to_array)

if __name__ == "__main__":
import sys
Expand Down
22 changes: 21 additions & 1 deletion js/src/volume.js
Expand Up @@ -30,6 +30,17 @@ shaders["volr_vertex"] = require('../glsl/volr-vertex.glsl');
shaders["screen_fragment"] = require('../glsl/screen-fragment.glsl');
shaders["screen_vertex"] = require('../glsl/screen-vertex.glsl');

function binary_array_or_json(data, manager) {
if(data && data.data && data.data.buffer) {
console.log("binary array")
window.last_data = data
var ar = new Float64Array(data.data.buffer)
window.last_array = ar
return ar
} else {
return data; // we assume it was json data
}
}
function to_rgb(color) {
color = new THREE.Color(color)
return [color.r, color.g, color.b]
Expand Down Expand Up @@ -296,6 +307,7 @@ var ScatterView = widgets.WidgetView.extend( {
this.renderer = this.options.parent;
this.previous_values = {}
this.attributes_changed = {}
window.last_scatter = this;

console.log("create scatter")

Expand Down Expand Up @@ -1494,7 +1506,15 @@ var ScatterModel = widgets.WidgetModel.extend({
color_selected: "white",
geo: 'diamond'
})
}
}}, {
serializers: _.extend({
x: { deserialize: binary_array_or_json },
y: { deserialize: binary_array_or_json },
z: { deserialize: binary_array_or_json },
vx: { deserialize: binary_array_or_json },
vy: { deserialize: binary_array_or_json },
vz: { deserialize: binary_array_or_json },
}, widgets.WidgetModel.serializers)
});

var WidgetManagerHackModel = widgets.WidgetModel.extend({
Expand Down

0 comments on commit d008749

Please sign in to comment.