processing_library/arrays/cleaners.py
def msclean(dirty, psf, window, gain, thresh, niter, scales, fracthresh, prefix=''):
    """ Perform multiscale clean

    Multiscale CLEAN (IEEE Journal of Selected Topics in Sig Proc, 2008 vol. 2 pp. 793-801)

    This version operates on numpy arrays.

    :param prefix:
    :param fracthresh:
    :param dirty: The dirty image, i.e., the image to be deconvolved
    :param psf: The point spread-function
    :param window: Regions where clean components are allowed. If True, all of the dirty image is allowed
    :param gain: The "loop gain", i.e., the fraction of the brightest pixel that is removed in each iteration
    :param thresh: Cleaning stops when the maximum of the absolute deviation of the residual is less than this value
    :param niter: Maximum number of components to make if the threshold "thresh" is not hit
    :param scales: Scales (in pixels width) to be used
    :return: clean component image, residual image
    """
    
    starttime = time.time()
    assert 0.0 < gain < 2.0
    assert niter > 0
    assert len(scales) > 0

    comps = numpy.zeros(dirty.shape)

    pmax = psf.max()
    assert pmax > 0.0

    # Rescale to unit peak PSF. We undo this at the end of iteration
    psfpeak = argmax(numpy.fabs(psf))
    log.info("msclean %s: Peak of PSF = %s at %s" % (prefix, pmax, psfpeak))
    dmax = dirty.max()
    dpeak = argmax(dirty)
    log.info("msclean %s: Peak of Dirty = %.6f Jy/beam at %s " % (prefix, dmax, dpeak))
    lpsf = psf / pmax
    ldirty = dirty / pmax

    # Create the scale images and form all the various products we need. We
    # use an extra dimension to hold the scale-related images. scalestack is a 3D
    # cube holding the different scale images. convolvestack will take a 2D Image
    # and add a third dimension holding the scale-convolved versions.

    scaleshape = [len(scales), ldirty.shape[0], ldirty.shape[1]]
    scalestack = create_scalestack(scaleshape, scales, norm=True)

    pscaleshape = [len(scales), lpsf.shape[0], lpsf.shape[1]]
    pscalestack = create_scalestack(pscaleshape, scales, norm=True)

    res_scalestack = convolve_scalestack(scalestack, numpy.array(ldirty))
    psf_scalescalestack = convolve_convolve_scalestack(pscalestack, numpy.array(lpsf))

    # Evaluate the coupling matrix between the various scale sizes.
    coupling_matrix = numpy.zeros([len(scales), len(scales)])
    for iscale in numpy.arange(len(scales)):
        for iscale1 in numpy.arange(len(scales)):
            coupling_matrix[iscale, iscale1] = numpy.max(psf_scalescalestack[iscale, iscale1, :, :])
    log.info("msclean %s: Coupling matrix =\n %s" % (prefix, coupling_matrix))

    # The window is scale dependent - we form it by smoothing and thresholding
    # the input window. This prevents components being placed too close to the
    # edge of the Image.

    if window is None:
        windowstack = None
    else:
        windowstack = numpy.zeros_like(scalestack)
        windowstack[convolve_scalestack(scalestack, window) > 0.9] = 1.0

    if windowstack is not None:
        assert numpy.sum(windowstack) > 0

    log.info("msclean %s: Max abs in dirty Image = %.6f Jy/beam" % (prefix, numpy.fabs(res_scalestack[0, :, :]).max()))
    absolutethresh = max(thresh, fracthresh * numpy.fabs(res_scalestack[0, :, :]).max())
    log.info("msclean %s: Start of minor cycle" % prefix)
    log.info("msclean %s: This minor cycle will stop at %d iterations or peak < %.6f (Jy/beam)" %
             (prefix, niter, absolutethresh))

    log.info('msclean %s: Timing for setup: %.3f (s) for dirty shape %s, PSF shape %s , scales %s' %
             (prefix, time.time() - starttime, str(dirty.shape), str(psf.shape), str(scales)))
    starttime = time.time()
    aiter = 0
    for i in range(niter):
        aiter = i + 1
        # Find peak over all smoothed images
        mx, my, mscale = find_max_abs_stack(res_scalestack, windowstack, coupling_matrix)
        # Find the values to subtract, accounting for the coupling matrix
        mval = res_scalestack[mscale, mx, my] / coupling_matrix[mscale, mscale]
        if niter < 10 or i % (niter // 10) == 0:
            log.info("msclean %s: Minor cycle %d, peak %s at [%d, %d, %d]" %
                     (prefix, i, res_scalestack[:, mx, my], mx, my, mscale))
        if numpy.fabs(res_scalestack[mscale, mx, my]) < 0.9 * absolutethresh:
            log.info("msclean %s: At iteration %d, absolute value of peak %.6f is below stopping threshold %.6f"
                     % (prefix, i, numpy.fabs(res_scalestack[mscale, mx, my]), absolutethresh))
            break

        # Update the cached residuals and add to the cached model.
        lhs, rhs = overlapIndices(dirty, psf, mx, my)
        if numpy.abs(mval) > 0:
            # Cross subtract from other scales
            for iscale in range(len(scales)):
                res_scalestack[iscale, lhs[0]:lhs[1], lhs[2]:lhs[3]] -= \
                    psf_scalescalestack[iscale, mscale, rhs[0]:rhs[1], rhs[2]:rhs[3]] * gain * mval
            comps[lhs[0]:lhs[1], lhs[2]:lhs[3]] += \
                pscalestack[mscale, rhs[0]:rhs[1], rhs[2]:rhs[3]] * gain * mval
        else:
            break
            
    log.info("msclean %s: End of minor cycle" % prefix)
    
    dtime = time.time() - starttime
    log.info('msclean %s: Timing for clean: %.3f (s) for dirty shape %s, PSF shape %s , scales %s, %d iterations, '
             'time per clean %.3f (ms)' %
             (prefix, dtime, str(dirty.shape), str(psf.shape), str(scales), aiter, 1000.0 * dtime / aiter))

    return comps, pmax * res_scalestack[0, :, :]
 Perform multiscale clean

    Multiscale CLEAN (IEEE Journal of Selected Topics in Sig Proc, 2008 vol. 2 pp. 793-801)

    This version operates on numpy arrays.

    :param prefix:
    :param fracthresh:
    :param dirty: The dirty image, i.e., the image to be deconvolved
    :param psf: The point spread-function
    :param window: Regions where clean components are allowed. If True, all of the dirty image is allowed
    :param gain: The "loop gain", i.e., the fraction of the brightest pixel that is removed in each iteration
    :param thresh: Cleaning stops when the maximum of the absolute deviation of the residual is less than this value
    :param niter: Maximum number of components to make if the threshold "thresh" is not hit
    :param scales: Scales (in pixels width) to be used
    :return: clean component image, residual image
    
Perform multiscale clean
Multiscale CLEAN

This version operates on numpy arrays.
Perform multiscale clean Multiscale CLEAN This version operates on numpy arrays .
def msclean(dirty, psf, window, gain, thresh, niter, scales, fracthresh, prefix=''):
    starttime = time.time()
    assert 0.0 < gain < 2.0
    assert niter > 0
    assert len(scales) > 0
    comps = numpy.zeros(dirty.shape)
    pmax = psf.max()
    assert pmax > 0.0
    psfpeak = argmax(numpy.fabs(psf))
    log.info("msclean %s: Peak of PSF = %s at %s" % (prefix, pmax, psfpeak))
    dmax = dirty.max()
    dpeak = argmax(dirty)
    log.info("msclean %s: Peak of Dirty = %.6f Jy/beam at %s " % (prefix, dmax, dpeak))
    lpsf = psf / pmax
    ldirty = dirty / pmax
    scaleshape = [len(scales), ldirty.shape[0], ldirty.shape[1]]
    scalestack = create_scalestack(scaleshape, scales, norm=True)
    pscaleshape = [len(scales), lpsf.shape[0], lpsf.shape[1]]
    pscalestack = create_scalestack(pscaleshape, scales, norm=True)
    res_scalestack = convolve_scalestack(scalestack, numpy.array(ldirty))
    psf_scalescalestack = convolve_convolve_scalestack(pscalestack, numpy.array(lpsf))
    coupling_matrix = numpy.zeros([len(scales), len(scales)])
    for iscale in numpy.arange(len(scales)):
        for iscale1 in numpy.arange(len(scales)):
            coupling_matrix[iscale, iscale1] = numpy.max(psf_scalescalestack[iscale, iscale1, :, :])
    log.info("msclean %s: Coupling matrix =\n %s" % (prefix, coupling_matrix))
    if window is None:
        windowstack = None
    else:
        windowstack = numpy.zeros_like(scalestack)
        windowstack[convolve_scalestack(scalestack, window) > 0.9] = 1.0
    if windowstack is not None:
        assert numpy.sum(windowstack) > 0
    log.info("msclean %s: Max abs in dirty Image = %.6f Jy/beam" % (prefix, numpy.fabs(res_scalestack[0, :, :]).max()))
    absolutethresh = max(thresh, fracthresh * numpy.fabs(res_scalestack[0, :, :]).max())
    log.info("msclean %s: Start of minor cycle" % prefix)
    log.info("msclean %s: This minor cycle will stop at %d iterations or peak < %.6f (Jy/beam)" %
             (prefix, niter, absolutethresh))
    log.info('msclean %s: Timing for setup: %.3f (s) for dirty shape %s, PSF shape %s , scales %s' %
             (prefix, time.time() - starttime, str(dirty.shape), str(psf.shape), str(scales)))
    starttime = time.time()
    aiter = 0
    for i in range(niter):
        aiter = i + 1
        mx, my, mscale = find_max_abs_stack(res_scalestack, windowstack, coupling_matrix)
        mval = res_scalestack[mscale, mx, my] / coupling_matrix[mscale, mscale]
        if niter < 10 or i % (niter // 10) == 0:
            log.info("msclean %s: Minor cycle %d, peak %s at [%d, %d, %d]" %
                     (prefix, i, res_scalestack[:, mx, my], mx, my, mscale))
        if numpy.fabs(res_scalestack[mscale, mx, my]) < 0.9 * absolutethresh:
            log.info("msclean %s: At iteration %d, absolute value of peak %.6f is below stopping threshold %.6f"
                     % (prefix, i, numpy.fabs(res_scalestack[mscale, mx, my]), absolutethresh))
            break
        lhs, rhs = overlapIndices(dirty, psf, mx, my)
        if numpy.abs(mval) > 0:
            for iscale in range(len(scales)):
                res_scalestack[iscale, lhs[0]:lhs[1], lhs[2]:lhs[3]] -= \
                    psf_scalescalestack[iscale, mscale, rhs[0]:rhs[1], rhs[2]:rhs[3]] * gain * mval
            comps[lhs[0]:lhs[1], lhs[2]:lhs[3]] += \
                pscalestack[mscale, rhs[0]:rhs[1], rhs[2]:rhs[3]] * gain * mval
        else:
            break
    log.info("msclean %s: End of minor cycle" % prefix)
    dtime = time.time() - starttime
    log.info('msclean %s: Timing for clean: %.3f (s) for dirty shape %s, PSF shape %s , scales %s, %d iterations, '
             'time per clean %.3f (ms)' %
             (prefix, dtime, str(dirty.shape), str(psf.shape), str(scales), aiter, 1000.0 * dtime / aiter))
    return comps, pmax * res_scalestack[0, :, :]
def msclean ( dirty , psf , window , gain , thresh , niter , scales , fracthresh , prefix = '' ) : starttime = time . time ( ) assert 0.0 < gain < 2.0 assert niter > 0 assert len ( scales ) > 0 comps = numpy . zeros ( dirty . shape ) pmax = psf . max ( ) assert pmax > 0.0 psfpeak = argmax ( numpy . fabs ( psf ) ) log . info ( "msclean %s: Peak of PSF = %s at %s" % ( prefix , pmax , psfpeak ) ) dmax = dirty . max ( ) dpeak = argmax ( dirty ) log . info ( "msclean %s: Peak of Dirty = %.6f Jy/beam at %s " % ( prefix , dmax , dpeak ) ) lpsf = psf / pmax ldirty = dirty / pmax scaleshape = [ len ( scales ) , ldirty . shape [ 0 ] , ldirty . shape [ 1 ] ] scalestack = create_scalestack ( scaleshape , scales , norm = True ) pscaleshape = [ len ( scales ) , lpsf . shape [ 0 ] , lpsf . shape [ 1 ] ] pscalestack = create_scalestack ( pscaleshape , scales , norm = True ) res_scalestack = convolve_scalestack ( scalestack , numpy . array ( ldirty ) ) psf_scalescalestack = convolve_convolve_scalestack ( pscalestack , numpy . array ( lpsf ) ) coupling_matrix = numpy . zeros ( [ len ( scales ) , len ( scales ) ] ) for iscale in numpy . arange ( len ( scales ) ) : for iscale1 in numpy . arange ( len ( scales ) ) : coupling_matrix [ iscale , iscale1 ] = numpy . max ( psf_scalescalestack [ iscale , iscale1 , : , : ] ) log . info ( "msclean %s: Coupling matrix =\n %s" % ( prefix , coupling_matrix ) ) if window is None : windowstack = None else : windowstack = numpy . zeros_like ( scalestack ) windowstack [ convolve_scalestack ( scalestack , window ) > 0.9 ] = 1.0 if windowstack is not None : assert numpy . sum ( windowstack ) > 0 log . info ( "msclean %s: Max abs in dirty Image = %.6f Jy/beam" % ( prefix , numpy . fabs ( res_scalestack [ 0 , : , : ] ) . max ( ) ) ) absolutethresh = max ( thresh , fracthresh * numpy . fabs ( res_scalestack [ 0 , : , : ] ) . max ( ) ) log . info ( "msclean %s: Start of minor cycle" % prefix ) log . info ( "msclean %s: This minor cycle will stop at %d iterations or peak < %.6f (Jy/beam)" % ( prefix , niter , absolutethresh ) ) log . info ( 'msclean %s: Timing for setup: %.3f (s) for dirty shape %s, PSF shape %s , scales %s' % ( prefix , time . time ( ) - starttime , str ( dirty . shape ) , str ( psf . shape ) , str ( scales ) ) ) starttime = time . time ( ) aiter = 0 for i in range ( niter ) : aiter = i + 1 mx , my , mscale = find_max_abs_stack ( res_scalestack , windowstack , coupling_matrix ) mval = res_scalestack [ mscale , mx , my ] / coupling_matrix [ mscale , mscale ] if niter < 10 or i % ( niter // 10 ) == 0 : log . info ( "msclean %s: Minor cycle %d, peak %s at [%d, %d, %d]" % ( prefix , i , res_scalestack [ : , mx , my ] , mx , my , mscale ) ) if numpy . fabs ( res_scalestack [ mscale , mx , my ] ) < 0.9 * absolutethresh : log . info ( "msclean %s: At iteration %d, absolute value of peak %.6f is below stopping threshold %.6f" % ( prefix , i , numpy . fabs ( res_scalestack [ mscale , mx , my ] ) , absolutethresh ) ) break lhs , rhs = overlapIndices ( dirty , psf , mx , my ) if numpy . abs ( mval ) > 0 : for iscale in range ( len ( scales ) ) : res_scalestack [ iscale , lhs [ 0 ] : lhs [ 1 ] , lhs [ 2 ] : lhs [ 3 ] ] -= psf_scalescalestack [ iscale , mscale , rhs [ 0 ] : rhs [ 1 ] , rhs [ 2 ] : rhs [ 3 ] ] * gain * mval comps [ lhs [ 0 ] : lhs [ 1 ] , lhs [ 2 ] : lhs [ 3 ] ] += pscalestack [ mscale , rhs [ 0 ] : rhs [ 1 ] , rhs [ 2 ] : rhs [ 3 ] ] * gain * mval else : break log . info ( "msclean %s: End of minor cycle" % prefix ) dtime = time . time ( ) - starttime log . info ( 'msclean %s: Timing for clean: %.3f (s) for dirty shape %s, PSF shape %s , scales %s, %d iterations, ' 'time per clean %.3f (ms)' % ( prefix , dtime , str ( dirty . shape ) , str ( psf . shape ) , str ( scales ) , aiter , 1000.0 * dtime / aiter ) ) return comps , pmax * res_scalestack [ 0 , : , : ]
Perform multiscale clean
Multiscale CLEAN (IEEE Journal of Selected Topics in Sig Proc, 2008 vol.
Perform multiscale clean Multiscale CLEAN ( IEEE Journal of Selected Topics in Sig Proc 2008 vol .
""" Perform multiscale clean

    Multiscale CLEAN (IEEE Journal of Selected Topics in Sig Proc, 2008 vol. 2 pp. 793-801)

    This version operates on numpy arrays.

    :param prefix:
    :param fracthresh:
    :param dirty: The dirty image, i.e., the image to be deconvolved
    :param psf: The point spread-function
    :param window: Regions where clean components are allowed. If True, all of the dirty image is allowed
    :param gain: The "loop gain", i.e., the fraction of the brightest pixel that is removed in each iteration
    :param thresh: Cleaning stops when the maximum of the absolute deviation of the residual is less than this value
    :param niter: Maximum number of components to make if the threshold "thresh" is not hit
    :param scales: Scales (in pixels width) to be used
    :return: clean component image, residual image
    """ # Rescale to unit peak PSF. We undo this at the end of iteration # Create the scale images and form all the various products we need. We # use an extra dimension to hold the scale-related images. scalestack is a 3D # cube holding the different scale images. convolvestack will take a 2D Image # and add a third dimension holding the scale-convolved versions. # Evaluate the coupling matrix between the various scale sizes. # The window is scale dependent - we form it by smoothing and thresholding # the input window. This prevents components being placed too close to the # edge of the Image. # Find peak over all smoothed images # Find the values to subtract, accounting for the coupling matrix # Update the cached residuals and add to the cached model. # Cross subtract from other scales
clean component image, residual image
clean component image residual image
dirty
The dirty image, i.e., the image to be deconvolved
The dirty image i . e . the image to be deconvolved
psf
The point spread-function
The point spread - function
window
Regions where clean components are allowed. If True, all of the dirty image is allowed
Regions where clean components are allowed . If True all of the dirty image is allowed
gain
The "loop gain", i.e., the fraction of the brightest pixel that is removed in each iteration
The " loop gain " i . e . the fraction of the brightest pixel that is removed in each iteration
thresh
Cleaning stops when the maximum of the absolute deviation of the residual is less than this value
Cleaning stops when the maximum of the absolute deviation of the residual is less than this value
niter
Maximum number of components to make if the threshold "thresh" is not hit
Maximum number of components to make if the threshold " thresh " is not hit
scales
Scales (in pixels width) to be used
Scales ( in pixels width ) to be used
fracthresh
None
prefix
None