#!/usr/bin/env python # coding: utf-8 # # Notebook support in Sherpa # # A number of objects have been updated to support HTML output when displayed in a Jupyter notebook. Let's take a quick tour! # # ## Data1D, Data1DInt, and Data2D # # First we have the data objects: # In[1]: import numpy as np from sherpa.data import Data1D, Data1DInt, Data2D x = np.arange(100, 200, 20) y = [120, 240, 30, 95, 130] d1 = Data1D('oned', x, y) d1i = Data1DInt('onedint', x[:-1], x[1:], y[:-1]) x0 = [150, 250, 100] x1 = [250, 200, 200] y2 = [50, 40, 70] d2 = Data2D('twod', x0, x1, y2) # Each can be displayed with `print`, which shows a textual representation of attribute and values: # In[2]: print(d1) # Or they can be displayed as-is which, **in a Jupyter notebook**, will display either a plot or a HTML table. The `Data1D` and `Data1DInt` classes will display a plot (if the `pylab` plotting backend is selected), and the `Data2D` class a table. # In[3]: d1 # In[4]: print(d1i) # In[5]: d1i # As mentioned, the `Data2D` class just gets a fancy HTML table but no plot: # In[6]: print(d2) # In[7]: d2 # ## DataPHA, DataARF, and DataRMF # # The Astronomy-specific PHA, ARF, and RMF data classes can also be displayed. These (when you have `pylab` selected) display both the data and a table of information. # In[8]: from sherpa.astro import io pha = io.read_pha('../sherpa-test-data/sherpatest/9774.pi') arf = io.read_arf('../sherpa-test-data/sherpatest/9774.arf') rmf = io.read_rmf('../sherpa-test-data/sherpatest/9774.rmf') # In[9]: print(pha) # In[10]: pha # The PHA object will change the display based on the data - that is, if you change the filtering and grouping you will see a different plot, # and the table will also change: # In[11]: pha.notice(0.3, 7) pha.group_counts(20, tabStops=~pha.mask) pha # It will also change if you change the analysis setting: # In[12]: pha.set_analysis('wave') pha # The ARF and RMF objects do not change based on their settings: # In[13]: print(arf) # In[14]: arf # In[15]: print(rmf) # For the RMF, five energies are selected that span the response of the instrument, and the response to these monochromatic energies are displayed. # In[16]: rmf # ## DataIMG # # For images with little metadata, and no WCS information, we just get an image: # In[17]: img = io.read_image('../sherpa-test-data/sherpatest/img.fits') # In[18]: img # If the image contains WCS information, or some basic metadata, then we will get extra tables # (unfortunately this test image doesn't display particularly wonderfully as the source # is faint!). # In[19]: img2 = io.read_image('../sherpa-test-data/sherpatest/acisf08478_000N001_r0043_regevt3_srcimg.fits') img2 # As with the PHA object, we can change the display slightly, such as changing the `coord` setting and spatially filtering the data: # In[20]: img2.set_coord('physical') img2.notice2d('circle(3150, 4520, 20)') img2 # ## Models and parameters # # Models and parameters can also be displayed directly as HTML tables, mirroring their `print` output. # In[21]: from sherpa.models.basic import Gauss2D, Const2D mgauss = Gauss2D() mconst = Const2D() mgauss.xpos = 3150 mgauss.ypos = 4520 mdl = mgauss + mconst # We can compare the model output (this also works with a single component, such as `mgauss` and `mconst`): # In[22]: print(mdl) # In[23]: mdl # We can have a display for parameters (I chose this model since we can see the minimum and maximum columns get displayed as units of $\pi$ in the notebook-display version): # In[24]: print(mgauss.theta) # In[25]: mgauss.theta # ## Fitting data # # Various objects related to fitting will also display in Jupyter notebooks. I fit a simple model (the model we just created, in fact) to the last image we were looking at. For this example I use the `sherpa.astro.ui` layer to fit, rather than creating the fit object manually. # In[26]: from sherpa.astro import ui ui.set_data(img2) ui.set_source(mdl) ui.set_stat('cash') ui.set_method('simplex') # The output of the fit call is still just text: # In[27]: ui.fit() # However, we can see the model results (as shown above): # In[28]: ui.get_source() # We can also display the fit results directly (I am dropping the comparison to the `print` output in part to show you can just call routines like `ui.get_git_results` and see the display without needing to call `print`, at least in a Jupyter notebook): # In[29]: ui.get_fit_results() # Similarly, the output of `conf` (or `covar`) is just text, but the results can be accessed directly with `ui.get_conf_results` or `ui.get_covar_results`: # In[30]: ui.conf() # In[31]: ui.get_conf_results() # The `get_stat_info` call returns information for each dataset. As this is a list, the overall output just gets displayed as text, but if you access an individual element you will get a HTML table: # In[32]: ui.get_stat_info() # In[33]: ui.get_stat_info()[0] # Once you have created an interval- or region-projection plot, such as this comparison of the x and y centers of the gaussian, you can display the results with the relevant `get` call (in this case `ui.get_reg_proj`). # In[34]: ui.reg_proj(mgauss.xpos, mgauss.ypos) # In[35]: ui.get_reg_proj()