Example #1
0
def calibrate_visibility(vt: Visibility,
                         model: Image = None,
                         components=None,
                         predict=predict_2d,
                         **kwargs) -> Visibility:
    """ calibrate Visibility with respect to model and optionally components

    :param vt: Visibility
    :param model: Model image
    :param components: Sky components
    :return: Calibrated visibility
    """
    assert model is not None or components is not None, "calibration requires a model or skycomponents"

    vtpred = copy_visibility(vt, zero=True)

    if model is not None:
        vtpred = predict(vtpred, model, **kwargs)
        if components is not None:
            vtpred = predict_skycomponent_visibility(vtpred, components)
    else:
        vtpred = predict_skycomponent_visibility(vtpred, components)

    bvt = decoalesce_visibility(vt)
    bvtpred = decoalesce_visibility(vtpred)
    gt = solve_gaintable(bvt, bvtpred, **kwargs)
    bvt = apply_gaintable(bvt,
                          gt,
                          inverse=get_parameter(kwargs, "inverse", False))
    return convert_blockvisibility_to_visibility(bvt)
Example #2
0
 def test_convert_decoalesce_zero(self):
     cvis = convert_blockvisibility_to_visibility(self.blockvis)
     assert numpy.min(cvis.frequency) == numpy.min(self.frequency)
     assert numpy.min(cvis.frequency) > 0.0
     dvis = decoalesce_visibility(cvis)
     assert dvis.nvis == self.blockvis.nvis
     dvis = decoalesce_visibility(cvis, overwrite=True)
     assert dvis.nvis == self.blockvis.nvis
 def test_coalesce_decoalesce_time(self):
     cvis = coalesce_visibility(self.blockvis, time_coal=1.0, frequency_coal=0.0, max_frequency_coal=1)
     assert numpy.min(cvis.frequency) == numpy.min(self.frequency)
     assert numpy.min(cvis.frequency) > 0.0
     dvis = decoalesce_visibility(cvis)
     assert dvis.nvis == self.blockvis.nvis
     dvis = decoalesce_visibility(cvis, overwrite=True)
     assert dvis.nvis == self.blockvis.nvis
Example #4
0
def calibrate_blockvisibility(bvt: BlockVisibility,
                              model=None,
                              components=None,
                              predict=predict_2d,
                              **kwargs):
    """ calibrate BlockVisibility with respect to model and optionally components

    :param bvt: BlockVisibility
    :param model: Model image
    :param components: Sky components
    :returns: Calibrated BlockVisibility

    """
    assert model is not None or components is not None, "calibration requires a model or skycomponents"

    if model is not None:
        vtpred = convert_blockvisibility_to_visibility(bvt)
        vtpred = predict(vtpred, model, **kwargs)
        bvtpred = decoalesce_visibility(vtpred)
        if components is not None:
            bvtpred = predict_skycomponent_blockvisibility(bvtpred, components)
    else:
        bvtpred = copy_visibility(bvt, zero=True)
        bvtpred = predict_skycomponent_blockvisibility(bvtpred, components)

    gt = solve_gaintable(bvt, bvtpred, **kwargs)
    return apply_gaintable(bvt, gt, **kwargs)
Example #5
0
 def test_coalesce_decoalesce_with_iter(self):
     for rows in vis_timeslice_iter(self.blockvis):
         visslice = create_visibility_from_rows(self.blockvis, rows)
         cvisslice = convert_blockvisibility_to_visibility(visslice)
         assert numpy.min(cvisslice.frequency) == numpy.min(self.frequency)
         assert numpy.min(cvisslice.frequency) > 0.0
         dvisslice = decoalesce_visibility(cvisslice)
         assert dvisslice.nvis == visslice.nvis
 def test_coalesce_decoalesce_singletime(self):
     self.times = numpy.array([0.0])
     self.blockvis = create_blockvisibility(self.lowcore, self.times, self.frequency, phasecentre=self.phasecentre,
                                            weight=1.0, polarisation_frame=PolarisationFrame('stokesI'),
                                            channel_bandwidth=self.channel_bandwidth)
     # Fill in the vis values so each can be uniquely identified
     self.blockvis.data['vis'] = range(self.blockvis.nvis)
     cvis = coalesce_visibility(self.blockvis, time_coal=1.0)
     assert numpy.min(cvis.frequency) == numpy.min(self.frequency)
     assert numpy.min(cvis.frequency) > 0.0
     dvis = decoalesce_visibility(cvis)
     assert dvis.nvis == self.blockvis.nvis
Example #7
0
def visibility_gather_w(visibility_list, vis, **kwargs):
    if type(vis) == BlockVisibility:
        cvis = coalesce_visibility(vis, **kwargs)
        return decoalesce_visibility(
            visibility_gather(visibility_list,
                              cvis,
                              vis_iter=vis_wstack_iter,
                              **kwargs))
    else:
        return visibility_gather(visibility_list,
                                 vis,
                                 vis_iter=vis_wstack_iter,
                                 **kwargs)
    def gather_vis(results, vis):
        # Gather across the visibility iteration axis
        assert vis is not None
        if isinstance(vis, BlockVisibility):
            avis = coalesce_visibility(vis, **kwargs)
        else:
            avis = vis
        for i, rows in enumerate(
                vis_iter(avis, vis_slices=vis_slices, **kwargs)):
            assert i < len(results), "Insufficient results for the gather"
            if rows is not None and results[i] is not None:
                avis.data['vis'][rows] = results[i].data['vis']

        if isinstance(vis, BlockVisibility):
            return decoalesce_visibility(avis, **kwargs)
        else:
            return avis
def predict_wstack_single(vis, model, remove=True, **kwargs) -> Visibility:
    """ Predict using a single w slices.
    
    This processes a single w plane, rotating out the w beam for the average w

    :param vis: Visibility to be predicted
    :param model: model image
    :return: resulting visibility (in place works)
    """

    if not isinstance(vis, Visibility):
        log.debug("predict_wstack_single: Coalescing")
        avis = coalesce_visibility(vis, **kwargs)
    else:
        avis = vis

    log.debug("predict_wstack_single: predicting using single w slice")

    avis.data['vis'] *= 0.0
    # We might want to do wprojection so we remove the average w
    w_average = numpy.average(avis.w)
    avis.data['uvw'][..., 2] -= w_average
    tempvis = copy_visibility(avis)

    # Calculate w beam and apply to the model. The imaginary part is not needed
    workimage = copy_image(model)
    w_beam = create_w_term_like(model, w_average, vis.phasecentre)

    # Do the real part
    workimage.data = w_beam.data.real * model.data
    avis = predict_2d_base(avis, workimage, **kwargs)

    # and now the imaginary part
    workimage.data = w_beam.data.imag * model.data
    tempvis = predict_2d_base(tempvis, workimage, **kwargs)
    avis.data['vis'] -= 1j * tempvis.data['vis']

    if not remove:
        avis.data['uvw'][..., 2] += w_average

    if isinstance(vis, BlockVisibility) and isinstance(avis, Visibility):
        log.debug("imaging.predict decoalescing post prediction")
        return decoalesce_visibility(avis)
    else:
        return avis
def predict_2d_base(vis: Union[BlockVisibility, Visibility], model: Image,
                    **kwargs) -> Union[BlockVisibility, Visibility]:
    """ Predict using convolutional degridding.

    This is at the bottom of the layering i.e. all transforms are eventually expressed in terms of
    this function. Any shifting needed is performed here.

    :param vis: Visibility to be predicted
    :param model: model image
    :return: resulting visibility (in place works)
    """
    if isinstance(vis, BlockVisibility):
        log.debug("imaging.predict: coalescing prior to prediction")
        avis = coalesce_visibility(vis, **kwargs)
    else:
        avis = vis

    assert isinstance(avis, Visibility), avis

    _, _, ny, nx = model.data.shape

    padding = {}
    if get_parameter(kwargs, "padding", False):
        padding = {'padding': get_parameter(kwargs, "padding", False)}
    spectral_mode, vfrequencymap = get_frequency_map(avis, model)
    polarisation_mode, vpolarisationmap = get_polarisation_map(avis, model)
    uvw_mode, shape, padding, vuvwmap = get_uvw_map(avis, model, **padding)
    kernel_name, gcf, vkernellist = get_kernel_list(avis, model, **kwargs)

    uvgrid = fft((pad_mid(model.data, int(round(padding * nx))) *
                  gcf).astype(dtype=complex))

    avis.data['vis'] = convolutional_degrid(vkernellist,
                                            avis.data['vis'].shape, uvgrid,
                                            vuvwmap, vfrequencymap,
                                            vpolarisationmap)

    # Now we can shift the visibility from the image frame to the original visibility frame
    svis = shift_vis_to_image(avis, model, tangent=True, inverse=True)

    if isinstance(vis, BlockVisibility) and isinstance(svis, Visibility):
        log.debug("imaging.predict decoalescing post prediction")
        return decoalesce_visibility(svis)
    else:
        return svis
Example #11
0
def predict_2d_base(vis: Visibility, model: Image, **kwargs) -> Visibility:
    """ Predict using convolutional degridding.

    This is at the bottom of the layering i.e. all transforms are eventually expressed in terms of
    this function. Any shifting needed is performed here.

    :param vis: Visibility to be predicted
    :param model: model image
    :return: resulting visibility (in place works)
    """
    if type(vis) is not Visibility:
        avis = coalesce_visibility(vis, **kwargs)
    else:
        avis = vis
    _, _, ny, nx = model.data.shape
    # print(model.shape)
    spectral_mode, vfrequencymap = get_frequency_map(avis, model)  # 可以并行
    polarisation_mode, vpolarisationmap = get_polarisation_map(
        avis, model, **kwargs)  # 可以并行
    uvw_mode, shape, padding, vuvwmap = get_uvw_map(avis, model,
                                                    **kwargs)  # 可以并行
    kernel_name, gcf, vkernellist = get_kernel_list(avis, model, **kwargs)
    uvgrid = fft((pad_mid(model.data, int(round(padding * nx))) *
                  gcf).astype(dtype=complex))
    avis.data['vis'] = convolutional_degrid(vkernellist,
                                            avis.data['vis'].shape, uvgrid,
                                            vuvwmap, vfrequencymap,
                                            vpolarisationmap)

    # Now we can shift the visibility from the image frame to the original visibility frame
    svis = shift_vis_to_image(avis, model, tangent=True, inverse=True)

    if type(vis) is not Visibility:
        return decoalesce_visibility(svis)
    else:
        return svis
Example #12
0
def predict_2d_base_timing(vis: Visibility, model: Image,
                           **kwargs) -> (Visibility, tuple):
    """ Predict using convolutional degridding.

    This is at the bottom of the layering i.e. all transforms are eventually expressed in terms of
    this function. Any shifting needed is performed here.

    :param vis: Visibility to be predicted
    :param model: model image
    :return: resulting visibility (in place works)
    """
    if not isinstance(vis, Visibility):
        avis = coalesce_visibility(vis, **kwargs)
    else:
        avis = vis

    _, _, ny, nx = model.data.shape

    opt = get_parameter(kwargs, 'opt', False)
    if not opt:
        log.debug('Using original algorithm')
    else:
        log.debug('Using optimized algorithm')

    padding = {}
    if get_parameter(kwargs, "padding", False):
        padding = {'padding': get_parameter(kwargs, "padding", False)}
    spectral_mode, vfrequencymap = get_frequency_map(avis, model, opt)
    polarisation_mode, vpolarisationmap = get_polarisation_map(avis, model)
    uvw_mode, shape, padding, vuvwmap = get_uvw_map(avis, model, **padding)
    kernel_name, gcf, vkernellist = get_kernel_list(avis, model, **kwargs)
    inarr = (pad_mid(model.data, int(round(padding * nx))) *
             gcf).astype(dtype=complex)

    # Use original algorithm
    if not opt:
        time_fft = -time.time()
        uvgrid = fft(inarr)
        time_fft += time.time()

        time_degrid = -time.time()
        vt = convolutional_degrid(vkernellist, avis.data['vis'].shape, uvgrid,
                                  vuvwmap, vfrequencymap, vpolarisationmap)
        time_degrid += time.time()

    # Use optimized algorithm
    else:
        time_fft = -time.time()
        uvgrid = numpy.zeros(inarr.shape, dtype=inarr.dtype)
        fft_c(uvgrid, inarr)
        time_fft += time.time()

        time_degrid = -time.time()
        kernel_indices, kernels = vkernellist
        ks0, ks1, ks2, ks3 = kernels[0].shape
        kernels_c = numpy.zeros((len(kernels), ks0, ks1, ks2, ks3),
                                dtype=kernels[0].dtype)
        for i in range(len(kernels)):
            kernels_c[i, ...] = kernels[i]

        vfrequencymap_c = numpy.array(vfrequencymap, dtype=numpy.int32)
        vt = numpy.zeros(avis.data['vis'].shape, dtype=numpy.complex128)
        convolutional_degrid_c(vt, native_order(kernels_c),
                               native_order(kernel_indices),
                               native_order(uvgrid), native_order(vuvwmap),
                               native_order(vfrequencymap_c))
        time_degrid += time.time()

    avis.data['vis'] = vt

    # Now we can shift the visibility from the image frame to the original visibility frame
    svis = shift_vis_to_image(avis, model, tangent=True, inverse=True)

    if not isinstance(vis, Visibility):
        svis = decoalesce_visibility(svis)

    return svis, (time_degrid, time_fft)
Example #13
0
def visibility_gather_w(visibility_list: List[Visibility], vis: Visibility, **kwargs) -> Visibility:
    if isinstance(vis, BlockVisibility):
        cvis = coalesce_visibility(vis, **kwargs)
        return decoalesce_visibility(visibility_gather(visibility_list, cvis, vis_iter=vis_wstack_iter, **kwargs))
    else:
        return visibility_gather(visibility_list, vis, vis_iter=vis_wstack_iter, **kwargs)
Example #14
0
    def test_solve_gaintable(self):
        '''
            测试solve_gaintable
        :return:
        '''
        #===串行===
        newphasecentre = SkyCoord(ra=+15.0 * u.deg,
                                  dec=-10.0 * u.deg,
                                  frame='icrs',
                                  equinox='J2000')
        image = copy.deepcopy(image_graph)
        image.data = np.zeros_like(image.data)
        #将lsm插入到image中
        insert_skycomponent(image, comp, insert_method="Sinc")
        # 期间可能会有fft和reproject暂不考虑

        #生成telescope_data
        telescope_data = copy.deepcopy(vis)

        #image做卷积并且degrid生成modelvis
        model_vis = predict_facets(telescope_data, image)
        model_vis = predict_skycomponent_visibility(model_vis, comp)
        model_vis = decoalesce_visibility(model_vis)

        #模拟望远镜实际接收到的visibility
        blockvis_observed = create_blockvisibility(
            lowcore,
            times=times,
            frequency=frequency,
            channel_bandwidth=channel_bandwidth,
            phasecentre=phasecentre,
            weight=1,
            polarisation_frame=PolarisationFrame('linear'),
            integration_time=1.0)
        # 用自然数值填充vis,模拟实际接收到的block_visibility,并且各个vis的值各不相同易于区分
        blockvis_observed.data['vis'] = range(blockvis_observed.nvis)

        gt = solve_gaintable(blockvis_observed, model_vis)
        apply_gaintable(blockvis_observed, gt)

        #===并行===
        # TODO暂未完成
        # 生成telescope_data
        telescope_data, visibility_share = visibility_to_visibility_para(
            vis, 'npol')
        ims, image_share = image_to_image_para(image_graph, FACETS)
        ims2 = []
        for i in ims:
            insert_skycomponent_para(i, comp, insert_method='Sinc')
            ims2.append(i)

        model_vis1 = []
        viss2 = []
        for v in viss:
            for im in ims2:
                if v[0][0] == im.frequency:
                    temp = predict_facets_para(v[1], im)
                    # (chan, time, ant1,  ant2)
                    model_vis1.append(
                        ((v[0][0], v[0][1], v[0][2], v[0][3]), temp))
                    viss2.append(((v[0][0], v[0][1], v[0][2], v[0][3],
                                   im.facet, im.polarisation),
                                  phaserotate_visibility_para(temp,
                                                              newphasecentre,
                                                              tangent=False)))
        predict_skycoponent_visibility_para_modified(viss2, comp, mode='test2')
        # 将visibility的facet和polarisation合并起来
        viss3 = defaultdict(list)
        model_vis2 = defaultdict(list)
        for v in range(0, len(viss2), 4 * 4):
            temp = copy.deepcopy(viss2[v][1])
            temp2 = copy.deepcopy(model_vis1[v][1])
            for id in range(v + 1, v + 16):
                temp.data['vis'] += viss2[id][1].data['vis']
                temp2.data['vis'] += model_vis1[id][1].data['vis']
            viss3[viss2[v][0][0:2]].append(((viss2[v][0][2:4]), temp))
            model_vis2[model_vis1[v][0][0:2]].append(
                ((model_vis1[v][0][2:]), temp2))

        # TODO 将并行程序从此开始填入
        gts = []
        for key in viss3:
            xs = []
            xwts = []
            for v, mv in zip(viss3[key], model_vis2[key]):
                x, xwt = solve_gaintable_para(v[1], mv[1])
                xs.append(((0, 0, 0, 0, key[0], key[1], v[0][0], v[0][1]), x))
                xwts.append(xwt)

            g = create_gaintable_from_visibility_para(viss3[key][0][1], 3)
            solve_from_X_para(xs, xwts, g, npol=viss3[key][0][1].npol)
            gts.append((key, g))

        gaintable_right(gt, gts)

        new_gain = combine_gaintable(gt, gts)

        for key in viss3:
            print(key)
            chan, time = key
            temp = gaintable_for_para(new_gain.gain[time, :,
                                                    chan, :, :].reshape[1, 3,
                                                                        2, 2])
            apply_gaintable_para(viss3[key], temp)

        for key in viss3:
            chan, time = key
            vis_serial = block_vis.vis[time, :, :chan, :]
            for id, v in viss3[key]:
                ant1 = id[2]
                ant2 = id[3]
                vis_serial = block_vis.vis[time, ant2, ant1, chan]
                print(vis_serial)
                print(v.vis)
                print()