Esempio n. 1
0
def solve_image(vis: Visibility,
                model: Image,
                components=None,
                predict=predict_2d,
                invert=invert_2d,
                **kwargs) -> (Visibility, Image, Image):
    """Solve for image using deconvolve_cube and specified predict, invert

    This is the same as a majorcycle/minorcycle algorithm. The components are removed prior to deconvolution.
    
    See also arguments for predict, invert, deconvolve_cube functions.2d

    :param vis:
    :param model: Model image
    :param predict: Predict function e.g. predict_2d, predict_wstack
    :param invert: Invert function e.g. invert_2d, invert_wstack
    :return: Visibility, model
    """
    nmajor = get_parameter(kwargs, 'nmajor', 5)
    log.info("solve_image: Performing %d major cycles" % nmajor)

    # The model is added to each major cycle and then the visibilities are
    # calculated from the full model
    vispred = copy_visibility(vis)
    visres = copy_visibility(vis)

    vispred = predict(vispred, model, **kwargs)

    if components is not None:
        vispred = predict_skycomponent_visibility(vispred, components)

    visres.data['vis'] = vis.data['vis'] - vispred.data['vis']
    dirty, sumwt = invert(visres, model, **kwargs)
    psf, sumwt = invert(visres, model, dopsf=True, **kwargs)

    thresh = get_parameter(kwargs, "threshold", 0.0)

    for i in range(nmajor):
        log.info("solve_image: Start of major cycle %d" % i)
        cc, res = deconvolve_cube(dirty, psf, **kwargs)
        res = None
        model.data += cc.data
        vispred = predict(vispred, model, **kwargs)
        visres.data['vis'] = vis.data['vis'] - vispred.data['vis']
        dirty, sumwt = invert(visres, model, **kwargs)
        if numpy.abs(dirty.data).max() < 1.1 * thresh:
            log.info("Reached stopping threshold %.6f Jy" % thresh)
            break
        log.info("solve_image: End of major cycle")

    log.info("solve_image: End of major cycles")
    return visres, model, dirty
Esempio n. 2
0
 def test_predict_sky_components_coalesce(self):
     sc = create_low_test_skycomponents_from_gleam(
         flux_limit=10.0,
         polarisation_frame=PolarisationFrame("stokesI"),
         frequency=self.frequency,
         kind='cubic',
         phasecentre=SkyCoord("17h20m31s", "-00d58m45s"),
         radius=0.1)
     self.config = create_named_configuration('LOWBD2-CORE')
     self.phasecentre = SkyCoord("17h20m31s", "-00d58m45s")
     sampling_time = 3.76
     self.times = numpy.arange(0.0, +300 * sampling_time, sampling_time)
     self.vis = create_blockvisibility(
         self.config,
         self.times,
         self.frequency,
         phasecentre=self.phasecentre,
         weight=1.0,
         polarisation_frame=PolarisationFrame('stokesI'),
         channel_bandwidth=self.channel_bandwidth)
     self.vis = predict_skycomponent_visibility(self.vis, sc)
     cvt = coalesce_visibility(self.vis, time_coal=1.0)
     assert cvt.cindex is not None
	newwcs = image_graph.wcs.deepcopy()
	newwcs.wcs.cdelt[0] = -0.001 * 180 / np.pi
	newwcs.wcs.cdelt[1] = 0.001 * 180 / np.pi
	newshape = np.array(image_graph.data.shape)
	newshape[2] /= 2
	newshape[3] /= 2
	newphasecentre = SkyCoord(ra=+15.0 * u.deg, dec=-10.0 * u.deg, frame='icrs', equinox='J2000')
	image = copy.deepcopy(image_graph)
	new_vis = copy.deepcopy(vis)
	insert_skycomponent(image, comp)
	# TODO FACET为1时,并行和串行的结果相同,但FACET不为1时结果不同,为了便于后面的验证,因此不再进行这两步操作
	# image = reproject_image(image, newwcs, newshape)[0]
	# image.data = fft(image.data)
	predict_facets(new_vis, image)
	new_vis = phaserotate_visibility(new_vis, newphasecentre, tangent=False)
	predict_skycomponent_visibility(new_vis, comp)




	# === Extract Lsm ===
	extract_lsm = extract_lsm_handle()
	broadcast_lsm = sc.broadcast(extract_lsm.collect())
	# === Local Sky Model ===
	local_sky_model = local_sky_model_handle()
	# === Telescope Management ===
	telescope_management = telescope_management_handle()
	# # === Visibility Buffer ===
	visibility_buffer = visibility_buffer_handle(vis)
	visibility_buffer.cache()
	broads_input1 = sc.broadcast(visibility_buffer.collect())
Esempio n. 4
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()
Esempio n. 5
0
def serialize_program():
    result = []
    lowcore = create_named_configuration('LOWBD2-CORE')
    times = metadata.create_time()  # time = 5
    frequency = metadata.create_frequency()  # nchan = 5
    channel_bandwidth = metadata.create_channel_bandwidth()  # nchan = 5
    #---predict_module---#
    phasecentre = metadata.create_phasecentre()
    comp = metadata.create_skycomponent()

    image = create_image(
        metadata.NY,
        metadata.NX,
        frequency=frequency,
        phasecentre=phasecentre,
        cellsize=0.001,
        polarisation_frame=metadata.create_polarisation_frame(),
    )
    insert_skycomponent(image, comp, insert_method="Sinc")
    blockvis = create_blockvisibility(
        lowcore,
        times=times,
        frequency=frequency,
        channel_bandwidth=channel_bandwidth,
        phasecentre=phasecentre,
        weight=1,
        polarisation_frame=metadata.create_polarisation_frame(),
        integration_time=1.0,
        NAN=metadata.NAN)
    visibility = coalesce_visibility(blockvis)
    visibility = predict_facets(visibility, image, facets=metadata.FACETS)
    # 不确定这一步是否需要还要再一步phaserotate,因为predict_facet已经在最后调用过一次phaserotate
    newphasecentre = SkyCoord(ra=+10.0 * u.deg,
                              dec=-30.0 * u.deg,
                              frame='icrs',
                              equinox='J2000')
    model_vis = phaserotate_visibility(visibility, newphasecentre)
    predict_skycomponent_visibility(model_vis, comp)
    result.append(model_vis)
    model_vis = decoalesce_visibility(model_vis)
    #---solve_module---#
    # 模拟望远镜实际接收到的visibility
    blockvis_observed = create_blockvisibility(
        lowcore,
        times=times,
        frequency=frequency,
        channel_bandwidth=channel_bandwidth,
        phasecentre=phasecentre,
        weight=1,
        polarisation_frame=metadata.create_polarisation_frame(),
        integration_time=1.0,
        NAN=metadata.NAN)
    # 用整数填充vis, 模拟实际接收到的block_visibility
    vis_observed = coalesce_visibility(blockvis_observed)
    vis_observed.data['vis'].flat = range(vis_observed.nvis * 4)
    blockvis_observed = decoalesce_visibility(vis_observed)

    gaintable = solve_gaintable(blockvis_observed, model_vis)
    apply_gaintable(blockvis_observed, gaintable)
    blockvis_observed.data[
        'vis'] = blockvis_observed.data['vis'] - model_vis.data['vis']
    visibility = coalesce_visibility(blockvis_observed,
                                     time_coal=metadata.time_coal,
                                     frequency_coal=metadata.frequency_coal)
    result.append(visibility)
    #---backwards_module---#
    image = create_image(metadata.NY,
                         metadata.NX,
                         frequency=frequency,
                         phasecentre=phasecentre,
                         cellsize=0.001,
                         polarisation_frame=metadata.create_polarisation_frame(
                         ))  # 空的image,接受visibility的invert
    image, wt = invert_facets(visibility, image, facets=metadata.FACETS)

    psf_image = create_image(
        metadata.NY,
        metadata.NX,
        frequency=frequency,
        phasecentre=phasecentre,
        cellsize=0.001,
        polarisation_frame=metadata.create_polarisation_frame())
    psf_image, psf_wt = invert_facets(visibility,
                                      psf_image,
                                      dopsf=True,
                                      facets=metadata.FACETS)
    dirty_taylor, psf_taylor = deconvolve_cube_sumfacet(
        image, psf_image, moments=metadata.MOMENTS)
    result.append((dirty_taylor, psf_taylor))

    #---deconvolution_module---#
    comp_image, residual_image = deconvolve_cube_identify(image,
                                                          dirty_taylor,
                                                          psf_taylor,
                                                          niter=metadata.niter)
    result.append((comp_image, residual_image))

    return result
def serialize_program():
    result = []
    lowcore = create_named_configuration('LOWBD2-CORE')
    times = numpy.linspace(-3, +3, 5) * (numpy.pi / 12.0)  # time = 5
    frequency = numpy.array([1e8, 1.1e8, 1.2e8, 1.3e8, 1.4e8])  # nchan = 5
    channel_bandwidth = numpy.array([1e7, 1e7, 1e7, 1e7, 1e7])  # nchan = 5

    f = numpy.array([100.0, 110.0, 120.0, 130.0])  # npol = 4
    flux = numpy.array([f, f + 100.0, f + 200.0, f + 300.0,
                        f + 400.0])  # nchan,npol = 2, 4

    phasecentre = SkyCoord(ra=+15.0 * u.deg,
                           dec=-35.0 * u.deg,
                           frame='icrs',
                           equinox='J2000')
    compabsdirection = SkyCoord(ra=17.0 * u.deg,
                                dec=-36.5 * u.deg,
                                frame='icrs',
                                equinox='J2000')
    comp = create_skycomponent(flux=flux,
                               frequency=frequency,
                               direction=compabsdirection,
                               polarisation_frame=PolarisationFrame('linear'))
    image = create_image(
        NY,
        NX,
        frequency=frequency,
        phasecentre=phasecentre,
        cellsize=0.001,
        polarisation_frame=PolarisationFrame('linear'),
    )
    insert_skycomponent(image, comp, insert_method="Sinc")
    blockvis = create_blockvisibility(
        lowcore,
        times=times,
        frequency=frequency,
        channel_bandwidth=channel_bandwidth,
        phasecentre=phasecentre,
        weight=1,
        polarisation_frame=PolarisationFrame('linear'),
        integration_time=1.0)
    visibility = coalesce_visibility(blockvis)
    visibility = predict_facets(visibility, image, facets=FACETS)
    # 不确定这一步是否需要还要再一步phaserotate,因为predict_facet已经在最后调用过一次phaserotate
    newphasecentre = SkyCoord(ra=+10.0 * u.deg,
                              dec=-30.0 * u.deg,
                              frame='icrs',
                              equinox='J2000')
    model_vis = phaserotate_visibility(visibility, newphasecentre)
    predict_skycomponent_visibility(model_vis, comp)
    result.append(model_vis)
    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_observed = coalesce_visibility(blockvis_observed)
    vis_observed.data['vis'].flat = range(vis_observed.nvis * 4)
    blockvis_observed = decoalesce_visibility(vis_observed)

    gaintable = solve_gaintable(blockvis_observed, model_vis)
    apply_gaintable(blockvis_observed, gaintable)
    blockvis_observed.data[
        'vis'] = blockvis_observed.data['vis'] - model_vis.data['vis']
    visibility = coalesce_visibility(
        blockvis_observed)  # 空的image,接受visibility的invert
    result.append(visibility)

    image = create_image(NY,
                         NX,
                         frequency=frequency,
                         phasecentre=phasecentre,
                         cellsize=0.001,
                         polarisation_frame=PolarisationFrame('linear'))
    image, wt = invert_facets(visibility, image)

    new_image = Image()
    new_image.wcs = image.wcs
    new_image.polarisation_frame = image.polarisation_frame
    new_image.data = np.sum(image.data, axis=0).reshape((1, NPOL, NY, NX)) * 4
    result.append(new_image)

    return result