From af98f80a813ed14437da558dfcc2152b1d7ca448 Mon Sep 17 00:00:00 2001 From: Ori Date: Wed, 15 Sep 2021 15:17:33 -0400 Subject: [PATCH] addressed some science and technical reviews --- .../NGC4151_FeII_ContinuumFit.ipynb | 196 +++++++++--------- .../IFU_cube_continuum_fit/requirements.txt | 12 +- 2 files changed, 101 insertions(+), 107 deletions(-) diff --git a/jdat_notebooks/IFU_cube_continuum_fit/NGC4151_FeII_ContinuumFit.ipynb b/jdat_notebooks/IFU_cube_continuum_fit/NGC4151_FeII_ContinuumFit.ipynb index 635dc182..f3c76034 100644 --- a/jdat_notebooks/IFU_cube_continuum_fit/NGC4151_FeII_ContinuumFit.ipynb +++ b/jdat_notebooks/IFU_cube_continuum_fit/NGC4151_FeII_ContinuumFit.ipynb @@ -195,27 +195,6 @@ "plt.show()" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "print(wave)\n", - "# print(wave-wave_emission_limit1)\n", - "# print(continuummin,continuummax)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - " For this particular dataset, this continuum region looks very good.\n", - " if you have a more structured continuum you can define additional\n", - " regions and append them into a larger set of wave / flux arrays to \n", - " derive a more accurate fit in the below poly-fit analysis." - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -230,7 +209,7 @@ "source": [ "### Video1: \n", "\n", - "YouTube Demo" + "This Cubeviz Instructional Demo is from STScI's official YouTube channel and provides an introduction to Cubeviz." ] }, { @@ -269,22 +248,26 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Defining your data sets\n", - "\n", "A video is shown below illustrating the procedure. The following steps are applied:\n", "\n", - "Use the flux viewer in cubeviz to extract a spectrum located at the central AGN position.\n", - "It should show in the above spectral viewer, too.\n", + "When you load your cube, you will see a collapsed spectrum of all the spaxels in the spectral viewer at the bottom.\n", "\n", - "To do this, use the expandable menu at the left of the flux viewer window and select the 'define circular region of interest' icon. Make a circular region at the central position of the bright AGN flux, which is at approximately the cube center position.\n", + "If you draw a region (circle or square) in the flux viewer, you will see a collapsed spectrum of that particular region in the spectral viewer, too. To do this, use the expandable menu at the left of the flux viewer window and select the 'define circular region of interest' icon. For this example, we want to first define a circular region at the central position of the bright AGN flux, which is at approximately the cube center position.\n", "\n", - "Now, use the flux viewer and again use the 'define circular region of interest' icon to make spectra at two positions associated with the outflow emission in [Fe II].\n", + "Now, use the flux viewer and again use the 'define circular region of interest' icon to make spectra at two positions associated with the outflow emission in [Fe II]. The redshifted outflow is at approximate x position = 12, y position = 36. This will be 'Subset 2' and will show up in green in the display. The blueshifted outflow is at approximately x position = 50, y position = 28 in pixel index units. This will be 'Subset 3' and will show up in blue in the display.\n", "\n", - "The redshifted outflow is at approximate x position = 12, y position = 36. This will be 'Subset 2' and will show up in green in the display.\n", + "(If the notebook is being run non-interactively, automatically make two datasets that mimic the AGN outflow red/blueshifted spectra)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Defining your Spectral Regions\n", "\n", - "The blueshifted outflow is at approximately x position = 50, y position = 28 in pixel index units. This will be 'Subset 3' and will show up in blue in the display.\n", + "Next, you will want to define the wavelengths of interest in your spectral viewer for both your line and continuum analysis. To do this, you will similarly click the 'define region of interest' icon in your spectral viewer and drag a box over the wavelengths you desire. Again, a video is show below illustrating the process.\n", "\n", - "(If the notebook is being run non-interactively, automatically make two datasets that mimic the AGN outflow red/blueshifted spectra)." + "There is no option to set the spectral regions to user input, so we recommend zooming in and drawing by eye. The line emission (;Subset 4' ) should span approximately 1.630 - 1.665 um, and the continuum emission ('Subset 5') should span approximately 1.656 - 1.673 um." ] }, { @@ -304,7 +287,7 @@ "source": [ "### Video2: \n", "\n", - "Here is a video illustrating how to load and manipulate data for this particular notebook." + "Here is a video illustrating how to load and manipulate data for this particular notebook. A description of how to manipulate the data and use the \"Collapse\" tool is given above." ] }, { @@ -376,39 +359,9 @@ "spectrum1 = cubeviz.app.get_data_from_viewer('spectrum-viewer', 'Subset 1') # AGN Center\n", "spectrum2 = cubeviz.app.get_data_from_viewer('spectrum-viewer', 'Subset 2') # Red shifted component\n", "spectrum3 = cubeviz.app.get_data_from_viewer('spectrum-viewer', 'Subset 3') # Blue shifted component\n", - "# spectrum3 = cubeviz.app.get_data_from_viewer('spectrum-viewer')['Subset 3'] # Blue shifted component\n", "spectrum1" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# regions = cubeviz.specviz.get_spectral_regions()\n", - "# regions" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# regions = cubeviz.app.get_data_from_viewer('flux-viewer')['Subset 4'] #AGN Center\n", - "# regions" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# regions = cubeviz.app.get" - ] - }, { "cell_type": "code", "execution_count": null, @@ -445,7 +398,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Apply the spectral region\n", + "#Apply the spectral region (This code creates new collapsed spectra if you did not create any yourself within jdaviz)\n", "from specutils.manipulation import extract_region\n", "\n", "if not spectrum1:\n", @@ -505,10 +458,11 @@ "source": [ "## Fit the Continuum at the Spectral Region Location\n", "\n", - "A video is shown below illustrating the procedure. The following steps are applied to this particular example:
\n", + "A video is shown below illustrating the procedure for fitting the continuum for 'Subset 5' throughout the entire cube. The following steps are applied to this particular example:
\n", + "\n", + "Open up Model Fitting Plugin. There are a number of fields to fill in and drop down menus to select from. It is important to keep in mind that the Data menu will provide only spectra to model, while the Spectral Region menu will provide only spectral region subsets to choose. In other words, you can fit the spectra in specific spectral regions. If no spectral region is selected, the entire wavelength array will be fit by the mode.
\n", "\n", - "Open up Model Fitting Plugin.
\n", - "Select Data Cube contentsSCI
\n", + "Select Data Cube: contentsSCI
\n", "Select Spectral Region Subset 5
\n", "Model: Linear1D
\n", "ModelID: L1
\n", @@ -516,7 +470,14 @@ "Model Equation Editor: L1
\n", "Model Label: LinFitCont
\n", "
\n", - "Hit Fit, and then Apply to Cube." + "Hit Fit, which fits the collapsed spectrum.
\n", + "View the fit in the spectral viewer and confirm you are happy with it.
\n", + "Then hit Apply to Cube.
\n", + "\n", + "This will create two models that can now be accessed within the Data Dropdown menus:
\n", + "A 1D linear fit of the continuum for the collapsed cube.
\n", + "A 3D linear fit of the continuum for each spaxel in the cube.
\n", + "\n" ] }, { @@ -641,13 +602,10 @@ " sci = cont_sub_cube['SCI'].data\n", "\n", " # Get List of different viewers\n", - " # linfitcube = cubeviz.app.get_data_from_viewer(\"uncert-viewer\", \"LinFitCube [Cube] 1\")\n", " continuumflux = cont_psf_cube[\"flux\"]\n", "\n", " sci_contsub = sci-continuumflux\n", " cont_sub_cube['SCI'].data = sci_contsub \n", - " # cubeviz.app.load_data(newcube)\n", - " # del newheader_cube['MODE']\n", " del cont_sub_cube['PRIMARY'].header['MODE']\n", " cont_sub_cube.writeto('NGC4151_Hband_ContinuumSubtract.fits')\n", " del newheader_cube['MODE']\n", @@ -666,13 +624,12 @@ ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "# And this is just an example of how you can acces the model fit parameter values\n", - "# params['LinFitCont_3d']['slope']" + "And this is just an example of how you can acces the model fit parameter values:\n", + "\n", + "params['LinFitCont_3d']['slope']" ] }, { @@ -707,8 +664,11 @@ "\n", "A video is shown below illustrating the procedure. The following steps are applied:
\n", "\n", + "First, select the wavelength region of interest following a similar procedure as performed at the top. There is no option to set the spectral regions to a user input, so we recommend zooming in and drawing by eye. The line emission ('Subset 1' ) should again span approximately 1.630 - 1.665 um.\n", + "\n", "For this example, we recommend setting up a 3 component gaussian model with the following inputs
\n", - "Open up Model Fitting Plugin.
\n", + "Open up Model Fitting Plugin. There are a number of fields to fill in and drop down menus to select from. It is important to keep in mind that the Data menu will provide only spectra to model, while the Spectral Region menu will provide only spectral region subsets to choose. In other words, you can fit the spectra in specific spectral regions. If no spectral region is selected, the entire wavelength array will be fit by the mode.
\n", + "\n", "Data: Continuum Subtracted
\n", "Spectral region or subset covering about 1.632 to 1.656 um
\n", "Model: Three different Gaussians with ModelID's set to G1, G2, and G3
\n", @@ -717,11 +677,18 @@ "G1: stdev=0.0008E-6, mean=1.641E-6
\n", "G2: stdev=0.0007E-6, mean=1.648E-6
\n", "G3: stdev=0.005E-6, mean=1.646E-6
\n", + "You can turn on the 'Fixed' option if you need to, but these numbers should provide a good starting guess for the fit.
\n", "\n", "Model Equation Editor: G1+G2+g3
\n", "Model Label: GaussAll
\n", "
\n", - "Hit Fit, and then Apply to Cube." + "Hit Fit, which fits the collapsed spectrum.
\n", + "View the fit in the spectral viewer and confirm you are happy with it. Modify if necessary.
\n", + "Then hit Apply to Cube.
\n", + "\n", + "This will again create two models that can now be accessed within the Data Dropdown menus:
\n", + "A 1D linear fit of the lines in the collapsed cube.
\n", + "A 3D linear fit of the lines for each spaxel in the cube.
" ] }, { @@ -794,26 +761,32 @@ "outputs": [], "source": [ "# Save your Gauss Model Cube If Necessary\n", - "# import os\n", - "# if os.path.exists(\"gauss_model_cube.fits\"):\n", - "# os.remove(\"gauss_model_cube.fits\")\n", - "# else:\n", - "# print(\"The file does not exist\")\n", + "import os\n", + "if os.path.exists(\"gauss_model_cube.fits\"):\n", + " os.remove(\"gauss_model_cube.fits\")\n", + "else:\n", + " print(\"The file does not exist\")\n", "\n", - "# gaussmodelflux = gauss_cube[\"flux\"]\n", - "# fits.writeto('gauss_model_cube.fits', gaussmodelflux, overwrite=True)\n", - "# print(type(gauss_cube[\"flux\"]))\n", - "# print(type(all_spec.flux))\n", + "gaussmodelflux = gauss_cube[\"flux\"]\n", + "fits.writeto('gauss_model_cube.fits', gaussmodelflux, overwrite=True)\n", + "print(type(gauss_cube[\"flux\"]))\n", + "print(type(all_spec.flux))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Developer Note: Saving with 'Count' doesn't work.\n", "\n", - "# Saving with 'Count' doesn't work.\n", - "# print(all_spec.flux)\n", - "# os.remove(\"all_spec.fits\")\n", - "# specref=Spectrum1D(flux=all_spec.flux*u.Unit('Jy'), spectral_axis=all_spec.spectral_axis)\n", - "# specref.write(\"all_spec.fits\")\n", + "print(all_spec.flux)
\n", + "os.remove(\"all_spec.fits\")
\n", + "specref=Spectrum1D(flux=all_spec.flux*u.Unit('Jy'), spectral_axis=all_spec.spectral_axis)
\n", + "specref.write(\"all_spec.fits\")
\n", "\n", - "# os.remove(\"gauss_spec.fits\")\n", - "# specref=Spectrum1D(flux=gauss_spec.flux*u.Unit('Jy'), spectral_axis=gauss_spec.spectral_axis)\n", - "# specref.write(\"gauss_spec.fits\")" + "os.remove(\"gauss_spec.fits\")
\n", + "specref=Spectrum1D(flux=gauss_spec.flux*u.Unit('Jy'), spectral_axis=gauss_spec.spectral_axis)
\n", + "specref.write(\"gauss_spec.fits\")
\n" ] }, { @@ -856,8 +829,8 @@ "outputs": [], "source": [ "# Save Parameters as a Pickle File if necessary\n", - "# save_obj(params, \"gauss_params.pkl\")\n", - "# params=load_obj(\"gauss_params.pkl\")" + "save_obj(params, \"gauss_params.pkl\")\n", + "params=load_obj(\"gauss_params.pkl\")" ] }, { @@ -907,7 +880,6 @@ "newfull_header = fits.getheader(continuum_file)\n", "\n", "with fits.open(continuum_file, memmap=False) as continuum_cube: \n", - " # print(continuum_cube[0].data)\n", " continuum_data = continuum_cube[0].data\n", " full_model = gauss_cube_2component+continuum_data" ] @@ -966,7 +938,7 @@ "metadata": {}, "outputs": [], "source": [ - "# Make the final plots to illustrated\n", + "# Make the final plots to illustrate the original spectrum, model fits, and final continuum+gassian subtracted cube\n", "plt.figure()\n", "plt.xlim([1.630E-6, 1.665E-6])\n", "plt.ylim([600, 900])\n", @@ -977,13 +949,35 @@ "plt.legend()\n", "plt.show()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "#Open up a new instance of Cubeviz to visualize continuum subtracted data\n", + "from jdaviz import CubeViz\n", + "cubeviz3 = CubeViz()\n", + "cubeviz3.app" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "cont_sub_cube = 'NGC4151_Hband_FinalSubtract.fits'\n", + "cubeviz3.app.load_data(cont_sub_cube, data_label='Red/Blue Shift')" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python [conda env:dat-ifu-cont-adv] *", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "conda-env-dat-ifu-cont-adv-py" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -995,7 +989,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.9.6" } }, "nbformat": 4, diff --git a/jdat_notebooks/IFU_cube_continuum_fit/requirements.txt b/jdat_notebooks/IFU_cube_continuum_fit/requirements.txt index cbfc4525..669df510 100644 --- a/jdat_notebooks/IFU_cube_continuum_fit/requirements.txt +++ b/jdat_notebooks/IFU_cube_continuum_fit/requirements.txt @@ -1,7 +1,7 @@ -astropy>=4.1 -specutils>=1.0 -matplotlib>=3.2 +astropy>=4.3.1 +specutils>=1.4.0 +matplotlib>=3.4.3 Bottleneck>=1.3.2 -ipyvue>=1.3.4 -ipyvuetify>=1.4.1 -git+https://github.com/spacetelescope/jdaviz.git@625acca8562249c2f4d9041c566c1820a4d480b2#egg=jdaviz +ipyvue>=1.5.0 +ipyvuetify>=1.8.1 +git+https://github.com/spacetelescope/jdaviz.git@main