Skip to content

Commit

Permalink
Objects empty #385
Browse files Browse the repository at this point in the history
Experimental support for colab
  • Loading branch information
artur-trzesiok committed May 22, 2023
1 parent d8a25e7 commit dc76f53
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 8 deletions.
110 changes: 110 additions & 0 deletions examples/force_text_websocket_protocol_COLAB.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b2d14edb",
"metadata": {},
"outputs": [],
"source": [
"import k3d"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c4359de5",
"metadata": {},
"outputs": [],
"source": [
"k3d.switch_to_text_protocol()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1197bd62",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'text'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"k3d._protocol.get_protocol()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7c464a5a",
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c9b7fdf9d1cc4321bd008e7cfb7cab92",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Output()"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import numpy as np\n",
"\n",
"points_number = 500\n",
"positions = 50 * np.random.random_sample((points_number,3)) - 25\n",
"colors = np.random.randint(0, 0xFFFFFF, points_number)\n",
"\n",
"plot = k3d.plot()\n",
"points = k3d.points(positions.astype(np.float32), colors.astype(np.uint32), point_size=3.0, shader='flat')\n",
"plot += points\n",
"plot.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a79645ee",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
29 changes: 23 additions & 6 deletions js/src/core/lib/helpers/serialize.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fflate = require('fflate');
const _ = require('../../../lodash');
const Float16Array = require('./float16Array');
const msgpack = require('msgpack-lite');
const buffer = require('./buffer');

const typesToArray = {
int8: Int8Array,
Expand All @@ -13,13 +15,17 @@ const typesToArray = {
float32: Float32Array,
float64: Float64Array,
};
const MsgpackCodec = msgpack.createCodec({ preset: true });
MsgpackCodec.addExtPacker(0x20, Float16Array, (val) => val);
MsgpackCodec.addExtUnpacker(0x20, (val) => Float16Array(val.buffer));


function isNumeric(n) {
return !Number.isNaN(parseFloat(n)) && Number.isFinite(parseFloat(n));
}

function deserializeArray(obj) {
let buffer;
let data;

if (typeof (obj.data) !== 'undefined') {
return {
Expand All @@ -28,13 +34,13 @@ function deserializeArray(obj) {
};
}
if (typeof (obj.compressed_data) !== 'undefined') {
buffer = new typesToArray[obj.dtype](fflate.unzlibSync(new Uint8Array(obj.compressed_data.buffer)).buffer);
data = new typesToArray[obj.dtype](fflate.unzlibSync(new Uint8Array(obj.compressed_data.buffer)).buffer);

console.log(`K3D: Receive: ${buffer.byteLength} bytes compressed to ${
console.log(`K3D: Receive: ${data.byteLength} bytes compressed to ${
obj.compressed_data.byteLength} bytes`);

return {
data: buffer,
data: data,
shape: obj.shape,
};
}
Expand All @@ -45,7 +51,7 @@ function serializeArray(obj) {
if (obj.compression_level && obj.compression_level > 0) {
return {
dtype: _.invert(typesToArray)[obj.data.constructor],
compressed_data: fflate.zlibSync(obj.data.buffer, {level: obj.compression_level}),
compressed_data: fflate.zlibSync(obj.data.buffer, { level: obj.compression_level }),
shape: obj.shape,
};
}
Expand All @@ -60,6 +66,9 @@ function deserialize(obj, manager) {
if (obj == null) {
return null;
}
if (typeof (obj) === 'string' && obj.substring(0, 7) === 'base64_') {
obj = msgpack.decode(buffer.base64ToArrayBuffer(obj.substring(7)), { codec: MsgpackCodec });
}
if (typeof (obj) === 'string' || typeof (obj) === 'boolean') {
return obj;
}
Expand Down Expand Up @@ -96,7 +105,7 @@ function deserialize(obj, manager) {
return deserializedObj;
}

function serialize(obj) {
function serialize_helper(obj) {
if (_.isNumber(obj)) {
return obj;
}
Expand Down Expand Up @@ -126,6 +135,14 @@ function serialize(obj) {
return null;
}

function serialize(obj) {
let data = serialize_helper(obj);

// TODO: convert to base64 if necessary

return data;
}

module.exports = {
deserialize,
serialize,
Expand Down
1 change: 1 addition & 0 deletions k3d/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from pathlib import Path

from ._protocol import switch_to_binary_protocol, switch_to_text_protocol
from ._version import __version__
from .colormaps import basic_color_maps
from .colormaps import matplotlib_color_maps
Expand Down
21 changes: 21 additions & 0 deletions k3d/_protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
__all__ = ["get_protocol", "switch_to_text_protocol", "switch_to_binary_protocol"]

_protocol = 'binary'


def switch_to_text_protocol():
global _protocol

_protocol = 'text'


def switch_to_binary_protocol():
global _protocol

_protocol = 'binary'


def get_protocol():
global _protocol

return _protocol
16 changes: 14 additions & 2 deletions k3d/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""Utilities module."""
import base64
import itertools
import msgpack
import numpy as np
import os
import zlib
from traitlets import TraitError
from urllib.request import urlopen

from ._protocol import get_protocol


# import logging
# from pprint import pprint, pformat
Expand Down Expand Up @@ -54,18 +58,23 @@ def array_to_json(ar, compression_level=0, force_contiguous=True):
ar = np.ascontiguousarray(ar)

if compression_level > 0:
return {
ret = {
"compressed_data": zlib.compress(ar.flatten(), compression_level),
"dtype": str(ar.dtype),
"shape": ar.shape,
}
else:
return {
ret = {
"data": memoryview(ar.flatten()),
"dtype": str(ar.dtype),
"shape": ar.shape,
}

if get_protocol() == 'text':
return 'base64_' + base64.b64encode(msgpack.packb(ret, use_bin_type=True)).decode('ascii')
else:
return ret


# noinspection PyUnusedLocal
def json_to_array(value, obj=None):
Expand Down Expand Up @@ -124,6 +133,9 @@ def to_json(name, input, obj=None, compression_level=0):

def from_json(input, obj=None):
"""Return JSON object deserialization."""
if isinstance(input, str) and input[0:7] == 'base64_':
input = msgpack.unpackb(base64.b64decode(input[7:]))

if isinstance(input, dict) \
and "dtype" in input \
and ("data" in input or "compressed_data" in input) \
Expand Down

0 comments on commit dc76f53

Please sign in to comment.