{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import k3d" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def get_plot():\n", " iteration = 4\n", " size = 3**iteration\n", "\n", " voxels = np.ones((size, size, size));\n", "\n", " def iterate(length, x, y, z):\n", "\n", " nl = length // 3 \n", "\n", " if nl < 1:\n", " return\n", "\n", " margin = (nl-1) // 2\n", "\n", " voxels[z-margin:z+margin+1, y-margin:y+margin+1, :] = 0\n", " voxels[z-margin:z+margin+1, :, x-margin:x+margin+1] = 0\n", " voxels[:, y-margin:y+margin+1, x-margin:x+margin+1] = 0 \n", "\n", " for ix,iy,iz in np.ndindex((3,3,3)):\n", " if (1 if ix !=1 else 0) + (1 if iy != 1 else 0) + (1 if iz != 1 else 0) !=2:\n", " iterate(nl, x + (ix-1) * nl, y + (iy-1) * nl , z + (iz-1) * nl)\n", "\n", " iterate(size, size//2, size//2, size//2)\n", "\n", " plot = k3d.plot()\n", " plot += k3d.voxels(voxels.astype(np.uint8), compression_level=9)\n", " \n", " return plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create and save to *.k3d file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = get_plot()\n", "plot.display()\n", "data = plot.get_binary_snapshot()\n", "\n", "with open('binary_snapshot.k3d', 'wb') as f:\n", " f.write(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load from *.k3d file" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot2 = k3d.plot()\n", "with open('binary_snapshot.k3d', 'rb') as f:\n", " plot2.load_binary_snapshot(f.read())\n", "plot2.display()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Save to standalone *.html file\n", "\n", "Standalone snapshot produce HTML file that include any necessary data to draw plot with data without internet connection/" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot3 = get_plot()\n", "plot3.display()\n", "\n", "data = plot3.get_snapshot()\n", "\n", "with open('snapshot_standalone.html', 'w') as f:\n", " f.write(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Save to online *.html file\n", "\n", "Online snapshot exclude whole js code of k3d. Produced HTML file contain a minimum code to download a K3D from NPM and display a plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot4 = get_plot()\n", "plot4.snapshot_type = 'online'\n", "plot4.display()\n", "\n", "data = plot4.get_snapshot()\n", "\n", "with open('snapshot_online.html', 'w') as f:\n", " f.write(data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Save to inline *.html file\n", "\n", "Inline element can be embeded in any html document like Sphinx documentation:\n", "\n", "```\n", ".. raw:: html\n", "\t:file: inline_shapshot.html\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot5 = get_plot()\n", "plot5.snapshot_type = 'inline'\n", "plot5.display()\n", "\n", "data = plot5.get_snapshot()\n", "\n", "with open('snapshot_inline.html', 'w') as f:\n", " f.write(data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "for f in ['snapshot_standalone.html', 'snapshot_online.html', 'snapshot_inline.html', 'binary_snapshot.k3d']:\n", " print(f, os.stat(f).st_size, 'bytes')" ] }, { "cell_type": "code", "execution_count": null, "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.8.12" } }, "nbformat": 4, "nbformat_minor": 4 }