Commit c2657fcb9cc772743a543a8d75c016f5bcc15753

Authored by Michael Mayer
2 parents df492129 5c264dcb

Merge branch 'devel' of https://cta-gitlab.irap.omp.eu/ctools/ctools into 1646-write-iact-docs

examples/benchmark_cta_analysis.py
@@ -32,12 +32,11 @@ except: @@ -32,12 +32,11 @@ except:
32 # ========================== # 32 # ========================== #
33 # Unbinned analysis pipeline # 33 # Unbinned analysis pipeline #
34 # ========================== # 34 # ========================== #
35 -def unbinned_pipeline(duration): 35 +def unbinned_pipeline(model_name, duration):
36 """ 36 """
37 Unbinned analysis pipeline. 37 Unbinned analysis pipeline.
38 """ 38 """
39 # Set script parameters 39 # Set script parameters
40 - model_name = "${CTOOLS}/share/models/crab.xml"  
41 caldb = "prod2" 40 caldb = "prod2"
42 irf = "South_50h" 41 irf = "South_50h"
43 ra = 83.63 42 ra = 83.63
@@ -50,7 +49,7 @@ def unbinned_pipeline(duration): @@ -50,7 +49,7 @@ def unbinned_pipeline(duration):
50 rad_select = 3.0 49 rad_select = 3.0
51 50
52 # Get start CPU time 51 # Get start CPU time
53 - tstart = time.clock() 52 + cpu_start = time.clock()
54 53
55 # Simulate events 54 # Simulate events
56 sim = ctools.ctobssim() 55 sim = ctools.ctobssim()
@@ -78,30 +77,29 @@ def unbinned_pipeline(duration): @@ -78,30 +77,29 @@ def unbinned_pipeline(duration):
78 select.run() 77 select.run()
79 78
80 # Get ctlike start CPU time 79 # Get ctlike start CPU time
81 - tctlike = time.clock() 80 + cpu_ctlike = time.clock()
82 81
83 # Perform maximum likelihood fitting 82 # Perform maximum likelihood fitting
84 like = ctools.ctlike(select.obs()) 83 like = ctools.ctlike(select.obs())
85 like.run() 84 like.run()
86 85
87 - # Get stop CPU time  
88 - tstop = time.clock()  
89 - telapsed = tstop - tstart  
90 - tctlike = tstop - tctlike 86 + # Get stop CPU time and compute elapsed times
  87 + cpu_stop = time.clock()
  88 + cpu_elapsed = cpu_stop - cpu_start
  89 + cpu_ctlike = cpu_stop - cpu_ctlike
91 90
92 # Return 91 # Return
93 - return telapsed, tctlike 92 + return cpu_elapsed, cpu_ctlike
94 93
95 94
96 # ======================== # 95 # ======================== #
97 # Binned analysis pipeline # 96 # Binned analysis pipeline #
98 # ======================== # 97 # ======================== #
99 -def binned_pipeline(duration): 98 +def binned_pipeline(model_name, duration):
100 """ 99 """
101 Binned analysis pipeline. 100 Binned analysis pipeline.
102 """ 101 """
103 # Set script parameters 102 # Set script parameters
104 - model_name = "${CTOOLS}/share/models/crab.xml"  
105 caldb = "prod2" 103 caldb = "prod2"
106 irf = "South_50h" 104 irf = "South_50h"
107 ra = 83.63 105 ra = 83.63
@@ -119,7 +117,7 @@ def binned_pipeline(duration): @@ -119,7 +117,7 @@ def binned_pipeline(duration):
119 proj = "CAR" 117 proj = "CAR"
120 118
121 # Get start CPU time 119 # Get start CPU time
122 - tstart = time.clock() 120 + cpu_start = time.clock()
123 121
124 # Simulate events 122 # Simulate events
125 sim = ctools.ctobssim() 123 sim = ctools.ctobssim()
@@ -151,30 +149,29 @@ def binned_pipeline(duration): @@ -151,30 +149,29 @@ def binned_pipeline(duration):
151 bin.run() 149 bin.run()
152 150
153 # Get ctlike start CPU time 151 # Get ctlike start CPU time
154 - tctlike = time.clock() 152 + cpu_ctlike = time.clock()
155 153
156 # Perform maximum likelihood fitting 154 # Perform maximum likelihood fitting
157 like = ctools.ctlike(bin.obs()) 155 like = ctools.ctlike(bin.obs())
158 like.run() 156 like.run()
159 157
160 - # Get stop CPU time  
161 - tstop = time.clock()  
162 - telapsed = tstop - tstart  
163 - tctlike = tstop - tctlike 158 + # Get stop CPU time and compute elapsed times
  159 + cpu_stop = time.clock()
  160 + cpu_elapsed = cpu_stop - cpu_start
  161 + cpu_ctlike = cpu_stop - cpu_ctlike
164 162
165 # Return 163 # Return
166 - return telapsed, tctlike 164 + return cpu_elapsed, cpu_ctlike
167 165
168 166
169 # ========================= # 167 # ========================= #
170 # Stacked analysis pipeline # 168 # Stacked analysis pipeline #
171 # ========================= # 169 # ========================= #
172 -def stacked_pipeline(duration): 170 +def stacked_pipeline(model_name, duration):
173 """ 171 """
174 Stacked analysis pipeline. 172 Stacked analysis pipeline.
175 """ 173 """
176 # Set script parameters 174 # Set script parameters
177 - model_name = "${CTOOLS}/share/models/crab.xml"  
178 caldb = "prod2" 175 caldb = "prod2"
179 irf = "South_50h" 176 irf = "South_50h"
180 ra = 83.63 177 ra = 83.63
@@ -192,7 +189,7 @@ def stacked_pipeline(duration): @@ -192,7 +189,7 @@ def stacked_pipeline(duration):
192 proj = "CAR" 189 proj = "CAR"
193 190
194 # Get start CPU time 191 # Get start CPU time
195 - tstart = time.clock() 192 + cpu_start = time.clock()
196 193
197 # Simulate events 194 # Simulate events
198 sim = ctools.ctobssim() 195 sim = ctools.ctobssim()
@@ -283,19 +280,19 @@ def stacked_pipeline(duration): @@ -283,19 +280,19 @@ def stacked_pipeline(duration):
283 bin.obs()[0].response(expcube.expcube(), psfcube.psfcube(), bkgcube.bkgcube()) 280 bin.obs()[0].response(expcube.expcube(), psfcube.psfcube(), bkgcube.bkgcube())
284 281
285 # Get ctlike start CPU time 282 # Get ctlike start CPU time
286 - tctlike = time.clock() 283 + cpu_ctlike = time.clock()
287 284
288 # Perform maximum likelihood fitting 285 # Perform maximum likelihood fitting
289 like = ctools.ctlike(bin.obs()) 286 like = ctools.ctlike(bin.obs())
290 like.run() 287 like.run()
291 288
292 - # Get stop CPU time  
293 - tstop = time.clock()  
294 - telapsed = tstop - tstart  
295 - tctlike = tstop - tctlike 289 + # Get stop CPU time and compute elapsed times
  290 + cpu_stop = time.clock()
  291 + cpu_elapsed = cpu_stop - cpu_start
  292 + cpu_ctlike = cpu_stop - cpu_ctlike
296 293
297 # Return 294 # Return
298 - return telapsed, tctlike 295 + return cpu_elapsed, cpu_ctlike
299 296
300 297
301 #==========================# 298 #==========================#
@@ -304,7 +301,11 @@ def stacked_pipeline(duration): @@ -304,7 +301,11 @@ def stacked_pipeline(duration):
304 if __name__ == '__main__': 301 if __name__ == '__main__':
305 """ 302 """
306 """ 303 """
307 - # Dump header 304 + # Initialise parameters
  305 + title = "CTA analysis benchmark"
  306 + model_name = "${CTOOLS}/share/models/crab.xml"
  307 +
  308 + # Log header
308 print("*************************************") 309 print("*************************************")
309 print("* CTA analysis benchmark *") 310 print("* CTA analysis benchmark *")
310 print("*************************************") 311 print("*************************************")
@@ -324,9 +325,9 @@ if __name__ == '__main__': @@ -324,9 +325,9 @@ if __name__ == '__main__':
324 for i in range(10): 325 for i in range(10):
325 326
326 # Perform analyses 327 # Perform analyses
327 - t_unbinned, ct_unbinned = unbinned_pipeline(duration)  
328 - t_binned, ct_binned = binned_pipeline(duration)  
329 - t_stacked, ct_stacked = stacked_pipeline(duration) 328 + t_unbinned, ct_unbinned = unbinned_pipeline(model_name, duration)
  329 + t_binned, ct_binned = binned_pipeline(model_name, duration)
  330 + t_stacked, ct_stacked = stacked_pipeline(model_name, duration)
330 331
331 # Collect results 332 # Collect results
332 a_duration.append(duration/3600.0) 333 a_duration.append(duration/3600.0)
@@ -348,14 +349,14 @@ if __name__ == '__main__': @@ -348,14 +349,14 @@ if __name__ == '__main__':
348 # Optionally plot results 349 # Optionally plot results
349 if has_matplotlib: 350 if has_matplotlib:
350 plt.figure(1) 351 plt.figure(1)
351 - plt.title("CTA analysis benchmark") 352 + plt.title(title)
352 plt.loglog(a_duration, a_unbinned, 'ro-', label='unbinned') 353 plt.loglog(a_duration, a_unbinned, 'ro-', label='unbinned')
353 plt.loglog(a_duration, a_binned, 'bo-', label='binned') 354 plt.loglog(a_duration, a_binned, 'bo-', label='binned')
354 plt.loglog(a_duration, a_stacked, 'go-', label='stacked') 355 plt.loglog(a_duration, a_stacked, 'go-', label='stacked')
355 plt.loglog(a_duration, c_unbinned, 'ro--') 356 plt.loglog(a_duration, c_unbinned, 'ro--')
356 plt.loglog(a_duration, c_binned, 'bo--') 357 plt.loglog(a_duration, c_binned, 'bo--')
357 plt.loglog(a_duration, c_stacked, 'go--') 358 plt.loglog(a_duration, c_stacked, 'go--')
358 - plt.xlabel("Duration (hours)") 359 + plt.xlabel("Observation duration (hours)")
359 plt.ylabel("CPU time (seconds)") 360 plt.ylabel("CPU time (seconds)")
360 plt.legend(loc="lower right") 361 plt.legend(loc="lower right")
361 plt.show() 362 plt.show()
pyext/ctools/__init__.py.in
1 # ========================================================================== 1 # ==========================================================================
2 # ctools Python module 2 # ctools Python module
3 # 3 #
4 -# Copyright (C) 2015 Juergen Knoedlseder 4 +# Copyright (C) 2015-2016 Juergen Knoedlseder
5 # 5 #
6 # This program is free software: you can redistribute it and/or modify 6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by 7 # it under the terms of the GNU General Public License as published by
@@ -21,7 +21,12 @@ @@ -21,7 +21,12 @@
21 import gammalib 21 import gammalib
22 22
23 # Import sub modules 23 # Import sub modules
24 -from .tools import * 24 +#from .tools import *
  25 +import sys
  26 +if sys.version_info[0] < 3:
  27 + from tools import *
  28 +else:
  29 + from .tools import *
25 30
26 # Remove swig register stuff 31 # Remove swig register stuff
27 bad_entries = [entry for entry in list(locals()) 32 bad_entries = [entry for entry in list(locals())