Beispiel #1
0
 def core_solve(self,
                spf,
                dpf,
                phase_error=0.1,
                amplitude_error=0.0,
                leakage=0.0,
                phase_only=True,
                niter=200,
                crosspol=False,
                residual_tol=1e-6,
                f=[100.0, 50.0, -10.0, 40.0]):
     self.actualSetup(spf, dpf, f=f)
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt,
                             phase_error=phase_error,
                             amplitude_error=amplitude_error,
                             leakage=leakage)
     original = copy_visibility(self.vis)
     vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis,
                             original,
                             phase_only=phase_only,
                             niter=niter,
                             crosspol=crosspol,
                             tol=1e-6)
     vis = apply_gaintable(vis, gtsol, inverse=True)
     residual = numpy.max(gtsol.residual)
     assert residual < residual_tol, "%s %s Max residual = %s" % (spf, dpf,
                                                                  residual)
     log.debug(qa_gaintable(gt))
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
def qa_gaintable_bag(gt, context=''):
    """ Print qa on gaintables, use this in a sequence of bag operations

    Can be used in bag.map() as a passthru
    
    :param gt:
    :return:
    """
    s = qa_gaintable(gt, context=context)
    log.info(s)
    return gt
Beispiel #3
0
def calibrate_function(vis, model_vis, context='T', controls=None, iteration=0, **kwargs):
    """ Calibrate using algorithm specified by context
    
    The context string can denote a sequence of calibrations e.g. TGB with different timescales.

    :param vis:
    :param model_vis:
    :param context: calibration contexts in order of correction e.g. 'TGB'
    :param control: controls dictionary, modified as necessary
    :param iteration: Iteration number to be compared to the 'first_selfcal' field.
    :param kwargs:
    :return: Calibrated data, dict(gaintables)
    """
    gaintables = {}
    
    if controls is None:
        controls = create_calibration_controls(**kwargs)
    
    isVis = isinstance(vis, Visibility)
    if isVis:
        avis = convert_visibility_to_blockvisibility(vis)
    else:
        avis = vis
    
    isMVis = isinstance(model_vis, Visibility)
    if isMVis:
        amvis = convert_visibility_to_blockvisibility(model_vis)
    else:
        amvis = model_vis
    
    for c in context:
        if iteration >= controls[c]['first_selfcal']:
            gaintables[c] = \
                create_gaintable_from_blockvisibility(avis,
                                                      timeslice=controls[c]['timeslice'])
            gaintables[c] = solve_gaintable(avis, amvis,
                                            timeslice=controls[c]['timeslice'],
                                            phase_only=controls[c]['phase_only'],
                                            crosspol=controls[c]['shape'] == 'matrix')
            log.debug('calibrate_function: Jones matrix %s, iteration %d' % (c, iteration))
            log.debug(qa_gaintable(gaintables[c], context='Jones matrix %s, iteration %d' % (c, iteration)))
            avis = apply_gaintable(avis, gaintables[c],
                                   inverse=True,
                                   timeslice=controls[c]['timeslice'])
        else:
            log.debug('calibrate_function: Jones matrix %s not solved, iteration %d' % (c, iteration))
    if isVis:
        return convert_blockvisibility_to_visibility(avis), gaintables
    else:
        return avis, gaintables
def sagecal_solve(vis,
                  components,
                  niter=10,
                  tol=1e-8,
                  gain=0.25,
                  callback=None,
                  **kwargs):
    """ Solve
    
    Solve by iterating, performing E step and M step.
    
    :param vis:
    :param components:
    :param gaintables:
    :param kwargs:
    :return: The individual data models and the residual visibility
    """
    thetas = create_sagecal_thetas(vis, components, **kwargs)

    for iter in range(niter):
        new_thetas = list()
        evis_all = sagecal_e_all(vis, thetas, **kwargs)
        print("Iteration %d" % (iter))
        for window_index, theta in enumerate(thetas):
            evis = sagecal_e_step(vis, evis_all, theta, gain=gain, **kwargs)
            new_theta = sagecal_m_step(evis, theta, gain=gain, **kwargs)
            new_thetas.append(new_theta)

            if callback is not None:
                callback(iter, thetas)

            flux = new_theta[0].flux[0, 0]
            qa = qa_gaintable(new_theta[1])
            residual = qa.data['residual']
            rms_phase = qa.data['rms-phase']
            print("\t Window %d, flux %s, residual %.3f, rms phase %.3f" %
                  (window_index, str(flux), residual, rms_phase))
        thetas = new_thetas

    residual_vis = copy_visibility(vis)
    final_vis = sagecal_e_all(vis, thetas, **kwargs)
    residual_vis.data['vis'][
        ...] = vis.data['vis'][...] - final_vis.data['vis'][...]
    return thetas, residual_vis
def skymodel_cal_solve(vis, skymodels, niter=10, tol=1e-8, gain=0.25, **kwargs):
    """ Solve
    
    Solve by iterating, performing E step and M step.
    
    :param vis: Initial visibility
    :param components: Initial components to be used
    :param gaintables: Initial gain tables to be used
    :param kwargs:
    :return: The individual data models and the residual visibility
    """
    calskymodels = initialise_skymodel_cal_skymodel(vis, skymodels=skymodels, **kwargs)
    
    for iter in range(niter):
        new_calskymodels = list()
        evis_all = skymodel_cal_e_all(vis, calskymodels)
        log.debug("skymodel_cal_solve: Iteration %d" % (iter))
        for window_index, csm in enumerate(calskymodels):
            evis = skymodel_cal_e_step(vis, evis_all, csm, gain=gain, **kwargs)
            new_csm = skymodel_cal_m_step(evis, csm, **kwargs)
            new_calskymodels.append((new_csm[0], new_csm[1]))
            
            flux = new_csm[0].components[0].flux[0, 0]
            qa = qa_gaintable(new_csm[1])
            residual = qa.data['residual']
            rms_phase = qa.data['rms-phase']
            log.debug("skymodel_cal_solve:\t Window %d, flux %s, residual %.3f, rms phase %.3f" % (window_index,
                                                                            str(flux), residual,
                                                                            rms_phase))
            
        calskymodels = [(copy_skymodel(csm[0]), copy_gaintable(csm[1])) for csm in new_calskymodels]
    
    residual_vis = copy_visibility(vis)
    final_vis = skymodel_cal_e_all(vis, calskymodels)
    residual_vis.data['vis'][...] = vis.data['vis'][...] - final_vis.data['vis'][...]
    return calskymodels, residual_vis
Beispiel #6
0
 def test_RCAL(self):
     self.setupVis(add_errors=True, block=True, freqwin=5)
     for igt, gt in enumerate(rcal(vis=self.vis, components=self.comps)):
         log.info("Chunk %d: %s" % (igt, qa_gaintable(gt)))