{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Interactive plotting of Data Lab catalog data\n", "In this notebook, we will retrieve the Hydra II SMASH catalog data and make an interactive pair of plots using the Bokeh (http://bokeh.pydata.org/en/latest/) library.\n", "\n", "## Data retrieval\n", "We will use the code example provided in the \"How to use the DataLab query manager service\" notebook to access the data. The columns we will need are RA, Dec, g magnitude, r magnitude, and depthflag.\n", "\n", "## Visualization\n", "Bokeh comes with a number of built-in tools for producing interactive plots. In this example, we will make a pair of plots, a plot of RA vs. Dec on the left and g-r vs. r on the right. We will use Bokeh's linked brushing tools to interactively select a set of points on the RA vs. Dec plot, which will automatically highlight the same points on the color-magnitude diagram on the right. The intended use of this notebook is that the user will start with candidate overdensities and then use Bokeh tools to explore them interactively.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Initialization\n", "\n", "We need modules from the Bokeh library, NumPy, and Pandas. For the Data Lab query, we need authClient and queryClient from Data Lab's dl library." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Start\n", "Got token anonymous.0.0.anon_access\n" ] } ], "source": [ "print \"Start\"\n", "from bokeh.models import ColumnDataSource\n", "from bokeh.models import LinearAxis,Range1d\n", "from bokeh.plotting import figure, gridplot, output_file, show\n", "from bokeh.io import output_notebook\n", "import numpy as np\n", "import sys\n", "import pandas as pd\n", "\n", "from cStringIO import StringIO\n", "from dl import authClient\n", "from dl import queryClient \n", "\n", "# Get the security token for the datalab demo user\n", "token = authClient.login('anonymous')\n", "print \"Got token\",token" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Query the SMASH DR1 database\n", "\n", "We will query the averaged photometry table from the SMASH catalog and select Field 169, which we know contains the Hydra II dwarf." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Your query is: select ra,dec,gmag,rmag,depthflag from smash_dr1.object where (fieldid = '169' AND (depthflag > 1) and (abs(sharp) < 0.5) and (gmag is not null) and (gmag between 9 and 25) and ((gmag-rmag) between -1.5 and 3.0))\n" ] } ], "source": [ "field = 169 # SMASH Field Number to query\n", "depth = 1 # minimum depth \n", "raname = 'ra'\n", "decname = 'dec'\n", "mags = 'gmag,rmag'\n", "dbase='smash_dr1.object'\n", "fid = 'fieldid'\n", "\n", "# Create the query string.\n", "query = ('select '+raname+','+decname+','+mags+',depthflag from '+dbase+ \\\n", " ' where ('+fid+' = \\'%d\\' AND' \\\n", " ' (depthflag > %d) and ' + \\\n", " ' (abs(sharp) < 0.5) and ' + \\\n", " ' (gmag is not null) and ' + \\\n", " ' (gmag between 9 and 25) and ' + \\\n", " ' ((gmag-rmag) between -1.5 and 3.0))') % \\\n", " (field, depth)\n", " \n", "print \"Your query is:\", query" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We issue the query through the Query Manager, which connects directly to the database." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Making query\n", "297788 objects found.\n", "CPU times: user 299 ms, sys: 68 ms, total: 367 ms\n", "Wall time: 12.6 s\n" ] } ], "source": [ "%%time\n", "print \"Making query\"\n", "# Call the Query Manager Service \n", "response = queryClient.query(token, adql = query, fmt = 'csv')\n", "df = pd.read_csv(StringIO(response))\n", "\n", "print len(df), \"objects found.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data munging\n", "\n", "Next we add a g-r color column to the Pandas dataframe." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | ra | \n", "dec | \n", "gmag | \n", "rmag | \n", "depthflag | \n", "g_r | \n", "
---|---|---|---|---|---|---|
297783 | \n", "185.509066 | \n", "-30.488367 | \n", "24.6092 | \n", "24.4970 | \n", "2 | \n", "0.1122 | \n", "
297784 | \n", "185.515371 | \n", "-30.490068 | \n", "24.8446 | \n", "25.0287 | \n", "2 | \n", "-0.1841 | \n", "
297785 | \n", "185.479043 | \n", "-30.480986 | \n", "24.9664 | \n", "25.2752 | \n", "2 | \n", "-0.3088 | \n", "
297786 | \n", "185.508911 | \n", "-30.476832 | \n", "23.9298 | \n", "23.8104 | \n", "2 | \n", "0.1194 | \n", "
297787 | \n", "185.526843 | \n", "-30.497895 | \n", "24.5801 | \n", "24.4355 | \n", "2 | \n", "0.1446 | \n", "
\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n",
" \"from bokeh.resources import INLINE\\n\"+\n",
" \"output_notebook(resources=INLINE)\\n\"+\n",
" \"
\\n\"+\n",
" \"