-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notebook to make RGB images with Imviz (#118)
* Notebook to make RGB images with Imviz * [BOT] Left PEP8 feedback on PR 118's notebooks Files: * Address PEP8, add batch loading --------- Co-authored-by: Camilla Pacifici <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
9c65022
commit fe0c0d5
Showing
2 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,343 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "035a23fd", | ||
"metadata": {}, | ||
"source": [ | ||
"# RGB images with Imviz\n", | ||
"\n", | ||
"**Use case**: create RGB images using Imviz programmatically.<br>\n", | ||
"**Data**: Carina Nebula, NIRCam observations<br>\n", | ||
"**Tools**: jdaviz<br>\n", | ||
"**Cross-intrument**: Any imager<br>\n", | ||
"**Documentation**: This notebook is part of a STScI's larger [post-pipeline Data Analysis Tools Ecosystem](https://jwst-docs.stsci.edu/jwst-post-pipeline-data-analysis).<br>\n", | ||
"\n", | ||
"For additional resources, please review our provided full example notebooks maintained in the Jdaviz repository: https://github.com/spacetelescope/jdaviz/tree/main/notebooks" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "884e8432", | ||
"metadata": {}, | ||
"source": [ | ||
"Import modules needed for this notebook:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8f2d1e37", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import warnings\n", | ||
"\n", | ||
"from astroquery.mast import Observations\n", | ||
"\n", | ||
"from jdaviz import Imviz" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "aaf960cd", | ||
"metadata": {}, | ||
"source": [ | ||
"We create an Imviz instance and grab the default viewer instance as well:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "409eedc4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"imviz = Imviz()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3a332f61", | ||
"metadata": {}, | ||
"source": [ | ||
"Let's download some data. The images are quite large. The download will take a few minutes." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7bddbc18", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data_dir = '.'\n", | ||
"\n", | ||
"files = ['jw02731-o001_t017_nircam_clear-f090w_i2d.fits',\n", | ||
" 'jw02731-o001_t017_nircam_clear-f187n_i2d.fits',\n", | ||
" 'jw02731-o001_t017_nircam_clear-f200w_i2d.fits',\n", | ||
" 'jw02731-o001_t017_nircam_clear-f335m_i2d.fits',\n", | ||
" 'jw02731-o001_t017_nircam_clear-f444w_i2d.fits',\n", | ||
" 'jw02731-o001_t017_nircam_f444w-f470n_i2d.fits']\n", | ||
"\n", | ||
"for fn in files:\n", | ||
" uri = f\"mast:JWST/product/{fn}\"\n", | ||
" result = Observations.download_file(uri, local_path=f'{data_dir}/{fn}')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e6108b52", | ||
"metadata": {}, | ||
"source": [ | ||
"And load into Imviz." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1135ef74", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"with warnings.catch_warnings():\n", | ||
" warnings.simplefilter('ignore')\n", | ||
" with imviz.batch_load():\n", | ||
" for fn in files:\n", | ||
" imviz.load_data(f'{data_dir}/{fn}', data_label=fn[31:36])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "8a4b2e1a", | ||
"metadata": {}, | ||
"source": [ | ||
"Now, we can display Imviz." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8b754faa", | ||
"metadata": { | ||
"scrolled": false | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"imviz.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "88b7129e", | ||
"metadata": {}, | ||
"source": [ | ||
"## WCS Linking Controls\n", | ||
"https://jdaviz.readthedocs.io/en/latest/imviz/plugins.html#link-control\n", | ||
"\n", | ||
"The first thing you will probably notice is that the image doesn't take up the entire viewer area. If you press the \"b\" key to blink to the next image, you will see that this image is zoomed correctly by default. The odd default zoom on the other is because the images are linked by pixel when loaded. We can instead link by WCS (world coordinates) so that the images will be properly aligned." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b34dde0c", | ||
"metadata": {}, | ||
"source": [ | ||
"There are two ways to link in Imviz. Firstly, you can do it from within the UI through the Links Control plugin by selecting \"WCS\". Either open the plugin tray to the right and search for \"Links Control\", or execute the following cell to open it automatically:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "812bbbf5", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"imviz.plugins['Links Control'].open_in_tray()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "75367551", | ||
"metadata": {}, | ||
"source": [ | ||
"You can also set the link type from the API using the Imviz Helper:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "f7c9f085", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Link images by WCS (without affine approximation)\n", | ||
"imviz.plugins['Links Control'].link_type = 'WCS'\n", | ||
"imviz.plugins['Links Control'].wcs_use_affine = False" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c9e777a4", | ||
"metadata": {}, | ||
"source": [ | ||
"Now if you blink, the images should be aligned" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "280cb7c7", | ||
"metadata": {}, | ||
"source": [ | ||
"## Plot Options\n", | ||
"The Plot Options plugins allows you to modify how your image appears. Here, we demonstrate how to use it to create RGB images." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "ca62dbf5", | ||
"metadata": {}, | ||
"source": [ | ||
"To show the Plot Options plugin, either search in the plugin bar, or execute the following cell to open it automatically:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "6706967c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_options = imviz.plugins['Plot Options']\n", | ||
"plot_options.open_in_tray()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "3e1285e7", | ||
"metadata": {}, | ||
"source": [ | ||
"Here you can adjust the color, opacity, contrast, and bias among other things. You can adjust them manually via the UI, or programmatically via the API to make image appear as you'd like:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c2211739", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plot_options.image_color_mode = 'Monochromatic'\n", | ||
"img_settings = {'f090w': {'image_color': '#cc7ace',\n", | ||
" 'stretch_vmax': 4,\n", | ||
" 'image_opacity': 0.32,\n", | ||
" 'image_contrast': 0.69,\n", | ||
" 'image_bias': 0.39},\n", | ||
" 'f187n': {'image_color': '#66cfef',\n", | ||
" 'stretch_vmin': 0,\n", | ||
" 'stretch_vmax': 16,\n", | ||
" 'image_opacity': 0.4,\n", | ||
" 'image_contrast': 0.94,\n", | ||
" 'image_bias': 0.74},\n", | ||
" 'f200w': {'image_color': '#61d3e1',\n", | ||
" 'stretch_vmax': 6,\n", | ||
" 'image_opacity': 0.31,\n", | ||
" 'image_contrast': 2.15,\n", | ||
" 'image_bias': 0.66},\n", | ||
" 'f335m': {'image_color': '#afff6b',\n", | ||
" 'stretch_vmin': 4,\n", | ||
" 'stretch_vmax': 27,\n", | ||
" 'image_opacity': 0.23,\n", | ||
" 'image_contrast': 3.13,\n", | ||
" 'image_bias': 0.67},\n", | ||
" 'f444w': {'image_color': '#ff767c',\n", | ||
" 'stretch_vmax': 10,\n", | ||
" 'image_opacity': 0.18,\n", | ||
" 'image_contrast': 1.77,\n", | ||
" 'image_bias': 0.56},\n", | ||
" 'f470n': {'image_color': '#f7787d',\n", | ||
" 'stretch_vmin': 1,\n", | ||
" 'stretch_vmax': 11,\n", | ||
" 'image_opacity': 0.4,\n", | ||
" 'image_contrast': 2.53,\n", | ||
" 'image_bias': 0.27}}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "78207a91", | ||
"metadata": {}, | ||
"source": [ | ||
"We can now iterate over this dictionary of settings and use the plot options API to set each of the options." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "c342cd9d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"for layer, settings in img_settings.items():\n", | ||
" plot_options.layer = f'{layer}[DATA]'\n", | ||
" for k, v in settings.items():\n", | ||
" setattr(plot_options, k, v)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "b1ac3c3f", | ||
"metadata": {}, | ||
"source": [ | ||
"And lastly, we can save to an image using the export plot plugin." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "fce20b3d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"imviz.plugins['Export Plot'].save_figure('carina_rgb.png')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "95de69d3", | ||
"metadata": {}, | ||
"source": [ | ||
"<img style=\"float: right;\" src=\"https://raw.githubusercontent.com/spacetelescope/notebooks/master/assets/stsci_pri_combo_mark_horizonal_white_bkgd.png\" alt=\"Space Telescope Logo\" width=\"200px\"/>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "c08db0ba", | ||
"metadata": {}, | ||
"source": [ | ||
"Notebook created by Kyle Conroy and adapted by Duy Nguyen and Camilla Pacifici." | ||
] | ||
} | ||
], | ||
"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.10.8" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
jdaviz |