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