Exemple #1
0
    def _predict_base(self,
                      context='2d',
                      extra='',
                      fluxthreshold=1.0,
                      facets=1,
                      vis_slices=1,
                      gcfcf=None,
                      **kwargs):

        centre = self.freqwin // 2
        vis_list = zero_list_serial_workflow(self.vis_list)
        vis_list = predict_list_serial_workflow(vis_list,
                                                self.model_list,
                                                context=context,
                                                vis_slices=vis_slices,
                                                facets=facets,
                                                gcfcf=gcfcf,
                                                **kwargs)
        vis_list = subtract_list_serial_workflow(self.vis_list, vis_list)

        dirty = invert_list_serial_workflow(vis_list,
                                            self.model_list,
                                            context='2d',
                                            dopsf=False,
                                            gcfcf=gcfcf,
                                            normalize=True)[centre]

        assert numpy.max(numpy.abs(dirty[0].data)), "Residual image is empty"
        export_image_to_fits(
            dirty[0], '%s/test_imaging_predict_%s%s_serial_dirty.fits' %
            (self.dir, context, extra))

        maxabs = numpy.max(numpy.abs(dirty[0].data))
        assert maxabs < fluxthreshold, "Error %.3f greater than fluxthreshold %.3f " % (
            maxabs, fluxthreshold)
Exemple #2
0
    def test_deconvolve_and_restore_cube_mmclean(self):
        self.actualSetUp(add_errors=True)
        dirty_imagelist = invert_list_serial_workflow(self.vis_list,
                                                      self.model_imagelist,
                                                      context='2d',
                                                      dopsf=False,
                                                      normalize=True)
        psf_imagelist = invert_list_serial_workflow(self.vis_list,
                                                    self.model_imagelist,
                                                    context='2d',
                                                    dopsf=True,
                                                    normalize=True)
        dec_imagelist = deconvolve_list_serial_workflow(
            dirty_imagelist,
            psf_imagelist,
            self.model_imagelist,
            niter=1000,
            fractional_threshold=0.01,
            scales=[0, 3, 10],
            algorithm='mmclean',
            nmoment=3,
            nchan=self.freqwin,
            threshold=0.1,
            gain=0.7)
        residual_imagelist = residual_list_serial_workflow(
            self.vis_list, model_imagelist=dec_imagelist, context='2d')
        restored = restore_list_serial_workflow(
            model_imagelist=dec_imagelist,
            psf_imagelist=psf_imagelist,
            residual_imagelist=residual_imagelist,
            empty=self.model_imagelist)[0]

        export_image_to_fits(
            restored,
            '%s/test_imaging_serial_mmclean_restored.fits' % (self.dir))
Exemple #3
0
    def _invert_base(self,
                     context,
                     extra='',
                     fluxthreshold=1.0,
                     positionthreshold=1.0,
                     check_components=True,
                     facets=1,
                     vis_slices=1,
                     gcfcf=None,
                     **kwargs):

        centre = self.freqwin // 2
        dirty = invert_list_serial_workflow(self.vis_list,
                                            self.model_list,
                                            context=context,
                                            dopsf=False,
                                            normalize=True,
                                            facets=facets,
                                            vis_slices=vis_slices,
                                            gcfcf=gcfcf,
                                            **kwargs)[centre]

        export_image_to_fits(
            dirty[0], '%s/test_imaging_invert_%s%s_serial_dirty.fits' %
            (self.dir, context, extra))

        assert numpy.max(numpy.abs(dirty[0].data)), "Image is empty"

        if check_components:
            self._checkcomponents(dirty[0], fluxthreshold, positionthreshold)
 def test_deconvolve_spectral(self):
     self.actualSetUp(add_errors=True)
     dirty_imagelist = invert_list_serial_workflow(self.vis_list, self.model_imagelist,
                                                   context='2d',
                                                   dopsf=False, normalize=True)
     psf_imagelist = invert_list_serial_workflow(self.vis_list, self.model_imagelist,
                                                 context='2d',
                                                 dopsf=True, normalize=True)
     deconvolved, _ = deconvolve_list_serial_workflow(dirty_imagelist, psf_imagelist, self.model_imagelist, niter=1000,
                                                      fractional_threshold=0.1, scales=[0, 3, 10],
                                                      threshold=0.1, gain=0.7)
     export_image_to_fits(deconvolved[0], '%s/test_imaging_serial_deconvolve_spectral.fits' %
                          (self.dir))
    def test_continuum_imaging_pipeline(self):
        self.actualSetUp(add_errors=False, zerow=True)
        clean, residual, restored = \
            continuum_imaging_list_serial_workflow(self.vis_list,
                                                   model_imagelist=self.model_imagelist,
                                                   context='2d',
                                                   algorithm='mmclean', facets=1,
                                                   scales=[0, 3, 10],
                                                   niter=1000, fractional_threshold=0.1, threshold=0.1,
                                                   nmoment=3,
                                                   nmajor=5, gain=0.1,
                                                   deconvolve_facets=4, deconvolve_overlap=32,
                                                   deconvolve_taper='tukey', psf_support=64)
        centre = len(clean) // 2
        if self.persist:
            export_image_to_fits(
                clean[centre],
                '%s/test_pipelines_continuum_imaging_pipeline_serial_clean.fits'
                % self.dir)
            export_image_to_fits(
                residual[centre][0],
                '%s/test_pipelines_continuum_imaging_pipeline_serial_residual.fits'
                % self.dir)
            export_image_to_fits(
                restored[centre],
                '%s/test_pipelines_continuum_imaging_pipeline_serial_restored.fits'
                % self.dir)

        qa = qa_image(restored[centre])
        assert numpy.abs(qa.data['max'] - 100.13762476849081) < 1.0, str(qa)
        assert numpy.abs(qa.data['min'] + 0.03627273884170454) < 1.0, str(qa)
Exemple #6
0
    def test_ical_pipeline_global(self):
        self.actualSetUp(add_errors=True)
        controls = create_calibration_controls()
        controls['T']['first_selfcal'] = 1
        controls['T']['timescale'] = 'auto'
        
        clean, residual, restored, gt_list = \
            ical_list_serial_workflow(self.vis_list,
                                      model_imagelist=self.model_imagelist,
                                      context='2d',
                                      algorithm='mmclean', facets=1,
                                      scales=[0, 3, 10],
                                      niter=1000, fractional_threshold=0.1, threshold=0.1,
                                      nmoment=3,
                                      nmajor=5, gain=0.1,
                                      deconvolve_facets=4, deconvolve_overlap=32,
                                      deconvolve_taper='tukey', psf_support=64,
                                      calibration_context='T', controls=controls, do_selfcal=True,
                                      global_solution=True)
        
        centre = len(clean) // 2
        if self.persist:
            export_image_to_fits(clean[centre], '%s/test_pipelines_ical_global_pipeline_serial_clean.fits' % self.dir)
            export_image_to_fits(residual[centre][0], '%s/test_pipelines_ical_global_pipeline_serial_residual.fits' % self.dir)
            export_image_to_fits(restored[centre], '%s/test_pipelines_ical_global_pipeline_serial_restored.fits' % self.dir)
            export_gaintable_to_hdf5(gt_list[0]['T'],
                                     '%s/test_pipelines_ical_global_pipeline_serial_gaintable.hdf5' %
                                     self.dir)

        qa = qa_image(restored[centre])
        assert numpy.abs(qa.data['max'] - 98.92656340122159) < 1.0, str(qa)
        assert numpy.abs(qa.data['min'] + 0.7024492707920869) < 1.0, str(qa)
    def _predict_base(self,
                      context='2d',
                      extra='',
                      fluxthreshold=1.0,
                      facets=1,
                      vis_slices=1,
                      **kwargs):

        vis_list = zero_list_mpi_workflow(self.vis_list)
        vis_list = predict_list_mpi_workflow(vis_list,
                                             self.model_list,
                                             context=context,
                                             vis_slices=vis_slices,
                                             facets=facets,
                                             **kwargs)
        vis_list = subtract_list_mpi_workflow(self.vis_list, vis_list)
        if self.rank == 0:
            vis_list = vis_list[0]
            model_list = self.model_list[0]
        else:
            vis_list = list()
            model_list = list()
        dirty = invert_list_mpi_workflow([vis_list], [model_list],
                                         context='2d',
                                         dopsf=False,
                                         normalize=True)
        if self.rank == 0:
            dirty = dirty[0]
        if self.rank == 0:
            assert numpy.max(numpy.abs(
                dirty[0].data)), "Residual image is empty"
            export_image_to_fits(
                dirty[0], '%s/test_imaging_predict_%s%s_serial_dirty.fits' %
                (self.dir, context, extra))

            maxabs = numpy.max(numpy.abs(dirty[0].data))
            assert maxabs < fluxthreshold, "Error %.3f greater than fluxthreshold %.3f " % (
                maxabs, fluxthreshold)
Exemple #8
0
    def test_ical_pipeline(self):
        amp_errors = {'T': 0.0, 'G': 0.00, 'B': 0.0}
        phase_errors = {'T': 0.1, 'G': 0.0, 'B': 0.0}
        self.actualSetUp(add_errors=True,
                         block=True,
                         amp_errors=amp_errors,
                         phase_errors=phase_errors)

        controls = create_calibration_controls()

        controls['T']['first_selfcal'] = 1
        controls['G']['first_selfcal'] = 3
        controls['B']['first_selfcal'] = 4

        controls['T']['timescale'] = 'auto'
        controls['G']['timescale'] = 'auto'
        controls['B']['timescale'] = 1e5

        clean, residual, restored = \
            ical_list_serial_workflow(self.vis_list, model_imagelist=self.model_imagelist, context='2d',
                                      calibration_context='T', controls=controls, do_selfcal=True,
                                      global_solution=False,
                                      algorithm='mmclean',
                                      facets=1,
                                      scales=[0, 3, 10],
                                      niter=1000, fractional_threshold=0.1,
                                      nmoments=2, nchan=self.freqwin,
                                      threshold=2.0, nmajor=5, gain=0.1,
                                      deconvolve_facets=8, deconvolve_overlap=16, deconvolve_taper='tukey')
        centre = len(clean) // 2
        export_image_to_fits(
            clean[centre],
            '%s/test_pipelines_ical_pipeline_clean.fits' % self.dir)
        export_image_to_fits(
            residual[centre][0],
            '%s/test_pipelines_ical_pipeline_residual.fits' % self.dir)
        export_image_to_fits(
            restored[centre],
            '%s/test_pipelines_ical_pipeline_restored.fits' % self.dir)

        qa = qa_image(restored[centre])
        assert numpy.abs(qa.data['max'] - 100.13739440876233) < 1.0, str(qa)
        assert numpy.abs(qa.data['min'] + 0.03644435471804354) < 1.0, str(qa)
Exemple #9
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
        else:
            future = invert_list_arlexecute_workflow(vt_list, targetimage_list, context=context)

        result = arlexecute.compute(future, sync=True)
        targetimage = result[0][0]

        show_image(targetimage)
        plt.title(context)
        plt.savefig('2_'+context+'.jpg')

        #plt.show()

        print("Dirty Image %s" % qa_image(targetimage, context="imaging-fits notebook, using processor %s" % context))

        export_image_to_fits(targetimage, '%s/imaging-fits_dirty_%s.fits' % (results_dir, context))
        comps = find_skycomponents(targetimage, fwhm=1.0, threshold=10.0, npixels=5)

        plt.clf()
        for comp in comps:
            distance = comp.direction.separation(model.phasecentre)
            dft_flux = sum_visibility(vt, comp.direction)[0]
            err = (comp.flux[0, 0] - dft_flux) / dft_flux
            plt.plot(distance, err, '.', color='r')
        plt.ylabel('Fractional error of image vs DFT')
        plt.xlabel('Distance from phasecentre (deg)')
        plt.title(
            "Fractional error in %s recovered flux vs distance from phasecentre" %
            context)
        plt.savefig('3_'+context+'.jpg')
        # plt.show()
    # In[ ]:
end = time.time()
log.info('%d: invert finished' % (rank))
print('%d: invert finished in %f seconds' % (rank, end - start), flush=True)

if rank == 0:
    #print("sumwts",flush=True)
    #print(dirty_list[0][1])

    log.info('After invert to get dirty image')
    dirty = dirty_list[0][0]
    #show_image(dirty, cm='Greys', vmax=1.0, vmin=-0.1)
    #plt.show()
    print(qa_image(dirty))
    export_image_to_fits(dirty, '%s/imaging-dirty.fits' % (results_dir))

    log.info('After invert to get PSF')
    psf = psf_list[0][0]
    #show_image(psf, cm='Greys', vmax=0.1, vmin=-0.01)
    #plt.show()
    print(qa_image(psf))
    export_image_to_fits(psf, '%s/imaging-psf.fits' % (results_dir))

# Now deconvolve using msclean

# In[ ]:

log.info('%d: About to run deconvolve' % (rank))
print('%d: About to run deconvolve' % (rank), flush=True)
start = time.time()
        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)))

        print("Max, min in dirty image = %.6f, %.6f, sumwt = %f" % (dirty.data.max(), dirty.data.min(), sumwt))

        export_image_to_fits(dirty, '%s/imaging-wterm_dirty.fits' % (results_dir))

        dirtyFacet = create_image_from_visibility(vt, npixel=npixel, npol=1, cellsize=cellsize)
        future = invert_list_arlexecute_workflow([vt], [dirtyFacet], facets=4, context='facets')
        dirtyFacet, sumwt = arlexecute.compute(future, sync=True)[0]
        dirtyFacet = smooth_image(dirtyFacet)
        if doplot:
            show_image(dirtyFacet,cm='hot')
            plt.title("Smoothed model image")
            plt.savefig('%s/%s_smooth.pdf' % (results_dir, str(freq)))

        print(
            "Max, min in dirty image = %.6f, %.6f, sumwt = %f" % (dirtyFacet.data.max(), dirtyFacet.data.min(), sumwt))
        export_image_to_fits(dirtyFacet, '%s/imaging-wterm_dirtyFacet.fits' % (results_dir))

        dirtyFacet2 = create_image_from_visibility(vt, npixel=npixel, npol=1, cellsize=cellsize)
Exemple #13
0
                                          context='wstack',
                                          vis_slices=vis_slices,
                                          dopsf=False)
    psf_list = invert_list_mpi_workflow(predicted_vislist,
                                        model_list,
                                        context='wstack',
                                        vis_slices=vis_slices,
                                        dopsf=True)

    # Create and execute graphs to make the dirty image and PSF

    # In[ ]:

if rank == 0:
    print("sumwts", flush=True)
    print(dirty_list[0][1])

    log.info('About to run invert to get dirty image')
    dirty = dirty_list[0][0]
    #show_image(dirty, cm='Greys', vmax=1.0, vmin=-0.1)
    #plt.show()
    print(qa_image(dirty))
    export_image_to_fits(dirty, '%s/imaging-dirty.fits' % (results_dir))

    log.info('About to run invert to get PSF')
    psf = psf_list[0][0]
    #show_image(psf, cm='Greys', vmax=0.1, vmin=-0.01)
    #plt.show()
    print(qa_image(psf))
    export_image_to_fits(psf, '%s/imaging-psf.fits' % (results_dir))
 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)]
    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
        else:
            d, sumwt = invert_list_serial_workflow([v], [m],
                                                   context=context,
                                                   dopsf=False,
                                                   vis_slices=vis_slices)[0]
            p, sumwt = invert_list_serial_workflow([v], [m],
                                                   context=context,
                                                   dopsf=True,
                                                   vis_slices=vis_slices)[0]
        c, resid = deconvolve_cube(d,
                                   p,
                                   m,
                                   threshold=0.01,
                                   fracthresh=0.01,
                                   window_shape='quarter',
                                   niter=100,
                                   gain=0.1,
                                   algorithm='hogbom-complex')
        r = restore_cube(c, p, resid, psfwidth=psfwidth)
        return r

    print('About assemble cubes and deconvolve each frequency')
    restored_list = [load_invert_and_deconvolve(c) for c in range(nchan)]
    restored_cube = image_gather_channels(restored_list)
    print("Processing took %.3f s" % (time.time() - start))

    print(qa_image(restored_cube, context='CLEAN restored cube'))
    export_image_to_fits(
        restored_cube, '%s/dprepb_serial_%s_clean_restored_cube.fits' %
        (results_dir, context))
Exemple #17
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)]
 
 # Test requires that casa be installed
 try:
     bvt = create_blockvisibility_from_ms(arl_path('data/vis/sim-2.ms'), channum=[35, 36, 37, 38, 39])[0]
     bvt.configuration.diameter[...] = 35.0
     vt = convert_blockvisibility_to_visibility(bvt)
     vt = convert_visibility_to_stokes(vt)
     
     cellsize = 20.0 * numpy.pi / (180.0 * 3600.0)
     npixel = 512
     
     model = create_image_from_visibility(vt, cellsize=cellsize, npixel=npixel,
                                          polarisation_frame=PolarisationFrame('stokesIQUV'))
     dirty, sumwt = invert_list_serial_workflow([vt], [model], context='2d')[0]
     psf, sumwt = invert_list_serial_workflow([vt], [model], context='2d', dopsf=True)[0]
     export_image_to_fits(dirty, '%s/compare_imaging_sim2_dirty.fits' % (results_dir))
     export_image_to_fits(psf, '%s/compare_imaging_sim2_psf.fits' % (results_dir))
     
     # Deconvolve using clean
     comp, residual = deconvolve_cube(dirty, psf, niter=10000, threshold=0.001, fractional_threshold=0.001,
                                      window_shape='quarter', gain=0.7, scales=[0, 3, 10, 30])
     
     restored = restore_cube(comp, psf, residual)
     export_image_to_fits(restored, '%s/compare_imaging_sim2_restored.fits' % (results_dir))
     export_image_to_fits(residual, '%s/compare_imaging_sim2_residual.fits' % (results_dir))
     
     qa = qa_image(restored)
     
     assert numpy.abs(qa.data['max'] - 1.006140596404203) < 1e-7, qa
     assert numpy.abs(qa.data['maxabs'] - 1.006140596404203) < 1e-7, qa
     assert numpy.abs(qa.data['min'] + 0.23890808520954754) < 1e-7, qa
        channel_model = create_image_from_visibility(
            vt,
            cellsize=cellsize,
            npixel=512,
            nchan=1,
            imagecentre=vis_list[0].phasecentre,
            polarisation_frame=PolarisationFrame('stokesIQUV'))

        beam = create_pb(channel_model,
                         telescope='VLA',
                         pointingcentre=vt.phasecentre,
                         use_local=False)
        beam.data /= numpy.max(beam.data)
        dirty, sumwt = invert_list_serial_workflow([vt], [channel_model])[0]
        print(sumwt)
        mosaic.data += dirty.data * beam.data
        mosaicsens.data += beam.data**2
        show_image(dirty)
        plt.show()

    show_image(mosaic, cm='Greys', title='Linear mosaic')
    plt.show()
    show_image(mosaicsens, cm='Greys', title='Linear mosaic sensitivity')
    plt.show()

    from wrappers.serial.image.operations import export_image_to_fits

    export_image_to_fits(mosaic, "mosaics.fits")
    export_image_to_fits(mosaicsens, "mosaicsens.fits")
# %%