#!/usr/bin/env python # coding: utf-8 # # A demonstration of Star/Galaxy separation in the SMASH catalog # In this notebook, we will query the SMASH DR1 catalog and apply constraints on the "sharp" and "prob" parameters to demonstrate ways to create samples of likely stars and galaxies. # # ## Visualization # We will use Datashader and Bokeh to make fast interactive plots. # # ## Known issues # The color-magnitude diagram is plotted upside-down from the usual sense due to a current limitation in the notebook's use of Datashader. # ### Initialization # # We need modules from the Bokeh library, Datashader, NumPy, and Pandas, as well as the Data Lab modules to connect to and query the database. # In[1]: print "Start" from cStringIO import StringIO from dl import authClient from dl import queryClient import pandas as pd import datashader as ds import datashader.glyphs import datashader.transfer_functions as tf import bokeh.plotting as bp from datashader.bokeh_ext import InteractiveImage # Get the security token for the datalab demo user token = authClient.login('anonymous') print "Got token",token # ### Querying the SMASH DR1 catalog # # We will query the SMASH catalog over a range of fields to sample a variety of field properties. We set a constraint on the depthflag parameter to insist on detection in the deep exposures. To make the query go faster, one can restrict the range of fieldid in the query. The default field range will return roughly 6 million objects. # In[2]: get_ipython().run_cell_magic('time', '', 'depth = 1 # minimum depth \nraname = \'ra\'\ndecname = \'dec\'\nmags = \'gmag,rmag\'\ndbase=\'smash_dr1.object\'\n\n# Create the query string.\nquery = (\'select \'+raname+\',\'+decname+\',\'+mags+\',sharp,chi,prob from \'+dbase+ \\\n \' where (depthflag > %d and \' + \\\n \' (gmag is not null) and \' + \\\n \' (fieldid>55 and fieldid<70))\') % \\\n (depth)\n \nprint "Your query is:", query\nprint "Making query"\n\n# Call the Query Manager Service \nresponse = queryClient.query(token, adql = query, fmt = \'csv\')\ndf = pd.read_csv(StringIO(response))\n\nprint len(df), "objects found."\n') # ### Making cuts on parameters to separate stars from galaxies # # We will first make a single cut on the sharp parameter, and classify objects with sharp>0.7 as galaxies. Pandas allows us to add a Class column to the dataframe and specify that it should be considered a category. We will also add a g_r column to the Pandas dataframe. # In[3]: sharpthresh=0.7 df["Class"]='Star' df.loc[(abs(df["sharp"])>sharpthresh),"Class"]='Galaxy' df["Class"]=df["Class"].astype('category') df["g_r"]=df["gmag"]-df["rmag"] df.tail() # ### Displaying the results of our cut # # We now use Datashader and Bokeh to make an interactive plot of g magnitude vs. chi. Datashader assigns different colors to each category, in this case blue for objects labeled stars, and red for objects labeled galaxies. The plot shows that the sharp cut separates two sequences in the magnitude-chi plane, with galaxies having larger chi values. One might be tempted to use chi as an additional constraint. However, at bright magnitudes the chi for stars clearly increases, while at bright magnitudes the sequences overlap in chi. The sharp cut likely confuses some stars and galaxies, especially at faint magnitudes. One could increase the sharp threshold for a more complete star sample, or decrease it for a more pure one. # In[4]: bp.output_notebook() p = bp.figure(tools='pan,wheel_zoom,box_zoom,reset',x_range=(14,26), y_range=(0,5)) p.xaxis.axis_label='gmag' p.yaxis.axis_label='chi' def image_callback(x_range, y_range, w, h): cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range) agg = cvs.points(df, 'gmag', 'chi', ds.count_cat('Class')) img = tf.shade(agg, how='log') return tf.dynspread(img, how='add',threshold=0.5) InteractiveImage(p, image_callback) # ### Effect of our cut on the CMD # # Here we make an additional interactive plot featureing the g-r,g color-magnitude diagram. Note that the CMD is upside down from the usual sense. # In[5]: p = bp.figure(tools='pan,wheel_zoom,box_zoom,reset',x_range=(-2,3), y_range=(14,26)) p.xaxis.axis_label='gmag-rmag' p.yaxis.axis_label='gmag' def image_callback(x_range, y_range, w, h): cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range) agg = cvs.points(df, 'g_r', 'gmag', ds.count_cat('Class')) img = tf.shade(agg, how='log') return tf.dynspread(img, how='add',threshold=0.5) InteractiveImage(p, image_callback) # ### Making a cut on both sharp and prob to get a cleaner sample of galaxies # # The simple sharp>0.7 cut potentially labels faint junk objects as galaxies. We will add a second cut on the prob parameter, which derives from SExtractor's Stellaricity Index, to get a cleaner sample of galaxies. # In[6]: sharpthresh=0.7 probthresh=0.2 df["Class"]='Star' df.loc[(abs(df["sharp"])>sharpthresh) & (df["prob"]