Exemplo n.º 1
0
def w_cache_predict(theta, lam, uvw, src, guv,
                    wstep=2000,
                    kernel_cache=None,
                    kernel_fn=w_kernel,
                    **kwargs):
    """Predict visibilities using w-kernel cache

    :param theta: Field of view (directional cosines)
    :param lam: UV grid range (wavelenghts)
    :param uvw: UVWs of visibilities  (wavelengths)
    :param guv: Input uv grid to de-grid from
    :param wstep: Size of w-bins (wavelengths)
    :param kernel_cache: Kernel cache. If not passed, we fall back
       to `kernel_fn`. See `w_cache_imaging` for details.
    :param kernel_fn: Function for generating the kernels. Parameters
       `(theta, w, **kwargs)`. Default `w_kernel`.
    :returns: degridded visibilities
    """

    if kernel_cache is None:
        kernel_cache = pylru.FunctionCacheManager(kernel_fn, 1)
    def kernel_binner(theta, w, **kw):
        wbin = wstep * numpy.round(w / wstep)
        return kernel_cache(theta, wbin, **kw)
    v = numpy.ndarray(nv, dtype=complex)
    for p, s in zip(uvw, src):
        wbin = wstep * numpy.round(p[2] / wstep)
        wg = kernel_cache(theta, wbin, *s, **kwargs)
        v[ixs] = convdegrid(wg, guv, numpy.array([p / lam]))
    return v
Exemplo n.º 2
0
def w_cache_predict(theta,
                    lam,
                    p,
                    guv,
                    wstep=2000,
                    kernel_cache=None,
                    kernel_fn=w_kernel,
                    **kwargs):
    """Predict visibilities using w-kernel cache

    :param theta: Field of view (directional cosines)
    :param lam: UV grid range (wavelenghts)
    :param p: UVWs of visibilities  (wavelengths)
    :param guv: Input uv grid to de-grid from
    :param wstep: Size of w-bins (wavelengths)
    :param kernel_cache: Kernel cache. If not passed, we fall back
       to `kernel_fn`. See `w_cache_imaging` for details.
    :param kernel_fn: Function for generating the kernels. Parameters
       `(theta, w, **kwargs)`. Default `w_kernel`.
    :returns: degridded visibilities
    """

    if kernel_cache is None:
        kernel_cache = pylru.FunctionCacheManager(kernel_fn, 1000)

    def kernel_binner(theta, w, **kw):
        wbin = wstep * numpy.round(w / wstep)
        return kernel_cache(theta, wbin, **kw)

    return w_slice_predict(theta, lam, p, guv, 1, kernel_binner, **kwargs)
Exemplo n.º 3
0
def w_cache_imaging(theta,
                    lam,
                    p,
                    v,
                    wstep=2000,
                    kernel_cache=None,
                    kernel_fn=w_kernel,
                    **kwargs):
    """Basic w-projection by caching convolution arl in w

    A simple cache can be constructed externally and passed in:

      kernel_cache = pylru.FunctionCacheManager(w_kernel, cachesize)

    If applicable, consider wrapping in `w_conj_kernel_fn` to improve
    effectiveness further.

    :param theta: Field of view (directional cosines)
    :param lam: UV grid range (wavelenghts)
    :param p: UVWs of visibilities (wavelengths)
    :param v: Visibilities to be imaged
    :param wstep: Size of w-bins (wavelengths)
    :param kernel_cache: Kernel cache. If not passed, we fall back
       to `kernel_fn`.
    :param kernel_fn: Function for generating the kernels. Parameters
       `(theta, w, **kwargs)`. Default `w_kernel`.
    :returns: UV grid

    """

    # Construct default cache, if needed. As visibilities are
    # traversed in w-order it only needs to hold the last w-kernel.
    if kernel_cache is None:
        kernel_cache = pylru.FunctionCacheManager(kernel_fn, 1000)
    # Bin w values, then run slice imager with slice size of 1
    def kernel_binner(theta, w, **kw):
        wbin = wstep * numpy.round(w / wstep)
        return kernel_cache(theta, wbin, **kw)

    N = int(round(theta * lam))
    assert N > 1
    guv = numpy.zeros([N, N], dtype=complex)
    for ps, vs in slices:
        w = numpy.mean(ps[:, 2])
        wg = numpy.conj(kernel_fn(theta, w, **kwargs))
        convolutional_grid(wg, guv, ps / lam, vs)
    return guv
Exemplo n.º 4
0
def w_cache_imaging(theta,
                    lam,
                    uvw,
                    src,
                    vis,
                    wstep=2000,
                    kernel_cache=None,
                    kernel_fn=w_kernel,
                    **kwargs):
    """Basic w-projection by caching convolution arl in w

    A simple cache can be constructed externally and passed in:

      kernel_cache = pylru.FunctionCacheManager(w_kernel, cachesize)

    If applicable, consider wrapping in `w_conj_kernel_fn` to improve
    effectiveness further.

    :param theta: Field of view (directional cosines)
    :param lam: UV grid range (wavelenghts)
    :param uvw: UVWs of visibilities (wavelengths)
    :param src: Visibility source information (various)
    :param vis: Visibilites to be imaged
    :param wstep: Size of w-bins (wavelengths)
    :param kernel_cache: Kernel cache. If not passed, we fall back
       to `kernel_fn`.
    :param kernel_fn: Function for generating the kernels. Parameters
       `(theta, w, **kwargs)`. Default `w_kernel`.
    :returns: UV grid

    """

    # Construct default cache, if needed. As visibilities are
    # traversed in w-order it only needs to hold the last w-kernel.
    if kernel_cache is None:
        kernel_cache = pylru.FunctionCacheManager(kernel_fn, 1)

    N = int(round(theta * lam))
    guv = numpy.zeros([N, N], dtype=complex)
    for p, s, v in zip(uvw, src, vis):
        wbin = wstep * numpy.round(p[2] / wstep)
        wg = kernel_cache(theta, wbin, *s, **kwargs)
        convgrid(wg, guv, numpy.array([p / lam]), [()], numpy.array([v]))
    return guv
Exemplo n.º 5
0
def invert_visibility(vis: Visibility, params={}):
    """Invert to make dirty Image and PSF

    :param vis:
    :type Visibility: Visibility to be processed
    :returns: (dirty image, psf)
    """
    log_parameters(params)
    log.debug("invert_visibility: Inverting Visibility to make dirty and psf")
    shape, reffrequency, cellsize, w, imagecentre = create_wcs_from_visibility(vis, params=params)

    npixel = shape[3]
    theta = npixel * cellsize

    log.debug("invert_visibility: Specified npixel=%d, cellsize = %f rad, FOV = %f rad" %
          (npixel, cellsize, theta))

    # Set up the gridding kernel. We try to use a cached version
    gridding_algorithm = get_parameter(params, 'gridding_algorithm', 'wprojection')

    if gridding_algorithm == 'wprojection':
        log.debug("invert_visibility: Gridding by w projection")

        wstep = get_parameter(params, "wstep", 10000.0)
        wcachesize = w_cache_size(vis, wstep)
        log.debug("invert_visibility: Making w-kernel cache of %d kernels" % wcachesize)

        cache_fn = w_conj_kernel_fn(pylru.FunctionCacheManager(w_kernel, wcachesize))
        imgfn = functools.partial(w_cache_imaging,
                                  wstep=wstep, kernel_cache=cache_fn,
                                  NpixFF=256, NpixKern=15, Qpx=4)
    else:
        raise NotImplementedError("gridding algorithm %s not supported" % gridding_algorithm)

    # Apply a phase rotation from the visibility phase centre to the image phase centre
    #    visphaserotate = phaserotate(vis, imagecentre)

    d = numpy.zeros(shape)
    p = numpy.zeros(shape)

    spectral_mode = get_parameter(params, 'spectral_mode', 'channel')
    log.debug('invert_visibility: spectral mode is %s' % spectral_mode)

    if spectral_mode == 'channel':
        pmax = 0.0
        nchan = shape[0]
        npol = shape[1]
        for channel in range(nchan):
            for pol in range(npol):
                log.debug('invert_visibility: Inverting channel %d, polarisation %d' % (channel, pol))
                d[channel, pol, :, :], p[channel, 0, :, :], pmax = \
                    do_imaging(theta, 1.0 / cellsize, vis.uvw_lambda(channel),
                               vis.vis[:, channel, pol], imgfn=imgfn)
            assert pmax > 0.0, ("No data gridded for channel %d" % channel)
    else:
        raise NotImplementedError("mode %s not supported" % spectral_mode)


    dirty = create_image_from_array(d, w)
    psf = create_image_from_array(p, w)
    log.debug("invert_visibility: Finished making dirty and psf")


    return dirty, psf, pmax
Exemplo n.º 6
0
def predict_visibility(vis: Visibility, sm: SkyModel, params={}) -> Visibility:
    """Predict the visibility from a SkyModel including both components and images

    :param vis:
    :type Visibility: Visibility to be processed
    :param sm:
    :type SkyModel:
    :returns: Visibility
    """
    shape, reffrequency, cellsize, w, imagecentre = create_wcs_from_visibility(vis, params=params)

    # Create copy of visibilities
    vis = copy.copy(vis)
    vis.data = copy.copy(vis.data)
    vis.data['vis'] = numpy.zeros(vis.vis.shape)

    spectral_mode = get_parameter(params, 'spectral_mode', 'channel')
    log.debug('predict_visibility: spectral mode is %s' % spectral_mode)

    if len(sm.images):
        log.debug("predict_visibility: Predicting Visibility from sky model images")

        for im in sm.images:
            assert_same_chan_pol(vis, im)

            # Determine image size
            cellsize = abs(im.wcs.wcs.cdelt[0]) * numpy.pi / 180.0
            theta = im.npixel * cellsize
            log.debug("predict_visibility: Image cellsize %f radians" % cellsize)
            log.debug("predict_visibility: Field of view %f radians" % theta)
            assert (theta / numpy.sqrt(2) < 1.0), "Field of view larger than celestial sphere"

            # Parameterise imaging
            wstep = get_parameter(params, "wstep", 10000.0)
            wcachesize = w_cache_size(vis, wstep)
            log.debug("predict_visibility: Making w-kernel cache of %d kernels" % wcachesize)

            cache_fn = w_conj_kernel_fn(pylru.FunctionCacheManager(w_kernel, wcachesize))
            predfn = functools.partial(w_cache_predict,
                                       wstep=wstep, kernel_cache=cache_fn,
                                       NpixFF=256, NpixKern=15, Qpx=4)

            spectral_mode = get_parameter(params, 'spectral_mode', 'channel')
            log.debug('predict_visibility: spectral mode is %s' % spectral_mode)

            if spectral_mode == 'channel':
                for channel in range(im.nchan):
                    uvw = vis.uvw_lambda(channel)
                    for pol in range(im.npol):
                        log.debug('predict_visibility: Predicting from image channel %d, polarisation %d' % (
                        channel, pol))
                        img = sm.images[0].data[channel, pol, :, :]
                        dv = do_predict(theta, 1.0 / cellsize, numpy.array(uvw), img, predfn)
                        vis.vis[:, channel, pol] += dv
            else:
                raise NotImplementedError("mode %s not supported" % spectral_mode)

            log.debug("predict_visibility: Finished predicting Visibility from sky model images")

    if len(sm.components):
        log.debug("predict_visibility: Predicting Visibility from sky model components")

        for icomp, comp in enumerate(sm.components):

            log.debug("predict_visibility: visibility shape = %s" % str(vis.vis.shape))
            assert_same_chan_pol(vis, comp)

            l,m,n = skycoord_to_lmn(comp.direction, vis.phasecentre)
            log.debug('fourier_transforms.predict_visibility: Cartesian representation of component %d = (%f, %f, %f)'
                  % (icomp, l,m,n))

            if spectral_mode =='channel':
                for channel in range(comp.nchan):
                    uvw = vis.uvw_lambda(channel)
                    phasor = simulate_point(uvw, l, m)
                    for pol in range(comp.npol):
                        log.debug(
                            'predict_visibility: Predicting from component %d channel %d, polarisation %d' % (
                            icomp, channel,
                            pol))
                        vis.vis[:, channel, pol] += comp.flux[channel, pol] * phasor
            else:
                raise NotImplementedError("mode %s not supported" % spectral_mode)

        log.debug("predict_visibility: Finished predicting Visibility from sky model components")

    return vis
Exemplo n.º 7
0
# calculating: 1 + 2
# 3
# 3
# calculating: 2 + 3
# 5

# 方法2 pylru 
# https://pypi.org/project/pylru/
def square(x):
    print('not hit cache')
    return x * x


size = 100
cached = pylru.FunctionCacheManager(square, size)

print(cached(7))
print(cached(6))
print(cached(7))

# The results of cached are the same as square, but automatically cached
# to speed up future calls.

cached.size()       # Returns the size of the cache
cached.size(3)      # Changes the size of the cache. x MUST be greater than
# zero. Returns the new size x.

cached.clear()      # Remove all items from the cache.

Exemplo n.º 8
0
    wsteps = numpy.array(
        sorted(map(float, wkern_file['wkern/%s' % args.theta].keys())))
    wstep = wsteps[1] - wsteps[0]
    print("w kernels:   %.2f - %.2f lambda (step %.2f lambda)" %
          (min(wsteps), max(wsteps), wstep))

    # Make a custom kernel cache that reads from the hdf5 file
    def closest(xs, x):
        return xs[numpy.argmin(numpy.abs(numpy.array(xs) - x))]

    def w_kernel_fn(theta, w):
        kernw = closest(wsteps, w)
        #print("w=", kernw)
        return wkern_file['wkern/%s/%s/kern' % (theta, kernw)]

    w_cache = pylru.FunctionCacheManager(w_kernel_fn, len(wsteps))

    # A-kernels?
    if args.akern is None:

        # Just pure w-projection, also no source dependency.
        print("Gridder: W-projection")
        grid_fn = w_cache_imaging
        grid_pars = {'wstep': wstep, 'kernel_cache': w_cache}
        src = numpy.zeros((src.shape[0], 0))

    else:

        # Open A-kernel file
        akern_file = h5py.File(args.akern.name, "r", driver='core')
        times = list(map(float, akern_file['akern/%s/0' % args.theta]))