Skip to content

Fix quantity errors from astro source models - #200

Merged
cdeil merged 1 commit into
masterfrom
astro_quantity_fix
Sep 20, 2014
Merged

cdeil merged 1 commit into
masterfrom
astro_quantity_fix

Conversation

@cdeil

@cdeil cdeil commented Sep 20, 2014

Copy link
Copy Markdown
Contributor

@adonath After updating to Numpy 1.9, I'm getting unit test fails on my Mac from the SNR and PWN code and tests you wrote.

E.g. this code:

from astropy.units import Quantity
from gammapy.astro.source import SNR
t = Quantity([0, 1, 10, 100, 1000, 10000], 'yr')
snr = SNR()
snr.radius(t)

fails like this:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/private/tmp/gammapy/a.py in <module>()
      4 t = Quantity([0, 1, 10, 100, 1000, 10000], 'yr')
      5 snr = SNR()
----> 6 snr.radius(t)
      7 

/private/tmp/gammapy/gammapy/astro/source/snr.py in radius(self, t)
     85                      self._radius_free_expansion(t).to('cm'))
     86         import IPython; IPython.embed()
---> 87         return Quantity(r, 'cm')
     88 
     89     def _radius_free_expansion(self, t):

/Users/deil/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9928-py3.4-macosx-10.9-x86_64.egg/astropy/units/quantity.py in __new__(cls, value, unit, dtype, copy, order, subok, ndmin)
    176         if isinstance(value, Quantity):
    177             if unit is not None and unit is not value.unit:
--> 178                 value = value.to(unit)
    179                 # the above already makes a copy (with float dtype)
    180                 copy = False

/Users/deil/Library/Python/3.4/lib/python/site-packages/astropy-1.0.dev9928-py3.4-macosx-10.9-x86_64.egg/astropy/units/quantity.py in to(self, unit, equivalencies)
    577         unit = Unit(unit)
    578         new_val = np.asarray(
--> 579             self.unit.to(unit, self.value, equivalencies=equivalencies))
    580         return self._new_view(new_val, unit)
    581 

AttributeError: 'NoneType' object has no attribute 'to'

I've traced it down to the np.where calls ... they return an un-initialised Quantity as mentioned here (I think this is a new issue that appeared with the update to numpy 1.9 !?):

>>> np.where([True, False], Quantity([1,2], 'cm'), Quantity([10, 20], 'cm'))
<Quantity [  1., 20.] (Unit not initialised)>

Fixing this now ... something to watch out in the future.

@cdeil cdeil self-assigned this Sep 20, 2014
@cdeil cdeil added this to the 0.2 milestone Sep 20, 2014
cdeil added a commit that referenced this pull request Sep 20, 2014
Quantity errors from gammapy.astro.source models
@cdeil
cdeil merged commit 9aa0a3b into master Sep 20, 2014
@cdeil cdeil changed the title Quantity errors from gammapy.astro.source models Fix quantity errors from astro source models Apr 8, 2015
@cdeil
cdeil deleted the astro_quantity_fix branch April 13, 2015 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant