Commit ddd8c148bb4b883264e7f2e41cb58d0683d285c7

Authored by Jürgen Knödlseder
1 parent e3348b38

Adapt script to actual GammaLib interface

inst/cta/test/benchmark_irf_computation.py
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 # Monte-Carlo simulations. This script is used to validate the absolute 6 # Monte-Carlo simulations. This script is used to validate the absolute
7 # normalization of the CTA response computations. 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 # This program is free software: you can redistribute it and/or modify 11 # This program is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by 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,7 +117,7 @@ def binned_irf(model, cntmap, irf, caldb, cntref):
117 # =========================== # 117 # =========================== #
118 # CTA stacked IRF computation # 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 Perform stacked IRF computation. 122 Perform stacked IRF computation.
123 """ 123 """
@@ -132,9 +132,10 @@ def stacked_irf(model, cntmap, expcube, psfcube, cntref): @@ -132,9 +132,10 @@ def stacked_irf(model, cntmap, expcube, psfcube, cntref):
132 obs.load(cntmap) 132 obs.load(cntmap)
133 133
134 # Specify response for CTA observation 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 # Load model to describe the data from XML file 140 # Load model to describe the data from XML file
140 models = gammalib.GModels(model) 141 models = gammalib.GModels(model)
@@ -179,6 +180,7 @@ if __name__ == '__main__': @@ -179,6 +180,7 @@ if __name__ == '__main__':
179 caldb = "../caldb" 180 caldb = "../caldb"
180 expcube = "data/expcube.fits" 181 expcube = "data/expcube.fits"
181 psfcube = "data/psfcube.fits" 182 psfcube = "data/psfcube.fits"
  183 + bkgcube = "data/bkgcube.fits"
182 184
183 # Set tests 185 # Set tests
184 tests = ["point", "disk", "gauss", "shell", "ellipse", "diffuse"] 186 tests = ["point", "disk", "gauss", "shell", "ellipse", "diffuse"]
@@ -235,4 +237,4 @@ if __name__ == '__main__': @@ -235,4 +237,4 @@ if __name__ == '__main__':
235 binned_irf(model, cntmap, irf, gammalib.GCaldb(caldb), cntref) 237 binned_irf(model, cntmap, irf, gammalib.GCaldb(caldb), cntref)
236 238
237 # Perform stacked computation 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,7 +3,7 @@
3 # This script performs a benchmark for maximum likelihood fitting of CTA 3 # This script performs a benchmark for maximum likelihood fitting of CTA
4 # data. 4 # data.
5 # 5 #
6 -# Copyright (C) 2012-2014 Juergen Knoedlseder 6 +# Copyright (C) 2012-2015 Juergen Knoedlseder
7 # 7 #
8 # This program is free software: you can redistribute it and/or modify 8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by 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,7 +142,7 @@ def binned_analysis(model, cntmap, irf, caldb):
142 # ==================== # 142 # ==================== #
143 # CTA stacked analysis # 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 Perform stacked maximum likelihood fitting of CTA data using GammaLib 147 Perform stacked maximum likelihood fitting of CTA data using GammaLib
148 classes. 148 classes.
@@ -166,9 +166,10 @@ def stacked_analysis(model, cntmap, expcube, psfcube): @@ -166,9 +166,10 @@ def stacked_analysis(model, cntmap, expcube, psfcube):
166 cta_obs.load(cntmap) 166 cta_obs.load(cntmap)
167 167
168 # Specify response for CTA observation 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 # Append CTA observation to observation container 174 # Append CTA observation to observation container
174 obs.append(cta_obs) 175 obs.append(cta_obs)
@@ -217,6 +218,7 @@ if __name__ == '__main__': @@ -217,6 +218,7 @@ if __name__ == '__main__':
217 caldb = "../caldb" 218 caldb = "../caldb"
218 expcube = "data/expcube.fits" 219 expcube = "data/expcube.fits"
219 psfcube = "data/psfcube.fits" 220 psfcube = "data/psfcube.fits"
  221 + bkgcube = "data/bkgcube.fits"
220 222
221 # Set tests 223 # Set tests
222 tests = ["point", "disk", "gauss", "shell", "ellipse", "diffuse"] 224 tests = ["point", "disk", "gauss", "shell", "ellipse", "diffuse"]
@@ -273,4 +275,4 @@ if __name__ == '__main__': @@ -273,4 +275,4 @@ if __name__ == '__main__':
273 binned_analysis(model, cntmap, irf, gammalib.GCaldb(caldb)) 275 binned_analysis(model, cntmap, irf, gammalib.GCaldb(caldb))
274 276
275 # Perform stacked analysis 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,7 +115,7 @@ def binned_analysis(model, cntmap, irf, caldb):
115 # ==================== # 115 # ==================== #
116 # CTA stacked analysis # 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 Perform stacked maximum likelihood fitting of CTA data using GammaLib 120 Perform stacked maximum likelihood fitting of CTA data using GammaLib
121 classes. The function performs two fits, one with the full model and 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,9 +141,10 @@ def stacked_analysis(model, cntmap, expcube, psfcube):
141 cta_obs.load(cntmap) 141 cta_obs.load(cntmap)
142 142
143 # Specify response for CTA observation 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 # Append CTA observation to observation container 149 # Append CTA observation to observation container
149 obs.append(cta_obs) 150 obs.append(cta_obs)
@@ -225,12 +226,13 @@ if __name__ == '__main__': @@ -225,12 +226,13 @@ if __name__ == '__main__':
225 cntmap = "data/crab_cntmap.fits" 226 cntmap = "data/crab_cntmap.fits"
226 expcube = "data/expcube.fits" 227 expcube = "data/expcube.fits"
227 psfcube = "data/psfcube.fits" 228 psfcube = "data/psfcube.fits"
  229 + bkgcube = "data/bkgcube.fits"
228 230
229 # Perform binned analysis 231 # Perform binned analysis
230 results_binned = binned_analysis(model, cntmap, irf, gammalib.GCaldb(caldb)) 232 results_binned = binned_analysis(model, cntmap, irf, gammalib.GCaldb(caldb))
231 233
232 # Perform stacked analysis 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 # Print model results 237 # Print model results
236 #print(result) 238 #print(result)
inst/cta/test/example_make_model.py
1 #! /usr/bin/env python 1 #! /usr/bin/env python
2 -# ===========================================================================================# 2 +# ==========================================================================
3 # This script makes a model map from an XML file. 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,16 +29,16 @@ def make_model_for_cntmap(cntmap, modname, xmlname, irf, caldb, clobber=True):
13 Make a model counts map from an XML model. 29 Make a model counts map from an XML model.
14 """ 30 """
15 # Allocate empty CTA observation 31 # Allocate empty CTA observation
16 - obs = GCTAObservation() 32 + obs = gammalib.GCTAObservation()
17 33
18 # Load counts map into CTA observation 34 # Load counts map into CTA observation
19 - obs.load_binned(cntmap) 35 + obs.load(cntmap)
20 36
21 # Set response 37 # Set response
22 obs.response(irf, caldb) 38 obs.response(irf, caldb)
23 39
24 # Load models from XML file 40 # Load models from XML file
25 - models = GModels(xmlname) 41 + models = gammalib.GModels(xmlname)
26 42
27 # Loop over all bins in counts map 43 # Loop over all bins in counts map
28 for event in obs.events(): 44 for event in obs.events():
@@ -44,18 +60,18 @@ if __name__ == &#39;__main__&#39;: @@ -44,18 +60,18 @@ if __name__ == &#39;__main__&#39;:
44 Example illustrating how to create a model map for a counts map. 60 Example illustrating how to create a model map for a counts map.
45 """ 61 """
46 # Dump header 62 # Dump header
47 - print  
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 # Set parameters 69 # Set parameters
54 - irf = "kb_E_50h_v3"  
55 - caldb = "../caldb" 70 + irf = "cta_dummy_irf"
  71 + caldb = "./caldb"
56 xmlname = "data/crab.xml" 72 xmlname = "data/crab.xml"
57 cntmap = "data/crab_cntmap.fits" 73 cntmap = "data/crab_cntmap.fits"
58 modname = "model.fits" 74 modname = "model.fits"
59 75
60 # Make the model 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,7 +116,7 @@ def show_photons(photons, xmlname, e_min, e_max, area, duration, ebins=30):
116 116
117 # Plot data 117 # Plot data
118 plt.loglog(energy, counts, 'ro') 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 # Plot model 121 # Plot model
122 plt.plot(energy, model, 'b-') 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,7 +89,7 @@ def sim_psf(response, energy, r_max=0.8, rbins=1000, nmc=1000000):
89 89
90 # Plot simulated data 90 # Plot simulated data
91 plt.plot(delta, counts, 'ro') 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 # Plot PSF 94 # Plot PSF
95 plt.plot(delta, psf, 'b-') 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,10 +174,10 @@ def observation(ra=83.6331, dec=22.0145, emin=0.1, emax=100.0,
174 obs = gammalib.GCTAObservation() 174 obs = gammalib.GCTAObservation()
175 175
176 # Set response 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 # Set pointing 182 # Set pointing
183 dir = gammalib.GSkyDir() 183 dir = gammalib.GSkyDir()
@@ -201,9 +201,6 @@ def observation(ra=83.6331, dec=22.0145, emin=0.1, emax=100.0, @@ -201,9 +201,6 @@ def observation(ra=83.6331, dec=22.0145, emin=0.1, emax=100.0,
201 obs.livetime(duration * deadc) 201 obs.livetime(duration * deadc)
202 obs.deadc(deadc) 202 obs.deadc(deadc)
203 203
204 - # Optionally show observation  
205 - # print(obs)  
206 -  
207 # Return observation 204 # Return observation
208 return obs 205 return obs
209 206
inst/mwl/test/test_python.py
1 #! /usr/bin/env python 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 import os 7 import os
6 8
7 9
@@ -17,16 +19,16 @@ def extract_data(filename): @@ -17,16 +19,16 @@ def extract_data(filename):
17 styles = ['ro', 'bo', 'go'] 19 styles = ['ro', 'bo', 'go']
18 20
19 # Open FITS file 21 # Open FITS file
20 - fits = GFits(filename) 22 + fits = gammalib.GFits(filename)
21 23
22 # Loop over all table HDUs 24 # Loop over all table HDUs
23 k = 0 25 k = 0
24 for extno in range(fits.size()): 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 # Load spectrum 30 # Load spectrum
29 - spectrum = GMWLSpectrum() 31 + spectrum = gammalib.GMWLSpectrum()
30 spectrum.load(filename, extno) 32 spectrum.load(filename, extno)
31 33
32 # Add spectral points 34 # Add spectral points
@@ -42,7 +44,7 @@ def extract_data(filename): @@ -42,7 +44,7 @@ def extract_data(filename):
42 y.append(yval * conv) 44 y.append(yval * conv)
43 dy.append(yerr * conv) 45 dy.append(yerr * conv)
44 plt.loglog(x, y, styles[k]) 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 # Increment style counter 49 # Increment style counter
48 k = k + 1 50 k = k + 1
@@ -63,37 +65,38 @@ def fit_spectrum(filename, xmlname): @@ -63,37 +65,38 @@ def fit_spectrum(filename, xmlname):
63 Fit spectrum. 65 Fit spectrum.
64 """ 66 """
65 # Load observations 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 obs.append(comptel) 72 obs.append(comptel)
71 obs.append(lat) 73 obs.append(lat)
72 obs.append(hess) 74 obs.append(hess)
73 75
74 # Setup model 76 # Setup model
75 - models = GModels(xmlname) 77 + models = gammalib.GModels(xmlname)
76 obs.models(models) 78 obs.models(models)
77 79
78 # Perform optimization 80 # Perform optimization
79 - opt = GOptimizerLM() 81 + opt = gammalib.GOptimizerLM()
80 opt.max_iter(1000) 82 opt.max_iter(1000)
81 obs.optimize(opt) 83 obs.optimize(opt)
82 - print obs 84 + print(obs)
83 85
84 # Get model values 86 # Get model values
85 x = np.power(10., np.arange(-1., 8., 0.1)) 87 x = np.power(10., np.arange(-1., 8., 0.1))
86 y = [] 88 y = []
87 - d = GSkyDir() 89 + d = gammalib.GSkyDir()
88 d.radec_deg(83.6331, 22.0145) 90 d.radec_deg(83.6331, 22.0145)
89 - e = GEnergy()  
90 - t = GTime() 91 + e = gammalib.GEnergy()
  92 + t = gammalib.GTime()
91 for energy in x: 93 for energy in x:
92 e.MeV(energy) 94 e.MeV(energy)
93 yval = 0.0 95 yval = 0.0
94 for model in obs.models(): 96 for model in obs.models():
95 if model.type() == "PointSource": 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 y.append(yval) 100 y.append(yval)
98 plt.plot(x, y) 101 plt.plot(x, y)
99 102
@@ -106,10 +109,10 @@ if __name__ == &#39;__main__&#39;: @@ -106,10 +109,10 @@ if __name__ == &#39;__main__&#39;:
106 Perform testing. 109 Perform testing.
107 """ 110 """
108 # Dump result 111 # Dump result
109 - print  
110 - print "****************************"  
111 - print "* Python interface testing *"  
112 - print "****************************" 112 + print("")
  113 + print("****************************")
  114 + print("* Python interface testing *")
  115 + print("****************************")
113 116
114 # Only proceed if matplotlib is available 117 # Only proceed if matplotlib is available
115 try: 118 try:
@@ -131,4 +134,4 @@ if __name__ == &#39;__main__&#39;: @@ -131,4 +134,4 @@ if __name__ == &#39;__main__&#39;:
131 plt.show() 134 plt.show()
132 135
133 except ImportError: 136 except ImportError:
134 - print "Matplotlib is not (correctly) installed on your system." 137 + print("Matplotlib is not (correctly) installed on your system.")