Commit b2d2aab6bfd0b489b35fd26c01166842c5576088

Authored by Jürgen Knödlseder
1 parent bcdc45a6

Add show_residuals.py unit test for On/Off spectrum

cscripts/Makefile.am
... ... @@ -196,6 +196,7 @@ testdata = $(top_srcdir)/test/data/crab.xml \
196 196 $(top_srcdir)/test/data/skymap.fits \
197 197 $(top_srcdir)/test/data/spectrum.fits \
198 198 $(top_srcdir)/test/data/residual.fits \
  199 + $(top_srcdir)/test/data/residual_onoff.fits \
199 200 $(top_srcdir)/test/data/lightcurve.fits \
200 201 $(top_srcdir)/test/data/ts.dat \
201 202 $(top_srcdir)/test/data/models_spatial.xml \
... ...
examples/show_residuals.py
... ... @@ -36,8 +36,19 @@ except (ImportError, RuntimeError):
36 36 # ======================= #
37 37 def set_residual_label(algorithm):
38 38 """
  39 + Set residual label text for a given algorithm.
  40 +
  41 + Parameters
  42 + ----------
  43 + algorithm : str
  44 + Algorithm
  45 +
  46 + Returns
  47 + -------
  48 + label : str
  49 + Label text
39 50 """
40   - # Set algorithm dependent residual level
  51 + # Set algorithm dependent residual label text
41 52 if algorithm == 'SUB':
42 53 label = 'Residuals (counts)'
43 54 elif algorithm == 'SUBDIV':
... ... @@ -74,11 +85,11 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model,
74 85 Counts error
75 86 e_resid : list of float
76 87 Residual error
77   - c_counts : `~gammalib.GFitsTableCol()'
  88 + c_counts : `~gammalib.GFitsTableCol'
78 89 Counts FITS table column
79   - c_model : `~gammalib.GFitsTableCol()'
  90 + c_model : `~gammalib.GFitsTableCol'
80 91 Model FITS table column
81   - c_resid : `~gammalib.GFitsTableCol()'
  92 + c_resid : `~gammalib.GFitsTableCol'
82 93 Residual FITS table column
83 94 algorithm : str
84 95 Algorithm
... ... @@ -86,7 +97,7 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model,
86 97 Returns
87 98 -------
88 99 counts, model, resid, e_counts, e_resid : tuple of float
89   - Values
  100 + Counts, model and residual arrays
90 101 """
91 102 # Extract values
92 103 counts.append(c_counts.real(row))
... ... @@ -96,10 +107,7 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model,
96 107 # Calculate count error
97 108 err = math.sqrt(c_counts.real(row))
98 109 if err == 1:
99   - # this prevents visualization problem in matplotlib
100   - err = 0.99
101   - else:
102   - pass
  110 + err = 0.99 # This prevents visualization problem in matplotlib
103 111 e_counts.append(err)
104 112  
105 113 # Calculate residual error
... ... @@ -110,15 +118,17 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model,
110 118 elif algorithm == 'SUBDIVSQRT':
111 119 e_resid.append(err / math.sqrt(c_model.real(row)))
112 120 elif algorithm == 'SIGNIFICANCE':
113   - e_resid.append(1.)
  121 + e_resid.append(1.0)
  122 + else:
  123 + e_resid.append(0.0)
114 124  
115 125 # Return tuple
116 126 return counts, model, resid, e_counts, e_resid
117 127  
118 128  
119   -# ============= #
120   -# Plot spectrum #
121   -# ============= #
  129 +# ====================== #
  130 +# Plot residual spectrum #
  131 +# ====================== #
122 132 def plot_residuals(filename, plotfile, hdu):
123 133 """
124 134 Plot spectrum
... ... @@ -129,7 +139,7 @@ def plot_residuals(filename, plotfile, hdu):
129 139 Name of spectrum FITS file
130 140 plotfile : str
131 141 Plot file name
132   - hdu : int
  142 + hdu : int
133 143 Number of observation to plot
134 144 """
135 145 # Read spectrum file
... ... @@ -270,20 +280,16 @@ def plot_residuals(filename, plotfile, hdu):
270 280 axarr[3].axhline(0, color='0.5', linestyle='--')
271 281 axarr[3].set_xlabel('Energy (TeV)')
272 282  
273   - # Add spectra of individual components
274   - skiplist = ['Counts', 'Model', 'Residuals',
275   - 'Counts_Off', 'Model_Off', 'Residuals_Off',
276   - 'Emin', 'Emax']
  283 + # Add spectra of individual components. Skip all standard columns.
  284 + skiplist = ['Counts', 'Model', 'Residuals', 'Counts_Off', 'Model_Off',
  285 + 'Residuals_Off', 'Emin', 'Emax']
277 286 for s in range(table.ncols()):
278   - if table[s].name() in skiplist:
279   - pass
280   - else:
  287 + if table[s].name() not in skiplist:
281 288 component = []
282 289 for row in range(nrows):
283 290 component.append(table[s].real(row))
284 291 component = [component[0]] + component
285   - axarr[0].step(ebounds, component, zorder=0,
286   - label=table[s].name())
  292 + axarr[0].step(ebounds, component, zorder=0, label=table[s].name())
287 293  
288 294 # Add legend
289 295 axarr[0].legend(loc='best')
... ...
test/data/residual_onoff.fits 0 → 100644
No preview for this file type
test/test_examples.py.in
... ... @@ -379,6 +379,13 @@ class Test(gammalib.GPythonTestSuite):
379 379 # Execute script
380 380 self._execute_python('show_residuals', args=args)
381 381  
  382 + # Set arguments
  383 + args = ('-p example_show_residuals_onoff.eps %s/residual_onoff.fits' %
  384 + self._datadir)
  385 +
  386 + # Execute script
  387 + self._execute_python('show_residuals', args=args)
  388 +
382 389 # Return
383 390 return
384 391  
... ...