Skip to content

Instantly share code, notes, and snippets.

@bmorris3
Created December 12, 2024 18:14
Show Gist options
  • Save bmorris3/7aa8725bb795722a46cc43000d645085 to your computer and use it in GitHub Desktop.
Save bmorris3/7aa8725bb795722a46cc43000d645085 to your computer and use it in GitHub Desktop.
Loading astropy tables as Data entries in jdaviz
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "e5cde002-d40e-4bc7-b7fd-c6abb7df4dc7",
"metadata": {},
"source": [
"# Demo: scatter layers in Imviz for catalogs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4c224e8a-7c41-4ea3-a780-4cc68af62b6f",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"\n",
"import astropy.units as u\n",
"from astropy.coordinates import SkyCoord\n",
"from astropy.coordinates import Distance\n",
"\n",
"from astroquery.vizier import Vizier\n",
"from astroquery.skyview import SkyView\n",
"\n",
"from glue.core.roi import EllipticalROI\n",
"from glue.core.link_helpers import LinkSame\n",
"from jdaviz import Imviz"
]
},
{
"cell_type": "markdown",
"id": "850741a0-2a76-4583-b759-1b4bb85acfc5",
"metadata": {},
"source": [
"Download blue/red/IR images of the Andromeda Galaxy:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3c4d7b79-8c8d-46f3-9f03-0b1df44cc282",
"metadata": {},
"outputs": [],
"source": [
"m31_coord = SkyCoord.from_name('M31')\n",
"surveys = ['DSS2 Blue', 'DSS2 Red', 'DSS2 IR']\n",
"\n",
"images = [\n",
" SkyView.get_images(m31_coord, survey, radius=1 * u.deg, pixels=2000)[0][0]\n",
" for survey in surveys\n",
"]"
]
},
{
"cell_type": "markdown",
"id": "4f29cb4d-22ed-4eec-8126-51d2bb9d0e3e",
"metadata": {},
"source": [
"We will later link the image and table coordinates by their RA and Dec:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "032ed8e2-d704-44da-a096-a0d5e42b5e7e",
"metadata": {},
"outputs": [],
"source": [
"def _link_new_data(app, reference_data=None, data_to_be_linked=None):\n",
" dc = app.data_collection\n",
" ref_data = dc[reference_data] if reference_data else dc[0]\n",
" linked_data = dc[data_to_be_linked] if data_to_be_linked else dc[-1]\n",
"\n",
" def get_cid_from_label(data, label):\n",
" for cid in data.component_ids():\n",
" if cid.label == label:\n",
" return cid\n",
"\n",
" image_component_names = [get_cid_from_label(ref_data, 'Right Ascension'), get_cid_from_label(ref_data, 'Declination')]\n",
" table_column_names = [get_cid_from_label(linked_data, 'RA_ICRS'), get_cid_from_label(linked_data, 'DE_ICRS')]\n",
" \n",
" for cid1, cid2 in zip(image_component_names, table_column_names):\n",
" new_link = LinkSame(\n",
" cid1=cid1,\n",
" cid2=cid2,\n",
" data1=ref_data,\n",
" data2=linked_data,\n",
" labels1=ref_data.label,\n",
" labels2=linked_data.label\n",
" )\n",
" \n",
" dc.add_link(new_link)\n",
" return"
]
},
{
"cell_type": "markdown",
"id": "425f02a0-2632-4e13-b7f9-26f4ffa22734",
"metadata": {},
"source": [
"Load the three images:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d301ad27-a11f-47fd-b16e-239905d83cd0",
"metadata": {},
"outputs": [],
"source": [
"imviz = Imviz()\n",
"\n",
"with imviz.batch_load():\n",
" for image in images:\n",
" imviz.load_data(image)\n",
"\n",
"imviz.show(height=1000)"
]
},
{
"cell_type": "markdown",
"id": "8268ae12-668f-4dd5-bb4c-b673b4653784",
"metadata": {},
"source": [
"Assign images to RGB channels:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2da23095-6f71-44dd-93ec-3fcd637b075f",
"metadata": {},
"outputs": [],
"source": [
"plot_options = imviz.plugins[\"Plot Options\"]\n",
"plot_options.image_color_mode = 'Color'\n",
"plot_options.apply_RGB_presets()"
]
},
{
"cell_type": "markdown",
"id": "350cdaa4-8589-4ff8-81ce-44ebced5807a",
"metadata": {},
"source": [
"Query for bright sources at M31-like distances by cutting the Gaia catalog on parallax:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c5a4075-19de-4e1f-b8b4-3dd31f8141a0",
"metadata": {},
"outputs": [],
"source": [
"# find sources with parallaxes less than this, which are likely \n",
"# to be at distances like M31:\n",
"m31_parallax_lt = Distance(0.6 * u.Mpc).parallax.to_value(u.mas)\n",
"\n",
"Vizier.ROW_LIMIT = 20_000\n",
"result = Vizier.query_region(\n",
" coordinates=m31_coord, \n",
" width=2*u.deg,\n",
" height=2*u.deg,\n",
" catalog='I/355/gaiadr3',\n",
" column_filters={\n",
" 'parallax': f'<{m31_parallax_lt}', \n",
" 'Gmag': '<20'\n",
" }\n",
")[0]"
]
},
{
"cell_type": "markdown",
"id": "fc41136b-ed5d-42a1-8ebc-69fdce379efa",
"metadata": {},
"source": [
"Convert the astropy `Table` that gets returned to a pandas `DataFrame`, load it as a glue `Data` entry, add to viewer, link its dimensions to the image:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "294f32de-44c0-4688-8aec-8d5bf2e09f23",
"metadata": {},
"outputs": [],
"source": [
"# generic astropy.table.Table has no glue translator in glue-astronomy (yet!), \n",
"# though we implemented a time-series focused solution in LCviz. here I'll \n",
"# convert Table -> DataFrame to borrow the pandas translator:\n",
"imviz.app.data_collection['table'] = result.to_pandas()\n",
"\n",
"# add the data to the viewer:\n",
"imviz.app.add_data_to_viewer('imviz-0', 'table')\n",
"\n",
"# link the table {ra, dec} columns to the image {Right Ascension, Declination} coordinates:\n",
"_link_new_data(\n",
" imviz.app\n",
")"
]
},
{
"cell_type": "markdown",
"id": "03041ec7-d426-4008-b9b8-77ddc1a88be3",
"metadata": {},
"source": [
"Add a composite subset on young stars in the disk:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "88ebf00c-372d-4b0e-8a6a-22652ae52336",
"metadata": {},
"outputs": [],
"source": [
"rois = [\n",
" EllipticalROI(1040, 1000, 300, 1200, theta=np.radians(37)),\n",
" EllipticalROI(1040, 1000, 300 / 2, 1200 / 2, theta=np.radians(37)),\n",
"]\n",
"\n",
"subset_tools = imviz.plugins['Subset Tools']\n",
"subset_tools.import_region(rois, combination_mode=['new', 'andnot'])"
]
},
{
"cell_type": "markdown",
"id": "7dfb08db-684b-47bc-acbb-2fe938942539",
"metadata": {},
"source": [
"Extract a `DataFrame` with only the sources in the spatial subset:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9100ab9e-736a-4b26-b396-3e358003cbb5",
"metadata": {},
"outputs": [],
"source": [
"\n",
"table_subset_rows = imviz.get_data(\n",
" data_label='table', \n",
"\n",
" # if you don't specify `cls`, jdaviz will incorrectly assume that you\n",
" # want to create a Spectrum1D from the result, but you can't:\n",
" cls=pd.DataFrame, \n",
"\n",
" # extract the spatial coordinates within the subset:\n",
" spatial_subset='Subset 1'\n",
")\n",
"\n",
"table_subset_rows"
]
},
{
"cell_type": "markdown",
"id": "94847621-d069-4e55-a0b0-186d95fd3883",
"metadata": {},
"source": [
"Compare the CMD in the subset with all sources:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c77818a9-31ac-4cd5-8d75-585173cd7534",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"fig, ax = plt.subplots()\n",
"\n",
"ax.plot(\n",
" result['BP-RP'], result['Gmag'], '.'\n",
")\n",
"ax.plot(\n",
" table_subset_rows['BP-RP'], table_subset_rows['Gmag'], '.'\n",
")\n",
"ax.invert_yaxis()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80887b25-dcc4-4307-b2b1-475f2d999a47",
"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.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment