Пример #1
0
    def ft_ift_sm(ov, sm, g):
        assert isinstance(ov, Visibility), ov
        assert isinstance(sm, SkyModel), sm
        if g is not None:
            assert len(g) == 2, g
            assert isinstance(g[0], Image), g[0]
            assert isinstance(g[1], ConvolutionFunction), g[1]

        v = copy_visibility(ov)

        v.data['vis'][...] = 0.0 + 0.0j

        if len(sm.components) > 0:

            if isinstance(sm.mask, Image):
                comps = copy_skycomponent(sm.components)
                comps = apply_beam_to_skycomponent(comps, sm.mask)
                v = predict_skycomponent_visibility(v, comps)
            else:
                v = predict_skycomponent_visibility(v, sm.components)

        if isinstance(sm.image, Image):
            if numpy.max(numpy.abs(sm.image.data)) > 0.0:
                if isinstance(sm.mask, Image):
                    model = copy_image(sm.image)
                    model.data *= sm.mask.data
                else:
                    model = sm.image
                v = predict_list_serial_workflow([v], [model],
                                                 context=context,
                                                 vis_slices=vis_slices,
                                                 facets=facets,
                                                 gcfcf=[g],
                                                 **kwargs)[0]

        assert isinstance(sm.image, Image), sm.image

        result = invert_list_serial_workflow([v], [sm.image],
                                             context=context,
                                             vis_slices=vis_slices,
                                             facets=facets,
                                             gcfcf=gcfcf,
                                             **kwargs)[0]
        if isinstance(sm.mask, Image):
            result[0].data *= sm.mask.data
        return result
def predict_skymodel_list_serial_workflow(vis_list,
                                          skymodel_list,
                                          context,
                                          vis_slices=1,
                                          facets=1,
                                          gcfcf=None,
                                          **kwargs):
    """Predict from a skymodel, iterating over both the vis_list and skymodel
    
    The visibility and image are scattered, the visibility is predicted on each part, and then the
    parts are assembled.

    :param vis_list:
    :param skymodel_list: skymodel list
    :param vis_slices: Number of vis slices (w stack or timeslice)
    :param facets: Number of facets (per axis)
    :param context: Type of processing e.g. 2d, wstack, timeslice or facets
    :param gcfcg: tuple containing grid correction and convolution function
    :param kwargs: Parameters for functions in components
    :return: List of vis_lists
   """
    def extract_comps(sm):
        return sm.components

    def extract_image(sm):
        return sm.images[0]

    comp = [extract_comps(sm) for sm in skymodel_list]
    images = [extract_image(sm) for sm in skymodel_list]

    dft_vis_list = zero_list_serial_workflow(vis_list)
    dft_vis_list = [
        predict_skycomponent_visibility(dft_vis_list[i], comp[i])
        for i, _ in enumerate(dft_vis_list)
    ]

    fft_vis_list = zero_list_serial_workflow(vis_list)
    fft_vis_list = predict_list_serial_workflow(fft_vis_list,
                                                images,
                                                context=context,
                                                vis_slices=vis_slices,
                                                facets=facets,
                                                gcfcf=gcfcf,
                                                **kwargs)

    def vis_add(v1, v2):
        vout = copy_visibility(v1)
        vout.data['vis'] += v2.data['vis']
        return vout

    return [
        vis_add(dft_vis_list[i], fft_vis_list[i])
        for i, _ in enumerate(dft_vis_list)
    ]
Пример #3
0
    def ft_cal_sm(ov, sm, g):
        assert isinstance(ov, Visibility), ov
        assert isinstance(sm, SkyModel), sm
        if g is not None:
            assert len(g) == 2, g
            assert isinstance(g[0], Image), g[0]
            assert isinstance(g[1], ConvolutionFunction), g[1]

        v = copy_visibility(ov)

        v.data['vis'][...] = 0.0 + 0.0j

        if len(sm.components) > 0:

            if isinstance(sm.mask, Image):
                comps = copy_skycomponent(sm.components)
                comps = apply_beam_to_skycomponent(comps, sm.mask)
                v = predict_skycomponent_visibility(v, comps)
            else:
                v = predict_skycomponent_visibility(v, sm.components)

        if isinstance(sm.image, Image):
            if numpy.max(numpy.abs(sm.image.data)) > 0.0:
                if isinstance(sm.mask, Image):
                    model = copy_image(sm.image)
                    model.data *= sm.mask.data
                else:
                    model = sm.image
                v = predict_list_serial_workflow([v], [model],
                                                 context=context,
                                                 vis_slices=vis_slices,
                                                 facets=facets,
                                                 gcfcf=[g],
                                                 **kwargs)[0]

        if docal and isinstance(sm.gaintable, GainTable):
            bv = convert_visibility_to_blockvisibility(v)
            bv = apply_gaintable(bv, sm.gaintable, inverse=True)
            v = convert_blockvisibility_to_visibility(bv)

        return v
Пример #4
0
    def test_zero_list(self):
        self.actualSetUp()

        centre = self.freqwin // 2
        vis_list = zero_list_serial_workflow(self.vis_list)

        assert numpy.max(numpy.abs(vis_list[centre].vis)) < 1e-15, numpy.max(
            numpy.abs(vis_list[centre].vis))

        predicted_vis_list = [
            predict_skycomponent_visibility(vis_list[freqwin],
                                            self.components_list[freqwin])
            for freqwin, _ in enumerate(self.frequency)
        ]
        assert numpy.max(numpy.abs(predicted_vis_list[centre].vis)) > 0.0, \
            numpy.max(numpy.abs(predicted_vis_list[centre].vis))

        diff_vis_list = subtract_list_serial_workflow(self.vis_list,
                                                      predicted_vis_list)

        assert numpy.max(numpy.abs(
            diff_vis_list[centre].vis)) < 1e-15, numpy.max(
                numpy.abs(diff_vis_list[centre].vis))
Пример #5
0
    # We calculate the source positions in pixels and then calculate the
    # world coordinates to put in the skycomponent description
    for iy in locations:
        for ix in locations:
            if ix >= iy:
                p = int(round(centre[0] + ix * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[0]))), \
                    int(round(centre[1] + iy * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[1])))
                sc = pixel_to_skycoord(p[0], p[1], model.wcs)
                log.info("Component at (%f, %f) [0-rel] %s" % (p[0], p[1], str(sc)))
                flux = numpy.array([[100.0 + 2.0 * ix + iy * 20.0]])
                comp = create_skycomponent(flux=flux, frequency=frequency, direction=sc,
                                        polarisation_frame=PolarisationFrame('stokesI'))
                original_comps.append(comp)
                insert_skycomponent(model, comp)

    predict_skycomponent_visibility(vt, original_comps)


    cmodel = smooth_image(model)
    show_image(cmodel)
    plt.title("Smoothed model image")
    plt.savefig('1.jpg')
    #plt.show()

    comps = find_skycomponents(cmodel, fwhm=1.0, threshold=10.0, npixels=5)
    plt.clf()
    for i in range(len(comps)):
        ocomp, sep = find_nearest_skycomponent(comps[i].direction, original_comps)
        plt.plot((comps[i].direction.ra.value  - ocomp.direction.ra.value)/cmodel.wcs.wcs.cdelt[0],
                (comps[i].direction.dec.value - ocomp.direction.dec.value)/cmodel.wcs.wcs.cdelt[1],
                '.', color='r')
Пример #6
0
    def actualSetUp(self,
                    add_errors=False,
                    freqwin=3,
                    block=False,
                    dospectral=True,
                    dopol=False,
                    zerow=False,
                    makegcfcf=False):

        self.npixel = 256
        self.low = create_named_configuration('LOWBD2', rmax=750.0)
        self.freqwin = freqwin
        self.vis_list = list()
        self.ntimes = 5
        self.cellsize = 0.0005
        # Choose the interval so that the maximum change in w is smallish
        integration_time = numpy.pi * (24 / (12 * 60))
        self.times = numpy.linspace(-integration_time * (self.ntimes // 2),
                                    integration_time * (self.ntimes // 2),
                                    self.ntimes)

        if freqwin > 1:
            self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin)
            self.channelwidth = numpy.array(
                freqwin * [self.frequency[1] - self.frequency[0]])
        else:
            self.frequency = numpy.array([1.0e8])
            self.channelwidth = numpy.array([4e7])

        if dopol:
            self.vis_pol = PolarisationFrame('linear')
            self.image_pol = PolarisationFrame('stokesIQUV')
            f = numpy.array([100.0, 20.0, -10.0, 1.0])
        else:
            self.vis_pol = PolarisationFrame('stokesI')
            self.image_pol = PolarisationFrame('stokesI')
            f = numpy.array([100.0])

        if dospectral:
            flux = numpy.array(
                [f * numpy.power(freq / 1e8, -0.7) for freq in self.frequency])
        else:
            flux = numpy.array([f])

        self.phasecentre = SkyCoord(ra=+180.0 * u.deg,
                                    dec=-60.0 * u.deg,
                                    frame='icrs',
                                    equinox='J2000')
        self.vis_list = [
            ingest_unittest_visibility(self.low, [self.frequency[freqwin]],
                                       [self.channelwidth[freqwin]],
                                       self.times,
                                       self.vis_pol,
                                       self.phasecentre,
                                       block=block,
                                       zerow=zerow)
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.model_list = [
            create_unittest_model(self.vis_list[freqwin],
                                  self.image_pol,
                                  cellsize=self.cellsize,
                                  npixel=self.npixel)
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.components_list = [
            create_unittest_components(self.model_list[freqwin],
                                       flux[freqwin, :][numpy.newaxis, :],
                                       single=True)
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.model_list = [
            insert_skycomponent(self.model_list[freqwin],
                                self.components_list[freqwin])
            for freqwin, _ in enumerate(self.frequency)
        ]

        self.vis_list = [
            predict_skycomponent_visibility(self.vis_list[freqwin],
                                            self.components_list[freqwin])
            for freqwin, _ in enumerate(self.frequency)
        ]
        centre = self.freqwin // 2
        # Calculate the model convolved with a Gaussian.
        self.model = self.model_list[centre]

        self.cmodel = smooth_image(self.model)
        export_image_to_fits(self.model,
                             '%s/test_imaging_model.fits' % self.dir)
        export_image_to_fits(self.cmodel,
                             '%s/test_imaging_cmodel.fits' % self.dir)

        if add_errors and block:
            self.vis_list = [
                insert_unittest_errors(self.vis_list[i])
                for i, _ in enumerate(self.frequency)
            ]

        self.components = self.components_list[centre]

        if makegcfcf:
            self.gcfcf = [
                create_awterm_convolutionfunction(self.model,
                                                  nw=61,
                                                  wstep=16.0,
                                                  oversampling=8,
                                                  support=64,
                                                  use_aaf=True)
            ]
            self.gcfcf_clipped = [
                (self.gcfcf[0][0],
                 apply_bounding_box_convolutionfunction(self.gcfcf[0][1],
                                                        fractional_level=1e-3))
            ]

            self.gcfcf_joint = [
                create_awterm_convolutionfunction(self.model,
                                                  nw=11,
                                                  wstep=16.0,
                                                  oversampling=8,
                                                  support=64,
                                                  use_aaf=True)
            ]

        else:
            self.gcfcf = None
            self.gcfcf_clipped = None
            self.gcfcf_joint = None
 def actualSetUp(self, add_errors=False, freqwin=7, block=False, dospectral=True, dopol=False,
                 zerow=True):
     
     self.npixel = 256
     self.low = create_named_configuration('LOWBD2', rmax=750.0)
     self.freqwin = freqwin
     self.vis_list = list()
     self.ntimes = 5
     cellsize = 0.001
     self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0
     self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin)
     
     if freqwin > 1:
         self.channelwidth = numpy.array(freqwin * [self.frequency[1] - self.frequency[0]])
     else:
         self.channelwidth = numpy.array([1e6])
     
     if dopol:
         self.vis_pol = PolarisationFrame('linear')
         self.image_pol = PolarisationFrame('stokesIQUV')
         f = numpy.array([100.0, 20.0, -10.0, 1.0])
     else:
         self.vis_pol = PolarisationFrame('stokesI')
         self.image_pol = PolarisationFrame('stokesI')
         f = numpy.array([100.0])
     
     if dospectral:
         flux = numpy.array([f * numpy.power(freq / 1e8, -0.7) for freq in self.frequency])
     else:
         flux = numpy.array([f])
     
     self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000')
     self.vis_list = [ingest_unittest_visibility(self.low,
                                                 [self.frequency[freqwin]],
                                                 [self.channelwidth[freqwin]],
                                                 self.times,
                                                 self.vis_pol,
                                                 self.phasecentre, block=block,
                                                 zerow=zerow)
                      for freqwin, _ in enumerate(self.frequency)]
     
     self.model_imagelist = [create_unittest_model(self.vis_list[freqwin],
                                                   self.image_pol,
                                                   cellsize=cellsize,
                                                   npixel=self.npixel)
                             for freqwin, _ in enumerate(self.frequency)]
     
     self.componentlist = [create_unittest_components(self.model_imagelist[freqwin],
                                                      flux[freqwin, :][numpy.newaxis, :])
                           for freqwin, _ in enumerate(self.frequency)]
     
     self.model_imagelist = [insert_skycomponent(self.model_imagelist[freqwin],
                                                 self.componentlist[freqwin])
                             for freqwin, _ in enumerate(self.frequency)]
     
     self.vis_list = [predict_skycomponent_visibility(self.vis_list[freqwin],
                                                      self.componentlist[freqwin])
                      for freqwin, _ in enumerate(self.frequency)]
     
     # Calculate the model convolved with a Gaussian.
     
     model = self.model_imagelist[0]
     
     self.cmodel = smooth_image(model)
     export_image_to_fits(model, '%s/test_imaging_serial_deconvolved_model.fits' % self.dir)
     export_image_to_fits(self.cmodel, '%s/test_imaging_serial_deconvolved_cmodel.fits' % self.dir)
     
     if add_errors and block:
         self.vis_list = [insert_unittest_errors(self.vis_list[i])
                          for i, _ in enumerate(self.frequency)]
Пример #8
0
 def actualSetUp(self, add_errors=False, nfreqwin=7, dospectral=True, dopol=False, zerow=True):
     
     self.npixel = 512
     self.low = create_named_configuration('LOWBD2', rmax=750.0)
     self.freqwin = nfreqwin
     self.vis_list = list()
     self.ntimes = 5
     self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0
     self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin)
     
     if self.freqwin > 1:
         self.channelwidth = numpy.array(self.freqwin * [self.frequency[1] - self.frequency[0]])
     else:
         self.channelwidth = numpy.array([1e6])
     
     if dopol:
         self.vis_pol = PolarisationFrame('linear')
         self.image_pol = PolarisationFrame('stokesIQUV')
         f = numpy.array([100.0, 20.0, -10.0, 1.0])
     else:
         self.vis_pol = PolarisationFrame('stokesI')
         self.image_pol = PolarisationFrame('stokesI')
         f = numpy.array([100.0])
     
     if dospectral:
         flux = numpy.array([f * numpy.power(freq / 1e8, -0.7) for freq in self.frequency])
     else:
         flux = numpy.array([f])
     
     self.phasecentre = SkyCoord(ra=+180.0 * u.deg, dec=-60.0 * u.deg, frame='icrs', equinox='J2000')
     self.blockvis_list = [ingest_unittest_visibility(self.low,
                                                      [self.frequency[i]],
                                                      [self.channelwidth[i]],
                                                      self.times,
                                                      self.vis_pol,
                                                      self.phasecentre, block=True,
                                                      zerow=zerow)
                           for i in range(nfreqwin)]
     
     self.vis_list = [convert_blockvisibility_to_visibility(bv) for bv in self.blockvis_list]
     
     self.model_imagelist = [
         create_unittest_model(self.vis_list[i], self.image_pol, npixel=self.npixel, cellsize=0.0005)
         for i in range(nfreqwin)]
     
     self.components_list = [create_unittest_components(self.model_imagelist[freqwin],
                                                        flux[freqwin, :][numpy.newaxis, :])
                             for freqwin, m in enumerate(self.model_imagelist)]
     
     self.blockvis_list = [
         predict_skycomponent_visibility(self.blockvis_list[freqwin], self.components_list[freqwin])
         for freqwin, _ in enumerate(self.blockvis_list)]
     
     self.model_imagelist = [insert_skycomponent(self.model_imagelist[freqwin], self.components_list[freqwin])
                             for freqwin in range(nfreqwin)]
     model = self.model_imagelist[0]
     self.cmodel = smooth_image(model)
     if self.persist:
         export_image_to_fits(model, '%s/test_imaging_serial_model.fits' % self.dir)
         export_image_to_fits(self.cmodel, '%s/test_imaging_serial_cmodel.fits' % self.dir)
     
     if add_errors:
         gt = create_gaintable_from_blockvisibility(self.blockvis_list[0])
         gt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.0, smooth_channels=1,
                    leakage=0.0, seed=180555)
         self.blockvis_list = [apply_gaintable(self.blockvis_list[i], gt)
                               for i in range(self.freqwin)]
     
     self.vis_list = [convert_blockvisibility_to_visibility(bv) for bv in self.blockvis_list]
     
     self.model_imagelist = [
         create_unittest_model(self.vis_list[i], self.image_pol, npixel=self.npixel, cellsize=0.0005)
         for i in range(nfreqwin)]
    def ingest_visibility(self,
                          freq=None,
                          chan_width=None,
                          times=None,
                          add_errors=False,
                          block=True,
                          bandpass=False):
        if freq is None:
            freq = [1e8]
        if chan_width is None:
            chan_width = [1e6]
        if times is None:
            times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 5)

        lowcore = create_named_configuration('LOWBD2', rmax=750.0)
        frequency = numpy.array(freq)
        channel_bandwidth = numpy.array(chan_width)

        phasecentre = SkyCoord(ra=+180.0 * u.deg,
                               dec=-60.0 * u.deg,
                               frame='icrs',
                               equinox='J2000')
        if block:
            vt = create_blockvisibility(
                lowcore,
                times,
                frequency,
                channel_bandwidth=channel_bandwidth,
                weight=1.0,
                phasecentre=phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"))
        else:
            vt = create_visibility(
                lowcore,
                times,
                frequency,
                channel_bandwidth=channel_bandwidth,
                weight=1.0,
                phasecentre=phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"))
        cellsize = 0.001
        model = create_image_from_visibility(
            vt,
            npixel=self.npixel,
            cellsize=cellsize,
            npol=1,
            frequency=frequency,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"))
        nchan = len(self.frequency)
        flux = numpy.array(nchan * [[100.0]])
        facets = 4

        rpix = model.wcs.wcs.crpix - 1.0
        spacing_pixels = self.npixel // facets
        centers = [-1.5, -0.5, 0.5, 1.5]
        comps = list()
        for iy in centers:
            for ix in centers:
                p = int(round(rpix[0] + ix * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[0]))), \
                    int(round(rpix[1] + iy * spacing_pixels * numpy.sign(model.wcs.wcs.cdelt[1])))
                sc = pixel_to_skycoord(p[0], p[1], model.wcs, origin=1)
                comp = create_skycomponent(
                    direction=sc,
                    flux=flux,
                    frequency=frequency,
                    polarisation_frame=PolarisationFrame("stokesI"))
                comps.append(comp)
        if block:
            predict_skycomponent_visibility(vt, comps)
        else:
            predict_skycomponent_visibility(vt, comps)
        insert_skycomponent(model, comps)
        self.comps = comps
        self.model = copy_image(model)
        self.empty_model = create_empty_image_like(model)
        export_image_to_fits(
            model, '%s/test_pipeline_functions_model.fits' % (self.dir))

        if add_errors:
            # These will be the same for all calls
            numpy.random.seed(180555)
            gt = create_gaintable_from_blockvisibility(vt)
            gt = simulate_gaintable(gt, phase_error=1.0, amplitude_error=0.0)
            vt = apply_gaintable(vt, gt)

            if bandpass:
                bgt = create_gaintable_from_blockvisibility(vt, timeslice=1e5)
                bgt = simulate_gaintable(bgt,
                                         phase_error=0.01,
                                         amplitude_error=0.01,
                                         smooth_channels=4)
                vt = apply_gaintable(vt, bgt)

        return vt
        model = create_image_from_visibility(vt, npixel=npixel, npol=1, cellsize=cellsize)
        spacing_pixels = npixel // facets
        log.info('Spacing in pixels = %s' % spacing_pixels)
        spacing = model.wcs.wcs.cdelt* spacing_pixels #180.0 * cellsize * spacing_pixels / numpy.pi
        centers = -3.0, -2.0, -1.0, -0.5, +0.5, +1.0, 2.0, 3.0
        comps = list()
        for iy in centers:
            for ix in centers:
                pra = int(round(npixel // 2 + ix * spacing_pixels - 1))
                pdec = int(round(npixel // 2 + iy * spacing_pixels - 1))
                sc = pixel_to_skycoord(pra, pdec, model.wcs)
                log.info("Component at (%f, %f) %s" % (pra, pdec, str(sc)))
                comp = create_skycomponent(flux=flux, frequency=frequency, direction=sc,
                                           polarisation_frame=PolarisationFrame("stokesI"))
                comps.append(comp)
        predict_skycomponent_visibility(vt, comps)

        arlexecute.set_client(use_dask=True)

        dirty = create_image_from_visibility(vt, npixel=npixel, cellsize=cellsize,
                                             polarisation_frame=PolarisationFrame("stokesI"))
        vt = weight_visibility(vt, dirty)

        future = invert_list_arlexecute_workflow([vt], [dirty], context='2d')
        dirty, sumwt = arlexecute.compute(future, sync=True)[0]

        if doplot:
            show_image(dirty,cm='hot')
            plt.title("Dirty image")
            plt.savefig('%s/%s_dirty.pdf' % (results_dir, str(freq)))