{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates basic usage of the firefly_client API for Firefly Slate Viewer to render tables, images and charts in a grid layout style. This notebook shows the same layout content as what ffapi-api-slate.html renders in Firefly. \n", "\n", "Note that it may be necessary to wait for some cells (like those displaying an image) to complete before executing later cells." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Setup" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imports for Python 2/3 compatibility" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function, division, absolute_import" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imports for firefly_client" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from firefly_client import FireflyClient" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example uses http://127.0.0.1:8080/firefly as the server\n", "\n", "'slate.html' is a template made for grid view " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url='http://127.0.0.1:8080/firefly'\n", "html = 'slate.html'\n", "\n", "fc = FireflyClient(url, html_file=html)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Display tables, images, XY charts, and Histograms in Window/Grid like layout\n", "\n", "Each rendered unit on Firefly Slate Viewer is called a'cell'. To render a cell and its content, the cell location (row, column, width, height), element type and cell ID are needed. (row, column) and (width, height) represent the position and size of the cell in terms of the grid blocks on Firefly Slate Viewer. Element types include types of 'tables', images', 'xyPlots', 'tableImageMeta' and 'coverageImage'. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use astropy here for convenience, but firefly_client itself does not depend on astropy." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import astropy.utils.data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open a browser to the firefly server in a new tab. Only works when running the notebook locally." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc.launch_browser()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "re-init Firefly completely and clean the viewer. You may restart the viewer instead of launching a new browser to repeat rendering cells. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc.reinit_viewer()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Display tables and catalogs\n", "\n", "\n", "Add some tables into cell 'main' (default cell id for tables)\n", "\n", "\n", "- add first table in cell 'main':\n", "- 'main' is the cell id currently supported by Firefly for element type 'tables'\n", "- this cell is shown at row = 0, col = 0 with width = 4, height = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(0, 0, 4, 2, 'tables', 'main')\n", "\n", "if r['success']:\n", " tbl_name = astropy.utils.data.download_file(\"http://web.ipac.caltech.edu/staff/roby/demo/moving/MOST_results-sample.tbl\",\n", " timeout=120, cache=True)\n", " meta_info = {'datasetInfoConverterId': 'SimpleMoving','positionCoordColumns': 'ra_obj;dec_obj;EQ_J2000',\n", " 'datasource': 'image_url'}\n", " fc.show_table(fc.upload_file(tbl_name), tbl_id='movingtbl', title='A moving object table', page_size=15, meta=meta_info)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- add 2nd table in cell 'main' for chart and histogram " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tbl_name = astropy.utils.data.download_file('http://web.ipac.caltech.edu/staff/roby/demo/WiseDemoTable.tbl',\n", " timeout=120, cache=True)\n", "fc.show_table(fc.upload_file(tbl_name), tbl_id='tbl_chart', title='table for xyplot and histogram', page_size=15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- add 3rd table in cell 'main'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tbl_name = astropy.utils.data.download_file(\"http://web.ipac.caltech.edu/staff/roby/demo/test-table4.tbl\", \n", " timeout=120, cache=True)\n", "meta_info = {'datasource': 'FITS'}\n", "fc.show_table(fc.upload_file(tbl_name), title='A table of simple images', page_size=15, meta=meta_info)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- add 4th table in cell 'main'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "tbl_name = astropy.utils.data.download_file(\"http://web.ipac.caltech.edu/staff/roby/demo/test-table-m31.tbl\", \n", " timeout=120, cache=True)\n", "\n", "meta_info = {'positionCoordColumns': 'ra_obj;dec_obj;EQ_J2000', 'datasource': 'FITS'}\n", "fc.show_table(fc.upload_file(tbl_name), title='A table of m31 images', page_size=15, meta=meta_info)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add different types of image displays\n", "\n", "- show cell with id 'image-meta' continaing image from the active table containing datasource column in cell 'main'\n", "- the cell is shown at row = 2, col = 0 with width = 4, height = 2 " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(2, 0, 4, 2, 'tableImageMeta', 'image-meta')\n", "if r['success']:\n", " fc.show_image_metadata(viewer_id=r['cell_id'])\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- show cell 'wise-content' containing fits at row = 0, col = 4 with width = 2, height = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(0, 4, 2, 2, 'images', 'wise-cutout')\n", "if r['success']:\n", " image_name = astropy.utils.data.download_file('http://irsa.ipac.caltech.edu/ibe/data/wise/allsky/4band_p1bm_frm/6a/02206a' +\n", " '/149/02206a149-w1-int-1b.fits?center=70,20&size=200pix')\n", " fc.show_fits(file_on_server=fc.upload_file(image_name), viewer_id=r['cell_id'], title='WISE Cutout')\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- show 4 fits of moving objects in cell 'movingStff' at row = 2, col = 4 with width = 2, height = 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(2, 4, 2, 2, 'images', 'movingStuff')\n", "if r['success']:\n", " v_id = r['cell_id']\n", " fc.show_fits(plot_id='m49025b_143_2', viewer_id=v_id, OverlayPosition='330.347003;-2.774482;EQ_J2000',\n", " ZoomType='TO_WIDTH_HEIGHT', Title='49025b143-w2', plotGroupId='movingGroup',\n", " URL='http://web.ipac.caltech.edu/staff/roby/demo/moving/49025b143-w2-int-1b.fits')\n", " fc.show_fits(plot_id='m49273b_134_2', viewer_id=v_id, OverlayPosition='333.539702;-0.779310;EQ_J2000',\n", " ZoomType='TO_WIDTH_HEIGHT', Title='49273b134-w2', plotGroupId='movingGroup',\n", " URL='http://web.ipac.caltech.edu/staff/roby/demo/moving/49273b134-w2-int-1b.fits')\n", " fc.show_fits(plot_id='m49277b_135_1', viewer_id=v_id, OverlayPosition='333.589054;-0.747251;EQ_J2000',\n", " ZoomType='TO_WIDTH_HEIGHT', Title='49277b135-w1', plotGroupId='movingGroup',\n", " URL='http://web.ipac.caltech.edu/staff/roby/demo/moving/49277b135-w1-int-1b.fits')\n", " fc.show_fits(plot_id='m49289b_134_2', viewer_id=v_id, OverlayPosition='333.736578;-0.651222;EQ_J2000',\n", " ZoomType='TO_WIDTH_HEIGHT', Title='49289b134-w2', plotGroupId='movingGroup',\n", " URL='http://web.ipac.caltech.edu/staff/roby/demo/moving/49289b134-w2-int-1b.fits')\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add charts (xy plot and histogram)\n", "\n", "- show the cell 'chart-cell-xy' with xy plot related to the table with id 'tbl_chart' in cell 'main'\n", "- this cell is shown at row = 4, col = 0 with width = 2, height = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(4, 0, 2, 3, 'xyPlots', 'chart-cell-xy')\n", "if r['success']:\n", " trace1 = {\n", " 'tbl_id': 'tbl_chart',\n", " 'x': \"tables::ra1\",\n", " 'y': \"tables::dec1\",\n", " 'mode': 'markers',\n", " 'type': 'scatter', \n", " 'marker': {'size': 4}}\n", " trace_data=[trace1]\n", " \n", " layout_s = {'title': 'Coordinates', \n", " 'xaxis': {'title': 'ra1 (deg)'}, 'yaxis': {'title': 'dec1 (deg)'}} \n", " fc.show_chart(group_id=r['cell_id'], layout=layout_s, data=trace_data ) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- show the cell with histogram related to the table with id 'tbl_chart' in cell 'main', \n", "- this cell is shown at row = 4, col = 2, with width = 2, height = 3 \n", "- the cell id is automatically created by FireflyClient in case it is not specified externally. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(4, 2, 2, 3, 'xyPlots')\n", "if r['success']:\n", " histData = [\n", " {\n", " 'type': 'fireflyHistogram',\n", " 'name': 'magzp', \n", " 'marker': {'color': 'rgba(153, 51, 153, 0.8)'},\n", " 'firefly': {\n", " 'tbl_id': 'tbl_chart',\n", " 'options': {\n", " 'algorithm': 'fixedSizeBins',\n", " 'fixedBinSizeSelection': 'numBins',\n", " 'numBins': 30,\n", " 'columnOrExpr': 'magzp'\n", " }\n", " },\n", " }\n", " ]\n", "\n", " layout_hist = {'title': 'Magnitude Zeropoints',\n", " 'xaxis': {'title': 'magzp'}, 'yaxis': {'title': ''}} \n", " result = fc.show_chart(group_id=r['cell_id'], layout=layout_hist, data=histData ) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Add more images\n", "\n", "- show coverage image associated with the active table in cell 'main'\n", "- ths cell is shown at row = 4, col = 4 with width = 2, height = 3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "r = fc.add_cell(4, 4, 2, 3, 'coverageImage', 'image-coverage')\n", "if r['success']:\n", " fc.show_coverage(viewer_id=r['cell_id'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- show image in random location without passing cell location and cell id (i.e. without calling add_cell)\n", "- the image is shown in the cell with id 'DEFAULT_FITS_VIEWER_ID' in default, \n", "- and the cell is automatically located by Firefly" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "img_name = astropy.utils.data.download_file('http://web.ipac.caltech.edu/staff/roby/demo/wise-m51-band2.fits', \n", " timeout=120, cache=True)\n", "fc.show_fits(file_on_server=fc.upload_file(img_name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- show second image in random. it is shown in the same cell as the previous one " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fc.show_fits(plot_id='xxq', Service='TWOMASS', Title='2mass from service', ZoomType='LEVEL',\n", " initZoomLevel=2, SurveyKey='asky', SurveyKeyBand='k',\n", " WorldPt='10.68479;41.26906;EQ_J2000', SizeInDeg='.12')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }