From 7635934740e061075b0c7ccb1220605a496086ff Mon Sep 17 00:00:00 2001 From: Camilla Pacifici Date: Wed, 11 Oct 2023 15:39:15 -0400 Subject: [PATCH] Update to new dataset --- .../pre-install.sh | 6 - .../requirements.txt | 2 +- ...iz_notebook_gui_interaction_redshift.ipynb | 117 +++++++----------- 3 files changed, 45 insertions(+), 80 deletions(-) delete mode 100644 notebooks/specviz_notebookGUI_interaction/pre-install.sh diff --git a/notebooks/specviz_notebookGUI_interaction/pre-install.sh b/notebooks/specviz_notebookGUI_interaction/pre-install.sh deleted file mode 100644 index b70768e33..000000000 --- a/notebooks/specviz_notebookGUI_interaction/pre-install.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -source bin/activate -pip install astropy-helpers==2.0.11 -pip install radio_beam==0.3.2 -pip install git+https://github.com/spacetelescope/jdaviz.git@625acca8562249c2f4d9041c566c1820a4d480b2#egg=jdaviz diff --git a/notebooks/specviz_notebookGUI_interaction/requirements.txt b/notebooks/specviz_notebookGUI_interaction/requirements.txt index 163ea3828..0f29b127b 100644 --- a/notebooks/specviz_notebookGUI_interaction/requirements.txt +++ b/notebooks/specviz_notebookGUI_interaction/requirements.txt @@ -1 +1 @@ -specutils +jdaviz diff --git a/notebooks/specviz_notebookGUI_interaction/specviz_notebook_gui_interaction_redshift.ipynb b/notebooks/specviz_notebookGUI_interaction/specviz_notebook_gui_interaction_redshift.ipynb index 862f55197..06cb82d24 100644 --- a/notebooks/specviz_notebookGUI_interaction/specviz_notebook_gui_interaction_redshift.ipynb +++ b/notebooks/specviz_notebookGUI_interaction/specviz_notebook_gui_interaction_redshift.ipynb @@ -12,7 +12,7 @@ "metadata": {}, "source": [ "**Use case:** This notebook demonstrates how to inspect spectra in Specviz, export spectra from the GUI in the notebook, select regions in the GUI and in the notebook, and measure the redshift of a source in the GUI.
\n", - "**Data:** NIRISS 1D Simulated Spectrum. The dataset is a JWST/NIRISS simulation of a generic scene with extended sources generated with the code MIRAGE (https://mirage-data-simulator.readthedocs.io/en/latest/) and run through the JWST calibration pipeline (https://jwst-pipeline.readthedocs.io/en/latest/).
\n", + "**Data:** NIRISS 1D spectra from the [NGDEEP survey](https://ui.adsabs.harvard.edu/abs/2023arXiv230205466B/abstract). The dataset is directly obtain from MAST after the default JWST pipeline processing.
\n", "**Tools:** specutils, jdaviz.
\n", "**Cross-intrument:** all instruments.
\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).
" @@ -22,7 +22,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Updated on**: 2021/3/4" + "**Updated on**: 2023/10/11" ] }, { @@ -34,7 +34,7 @@ "outputs": [], "source": [ "#to use 100% of the browser window\n", - "from IPython.core.display import display, HTML\n", + "from IPython.display import display, HTML\n", "display(HTML(\"\"))" ] }, @@ -74,7 +74,7 @@ "\n", "#import viztools\n", "import jdaviz\n", - "from jdaviz import Specviz" + "from jdaviz import Specviz, Mosviz" ] }, { @@ -119,7 +119,7 @@ "metadata": {}, "source": [ "## 1. Load NIRISS pipeline output\n", - "The JWST/NIRISS simulation is stored on box. We work with the x1d file which contains all extracted 1D spectra." + "The JWST/NIRISS data are stored on box. We work with the x1d file which contains all extracted 1D spectra." ] }, { @@ -130,7 +130,7 @@ }, "outputs": [], "source": [ - "filelink = 'https://data.science.stsci.edu/redirect/JWST/jwst-data_analysis_tools/specviz_notebook_gui_interaction/basic_F150W_WFSSR_dit1_x1d.fits'\n", + "filelink = 'https://data.science.stsci.edu/redirect/JWST/jwst-data_analysis_tools/specviz_notebook_gui_interaction/jw02079004002_11101_00001_nis_x1d.fits'\n", "hdu = fits.open(filelink)\n", "\n", "hdu.info()" @@ -140,17 +140,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 2. Open Specviz and load all the 1D spectra\n", - "Parsers are not ready for all instruments, so we load the spectrum manually and we write in a Spectrum1D." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "**Developer note:**\n", - "\n", - "We need a parser for NIRISS WFSS x1d.fits files." + "## 2. Open Specviz and load the 1D spectra we are interested in" ] }, { @@ -160,14 +150,14 @@ "outputs": [], "source": [ "viz = Specviz()\n", - "viz.app" + "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The following cell opens one-by-one the extensions of the x1d file, creates Spectrum1D objects for all the spectra, and loads them into Specviz. A mask is set to only keep the part of the spectra with good sensitivity (1.34 to 1.66 micron) in the F150W filter. The for loop runs from 2 to 19, because spectra 0 and 1 have very short extraction boxes and are not useful for this work (this can be seen in the dimentions of the extensions of the fits file). The spectra are also inverted. The pipeline extracts them in reverse wavelength order because the dispersion direction is from right to left on the detector." + "The following cell opens one extension of the x1d file (75), creates a Spectrum1D object, and loads it into Specviz. A mask is set to only keep the part of the spectra with good sensitivity (1.34 to 1.66 micron) in the F150W filter." ] }, { @@ -178,34 +168,18 @@ }, "outputs": [], "source": [ - "for i in range (2,19):\n", + "for i in range(74,75):\n", " spec_load = hdu[i+1].data\n", - "\n", - " #invert axis\n", " wave = spec_load['WAVELENGTH']\n", " flux = spec_load['FLUX']\n", - " error = spec_load['ERROR']\n", - " invwave = wave[::-1]\n", - " invflux = flux[::-1]\n", - " inverror = error[::-1]\n", - "\n", - " #mask the parts where the sensitivity in the bandpass is poor\n", - " mask = ((invwave>1.34) & (invwave<1.66))\n", - "\n", - " spec1d = Spectrum1D(spectral_axis=invwave[mask]*u.um, \n", - " flux=invflux[mask]*u.Jy, \n", - " uncertainty=StdDevUncertainty(inverror[mask]*u.Jy)) #\n", + " error = spec_load['FLUX_ERROR']\n", + " # mask the parts where the sensitivity in the bandpass is poor\n", + " mask = ((wave>1.34) & (wave<1.66))\n", + " spec1d = Spectrum1D(spectral_axis=wave[mask]*u.um, \n", + " flux=flux[mask]*u.Jy, \n", + " uncertainty=StdDevUncertainty(error[mask]*u.Jy)) #\n", " \n", - " viz.load_spectrum(spec1d,\"NIRISS 1D {}\".format(str(i+1)))\n", - "\n", - " #print(spec1d)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "This is just for quick look to see all the spectra that are in the file. We can go to the hammer and screwdriver icon, then the gear icon and select/deselect the spectra. We find our favorite spectrum (NIRISS ID 6) and work with that." + " viz.load_data(spec1d,\"NIRISS 1D {}\".format(str(i+1)))" ] }, { @@ -213,7 +187,7 @@ "metadata": {}, "source": [ "## 3. Select the emission lines using the GUI and in the notebook\n", - "I open the hammer and screwdriver menu and click the \"Select range of x values\" icon. Then I select the region spanning the emission lines from roughly 1.35 to 1.43 microns.\n", + "I select the region spanning the emission lines from roughly 1.58 to 1.63 microns.\n", "\n", "Instructions: https://jdaviz.readthedocs.io/en/latest/specviz/displaying.html#defining-spectral-regions" ] @@ -231,8 +205,8 @@ "metadata": {}, "outputs": [], "source": [ - "dataout = viz.get_spectra()\n", - "spec1d_line = dataout[\"NIRISS 1D 6\"]" + "dataout = viz.get_spectra(apply_slider_redshift=False)\n", + "spec1d_line = dataout[\"NIRISS 1D 75\"]" ] }, { @@ -272,7 +246,7 @@ "metadata": {}, "source": [ "### Select the same region programmatically\n", - "I can define my own region (cont_region) between arbitrary bounds. I choose 1.35um and 1.42um. I can then extract the spectrum in that region." + "I can define my own region (cont_region) between arbitrary bounds. I choose 1.598um and 1.621um. I can then extract the spectrum in that region." ] }, { @@ -283,7 +257,7 @@ }, "outputs": [], "source": [ - "cont_region = SpectralRegion(1.35*u.um,1.42*u.um)\n", + "cont_region = SpectralRegion(1.598*u.um,1.621*u.um)\n", "spec1d_el_code = extract_region(spec1d_line, cont_region)\n", "print(spec1d_el_code)" ] @@ -325,12 +299,13 @@ "metadata": {}, "outputs": [], "source": [ - "plt.plot(spec1d_line.spectral_axis,spec1d_line.flux)\n", - "plt.plot(spec1d_el_viz.spectral_axis,spec1d_el_viz.flux)\n", - "\n", + "plt.plot(spec1d_line.spectral_axis,spec1d_line.flux, label='data')\n", + "plt.plot(spec1d_el_viz.spectral_axis,spec1d_el_viz.flux, label='subset defined in tool')\n", + "plt.plot(spec1d_el_code.spectral_axis,spec1d_el_code.flux, label='subset defined in code')\n", + "plt.legend()\n", "plt.xlabel(\"wavelength ({:latex})\".format(spec1d_line.spectral_axis.unit))\n", "plt.ylabel(\"flux ({:latex})\".format(spec1d_line.flux.unit))\n", - "plt.title(\"NIRISS ID 6\")\n", + "plt.title(\"NIRISS ID 75\")\n", "plt.show()\n" ] }, @@ -355,7 +330,7 @@ "outputs": [], "source": [ "viz2 = Specviz()\n", - "viz2.app" + "viz2.show()" ] }, { @@ -371,14 +346,14 @@ "metadata": {}, "outputs": [], "source": [ - "viz2.load_spectrum(spec1d_line,\"NIRISS 1D lines\")" + "viz2.load_data(spec1d_line,\"NIRISS 1D lines\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "I can use an available line lists or define my own lines (I know I need Hb4861.3 and the [OIII]4958.9,5006.8 doublet) and play with the redshift slider to match the lines in the line list with the lines in the spectrum. The line list plugin can be found clicking the Lego icon on the upper right of the viewer. To input just the three lines, I can use the \"Custom\" menu.\n", + "I can use an available line lists or define my own lines (I know I need Hb4861.3 and the [OIII]4958.9,5006.8 doublet) and play with the redshift slider to match the lines in the line list with the lines in the spectrum. The line list plugin can be found clicking the plugin icon on the upper right of the viewer. To input just the three lines, I can use the \"Custom\" menu.\n", "\n", "Here is the documentation where line lists are explained: https://jdaviz.readthedocs.io/en/latest/specviz/plugins.html#line-lists\n", "\n", @@ -402,7 +377,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The lines are not showing now because their rest value is outside the range plotted here. I can move the lines using the redshift slider at the top. It is best to first set the redshift to 2 in the box with the number and then move the slider to bring the lines on top of the observed emission lines." + "The lines are not showing now because their rest value is outside the range plotted here. I can move the lines using the redshift slider in the line list plugin. It is best to first set the redshift to 2 in the box with the number and then move the slider to bring the lines on top of the observed emission lines." ] }, { @@ -418,7 +393,7 @@ "metadata": {}, "outputs": [], "source": [ - "spec1d_redshift = viz2.get_spectra()[\"NIRISS 1D lines\"]\n", + "spec1d_redshift = viz2.get_spectra(apply_slider_redshift=True)[\"NIRISS 1D lines\"]\n", "print(spec1d_redshift)\n", "\n", "print()\n", @@ -427,7 +402,7 @@ " print(\"NIRISS 1D lines redshift=\",spec1d_redshift.redshift)\n", "else:\n", " print(\"Redshift was not defined in GUI. Defining it here.\")\n", - " spec1d_redshift.redshift = 1.798\n", + " spec1d_redshift.set_redshift_to(2.2138)\n", " print(\"NIRISS 1D lines redshift=\",spec1d_redshift.redshift)" ] }, @@ -452,7 +427,7 @@ "outputs": [], "source": [ "viz3 = Specviz()\n", - "viz3.app" + "viz3.show()" ] }, { @@ -461,41 +436,33 @@ "metadata": {}, "outputs": [], "source": [ - "viz3.load_spectrum(spec1d_line,\"NIRISS 1D lines\")" + "viz3.load_data(spec1d_line,\"NIRISS 1D lines\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "I can use the GUI to select the region where I see the continuum. This is between 1.45 and 1.63 microns roughly. To do so, I go to the hammer and screwdriver icon and use the Select Region button." + "I can use the GUI to select the region where I see the continuum. _Challenge_: select a discontinuous subset that covers two intervals (1.35-1.55um and 1.63-1.65um). _Hint_: select \"Add\" at the top near the Subset dropdown." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "I can then use the Model Fitting plugin under the Lego icon to fit a linear model to the selected region. Instructions can be found here: https://jdaviz.readthedocs.io/en/latest/specviz/plugins.html#model-fitting. The individual steps to complete this task are:\n", + "I can then use the Model Fitting plugin under the plugin icon to fit a linear model to the selected region. Instructions can be found here: https://jdaviz.readthedocs.io/en/latest/specviz/plugins.html#model-fitting. The individual steps to complete this task are:\n", "- Select Subset 1 under Data\n", "- Select Linear1D under Model\n", - "- Enter a model ID under Model ID (I use \"lin\")\n", "- Click Add model\n", - "- Enter the needed combination of models under Model Equation Editor (it is just one model here, so I enter just \"lin\")\n", "- Enter a name for the model under Model Label (I choose \"continuum\")\n", - "- Click Fit\n", - "- Click Add to viewer\n", - "- Go to the hammer and screwdiver icon, then gear icon, and select the new dataset \"continuum\"." + "- Click Fit" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "I can extract the model from the datasets in use.\n", - "\n", - "**Developer Note:**\n", - "\n", - "I would like to be able to extract also the model parameters, not just the model spectrum. This is being worked on." + "I can extract the model and its parameters from the datasets in use." ] }, { @@ -508,6 +475,10 @@ " dataout3 = viz3.get_spectra()\n", " spectrum = dataout3[\"NIRISS 1D lines\"] #this is exactly the same as the spec1d_lines loaded a few cells above\n", " continuum = dataout3[\"continuum\"]\n", + " model_param = viz3.get_model_parameters()\n", + " print(continuum)\n", + " print(model_param['continuum'])\n", + " \n", "except:\n", " print(\"Continuum has not been created. Setting it to 0\")\n", " continuum = Spectrum1D(spectral_axis=spectrum.spectral_axis, flux= 0.*spectrum.flux)" @@ -568,7 +539,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.11.5" } }, "nbformat": 4,