Created
March 23, 2016 21:35
-
-
Save ElDeveloper/b218ad5b869c2cc91e8e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "# Emperor's Python API\n", | |
| "\n", | |
| "**This notebook demonstrate Emperor's new Python API, which can and will change as we continue to exercise this interface, for more information, have a look at the [pull request here](https://github.com/biocore/emperor/pull/452).**" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import pandas as pd, numpy as np\n", | |
| "from emperor import Emperor\n", | |
| "from skbio.stats.ordination import OrdinationResults" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Ordination plots are almost invariantly associated with a set of data, that\n", | |
| " relates each sample to its scientific context, we refer to this as the\n", | |
| " *sample metadata*, and represent it using Pandas `DataFrame`s. For this\n", | |
| " example we will need some metadata, we start by creating our metadata\n", | |
| " object:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "data = [['PC.354', 'Control', '20061218', 'Control_mouse_I.D._354'],\n", | |
| "['PC.355', 'Control', '20061218', 'Control_mouse_I.D._355'],\n", | |
| "['PC.356', 'Control', '20061126', 'Control_mouse_I.D._356'],\n", | |
| "['PC.481', 'Control', '20070314', 'Control_mouse_I.D._481'],\n", | |
| "['PC.593', 'Control', '20071210', 'Control_mouse_I.D._593'],\n", | |
| "['PC.607', 'Fast', '20071112', 'Fasting_mouse_I.D._607'],\n", | |
| "['PC.634', 'Fast', '20080116', 'Fasting_mouse_I.D._634'],\n", | |
| "['PC.635', 'Fast', '20080116', 'Fasting_mouse_I.D._635'],\n", | |
| "['PC.636', 'Fast', '20080116', 'Fasting_mouse_I.D._636']]\n", | |
| "columns = ['SampleID', 'Treatment', 'DOB', 'Description']\n", | |
| "mf = pd.DataFrame(columns=columns, data=data)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Before we can use this mapping file in Emperor, we should set the index\n", | |
| " to be `SampleID`." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "mf.set_index('SampleID', inplace=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Then let's create some artificial ordination data, and encapsulate it in an `OrdinationResults` object:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "eigvals = np.array([0.47941212, 0.29201496, 0.24744925,\n", | |
| " 0.20149607, 0.18007613, 0.14780677,\n", | |
| " 0.13579593, 0.1122597, 0.])\n", | |
| "n = eigvals.shape[0]\n", | |
| "site = np.random.randn(n, n)\n", | |
| "site_ids = ('PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593',\n", | |
| " 'PC.355', 'PC.607', 'PC.634')\n", | |
| "p_explained = np.array([0.26688705, 0.1625637, 0.13775413, 0.11217216,\n", | |
| " 0.10024775, 0.08228351, 0.07559712, 0.06249458,\n", | |
| " 0.])\n", | |
| "ores = OrdinationResults(eigvals, site=site, site_ids=site_ids,\n", | |
| " proportion_explained=p_explained)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Finally import the Emperor object and display it using Jupyter:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<script type=\"text/javascript\">\n", | |
| "\n", | |
| "if ($(\"#emperor-css\").length == 0){{\n", | |
| " $(\"head\").append([\n", | |
| "\n", | |
| " '<link id=\"emperor-css\" rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/css/emperor.css\">',\n", | |
| " '<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/css/jquery-ui.min.css\">',\n", | |
| " '<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/css/slick.grid.min.css\">',\n", | |
| " '<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/css/spectrum.min.css\">',\n", | |
| " '<link rel=\"stylesheet\" type=\"text/css\" href=\"https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/css/chosen.min.css\">'\n", | |
| " ]);\n", | |
| "}}\n", | |
| "</script>\n", | |
| "<div id='python-penguin' style=\"position: relative; width:100%; height:500px;\"></div>\n", | |
| "</div><script type=\"text/javascript\">\n", | |
| "requirejs.config({\n", | |
| "'baseUrl': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/',\n", | |
| "\n", | |
| "// the left side is the module name, and the right side is the path\n", | |
| "// relative to the baseUrl attribute, do NOT include the .js extension\n", | |
| "'paths': {\n", | |
| " /* jQuery */\n", | |
| " 'jquery': './vendor/js/jquery-2.1.4.min',\n", | |
| " 'jqueryui': './vendor/js/jquery-ui.min',\n", | |
| " 'jquery_drag': './vendor/js/jquery.event.drag-2.2.min',\n", | |
| "\n", | |
| " /* jQuery plugins */\n", | |
| " 'chosen': './vendor/js/chosen.jquery.min',\n", | |
| " 'spectrum': './vendor/js/spectrum.min',\n", | |
| "\n", | |
| " /* other libraries */\n", | |
| " 'underscore': './vendor/js/underscore-min',\n", | |
| " 'chroma': './vendor/js/chroma.min',\n", | |
| "\n", | |
| "\n", | |
| " /* THREE.js and plugins */\n", | |
| " 'three': './vendor/js/three.min',\n", | |
| " 'orbitcontrols': './vendor/js/three.js-plugins/OrbitControls',\n", | |
| "\n", | |
| " /* SlickGrid */\n", | |
| " 'slickcore': './vendor/js/slick.core.min',\n", | |
| " 'slickgrid': './vendor/js/slick.grid.min',\n", | |
| " 'slickformatters': './vendor/js/slick.editors.min',\n", | |
| " 'slickeditors': './vendor/js/slick.formatters.min',\n", | |
| "\n", | |
| " /* Emperor's objects */\n", | |
| " 'model': './js/model',\n", | |
| " 'view': './js/view',\n", | |
| " 'controller': './js/controller',\n", | |
| " 'draw': './js/draw',\n", | |
| " 'scene3d': './js/sceneplotview3d',\n", | |
| " 'viewcontroller': './js/view-controller',\n", | |
| " 'colorviewcontroller': './js/color-view-controller',\n", | |
| " 'visibilitycontroller': './js/visibility-controller',\n", | |
| " 'color-editor': './js/color-editor',\n", | |
| "},\n", | |
| "/*\n", | |
| " Libraries that are not AMD compatible need shim to declare their\n", | |
| " dependencies.\n", | |
| " */\n", | |
| "'shim': {\n", | |
| " 'jquery_drag': {\n", | |
| " 'deps': ['jquery', 'jqueryui']\n", | |
| " },\n", | |
| " 'chosen': {\n", | |
| " 'deps': ['jquery'],\n", | |
| " 'exports': 'jQuery.fn.chosen'\n", | |
| " },\n", | |
| " 'orbitcontrols': {\n", | |
| " 'deps': ['three']\n", | |
| " },\n", | |
| "'slickcore': ['jqueryui'],\n", | |
| "'slickgrid': ['slickcore', 'jquery_drag', 'slickformatters',\n", | |
| " 'slickeditors']\n", | |
| "}\n", | |
| "});\n", | |
| "\n", | |
| "requirejs(\n", | |
| "[\"jquery\", \"model\", \"controller\"],\n", | |
| "function($, model, EmperorController) {\n", | |
| " var DecompositionModel = model.DecompositionModel;\n", | |
| "\n", | |
| " var div = $('#python-penguin');\n", | |
| " console.log(div)\n", | |
| "\n", | |
| " var ids = ['PC.636', 'PC.635', 'PC.356', 'PC.481', 'PC.354', 'PC.593', 'PC.355', 'PC.607', 'PC.634'];\n", | |
| " var coords = [[-0.5418718907766492, -0.2322126893194602, -1.5539895843555012, 0.9779585378585399, 0.6042066278652377], [-0.2503362247947621, 0.8942067532695469, -1.0375639554615006, -0.9668682696455478, 1.2991267859486286], [-1.2200313367348732, 0.6643872498952018, -0.65388232100731, -2.345168064831795, 0.07365375267196608], [-1.299922446104461, -0.49940762996086824, -1.4588543159826317, -0.4581513497497899, -0.26903536762453084], [-0.4707135792295782, 0.6552745171213014, 0.5823626766059697, -1.3364612709123511, -0.766335524161582], [-0.049433459884668546, 1.0362826585877511, 0.8842607848886942, 1.2840369826241902, -1.6017134657210061], [1.1372013797254774, 0.24274217197919976, 0.7617551968188236, -0.13655232966645264, -0.006742256118561079], [-1.3895886254303131, 0.8943439629112457, -0.024193070133999576, -1.3511555607575463, -0.020358237939723352], [-0.19329959778688968, 0.6177460059036628, -0.6422863415264983, -0.4271313469417334, -0.4913303558720307]];\n", | |
| " var pct_var = [0.26688705, 0.1625637, 0.13775413, 0.11217216, 0.10024775];\n", | |
| " var md_headers = ['SampleID', 'Treatment', 'DOB', 'Description'];\n", | |
| " var metadata = [['PC.636', 'Fast', '20080116', 'Fasting_mouse_I.D._636'], ['PC.635', 'Fast', '20080116', 'Fasting_mouse_I.D._635'], ['PC.356', 'Control', '20061126', 'Control_mouse_I.D._356'], ['PC.481', 'Control', '20070314', 'Control_mouse_I.D._481'], ['PC.354', 'Control', '20061218', 'Control_mouse_I.D._354'], ['PC.593', 'Control', '20071210', 'Control_mouse_I.D._593'], ['PC.355', 'Control', '20061218', 'Control_mouse_I.D._355'], ['PC.607', 'Fast', '20071112', 'Fasting_mouse_I.D._607'], ['PC.634', 'Fast', '20080116', 'Fasting_mouse_I.D._634']];\n", | |
| "\n", | |
| " var dm, ec;\n", | |
| "\n", | |
| " function init() {\n", | |
| " // Initialize the DecompositionModel\n", | |
| " dm = new DecompositionModel(name, ids, coords, pct_var,\n", | |
| " md_headers, metadata);\n", | |
| " // Initialize the EmperorController\n", | |
| " ec = new EmperorController(dm, 'python-penguin');\n", | |
| " lol = ec\n", | |
| " }\n", | |
| "\n", | |
| " function animate() {\n", | |
| " requestAnimationFrame(animate);\n", | |
| " ec.render();\n", | |
| " }\n", | |
| " $(window).resize(function() {\n", | |
| " ec.resize(window.innerWidth, window.innerHeight);\n", | |
| " });\n", | |
| "\n", | |
| " $(function(){\n", | |
| " init();\n", | |
| " animate();\n", | |
| "\n", | |
| " });\n", | |
| "\n", | |
| "}); // END REQUIRE.JS block\n", | |
| "</script>" | |
| ], | |
| "text/plain": [ | |
| "<IPython.core.display.HTML object>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "<emperor.core.Emperor at 0x10a6b4810>" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "Emperor(ores, mf)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.9" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment