From 03ebfe59beb1682db1dba4796041ffcb47e0d35d Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Sat, 6 Aug 2016 02:27:17 +0200 Subject: [PATCH] Add ctool and cscript pars() methods (#1838) --- ChangeLog | 3 ++- NEWS | 6 ++++-- README.md | 2 +- pyext/ctool.i | 47 ++++++++++++++++++++++++++++++++++++++++++++++- test/test_ctbin.py | 16 ++++++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 48c5409..4295119 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,9 @@ -2016-08-04 +2016-08-06 * Version 1.2.0 released ======================== + Add ctool and cscript pars() methods (#1838) Fix ctbin::save() bug for empty observation container (#1835) Fix exception when calling ctools clear() methods (#1835) Add support for new spectral models in ctulimit (#1832) diff --git a/NEWS b/NEWS index 37d4feb..3d2ad13 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,6 @@ New Features and Important Changes in ctools 1.2.0 -4 August 2016 +6 August 2016 Introduction @@ -17,7 +17,9 @@ from the command line are included in the coverage reports (#1827). Python interface ---------------- -None +A pars() method has been added to the Python interface of all ctools and +cscripts that allows assigning parameters from a dictionary or retrieving +all parameters in a dictionary (#1838). Support model diff --git a/README.md b/README.md index 7c8ecdb..357dae6 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ctools information ================== -* Version: 1.2.0.dev1 (4 August 2016) +* Version: 1.2.0.dev1 (6 August 2016) * Author: Juergen Knoedlseder (jurgen.knodlseder@irap.omp.eu) * GammaLib dependency: 1.2.0.dev1 diff --git a/pyext/ctool.i b/pyext/ctool.i index 5059b7f..d81fb2e 100644 --- a/pyext/ctool.i +++ b/pyext/ctool.i @@ -181,7 +181,7 @@ public: } /***********************************************************************//** - * @brief cscript base class Python extensions + * @brief ctool and cscript base class Python extensions ***************************************************************************/ %pythoncode %{ @@ -210,4 +210,49 @@ def _init_cscript(self, argv): self._log_header() self._log.date(True) cscript._init_cscript = _init_cscript + +# Return or assigns user parameters in form of a dictionary +# +# This function either returns or assigns user parameters in form of a +# dictionary. The assignment function takes a dictionary as argument and +# the function then loops over all keys in that dictionary and assigns +# the value to the parameter. For example +# +# >>> tool.pars({'ra': 83.63, 'dec': 22.01}) +# +# is equivalent to +# +# >>> tool['ra'] = 83.63 +# >>> tool['dec'] = 22.01 +# +# and assigns the 'ra' and 'dec' parameters of the tool. The return function +# takes no argument and returns a dictionary with the parameter names as +# keys and the current values as values. No parameter querying is performed. +# For example +# +# >>> d = tool.pars() +# +# puts all parameters in the dictionary d. +def _parameters(self, *args): + if len(args) == 0: + d = {} + for par in self._pars(): + if par.type() == 'b': + v = gammalib.tolower(par.current_value()) + value = (v == "yes" or v == "y" or v == "true" or v == "t") + d[par.name()] = value + elif par.type() == 'i': + d[par.name()] = int(par.current_value()) + elif par.type() == 'r': + d[par.name()] = float(par.current_value()) + else: + d[par.name()] = str(par.current_value()) + return d + elif len(args) == 1: + for key in args[0]: + self[key] = args[0][key] + else: + raise TypeError('pars() takes 0 or 1 arguments (%d given)' % len(args)) +ctool.pars = _parameters +cscript.pars = _parameters %} diff --git a/test/test_ctbin.py b/test/test_ctbin.py index a54f042..08ce84c 100755 --- a/test/test_ctbin.py +++ b/test/test_ctbin.py @@ -235,6 +235,22 @@ class Test(test): evt = gammalib.GCTAEventCube('ctbin_py3.fits') self._check_cube(evt, 5542, multiplier=3) + # And finally go for a fully Pythonic version with all parameters + # being specified in a dictionary + dict = {'inobs': self._events, 'ebinalg': 'LOG', 'emin': 0.1, + 'emax': 100.0, 'enumbins': 10, 'nxpix': 40, 'nypix': 40, + 'binsz': 0.1, 'coordsys': 'CEL', 'proj': 'CAR', + 'xref': 83.63, 'yref': 22.01, 'outcube': 'ctbin_py4.fits', + 'logfile': 'ctbin_py4.log', 'chatter': 2} + bin = ctools.ctbin() + bin.pars(dict) + bin.logFileOpen() + bin.execute() + + # Load counts cube and check content + evt = gammalib.GCTAEventCube('ctbin_py4.fits') + self._check_cube(evt, 5542) + # Return return -- libgit2 0.21.4