Created
February 3, 2025 19:00
-
-
Save bmorris3/8ffb5fd6518152e7fa4c04eb051b77b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "2e41ab22-1af8-4b2b-b7b5-6fd19bd41f1a", | |
"metadata": {}, | |
"source": [ | |
"# `glue-jupyter` demo: omega Centauri\n", | |
"\n", | |
"<img src=\"https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Omega_Centauri_by_ESO.jpg/2880px-Omega_Centauri_by_ESO.jpg\" width=600>\n", | |
"\n", | |
"\n", | |
"We import the `JupyterApplication` from `glue_jupyter` ([source](https://github.com/glue-viz/glue-jupyter)), which provides access to `glue` [(aka \"glue-core\")](https://github.com/glue-viz/glue) in a Jupyter environment." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1cff079f-b497-4991-970f-bd08129f7c5d", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from glue_jupyter.app import JupyterApplication\n", | |
"\n", | |
"import numpy as np\n", | |
"\n", | |
"import astropy.units as u\n", | |
"from astropy.coordinates import SkyCoord\n", | |
"\n", | |
"from astroquery.vizier import Vizier\n", | |
"from astroquery.skyview import SkyView\n", | |
"\n", | |
"import matplotlib.pyplot as plt\n", | |
"from ipywidgets import HBox, VBox, Output" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c6252f24-7a30-436a-83c1-ebc25b523f26", | |
"metadata": {}, | |
"source": [ | |
"We'll specify where omega Cen is in the sky, and a radius around omega Cen where we will query for sources in a catalog:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "5267472e-3a7f-4dd9-9bbc-4e78a4f7b192", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"target = SkyCoord(ra=201.697 * u.deg, dec=-47.479472 * u.deg, distance=5240 * u.pc)\n", | |
"radius = 20 * u.arcmin" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "fdf5935b-548f-4fad-ab15-a8062659d94c", | |
"metadata": {}, | |
"source": [ | |
"We'll use [astroquery.vizier](https://astroquery.readthedocs.io/en/latest/vizier/vizier.html) to query [the Gaia DR3 main source catalog](https://vizier.cds.unistra.fr/viz-bin/VizieR-3?-source=I/355/gaiadr3) for sources that are:\n", | |
"* near omega Cen\n", | |
"* within a range of [parallaxes](https://en.wikipedia.org/wiki/Stellar_parallax#Parallax_method) corresponding to the distance of omega Cen\n", | |
" * this filters out some foreground and background objects)\n", | |
"* within a range of colors\n", | |
" * `BP-RP` is the difference between the brightness in a blue filter and a red filter. smaller values are bluer\n", | |
"* brighter than some minimum brightness\n", | |
" * the magnitude system is inverted from common sense – a minimim brightness would be a a maximum magnitude" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "3cbd5547-7392-435c-b0c9-6251b99122bd", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"table = Vizier(\n", | |
" \n", | |
" # limit the number of rows returned\n", | |
" row_limit=5_000,\n", | |
"\n", | |
" # find sources from the Gaia DR3 catalog\n", | |
" catalog=\"I/355/gaiadr3\", \n", | |
" \n", | |
" # return all columns from the database\n", | |
" columns=['*'],\n", | |
").query_region(\n", | |
" coordinates=target,\n", | |
" radius=radius,\n", | |
" column_filters=dict(\n", | |
" Plx='<0.20 AND >0.18',\n", | |
" # Dist='>4200 AND <6200',\n", | |
" BP_RP='>0 AND <1.5',\n", | |
" # Gmag='<20',\n", | |
" )\n", | |
")[0]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "075d6f9c-6d62-4294-a182-03ac069c285f", | |
"metadata": {}, | |
"source": [ | |
"Let's check how many sources we found:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "acb0a9dd-4acd-482e-9dc7-768b8f10d5ce", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"len(table)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "6a6ea626-dd40-4f65-ad8f-bc25f40690fe", | |
"metadata": {}, | |
"source": [ | |
"Let's save the table to a local CSV file:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "c39fe881-b82c-4c17-896d-533c33ca865f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"table_path = 'gaia_dr3_omega_cen.csv'\n", | |
"table.write(table_path, format='ascii.csv', overwrite=True)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "e8aac4fe-141e-4d70-98d4-478bbf18cb71", | |
"metadata": {}, | |
"source": [ | |
"Launch the app and load the table as a `Data` entry:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4e4c114c-383a-43e3-a0e3-0ad7b8533dd5", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"app = JupyterApplication()\n", | |
"\n", | |
"table_data = app.load_data(table_path)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "0cdc2ad0-2cf7-4378-a700-b640934a4229", | |
"metadata": {}, | |
"source": [ | |
"`Data` entries containing tables have `components` for each table column:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "d621ddbd-4d86-4e78-8ebe-d716ef958876", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"table_data.components" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "f9de3ee9-1544-4287-be71-d6301fa904a5", | |
"metadata": {}, | |
"source": [ | |
"Let's download an image of the cluster from [SkyView](https://skyview.gsfc.nasa.gov/current/cgi/titlepage.pl). Note that `SkyView` is not related to the tool called sometimes called \"SkyViewer\" that we're working on." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "f14e022d-e976-4187-8093-2c117dcac573", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image = SkyView().get_images(\n", | |
" position=target,\n", | |
" radius=radius,\n", | |
" survey='2MASS-J',\n", | |
" pixels=5000,\n", | |
" coordinates='J2000'\n", | |
")\n", | |
"\n", | |
"# save the FITS image to this path:\n", | |
"image_path = 'omega_cen_2mass.fits'\n", | |
"image[0][0].writeto(image_path, overwrite=True)\n", | |
"\n", | |
"# load the FITS image:\n", | |
"image_data = app.load_data(image_path)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "c048808c-9c5d-417f-bc89-59c67986d952", | |
"metadata": {}, | |
"source": [ | |
"The `components` of the image `Data` have coordinates that can be [*linked*](https://docs.glueviz.org/en/stable/developer_guide/linking.html) to components of other `Data` objects." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "9f7ba1fc-da4c-448c-a311-a965dd4f38ea", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_data.components" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "5bf4dadb-377c-434d-82b8-9c7bc3f91a51", | |
"metadata": {}, | |
"source": [ | |
"We want to arrange our widgets into a grid, so we'll capture the output widget each time we create one, then arrange them before we display them." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "ed5bb01a-efe3-4661-a48f-73464384eae7", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"image_out = Output()\n", | |
"cmd_out = Output()\n", | |
"hist_out = Output()\n", | |
"other_out = Output()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "a37387ba-8ae1-4536-9252-d8808077c7e1", | |
"metadata": {}, | |
"source": [ | |
"Create each viewer, set axis limits and colormaps:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "8a5ee930-7023-4531-8204-39219c64360b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with cmd_out:\n", | |
" cmd_viewer = app.scatter2d(data=table_data, x='BP-RP', y='Gmag')\n", | |
" \n", | |
" # astronomers expect us to invert y axis:\n", | |
" cmd_viewer.state.y_max = 10\n", | |
" cmd_viewer.state.y_min = 22\n", | |
"\n", | |
"with image_out:\n", | |
" image_viewer = app.imshow(data=image_data)\n", | |
" image_viewer.add_data(table_data)\n", | |
" \n", | |
" image_viewer.layers[0].state.cmap = plt.cm.viridis_r\n", | |
" image_viewer.layers[0].state.stretch = 'arcsinh'\n", | |
"\n", | |
"with other_out:\n", | |
" other_viewer = app.scatter2d(data=table_data, x='RV', y='Plx')\n", | |
"\n", | |
"\n", | |
"with hist_out:\n", | |
" hist_viewer = app.histogram1d(data=table_data, x='Dist', n_bin=100)\n", | |
" hist_viewer.state.x_log = True" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "28bcb308-2d8a-4f3e-a68e-920fb13bd6e4", | |
"metadata": {}, | |
"source": [ | |
"Add links between columns of the catalog data and the image coordinates:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "4223fb34-6cac-4427-af41-4e9d4283cfb1", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"app.add_link(table_data, 'RAJ2000', image_data, 'Right Ascension')\n", | |
"app.add_link(table_data, 'DEJ2000', image_data, 'Declination')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "eb3897ac-0ecc-4948-916b-8adef98f3706", | |
"metadata": {}, | |
"source": [ | |
"Here I'm manually arranging the widgets:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "971999e1-dd36-4133-a3ca-0cf11f8b897e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"box = VBox([\n", | |
" HBox([image_out, cmd_out]),\n", | |
" HBox([hist_out, other_out])\n", | |
"])\n", | |
"\n", | |
"box" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "0372a697-5fd1-44a5-a5c6-b2b5c0e9b077", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.11.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment