Commit 03ebfe59beb1682db1dba4796041ffcb47e0d35d

Authored by Jürgen Knödlseder
1 parent 9830efd9

Add ctool and cscript pars() methods (#1838)

The pars() methods allow to assign or to retrieve all application parameters in a dictionary. Makes ctools a bit more "Pythonic".
ChangeLog
1 -2016-08-04 1 +2016-08-06
2 2
3 * Version 1.2.0 released 3 * Version 1.2.0 released
4 ======================== 4 ========================
5 5
  6 + Add ctool and cscript pars() methods (#1838)
6 Fix ctbin::save() bug for empty observation container (#1835) 7 Fix ctbin::save() bug for empty observation container (#1835)
7 Fix exception when calling ctools clear() methods (#1835) 8 Fix exception when calling ctools clear() methods (#1835)
8 Add support for new spectral models in ctulimit (#1832) 9 Add support for new spectral models in ctulimit (#1832)
1 New Features and Important Changes in ctools 1.2.0 1 New Features and Important Changes in ctools 1.2.0
2 2
3 -4 August 2016 3 +6 August 2016
4 4
5 5
6 Introduction 6 Introduction
@@ -17,7 +17,9 @@ from the command line are included in the coverage reports (#1827). @@ -17,7 +17,9 @@ from the command line are included in the coverage reports (#1827).
17 17
18 Python interface 18 Python interface
19 ---------------- 19 ----------------
20 -None 20 +A pars() method has been added to the Python interface of all ctools and
  21 +cscripts that allows assigning parameters from a dictionary or retrieving
  22 +all parameters in a dictionary (#1838).
21 23
22 24
23 Support model 25 Support model
README.md
1 ctools information 1 ctools information
2 ================== 2 ==================
3 -* Version: 1.2.0.dev1 (4 August 2016) 3 +* Version: 1.2.0.dev1 (6 August 2016)
4 * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu) 4 * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu)
5 * GammaLib dependency: 1.2.0.dev1 5 * GammaLib dependency: 1.2.0.dev1
6 6
pyext/ctool.i
@@ -181,7 +181,7 @@ public: @@ -181,7 +181,7 @@ public:
181 } 181 }
182 182
183 /***********************************************************************//** 183 /***********************************************************************//**
184 - * @brief cscript base class Python extensions 184 + * @brief ctool and cscript base class Python extensions
185 ***************************************************************************/ 185 ***************************************************************************/
186 %pythoncode %{ 186 %pythoncode %{
187 187
@@ -210,4 +210,49 @@ def _init_cscript(self, argv): @@ -210,4 +210,49 @@ def _init_cscript(self, argv):
210 self._log_header() 210 self._log_header()
211 self._log.date(True) 211 self._log.date(True)
212 cscript._init_cscript = _init_cscript 212 cscript._init_cscript = _init_cscript
  213 +
  214 +# Return or assigns user parameters in form of a dictionary
  215 +#
  216 +# This function either returns or assigns user parameters in form of a
  217 +# dictionary. The assignment function takes a dictionary as argument and
  218 +# the function then loops over all keys in that dictionary and assigns
  219 +# the value to the parameter. For example
  220 +#
  221 +# >>> tool.pars({'ra': 83.63, 'dec': 22.01})
  222 +#
  223 +# is equivalent to
  224 +#
  225 +# >>> tool['ra'] = 83.63
  226 +# >>> tool['dec'] = 22.01
  227 +#
  228 +# and assigns the 'ra' and 'dec' parameters of the tool. The return function
  229 +# takes no argument and returns a dictionary with the parameter names as
  230 +# keys and the current values as values. No parameter querying is performed.
  231 +# For example
  232 +#
  233 +# >>> d = tool.pars()
  234 +#
  235 +# puts all parameters in the dictionary d.
  236 +def _parameters(self, *args):
  237 + if len(args) == 0:
  238 + d = {}
  239 + for par in self._pars():
  240 + if par.type() == 'b':
  241 + v = gammalib.tolower(par.current_value())
  242 + value = (v == "yes" or v == "y" or v == "true" or v == "t")
  243 + d[par.name()] = value
  244 + elif par.type() == 'i':
  245 + d[par.name()] = int(par.current_value())
  246 + elif par.type() == 'r':
  247 + d[par.name()] = float(par.current_value())
  248 + else:
  249 + d[par.name()] = str(par.current_value())
  250 + return d
  251 + elif len(args) == 1:
  252 + for key in args[0]:
  253 + self[key] = args[0][key]
  254 + else:
  255 + raise TypeError('pars() takes 0 or 1 arguments (%d given)' % len(args))
  256 +ctool.pars = _parameters
  257 +cscript.pars = _parameters
213 %} 258 %}
test/test_ctbin.py
@@ -235,6 +235,22 @@ class Test(test): @@ -235,6 +235,22 @@ class Test(test):
235 evt = gammalib.GCTAEventCube('ctbin_py3.fits') 235 evt = gammalib.GCTAEventCube('ctbin_py3.fits')
236 self._check_cube(evt, 5542, multiplier=3) 236 self._check_cube(evt, 5542, multiplier=3)
237 237
  238 + # And finally go for a fully Pythonic version with all parameters
  239 + # being specified in a dictionary
  240 + dict = {'inobs': self._events, 'ebinalg': 'LOG', 'emin': 0.1,
  241 + 'emax': 100.0, 'enumbins': 10, 'nxpix': 40, 'nypix': 40,
  242 + 'binsz': 0.1, 'coordsys': 'CEL', 'proj': 'CAR',
  243 + 'xref': 83.63, 'yref': 22.01, 'outcube': 'ctbin_py4.fits',
  244 + 'logfile': 'ctbin_py4.log', 'chatter': 2}
  245 + bin = ctools.ctbin()
  246 + bin.pars(dict)
  247 + bin.logFileOpen()
  248 + bin.execute()
  249 +
  250 + # Load counts cube and check content
  251 + evt = gammalib.GCTAEventCube('ctbin_py4.fits')
  252 + self._check_cube(evt, 5542)
  253 +
238 # Return 254 # Return
239 return 255 return
240 256