Commit b2d2aab6bfd0b489b35fd26c01166842c5576088
1 parent
bcdc45a6
Add show_residuals.py unit test for On/Off spectrum
Showing
4 changed files
with
37 additions
and
23 deletions
cscripts/Makefile.am
| @@ -196,6 +196,7 @@ testdata = $(top_srcdir)/test/data/crab.xml \ | @@ -196,6 +196,7 @@ testdata = $(top_srcdir)/test/data/crab.xml \ | ||
| 196 | $(top_srcdir)/test/data/skymap.fits \ | 196 | $(top_srcdir)/test/data/skymap.fits \ |
| 197 | $(top_srcdir)/test/data/spectrum.fits \ | 197 | $(top_srcdir)/test/data/spectrum.fits \ |
| 198 | $(top_srcdir)/test/data/residual.fits \ | 198 | $(top_srcdir)/test/data/residual.fits \ |
| 199 | + $(top_srcdir)/test/data/residual_onoff.fits \ | ||
| 199 | $(top_srcdir)/test/data/lightcurve.fits \ | 200 | $(top_srcdir)/test/data/lightcurve.fits \ |
| 200 | $(top_srcdir)/test/data/ts.dat \ | 201 | $(top_srcdir)/test/data/ts.dat \ |
| 201 | $(top_srcdir)/test/data/models_spatial.xml \ | 202 | $(top_srcdir)/test/data/models_spatial.xml \ |
examples/show_residuals.py
| @@ -36,8 +36,19 @@ except (ImportError, RuntimeError): | @@ -36,8 +36,19 @@ except (ImportError, RuntimeError): | ||
| 36 | # ======================= # | 36 | # ======================= # |
| 37 | def set_residual_label(algorithm): | 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 | if algorithm == 'SUB': | 52 | if algorithm == 'SUB': |
| 42 | label = 'Residuals (counts)' | 53 | label = 'Residuals (counts)' |
| 43 | elif algorithm == 'SUBDIV': | 54 | elif algorithm == 'SUBDIV': |
| @@ -74,11 +85,11 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | @@ -74,11 +85,11 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | ||
| 74 | Counts error | 85 | Counts error |
| 75 | e_resid : list of float | 86 | e_resid : list of float |
| 76 | Residual error | 87 | Residual error |
| 77 | - c_counts : `~gammalib.GFitsTableCol()' | 88 | + c_counts : `~gammalib.GFitsTableCol' |
| 78 | Counts FITS table column | 89 | Counts FITS table column |
| 79 | - c_model : `~gammalib.GFitsTableCol()' | 90 | + c_model : `~gammalib.GFitsTableCol' |
| 80 | Model FITS table column | 91 | Model FITS table column |
| 81 | - c_resid : `~gammalib.GFitsTableCol()' | 92 | + c_resid : `~gammalib.GFitsTableCol' |
| 82 | Residual FITS table column | 93 | Residual FITS table column |
| 83 | algorithm : str | 94 | algorithm : str |
| 84 | Algorithm | 95 | Algorithm |
| @@ -86,7 +97,7 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | @@ -86,7 +97,7 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | ||
| 86 | Returns | 97 | Returns |
| 87 | ------- | 98 | ------- |
| 88 | counts, model, resid, e_counts, e_resid : tuple of float | 99 | counts, model, resid, e_counts, e_resid : tuple of float |
| 89 | - Values | 100 | + Counts, model and residual arrays |
| 90 | """ | 101 | """ |
| 91 | # Extract values | 102 | # Extract values |
| 92 | counts.append(c_counts.real(row)) | 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,10 +107,7 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | ||
| 96 | # Calculate count error | 107 | # Calculate count error |
| 97 | err = math.sqrt(c_counts.real(row)) | 108 | err = math.sqrt(c_counts.real(row)) |
| 98 | if err == 1: | 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 | e_counts.append(err) | 111 | e_counts.append(err) |
| 104 | 112 | ||
| 105 | # Calculate residual error | 113 | # Calculate residual error |
| @@ -110,15 +118,17 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | @@ -110,15 +118,17 @@ def fill_cmr(row, counts, model, resid, e_counts, e_resid, c_counts, c_model, | ||
| 110 | elif algorithm == 'SUBDIVSQRT': | 118 | elif algorithm == 'SUBDIVSQRT': |
| 111 | e_resid.append(err / math.sqrt(c_model.real(row))) | 119 | e_resid.append(err / math.sqrt(c_model.real(row))) |
| 112 | elif algorithm == 'SIGNIFICANCE': | 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 | # Return tuple | 125 | # Return tuple |
| 116 | return counts, model, resid, e_counts, e_resid | 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 | def plot_residuals(filename, plotfile, hdu): | 132 | def plot_residuals(filename, plotfile, hdu): |
| 123 | """ | 133 | """ |
| 124 | Plot spectrum | 134 | Plot spectrum |
| @@ -129,7 +139,7 @@ def plot_residuals(filename, plotfile, hdu): | @@ -129,7 +139,7 @@ def plot_residuals(filename, plotfile, hdu): | ||
| 129 | Name of spectrum FITS file | 139 | Name of spectrum FITS file |
| 130 | plotfile : str | 140 | plotfile : str |
| 131 | Plot file name | 141 | Plot file name |
| 132 | - hdu : int | 142 | + hdu : int |
| 133 | Number of observation to plot | 143 | Number of observation to plot |
| 134 | """ | 144 | """ |
| 135 | # Read spectrum file | 145 | # Read spectrum file |
| @@ -270,20 +280,16 @@ def plot_residuals(filename, plotfile, hdu): | @@ -270,20 +280,16 @@ def plot_residuals(filename, plotfile, hdu): | ||
| 270 | axarr[3].axhline(0, color='0.5', linestyle='--') | 280 | axarr[3].axhline(0, color='0.5', linestyle='--') |
| 271 | axarr[3].set_xlabel('Energy (TeV)') | 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 | for s in range(table.ncols()): | 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 | component = [] | 288 | component = [] |
| 282 | for row in range(nrows): | 289 | for row in range(nrows): |
| 283 | component.append(table[s].real(row)) | 290 | component.append(table[s].real(row)) |
| 284 | component = [component[0]] + component | 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 | # Add legend | 294 | # Add legend |
| 289 | axarr[0].legend(loc='best') | 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,6 +379,13 @@ class Test(gammalib.GPythonTestSuite): | ||
| 379 | # Execute script | 379 | # Execute script |
| 380 | self._execute_python('show_residuals', args=args) | 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 | # Return | 389 | # Return |
| 383 | return | 390 | return |
| 384 | 391 |