Skip to content

Commit

Permalink
Merge pull request #67 from desihub/classes
Browse files Browse the repository at this point in the history
Classes
  • Loading branch information
armengau committed Feb 10, 2021
2 parents 7e00380 + ee8e009 commit b6775ce
Show file tree
Hide file tree
Showing 19 changed files with 2,311 additions and 1,913 deletions.
24 changes: 24 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ prospect API
.. automodule:: prospect
:members:

.. automodule:: prospect.grid_thumbs
:members:

.. automodule:: prospect.mycoaddcam
:members:

Expand Down Expand Up @@ -38,6 +41,9 @@ prospect API
.. automodule:: prospect.scripts.specview_cmx_targets
:members:

.. automodule:: prospect.scripts.specview_per_expo
:members:

.. automodule:: prospect.scripts.specview_per_night
:members:

Expand All @@ -49,3 +55,21 @@ prospect API

.. automodule:: prospect.utilities
:members:

.. automodule:: prospect.viewer
:members:

.. automodule:: prospect.viewer.cds
:members:

.. automodule:: prospect.viewer.plots
:members:

.. automodule:: prospect.viewer.layouts
:members:

.. automodule:: prospect.viewer.widgets
:members:

.. automodule:: prospect.viewer.vi_widgets
:members:
2 changes: 2 additions & 0 deletions doc/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ prospect's Change Log
* Optional countdown widget (PR `#58`_).
* Updated requests to legacysurvey (PR `#65`_)
* Merge of DESI and specutils code in :mod:`prospect.viewer` (PR `#66`_).
* Restructure :mod:`prospect.viewer` into classes (PR `#67`_).

.. _`#58`: https://github.com/desihub/prospect/pull/58
.. _`#65`: https://github.com/desihub/prospect/pull/65
.. _`#66`: https://github.com/desihub/prospect/pull/66
.. _`#67`: https://github.com/desihub/prospect/pull/67

1.0.1 (2021-01-07)
------------------
Expand Down
107 changes: 74 additions & 33 deletions doc/nb/Prospect_demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"\n",
"# Uncomment this line to use a specific version of prospect\n",
"sys.path.insert(0,\"/global/homes/a/armengau/prospect/py\")\n",
"from prospect import utilities,plotframes"
"from prospect import viewer,plotframes"
]
},
{
Expand All @@ -43,6 +43,20 @@
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO:spectra.py:253:read_spectra: iotime 0.991 sec to read coadd-3-70003-20200228.fits at 2021-02-09T12:44:03.833097\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:desiutil.log.dlm58.info:iotime 0.991 sec to read coadd-3-70003-20200228.fits at 2021-02-09T12:44:03.833097\n"
]
},
{
"name": "stdout",
"output_type": "stream",
Expand Down Expand Up @@ -96,48 +110,75 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function plotspectra in module prospect.plotframes:\n",
"Help on function plotspectra in module prospect.viewer:\n",
"\n",
"plotspectra(spectra, nspec=None, startspec=None, zcatalog=None, redrock_cat=None, num_approx_fits=None, with_full_2ndfit=True, model_from_zcat=True, model=None, notebook=False, is_coadded=True, title=None, html_dir=None, with_imaging=True, with_noise=True, with_coaddcam=True, mask_type='DESI_TARGET', with_thumb_tab=True, with_vi_widgets=True, with_thumb_only_page=False, template_dir=None, archetype_fit=False, archetypes_dir=None)\n",
"plotspectra(spectra, zcatalog=None, redrock_cat=None, notebook=False, html_dir=None, title=None, with_imaging=True, with_noise=True, with_thumb_tab=True, with_vi_widgets=True, vi_countdown=-1, with_thumb_only_page=False, is_coadded=True, with_coaddcam=True, mask_type='DESI_TARGET', model_from_zcat=True, model=None, num_approx_fits=None, with_full_2ndfit=True, template_dir=None, archetype_fit=False, archetypes_dir=None)\n",
" Main prospect routine. From a set of spectra, creates a bokeh document\n",
" used for VI, to be displayed as an HTML page or within a Jupyter notebook.\n",
" \n",
" Parameters\n",
" ----------\n",
" spectra: input spectra. Supported formats: 1) a 3-band DESI spectra object, with bands 'b', 'r', 'z'. 2) a single-band\n",
" DESI spectra object, bandname 'brz'. 2) a list of 3 frames, associated to the b, r and z bands.\n",
" zcatalog (default None): astropy Table, containing the 'ZBEST' output redrock. Currently supports redrock-PCA or archetype files. The entries in zcatalog must be matched one-by-one (in order) to spectra.\n",
" redrock_cat (default None): astropy Table, containing Redrock output (as defined in utilities.match_redrock_zfit_to_spectra). Entries must be matched one-by-one (in order) to spectra.\n",
" notebook (bool): if True, bokeh outputs the viewer to a notebook, else to a (static) HTML page\n",
" html_dir (string): directory to store the HTML page if notebook is False\n",
" title (string): title used to name the HTML page / the bokeh figure / the VI file\n",
" mask_type : mask type to identify target categories from the fibermap. Available : DESI_TARGET,\n",
" SV1_DESI_TARGET, CMX_TARGET. Default : DESI_TARGET.\n",
" with_vi_widgets (bool): include widgets used to enter VI informations. Set it to False if you do not intend to\n",
" record VI files.\n",
" with_thumb_tab (bool): include a tab with thumbnails of spectra in bokeh viewer\n",
" with_thumb_only_page (bool): when creating a static HTML (notebook==False), a light HTML page including only the thumb\n",
" gallery will also be produced.\n",
" nspec: select subsample of spectra, only for frame input\n",
" startspec: if nspec is set, subsample selection will be [startspec:startspec+nspec]\n",
" model_from_zcat: if True, model spectra will be computed from the input zcatalog\n",
" model: if set, use this input set of model spectra (instead of computing it from zcat)\n",
" model format (mwave, mflux); model must be entry-matched to zcatalog.\n",
" is_coadded : set to True if spectra are coadds\n",
" with_imaging : include thumb image from legacysurvey.org\n",
" with_noise : include noise for each spectrum\n",
" with_coaddcam : include camera-coaddition\n",
" template_dir: Redrock template directory\n",
" archetype_fit : if True, assume zbest derived from redrock --archetypes and plot model accordingly.\n",
" archetypes_dir : directory path for archetypes if not $RR__ARCHETYPE_DIR.\n",
" num_approx_fits (default None): nb of best fit models to display if redrock_cat is given. By default,\n",
" num_approx_fits=(nb of best fits available in redrock_cat)\n",
" spectra : :class:`~desispec.spectra.Spectra` or :class:`~specutils.Spectrum1D` or :class:`~specutils.SpectrumList` or list of :class:`~desispec.frame.Frame`\n",
" Input spectra. :class:`~specutils.Spectrum1D` are assumed to be SDSS/BOSS/eBOSS.\n",
" Otherwise DESI spectra or frames is assumed.\n",
" zcatalog : :class:`~astropy.table.Table`, optional\n",
" Redshift values, matched one-to-one with the input spectra.\n",
" redrock_cat : :class:`~astropy.table.Table`, optional\n",
" Redrock output (as defined in :func:`~prospect.utilities.match_redrock_zfit_to_spectra`).\n",
" Entries must be matched one-by-one (in order) to spectra.\n",
" notebook : :class:`bool`, optional\n",
" If ``True``, bokeh outputs the viewer to a Jupyter notebook.\n",
" html_dir : :class:`str`, optional\n",
" Directory to store the HTML page if `notebook` is ``False``.\n",
" title : :class:`str`, optional\n",
" Title used to name the HTML page / the bokeh figure / the VI file.\n",
" with_imaging : :class:`bool`, optional\n",
" If ``False``, don't include thumb image from https://www.legacysurvey.org/viewer.\n",
" with_noise : :class:`bool`, optional\n",
" If ``False``, don't include uncertainty for each spectrum.\n",
" with_thumb_tab : :class:`bool`, optional\n",
" If ``False``, don't include a tab with spectra thumbnails.\n",
" with_vi_widgets : :class:`bool`, optional\n",
" Include widgets used to enter VI information. Set it to ``False`` if\n",
" you do not intend to record VI files.\n",
" vi_countdown : :class:`int`, optional\n",
" If ``>0``, add a countdown widget in the VI panel, with a value in minutes given\n",
" by `vi_countdown``.\n",
" with_thumb_only_page : :class:`bool`, optional\n",
" When creating a static HTML (`notebook` is ``False``), a light HTML\n",
" page including only the thumb gallery will also be produced.\n",
" is_coadded : :class:`bool`, optional\n",
" Set to ``True`` if `spectra` are coadds. This will always be assumed\n",
" for SDSS-style inputs, but not for DESI inputs.\n",
" with_coaddcam : :class:`bool`, optional\n",
" Include camera-coaddition, only relevant for DESI.\n",
" mask_type : :class:`str`, optional (default: DESI_TARGET)\n",
" Bitmask type to identify target categories in the spectra. For DESI\n",
" these could be: DESI_TARGET, SV1_DESI_TARGET, SV1_BGS_TARGET, CMX_TARGET.\n",
" model_from_zcat : :class:`bool`, optional\n",
" If ``True``, model spectra will be computed from the input `zcatalog`.\n",
" model : :func:`tuple`, optional\n",
" If set, use this input set of model spectra instead of computing it from `zcatalog`.\n",
" model consists of (mwave, mflux); model must be entry-matched to `zcatalog`.\n",
" num_approx_fits : :class:`int`, optional\n",
" Number of best-fit models to display, if `redrock_cat` is provided.\n",
" By default, all best-fit models available in `redrock_cat` are diplayed.\n",
" with_full_2ndfit : :class:`bool`, optional\n",
" If ``True``, the second best-fit model from `redrock_cat` will be displayed\n",
" without approximation (no undersampling, full resolution).\n",
" template_dir : :class:`str`, optional\n",
" Redrock template directory.\n",
" archetype_fit : :class:`bool`, optional\n",
" If ``True``, assume `zcatalog` derived from :command:`redrock --archetypes`\n",
" and plot model accordingly.\n",
" archetypes_dir : :class:`str`, optional\n",
" Directory path for archetypes if not :envvar:`RR_ARCHETYPE_DIR`.\n",
"\n"
]
}
],
"source": [
"help(plotframes.plotspectra)"
"help(viewer.plotspectra)"
]
},
{
Expand All @@ -146,7 +187,7 @@
"metadata": {},
"outputs": [],
"source": [
"plotframes.plotspectra(coadds_select, zcatalog=zbest_select, notebook=True, \n",
"viewer.plotspectra(coadds_select, zcatalog=zbest_select, notebook=True, \n",
" title='Prospect_demo', \n",
" mask_type='CMX_TARGET')"
]
Expand Down
7 changes: 3 additions & 4 deletions doc/nb/Prospect_spectrum_service.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"from astropy.table import Table\n",
"from specutils import Spectrum1D, SpectrumCollection, SpectrumList\n",
"from prospect.specutils import read_spPlate, read_spZbest\n",
"from prospect.plotspecutils import plotspectra"
"from prospect.viewer import plotspectra"
]
},
{
Expand All @@ -40,8 +40,7 @@
"metadata": {},
"outputs": [],
"source": [
"from dl import queryClient as qc, storeClient as sc\n",
"# import specClient as spec"
"from dl import queryClient as qc, storeClient as sc, specClient as spec"
]
},
{
Expand Down Expand Up @@ -569,7 +568,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions doc/nb/Prospect_targetid.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"# If not using the desiconda version of prospect: EDIT THIS to your path\n",
"#sys.path.insert(0,\"/global/homes/X/XXXXX/prospect/py\") \n",
"sys.path.insert(0,\"/global/homes/a/armengau/prospect/py\")\n",
"from prospect import plotframes, utilities\n",
"from prospect import viewer, utilities\n",
"import desispec.io\n",
"from astropy.table import Table, vstack\n",
"import numpy as np\n",
Expand Down Expand Up @@ -63,7 +63,7 @@
"spectra, zcat= utilities.load_spectra_zcat_from_targets(targets, tiledir, obs_db)\n",
"\n",
"# VI interface in notebook\n",
"plotframes.plotspectra(spectra, zcatalog=zcat, title='Target_select', notebook=True, mask_type='CMX_TARGET')"
"viewer.plotspectra(spectra, zcatalog=zcat, title='Target_select', notebook=True, mask_type='CMX_TARGET')"
]
},
{
Expand Down Expand Up @@ -96,7 +96,7 @@
"spectra, zcat, rrtable = utilities.load_spectra_zcat_from_targets(targets, tiledir, obs_db, with_redrock=True)\n",
"\n",
"# VI interface in notebook\n",
"plotframes.plotspectra(spectra, zcatalog=zcat, redrock_cat=rrtable, title='Target_select', notebook=True, mask_type='CMX_TARGET')"
"viewer.plotspectra(spectra, zcatalog=zcat, redrock_cat=rrtable, title='Target_select', notebook=True, mask_type='CMX_TARGET')"
]
}
],
Expand Down
66 changes: 66 additions & 0 deletions py/prospect/grid_thumbs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-
"""
====================
prospect.grid_thumbs
====================
Grid of thumbnail plots displaying a set of spectra
"""

import numpy as np
import astropy.convolution

import bokeh.plotting as bk
import bokeh.layouts as bl

from .mycoaddcam import coaddcam_prospect

# TODOS:
# - structure: class??
# - improve thumbs with infos, plot quality

def grid_thumbs(spectra, thumb_width, x_range=(3400,10000), thumb_height=None, resamp_factor=15, ncols_grid=5, titles=None) :
'''
Create a bokeh gridplot of thumbnail pictures from spectra
- coadd arms
- smooth+resample to reduce size of embedded CDS, according to resamp_factor
- titles : optional list of titles for each thumb
TODO: Not tested on Spectrum1D objects.
'''

if thumb_height is None : thumb_height = thumb_width//2
if titles is not None : assert len(titles) == spectra.num_spectra()
thumb_wave, thumb_flux, dummy = coaddcam_prospect(spectra)
kernel = astropy.convolution.Gaussian1DKernel(stddev=resamp_factor)

thumb_plots = []
for i_spec in range(spectra.num_spectra()) :
x_vals = (thumb_wave[::resamp_factor])[resamp_factor:-resamp_factor]
# Use astropy convolution : handles NaNs
y_vals = astropy.convolution.convolve(thumb_flux[i_spec,:], kernel)
y_vals = (y_vals[::resamp_factor])[resamp_factor:-resamp_factor]
x_vals = x_vals[~np.isnan(y_vals)] # Needed to avoid 'ValueError: Out of range float values are not JSON compliant'
y_vals = y_vals[~np.isnan(y_vals)]
if len(x_vals)==0 : # All NaN ... this should not happen ...
ymin, ymax = -1, 1
else :
yampl = np.max(y_vals) - np.min(y_vals)
ymin = np.min(y_vals) - 0.1*yampl
ymax = np.max(y_vals) + 0.1*yampl
plot_title = None
if titles is not None : plot_title = titles[i_spec]
mini_plot = bk.figure(plot_width=thumb_width, plot_height=thumb_height, x_range=x_range, y_range=(ymin,ymax), title=plot_title)
if len(x_vals)!=0 : mini_plot.line(x_vals, y_vals, line_color='red')
mini_plot.xaxis.visible = False
mini_plot.yaxis.visible = False
mini_plot.min_border_left = 0
mini_plot.min_border_right = 0
mini_plot.min_border_top = 0
mini_plot.min_border_bottom = 0
thumb_plots.append(mini_plot)

return bl.gridplot(thumb_plots, ncols=ncols_grid, toolbar_location=None, sizing_mode='scale_width')

2 changes: 1 addition & 1 deletion py/prospect/scripts/specview_cmx_coadds.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import desispec.spectra
import desispec.frame

from ..plotframes import plotspectra
from ..viewer import plotspectra
from ..myspecselect import myspecselect
from ..myspecupdate import myspecupdate
from ..utilities import specviewer_selection, match_redrock_zfit_to_spectra, match_zcat_to_spectra
Expand Down
2 changes: 1 addition & 1 deletion py/prospect/scripts/specview_cmx_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import desispec.spectra
import desispec.frame

from ..plotframes import plotspectra
from ..viewer import plotspectra
from ..myspecselect import myspecselect
from ..myspecupdate import myspecupdate
from ..utilities import frames2spectra, specviewer_selection, coadd_targets
Expand Down
2 changes: 1 addition & 1 deletion py/prospect/scripts/specview_cmx_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import desispec.io
from desiutil.log import get_logger

from ..plotframes import plotspectra
from ..viewer import plotspectra
from ..myspecselect import myspecselect
from ..utilities import make_targetdict, load_spectra_zcat_from_targets #, match_zcat_to_spectra

Expand Down
2 changes: 1 addition & 1 deletion py/prospect/scripts/specview_per_expo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

from ..myspecselect import myspecselect # special (to be edited)
from ..plotframes import create_model, plotspectra
from ..utilities import match_zbest_to_spectra, match_vi_targets #, miniplot_spectrum
from ..utilities import match_zcat_to_spectra, match_vi_targets #, miniplot_spectrum

def _parse():
parser = argparse.ArgumentParser(description='Create html pages for the spectral viewer')
Expand Down
6 changes: 3 additions & 3 deletions py/prospect/scripts/specview_per_night.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import desispec.frame

from ..myspecselect import myspecselect # special (to be edited)
from ..plotframes import plotspectra
from ..viewer import plotspectra
from ..utilities import match_zcat_to_spectra #, match_vi_targets, miniplot_spectrum

def _parse():
Expand Down Expand Up @@ -70,7 +70,7 @@ def main():
the_indices = sort_indices[(i_page-1)*args.nspecperfile:i_page*args.nspecperfile]
thespec = myspecselect(spectra, indices=the_indices)
thezb, kk = match_zcat_to_spectra(zbest,thespec)
model = plotframes.create_model(thespec, thezb)
#model = plotframes.create_model(thespec, thezb)
### No VI results to display by default
# vifile = os.environ['HOME']+"/prospect/vilist_prototype.fits"
# vidata = match_vi_targets(vifile, thespec.fibermap["TARGETID"])
Expand All @@ -80,7 +80,7 @@ def main():
os.makedirs(html_dir)
os.mkdir(html_dir+"/vignettes")

plotframes.plotspectra(thespec, zcatalog=thezb, vidata=None, model=model, title=titlepage, html_dir=html_dir, is_coadded=False)
plotspectra(thespec, zcatalog=thezb, title=titlepage, html_dir=html_dir, is_coadded=False)
# for i_spec in range(thespec.num_spectra()) :
# saveplot = html_dir+"/vignettes/night"+thenight+"_"+file_label+"_"+str(i_page)+"_"+str(i_spec)+".png"
# miniplot_spectrum(thespec, i_spec, model=model, saveplot=saveplot, smoothing = args.vignette_smoothing)

0 comments on commit b6775ce

Please sign in to comment.