Skip to content

Instantly share code, notes, and snippets.

@ElDeveloper
Created April 10, 2016 00:01
Show Gist options
  • Select an option

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

Select an option

Save ElDeveloper/e6abe9d4bc3c108db34c519018e86185 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/453).**"
]
},
{
"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": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"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": 8,
"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": 9,
"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": 10,
"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": 11,
"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-0xc0aba1ed' 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-0xc0aba1ed');\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.5396115851815823, -2.2114886574817905, 0.5622671332033582, -0.39925523990059025, -0.6109896191002551], [0.9059588747695676, -0.4503605281093799, -0.7247898229939138, 0.3252834454173237, -1.1420071701136327], [-0.8740018805353699, 1.224122832192673, -1.6345694467514236, 0.19168894303903208, -0.781600138103193], [0.32391524066180377, -0.1256450820284476, -1.463516572061507, 1.3567417588513442, 1.375528886370264], [-0.7595905259706305, -0.19251533211409227, 0.3361331694319654, -1.029284876326024, -0.16494079308180273], [-1.6205568535304165, 0.7484373683906822, -0.14922340010979587, -1.0168878614605332, -0.38031043018456484], [-0.4199706495919136, -0.5416730155297819, 0.9691266159444735, 0.4794465298848642, 1.4603807363726622], [-0.6890864898424187, 2.756169841988562, -0.07790599985945258, -0.0012879792482044016, 0.0626145719416414], [-0.0816642400304298, -0.009662185813216942, 0.7608898167094942, -0.36006572200662096, 0.2858303126575494]];\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-0xc0aba1ed');\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 0x10c0d9250>"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Emperor(ores, mf, remote=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercising the remote functionality"
]
},
{
"cell_type": "code",
"execution_count": 13,
"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='emperor-notebook-0xc868f9f3' 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': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/jquery-2.1.4.min',\n",
" 'jqueryui': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/jquery-ui.min',\n",
" 'jquery_drag': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/jquery.event.drag-2.2.min',\n",
"\n",
" /* jQuery plugins */\n",
" 'chosen': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/chosen.jquery.min',\n",
" 'spectrum': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/spectrum.min',\n",
"\n",
" /* other libraries */\n",
" 'underscore': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/underscore-min',\n",
" 'chroma': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/chroma.min',\n",
"\n",
"\n",
" /* THREE.js and plugins */\n",
" 'three': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/three.min',\n",
" 'orbitcontrols': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/three.js-plugins/OrbitControls',\n",
"\n",
" /* SlickGrid */\n",
" 'slickcore': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/slick.core.min',\n",
" 'slickgrid': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/slick.grid.min',\n",
" 'slickformatters': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/slick.editors.min',\n",
" 'slickeditors': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/vendor/js/slick.formatters.min',\n",
"\n",
" /* Emperor's objects */\n",
" 'model': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/model',\n",
" 'view': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/view',\n",
" 'controller': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/controller',\n",
" 'draw': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/draw',\n",
" 'scene3d': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/sceneplotview3d',\n",
" 'viewcontroller': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/view-controller',\n",
" 'colorviewcontroller': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/color-view-controller',\n",
" 'visibilitycontroller': 'https://cdn.rawgit.com/biocore/emperor/new-api/emperor/support_files/js/visibility-controller',\n",
" 'color-editor': 'https://cdn.rawgit.com/biocore/emperor/new-api/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-0xc868f9f3');\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.5396115851815823, -2.2114886574817905, 0.5622671332033582, -0.39925523990059025, -0.6109896191002551], [0.9059588747695676, -0.4503605281093799, -0.7247898229939138, 0.3252834454173237, -1.1420071701136327], [-0.8740018805353699, 1.224122832192673, -1.6345694467514236, 0.19168894303903208, -0.781600138103193], [0.32391524066180377, -0.1256450820284476, -1.463516572061507, 1.3567417588513442, 1.375528886370264], [-0.7595905259706305, -0.19251533211409227, 0.3361331694319654, -1.029284876326024, -0.16494079308180273], [-1.6205568535304165, 0.7484373683906822, -0.14922340010979587, -1.0168878614605332, -0.38031043018456484], [-0.4199706495919136, -0.5416730155297819, 0.9691266159444735, 0.4794465298848642, 1.4603807363726622], [-0.6890864898424187, 2.756169841988562, -0.07790599985945258, -0.0012879792482044016, 0.0626145719416414], [-0.0816642400304298, -0.009662185813216942, 0.7608898167094942, -0.36006572200662096, 0.2858303126575494]];\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-0xc868f9f3');\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 0x10c0d9710>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Emperor(ores, mf, remote=True)"
]
},
{
"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