Skip to content

Instantly share code, notes, and snippets.

@ElDeveloper
Created April 9, 2016 23:56
Show Gist options
  • Select an option

  • Save ElDeveloper/bcd8258d295e0a451987e48189d97e3e to your computer and use it in GitHub Desktop.

Select an option

Save ElDeveloper/bcd8258d295e0a451987e48189d97e3e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"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": false
},
"outputs": [],
"source": [
"import pandas as pd, numpy as np\n",
"from emperor import Emperor\n",
"from skbio.stats.ordination import OrdinationResults\n",
"\n",
"\n",
"from emperor.util import nbinstall"
]
},
{
"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": [],
"source": [
"x = Emperor(ores, mf, remote=False)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"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=\"/nbextensions/emperor/support_files/css/emperor.css\">',\n",
" '<link rel=\"stylesheet\" type=\"text/css\" href=\"/nbextensions/emperor/support_files/vendor/css/jquery-ui.min.css\">',\n",
" '<link rel=\"stylesheet\" type=\"text/css\" href=\"/nbextensions/emperor/support_files/vendor/css/slick.grid.min.css\">',\n",
" '<link rel=\"stylesheet\" type=\"text/css\" href=\"/nbextensions/emperor/support_files/vendor/css/spectrum.min.css\">',\n",
" '<link rel=\"stylesheet\" type=\"text/css\" href=\"/nbextensions/emperor/support_files/vendor/css/chosen.min.css\">'\n",
" ]);\n",
"}}\n",
"</script>\n",
"<div id='emperor-notebook-0x2fced7f9' style=\"position: relative; width:100%; height:500px;\"></div>\n",
"</div>\n",
"\n",
"<script type=\"text/javascript\">\n",
"requirejs.config({\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': '/nbextensions/emperor/support_files/vendor/js/jquery-2.1.4.min',\n",
" 'jqueryui': '/nbextensions/emperor/support_files/vendor/js/jquery-ui.min',\n",
" 'jquery_drag': '/nbextensions/emperor/support_files/vendor/js/jquery.event.drag-2.2.min',\n",
"\n",
" /* jQuery plugins */\n",
" 'chosen': '/nbextensions/emperor/support_files/vendor/js/chosen.jquery.min',\n",
" 'spectrum': '/nbextensions/emperor/support_files/vendor/js/spectrum.min',\n",
"\n",
" /* other libraries */\n",
" 'underscore': '/nbextensions/emperor/support_files/vendor/js/underscore-min',\n",
" 'chroma': '/nbextensions/emperor/support_files/vendor/js/chroma.min',\n",
"\n",
"\n",
" /* THREE.js and plugins */\n",
" 'three': '/nbextensions/emperor/support_files/vendor/js/three.min',\n",
" 'orbitcontrols': '/nbextensions/emperor/support_files/vendor/js/three.js-plugins/OrbitControls',\n",
"\n",
" /* SlickGrid */\n",
" 'slickcore': '/nbextensions/emperor/support_files/vendor/js/slick.core.min',\n",
" 'slickgrid': '/nbextensions/emperor/support_files/vendor/js/slick.grid.min',\n",
" 'slickformatters': '/nbextensions/emperor/support_files/vendor/js/slick.editors.min',\n",
" 'slickeditors': '/nbextensions/emperor/support_files/vendor/js/slick.formatters.min',\n",
"\n",
" /* Emperor's objects */\n",
" 'model': '/nbextensions/emperor/support_files/js/model',\n",
" 'view': '/nbextensions/emperor/support_files/js/view',\n",
" 'controller': '/nbextensions/emperor/support_files/js/controller',\n",
" 'draw': '/nbextensions/emperor/support_files/js/draw',\n",
" 'scene3d': '/nbextensions/emperor/support_files/js/sceneplotview3d',\n",
" 'viewcontroller': '/nbextensions/emperor/support_files/js/view-controller',\n",
" 'colorviewcontroller': '/nbextensions/emperor/support_files/js/color-view-controller',\n",
" 'visibilitycontroller': '/nbextensions/emperor/support_files/js/visibility-controller',\n",
" 'color-editor': '/nbextensions/emperor/support_files/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 = $('#emperor-notebook-0x2fced7f9');\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.5195994588154056, 0.8101944362813259, -0.9580272453644373, 0.24365834012553392, -0.04562434096216547], [0.05371499490410812, -0.8118327253570601, 2.1125660235307993, -0.6583037226149553, 1.1103367735883327], [0.5177955614602356, -0.5713779213446873, 0.5575859787698958, 0.28120474910974447, -1.0419118386179258], [0.5531004854703466, 0.038638808573477734, 1.3479600328182506, -0.7258565191158548, 0.878141721411364], [0.14431478670156242, 0.06957979044765827, -0.5744574825945004, -0.42110061093911416, -0.3199078664921232], [1.38353775281821, -0.5734566582511832, -0.5959986897888546, 0.691340775991246, -0.27517210981575024], [-1.0100795009630266, -0.7794320419370481, -0.9888554726326914, 0.41364698869219607, 1.5078448485464477], [3.3169406795244107, -0.7038939367049797, -0.7875975155373139, -0.38164076592261814, 0.07587361722442403], [-0.8923966667832034, -1.0028566795367995, 1.2914418428132173, 1.3644929547114284, -0.6664087934154725]];\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, 'emperor-notebook-0x2fced7f9');\n",
" }\n",
"\n",
" function animate() {\n",
" requestAnimationFrame(animate);\n",
" ec.render();\n",
" }\n",
" $(div).resize(function() {\n",
" ec.resize(div.innerWidth(), div.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 0x10c1b67d0>"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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