Change request #1494

Simplify GApplication parameter setting from Python

Added by Deil Christoph almost 9 years ago. Updated over 8 years ago.

Status:ClosedStart date:06/30/2015
Priority:NormalDue date:
Assigned To:Deil Christoph% Done:

100%

Category:-
Target version:1.0.0
Duration:

Description

As discussed today, setting GApplication parameters from Python can be simplified.

Example: http://cta.irap.omp.eu/ctools-devel/user_manual/getting_started/python.html#getting-started

app["parname"].real(42)  # current method
app["parname"] = 42   # simpler

In the future, I’d also like to add an app.pars interface which makes it easy to get / set all parameters via a Python dict (useful when writing scripts).

Here’s the first step ... accept integers for real parameters:
https://github.com/gammalib/gammalib/compare/devel...cdeil:gapplication-pars
I think this is the only change needed in Gammalib for now?
Jürgen, does it look OK?

Now I’ll start using this simpler way to set app parameters in all the test / examples / docs in ctools:
https://github.com/ctools/ctools/search?utf8=%E2%9C%93&q=.real+in%3Afile+language%3Apython&type=Code


Recurrence

No recurrence.

History

#1 Updated by Deil Christoph almost 9 years ago

  • Status changed from In Progress to Pull request

Here’s the corresponding change in the ctools repo:
https://github.com/ctools/ctools/compare/devel...cdeil:gapplication-pars

#2 Updated by Deil Christoph almost 9 years ago

There’s a test fail for ctools:
https://gist.github.com/cdeil/e6fb9ed0209487d48af2#file-gistfile1-txt-L36

Even if I add this:
https://github.com/cdeil/gammalib/commit/986f976a38e07b5f70f718daa4abd97c3d0b922d
I think still the setitem(string, int) is called if a bool is passed in?

Jürgen, can you have a look, please?

#3 Updated by Knödlseder Jürgen almost 9 years ago

I have done some additional modifications to the Python interface, please see the GApplication.i file. I think this takes now care of all the special issues, and also deals with Boolean parameters (this is however not 100% clean as a Boolean seems to be presented by an integer value; hence you can set for example sim["ra"] = True - I would be happy if I could catch this).

#4 Updated by Deil Christoph almost 9 years ago

Agreed it’s bad to accept sim[“ra”] = True.

I’ve never used this, but probably the solution is to do input checking in Python using e.g. pythonprepend?
http://www.swig.org/Doc3.0/SWIGDocumentation.html#Python_nn42

#5 Updated by Knödlseder Jürgen almost 9 years ago

  • Status changed from Pull request to In Progress
  • % Done changed from 0 to 90

Merged now into devel (all tests have been passed).

I put the issue back to In Progress to see whether the boolean case can be fixed.

Could you look into that?

#6 Updated by Knödlseder Jürgen over 8 years ago

I note that swig changed the bool behavior, but this was switched of by specifying the -DSWIG_PYTHON_LEGACY_BOOL flag. I could not find any trace of why I used this option.

#7 Updated by Knödlseder Jürgen over 8 years ago

Looks like I introduced the -DSWIG_PYTHON_LEGACY_BOOL because this resulted in some test errors, but by changing the test scripts this could be fixed:

test_GApplication.py:
        #ref.append("1\n")
        ref.append("true\n") # New swig version

test_GFits.py:
            #col1[i] = i % 2
            #col2[i] = i % 2
            col1[i] = bool(i % 2)  # New swig version
            col2[i] = bool(i % 2)  # New swig version

#8 Updated by Knödlseder Jürgen over 8 years ago

I now added a __setitem__(const std::string& name, const bool& val) method to GApplication and disallowed setting of a boolean parameter with an integer value. This results in the expected behavior:

>>> import ctools
>>> sim=ctools.ctobssim()
>>> sim["ra"]=True
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/gamma/lib/python2.6/site-packages/gammalib/app.py", line 280, in __setitem__
    return _app.GApplication___setitem__(self, *args)
RuntimeError: *** ERROR in __setitem__(std::string, int): Invalid argument. Attempt to set "r" parameter "ra" with boolean value "1".
>>> sim["clobber"]=False
>>> sim["clobber"]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/gamma/lib/python2.6/site-packages/gammalib/app.py", line 280, in __setitem__
    return _app.GApplication___setitem__(self, *args)
RuntimeError: *** ERROR in __setitem__(std::string, int): Invalid argument. Attempt to set "b" parameter "clobber" with integer value "1".
>>> sim["clobber"]=0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/gamma/lib/python2.6/site-packages/gammalib/app.py", line 280, in __setitem__
    return _app.GApplication___setitem__(self, *args)
RuntimeError: *** ERROR in __setitem__(std::string, int): Invalid argument. Attempt to set "b" parameter "clobber" with integer value "0".

#9 Updated by Knödlseder Jürgen over 8 years ago

  • Project changed from ctools to GammaLib
  • Target version deleted (1.0.0)

#10 Updated by Knödlseder Jürgen over 8 years ago

  • Target version set to 1.0.0

#11 Updated by Knödlseder Jürgen over 8 years ago

  • Status changed from In Progress to Closed
  • % Done changed from 90 to 100

Merged into devel.

Also available in: Atom PDF