<commit_before>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import numpy as np\n",
    "import numpy.random as rd\n",
    "from IPython.display import HTML, Javascript, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def json_numpy_serializer(o):\n",
    "    if isinstance(o, np.ndarray):\n",
    "        return o.tolist()\n",
    "    raise TypeError(\"{} of type {} is not JSON serializable\".format(repr(o), type(o)))\n",
    "\n",
    "def jsglobal(**params):\n",
    "    code = [];\n",
    "    for name, value in params.items():\n",
    "        jsdata = json.dumps(value, default=json_numpy_serializer)\n",
    "        code.append(\"window.{}={};\".format(name, jsdata))\n",
    "    display(Javascript(\"\\n\".join(code)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from covertree.covertree import CoverTree\n",
    "\n",
    "def l2(p, q):\n",
    "    return np.sqrt((p-q) @ (p-q))\n",
    "\n",
    "def extract_node_data(n0, x, y, z, levels, links):\n",
    "    for k in n0.children.keys():\n",
    "        for n in n0.children[k]:\n",
    "            x.append(n.data[0])\n",
    "            y.append(n.data[1])\n",
    "            z.append(n.data[2])\n",
    "            levels.append(k)\n",
    "            links.append(n0.data)\n",
    "            links.append(n.data)\n",
    "            extract_node_data(n, x, y, z, levels, links)\n",
    "\n",
    "num_pts = 1000\n",
    "x = np.reshape(rd.randn(3*num_pts), (num_pts, 3))\n",
    "x = np.apply_along_axis(lambda v: v / np.sqrt(np.sum(v**2)), 1, x)\n",
    "cube_ct = CoverTree(l2)\n",
    "for i in range(num_pts):\n",
    "    cube_ct.insert(x[i, :])\n",
    "    \n",
    "x = []\n",
    "y = []\n",
    "z = []\n",
    "r = {}\n",
    "levels = []\n",
    "links = []\n",
    "\n",
    "x.append(cube_ct.root.data[0])\n",
    "y.append(cube_ct.root.data[1])\n",
    "z.append(cube_ct.root.data[2])\n",
    "levels.append(cube_ct.maxlevel)\n",
    "r['min'] = cube_ct.minlevel\n",
    "r['max'] = cube_ct.maxlevel\n",
    "\n",
    "extract_node_data(cube_ct.root, x, y, z, levels, links)\n",
    "\n",
    "pos = np.vstack([x, y, z]).T\n",
    "        \n",
    "jsglobal(POS=pos)\n",
    "jsglobal(LINKS=links)\n",
    "jsglobal(R=r)\n",
    "jsglobal(LEVELS=levels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%javascript\n",
    "\n",
    "// window.location.reload(true)\n",
    "\n",
    "// Loading the compiled MathBox bundle.\n",
    "require.config({\n",
    "    urlArgs: \"\", // Make suffix empty\n",
    "    paths: {\n",
    "        mathBox: '../static/mathbox/build/mathbox-bundle'\n",
    "    }\n",
    "});\n",
    "\n",
    "// Helper function that setups WebGL context and initializes MathBox.\n",
    "window.with_mathbox = function(element, func) {\n",
    "    require(['mathBox'], function(){\n",
    "        var mathbox = mathBox({\n",
    "          plugins: ['core', 'controls', 'cursor', 'mathbox'],\n",
    "          controls: { klass: THREE.OrbitControls },\n",
    "          mathbox: {inspect: false},\n",
    "          element: element[0],\n",
    "          loop: {start: false},\n",
    "            \n",
    "        });\n",
    "        var three = mathbox.three;\n",
    "        three.renderer.setClearColor(new THREE.Color(0xFFFFFF), 1.0);\n",
    "        three.camera.position.set(-1, 1, 2);\n",
    "        three.controls.noKeys = true;\n",
    "        \n",
    "        three.element.style.height = \"400px\";\n",
    "        three.element.style.width = \"100%\";\n",
    "        \n",
    "        function isInViewport(element) {\n",
    "          var rect = element.getBoundingClientRect();\n",
    "          var html = document.documentElement;\n",
    "          var w = window.innerWidth || html.clientWidth;\n",
    "          var h = window.innerHeight || html.clientHeight;\n",
    "          return rect.top < h && rect.left < w && rect.bottom > 0 && rect.right > 0;\n",
    "        }\n",
    "        \n",
    "        // Running update/render loop only for visible plots.\n",
    "        var intervalId = setInterval(function(){\n",
    "            if (three.element.offsetParent === null) {\n",
    "                clearInterval(intervalId);\n",
    "                three.destroy();\n",
    "                return;\n",
    "            }\n",
    "            var visible = isInViewport(three.canvas);\n",
    "            if (three.Loop.running != visible) {\n",
    "                visible? three.Loop.start() : three.Loop.stop();\n",
    "            }\n",
    "        }, 100);\n",
    "\n",
    "        func(mathbox);\n",
    "        \n",
    "        window.dispatchEvent(new Event('resize'));\n",
    "    })\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%javascript\n",
    "with_mathbox(element, function(mathbox) {\n",
    "    \n",
    "    var view = mathbox.cartesian({},{rotation:(t)=>[0, t*0.02, 0]})\n",
    "      .grid({axes: [1, 3]})\n",
    "    \n",
    "    view.array({\n",
    "        width: LEVELS.length,\n",
    "      expr: function (emit, i, time) {\n",
    "        if ((time % (R['max'] - R['min'] + 10))> R['max']-LEVELS[i])\n",
    "            emit(POS[i][0], POS[i][1], POS[i][2]);\n",
    "      },\n",
    "      channels: 3\n",
    "    });\n",
    "        \n",
    "    // Now we can see the data on JS side!\n",
    "    view.point({color:\"#55a\", size: 4});\n",
    "    \n",
    "    //view.array({width: LINKS.length/2, items: 2, channels: 3, data: LINKS, live: false}).vector({color: 0x4444ff, width: 1});\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}<commit_msg>"Updated jupyter notebook."<commit_after>{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import numpy as np\n",
    "import numpy.random as rd\n",
    "from IPython.display import HTML, Javascript, display"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def json_numpy_serializer(o):\n",
    "    if isinstance(o, np.ndarray):\n",
    "        return o.tolist()\n",
    "    raise TypeError(\"{} of type {} is not JSON serializable\".format(repr(o), type(o)))\n",
    "\n",
    "def jsglobal(**params):\n",
    "    code = [];\n",
    "    for name, value in params.items():\n",
    "        jsdata = json.dumps(value, default=json_numpy_serializer)\n",
    "        code.append(\"window.{}={};\".format(name, jsdata))\n",
    "    display(Javascript(\"\\n\".join(code)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from covertree.covertree import CoverTree\n",
    "\n",
    "def l2(p, q):\n",
    "    return np.sqrt((p-q) @ (p-q))\n",
    "\n",
    "def extract_node_data(n0, x, y, z, levels, links):\n",
    "    for k in n0.children.keys():\n",
    "        for n in n0.children[k]:\n",
    "            x.append(n.data[0])\n",
    "            y.append(n.data[1])\n",
    "            z.append(n.data[2])\n",
    "            levels.append(k)\n",
    "            links.append(n0.data)\n",
    "            links.append(n.data)\n",
    "            extract_node_data(n, x, y, z, levels, links)\n",
    "\n",
    "num_pts = 1000\n",
    "x = np.reshape(rd.randn(3*num_pts), (num_pts, 3))\n",
    "x = np.apply_along_axis(lambda v: v / np.sqrt(np.sum(v**2)), 1, x)\n",
    "cube_ct = CoverTree(l2)\n",
    "for i in range(num_pts):\n",
    "    cube_ct.insert(x[i, :])\n",
    "    \n",
    "x = []\n",
    "y = []\n",
    "z = []\n",
    "r = {}\n",
    "levels = []\n",
    "links = []\n",
    "\n",
    "x.append(cube_ct.root.data[0])\n",
    "y.append(cube_ct.root.data[1])\n",
    "z.append(cube_ct.root.data[2])\n",
    "levels.append(cube_ct.maxlevel)\n",
    "r['min'] = cube_ct.minlevel\n",
    "r['max'] = cube_ct.maxlevel\n",
    "\n",
    "extract_node_data(cube_ct.root, x, y, z, levels, links)\n",
    "\n",
    "pos = np.vstack([x, y, z]).T\n",
    "        \n",
    "jsglobal(POS=pos)\n",
    "jsglobal(LINKS=links)\n",
    "jsglobal(R=r)\n",
    "jsglobal(LEVELS=levels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%javascript\n",
    "\n",
    "// window.location.reload(true)\n",
    "\n",
    "// Loading the compiled MathBox bundle.\n",
    "require.config({\n",
    "    urlArgs: \"\", // Make suffix empty\n",
    "    paths: {\n",
    "        mathBox: '../master/static/mathbox/build/mathbox-bundle'\n",
    "    }\n",
    "});\n",
    "\n",
    "// Helper function that setups WebGL context and initializes MathBox.\n",
    "window.with_mathbox = function(element, func) {\n",
    "    require(['mathBox'], function(){\n",
    "        var mathbox = mathBox({\n",
    "          plugins: ['core', 'controls', 'cursor', 'mathbox'],\n",
    "          controls: { klass: THREE.OrbitControls },\n",
    "          mathbox: {inspect: false},\n",
    "          element: element[0],\n",
    "          loop: {start: false},\n",
    "            \n",
    "        });\n",
    "        var three = mathbox.three;\n",
    "        three.renderer.setClearColor(new THREE.Color(0xFFFFFF), 1.0);\n",
    "        three.camera.position.set(-1, 1, 2);\n",
    "        three.controls.noKeys = true;\n",
    "        \n",
    "        three.element.style.height = \"400px\";\n",
    "        three.element.style.width = \"100%\";\n",
    "        \n",
    "        function isInViewport(element) {\n",
    "          var rect = element.getBoundingClientRect();\n",
    "          var html = document.documentElement;\n",
    "          var w = window.innerWidth || html.clientWidth;\n",
    "          var h = window.innerHeight || html.clientHeight;\n",
    "          return rect.top < h && rect.left < w && rect.bottom > 0 && rect.right > 0;\n",
    "        }\n",
    "        \n",
    "        // Running update/render loop only for visible plots.\n",
    "        var intervalId = setInterval(function(){\n",
    "            if (three.element.offsetParent === null) {\n",
    "                clearInterval(intervalId);\n",
    "                three.destroy();\n",
    "                return;\n",
    "            }\n",
    "            var visible = isInViewport(three.canvas);\n",
    "            if (three.Loop.running != visible) {\n",
    "                visible? three.Loop.start() : three.Loop.stop();\n",
    "            }\n",
    "        }, 100);\n",
    "\n",
    "        func(mathbox);\n",
    "        \n",
    "        window.dispatchEvent(new Event('resize'));\n",
    "    })\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%javascript\n",
    "with_mathbox(element, function(mathbox) {\n",
    "    \n",
    "    var view = mathbox.cartesian({},{rotation:(t)=>[0, t*0.02, 0]})\n",
    "      .grid({axes: [1, 3]})\n",
    "    \n",
    "    view.array({\n",
    "        width: LEVELS.length,\n",
    "      expr: function (emit, i, time) {\n",
    "        if ((time % (R['max'] - R['min'] + 10))> R['max']-LEVELS[i])\n",
    "            emit(POS[i][0], POS[i][1], POS[i][2]);\n",
    "      },\n",
    "      channels: 3\n",
    "    });\n",
    "        \n",
    "    // Now we can see the data on JS side!\n",
    "    view.point({color:\"#55a\", size: 4});\n",
    "    \n",
    "    //view.array({width: LINKS.length/2, items: 2, channels: 3, data: LINKS, live: false}).vector({color: 0x4444ff, width: 1});\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    ""
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda root]",
   "language": "python",
   "name": "conda-root-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
{'fraction_non_alphanumeric': 0.22143637883366554, 'fraction_numerical': 0.009773034141323217, 'mean_word_length': 1.8819714656290532, 'pattern_counts': {'":': 158, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3479007', 'n_tokens_mistral': 5993, 'n_tokens_neox': 5811, 'n_words': 964}