Commit ddd8c148bb4b883264e7f2e41cb58d0683d285c7
1 parent
e3348b38
Adapt script to actual GammaLib interface
Showing
8 changed files
with
85 additions
and
63 deletions
inst/cta/test/benchmark_irf_computation.py
| ... | ... | @@ -6,7 +6,7 @@ |
| 6 | 6 | # Monte-Carlo simulations. This script is used to validate the absolute |
| 7 | 7 | # normalization of the CTA response computations. |
| 8 | 8 | # |
| 9 | -# Copyright (C) 2014 Juergen Knoedlseder | |
| 9 | +# Copyright (C) 2014-2015 Juergen Knoedlseder | |
| 10 | 10 | # |
| 11 | 11 | # This program is free software: you can redistribute it and/or modify |
| 12 | 12 | # it under the terms of the GNU General Public License as published by |
| ... | ... | @@ -117,7 +117,7 @@ def binned_irf(model, cntmap, irf, caldb, cntref): |
| 117 | 117 | # =========================== # |
| 118 | 118 | # CTA stacked IRF computation # |
| 119 | 119 | # =========================== # |
| 120 | -def stacked_irf(model, cntmap, expcube, psfcube, cntref): | |
| 120 | +def stacked_irf(model, cntmap, expcube, psfcube, bkgcube, cntref): | |
| 121 | 121 | """ |
| 122 | 122 | Perform stacked IRF computation. |
| 123 | 123 | """ |
| ... | ... | @@ -132,9 +132,10 @@ def stacked_irf(model, cntmap, expcube, psfcube, cntref): |
| 132 | 132 | obs.load(cntmap) |
| 133 | 133 | |
| 134 | 134 | # Specify response for CTA observation |
| 135 | - exposure = gammalib.GCTACubeExposure(expcube) | |
| 136 | - psf = gammalib.GCTACubePsf(psfcube) | |
| 137 | - obs.response(exposure, psf) | |
| 135 | + exposure = gammalib.GCTACubeExposure(expcube) | |
| 136 | + psf = gammalib.GCTACubePsf(psfcube) | |
| 137 | + background = gammalib.GCTACubeBackground(bkgcube) | |
| 138 | + obs.response(exposure, psf, background) | |
| 138 | 139 | |
| 139 | 140 | # Load model to describe the data from XML file |
| 140 | 141 | models = gammalib.GModels(model) |
| ... | ... | @@ -179,6 +180,7 @@ if __name__ == '__main__': |
| 179 | 180 | caldb = "../caldb" |
| 180 | 181 | expcube = "data/expcube.fits" |
| 181 | 182 | psfcube = "data/psfcube.fits" |
| 183 | + bkgcube = "data/bkgcube.fits" | |
| 182 | 184 | |
| 183 | 185 | # Set tests |
| 184 | 186 | tests = ["point", "disk", "gauss", "shell", "ellipse", "diffuse"] |
| ... | ... | @@ -235,4 +237,4 @@ if __name__ == '__main__': |
| 235 | 237 | binned_irf(model, cntmap, irf, gammalib.GCaldb(caldb), cntref) |
| 236 | 238 | |
| 237 | 239 | # Perform stacked computation |
| 238 | - stacked_irf(model, cntmap, expcube, psfcube, cntref) | |
| 240 | + stacked_irf(model, cntmap, expcube, psfcube, bkgcube, cntref) | ... | ... |
inst/cta/test/benchmark_ml_fitting.py
| ... | ... | @@ -3,7 +3,7 @@ |
| 3 | 3 | # This script performs a benchmark for maximum likelihood fitting of CTA |
| 4 | 4 | # data. |
| 5 | 5 | # |
| 6 | -# Copyright (C) 2012-2014 Juergen Knoedlseder | |
| 6 | +# Copyright (C) 2012-2015 Juergen Knoedlseder | |
| 7 | 7 | # |
| 8 | 8 | # This program is free software: you can redistribute it and/or modify |
| 9 | 9 | # it under the terms of the GNU General Public License as published by |
| ... | ... | @@ -142,7 +142,7 @@ def binned_analysis(model, cntmap, irf, caldb): |
| 142 | 142 | # ==================== # |
| 143 | 143 | # CTA stacked analysis # |
| 144 | 144 | # ==================== # |
| 145 | -def stacked_analysis(model, cntmap, expcube, psfcube): | |
| 145 | +def stacked_analysis(model, cntmap, expcube, psfcube, bkgcube): | |
| 146 | 146 | """ |
| 147 | 147 | Perform stacked maximum likelihood fitting of CTA data using GammaLib |
| 148 | 148 | classes. |
| ... | ... | @@ -166,9 +166,10 @@ def stacked_analysis(model, cntmap, expcube, psfcube): |
| 166 | 166 | cta_obs.load(cntmap) |
| 167 | 167 | |
| 168 | 168 | # Specify response for CTA observation |
| 169 | - exposure = gammalib.GCTACubeExposure(expcube) | |
| 170 | - psf = gammalib.GCTACubePsf(psfcube) | |
| 171 | - cta_obs.response(exposure, psf) | |
| 169 | + exposure = gammalib.GCTACubeExposure(expcube) | |
| 170 | + psf = gammalib.GCTACubePsf(psfcube) | |
| 171 | + background = gammalib.GCTACubeBackground(bkgcube) | |
| 172 | + cta_obs.response(exposure, psf, background) | |
| 172 | 173 | |
| 173 | 174 | # Append CTA observation to observation container |
| 174 | 175 | obs.append(cta_obs) |
| ... | ... | @@ -217,6 +218,7 @@ if __name__ == '__main__': |
| 217 | 218 | caldb = "../caldb" |
| 218 | 219 | expcube = "data/expcube.fits" |
| 219 | 220 | psfcube = "data/psfcube.fits" |
| 221 | + bkgcube = "data/bkgcube.fits" | |
| 220 | 222 | |
| 221 | 223 | # Set tests |
| 222 | 224 | tests = ["point", "disk", "gauss", "shell", "ellipse", "diffuse"] |
| ... | ... | @@ -273,4 +275,4 @@ if __name__ == '__main__': |
| 273 | 275 | binned_analysis(model, cntmap, irf, gammalib.GCaldb(caldb)) |
| 274 | 276 | |
| 275 | 277 | # Perform stacked analysis |
| 276 | - stacked_analysis(model, cntmap, expcube, psfcube) | |
| 278 | + stacked_analysis(model, cntmap, expcube, psfcube, bkgcube) | ... | ... |
inst/cta/test/example_binned_ml_fit.py
| ... | ... | @@ -115,7 +115,7 @@ def binned_analysis(model, cntmap, irf, caldb): |
| 115 | 115 | # ==================== # |
| 116 | 116 | # CTA stacked analysis # |
| 117 | 117 | # ==================== # |
| 118 | -def stacked_analysis(model, cntmap, expcube, psfcube): | |
| 118 | +def stacked_analysis(model, cntmap, expcube, psfcube, bkgcube): | |
| 119 | 119 | """ |
| 120 | 120 | Perform stacked maximum likelihood fitting of CTA data using GammaLib |
| 121 | 121 | classes. The function performs two fits, one with the full model and |
| ... | ... | @@ -141,9 +141,10 @@ def stacked_analysis(model, cntmap, expcube, psfcube): |
| 141 | 141 | cta_obs.load(cntmap) |
| 142 | 142 | |
| 143 | 143 | # Specify response for CTA observation |
| 144 | - exposure = gammalib.GCTACubeExposure(expcube) | |
| 145 | - psf = gammalib.GCTACubePsf(psfcube) | |
| 146 | - cta_obs.response(exposure, psf) | |
| 144 | + exposure = gammalib.GCTACubeExposure(expcube) | |
| 145 | + psf = gammalib.GCTACubePsf(psfcube) | |
| 146 | + background = gammalib.GCTACubeBackground(bkgcube) | |
| 147 | + cta_obs.response(exposure, psf, background) | |
| 147 | 148 | |
| 148 | 149 | # Append CTA observation to observation container |
| 149 | 150 | obs.append(cta_obs) |
| ... | ... | @@ -225,12 +226,13 @@ if __name__ == '__main__': |
| 225 | 226 | cntmap = "data/crab_cntmap.fits" |
| 226 | 227 | expcube = "data/expcube.fits" |
| 227 | 228 | psfcube = "data/psfcube.fits" |
| 229 | + bkgcube = "data/bkgcube.fits" | |
| 228 | 230 | |
| 229 | 231 | # Perform binned analysis |
| 230 | 232 | results_binned = binned_analysis(model, cntmap, irf, gammalib.GCaldb(caldb)) |
| 231 | 233 | |
| 232 | 234 | # Perform stacked analysis |
| 233 | - results_cube = stacked_analysis(model, cntmap, expcube, psfcube) | |
| 235 | + results_cube = stacked_analysis(model, cntmap, expcube, psfcube, bkgcube) | |
| 234 | 236 | |
| 235 | 237 | # Print model results |
| 236 | 238 | #print(result) | ... | ... |
inst/cta/test/example_make_model.py
| 1 | 1 | #! /usr/bin/env python |
| 2 | -# ===========================================================================================# | |
| 2 | +# ========================================================================== | |
| 3 | 3 | # This script makes a model map from an XML file. |
| 4 | -# ===========================================================================================# | |
| 5 | -from gammalib import * | |
| 4 | +# | |
| 5 | +# Copyright (C) 2015 Juergen Knoedlseder | |
| 6 | +# | |
| 7 | +# This program is free software: you can redistribute it and/or modify | |
| 8 | +# it under the terms of the GNU General Public License as published by | |
| 9 | +# the Free Software Foundation, either version 3 of the License, or | |
| 10 | +# (at your option) any later version. | |
| 11 | +# | |
| 12 | +# This program is distributed in the hope that it will be useful, | |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | +# GNU General Public License for more details. | |
| 16 | +# | |
| 17 | +# You should have received a copy of the GNU General Public License | |
| 18 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 19 | +# | |
| 20 | +# ========================================================================== | |
| 21 | +import gammalib | |
| 6 | 22 | |
| 7 | 23 | |
| 8 | 24 | # ============================ # |
| ... | ... | @@ -13,16 +29,16 @@ def make_model_for_cntmap(cntmap, modname, xmlname, irf, caldb, clobber=True): |
| 13 | 29 | Make a model counts map from an XML model. |
| 14 | 30 | """ |
| 15 | 31 | # Allocate empty CTA observation |
| 16 | - obs = GCTAObservation() | |
| 32 | + obs = gammalib.GCTAObservation() | |
| 17 | 33 | |
| 18 | 34 | # Load counts map into CTA observation |
| 19 | - obs.load_binned(cntmap) | |
| 35 | + obs.load(cntmap) | |
| 20 | 36 | |
| 21 | 37 | # Set response |
| 22 | 38 | obs.response(irf, caldb) |
| 23 | 39 | |
| 24 | 40 | # Load models from XML file |
| 25 | - models = GModels(xmlname) | |
| 41 | + models = gammalib.GModels(xmlname) | |
| 26 | 42 | |
| 27 | 43 | # Loop over all bins in counts map |
| 28 | 44 | for event in obs.events(): |
| ... | ... | @@ -44,18 +60,18 @@ if __name__ == '__main__': |
| 44 | 60 | Example illustrating how to create a model map for a counts map. |
| 45 | 61 | """ |
| 46 | 62 | # Dump header |
| 47 | ||
| 48 | - print "***************************************************" | |
| 49 | - print "* Create model map for counts map using XML model *" | |
| 50 | - print "***************************************************" | |
| 51 | - print "... please wait for a few seconds" | |
| 63 | + print("") | |
| 64 | + print("***************************************************") | |
| 65 | + print("* Create model map for counts map using XML model *") | |
| 66 | + print("***************************************************") | |
| 67 | + print("... please wait for a few seconds") | |
| 52 | 68 | |
| 53 | 69 | # Set parameters |
| 54 | - irf = "kb_E_50h_v3" | |
| 55 | - caldb = "../caldb" | |
| 70 | + irf = "cta_dummy_irf" | |
| 71 | + caldb = "./caldb" | |
| 56 | 72 | xmlname = "data/crab.xml" |
| 57 | 73 | cntmap = "data/crab_cntmap.fits" |
| 58 | 74 | modname = "model.fits" |
| 59 | 75 | |
| 60 | 76 | # Make the model |
| 61 | - make_model_for_cntmap(cntmap, modname, xmlname, irf, caldb) | |
| 77 | + make_model_for_cntmap(cntmap, modname, xmlname, irf, gammalib.GCaldb(caldb)) | ... | ... |
inst/cta/test/example_sim_photons.py
| ... | ... | @@ -116,7 +116,7 @@ def show_photons(photons, xmlname, e_min, e_max, area, duration, ebins=30): |
| 116 | 116 | |
| 117 | 117 | # Plot data |
| 118 | 118 | plt.loglog(energy, counts, 'ro') |
| 119 | - plt.errorbar(energy, counts, error, fmt=None, ecolor='r') | |
| 119 | + plt.errorbar(energy, counts, error, ecolor='r') | |
| 120 | 120 | |
| 121 | 121 | # Plot model |
| 122 | 122 | plt.plot(energy, model, 'b-') | ... | ... |
inst/cta/test/example_sim_psf.py
| ... | ... | @@ -89,7 +89,7 @@ def sim_psf(response, energy, r_max=0.8, rbins=1000, nmc=1000000): |
| 89 | 89 | |
| 90 | 90 | # Plot simulated data |
| 91 | 91 | plt.plot(delta, counts, 'ro') |
| 92 | - plt.errorbar(delta, counts, error, fmt=None, ecolor='r') | |
| 92 | + plt.errorbar(delta, counts, error, ecolor='r') | |
| 93 | 93 | |
| 94 | 94 | # Plot PSF |
| 95 | 95 | plt.plot(delta, psf, 'b-') | ... | ... |
inst/cta/test/test_model.py
| ... | ... | @@ -174,10 +174,10 @@ def observation(ra=83.6331, dec=22.0145, emin=0.1, emax=100.0, |
| 174 | 174 | obs = gammalib.GCTAObservation() |
| 175 | 175 | |
| 176 | 176 | # Set response |
| 177 | - #obs.response("cta_dummy_irf", GCaldb("../caldb")) | |
| 178 | - exposure = gammalib.GCTACubeExposure("data/expcube.fits") | |
| 179 | - psf = gammalib.GCTACubePsf("data/psfcube.fits") | |
| 180 | - obs.response(exposure, psf) | |
| 177 | + exposure = gammalib.GCTACubeExposure("data/expcube.fits") | |
| 178 | + psf = gammalib.GCTACubePsf("data/psfcube.fits") | |
| 179 | + background = gammalib.GCTACubeBackground("data/bkgcube.fits") | |
| 180 | + obs.response(exposure, psf, background) | |
| 181 | 181 | |
| 182 | 182 | # Set pointing |
| 183 | 183 | dir = gammalib.GSkyDir() |
| ... | ... | @@ -201,9 +201,6 @@ def observation(ra=83.6331, dec=22.0145, emin=0.1, emax=100.0, |
| 201 | 201 | obs.livetime(duration * deadc) |
| 202 | 202 | obs.deadc(deadc) |
| 203 | 203 | |
| 204 | - # Optionally show observation | |
| 205 | - # print(obs) | |
| 206 | - | |
| 207 | 204 | # Return observation |
| 208 | 205 | return obs |
| 209 | 206 | ... | ... |
inst/mwl/test/test_python.py
| 1 | 1 | #! /usr/bin/env python |
| 2 | 2 | |
| 3 | -from gammalib import * | |
| 4 | -from math import * | |
| 3 | +import gammalib | |
| 4 | +import math | |
| 5 | +#from gammalib import * | |
| 6 | +#from math import * | |
| 5 | 7 | import os |
| 6 | 8 | |
| 7 | 9 | |
| ... | ... | @@ -17,16 +19,16 @@ def extract_data(filename): |
| 17 | 19 | styles = ['ro', 'bo', 'go'] |
| 18 | 20 | |
| 19 | 21 | # Open FITS file |
| 20 | - fits = GFits(filename) | |
| 22 | + fits = gammalib.GFits(filename) | |
| 21 | 23 | |
| 22 | 24 | # Loop over all table HDUs |
| 23 | 25 | k = 0 |
| 24 | 26 | for extno in range(fits.size()): |
| 25 | - if fits.at(extno).exttype() == GFitsHDU.HT_ASCII_TABLE or \ | |
| 26 | - fits.at(extno).exttype() == GFitsHDU.HT_BIN_TABLE: | |
| 27 | + if fits.at(extno).exttype() == gammalib.GFitsHDU.HT_ASCII_TABLE or \ | |
| 28 | + fits.at(extno).exttype() == gammalib.GFitsHDU.HT_BIN_TABLE: | |
| 27 | 29 | |
| 28 | 30 | # Load spectrum |
| 29 | - spectrum = GMWLSpectrum() | |
| 31 | + spectrum = gammalib.GMWLSpectrum() | |
| 30 | 32 | spectrum.load(filename, extno) |
| 31 | 33 | |
| 32 | 34 | # Add spectral points |
| ... | ... | @@ -42,7 +44,7 @@ def extract_data(filename): |
| 42 | 44 | y.append(yval * conv) |
| 43 | 45 | dy.append(yerr * conv) |
| 44 | 46 | plt.loglog(x, y, styles[k]) |
| 45 | - plt.errorbar(x, y, dy, fmt=None, ecolor=ecolor[k]) | |
| 47 | + plt.errorbar(x, y, dy, ecolor=ecolor[k]) | |
| 46 | 48 | |
| 47 | 49 | # Increment style counter |
| 48 | 50 | k = k + 1 |
| ... | ... | @@ -63,37 +65,38 @@ def fit_spectrum(filename, xmlname): |
| 63 | 65 | Fit spectrum. |
| 64 | 66 | """ |
| 65 | 67 | # Load observations |
| 66 | - obs = GObservations() | |
| 67 | - comptel = GMWLObservation(filename, "COMPTEL") | |
| 68 | - lat = GMWLObservation(filename, "LAT") | |
| 69 | - hess = GMWLObservation(filename, "HESS") | |
| 68 | + obs = gammalib.GObservations() | |
| 69 | + comptel = gammalib.GMWLObservation(filename, "COMPTEL") | |
| 70 | + lat = gammalib.GMWLObservation(filename, "LAT") | |
| 71 | + hess = gammalib.GMWLObservation(filename, "HESS") | |
| 70 | 72 | obs.append(comptel) |
| 71 | 73 | obs.append(lat) |
| 72 | 74 | obs.append(hess) |
| 73 | 75 | |
| 74 | 76 | # Setup model |
| 75 | - models = GModels(xmlname) | |
| 77 | + models = gammalib.GModels(xmlname) | |
| 76 | 78 | obs.models(models) |
| 77 | 79 | |
| 78 | 80 | # Perform optimization |
| 79 | - opt = GOptimizerLM() | |
| 81 | + opt = gammalib.GOptimizerLM() | |
| 80 | 82 | opt.max_iter(1000) |
| 81 | 83 | obs.optimize(opt) |
| 82 | - print obs | |
| 84 | + print(obs) | |
| 83 | 85 | |
| 84 | 86 | # Get model values |
| 85 | 87 | x = np.power(10., np.arange(-1., 8., 0.1)) |
| 86 | 88 | y = [] |
| 87 | - d = GSkyDir() | |
| 89 | + d = gammalib.GSkyDir() | |
| 88 | 90 | d.radec_deg(83.6331, 22.0145) |
| 89 | - e = GEnergy() | |
| 90 | - t = GTime() | |
| 91 | + e = gammalib.GEnergy() | |
| 92 | + t = gammalib.GTime() | |
| 91 | 93 | for energy in x: |
| 92 | 94 | e.MeV(energy) |
| 93 | 95 | yval = 0.0 |
| 94 | 96 | for model in obs.models(): |
| 95 | 97 | if model.type() == "PointSource": |
| 96 | - yval += model.value(GPhoton(d, e, t)) * energy * energy * 1.6021765e-6 | |
| 98 | + yval += model.value(gammalib.GPhoton(d, e, t)) * \ | |
| 99 | + energy * energy * 1.6021765e-6 | |
| 97 | 100 | y.append(yval) |
| 98 | 101 | plt.plot(x, y) |
| 99 | 102 | |
| ... | ... | @@ -106,10 +109,10 @@ if __name__ == '__main__': |
| 106 | 109 | Perform testing. |
| 107 | 110 | """ |
| 108 | 111 | # Dump result |
| 109 | ||
| 110 | - print "****************************" | |
| 111 | - print "* Python interface testing *" | |
| 112 | - print "****************************" | |
| 112 | + print("") | |
| 113 | + print("****************************") | |
| 114 | + print("* Python interface testing *") | |
| 115 | + print("****************************") | |
| 113 | 116 | |
| 114 | 117 | # Only proceed if matplotlib is available |
| 115 | 118 | try: |
| ... | ... | @@ -131,4 +134,4 @@ if __name__ == '__main__': |
| 131 | 134 | plt.show() |
| 132 | 135 | |
| 133 | 136 | except ImportError: |
| 134 | - print "Matplotlib is not (correctly) installed on your system." | |
| 137 | + print("Matplotlib is not (correctly) installed on your system.") | ... | ... |