def test_fg_creation_custom_hdu(self, tmpdir, hdu): tmpdir, flist = tmpdir fg = FitsFileGroup(location=str(tmpdir / 'custom_hdu'), ext=hdu) assert_is_instance(fg, FitsFileGroup) assert_equal(len(fg), 10) assert_equal(sorted(fg.files), sorted(flist['custom_hdu'])) for k in ('object', 'exptime', 'observer', 'filter'): assert_in(k, fg.summary.colnames) for i in fg.summary: assert_equal(i['object'], 'bias')
def test_simbad_catalog_get_simbad(self): # must be free of fluxdata s = self.cat.get_simbad() assert_is_instance(s, Simbad.__class__) for i in s.get_votable_fields(): assert_not_in('fluxdata', i) # must contain flux data s = self.cat.get_simbad(band='V') assert_is_instance(s, Simbad.__class__) assert_in('fluxdata(V)', s.get_votable_fields())
def test_extract_header_nosip(self): header = fits.Header.fromstring(_base_header + _wcs_no_sip, sep='\n') h, wcs = extract_header_wcs(header) assert_is_instance(wcs, WCS) assert_equal(wcs.wcs.ctype[0], 'RA---TAN') assert_equal(wcs.wcs.ctype[1], 'DEC--TAN') assert_is_instance(h, fits.Header) for i in _comon_wcs_keys: assert_not_in(f'{i}1', h.keys()) assert_not_in(f'{i}2', h.keys()) assert_in('DATE-OBS', h.keys()) assert_false(h is header) assert_not_equal(h, header)
def test_assert_in(self): assert_in(1, [1, 2, 3, 4, 5]) assert_in('a', 'abc') with pytest.raises(AssertionError): assert_in('d', 'abc') with pytest.raises(AssertionError): assert_in(1, [2, 3, 4])
def test_logger_remove_handler(): mylog = logger.getChild('testing') msg = 'Some error happend here.' logs = [] lh = log_to_list(mylog, logs) mylog.setLevel('DEBUG') mylog.error(msg) assert_is_instance(lh, ListHandler) assert_in(lh, mylog.handlers) mylog.removeHandler(lh) assert_not_in(lh, mylog.handlers) assert_equal(logs[0], msg) assert_equal(lh.log_list[0], msg) assert_equal(lh.log_list, logs)
def test_chunk_yielder_uncertainty(self): n = 100 d = np.random.random((100, 100)).astype(np.float64) u = np.random.random((100, 100)).astype(np.float64) l = [FrameData(d, uncertainty=u, unit='adu') for i in range(n)] # simple sum with uncertainties comb = ImCombiner(max_memory=2e6, dtype=np.float64) comb._load_images(l) i = 0 for chunk, unct, slc in comb._chunk_yielder(method='sum'): i += 1 for k, un in zip(chunk, unct): assert_in(k.shape, ((7, 100), (2, 100))) assert_almost_equal(k, d[slc]) assert_almost_equal(un, u[slc]) assert_is_instance(un, np.ma.MaskedArray) assert_equal(i, 15) # if a single uncertainty is empty, disable it logs = [] lh = log_to_list(logger, logs, False) level = logger.getEffectiveLevel() logger.setLevel('DEBUG') l[5].uncertainty = None comb = ImCombiner(max_memory=2e6, dtype=np.float64) comb._load_images(l) i = 0 for chunk, unct, slc in comb._chunk_yielder(method='sum'): i += 1 for k in chunk: assert_in(k.shape, ((7, 100), (2, 100))) assert_almost_equal(k, d[slc]) assert_equal(unct, None) assert_equal(i, 15) assert_in( 'One or more frames have empty uncertainty. ' 'Some features are disabled.', logs) logs.clear() logger.setLevel(level) logger.removeHandler(lh)
def test_chunk_yielder_f32(self): # using float32, the number of chunks are almost halved n = 100 d = np.random.random((100, 100)).astype(np.float64) l = [FrameData(d, unit='adu') for i in range(n)] # data size = 4 000 000 = 4 bytes * 100 * 100 * 100 # mask size = 1 000 000 = 1 bytes * 100 * 100 * 100 # total size = 5 000 000 comb = ImCombiner(max_memory=1e6, dtype=np.float32) comb._load_images(l) logs = [] lh = log_to_list(logger, logs, False) level = logger.getEffectiveLevel() logger.setLevel('DEBUG') # for median, tot_size=5*4.5=22.5 # xstep = 4, so n_chuks=25 i = 0 for chunk, unct, slc in comb._chunk_yielder(method='median'): i += 1 for k in chunk: assert_equal(k.shape, (4, 100)) assert_almost_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 25) assert_in('Splitting the images into 25 chunks.', logs) logs.clear() # for mean and sum, tot_size=5*3=15 # xstep = 6, so n_chunks=16+1 i = 0 for chunk, unct, slc in comb._chunk_yielder(method='mean'): i += 1 for k in chunk: assert_in(k.shape, [(6, 100), (4, 100)]) assert_almost_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 17) assert_in('Splitting the images into 17 chunks.', logs) logs.clear() i = 0 for chunk, unct, slc in comb._chunk_yielder(method='sum'): i += 1 for k in chunk: assert_in(k.shape, [(6, 100), (4, 100)]) assert_almost_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 17) assert_in('Splitting the images into 17 chunks.', logs) logs.clear() # this should not split into chunks comb = ImCombiner(max_memory=1e8, dtype=np.float32) comb._load_images(l) i = 0 for chunk, unct, slc in comb._chunk_yielder(method='median'): i += 1 for k in chunk: assert_equal(k.shape, (100, 100)) assert_almost_equal(k, d) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 1) assert_equal(len(logs), 0) logs.clear() # this should split in 300 chunks! # total_size = 4.5*5e6=22.5e6 = 225 chunks # x_step = 1 # y_step = 45 comb = ImCombiner(max_memory=1e5, dtype=np.float32) comb._load_images(l) i = 0 for chunk, unct, slc in comb._chunk_yielder(method='median'): i += 1 for k in chunk: assert_in(k.shape, ((1, 45), (1, 10))) assert_almost_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 300) assert_in('Splitting the images into 300 chunks.', logs) logs.clear() logger.setLevel(level) logger.removeHandler(lh)
def test_chunk_yielder_f64(self): n = 100 d = np.random.random((100, 100)).astype(np.float64) l = [FrameData(d, unit='adu') for i in range(n)] # data size = 8 000 000 = 8 bytes * 100 * 100 * 100 # mask size = 1 000 000 = 1 bytes * 100 * 100 * 100 # total size = 9 000 000 comb = ImCombiner(max_memory=1e6, dtype=np.float64) comb._load_images(l) logs = [] lh = log_to_list(logger, logs, False) level = logger.getEffectiveLevel() logger.setLevel('DEBUG') # for median, tot_size=9*4.5=41 # xstep = 2, so n_chuks=50 i = 0 for chunk, unct, slc in comb._chunk_yielder(method='median'): i += 1 for k in chunk: assert_equal(k.shape, (2, 100)) assert_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 50) assert_in('Splitting the images into 50 chunks.', logs) logs.clear() # for mean and sum, tot_size=9*3=27 # xstep = 3, so n_chunks=33+1 i = 0 for chunk, unct, slc in comb._chunk_yielder(method='mean'): i += 1 for k in chunk: assert_in(k.shape, [(3, 100), (1, 100)]) assert_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 34) assert_in('Splitting the images into 34 chunks.', logs) logs.clear() i = 0 for chunk, unct, slc in comb._chunk_yielder(method='sum'): i += 1 for k in chunk: assert_in(k.shape, [(3, 100), (1, 100)]) assert_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 34) assert_in('Splitting the images into 34 chunks.', logs) logs.clear() # this should not split into chunks comb = ImCombiner(max_memory=1e8) comb._load_images(l) i = 0 for chunk, unct, slc in comb._chunk_yielder(method='median'): i += 1 for k in chunk: assert_equal(k.shape, (100, 100)) assert_equal(k, d) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 1) assert_equal(len(logs), 0) logs.clear() # this should split in 400 chunks! comb = ImCombiner(max_memory=1e5) comb._load_images(l) i = 0 for chunk, unct, slc in comb._chunk_yielder(method='median'): i += 1 for k in chunk: assert_equal(k.shape, (1, 25)) assert_equal(k, d[slc]) assert_is_none(unct) assert_is_instance(k, np.ma.MaskedArray) assert_equal(i, 400) assert_in('Splitting the images into 400 chunks.', logs) logs.clear() logger.setLevel(level) logger.removeHandler(lh)
def test_framemeta_get(self): a = FrameMeta({'A': 1, 'b': 'test', 'TesT': 'AaA'}) res = a.get('test') assert_equal(res, 'AaA') assert_in('test', a) assert_in('TEST', a)
def test_raise_north_angle(): with pytest.raises(ValueError) as exc: wcs_from_coords(0, 0, 0, 0, 0, 'not a direction') assert_in('invalid value for north', str(exc.value))
def test_invalid_op(): frame1 = FrameData(np.zeros((10, 10)), unit='') frame2 = FrameData(np.zeros((10, 10)), unit='') with pytest.raises(ValueError) as exc: imarith(frame1, frame2, 'not an op') assert_in('not supported', str(exc.value))