Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev-codacy #48

Merged
merged 1 commit into from
Jan 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions tests/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ def test_norm_dist():
src2.dec = 1/3600.
src2.a = 1
src2.b = 1
assert cluster.norm_dist(src1, src1) == 0
assert cluster.norm_dist(src1, src2) == 1/math.sqrt(2)
if not cluster.norm_dist(src1, src1) == 0:
raise AssertionError()
if not cluster.norm_dist(src1, src2) == 1/math.sqrt(2):
raise AssertionError()


def test_sky_dist():
Expand All @@ -38,8 +40,10 @@ def test_sky_dist():
src2 = SimpleSource()
src2.ra = 0
src2.dec = 1/3600.
assert cluster.sky_dist(src1, src1) == 0.
assert cluster.sky_dist(src1, src2) == 1/3600.
if not cluster.sky_dist(src1, src1) == 0.:
raise AssertionError()
if not cluster.sky_dist(src1, src2) == 1/3600.:
raise AssertionError()


def test_pairwise_elliptical_binary():
Expand All @@ -53,7 +57,8 @@ def test_pairwise_elliptical_binary():
src3 = deepcopy(src1)
src3.dec = 50
mat = cluster.pairwise_ellpitical_binary([src1, src2, src3], eps=0.5)
assert np.all(mat == [[False, True, False], [True, False, False], [False, False, False]])
if not np.all(mat == [[False, True, False], [True, False, False], [False, False, False]]):
raise AssertionError()


def test_regroup():
Expand All @@ -65,10 +70,14 @@ def test_regroup():

# this should result in 51 groups
a = cluster.regroup('tests/test_files/1904_comp.fits', eps=1/3600.)
assert len(a) == 51
if not len(a) == 51:
raise AssertionError()

# this should give 1 group
a = cluster.regroup('tests/test_files/1904_comp.fits', eps=10, far=1000)
assert len(a) == 1
if not len(a) == 1:
raise AssertionError()


if __name__ == "__main__":
# introspect and run all the functions starting with 'test'
Expand Down
36 changes: 18 additions & 18 deletions tests/test_fits_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,46 @@ def test_get_pixinfo():
header = fits.getheader('tests/test_files/1904-66_SIN.fits')

area, scale = fi.get_pixinfo(header)
assert area > 0
assert len(scale) == 2
if not area > 0: raise AssertionError()
if not len(scale) == 2: raise AssertionError()

header['CD1_1'] = header['CDELT1']
del header['CDELT1']
header['CD2_2'] = header['CDELT2']
del header['CDELT2']
area, scale = fi.get_pixinfo(header)
assert area > 0
assert len(scale) == 2
if not area > 0: raise AssertionError()
if not len(scale) == 2: raise AssertionError()

header['CD1_2'] = 0
header['CD2_1'] = 0
area, scale = fi.get_pixinfo(header)
assert area > 0
assert len(scale) == 2
if not area > 0: raise AssertionError()
if not len(scale) == 2: raise AssertionError()

header['CD1_2'] = header['CD1_1']
header['CD2_1'] = header['CD2_2']
area, scale = fi.get_pixinfo(header)
assert area == 0
assert len(scale) == 2
if not area == 0: raise AssertionError()
if not len(scale) == 2: raise AssertionError()

for f in ['CD1_1', 'CD1_2', 'CD2_2', 'CD2_1']:
del header[f]
area, scale = fi.get_pixinfo(header)
assert area == 0
assert scale == (0, 0)
if not area == 0: raise AssertionError()
if not scale == (0, 0): raise AssertionError()


def test_get_beam():
header = fits.getheader('tests/test_files/1904-66_SIN.fits')
beam = fi.get_beam(header)
print(beam)
assert beam is not None
assert beam.pa == header['BPA']
if beam is None : raise AssertionError()
if beam.pa != header['BPA']: raise AssertionError()

del header['BMAJ'], header['BMIN'], header['BPA']
beam = fi.get_beam(header)
assert beam is None
if beam is not None : raise AssertionError()


def test_fix_aips_header():
Expand Down Expand Up @@ -88,13 +88,13 @@ def test_init():
hdu[0].header['BZERO'] = 1
hdu[0].header['BSCALE'] = 2
im = fi.FitsImage(hdu)
assert im.bscale == 2
assert im.bzero == 1
if not im.bscale == 2: raise AssertionError()
if not im.bzero == 1: raise AssertionError()

# should be able to supply a beam directly
beam = fi.Beam(1, 1, 0)
im = fi.FitsImage(hdu, beam=beam, slice=0)
assert im.beam is beam
if not (im.beam is beam): raise AssertionError()

# raise exception if the beam cannot be determined
del hdu[0].header['BMAJ']
Expand All @@ -107,7 +107,7 @@ def test_init():
assert_raises(Exception, fi.FitsImage, hdu)
# this should be fine
im = fi.FitsImage(hdu, slice=0)
assert im.x == im.y == 3
if not (im.x == im.y == 3): raise AssertionError()

# can't work with 4d data
hdu[0].data = np.empty((3, 3, 3, 3))
Expand All @@ -119,7 +119,7 @@ def test_get_background_rms():
hdu = fits.open(filename)
hdu[0].data = np.empty((40, 40))
im = fi.FitsImage(hdu)
assert im.get_background_rms() > 0
if not (im.get_background_rms() > 0): raise AssertionError()


def test_pix2sky_sky2pix():
Expand Down
24 changes: 12 additions & 12 deletions tests/test_fits_interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,57 @@
def test_load_file_or_hdu():
fname = 'tests/test_files/1904-66_AIT.fits'
hdulist = fits.open(fname)
assert fits_interp.load_file_or_hdu(hdulist) is hdulist
if not (fits_interp.load_file_or_hdu(hdulist) is hdulist): raise AssertionError()


def test_compress():
# return None when the factor is not a positive integer
assert fits_interp.compress(None, factor=-1) is None
assert fits_interp.compress(None, factor=0.3) is None
assert fits_interp.compress(None, factor=0) is None
if not (fits_interp.compress(None, factor=-1) is None): raise AssertionError()
if not (fits_interp.compress(None, factor=0.3) is None): raise AssertionError()
if not (fits_interp.compress(None, factor=0) is None): raise AssertionError()
# test with factor = 10 for speed
fname = 'tests/test_files/1904-66_AIT.fits'
hdulist = fits.open(fname)
cd1 = hdulist[0].header['CDELT1']
cd2 = hdulist[0].header['CDELT2']
# compress using CDELT1 and CDELT2
assert isinstance(fits_interp.compress(hdulist, factor=10), fits.HDUList)
if not (isinstance(fits_interp.compress(hdulist, factor=10), fits.HDUList)): raise AssertionError()
hdulist[0].header['CD1_1'] = cd1
del hdulist[0].header['CDELT1']
hdulist[0].header['CD2_2'] = cd2
del hdulist[0].header['CDELT2']
# compress using CD1_1 and CDELT2_2 instead
assert isinstance(fits_interp.compress(hdulist, factor=10), fits.HDUList)
if not (isinstance(fits_interp.compress(hdulist, factor=10), fits.HDUList)): raise AssertionError()
# now strip CD2_2 and we should get error
del hdulist[0].header['CD2_2']
assert fits_interp.compress(hdulist, factor=10) is None
if not (fits_interp.compress(hdulist, factor=10) is None): raise AssertionError()
# same for CD1_1
del hdulist[0].header['CD1_1']
assert fits_interp.compress(hdulist, factor=10) is None
if not (fits_interp.compress(hdulist, factor=10) is None): raise AssertionError()


def test_expand():
fname = 'tests/test_files/1904-66_AIT.fits'
hdulist = fits.open(fname)
compressed = fits_interp.compress(hdulist, factor=10)
# the uncompressed hdu list is missing header keys so test that this gives the expected result
assert fits_interp.expand(compressed) is hdulist
if not (fits_interp.expand(compressed) is hdulist): raise AssertionError()

# now mix up the CDELT and CD keys
compressed = fits_interp.compress(fname, factor=10) # reload because we pass references
compressed[0].header['CD1_1'] = compressed[0].header['CDELT1']
del compressed[0].header['CDELT1']
compressed[0].header['CD2_2'] = compressed[0].header['CDELT2']
del compressed[0].header['CDELT2']
assert isinstance(fits_interp.expand(compressed), fits.HDUList)
if not (isinstance(fits_interp.expand(compressed), fits.HDUList)): raise AssertionError()

# now strip CD2_2 and we should return None
compressed = fits_interp.compress(fname, factor=10)
del compressed[0].header['CDELT2']
assert fits_interp.expand(compressed) is None
if not (fits_interp.expand(compressed) is None): raise AssertionError()
# same for CD1_1
del compressed[0].header['CDELT1']
assert fits_interp.expand(compressed) is None
if not (fits_interp.expand(compressed) is None): raise AssertionError()


def test_fits_interp_compress_then_expand():
Expand Down
34 changes: 17 additions & 17 deletions tests/test_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ def make_model():
def test_elliptical_gaussian():
x, y = np.indices((3, 3))
gauss = fitting.elliptical_gaussian(x, y, amp=1, xo=0, yo=1, sx=1, sy=1, theta=0)
assert not np.any(np.isnan(gauss))
if np.any(np.isnan(gauss)): raise AssertionError()
gauss = fitting.elliptical_gaussian(x, y, amp=1, xo=0, yo=1, sx=1, sy=1, theta=np.inf)
assert np.all(np.isnan(gauss))
if not (np.all(np.isnan(gauss))): raise AssertionError()


def test_CandBmatrix():
x, y = map(np.ravel, np.indices((3, 3)))
C = fitting.Cmatrix(x, y, sx=1, sy=2, theta=0)
assert not np.any(np.isnan(C))
if np.any(np.isnan(C)): raise AssertionError()
B = fitting.Bmatrix(C)
assert not np.any(np.isnan(C))
if np.any(np.isnan(C)): raise AssertionError()


def test_hessian_shape():
Expand All @@ -50,7 +50,7 @@ def test_hessian_shape():
nvar = 3
x, y = np.indices((10, 10))
Hij = fitting.hessian(model, x, y)
assert Hij.shape == (nvar, nvar, 10, 10)
if not (Hij.shape == (nvar, nvar, 10, 10)): raise AssertionError()

# now add another component
model.add('c1_amp', 1, vary=True)
Expand All @@ -62,7 +62,7 @@ def test_hessian_shape():
nvar = 9
model['components'].value = 2
Hij = fitting.hessian(model, x, y)
assert Hij.shape == (nvar, nvar, 10, 10)
if not (Hij.shape == (nvar, nvar, 10, 10)): raise AssertionError()


def test_jacobian_shape():
Expand All @@ -82,7 +82,7 @@ def test_jacobian_shape():
nvar = 3
x, y = np.indices((10, 10))
Jij = fitting.jacobian(model, x, y)
assert Jij.shape == (nvar, 10, 10)
if not (Jij.shape == (nvar, 10, 10)): raise AssertionError()

model.add('c1_amp', 1, vary=True)
model.add('c1_xo', 5, vary=True)
Expand All @@ -93,7 +93,7 @@ def test_jacobian_shape():
nvar = 9
model['components'].value = 2
Jij = fitting.jacobian(model, x, y)
assert Jij.shape == (nvar, 10, 10)
if not (Jij.shape == (nvar, 10, 10)): raise AssertionError()


def test_emp_vs_ana_jacobian():
Expand All @@ -110,7 +110,7 @@ def test_emp_vs_ana_jacobian():
emp_Jij = fitting.emp_jacobian(model, x, y)
ana_Jij = fitting.jacobian(model, x, y)
diff = np.abs(ana_Jij - emp_Jij)
assert np.max(diff) < 1e-5
if not (np.max(diff) < 1e-5): raise AssertionError()

model.add('c1_amp', 1, vary=True)
model.add('c1_xo', 5, vary=True)
Expand All @@ -123,7 +123,7 @@ def test_emp_vs_ana_jacobian():
emp_Jij = fitting.emp_jacobian(model, x, y)
ana_Jij = fitting.jacobian(model, x, y)
diff = np.abs(ana_Jij - emp_Jij)
assert np.max(diff) < 1e-3
if not (np.max(diff) < 1e-3): raise AssertionError()


def test_emp_vs_ana_hessian():
Expand All @@ -140,7 +140,7 @@ def test_emp_vs_ana_hessian():
emp_Hij = fitting.emp_hessian(model, x, y)
ana_Hij = fitting.hessian(model, x, y)
diff = np.abs(ana_Hij - emp_Hij)
assert np.max(diff) < 1e-5
if not (np.max(diff) < 1e-5): raise AssertionError()

model.add('c1_amp', 1, vary=True)
model.add('c1_xo', 5, vary=True)
Expand All @@ -153,23 +153,23 @@ def test_emp_vs_ana_hessian():
emp_Hij = fitting.emp_hessian(model, x, y)
ana_Hij = fitting.hessian(model, x, y)
diff = np.abs(ana_Hij - emp_Hij)
assert np.max(diff) < 1
if not (np.max(diff) < 1): raise AssertionError()


def test_make_ita():
noise = np.random.random((10, 10))
ita = fitting.make_ita(noise)
assert ita.shape == (100, 100)
if not (ita.shape == (100, 100)): raise AssertionError()
noise *= np.nan
ita = fitting.make_ita(noise)
assert len(ita) == 0
if not (len(ita) == 0): raise AssertionError()


def test_RB_bias():
data = np.random.random((4, 4))
model = make_model()
bias = fitting.RB_bias(data, model)
assert len(bias) == 3
if not (len(bias) == 3): raise AssertionError()


def test_bias_correct():
Expand Down Expand Up @@ -199,9 +199,9 @@ def test_condon_errs():
source.peak_flux = 1
source.int_flux = 1
fitting.condon_errors(source, None)
assert source.err_a == 0
if not (source.err_a == 0): raise AssertionError()
fitting.condon_errors(source, theta_n=8.)
assert source.err_a > 0
if not (source.err_a > 0): raise AssertionError()


if __name__ == "__main__":
Expand Down
Loading