def _invert_base(self,
                     fluxthreshold=1.0,
                     positionthreshold=1.0,
                     check_components=True,
                     name='predict_ng',
                     **kwargs):

        # dirty = invert_ng(self.blockvis, self.model, dopsf=False, normalize=True, **kwargs)
        from processing_components.imaging.ng import predict_ng, invert_ng
        dirty = invert_ng(self.blockvis,
                          self.model,
                          normalize=True,
                          verbosity=self.verbosity,
                          **kwargs)

        if self.persist:
            export_image_to_fits(
                dirty[0],
                '%s/test_imaging_ng_%s_dirty.fits' % (self.dir, name))

        # import matplotlib.pyplot as plt
        # from processing_components.image.operations import show_image
        # npol = dirty[0].shape[1]
        # for pol in range(npol):
        #     plt.clf()
        #     show_image(dirty[0], pol=pol)
        #     plt.show(block=False)

        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_workflow(self.vis_list,
                                          self.model_imagelist,
                                          context='2d',
                                          dopsf=False,
                                          normalize=True)
        psf_imagelist = invert_workflow(self.vis_list,
                                        self.model_imagelist,
                                        context='2d',
                                        dopsf=True,
                                        normalize=True)
        deconvolved, _ = deconvolve_workflow(dirty_imagelist,
                                             psf_imagelist,
                                             self.model_imagelist,
                                             niter=1000,
                                             fractional_threshold=0.1,
                                             scales=[0, 3, 10],
                                             threshold=0.1,
                                             gain=0.7)
        deconvolved = arlexecute.compute(deconvolved, sync=True)

        export_image_to_fits(
            deconvolved[0], '%s/test_imaging_%s_deconvolve_spectral.fits' %
            (self.dir, arlexecute.type()))
    def _invert_base(self,
                     context,
                     extra='',
                     fluxthreshold=1.0,
                     positionthreshold=1.0,
                     check_components=True,
                     facets=1,
                     vis_slices=1,
                     **kwargs):

        dirty = invert_serial(self.vis,
                              self.model,
                              context=context,
                              dopsf=False,
                              normalize=True,
                              facets=facets,
                              vis_slices=vis_slices,
                              **kwargs)
        export_image_to_fits(
            dirty[0], '%s/test_imaging_invert_%s%s_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 _predict_base(self, fluxthreshold=1.0, name='predict_ng', **kwargs):

        from processing_components.imaging.ng import predict_ng, invert_ng
        original_vis = copy_visibility(self.blockvis)
        vis = predict_ng(self.blockvis,
                         self.model,
                         verbosity=self.verbosity,
                         **kwargs)
        vis.data['vis'] = vis.data['vis'] - original_vis.data['vis']
        dirty = invert_ng(vis,
                          self.model,
                          dopsf=False,
                          normalize=True,
                          verbosity=self.verbosity,
                          **kwargs)

        # import matplotlib.pyplot as plt
        # from processing_components.image.operations import show_image
        # npol = dirty[0].shape[1]
        # for pol in range(npol):
        #     plt.clf()
        #     show_image(dirty[0], pol=pol)
        #     plt.show(block=False)

        if self.persist:
            export_image_to_fits(
                dirty[0],
                '%s/test_imaging_ng_%s_residual.fits' % (self.dir, name))

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

        maxabs = numpy.max(numpy.abs(dirty[0].data))
        assert maxabs < fluxthreshold, "Error %.3f greater than fluxthreshold %.3f " % (
            maxabs, fluxthreshold)
    def test_w_kernel_list(self):
        oversampling = 2
        kernelwidth = 32
        kernel_indices, kernels = w_kernel_list(self.vis,
                                                self.model,
                                                kernelwidth=kernelwidth,
                                                wstep=50,
                                                oversampling=oversampling)
        assert numpy.max(numpy.abs(kernels[0].data)) > 0.0
        assert len(kernel_indices) > 0
        assert max(kernel_indices) == len(kernels) - 1
        assert isinstance(kernels[0], numpy.ndarray)
        assert len(kernels[0].shape) == 4
        assert kernels[0].shape == (oversampling, oversampling, kernelwidth, kernelwidth), \
            "Actual shape is %s" % str(kernels[0].shape)
        kernel0 = create_image_from_array(
            kernels[0],
            self.model.wcs,
            polarisation_frame=PolarisationFrame('stokesI'))
        kernel0.data = kernel0.data.real
        export_image_to_fits(kernel0,
                             "%s/test_w_kernel_list_kernel0.fits" % (self.dir))

        with self.assertRaises(AssertionError):
            kernel_indices, kernels = w_kernel_list(self.vis,
                                                    self.model,
                                                    kernelwidth=32,
                                                    wstep=50,
                                                    oversampling=3,
                                                    maxsupport=128)
    def _predict_base(self,
                      context='2d',
                      extra='',
                      fluxthreshold=1.0,
                      facets=1,
                      vis_slices=1,
                      **kwargs):

        vis = copy_visibility(self.vis)
        vis.data['vis'][...] = 0
        vis = predict_serial(vis,
                             self.model,
                             context=context,
                             vis_slices=vis_slices,
                             facets=facets,
                             **kwargs)

        vis.data['vis'][...] -= self.vis.data['vis'][...]

        dirty = invert_serial(vis,
                              self.model,
                              context='2d',
                              dopsf=False,
                              normalize=True)

        assert numpy.max(numpy.abs(dirty[0].data)), "Residual image is empty"
        export_image_to_fits(
            dirty[0], '%s/test_imaging_predict_%s%s_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)
Ejemplo n.º 7
0
def moments_save_to_disk(moments_im, stokes_type='q', results_dir='./results_dir', outname='mean'):
    """Save the Faraday moments images to disk.
    
    Args:
    rmsynth (numpy array): array of complex numbers from RM Synthesis.
    cellsize (float): advised cellsize in Faraday space.
    maxrm_est (float): maximum observable RM (50 percent sensitivity).
    rmtype (str): the component of the complex numbers to process and save.
    results_dir (str): directory to save results.
    outname (str): outname for saved file.
    
    Returns:
    None
    """
    # Read in the first channel image, and appropriate it as the new moments image:
    im_moments = import_image_from_fits('%s/imaging_clean_WStack-%s.fits' % (results_dir, 0))
    # Place the data into the open image:
    im_moments.data = moments_im
    try:
        if stokes_type == 'p':
            stokes_val = 0.0
        elif stokes_type == 'q':
            stokes_val = 2.0
        elif stokes_type == 'u':
            stokes_val = 3.0
    except:
        print("Unknown value for stokes_type:", sys.exc_info()[0])
        raise

    # This line also adjusts the listed Stokes parameter (use 0.0=? for P):
    im_moments.wcs.wcs.crval = [im_moments.wcs.wcs.crval[0], im_moments.wcs.wcs.crval[1],
                                stokes_val, im_moments.wcs.wcs.crval[3]]
    # Output the file to disk:
    export_image_to_fits(im_moments, '%s/%s_%s.fits' % (results_dir, outname, stokes_type))
    return
Ejemplo n.º 8
0
    def test_create_voltage_patterns_zernike(self):

        self.createVis(freq=1.4e9)
        cellsize = 8 * numpy.pi / 180.0 / 280
        model = create_image_from_visibility(self.vis,
                                             npixel=512,
                                             cellsize=cellsize,
                                             override_cellsize=False)
        plt.clf()
        fig, axs = plt.subplots(7, 7, gridspec_kw={'hspace': 0, 'wspace': 0})
        for noll in range(1, 50):
            zernikes = [{'coeff': 1.0, 'noll': noll}]
            vp = create_vp_generic_numeric(model,
                                           pointingcentre=None,
                                           diameter=15.0,
                                           blockage=0.0,
                                           taper='gaussian',
                                           edge=0.03162278,
                                           zernikes=zernikes,
                                           padding=2,
                                           use_local=True)
            vp_data = vp.data
            vp.data = numpy.real(vp_data)
            export_image_to_fits(
                vp, "%s/test_voltage_pattern_real_%s_NOLL%d.fits" %
                (self.dir, 'MID_ZERNIKES', noll))
            row = (noll - 1) // 7
            col = (noll - 1) - 7 * row
            ax = axs[row, col]
            ax.imshow(vp.data[0, 0], vmax=0.1, vmin=-0.01)
            #ax.set_title('Noll %d' % noll)
            ax.axis('off')

        plt.savefig("zernikes.png")
        plt.show()
 def test_scatter_gather_facet_overlap_taper(self):
 
     m31original = create_test_image(polarisation_frame=PolarisationFrame('stokesI'))
     assert numpy.max(numpy.abs(m31original.data)), "Original is empty"
 
     for taper in ['linear', None]:
         for nraster, overlap in [(1, 0), (4, 8), (8, 8), (8, 16)]:
             m31model = create_test_image(polarisation_frame=PolarisationFrame('stokesI'))
             image_list = image_scatter_facets(m31model, facets=nraster, overlap=overlap, taper=taper)
             for patch in image_list:
                 assert patch.data.shape[3] == (2 * overlap + m31model.data.shape[3] // nraster), \
                     "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[3],
                                                                                 (2 * overlap + m31model.data.shape[3] //
                                                                                  nraster))
                 assert patch.data.shape[2] == (2 * overlap + m31model.data.shape[2] // nraster), \
                     "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[2],
                                                                                 (2 * overlap + m31model.data.shape[2] //
                                                                                  nraster))
             m31reconstructed = create_empty_image_like(m31model)
             m31reconstructed = image_gather_facets(image_list, m31reconstructed, facets=nraster, overlap=overlap,
                                                    taper=taper)
             flat = image_gather_facets(image_list, m31reconstructed, facets=nraster, overlap=overlap,
                                        taper=taper, return_flat=True)
             export_image_to_fits(m31reconstructed,
                                  "%s/test_image_gather_scatter_%dnraster_%doverlap_%s_reconstructed.fits" %
                                  (self.dir, nraster, overlap, taper))
             export_image_to_fits(flat,
                                  "%s/test_image_gather_scatter_%dnraster_%doverlap_%s_flat.fits" %
                                  (self.dir, nraster, overlap, taper))
 
             assert numpy.max(numpy.abs(flat.data)), "Flat is empty for %d" % nraster
             assert numpy.max(numpy.abs(m31reconstructed.data)), "Raster is empty for %d" % nraster
Ejemplo n.º 10
0
 def test_calculate_image_frequency_moments(self):
     frequency = numpy.linspace(0.9e8, 1.1e8, 9)
     cube = create_low_test_image_from_gleam(npixel=512,
                                             cellsize=0.0001,
                                             frequency=frequency,
                                             flux_limit=1.0)
     log.debug(
         export_image_to_fits(cube,
                              fitsfile='%s/test_moments_cube.fits' %
                              (self.dir)))
     original_cube = copy_image(cube)
     moment_cube = calculate_image_frequency_moments(cube, nmoment=3)
     log.debug(
         export_image_to_fits(moment_cube,
                              fitsfile='%s/test_moments_moment_cube.fits' %
                              (self.dir)))
     reconstructed_cube = calculate_image_from_frequency_moments(
         cube, moment_cube)
     log.debug(
         export_image_to_fits(
             reconstructed_cube,
             fitsfile='%s/test_moments_reconstructed_cube.fits' %
             (self.dir)))
     error = numpy.std(reconstructed_cube.data - original_cube.data)
     assert error < 0.2
 def test_create_convolutionfunction(self):
     cf = create_convolutionfunction_from_image(self.image, nz=1)
     cf_image = convert_convolutionfunction_to_image(cf)
     cf_image.data = numpy.real(cf_image.data)
     if self.persist:
         export_image_to_fits(
             cf_image, "%s/test_convolutionfunction_cf.fits" % self.dir)
 def test_griddata_predict_awterm(self):
     self.actualSetUp(zerow=False)
     make_pb = functools.partial(create_pb_generic,
                                 diameter=35.0,
                                 blockage=0.0,
                                 use_local=False)
     pb = make_pb(self.model)
     if self.persist:
         export_image_to_fits(pb,
                              "%s/test_gridding_awterm_pb.fits" % self.dir)
     gcf, cf = create_awterm_convolutionfunction(self.model,
                                                 make_pb=make_pb,
                                                 nw=100,
                                                 wstep=8.0,
                                                 oversampling=16,
                                                 support=32,
                                                 use_aaf=True)
     griddata = create_griddata_from_image(self.model, nw=100, wstep=8.0)
     griddata = fft_image_to_griddata(self.model, griddata, gcf)
     newvis = degrid_visibility_from_griddata(self.vis,
                                              griddata=griddata,
                                              cf=cf)
     qa = qa_visibility(newvis)
     assert qa.data['rms'] < 120.0, str(qa)
     self.plot_vis(newvis, 'awterm')
    def test_compare_wterm_kernels(self):
        _, cf = create_awterm_convolutionfunction(self.image,
                                                  nw=110,
                                                  wstep=8,
                                                  oversampling=8,
                                                  support=60,
                                                  use_aaf=True)
        cf.data = numpy.real(cf.data)

        peak_location = numpy.unravel_index(numpy.argmax(numpy.abs(cf.data)),
                                            cf.shape)
        assert peak_location == (0, 0, 55, 4, 4, 30, 30), peak_location
        assert numpy.abs(cf.data[peak_location] - (0.18704481681878257 - 0j)) < 1e-7, \
            cf.data[peak_location]

        _, cf_noover = create_awterm_convolutionfunction(self.image,
                                                         nw=111,
                                                         wstep=8,
                                                         oversampling=1,
                                                         support=60,
                                                         use_aaf=True)
        cf_noover.data = numpy.real(cf_noover.data)
        cf.data[...] -= cf_noover.data[0, 0, 0, 0, 0]

        cf_image = convert_convolutionfunction_to_image(cf)
        cf_image.data = numpy.real(cf_image.data)
        export_image_to_fits(
            cf_image,
            "%s/test_convolutionfunction_compare_wterm_kernels.fits" %
            self.dir)

        assert numpy.abs(cf.data[0, 0, 0, 4, 4, 30, 30]) < 5e-6, cf.data[0, 0,
                                                                         0, 4,
                                                                         4, 30,
                                                                         30]
    def test_create_voltage_patterns_illumination(self):

        self.createVis(freq=1.4e9)
        cellsize = 8 * numpy.pi / 180.0 / 280
        model = create_image_from_visibility(self.vis,
                                             npixel=512,
                                             cellsize=cellsize,
                                             override_cellsize=False)
        plt.clf()
        fig, axs = plt.subplots(5, 5, gridspec_kw={'hspace': 0, 'wspace': 0})
        # (r ** 2 + rho * (dx * dy) + diff * (dx ** 2 - dy ** 2))
        for irho, rho in enumerate([-0.1, -0.05, 0.0, 0.05, 0.1]):
            for idiff, diff in enumerate([-0.2, -0.15, -0.1, -0.05, 0.0]):
                vp = create_vp_generic_numeric(model,
                                               pointingcentre=None,
                                               diameter=15.0,
                                               blockage=0.0,
                                               taper='gaussian',
                                               edge=0.03162278,
                                               padding=2,
                                               use_local=True,
                                               rho=rho,
                                               diff=diff)
                vp_data = vp.data
                vp.data = numpy.real(vp_data)
                if self.persist:
                    export_image_to_fits(
                        vp,
                        "%s/test_voltage_pattern_real_%s_rho%.3f_diff%.3f.fits"
                        % (self.dir, "MID_TAPER", rho, diff))
                ax = axs[irho, idiff]
                ax.imshow(vp.data[0, 0])  #, vmax=0.1, vmin=-0.01)
                ax.axis('off')

        plt.show()
    def test_skymodel_solve_fixed(self):
        self.actualSetup(ntimes=1, doiso=True, fixed=True)
        calskymodel, residual_vis = calskymodel_solve(self.vis,
                                                      self.skymodels,
                                                      niter=30,
                                                      gain=0.25)

        # Check that the components are unchanged
        calskymodel_skycomponents = list()
        for sm in [csm[0] for csm in calskymodel]:
            for comp in sm.components:
                calskymodel_skycomponents.append(comp)

        recovered_components = find_skycomponent_matches(
            calskymodel_skycomponents, self.components, 1e-5)
        for p in recovered_components:
            assert numpy.abs(calskymodel_skycomponents[p[0]].flux[0, 0] -
                             self.components[p[1]].flux[0, 0]) < 1e-15
            assert calskymodel_skycomponents[p[0]].direction.separation(
                self.components[p[1]].direction).rad < 1e-15

        residual_vis = convert_blockvisibility_to_visibility(residual_vis)
        residual_vis, _, _ = weight_visibility(residual_vis, self.beam)
        dirty, sumwt = invert_function(residual_vis, self.beam, context='2d')
        export_image_to_fits(
            dirty, "%s/test_skymodel-final-iso-residual.fits" % self.dir)

        qa = qa_image(dirty)
        assert qa.data['rms'] < 3.4e-3, qa
Ejemplo n.º 16
0
 def test_tapering_Gaussian(self):
     self.actualSetUp()
     size_required = 0.003542
     self.componentvis, _, _ = weight_visibility(self.componentvis,
                                                 self.model,
                                                 algoritm='uniform')
     self.componentvis = taper_visibility_gaussian(self.componentvis,
                                                   beam=size_required)
     psf, sumwt = invert_2d(self.componentvis, self.model, dopsf=True)
     export_image_to_fits(
         psf, '%s/test_weighting_gaussian_taper_psf.fits' % self.dir)
     xfr = fft_image(psf)
     xfr.data = xfr.data.real.astype('float')
     export_image_to_fits(
         xfr, '%s/test_weighting_gaussian_taper_xfr.fits' % self.dir)
     npixel = psf.data.shape[3]
     sl = slice(npixel // 2 - 7, npixel // 2 + 8)
     fit = fit_2dgaussian(psf.data[0, 0, sl, sl])
     if fit.x_stddev <= 0.0 or fit.y_stddev <= 0.0:
         raise ValueError('Error in fitting to psf')
     # fit_2dgaussian returns sqrt of variance. We need to convert that to FWHM.
     # https://en.wikipedia.org/wiki/Full_width_at_half_maximum
     scale_factor = numpy.sqrt(8 * numpy.log(2.0))
     size = numpy.sqrt(fit.x_stddev * fit.y_stddev) * scale_factor
     # Now we need to convert to radians
     size *= numpy.pi * self.model.wcs.wcs.cdelt[1] / 180.0
     # Very impressive! Desired 0.01 Acheived 0.0100006250829
     assert numpy.abs(size - size_required) < 0.001 * size_required, \
         "Fit should be %f, actually is %f" % (size_required, size)
Ejemplo n.º 17
0
    def _invert_base(self,
                     context,
                     extra='',
                     fluxthreshold=1.0,
                     positionthreshold=1.0,
                     check_components=True,
                     facets=1,
                     vis_slices=1,
                     **kwargs):

        dirty = invert_component(self.vis_list,
                                 self.model_graph,
                                 context=context,
                                 dopsf=False,
                                 normalize=True,
                                 facets=facets,
                                 vis_slices=vis_slices,
                                 **kwargs)[0]
        dirty = arlexecute.compute(dirty, sync=True)

        export_image_to_fits(
            dirty[0], '%s/test_imaging_invert_%s%s_%s_dirty.fits' %
            (self.dir, context, extra, arlexecute.type()))

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

        if check_components:
            self._checkcomponents(dirty[0], fluxthreshold, positionthreshold)
 def test_griddata_invert_aterm_noover(self):
     self.actualSetUp(zerow=True)
     make_pb = functools.partial(create_pb_generic,
                                 diameter=35.0,
                                 blockage=0.0,
                                 use_local=False)
     pb = make_pb(self.model)
     if self.persist:
         export_image_to_fits(pb,
                              "%s/test_gridding_aterm_pb.fits" % self.dir)
     gcf, cf = create_awterm_convolutionfunction(self.model,
                                                 make_pb=make_pb,
                                                 nw=1,
                                                 oversampling=1,
                                                 support=16,
                                                 use_aaf=True)
     griddata = create_griddata_from_image(self.model)
     griddata, sumwt = grid_visibility_to_griddata(self.vis,
                                                   griddata=griddata,
                                                   cf=cf)
     im = fft_griddata_to_image(griddata, gcf)
     im = normalize_sumwt(im, sumwt)
     if self.persist:
         export_image_to_fits(
             im, '%s/test_gridding_dirty_aterm_noover.fits' % self.dir)
     self.check_peaks(im, 97.10594988491549)
Ejemplo n.º 19
0
    def test_invert(self):

        uvfitsfile = arl_path("data/vis/ASKAP_example.fits")

        nchan_ave = 32
        nchan = 192
        for schan in range(0, nchan, nchan_ave):
            max_chan = min(nchan, schan + nchan_ave)
            bv = create_blockvisibility_from_uvfits(uvfitsfile,
                                                    range(schan, max_chan))[0]
            vis = convert_blockvisibility_to_visibility(bv)
            from processing_components.visibility.operations import convert_visibility_to_stokesI
            vis = convert_visibility_to_stokesI(vis)
            model = create_image_from_visibility(
                vis,
                npixel=256,
                polarisation_frame=PolarisationFrame('stokesI'))
            dirty, sumwt = invert_2d(vis, model, context='2d')
            assert (numpy.max(numpy.abs(dirty.data))) > 0.0
            assert dirty.shape == (nchan_ave, 1, 256, 256)
            import matplotlib.pyplot as plt
            from processing_components.image.operations import show_image
            show_image(dirty)
            plt.show()
            if self.persist:
                export_image_to_fits(
                    dirty, '%s/test_visibility_uvfits_dirty.fits' % self.dir)
Ejemplo n.º 20
0
 def test_create_primary_beams_AZELGEO(self):
     self.createVis()
     for telescope in ['VLA', 'ASKAP', 'MID', 'MID_GAUSS', 'MID_GRASP', 'LOW']:
         model = create_image_from_visibility(self.vis, cellsize=0.001, override_cellsize=False)
         beam = create_pb(model, telescope=telescope, use_local=True)
         assert numpy.max(beam.data) > 0.0
         export_image_to_fits(beam, "%s/test_primary_beam_AZELGEO_%s.fits" % (self.dir, telescope))
    def test_griddata_invert_wterm(self):
        self.actualSetUp(zerow=False)
        gcf, cf = create_awterm_convolutionfunction(self.model,
                                                    nw=100,
                                                    wstep=8.0,
                                                    oversampling=8,
                                                    support=32,
                                                    use_aaf=True)

        cf_image = convert_convolutionfunction_to_image(cf)
        cf_image.data = numpy.real(cf_image.data)
        if self.persist:
            export_image_to_fits(cf_image,
                                 "%s/test_gridding_wterm_cf.fits" % self.dir)

        griddata = create_griddata_from_image(self.model, nw=1)
        griddata, sumwt = grid_visibility_to_griddata(self.vis,
                                                      griddata=griddata,
                                                      cf=cf)
        im = fft_griddata_to_image(griddata, gcf)
        im = normalize_sumwt(im, sumwt)
        if self.persist:
            export_image_to_fits(
                im, '%s/test_gridding_dirty_wterm.fits' % self.dir)
        self.check_peaks(im, 97.13215242859648)
Ejemplo n.º 22
0
    def test_create_low_test_beam(self):
        im = create_test_image(
            canonical=True,
            cellsize=0.002,
            frequency=numpy.array([1e8 - 5e7, 1e8, 1e8 + 5e7]),
            channel_bandwidth=numpy.array([5e7, 5e7, 5e7]),
            polarisation_frame=PolarisationFrame("stokesIQUV"),
            phasecentre=self.phasecentre)
        bm = create_low_test_beam(model=im)
        export_image_to_fits(bm,
                             '%s/test_test_support_low_beam.fits' % (self.dir))

        assert bm.data.shape[0] == 3
        assert bm.data.shape[1] == 4
        assert bm.data.shape[2] == im.data.shape[2]
        assert bm.data.shape[3] == im.data.shape[3]
        # Check to see if the beam scales as expected
        for i in [30, 40]:
            assert numpy.max(
                numpy.abs(bm.data[0, 0, 128, 128 - 2 * i] -
                          bm.data[1, 0, 128, 128 - i])) < 0.02
            assert numpy.max(
                numpy.abs(bm.data[0, 0, 128, 128 - 3 * i] -
                          bm.data[2, 0, 128, 128 - i])) < 0.02
            assert numpy.max(
                numpy.abs(bm.data[0, 0, 128 - 2 * i, 128] -
                          bm.data[1, 0, 128 - i, 128])) < 0.02
            assert numpy.max(
                numpy.abs(bm.data[0, 0, 128 - 3 * i, 128] -
                          bm.data[2, 0, 128 - i, 128])) < 0.02
Ejemplo n.º 23
0
 def test_deconvolve_hogbom(self):
     self.comp, self.residual = deconvolve_cube(self.dirty, self.psf, niter=10000, gain=0.1, algorithm='hogbom',
                                                threshold=0.01)
     export_image_to_fits(self.residual, "%s/test_deconvolve_hogbom-residual.fits" % (self.dir))
     self.cmodel = restore_cube(self.comp, self.psf, self.residual)
     export_image_to_fits(self.cmodel, "%s/test_deconvolve_hogbom-clean.fits" % (self.dir))
     assert numpy.max(self.residual.data) < 1.2
    def _predict_base(self,
                      fluxthreshold=1.0,
                      gcf=None,
                      cf=None,
                      name='predict_2d',
                      gcfcf=None,
                      **kwargs):

        vis = predict_2d(self.vis, self.model, gcfcf=gcfcf, **kwargs)
        vis.data['vis'] = self.vis.data['vis'] - vis.data['vis']
        dirty = invert_2d(vis,
                          self.model,
                          dopsf=False,
                          normalize=True,
                          gcfcf=gcfcf)

        if self.persist:
            export_image_to_fits(
                dirty[0],
                '%s/test_imaging_%s_residual.fits' % (self.dir, name))
        assert numpy.max(numpy.abs(dirty[0].data)), "Residual image is empty"

        maxabs = numpy.max(numpy.abs(dirty[0].data))
        assert maxabs < fluxthreshold, "Error %.3f greater than fluxthreshold %.3f " % (
            maxabs, fluxthreshold)
    def test_fill_pswf_to_convolutionfunction_nooversampling(self):
        oversampling = 1
        support = 6
        gcf, cf = create_pswf_convolutionfunction(self.image,
                                                  oversampling=oversampling,
                                                  support=support)

        assert numpy.max(numpy.abs(cf.data)) > 0.0
        export_image_to_fits(
            gcf, "%s/test_convolutionfunction_pswf_nooversampling_gcf.fits" %
            self.dir)

        cf_image = convert_convolutionfunction_to_image(cf)
        cf_image.data = numpy.real(cf_image.data)
        export_image_to_fits(
            cf_image,
            "%s/test_convolutionfunction_pwsf_nooversampling_cf.fits" %
            self.dir)

        peak_location = numpy.unravel_index(numpy.argmax(numpy.abs(cf.data)),
                                            cf.shape)
        assert numpy.abs(cf.data[peak_location] - 0.18712109669890536 +
                         0j) < 1e-7, cf.data[peak_location]
        assert peak_location == (0, 0, 0, 0, 0, 3, 3), peak_location
        u_peak, v_peak = cf.grid_wcs.sub([1, 2]).wcs_pix2world(
            peak_location[-2], peak_location[-1], 0)
        assert numpy.abs(u_peak) < 1e-7, u_peak
        assert numpy.abs(v_peak) < 1e-7, u_peak
Ejemplo n.º 26
0
    def _predict_base(self,
                      context='2d',
                      extra='',
                      fluxthreshold=1.0,
                      facets=1,
                      vis_slices=1,
                      **kwargs):
        vis_list = zero_vislist_component(self.vis_list)
        vis_list = predict_component(vis_list,
                                     self.model_graph,
                                     context=context,
                                     vis_slices=vis_slices,
                                     facets=facets,
                                     **kwargs)
        vis_list = subtract_vislist_component(self.vis_list, vis_list)[0]

        vis_list = arlexecute.compute(vis_list, sync=True)

        dirty = invert_component([vis_list], [self.model_graph[0]],
                                 context='2d',
                                 dopsf=False,
                                 normalize=True)[0]
        dirty = arlexecute.compute(dirty, sync=True)

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

        maxabs = numpy.max(numpy.abs(dirty[0].data))
        assert maxabs < fluxthreshold, "Error %.3f greater than fluxthreshold %.3f " % (
            maxabs, fluxthreshold)
Ejemplo n.º 27
0
 def test_deconvolve_hogbom_subpsf(self):
     
     self.comp, self.residual = deconvolve_cube(self.dirty, psf=self.psf, psf_support=200, window='quarter',
                                                niter=10000, gain=0.1, algorithm='hogbom', threshold=0.01)
     export_image_to_fits(self.residual, "%s/test_deconvolve_hogbom_subpsf-residual.fits" % (self.dir))
     self.cmodel = restore_cube(self.comp, self.psf, self.residual)
     export_image_to_fits(self.cmodel, "%s/test_deconvolve_hogbom_subpsf-clean.fits" % (self.dir))
     assert numpy.max(self.residual.data[..., 56:456, 56:456]) < 1.2
 def test_griddata_invert_fast(self):
     self.actualSetUp(zerow=True)
     gcf, cf = create_box_convolutionfunction(self.model)
     griddata = create_griddata_from_image(self.model)
     griddata, sumwt = grid_visibility_to_griddata(self.vis, griddata=griddata, cf=cf)
     im = fft_griddata_to_image(griddata, gcf)
     im = normalize_sumwt(im, sumwt)
     export_image_to_fits(im, '%s/test_gridding_dirty_fast.fits' % self.dir)
     self.check_peaks(im, 96.74492791536595, tol=1e-7)
 def test_griddata_invert_pswf_w(self):
     self.actualSetUp(zerow=False)
     gcf, cf = create_pswf_convolutionfunction(self.model, support=6, oversampling=32)
     griddata = create_griddata_from_image(self.model)
     griddata, sumwt = grid_visibility_to_griddata(self.vis, griddata=griddata, cf=cf)
     im = fft_griddata_to_image(griddata, gcf)
     im = normalize_sumwt(im, sumwt)
     export_image_to_fits(im, '%s/test_gridding_dirty_pswf_w.fits' % self.dir)
     self.check_peaks(im, 96.62754566597258, tol=1e-7)
Ejemplo n.º 30
0
 def test_create_test_image_from_s3_low(self):
     im = create_test_image_from_s3(npixel=1024, channel_bandwidth=numpy.array([1e6]),
                                    frequency=numpy.array([1e8]),
                                    phasecentre=self.phasecentre, fov=10)
     assert im.data.shape[0] == 1
     assert im.data.shape[1] == 1
     assert im.data.shape[2] == 1024
     assert im.data.shape[3] == 1024
     export_image_to_fits(im, '%s/test_test_support_low_s3.fits' % (self.dir))
def main():
    """Run the workflow."""
    init_logging()

    LOG.info("Starting imaging-pipeline")

    # Read parameters
    PARFILE = 'parameters.json'
    if len(sys.argv) > 1:
       PARFILE = sys.argv[1]
    LOG.info("JSON parameter file = %s", PARFILE)
    try: 	
       with open(PARFILE, "r") as par_file:
             jspar = json.load(par_file)       
    except AssertionError as error:
       LOG.critical('ERROR %s', error)
       return

    # We will use dask
    arlexecute.set_client(get_dask_Client())
    arlexecute.run(init_logging)

    # Import visibility list from HDF5 file
    vis_list = import_blockvisibility_from_hdf5(
        '%s/%s' % (RESULTS_DIR, jspar["files"]["vis_list"]))

    # Now read the BlockVisibilities constructed using a model drawn from GLEAM
    predicted_vislist = import_blockvisibility_from_hdf5(
        '%s/%s' % (RESULTS_DIR, jspar["files"]["predicted_vis_list"]))
    corrupted_vislist = import_blockvisibility_from_hdf5(
        '%s/%s' % (RESULTS_DIR, jspar["files"]["corrupted_vis_list"]))

# Reproduce parameters from the visibility data
    ntimes = vis_list[0].nvis

    phasecentre = vis_list[0].phasecentre
    print(phasecentre)
    polframe = vis_list[0].polarisation_frame.type
    LOG.info("Polarisation Frame of vis_list: %s", polframe)

    wprojection_planes = jspar["advice"]["wprojection_planes"]
    guard_band_image   = jspar["advice"]["guard_band_image"]
    delA               = jspar["advice"]["delA"]
    advice_low = advise_wide_field(vis_list[0], guard_band_image=guard_band_image,
                                   delA=delA,
                                   wprojection_planes=wprojection_planes)
    advice_high = advise_wide_field(vis_list[-1], guard_band_image=guard_band_image,
                                    delA=delA,
                                    wprojection_planes=wprojection_planes)

    vis_slices = advice_low['vis_slices']
    npixel = advice_high['npixels2']
    cellsize = min(advice_low['cellsize'], advice_high['cellsize'])
    
# Recovering frequencies
    fstart = vis_list[0].frequency
    fend = vis_list[-1].frequency
    num_freq_win = len(vis_list)
    frequency = numpy.linspace(fstart, fend, num_freq_win)

# Recovering bandwidths
    channel_bandwidth = numpy.array(num_freq_win * [vis_list[1].frequency - vis_list[0].frequency])
    
    # Get the LSM. This is currently blank.
    model_list = [
        arlexecute.execute(create_image_from_visibility)(
            vis_list[f],
            npixel=npixel,
            frequency=[frequency[f]],
            channel_bandwidth=[channel_bandwidth[f]],
            cellsize=cellsize,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame(polframe))
        for f, freq in enumerate(frequency)
    ]
    # future_predicted_vislist = arlexecute.scatter(predicted_vislist)

    # Create and execute graphs to make the dirty image and PSF
    # LOG.info('About to run invert to get dirty image')
    # dirty_list = invert_component(future_predicted_vislist, model_list,
    #                               context='wstack',
    #                               vis_slices=vis_slices, dopsf=False)
    # dirty_list = arlexecute.compute(dirty_list, sync=True)

    # LOG.info('About to run invert to get PSF')
    # psf_list = invert_component(future_predicted_vislist, model_list,
    #                             context='wstack',
    #                             vis_slices=vis_slices, dopsf=True)
    # psf_list = arlexecute.compute(psf_list, sync=True)

    # Now deconvolve using msclean
    # LOG.info('About to run deconvolve')
    # deconvolve_list, _ = deconvolve_component(
    #     dirty_list, psf_list,
    #     model_imagelist=model_list,
    #     deconvolve_facets=8,
    #     deconvolve_overlap=16,
    #     deconvolve_taper='tukey',
    #     scales=[0, 3, 10],
    #     algorithm='msclean',
    #     niter=1000,
    #     fractional_threshold=0.1,
    #     threshold=0.1,
    #     gain=0.1,
    #     psf_support=64)
    # deconvolved = arlexecute.compute(deconvolve_list, sync=True)

    LOG.info('About to run continuum imaging')
    continuum_imaging_list = continuum_imaging_arlexecute(
        predicted_vislist,
        model_imagelist		= model_list,
        context			= jspar["processing"]["continuum_imaging"]["context"] , #'wstack',
        vis_slices		= vis_slices,
        scales			= jspar["processing"]["continuum_imaging"]["scales"],             #[0, 3, 10],
        algorithm		= jspar["processing"]["continuum_imaging"]["algorithm"],          #'mmclean',
        nmoment			= jspar["processing"]["continuum_imaging"]["nmoment"],            #3,
        niter			= jspar["processing"]["continuum_imaging"]["niter"],		  #1000,
        fractional_threshold	= jspar["processing"]["continuum_imaging"]["fractional_threshold"], #0.1,
        threshold		= jspar["processing"]["continuum_imaging"]["threshold"], 	  #0.1,
        nmajor			= jspar["processing"]["continuum_imaging"]["nmajor"], 		  #5,
        gain			= jspar["processing"]["continuum_imaging"]["gain"],               #0.25,
        deconvolve_facets	= jspar["processing"]["continuum_imaging"]["deconvolve_facets"],  #8,
        deconvolve_overlap	= jspar["processing"]["continuum_imaging"]["deconvolve_overlap"], #16,
        deconvolve_taper	= jspar["processing"]["continuum_imaging"]["deconvolve_taper"],   #'tukey',
        psf_support		= jspar["processing"]["continuum_imaging"]["psf_support"] )        #64)
    result = arlexecute.compute(continuum_imaging_list, sync=True)
    deconvolved = result[0][0]
    residual = result[1][0]
    restored = result[2][0]

    print(qa_image(deconvolved, context='Clean image - no selfcal'))
    print(qa_image(restored, context='Restored clean image - no selfcal'))
    export_image_to_fits(restored,
                         '%s/%s' % (RESULTS_DIR, jspar["files"]["continuum_imaging_restored"]))

    print(qa_image(residual[0], context='Residual clean image - no selfcal'))
    export_image_to_fits(residual[0],
                         '%s/%s' % (RESULTS_DIR, jspar["files"]["continuum_imaging_residual"]))

    controls = create_calibration_controls()

    controls['T']['first_selfcal'] = jspar["processing"]["controls"]["T"]["first_selfcal"]
    controls['G']['first_selfcal'] = jspar["processing"]["controls"]["G"]["first_selfcal"]
    controls['B']['first_selfcal'] = jspar["processing"]["controls"]["B"]["first_selfcal"]

    controls['T']['timescale'] = jspar["processing"]["controls"]["T"]["timescale"]
    controls['G']['timescale'] = jspar["processing"]["controls"]["G"]["timescale"]
    controls['B']['timescale'] = jspar["processing"]["controls"]["B"]["timescale"]

    PP.pprint(controls)

    future_corrupted_vislist = arlexecute.scatter(corrupted_vislist)
    ical_list = ical_arlexecute(future_corrupted_vislist,
        model_imagelist		= model_list,
        context			= jspar["processing"]["ical"]["context"] , 		#'wstack',
        calibration_context	= jspar["processing"]["ical"]["calibration_context"] , 	#'TG',
        controls		= controls,
        vis_slices		= ntimes,
        scales			= jspar["processing"]["ical"]["scales"],             	#[0, 3, 10],
        timeslice		= jspar["processing"]["ical"]["timeslice"],          	#'auto',
        algorithm		= jspar["processing"]["ical"]["algorithm"],          	#'mmclean',
        nmoment			= jspar["processing"]["ical"]["nmoment"],            	#3,
        niter			= jspar["processing"]["ical"]["niter"],		  	#1000,
        fractional_threshold	= jspar["processing"]["ical"]["fractional_threshold"], 	#0.1,
        threshold		= jspar["processing"]["ical"]["threshold"], 	  	#0.1,
        nmajor			= jspar["processing"]["ical"]["nmajor"], 		#5,
        gain			= jspar["processing"]["ical"]["gain"],               	#0.25,
        deconvolve_facets	= jspar["processing"]["ical"]["deconvolve_facets"],  	#8,
        deconvolve_overlap	= jspar["processing"]["ical"]["deconvolve_overlap"], 	#16,
        deconvolve_taper	= jspar["processing"]["ical"]["deconvolve_taper"],   	#'tukey',
        global_solution		= jspar["processing"]["ical"]["global_solution"],    	#False,
        do_selfcal		= jspar["processing"]["ical"]["do_selfcal"],         	#True,
        psf_support		= jspar["processing"]["ical"]["psf_support"])         	#64


    LOG.info('About to run ical')
    result = arlexecute.compute(ical_list, sync=True)
    deconvolved = result[0][0]
    residual = result[1][0]
    restored = result[2][0]

    print(qa_image(deconvolved, context='Clean image'))
    print(qa_image(restored, context='Restored clean image'))
    export_image_to_fits(restored, '%s/%s' % (RESULTS_DIR, jspar["files"]["ical_restored"]))

    print(qa_image(residual[0], context='Residual clean image'))
    export_image_to_fits(residual[0], '%s/%s' % (RESULTS_DIR, jspar["files"]["ical_residual"]))

    arlexecute.close()