def insert_unittest_errors(vt, seed=180555): """Simulate gain errors and apply :param vt: :param phase_error: :param amplitude_error: :return: """ numpy.random.seed(seed) controls = create_calibration_controls() amp_errors = {'T': 0.0, 'G': 0.01, 'B': 0.01} phase_errors = {'T': 1.0, 'G': 0.1, 'B': 0.01} for c in "TGB": gaintable = \ create_gaintable_from_blockvisibility(vt, timeslice=controls[c]['timeslice']) gaintable = simulate_gaintable( gaintable, timeslice=controls[c]['timeslice'], phase_only=controls[c]['phase_only'], crosspol=controls[c]['shape'] == 'matrix', phase_error=phase_errors[c], amplitude_error=amp_errors[c]) vt = apply_gaintable(vt, gaintable, inverse=True, timeslice=controls[c]['timeslice']) return vt
def test_ical_pipeline(self): self.actualSetUp(add_errors=True, block=True) controls = create_calibration_controls() controls['T']['first_selfcal'] = 2 controls['G']['first_selfcal'] = 3 controls['B']['first_selfcal'] = 4 controls['T']['timescale'] = 'auto' controls['G']['timescale'] = 'auto' controls['B']['timescale'] = 1e5 ical_graph = \ create_ical_pipeline_graph(self.vis_graph_list, model_graph=self.model_graph, context='wstack', do_selfcal=1, global_solution=False, algorithm='mmclean', vis_slices=51, facets=1, niter=1000, fractional_threshold=0.1, nmoments=3, nchan=self.freqwin, threshold=2.0, nmajor=6, gain=0.1) if self.compute: clean, residual, restored = ical_graph.compute() export_image_to_fits( clean[0], '%s/test_pipelines_ical_pipeline_clean.fits' % self.dir) export_image_to_fits( residual[0][0], '%s/test_pipelines_ical_pipeline_residual.fits' % self.dir) export_image_to_fits( restored[0], '%s/test_pipelines_ical_pipeline_restored.fits' % self.dir) qa = qa_image(restored[0]) assert numpy.abs(qa.data['max'] - 116.86978265) < 5.0, str(qa) assert numpy.abs(qa.data['min'] + 0.323425377573) < 5.0, str(qa)
def test_calibrate_function(self): self.actualSetup('stokesI', 'stokesI', f=[100.0]) # Prepare the corrupted visibility data gt = create_gaintable_from_blockvisibility(self.vis) log.info("Created gain table: %s" % (gaintable_summary(gt))) gt = simulate_gaintable(gt, phase_error=10.0, amplitude_error=0.1, timeslice='auto') bgt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.01, timeslice=1e5) original = copy_visibility(self.vis) self.vis = apply_gaintable(self.vis, bgt, timeslice=1e5) self.vis = apply_gaintable(self.vis, gt, timeslice='auto') # Now get the control dictionary and calibrate controls = create_calibration_controls() controls['T']['first_selfcal'] = 0 controls['B']['first_selfcal'] = 0 calibrated_vis, gaintables = calibrate_function(self.vis, original, context='TB', controls=controls) residual = numpy.max(gaintables['T'].residual) assert residual < 3e-2, "Max T residual = %s" % (residual) residual = numpy.max(gaintables['B'].residual) assert residual < 6e-5, "Max B residual = %s" % (residual)
def ical(block_vis: BlockVisibility, model: Image, components=None, context='2d', controls=None, **kwargs): """ Post observation image, deconvolve, and self-calibrate :param vis: :param model: Model image :param components: Initial components :param context: Imaging context :param controls: Calibration controls dictionary :return: model, residual, restored """ nmajor = get_parameter(kwargs, 'nmajor', 5) log.info("ical: Performing %d major cycles" % nmajor) do_selfcal = get_parameter(kwargs, "do_selfcal", False) if controls is None: controls = create_calibration_controls(**kwargs) # The model is added to each major cycle and then the visibilities are # calculated from the full model vis = convert_blockvisibility_to_visibility(block_vis) block_vispred = copy_visibility(block_vis, zero=True) vispred = convert_blockvisibility_to_visibility(block_vispred) vispred.data['vis'][...] = 0.0 visres = copy_visibility(vispred) vispred = predict_function(vispred, model, context=context, **kwargs) if components is not None: vispred = predict_skycomponent_visibility(vispred, components) if do_selfcal: vis, gaintables = calibrate_function(vis, vispred, 'TGB', controls, iteration=-1) visres.data['vis'] = vis.data['vis'] - vispred.data['vis'] dirty, sumwt = invert_function(visres, model, context=context, **kwargs) log.info("Maximum in residual image is %.6f" % (numpy.max(numpy.abs(dirty.data)))) psf, sumwt = invert_function(visres, model, dopsf=True, context=context, **kwargs) thresh = get_parameter(kwargs, "threshold", 0.0) for i in range(nmajor): log.info("ical: Start of major cycle %d of %d" % (i, nmajor)) cc, res = deconvolve_cube(dirty, psf, **kwargs) model.data += cc.data vispred.data['vis'][...] = 0.0 vispred = predict_function(vispred, model, context=context, **kwargs) if do_selfcal: vis, gaintables = calibrate_function(vis, vispred, 'TGB', controls, iteration=i) visres.data['vis'] = vis.data['vis'] - vispred.data['vis'] dirty, sumwt = invert_function(visres, model, context=context, **kwargs) log.info("Maximum in residual image is %s" % (numpy.max(numpy.abs(dirty.data)))) if numpy.abs(dirty.data).max() < 1.1 * thresh: log.info("ical: Reached stopping threshold %.6f Jy" % thresh) break log.info("ical: End of major cycle") log.info("ical: End of major cycles") restored = restore_cube(model, psf, dirty, **kwargs) return model, dirty, restored