Bug #1418

ctools module seems to corrupt gammalib module

Added by Knödlseder Jürgen almost 10 years ago. Updated over 9 years ago.

Status:ClosedStart date:02/05/2015
Priority:LowDue date:
Assigned To:Bregeon Johan% Done:

100%

Category:-
Target version:1.0.0
Duration:

Description

I just encountered this strange problem:

$ python
>>> import ctools
>>> import gammalib
>>> obs = gammalib.GObservations()
>>> cta = gammalib.GCTAObservation()
>>> obs.append(cta)
<Swig Object of type 'GCTAObservation *' at 0x10042cf90>
>>> print obs[0]
<Swig Object of type 'GCTAObservation *' at 0x102174a50>

$ python
>>> import gammalib
>>> obs = gammalib.GObservations()
>>> cta = gammalib.GCTAObservation()
>>> obs.append(cta)
<gammalib.cta.GCTAObservation; proxy of <Swig Object of type 'GCTAObservation *' at 0x1020d5690> >
>>> print obs[0]
=== GCTAObservation ===
 Name ......................: 
 Identifier ................: 
 Instrument ................: CTA
 Event file ................: 
 Event type ................: 
 Statistics ................: Poisson
 Ontime ....................: 0 s
 Livetime ..................: 0 s
 Deadtime correction .......: 0
 User energy range .........: undefined
=== GCTAPointing ===
 Pointing direction ........: (RA,Dec)=(not initialised)
 Response function .........: undefined

Depending whether ctools is imported or not, gammalib behaves differently.


Recurrence

No recurrence.

History

#1 Updated by Knödlseder Jürgen almost 10 years ago

  • Status changed from New to In Progress

Interesting, the order of the import has an impact.

The following works:

$ python
>>> import gammalib
>>> import ctools
>>> obs = gammalib.GObservations()
>>> cta = gammalib.GCTAObservation()
>>> obs.append(cta)
>>> print(obs[0])
=== GCTAObservation ===
 Name ......................: 
 Identifier ................: 
 Instrument ................: CTA
 Event file ................: 
 Event type ................: 
 Statistics ................: Poisson
 Ontime ....................: 0 s
 Livetime ..................: 0 s
 Deadtime correction .......: 0
 User energy range .........: undefined
=== GCTAPointing ===
 Pointing direction ........: (RA,Dec)=(not initialised)
 Response function .........: undefined

#2 Updated by Knödlseder Jürgen almost 10 years ago

  • Priority changed from Urgent to Low
  • % Done changed from 0 to 10

There is maybe no way around this. Just need to import gammalib before ctools.

Maybe a Python / SWIG geek can fix this.

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

  • Assigned To changed from Knödlseder Jürgen to Bregeon Johan

#4 Updated by Bregeon Johan over 9 years ago

strange thing ideed...

in the correct case obs[0] is a “proxy”:
<gammalib.cta.GCTAObservation; proxy of <Swig Object of type 'GCTAObservation *' at 0x1020d5690> >

in the bad case, obs[0] is just a pointer:
<Swig Object of type 'GCTAObservation *' at 0x10042cf90>

Still more funny is that cta is always a “proxy”, so strange things happen when cta is appended to obs.

#5 Updated by Bregeon Johan over 9 years ago

when importing ctools, some gammalib modules are imported, but do not seem to be regularly accessible:

>>> import ctools
>>> import sys
>>> print sys.modules.keys()
['gammalib.sky', 'encodings.codecs', 'sysconfig', 'os.path', 'encodings.encodings', '_mwl', 'ctools.os', 'imp', '_app', 'zipimport', 'encodings.utf_8', 'ctools.imp', '_com', '_support', '_cta', 'signal', 'gammalib.support', 'gammalib.gammalib', 'stat', 'ctools.tools', 'encodings', 'abc', 're', '_fits', 'mpl_toolkits', 'gammalib.linalg', 'UserDict', 'swig_runtime_data4', 'codecs', 'ctools.gammalib', 'paste', 'gammalib.fits', 'gammalib', '_locale', '_lat', 'traceback', 'gammalib.model', 'os', 'gammalib.imp', '_sre', '_test', 'gammalib.opt', '__builtin__', '_sky', 'gammalib.numerics', 'posixpath', 'errno', '_obs', '_vo', 'sre_constants', 'gammalib.mwl', '_warnings', 'gammalib.com', 'encodings.__builtin__', '_codecs', '_numerics', 'gammalib.sys', '_base', '_sysconfigdata', 'gammalib.obs', 'gammalib.vo', 'posix', 'encodings.aliases', 'exceptions', 'sre_parse', 'abrt_exception_handler', 'gammalib.test', 'copy_reg', 'sre_compile', 'gammalib.lat', '_tools', 'ctools.sys', 'site', '__main__', 'ctools', 'linecache', 'gammalib.app', 'gammalib.base', 'gammalib.xspec', '_xml', '_abcoll', 'gammalib.cta', 'genericpath', '_opt', 'warnings', '_linalg', 'sys', 'readline', 'types', '_model', '_weakref', 'gammalib.os', '_weakrefset', 'gammalib.xml', '_xspec']
>>> dir(gammalib)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'gammalib' is not defined
>>> dir(gammalib.gammlib)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'gammalib' is not defined

Overall, I think the root problem is that the user can import the ctools without importing gammalib, ending up with a system in a fishy state.
The simple solution (without trying to fully understand the strange behaviour) is to import gammalib at the top of the ctools __init__.py.

> head -5 $CTOOLS/lib64/python2.7/site-packages/ctools/__init__.py
# import gammalib first
import gammalib

# Import sub modules
from .tools import *

As an additional note, for a reason that I do not understand, with just import ctools, the gammalib module does not become
visible to the user so that the user would not even know that gammalib has been imported before ctools,
and would have to import gammalib anyway to have the module fully available in its script/session.

#6 Updated by Deil Christoph over 9 years ago

+1 to do this change now ... if there’s new or remaining issues we’ll notice it throughout the week.

This warning in the user manual should be removed:
https://github.com/ctools/ctools/blame/d58354de28fa603dbb1599f3f5f0e382763548b2/doc/source/user_manual/getting_started/python.rst#L21

Jürgen, do you want to do this change directly or should I make a commit?

#7 Updated by Bregeon Johan over 9 years ago

for the fun of it, I have created py firt PR...
https://github.com/ctools/ctools/pull/1

woohoo :)

ok... too much excitement !

https://github.com/bregeon/ctools/tree/fiximport

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

  • Status changed from In Progress to Feedback
  • % Done changed from 10 to 100

The problem is now fixed:

>>> import ctools
>>> import gammalib
>>> obs = gammalib.GObservations()
>>> cta = gammalib.GCTAObservation()
>>> obs.append(cta)
<gammalib.cta.GCTAObservation; proxy of <Swig Object of type 'GCTAObservation *' at 0x10226b630> >
>>> print obs[0]
=== GCTAObservation ===
 Name ......................: 
 Identifier ................: 
 Instrument ................: CTA
 Event file ................: 
 Event type ................: 
 Statistics ................: Poisson
 Ontime ....................: 0 s
 Livetime ..................: 0 s
 Deadtime correction .......: 1
 User energy range .........: undefined
=== GCTAPointing ===
 Pointing direction ........: (RA,Dec)=(not initialised)
 Response function .........: undefined

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

  • Status changed from Feedback to Closed

Merged now into devel.

Also available in: Atom PDF