Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color array when rgb can be transferred with binary transfer #16

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 13 additions & 9 deletions ipyvolume/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ def array_to_binary_or_json(ar, obj=None):
elif performance == 0:
return array_to_json(ar, obj=obj)
elif performance == 1:
ar = np.array(ar, dtype=np.float32) # this mode only support 'regular' arrays
#known_type = ["|u1", "|i1", "<u2", "<i2", "<u4", "<i4", "<f4", "<f8"]
#if ar.dtype in known_type and len(ar.shape) <= 2:
iobyte = StringIO()
np.save(iobyte, ar)
return iobyte.getvalue()
#else:
# return ar.tolist()
known_type = ["|u1", "|i1", "<u2", "<i2", "<u4", "<i4", "<f4", "<f8"]
if ar.dtype in known_type and len(ar.shape) <= 3:
#print(ar.flags)
#ar = np.array(ar, dtype=np.float32) # this mode only support 'regular' arrays
iobyte = StringIO()
if ar.flags["C_CONTIGUOUS"]:
np.save(iobyte, ar)
else:
np.save(iobyte, np.ascontiguousarray(ar)) #The copy is to ensure the C order
return iobyte.getvalue()
else:
return array_to_json(ar)
elif performance == 2:
if ar is not None:
#ar = ar.astype(np.float64)
Expand Down Expand Up @@ -134,4 +138,4 @@ def from_json_to_array(value, obj=None):
import sys
grid = np.load(sys.argv[1]).items()[0][1]
with open(sys.argv[2], "wb") as f:
cube_to_png(np.log10(grid+1), f)
cube_to_png(np.log10(grid+1), f)
8 changes: 4 additions & 4 deletions ipyvolume/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ class Scatter(widgets.DOMWidget):
vz = Array(default_value=None,allow_none=True).tag(sync=True, **create_array_binary_serialization('vz'))
selected = Array(default_value=None,allow_none=True).tag(sync=True, **array_serialization)
sequence_index = Integer(default_value=0).tag(sync=True)
size = traitlets.Union([traitlets.Float().tag(sync=True),
Array(default_value=None,allow_none=True).tag(sync=True, **array_serialization)],
size = traitlets.Union([Array(default_value=None,allow_none=True).tag(sync=True, **array_serialization),
traitlets.Float().tag(sync=True)],
default_value=0.1).tag(sync=True)
size_selected = traitlets.Float(0.02).tag(sync=True)
color = traitlets.Union([Unicode().tag(sync=True),
Array(default_value=None,allow_none=True).tag(sync=True, **array_serialization)],
color = traitlets.Union([Array(default_value=None,allow_none=True).tag(sync=True, **create_array_binary_serialization('color')),
Unicode().tag(sync=True)],
default_value="red").tag(sync=True)
color_selected = traitlets.Unicode(default_value="white").tag(sync=True)
geo = traitlets.Unicode('diamond').tag(sync=True)
Expand Down
59 changes: 40 additions & 19 deletions js/src/volume.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ function read_uint16_LE(buffer) {
}

function numpy_buffer_to_array(buf) {
console.log("l",buf.slice(1,6) )

var magic = ascii_decode(buf.slice(0,6));
if (magic.slice(1,6) != 'NUMPY') {
Expand Down Expand Up @@ -80,11 +79,23 @@ function numpy_buffer_to_array(buf) {
}

var shape = info.shape;
console.log(shape)
if (shape.length == 2) {
var ndata = new Array(shape[0])
for(var i = 0; i< shape[0]; i++){
ndata[i] = data.slice(i*shape[1],(i+1)*shape[1])
}

} else if (shape.length == 3){
var ndata = new Array(shape[0])
var count = 0;
for(var i = 0; i< shape[0]; i++){
ndata[i] = new Array(shape[1])
for(var j = 0; j< shape[1]; j++){
ndata[i][j] = data.slice(count,count+shape[2])
count +=shape[2]
}
}
} else {
var ndata = data
}
Expand All @@ -95,31 +106,39 @@ function numpy_buffer_to_array(buf) {
function binary_array_or_json(data, manager) {
if(data == null)
return null;

var arrays = null;
if(data && _.isArray(data) && !data.buffer) { // plain json, or list of buffers

if (data.buffer){
arrays = numpy_buffer_to_array(data.buffer)
arrays.original_data = data;
}

else if (data && !_.isArray(data)){
arrays = data //For color unicode
}
else if (data && _.isArray(data) && !data.buffer) { // plain json, or list of buffers
if(!data[0].buffer) {
// plain json
if(_.isArray(data[0])) {
arrays = _.map(data, function(array1d) { return new Float32Array(array1d)})
} else {
arrays = new Float32Array(data)
}
arrays = data //Anykind of data
} else {
arrays = _.map(data, function(data) { return new Float32Array(data.buffer)});
return buffer_list
arrays = _.map(data, function(array1d) { return new Float32Array(array1d)})
arrays.original_data = data;
}
} else {
arrays = numpy_buffer_to_array(data.buffer)
}
arrays.original_data = data;
}

return arrays;
}

function serialize_binary_array_or_json(obj, manager) {
if(obj)
return obj.original_data; // ftm we just feed back the original data, we don't modify currently
else
return null;
if(obj){
//here to avaid unecessary copy, if the obj is the original data we don't keep it
if (obj.original_data)
return obj.original_data; // ftm we just feed back the original data, we don't modify currently
else
return obj
}
else
return null;
}

function to_rgb(color) {
Expand Down Expand Up @@ -550,7 +569,7 @@ var ScatterView = widgets.WidgetView.extend( {
},
create_mesh: function() {
console.log("previous values: ")
console.log(this.previous_values)
//console.log(this.previous_values)
var geo = this.model.get("geo")
console.log(geo)
if(!geo)
Expand Down Expand Up @@ -1604,6 +1623,8 @@ var ScatterModel = widgets.WidgetModel.extend({
vx: { deserialize: binary_array_or_json, serialize: serialize_binary_array_or_json },
vy: { deserialize: binary_array_or_json, serialize: serialize_binary_array_or_json },
vz: { deserialize: binary_array_or_json, serialize: serialize_binary_array_or_json },
color: { deserialize: binary_array_or_json, serialize: serialize_binary_array_or_json },

}, widgets.WidgetModel.serializers)
});

Expand Down