def test_residual_image_bag(self): context = 'wstack' vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} dirty_bag = invert_bag(self.vis_bag, self.model, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) psf_bag = invert_bag(self.vis_bag, self.model, dopsf=True, context=context, normalize=True, vis_slices=vis_slices[context]) model_bag = deconvolve_bag(dirty_bag, psf_bag, niter=1000, gain=0.1, algorithm='msclean', threshold=0.01, window_shape=None) residual_bag = residual_image_bag(self.vis_bag, model_bag, context=context, vis_slices=vis_slices[context]) final = list(residual_bag)[0][0] export_image_to_fits( final, '%s/test_bag_%s_residual.fits' % (self.results_dir, context)) qa = qa_image(final, context=context) assert qa.data['max'] < 15.0, str(qa)
def test_deconvolve_bag(self): context = 'wstack' vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} dirty_bag = invert_bag(self.vis_bag, self.model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) psf_bag = invert_bag(self.vis_bag, self.model_bag, dopsf=True, context=context, normalize=True, vis_slices=vis_slices[context]) dirty_bag = reify(dirty_bag) psf_bag = reify(psf_bag) model_bag = deconvolve_bag(dirty_bag, psf_bag, self.empty_model_bag, niter=1000, gain=0.7, algorithm='msclean', threshold=0.01, window_shape=None) model = model_bag.compute()[0]['image'] qa = qa_image(model, context=context) export_image_to_fits( model, '%s/test_bags_%s_deconvolve.fits' % (self.dir, context)) assert numpy.abs(qa.data['max'] - 60.5) < 0.1, str(qa)
def test_deconvolve_bag(self): context = 'wstack' vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} dirty_bag = invert_bag(self.vis_bag, self.model, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) psf_bag = invert_bag(self.vis_bag, self.model, dopsf=True, context=context, normalize=True, vis_slices=vis_slices[context]) model_bag = deconvolve_bag(dirty_bag, psf_bag, niter=1000, gain=0.7, algorithm='msclean', threshold=0.01, window_shape=None) model = list(model_bag)[0] qa = qa_image(model, context=context) export_image_to_fits( model, '%s/test_bag_%s_deconvolve.fits' % (self.results_dir, context)) assert numpy.abs(qa.data['max'] - 60.5293158864) < 1.0e-7, str(qa)
def test_residual_image_bag(self): context = 'wstack' vis_slices = {'wstack': 101} dirty_bag = invert_bag(self.vis_bag, self.empty_model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) psf_bag = invert_bag(self.vis_bag, self.empty_model_bag, dopsf=True, context=context, normalize=True, vis_slices=vis_slices[context]) dirty_bag = reify(dirty_bag) psf_bag = reify(psf_bag) model_bag = deconvolve_bag(dirty_bag, psf_bag, self.empty_model_bag, niter=1000, gain=0.1, algorithm='msclean', threshold=0.01, window_shape=None) residual_bag = residual_image_bag(self.vis_bag, model_bag, context=context, vis_slices=vis_slices[context]) final = residual_bag.compute()[0]['image'][0] export_image_to_fits( final, '%s/test_bags_%s_residual.fits' % (self.dir, context)) qa = qa_image(final, context=context) assert qa.data['max'] < 15.0, str(qa)
def test_predict_bag(self): errors = {'2d': 28.0, 'timeslice': 30.0, 'wstack': 2.3} vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} for context in ['wstack', 'timeslice']: model_vis_bag = predict_bag(self.vis_bag, self.model_bag, context, vis_slices=vis_slices[context]) model_vis_bag = reify(model_vis_bag) error_vis_bag = self.vis_bag.map(predict_record_subtract, model_vis_bag) error_vis_bag = reify(error_vis_bag) error_image_bag = invert_bag(error_vis_bag, self.model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) result = error_image_bag.compute() error_image = result[0]['image'][0] export_image_to_fits( error_image, '%s/test_bags_%s_predict_error_image.fits' % (self.dir, context)) qa = qa_image(error_image, context='error image for %s' % context) assert qa.data['max'] < errors[context], str(qa)
def test_restore_bag(self): peaks = {'wstack': 98.8113067286} vis_slices = {'wstack': 101} context = 'wstack' dirty_bag = invert_bag(self.vis_bag, self.model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) psf_bag = invert_bag(self.vis_bag, self.model_bag, dopsf=True, context=context, normalize=True, vis_slices=vis_slices[context]) dirty_bag = reify(dirty_bag) psf_bag = reify(psf_bag) model_bag = deconvolve_bag(dirty_bag, psf_bag, self.empty_model_bag, niter=1000, gain=0.7, algorithm='msclean', threshold=0.01, window_shape=None) model_bag = reify(model_bag) res_image_bag = residual_image_bag(self.vis_bag, model_bag, context=context, vis_slices=vis_slices[context]) res_image_bag = reify(res_image_bag) residual = res_image_bag.compute()[0]['image'][0] export_image_to_fits( residual, '%s/test_bags_%s_residual.fits' % (self.dir, context)) final_bag = restore_bag(model_bag, psf_bag, res_image_bag) final = final_bag.compute()[0] qa = qa_image(final, context=context) export_image_to_fits( final, '%s/test_bags_%s_restored.fits' % (self.dir, context)) assert numpy.abs(qa.data['max'] - peaks[context]) < 0.1, str(qa)
def test_invert_bag(self): peaks = {'2d': 65.2997439062, 'timeslice': 99.6183393299, 'wstack': 100.702701119} vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} for context in ['wstack', '2d', 'timeslice']: dirty_bag = invert_bag(self.vis_bag, self.empty_model, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) dirty_bag.visualize('test_invert_bag.svg') result=dirty_bag.compute() dirty, sumwt = list(dirty_bag)[0] export_image_to_fits(dirty, '%s/test_bag_%s_dirty.fits' % (self.results_dir, context)) qa = qa_image(dirty, context=context) assert numpy.abs(qa.data['max'] - peaks[context]) < 1.0e-2, str(qa)
def test_restore_bag(self): cvis_bag = self.vis_bag.map(coalesce_visibility) peaks = {'timeslice': 103.55068395, 'wstack': 98.790503433} vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} for context in ['timeslice', 'wstack']: dirty_bag = invert_bag(self.vis_bag, self.model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) psf_bag = invert_bag(self.vis_bag, self.model_bag, dopsf=True, context=context, normalize=True, vis_slices=vis_slices[context]) model_bag = deconvolve_bag(dirty_bag, psf_bag, self.empty_model_bag, niter=1000, gain=0.7, algorithm='msclean', threshold=0.01, window_shape=None) residual_bag = residual_image_bag(cvis_bag, model_bag, context=context, vis_slices=vis_slices[context]) residual = list(residual_bag)[0][0] export_image_to_fits(residual, '%s/test_bag_%s_residual.fits' % (self.results_dir, context)) final_bag = restore_bag(model_bag, psf_bag, residual_bag) final = list(final_bag)[0] qa = qa_image(final, context=context) export_image_to_fits(final, '%s/test_bag_%s_restored.fits' % (self.results_dir, context)) assert numpy.abs(qa.data['max'] - peaks[context]) < 1.0e-2, str(qa)
def test_predict_bag(self): cvis_bag = self.vis_bag.map(coalesce_visibility) errors = {'2d': 28.0, 'timeslice': 30.0, 'wstack': 2.3} vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} for context in ['timeslice', 'wstack']: model_vis_bag = predict_bag(cvis_bag, self.model_bag, context, vis_slices=vis_slices[context])\ .map(decoalesce_visibility, self.vis_bag) model_vis_bag = bag.from_sequence(model_vis_bag.compute()) error_vis_bag = self.vis_bag.map(subtract_visibility, model_vis_bag).compute() error_vis_bag = bag.from_sequence(error_vis_bag) error_image_bag = invert_bag(error_vis_bag, self.model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) result = list(error_image_bag)[0] error_image = result[0] export_image_to_fits(error_image, '%s/test_bag_%s_predict_error_image.fits' % (self.results_dir, context)) qa = qa_image(error_image, context='error image for %s' % context) assert qa.data['max'] < errors[context], str(qa)
def test_invert_bag(self): peaks = { '2d': 65.2997439062, 'timeslice': 99.6183393299, 'wstack': 101.099173809 } vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} for context in ['2d', 'timeslice', 'wstack']: dirty_bag = invert_bag(self.vis_bag, self.empty_model_bag, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) dirty, sumwt = dirty_bag.compute()[0]['image'] export_image_to_fits( dirty, '%s/test_bags_%s_dirty.fits' % (self.dir, context)) qa = qa_image(dirty, context=context) assert numpy.abs(qa.data['max'] - peaks[context]) < 1.0e-2, str(qa)
def test_predict_bag(self): errors = {'2d': 28.0, 'timeslice': 31.0, 'wstack': 2.3} vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} for context in ['2d', 'timeslice', 'wstack']: model_vis_bag = predict_bag(self.vis_bag, self.model, context, vis_slices=vis_slices[context]) error_vis_bag = residual_vis_bag(self.vis_bag, model_vis_bag) error_image_bag = invert_bag(error_vis_bag, self.model, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) result = list(error_image_bag)[0] error_image = result[0] export_image_to_fits( error_image, '%s/test_bag_%s_predict_error_image.fits' % (self.results_dir, context)) qa = qa_image(error_image, context='error image for %s' % context) assert qa.data['max'] < errors[context], str(qa)
def test_invert_bag(self): peaks = { '2d': 65.440798589, 'timeslice': 99.7403479215, 'wstack': 100.654001673 } vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101} model = copy_image(self.model) for context in ['wstack', '2d', 'timeslice']: dirty_bag = invert_bag(self.vis_bag, model, dopsf=False, context=context, normalize=True, vis_slices=vis_slices[context]) dirty, sumwt = list(dirty_bag)[0] export_image_to_fits( dirty, '%s/test_bag_%s_dirty.fits' % (self.results_dir, context)) qa = qa_image(dirty, context=context) assert numpy.abs(qa.data['max'] - peaks[context]) < 1.0e-7, str(qa)
def test_selfcal_global_bag(self): self.setupVis(add_errors=True) selfcal_vis_bag = selfcal_bag(self.vis_bag, self.model_bag, global_solution=True, context='wstack', vis_slices=51) dirty_bag = invert_bag(selfcal_vis_bag, self.model_bag, dopsf=False, normalize=True, context='wstack', vis_slices=101) if self.compute: dirty, sumwt = dirty_bag.compute()[0]['image'] export_image_to_fits( dirty, '%s/test_imaging_bags_global_selfcal_dirty.fits' % (self.dir)) qa = qa_image(dirty) assert numpy.abs(qa.data['max'] - 101.7) < 1.0, str(qa) assert numpy.abs(qa.data['min'] + 3.5) < 1.0, str(qa)
def ical_pipeline_bag(block_vis_bag, model_bag, context='2d', first_selfcal=None, **kwargs) -> bag: """Create bag for ICAL pipeline :param vis_bag: :param model_bag: :param context: Imaging context :param first_selfcal: First cycle for phase only selfcal :param kwargs: Parameters for functions in bags :return: """ assert isinstance(block_vis_bag, bag.Bag), block_vis_bag assert isinstance(model_bag, bag.Bag), model_bag vis_bag = block_vis_bag.map(map_record, convert_blockvisibility_to_visibility) psf_bag = invert_bag(vis_bag, model_bag, context=context, dopsf=True, **kwargs) psf_bag = reify(psf_bag) # Make the predicted visibilities, selfcalibrate against it correcting the gains, then # form the residual visibility, then make the residual image model_vis_bag = reify( predict_bag(vis_bag, model_bag, context=context, **kwargs)) if first_selfcal is not None and first_selfcal == 0: vis_bag = reify( selfcal_record(block_vis_bag, model_vis_bag=model_vis_bag, **kwargs)) res_vis_bag = reify(residual_vis_bag(vis_bag, model_vis_bag)) res_bag = invert_bag(res_vis_bag, model_bag, context=context, dopsf=False, **kwargs) res_bag = reify(res_bag) deconvolve_model_bag = reify( deconvolve_bag(res_bag, psf_bag, model_bag, **kwargs)) nmajor = get_parameter(kwargs, "nmajor", 5) if nmajor > 1: for cycle in range(nmajor): model_vis_bag = reify( predict_bag(vis_bag, deconvolve_model_bag, context=context, **kwargs)) if first_selfcal is not None and cycle >= first_selfcal: vis_bag = reify( selfcal_record(block_vis_bag, model_vis_bag=model_vis_bag, **kwargs)) res_vis_bag = reify(residual_vis_bag(vis_bag, model_vis_bag)) res_bag = invert_bag(res_vis_bag, model_bag, context=context, dopsf=False, **kwargs) res_bag = reify(res_bag) deconvolve_model_bag = reify( deconvolve_bag(res_bag, psf_bag, deconvolve_model_bag, **kwargs)) res_bag = residual_image_bag(vis_bag, deconvolve_model_bag, context=context, **kwargs) res_bag = reify(res_bag) deconvolve_model_bag = reify(deconvolve_model_bag) rest_bag = restore_bag(deconvolve_model_bag, psf_bag, res_bag, **kwargs) rest_bag = reify(rest_bag) return bag.from_sequence([deconvolve_model_bag, res_bag, rest_bag])