5

In previous astropy versions it was possible to handle propagation of uncertainties along the following lines:

from astropy.nddata import NDData, StdDevUncertainty

x = NDData( 16.0, uncertainty=StdDevUncertainty( 4.0 ))
y = NDData( 361.0, uncertainty=StdDevUncertainty( 19.0 ))

print x.add(y)

Changes to NDData seem to have removed this capability. I get "AttributeError: 'NDData' object has no attribute 'add'", and I can't find any useful advice in the documentation. How is error propagation handled now?

TheBigH
  • 524
  • 3
  • 15

1 Answers1

3

It looks like this functionality has been moved to a mixin, NDArithmeticMixin.

The example in the Arithmetic mixin documentation suggests to create one's own class and use that.

So your example would become:

from astropy.nddata import NDData, StdDevUncertainty, NDArithmeticMixin
class MyData(NDData, NDArithmeticMixin):
    pass
x = MyData( 16.0, uncertainty=StdDevUncertainty( 4.0 ))
y = MyData( 361.0, uncertainty=StdDevUncertainty( 19.0 ))
z = x.add(y)
print(z)
print(z.uncertainty.array)

which gives:

MyData(377.0)
19.416487838947599

Update

The class NDDataArray actually does what the above class MyData does: it includes the three mixins (arithmetic, io & slicing).
That makes the above a bit simpler:

from astropy.nddata import StdDevUncertainty, NDDataArray
x = NDDataArray(16, uncertainty=StdDevUncertainty(4.0))
y = NDDataArray(361, uncertainty=StdDevUncertainty(19.0))
z = x.add(y)
print(z)
print(z.uncertainty.array)

I think this interface is rather clunky. Perhaps it clears up over time, to become as simple as

z = x + y
print(z)

377.0 +/- 19.416487838947599
  • Perfect! Thank you. It even seems to handle units, which is more than I hoped for – TheBigH Dec 16 '15 at 20:18
  • 1
    @TheBigH I found that `NDDataArray` does what you want, and avoids the need for creating your own class. See also the update in my answer. –  Dec 20 '15 at 10:11
  • 1
    This looks far more clumsy than with uncertainties. What advantages does this have over uncertainties? – MaxNoe Dec 20 '15 at 14:34
  • @MaxNoe All the details are in [the nddata documentation](http://astropy.readthedocs.org/en/stable/nddata/index.html), but very briefly: NDData allows for storing a mask, metadata, uncertainties, units and so on. So it can do a lot more than just propagate uncertainties. –  Dec 21 '15 at 01:52
  • @Evert: One thing that you [lose is the correct handling of correlations](http://astropy.readthedocs.org/en/stable/nddata/mixins/ndarithmetic.html#overview). :) Also, the [Pint](https://pint.readthedocs.org/en/0.7.2/) package handles both uncertainties and units, and if I remember correctly it is also compatible with NumPy, so you also get masks. It thus looks to me that with Pint, you only lose metadata, and gain correct calculations (correlations). Since performing correct calculations is harder, it is also probably slower, though. :) – Eric O. Lebigot Mar 13 '16 at 12:29