Skip to content
This repository has been archived by the owner on Apr 10, 2024. It is now read-only.

Commit

Permalink
Switch from ndarray to numjs.
Browse files Browse the repository at this point in the history
  • Loading branch information
arvind committed Jun 1, 2018
1 parent 1e866d1 commit 3ffc8cb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lucid/scratch/web/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"svelte": "^1.56.1",
"d3": "^5.1.0",
"d3-contour": "^1.2.0",
"ndarray": "^1.0.18"
"numjs": "^0.15.1"
}
}
13 changes: 6 additions & 7 deletions lucid/scratch/web/js_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def to_js(obj):

if hasattr(obj, "__js__"):
return obj.__js__()
elif isinstance(obj, np.ndarray):
return js_ndarray(obj, obj.dtype).__js__()
elif isinstance(obj, str):
return repr(obj)
elif isinstance(obj, (int, float)):
Expand Down Expand Up @@ -36,16 +38,13 @@ def js_ndarray(arr, dtype):
arr = np.asarray(arr, dtype)
b64arr = base64.b64encode(arr)
shape = list(arr.shape)
constructor_map = {
"float32" : "Float32Array"
}
constructor = constructor_map[dtype]

dtype = str(dtype)

code = "(function () {\n"
code += " var s = \"%s\";\n" % b64arr
code += " var buffer = Uint8Array.from(atob(s), c => c.charCodeAt(0)).buffer;\n"
code += " var typed_arr = new %s(buffer);\n" % constructor
code += " return ndarray(typed_arr, %s);\n" % shape
code += " var typed_arr = new %s(buffer);\n" % (dtype.capitalize() + "Array")
code += " return nj.array(typed_arr, '%s').reshape(%s);\n" % (dtype, shape)
code += "})()\n"
return JsExpr(code)

1 change: 1 addition & 0 deletions lucid/scratch/web/svelte.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


_template = """
<script src="https://cdn.jsdelivr.net/gh/nicolaspanel/numjs@0.15.1/dist/numjs.min.js"></script>
<div id='$div_id'></div>
<script>
$js
Expand Down

0 comments on commit 3ffc8cb

Please sign in to comment.