Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bmorris3/5ad095d9edeceb1720429be02bb273ab to your computer and use it in GitHub Desktop.
Save bmorris3/5ad095d9edeceb1720429be02bb273ab to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "091f1aa5-997c-4860-88e8-15b550db388c",
"metadata": {},
"source": [
"# Interacting with Roman L2 Images in Imviz on RSP\n",
"\n",
"Brett Morris\n",
"\n",
"June 27, 2024"
]
},
{
"cell_type": "markdown",
"id": "11b201b9-2b67-41fe-82f5-9af3ca67f7e4",
"metadata": {},
"source": [
"Let's load an Level 2 image:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "51bcf6f6-4786-40ec-b9b0-23f3e4868d48",
"metadata": {},
"outputs": [],
"source": [
"import s3fs\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import astropy.units as u\n",
"from astropy.coordinates import SkyCoord\n",
"from astropy.table import Table\n",
"import roman_datamodels as rdm\n",
"\n",
"from jdaviz import Imviz\n",
"\n",
"asdf_dir_uri = 's3://roman-sci-test-data-prod-summer-beta-test/'\n",
"fs = s3fs.S3FileSystem()\n",
"\n",
"asdf_file_uri = asdf_dir_uri + 'ROMANISIM/DENSE_REGION/R0.5_DP0.5_PA0/r0000101001001001001_01101_0001_WFI16_cal.asdf'"
]
},
{
"cell_type": "markdown",
"id": "95c4f8af-76d4-4f18-88a6-5652bbfe7ad5",
"metadata": {},
"source": [
"Let's load the data extension, and the DQ exension (turn off its visibility at first):"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "870bcc25-36b3-40c6-a5d6-1d13e819422d",
"metadata": {},
"outputs": [],
"source": [
"imviz = Imviz()\n",
"\n",
"# load the data into Imviz:\n",
"with fs.open(asdf_file_uri, 'rb') as f:\n",
" file = rdm.open(f) \n",
" imviz.load_data(file, ext=('data', 'dq'))\n",
"\n",
"# Turn off the visibility of the DQ arrays, to start:\n",
"dq = imviz.plugins['Data Quality']\n",
"for data in imviz.app.data_collection:\n",
" if data.label.endswith('[DQ]'):\n",
" imviz.app.set_data_visibility('imviz-0', data.label, visible=False)\n",
"\n",
"# use better-than-default colormaps for these data:\n",
"plot_options = imviz.plugins['Plot Options']\n",
"\n",
"for layer in plot_options.layer.choices:\n",
" plot_options.layer = layer\n",
" plot_options.stretch_function = 'Logarithmic'\n",
" plot_options.stretch_vmin = 0\n",
" plot_options.stretch_vmax = 2000\n",
" plot_options.image_colormap = 'Reversed: Gray'\n",
"\n",
"imviz.show('sidecar:split-right', height=1000)"
]
},
{
"cell_type": "markdown",
"id": "f9459fcb-19c7-48a8-b988-7dc9294ee6c7",
"metadata": {},
"source": [
"This synthetic image was generated from a synthetic source catalog. Let's get the source catalog:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27ac0a22-302c-4e11-85fb-4ee452dd98c1",
"metadata": {},
"outputs": [],
"source": [
"catalog_uri = asdf_dir_uri + 'ROMANISIM/CATALOGS_SCRIPTS/fullcat_101M_pared_ra0.50_dec0.50_WFI16.ecsv'\n",
"\n",
"# load the catalog file\n",
"with fs.open(catalog_uri, 'rb') as f:\n",
" catalog = Table.read(f, format='ascii.ecsv')"
]
},
{
"cell_type": "markdown",
"id": "b0e2d146-7dc5-4aa2-8ae9-5b6f1af7063f",
"metadata": {},
"source": [
"Let's add a column to the catalog table containing sky coordinates for each source:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64f39f34-9c45-4ae8-8704-0ca1ee360906",
"metadata": {},
"outputs": [],
"source": [
"catalog_coords = SkyCoord(ra=catalog['ra'], dec=catalog['dec'], unit=u.deg)\n",
"catalog['coord'] = catalog_coords"
]
},
{
"cell_type": "markdown",
"id": "6711f5a0-71bf-430a-bbb1-52a5b8f6d4b6",
"metadata": {},
"source": [
"In a moment, we'll plot all sources brighter than some flux threshold. Here we define that threshold by comparing against all the fluxes in the catalog:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e61532d-9bee-466f-8768-79dca3e21a67",
"metadata": {},
"outputs": [],
"source": [
"threshold_F158 = 1e-7\n",
"plt.hist(np.log10(catalog['F158']), bins=100)\n",
"plt.axvspan(\n",
" plt.gca().get_xlim()[0], \n",
" np.log10(threshold_F158), \n",
" color='k', alpha=0.2\n",
")\n",
"plt.axvline(np.log10(threshold_F158), color='r')\n",
"plt.show()\n",
"\n",
"print(f\"Number of sources to mark: {np.count_nonzero(catalog['F158'] > threshold_F158)}\")"
]
},
{
"cell_type": "markdown",
"id": "d1970f10-230f-4ed3-b774-e16e45228956",
"metadata": {},
"source": [
"This function will create the markers for any catalog entries that meet `catalog_criteria`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4c8ecc6-c6c7-40a5-bf41-f00fc507b726",
"metadata": {},
"outputs": [],
"source": [
"def add_markers(catalog_criteria):\n",
" imviz.default_viewer.add_markers(\n",
" catalog[catalog_criteria], \n",
" use_skycoord=True\n",
" )\n",
" \n",
" # set markers as big, red, unfilled circles:\n",
" marker_layer_state = imviz.default_viewer._obj.layers[-1].state\n",
" marker_layer_state.size = 100\n",
" marker_layer_state.fill = False\n",
" marker_layer_state.color = 'r'\n",
" \n",
" # when there are tons of scatter markers, they get automatically \n",
" # re-rendered as a density map. here we turn that off:\n",
" marker_layer_state.density_map = False"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e62e972-8fd2-41eb-8457-e079800fc57e",
"metadata": {},
"outputs": [],
"source": [
"# add markers from the catalog brighter than the threshold:\n",
"add_markers(catalog['F158'] > threshold_F158)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1375d416-d2b9-4855-b3f3-f010af86016b",
"metadata": {},
"outputs": [],
"source": [
"# mark only the bright galaxies:\n",
"criteria = (\n",
" (catalog['type'] == 'SER') & \n",
" (catalog['F184'] > 1e-8)\n",
")\n",
"add_markers(criteria)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7f879e3-2e66-47eb-9c14-9a76aec895b4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Roman Calibration latest (2024-03-25)",
"language": "python",
"name": "roman-cal"
},
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment