def test_daofind_strong_and_weak(self): size = (128, 128) posx = (45, 90) posy = (45, 90) sky = 800 rdnoise = 20 flux = (32000, 16000) fwhm = 3 sigma = fwhm * gaussian_fwhm_to_sigma theta = 0 im = gen_image(size, posx, posy, flux, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) threshold = 10 sources = daofind(im, threshold, sky, rdnoise, fwhm) assert_equal(len(sources), 2) assert_almost_equal(sources['x'], posx, decimal=0) assert_almost_equal(sources['y'], posy, decimal=0)
def test_daofind_four_stars_fixed_position(self): size = (128, 128) posx = (45, 90, 45, 90) posy = (45, 50, 90, 100) sky = 800 rdnoise = 20 flux = (15000, 3000, 5000, 35000) fwhm = 3 sigma = fwhm * gaussian_fwhm_to_sigma theta = 0 threshold = 10 im = gen_image(size, posx, posy, flux, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = daofind(im, threshold, sky, rdnoise, fwhm) assert_equal(len(sources), 4) assert_almost_equal(sources['x'], posx, decimal=0) assert_almost_equal(sources['y'], posy, decimal=0)
def std_test_frame(op, frame1, frame2, result, inplace, handle_mask): """Standart comparison tests with frames.""" if handle_mask: # Use two different masks to check if they are compined mask1 = np.zeros(SHAPE, dtype=bool) mask1[2, 2] = 1 frame1.mask = mask1 exp_mask = np.zeros(SHAPE, dtype=bool) exp_mask[2, 2] = 1 if isinstance(frame2, FrameData): # If frame2 is qfloat, quantity or a number, it don't have mask mask2 = np.zeros(SHAPE, dtype=bool) mask2[3, 3] = 1 exp_mask[3, 3] = 1 frame2.mask = mask2 result.mask = exp_mask res = imarith(frame1, frame2, op, inplace=inplace, join_masks=handle_mask) assert_equal(res.data, result.data) assert_almost_equal(res.get_uncertainty(False), result.get_uncertainty(False)) assert_equal(res.unit, result.unit) if handle_mask: assert_equal(res.mask, result.mask) if inplace: assert_is(res, frame1) else: assert_is_not(res, frame1)
def test_sepfind_multiple_stars(self): size = (1024, 1024) number = 15 low = 2000 high = 30000 sky = 800 rdnoise = 20 sigma = 3 theta = 0 x, y, f = gen_position_flux(size, number, low, high, rng_seed=456) im = gen_image(size, x, y, f, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = sepfind(im, 5, sky, rdnoise) x, y, f = self.resort_sources(x, y, f) assert_equal(len(sources), number) assert_almost_equal(sources['x'], x, decimal=0) assert_almost_equal(sources['y'], y, decimal=0)
def test_daofind_multiple_stars(self): size = (512, 512) number = 15 low = 2000 high = 30000 sky = 800 rdnoise = 20 fwhm = 5 sigma = fwhm * gaussian_fwhm_to_sigma theta = 0 threshold = 10 x, y, f = gen_position_flux(size, number, low, high, rng_seed=456) im = gen_image(size, x, y, f, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = daofind(im, threshold, sky, rdnoise, fwhm) x, y, f = self.resort_sources(x, y, f) assert_equal(len(sources), number) assert_almost_equal(sources['x'], x, decimal=0) assert_almost_equal(sources['y'], y, decimal=0)
def test_starfind_recenter_sources(self): size = (256, 256) number = 10 sky = 70 rdnoise = 20 low = 120000 high = 320000 sigma = 5 theta = 0 x, y, f = gen_position_flux(size, number, low, high, rng_seed=456) im = gen_image(size, x, y, f, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) nx, ny = recenter_sources( im, x + np.random.normal(loc=0, scale=1, size=len(x)), y + np.random.normal(loc=0, scale=1, size=len(x)), box_size=15, model='gaussian') assert_equal(len(nx), number) assert_equal(len(ny), number) # TODO: this seems unprecise. Investigate it. assert_almost_equal(nx, x, decimal=-1) assert_almost_equal(ny, y, decimal=-1)
def test_simbad_catalog_query_object(self): obj = self.cat.query_object('Sirius', band='V') assert_equal(obj[0]['MAIN_ID'], '* alf CMa') assert_equal(obj[0]['RA'], '06 45 08.9172') assert_equal(obj[0]['DEC'], '-16 42 58.017') assert_equal(obj[0]['COO_BIBCODE'], '2007A&A...474..653V') assert_almost_equal(obj[0]['FLUX_V'], -1.46) assert_equal(obj[0]['FLUX_SYSTEM_V'], 'Vega') assert_equal(obj[0]['FLUX_BIBCODE_V'], '2002yCat.2237....0D')
def test_simple_aperture(r): data = np.ones((10, 10)) res = aperture_photometry(data, [5], [5], r=r, r_ann=None) assert_equal(res['x'], [5]) assert_equal(res['y'], [5]) assert_equal(res['aperture'], [r]) assert_almost_equal(res['flux'], [np.pi * r**2]) assert_not_in('sky', res.colnames) assert_almost_equal(res['flux_error'], [np.sqrt(np.pi * r**2)]) assert_equal(res['flags'], [0])
def test_numpy_err_msg(self): # Ensure compatibility with numpy testing with pytest.raises(AssertionError): assert_almost_equal([1, 2], [3, 4], 0, None) with pytest.raises(AssertionError): assert_equal([1, 2], [3, 4], None) with pytest.raises(AssertionError): assert_almost_equal([1, 2], [3, 4], 0, '') with pytest.raises(AssertionError): assert_equal([1, 2], [3, 4], '')
def test_manual_wcs_angle(): # Checked with DS9 x, y = (11, 11) ra, dec = (10.0, 0.0) ps = 36 # arcsec/px north = 45 wcs = wcs_from_coords(x, y, ra, dec, ps, north) assert_almost_equal(wcs.all_pix2world(11, 11, 1), (ra, dec)) assert_almost_equal(wcs.all_pix2world(21, 21, 1), (10.0, 0.14142)) assert_almost_equal(wcs.all_pix2world(1, 1, 1), (10.0, -0.14142)) assert_almost_equal(wcs.all_pix2world(1, 21, 1), (10.14142, 0.0)) assert_almost_equal(wcs.all_pix2world(21, 1, 1), (9.858579, 0.0))
def test_background_stars(self): size = (1024, 1024) stars_n = 50 flux_low = 1500 flux_high = 25000 fwhm = 5 level = 800 rdnoise = 20 x, y, f = gen_position_flux(size, stars_n, flux_low, flux_high) image_test = gen_bkg(size, level, rdnoise) image_test += gen_stars_moffat(size, x, y, f, fwhm) box_size = 64 filter_size = 3 global_bkg, global_rms = background(image_test, box_size, filter_size, mask=None, global_bkg=True) bkg, rms = background(image_test, box_size, filter_size, mask=None, global_bkg=False) assert_equal(type(global_bkg), float) assert_equal(type(global_rms), float) assert_almost_equal(global_bkg, level, decimal=0) assert_almost_equal(global_rms, rdnoise, decimal=0) assert_equal(bkg.shape, size) assert_equal(rms.shape, size) # with stars, the dispersion increases assert_almost_equal(bkg, np.ones(size) * level, decimal=-1) assert_almost_equal(rms, np.ones(size) * rdnoise, decimal=-1)
def test_background_simple_negative_sky(self): size = (2048, 2048) level = -100 rdnoise = 20 image_test = gen_bkg(size, level, rdnoise) box_size = 64 filter_size = 3 global_bkg, global_rms = background(image_test, box_size, filter_size, mask=None, global_bkg=True) bkg, rms = background(image_test, box_size, filter_size, mask=None, global_bkg=False) assert_equal(type(global_bkg), float) assert_equal(type(global_rms), float) assert_almost_equal(global_bkg, level, decimal=0) assert_almost_equal(global_rms, rdnoise, decimal=0) assert_equal(bkg.shape, size) assert_equal(rms.shape, size) assert_almost_equal(bkg, np.ones(size) * level, decimal=0) assert_almost_equal(rms, np.ones(size) * rdnoise, decimal=0)
def test_daofind_one_star_subpixel(self): size = (128, 128) pos = (54.32, 47.86) im = gen_image(size, [pos[0]], [pos[1]], [45000], 800, 0, sigma=[5 * gaussian_fwhm_to_sigma], skip_poisson=True) sources = daofind(im, 5, 800, 10, 5) assert_equal(len(sources), 1) # no error, 2 decimals ok! assert_almost_equal(sources[0]['x'], pos[0], decimal=2) assert_almost_equal(sources[0]['y'], pos[1], decimal=2)
def test_register_framedata_crosscorr(self, inplace): frame_list = self.gen_frame_list((1024, 1024)) reg_list = register_framedata_list(frame_list, algorithm='cross-correlation', inplace=inplace, upsample_factor=10, space='real') assert_equal(len(frame_list), len(reg_list)) for org, reg in zip(frame_list, reg_list): if inplace: assert_is(org, reg) else: assert_is_not(org, reg) assert_almost_equal(reg.meta['astropop registration_shift'], org.meta['test expect_shift'], decimal=0)
def test_register_framedata_asterism(self, inplace): frame_list = self.gen_frame_list((1024, 1024)) reg_list = register_framedata_list(frame_list, algorithm='asterism-matching', inplace=inplace, max_control_points=30, detection_threshold=5) assert_equal(len(frame_list), len(reg_list)) for org, reg in zip(frame_list, reg_list): if inplace: assert_is(org, reg) else: assert_is_not(org, reg) assert_almost_equal(reg.meta['astropop registration_shift'], org.meta['test expect_shift'], decimal=0)
def test_starfind_blind_fwhm(self): size = (512, 512) number = 12 low, high = (15000, 60000) sky = 800 rdnoise = 20 sigma = 2 theta = 0 threshold = 8 x, y, f = gen_position_flux(size, number, low, high, rng_seed=456) x, y, f = self.resort_sources(x, y, f) im = gen_image(size, x, y, f, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = starfind(im, threshold, sky, rdnoise) assert_equal(len(sources), number) assert_almost_equal(sources['x'], x, decimal=1) assert_almost_equal(sources['y'], y, decimal=1) assert_almost_equal(sources.meta['astropop fwhm'], sigma * 2.355, decimal=2)
def test_starfind_strong_weak(self): size = (100, 200) posx = (50, 150) posy = (40, 60) sky = 800 rdnoise = 20 flux = (64000, 6000) theta = 0 fwhm = 3 sigma = fwhm * gaussian_fwhm_to_sigma threshold = 10 im = gen_image(size, posx, posy, flux, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = starfind(im, threshold, sky, rdnoise, fwhm) assert_equal(len(sources), 2) assert_almost_equal(sources['x'], posx, decimal=1) assert_almost_equal(sources['y'], posy, decimal=1) assert_almost_equal(sources.meta['astropop fwhm'], sigma * 2.355, decimal=1)
def test_starfind_one_star(self): size = (128, 128) x, y = (64, 64) f = 80000 sky = 70 rdnoise = 20 sigma = 5 theta = 0 fwhm = 5 # dummy low value threshold = 10 im = gen_image(size, [x], [y], [f], sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = starfind(im, threshold, sky, rdnoise, fwhm) assert_equal(len(sources), 1) assert_almost_equal(sources['x'], x, decimal=0) assert_almost_equal(sources['y'], y, decimal=0) assert_almost_equal(sources.meta['astropop fwhm'], sigma * 2.355, decimal=1)
def test_compute_transform_rotation(self): size = (1024, 1024) sky = 800 n = 300 rdnoise = 5 x, y, f = gen_position_flux(2 * np.array(size), n, 1e4, 4e6) x -= 500 y -= 500 x1, y1, flux1 = gen_positions_transformed(x, y, f, 0, 0, size) im1 = gen_image(size, x1, y1, flux1, sky, rdnoise, sigma=2) x2, y2, flux2 = gen_positions_transformed(x, y, f, 0, 0, size, rotation=35.2) im2 = gen_image(size, x2, y2, flux2, sky, rdnoise, sigma=2) ar = AsterismRegister() tform = ar._compute_transform(im1, im2) assert_almost_equal(tform.rotation, np.deg2rad(35.2), decimal=3) # the translation is needed due to the form skimage handles rotation assert_almost_equal(tform.translation, [388.7, -201.5], decimal=0) assert_almost_equal(tform.scale, 1, decimal=4)
def test_daofind_reject_roundness(self): size = (128, 128) pos_x = [20, 60, 100, 40, 80] pos_y = [20, 30, 40, 50, 60] sky = 70 rdnoise = 20 flux = [30000] * 5 theta = 0 fwhm = 3 sigma_x = np.array([1, 0.5, 1, 2.0, 0.1 ]) * gaussian_fwhm_to_sigma * fwhm sigma_y = np.array([1, 1.0, 1, 0.5, 0.1 ]) * gaussian_fwhm_to_sigma * fwhm threshold = 10 # stars 0, 2 -> passed # star 4 -> rejected by sharpness # stars 1, 3 -> rejected by roundness im = gen_image(size, pos_x, pos_y, flux, sky, rdnoise, model='gaussian', sigma=(sigma_x, sigma_y), theta=theta) sources = daofind(im, threshold, sky, rdnoise, fwhm, sharp_limit=(0.3, 0.6), round_limit=(-0.5, 0.5)) assert_equal(len(sources), 2) assert_almost_equal(sources['x'], [20, 100], decimal=0) assert_almost_equal(sources['y'], [20, 40], decimal=0)
def test_sepfind_negative_sky(self): size = (128, 128) pos = (64, 64) sky = 0 rdnoise = 20 flux = 32000 sigma = 3 theta = 0 threshold = 10 im = gen_image(size, [pos[0]], [pos[1]], [flux], sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = sepfind(im, threshold, sky, rdnoise) assert_equal(len(sources), 1) assert_almost_equal(sources['x'][0], 64, decimal=0) assert_almost_equal(sources['y'][0], 64, decimal=0)
def test_combine_median_simple(self): arr, base = self.create_framedata_array([0.8, 1.0, 1.2, 1.0, 1.2], size=[1024, 1024], unct=0.1) comb = ImCombiner() # the unclipped median of [0.8, 1.0, 1.2, 1.0, 1.2] is 1.0 # std([0.8, 1.0, 1.2, 1.0, 1.2])/sqrt(5) = 0.06693280212272602 res = comb.combine(arr, method='median') assert_equal(res.data, base) assert_almost_equal(res.uncertainty, 0.06693280212272602 * base) assert_equal(res.meta['astropop imcombine nimages'], 5) assert_equal(res.meta['astropop imcombine method'], 'median') # same for 3 sigma clipping comb.set_sigma_clip(3, 'median', 'mad_std') res = comb.combine(arr, method='median') assert_equal(res.data, base) assert_almost_equal(res.uncertainty, 0.06693280212272602 * base) assert_equal(res.meta['astropop imcombine nimages'], 5) assert_equal(res.meta['astropop imcombine method'], 'median')
def test_imarith_ops_frames(op, vs, inplace, handle_mask): def gen_frame(v): # Gen frames with {'v', 'u'} dict shape = (10, 10) if v['u'] is None: frame = FrameData(np.ones(shape, dtype='f8'), unit='adu') else: frame = FrameData(np.ones(shape, dtype='f8'), unit='adu', uncertainty=v['u']) frame.data[:] = v['v'] return frame frame1 = gen_frame(vs['f1']) frame2 = gen_frame(vs['f2']) exp_res = gen_frame(vs['r']) if handle_mask: mask1 = np.zeros((10, 10)) mask2 = np.zeros((10, 10)) mask1[5, 5] = 1 mask2[3, 3] = 1 exp_mask = np.zeros((10, 10)) exp_mask[5, 5] = 1 exp_mask[3, 3] = 1 frame1.mask = mask1 frame2.mask = mask2 exp_res.mask = exp_mask res = imarith(frame1, frame2, op, inplace=inplace, join_masks=handle_mask) assert_almost_equal(res.data, exp_res.data) assert_almost_equal(res.uncertainty, exp_res.uncertainty) if handle_mask: assert_equal(res.mask, exp_res.mask) if inplace: assert_is(res, frame1) else: assert_is_not(res, frame1)
def test_daofind_one_star(self): size = (128, 128) pos = (64, 64) sky = 70 rdnoise = 20 flux = 32000 theta = 0 fwhm = 3 sigma = fwhm * gaussian_fwhm_to_sigma threshold = 10 im = gen_image(size, [pos[0]], [pos[1]], [flux], sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = daofind(im, threshold, sky, rdnoise, fwhm) assert_equal(len(sources), 1) assert_almost_equal(sources['x'][0], pos[0], decimal=1) assert_almost_equal(sources['y'][0], pos[1], decimal=1)
def test_create_and_delete_memmap(tmpdir): # Creation f = os.path.join(tmpdir, 'testarray.npy') g = os.path.join(tmpdir, 'test2array.npy') a = np.ones((30, 30), dtype='f8') b = create_array_memmap(f, a) c = create_array_memmap(g, a, dtype=bool) assert_is_instance(b, np.memmap) assert_is_instance(c, np.memmap) assert_equal(a, b) assert_almost_equal(a, c) assert_true(os.path.exists(f)) assert_true(os.path.exists(g)) # Deletion # Since for the uses the object is overwritten, we do it here too d = delete_array_memmap(b, read=True, remove=False) e = delete_array_memmap(c, read=True, remove=False) assert_is_not_instance(d, np.memmap) assert_is_not_instance(e, np.memmap) assert_is_instance(d, np.ndarray) assert_is_instance(e, np.ndarray) assert_equal(a, d) assert_almost_equal(a, e) assert_true(os.path.exists(f)) assert_true(os.path.exists(g)) d = delete_array_memmap(b, read=False, remove=True) e = delete_array_memmap(c, read=False, remove=True) assert_true(d is None) assert_true(e is None) assert_false(os.path.exists(f)) assert_false(os.path.exists(g)) # None should not raise errors create_array_memmap('dummy', None) delete_array_memmap(None)
def test_sepfind_strong_and_weak(self): size = (128, 128) posx = (60, 90) posy = (20, 90) sky = 800 rdnoise = 20 flux = (32000, 600) sigma = 1.5 theta = 0 im = gen_image(size, posx, posy, flux, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = sepfind(im, 3, sky, rdnoise) assert_equal(len(sources), 2) assert_almost_equal(sources['x'], posx, decimal=0) assert_almost_equal(sources['y'], posy, decimal=0)
def test_sepfind_four_stars_fixed_position(self): size = (1024, 1024) posx = (10, 120, 500, 1000) posy = (20, 200, 600, 800) sky = 800 rdnoise = 20 flux = (15000, 1500, 1500, 35000) sigma = 3 theta = 0 im = gen_image(size, posx, posy, flux, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) sources = sepfind(im, 3, sky, rdnoise) assert_equal(len(sources), 4) assert_almost_equal(sources['x'], posx, decimal=0) assert_almost_equal(sources['y'], posy, decimal=0)
def test_starfind_calc_fwhm(self): size = (512, 512) number = 15 sky = 70 rdnoise = 20 low = 120000 high = 320000 sigma = 5 theta = 0 x, y, f = gen_position_flux(size, number, low, high, rng_seed=456) im = gen_image(size, x, y, f, sky, rdnoise, model='gaussian', sigma=sigma, theta=theta) fwhm = calc_fwhm(im, x, y, box_size=25, model='gaussian', min_fwhm=3.0) assert_almost_equal(fwhm, 2.35 * sigma, decimal=0)
def test_background_simple_cosmic(self): size = (2048, 2048) level = 800 rdnoise = 20 image_test = gen_bkg(size, level, rdnoise) # add some cosmics for i in range(100): # 100 single pixel cosmics x = np.random.randint(0, size[0] - 1) y = np.random.randint(0, size[1] - 1) image_test[x, y] = np.random.randint(16000, 64000) for i in range(4): # 4 square block cosmics x = np.random.randint(0, size[0] - 3) y = np.random.randint(0, size[1] - 3) image_test[x:x + 2, y:y + 2] = np.random.randint(16000, 64000) box_size = 64 filter_size = 3 global_bkg, global_rms = background(image_test, box_size, filter_size, mask=None, global_bkg=True) bkg, rms = background(image_test, box_size, filter_size, mask=None, global_bkg=False) assert_equal(type(global_bkg), float) assert_equal(type(global_rms), float) assert_almost_equal(global_bkg, level, decimal=0) assert_almost_equal(global_rms, rdnoise, decimal=0) assert_equal(bkg.shape, size) assert_equal(rms.shape, size) assert_almost_equal(bkg, np.ones(size) * level, decimal=0) assert_almost_equal(rms, np.ones(size) * rdnoise, decimal=0)
def test_daofind_roundness(self): # compare my implementation with D.Jones PythonPhot # without filtering, both have to output the same round/sharp and # the same coordinates for all stars, because use the same algorithm image_size = (200, 525) xpos = np.arange(10) * 50 + 25 ypos = np.ones_like(xpos) * 30 + np.arange(len(xpos)) * 10 sky = 800 rdnoise = 50 threshold = 50 e = (np.arange(len(xpos))) * 0.1 flux = np.sqrt(np.arange(len(xpos)) + 1) * 80000 sigma_x = np.sqrt(1 / (1 - e**2)) * 2 sigma_y = np.ones_like(xpos) * 2 fwhm = 5 theta = np.zeros_like(xpos) sky = 800 rdnoise = 20 # very low noise expect_round = np.array( [0.0, 0.02, 0.03, 0.08, 0.15, 0.25, 0.39, 0.59, 0.88, 1.39]) im = gen_image(image_size, xpos, ypos, flux, sky, rdnoise, model='gaussian', sigma=(sigma_x, sigma_y), theta=theta) sources = daofind(im, threshold, sky, rdnoise, fwhm, sharp_limit=None, round_limit=None) assert_equal(len(sources), len(xpos)) assert_almost_equal(sources['x'], xpos, decimal=0) assert_almost_equal(sources['y'], ypos, decimal=0) assert_almost_equal(sources['round'], -expect_round, decimal=1)