Skip to content

Commit

Permalink
add mad_std test
Browse files Browse the repository at this point in the history
  • Loading branch information
keflavich committed May 22, 2017
1 parent fd43fe8 commit 05d537b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,12 @@ def mad_std(self, axis=None):
"""
Use astropy's mad_std to computer the standard deviation
"""
if int(astropy.__version__[0]) < 2:
raise NotImplementedError("mad_std requires astropy >= 2")
projection = self._naxes_dropped(axis) in (1,2)
return self.apply_numpy_function(stats.mad_std, fill=np.nan,
how='cube', axis=axis, unit=self.unit,
ignore_nan=True,
projection=projection)


Expand Down
24 changes: 24 additions & 0 deletions spectral_cube/tests/test_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pytest

import astropy
from astropy.io import fits
from astropy import units as u
from astropy.wcs import WCS
Expand Down Expand Up @@ -1297,3 +1298,26 @@ def test_mask_bad_beams():

mean2 = masked_cube2.mean(axis=0)
assert np.all(mean2 == (cube[2,:,:]+cube[1,:,:])/2)

def test_mad_std():
cube, data = cube_and_raw('adv.fits')

if int(astropy.__version__[0]) < 2:
with pytest.raises(NotImplementedError) as exc:
cube.mad_std()

else:
# mad_std run manually on data
result = np.array([[0.15509701, 0.45763670],
[0.55907956, 0.42932451],
[0.48819454, 0.25499305]])

np.testing.assert_almost_equal(cube.mad_std(axis=0).value, result)

mcube = cube.with_mask(cube < 0.98*u.K)

result2 = np.array([[0.15509701, 0.45763670],
[0.55907956, 0.23835865],
[0.48819454, 0.25499305]])

np.testing.assert_almost_equal(mcube.mad_std(axis=0).value, result2)

0 comments on commit 05d537b

Please sign in to comment.