def test_calibration_plot_two_cols(self): """Simple calibration from scans.""" caltable = CalibratorTable.read(self.calfile) caltable.plot_two_columns('RA', "Flux/Counts", xerrcol="RA err", yerrcol="Flux/Counts Err", test=True)
def test_calibration_counts(self): """Simple calibration from scans.""" caltable = CalibratorTable.read(self.calfile) caltable = caltable[compare_strings(caltable['Source'], 'DummyCal')] caltable_0 = caltable[compare_strings(caltable['Chan'], 'Feed0_LCP')] assert np.all( np.abs(caltable_0['Counts'] - 100.) < 3 * caltable_0['Counts Err']) caltable_1 = caltable[compare_strings(caltable['Chan'], 'Feed0_RCP')] assert np.all( np.abs(caltable_1['Counts'] - 80.) < 3 * caltable_1['Counts Err'])
def test_check_consistency_fails_with_bad_data(self): scan_list = \ list_scans(self.caldir, ['./']) + \ list_scans(self.caldir2, ['./']) + \ list_scans(self.caldir3, ['./']) + \ list_scans(self.crossdir, ['./']) scan_list.sort() caltable = CalibratorTable() caltable.from_scans(scan_list) caltable.update() res = caltable.check_consistency(channel='Feed0_LCP') assert not np.all(res)
def test_calibration_width(self): """Simple calibration from scans.""" caltable = CalibratorTable.read(self.calfile) caltable0 = caltable[compare_strings(caltable['Chan'], 'Feed0_LCP')] assert np.all( np.abs(caltable0['Width'] - 2.5 / 60.) < 5 * caltable0['Width Err']) caltable1 = caltable[compare_strings(caltable['Chan'], 'Feed0_RCP')] assert np.all( np.abs(caltable1['Width'] - 2.5 / 60.) < 5 * caltable1['Width Err']) beam, beam_err = caltable.beam_width(channel='Feed0_LCP') assert np.all(beam - np.radians(2.5 / 60) < 3 * beam_err)
def test_Jy_over_counts_rough_one_bad_value(self, logger, caplog): caltable = CalibratorTable.read(self.calfile) flux_quantity = _get_flux_quantity('Jy/beam') good = ~np.isnan(caltable[flux_quantity + "/Counts"]) good = good & (caltable['Chan'] == 'Feed0_LCP') assert np.count_nonzero(good) > 1 std = np.std(np.diff(caltable[flux_quantity + "/Counts"][good])) assert std > 0 firstidx = np.where(good)[0][0] caltable[flux_quantity + "/Counts"][firstidx] = 20000 assert caltable[flux_quantity + "/Counts"][firstidx] == 20000 Jc, Jce = caltable.Jy_over_counts_rough(channel='Feed0_LCP', map_unit='Jy/beam') assert 'Outliers: ' in caplog.text Cj, Cje = caltable.counts_over_Jy(channel='Feed0_LCP') np.testing.assert_allclose(Jc, 1 / Cj)
def test_bad_file_generic_error(self, logger, caplog): caltable = CalibratorTable() caltable.from_scans( [os.path.join(self.config['datadir'], 'calibrators', 'bubu.fits')]) assert "Error while processing" in caplog.text
def test_import_calibratortable(): from srttools.calibration import CalibratorTable s = CalibratorTable() assert isinstance(s, Table)
def test_check_consistency_all(self): caltable = CalibratorTable.read(self.calfile) res = caltable.check_consistency() assert np.all(res)
def test_check_class_from_file(self): caltable = CalibratorTable.read(self.calfile) assert caltable.check_up_to_date()
def test_check_class(self): caltable = CalibratorTable() caltable.from_scans(self.scan_list) with pytest.warns(UserWarning): caltable.check_up_to_date()
def test_get_fluxes_empty_return_none(self): caltable = CalibratorTable() assert caltable.get_fluxes() is None
def test_update_empty_return_none(self): caltable = CalibratorTable() assert caltable.update() is None
def test_check_up_to_date_empty_return_false(self): caltable = CalibratorTable() assert not caltable.check_up_to_date()
def test_check_not_empty(self): caltable = CalibratorTable() assert not caltable.check_not_empty()
def test_calibration_show(self): """Simple calibration from scans.""" caltable = CalibratorTable.read(self.calfile) caltable.show()
def test_calibrated_crossscans(self): caltable = CalibratorTable.read(self.calfile) dummy_flux, dummy_flux_err = \ caltable.calculate_src_flux(source='DummySrc', channel='Feed0_LCP') assert (dummy_flux[0] - 0.52) < dummy_flux_err[0] * 3
def test_Jy_over_counts_and_back(self): caltable = CalibratorTable.read(self.calfile) Jc, Jce = caltable.Jy_over_counts(channel='Feed0_LCP') Cj, Cje = caltable.counts_over_Jy(channel='Feed0_LCP') np.testing.assert_allclose(Jc, 1 / Cj)
def test_check_consistency_chan_by_chan(self, chan): caltable = CalibratorTable.read(self.calfile) res = caltable.check_consistency(channel=chan) assert np.all(res)
def test_bad_file_missing_key(self, logger, caplog): caltable = CalibratorTable() caltable.from_scans([ os.path.join(self.config['datadir'], 'calibrators', 'summary.fits') ]) assert "Missing key" in caplog.text
def setup_class(klass): import os klass.curdir = os.path.dirname(__file__) klass.datadir = os.path.join(klass.curdir, 'data') klass.config_file = \ os.path.abspath(os.path.join(klass.datadir, "calibrators.ini")) klass.config_file_empty = \ os.path.abspath(os.path.join(klass.datadir, "calibrators_nocal.ini")) klass.config = read_config(klass.config_file) klass.simdir = klass.caldir = os.path.join(klass.datadir, 'sim') if os.getenv('CI') and os.path.exists(klass.simdir): shutil.rmtree(klass.simdir) klass.caldir = os.path.join(klass.simdir, 'calibration') klass.caldir2 = os.path.join(klass.simdir, 'calibration2') klass.caldir3 = os.path.join(klass.simdir, 'calibration_bad') klass.crossdir = os.path.join(klass.simdir, 'crossscans') if not os.path.exists(klass.caldir): log.info('Fake calibrators: DummyCal, 1 Jy.') mkdir_p(klass.caldir) sim_crossscans(5, klass.caldir) if not os.path.exists(klass.caldir2): log.info('Fake calibrators: DummyCal2, 1.321 Jy.') mkdir_p(klass.caldir2) sim_crossscans(5, klass.caldir2, srcname='DummyCal2', scan_func=cal2_scan_func) if not os.path.exists(klass.caldir3): log.info('Fake calibrators: DummyCal2, wrong flux 0.52 Jy.') mkdir_p(klass.caldir3) sim_crossscans(1, klass.caldir3, srcname='DummyCal2', scan_func=source_scan_func) if not os.path.exists(klass.crossdir): log.info('Fake cross scans: DummySrc, 0.52 Jy.') mkdir_p(klass.crossdir) sim_crossscans(5, klass.crossdir, srcname='DummySrc', scan_func=source_scan_func) klass.scan_list = \ list_scans(klass.caldir, ['./']) + \ list_scans(klass.caldir2, ['./']) + \ list_scans(klass.crossdir, ['./']) klass.scan_list.sort() caltable = CalibratorTable() caltable.from_scans(klass.scan_list) caltable.update() klass.calfile = os.path.join(klass.curdir, 'test_calibration.hdf5') for calfile in [klass.calfile, klass.calfile.replace('hdf5', 'csv')]: if os.path.exists(calfile): os.remove(calfile) caltable.write(klass.calfile, overwrite=True) caltable.write(klass.calfile.replace('hdf5', 'csv'))
def setup_class(klass): import os klass.curdir = os.path.dirname(__file__) klass.datadir = os.path.join(klass.curdir, 'data') klass.sim_dir = os.path.join(klass.datadir, 'sim') klass.prodir_ra = os.path.join(klass.datadir, 'sim', 'test_image', 'gauss_ra') klass.prodir_dec = os.path.join(klass.datadir, 'sim', 'test_image', 'gauss_dec') klass.obsdir_ra = os.path.join(klass.datadir, 'sim', 'gauss_ra') klass.obsdir_dec = os.path.join(klass.datadir, 'sim', 'gauss_dec') klass.config_file = \ os.path.abspath(os.path.join(klass.sim_dir, 'test_config_sim.ini')) klass.caldir = os.path.join(klass.datadir, 'sim', 'calibration') klass.simulated_flux = 0.25 # First off, simulate a beamed observation ------- sim_config_file(klass.config_file, add_garbage=True, prefix="./") if (not os.path.exists(klass.obsdir_ra)) or \ (not os.path.exists(klass.obsdir_dec)): mkdir_p(klass.obsdir_ra) mkdir_p(klass.obsdir_dec) log.info('Fake map: Point-like (but Gaussian beam shape), ' '{} Jy.'.format(klass.simulated_flux)) sim_map(klass.obsdir_ra, klass.obsdir_dec) defective_dir = os.path.join(klass.sim_dir, 'defective') if not os.path.exists(defective_dir): shutil.copytree(os.path.join(klass.datadir, 'calibrators'), defective_dir) # Copy skydip scan skydip_dir = os.path.join(klass.datadir, 'gauss_skydip') new_skydip_dir = os.path.join(klass.sim_dir, 'gauss_skydip') if os.path.exists(skydip_dir) and not os.path.exists(new_skydip_dir): shutil.copytree(skydip_dir, new_skydip_dir) caltable = CalibratorTable() caltable.from_scans(glob.glob(os.path.join(klass.caldir, '*.fits')), debug=True) caltable.update() klass.calfile = os.path.join(klass.datadir, 'calibrators.hdf5') caltable.write(klass.calfile, overwrite=True) klass.config = read_config(klass.config_file) klass.raonly = os.path.abspath(os.path.join(klass.datadir, 'test_raonly.ini')) klass.deconly = os.path.abspath(os.path.join(klass.datadir, 'test_deconly.ini')) if HAS_PYREGION: excluded_xy, excluded_radec = \ _excluded_regions_from_args([os.path.join(klass.datadir, "center.reg")]) else: excluded_xy, excluded_radec = None, None klass.scanset = ScanSet(klass.config_file, nosub=False, norefilt=False, debug=True, avoid_regions=excluded_radec) klass.scanset.write('test.hdf5', overwrite=True) klass.stdinfo = {} klass.stdinfo['FLAG'] = None klass.stdinfo['zap'] = intervals() klass.stdinfo['base'] = intervals() klass.stdinfo['fitpars'] = np.array([0, 0]) def scan_no(scan_str): basename = os.path.splitext(os.path.basename(scan_str))[0] return int(basename.replace('Dec', '').replace('Ra', '')) klass.dec_scans = \ dict([(scan_no(s), s) for s in klass.scanset.scan_list if 'Dec' in s]) klass.ra_scans = \ dict([(scan_no(s), s) for s in klass.scanset.scan_list if 'Ra' in s]) klass.n_ra_scans = max(list(klass.ra_scans.keys())) klass.n_dec_scans = max(list(klass.dec_scans.keys())) if HAS_MPL: plt.ioff()