def test_scatter_gather_facet(self):

        m31original = create_test_image(
            polarisation_frame=PolarisationFrame('stokesI'))
        assert numpy.max(numpy.abs(m31original.data)), "Original is empty"

        for nraster in [1, 4, 8]:
            m31model = create_test_image(
                polarisation_frame=PolarisationFrame('stokesI'))
            image_list = image_scatter_facets(m31model, facets=nraster)
            for patch in image_list:
                assert patch.data.shape[3] == (m31model.data.shape[3] // nraster), \
                    "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[3],
                                                                                (m31model.data.shape[3] // nraster))
                assert patch.data.shape[2] == (m31model.data.shape[2] // nraster), \
                    "Number of pixels in each patch: %d not as expected: %d" % (patch.data.shape[2],
                                                                                (m31model.data.shape[2] // nraster))
                patch.data[...] = 1.0
            m31reconstructed = create_empty_image_like(m31model)
            m31reconstructed = image_gather_facets(image_list,
                                                   m31reconstructed,
                                                   facets=nraster)
            flat = image_gather_facets(image_list,
                                       m31reconstructed,
                                       facets=nraster,
                                       return_flat=True)

            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
 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
    # Create test image
    frequency = numpy.array([1e8])
    phasecentre = SkyCoord(ra=+15.0 * u.deg,
                           dec=-35.0 * u.deg,
                           frame='icrs',
                           equinox='J2000')
    model = create_test_image(frequency=frequency,
                              phasecentre=phasecentre,
                              cellsize=0.001,
                              polarisation_frame=PolarisationFrame('stokesI'))

    # Rank 0 scatters the test image
    if rank == 0:
        subimages = image_scatter_facets(model, facets=facets)
        subimages = numpy.array_split(subimages, size)
    else:
        subimages = list()

    sublist = comm.scatter(subimages, root=0)

    root_images = imagerooter(sublist)

    roots = comm.gather(root_images, root=0)

    if rank == 0:
        results = sum(roots, [])
        root_model = create_empty_image_like(model)
        result = image_gather_facets(results, root_model, facets=facets)
        numpy.testing.assert_array_almost_equal_nulp(result.data**2,
                                                     numpy.abs(model.data), 7)
예제 #4
0
def deconvolve_list_serial_workflow(dirty_list,
                                    psf_list,
                                    model_imagelist,
                                    prefix='',
                                    **kwargs):
    """Create a graph for deconvolution, adding to the model

    :param dirty_list:
    :param psf_list:
    :param model_imagelist:
    :param kwargs: Parameters for functions in components
    :return: (graph for the deconvolution, graph for the flat)
    """
    nchan = len(dirty_list)

    def deconvolve(dirty, psf, model, facet, gthreshold):
        import time
        starttime = time.time()
        if prefix == '':
            lprefix = "facet %d" % facet
        else:
            lprefix = "%s, facet %d" % (prefix, facet)

        nmoments = get_parameter(kwargs, "nmoments", 0)

        if nmoments > 0:
            moment0 = calculate_image_frequency_moments(dirty)
            this_peak = numpy.max(numpy.abs(
                moment0.data[0, ...])) / dirty.data.shape[0]
        else:
            this_peak = numpy.max(numpy.abs(dirty.data[0, ...]))

        if this_peak > 1.1 * gthreshold:
            log.info(
                "deconvolve_list_serial_workflow %s: cleaning - peak %.6f > 1.1 * threshold %.6f"
                % (lprefix, this_peak, gthreshold))
            kwargs['threshold'] = gthreshold
            result, _ = deconvolve_cube(dirty, psf, prefix=lprefix, **kwargs)

            if result.data.shape[0] == model.data.shape[0]:
                result.data += model.data
            else:
                log.warning(
                    "deconvolve_list_serial_workflow %s: Initial model %s and clean result %s do not have the same shape"
                    % (lprefix, str(
                        model.data.shape[0]), str(result.data.shape[0])))

            flux = numpy.sum(result.data[0, 0, ...])
            log.info(
                '### %s, %.6f, %.6f, True, %.3f # cycle, facet, peak, cleaned flux, clean, time?'
                % (lprefix, this_peak, flux, time.time() - starttime))

            return result
        else:
            log.info(
                "deconvolve_list_serial_workflow %s: Not cleaning - peak %.6f <= 1.1 * threshold %.6f"
                % (lprefix, this_peak, gthreshold))
            log.info(
                '### %s, %.6f, %.6f, False, %.3f # cycle, facet, peak, cleaned flux, clean, time?'
                % (lprefix, this_peak, 0.0, time.time() - starttime))

            return copy_image(model)

    deconvolve_facets = get_parameter(kwargs, 'deconvolve_facets', 1)
    deconvolve_overlap = get_parameter(kwargs, 'deconvolve_overlap', 0)
    deconvolve_taper = get_parameter(kwargs, 'deconvolve_taper', None)
    if deconvolve_overlap > 0:
        deconvolve_number_facets = (deconvolve_facets - 2)**2
    else:
        deconvolve_number_facets = deconvolve_facets**2

    model_imagelist = image_gather_channels(model_imagelist)

    # Scatter the separate channel images into deconvolve facets and then gather channels for each facet.
    # This avoids constructing the entire spectral cube.
    #    dirty_list = remove_sumwt, nout=nchan)(dirty_list)
    scattered_channels_facets_dirty_list = \
        [image_scatter_facets(d[0], facets=deconvolve_facets,
                              overlap=deconvolve_overlap,
                              taper=deconvolve_taper)
         for d in dirty_list]

    # Now we do a transpose and gather
    scattered_facets_list = [
        image_gather_channels([
            scattered_channels_facets_dirty_list[chan][facet]
            for chan in range(nchan)
        ]) for facet in range(deconvolve_number_facets)
    ]

    psf_list = remove_sumwt(psf_list)
    psf_list = image_gather_channels(psf_list)

    scattered_model_imagelist = \
        image_scatter_facets(model_imagelist,
                             facets=deconvolve_facets,
                             overlap=deconvolve_overlap)

    # Work out the threshold. Need to find global peak over all dirty_list images
    threshold = get_parameter(kwargs, "threshold", 0.0)
    fractional_threshold = get_parameter(kwargs, "fractional_threshold", 0.1)
    nmoments = get_parameter(kwargs, "nmoments", 0)
    use_moment0 = nmoments > 0

    # Find the global threshold. This uses the peak in the average on the frequency axis since we
    # want to use it in a stopping criterion in a moment clean
    global_threshold = threshold_list(scattered_facets_list,
                                      threshold,
                                      fractional_threshold,
                                      use_moment0=use_moment0,
                                      prefix=prefix)

    facet_list = numpy.arange(deconvolve_number_facets).astype('int')
    scattered_results_list = [
        deconvolve(d, psf_list, m, facet, global_threshold) for d, m, facet in
        zip(scattered_facets_list, scattered_model_imagelist, facet_list)
    ]

    # Gather the results back into one image, correcting for overlaps as necessary. The taper function is is used to
    # feather the facets together
    gathered_results_list = image_gather_facets(scattered_results_list,
                                                model_imagelist,
                                                facets=deconvolve_facets,
                                                overlap=deconvolve_overlap,
                                                taper=deconvolve_taper)
    flat_list = image_gather_facets(scattered_results_list,
                                    model_imagelist,
                                    facets=deconvolve_facets,
                                    overlap=deconvolve_overlap,
                                    taper=deconvolve_taper,
                                    return_flat=True)

    return image_scatter_channels(gathered_results_list,
                                  subimages=nchan), flat_list