def test_partition_skycomponent_neighbours(self):
        all_components = create_low_test_skycomponents_from_gleam(
            flux_limit=0.1,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.5)

        bright_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.5)
        model = create_image(npixel=512,
                             cellsize=0.001,
                             phasecentre=self.phasecentre,
                             frequency=self.frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
        beam = create_low_test_beam(model)
        all_components = apply_beam_to_skycomponent(all_components, beam)
        all_components = filter_skycomponents_by_flux(all_components,
                                                      flux_min=0.1)
        bright_components = apply_beam_to_skycomponent(bright_components, beam)
        bright_components = filter_skycomponents_by_flux(bright_components,
                                                         flux_min=2.0)

        comps_lists = partition_skycomponent_neighbours(
            all_components, bright_components)
        assert len(comps_lists) == len(bright_components)
        assert len(comps_lists[0]) > 0
        assert len(comps_lists[-1]) > 0
Esempio n. 2
0
    def test_create_gaintable_from_screen(self):
        screen = import_image_from_fits(
            arl_path('data/models/test_mpc_screen.fits'))
        beam = create_test_image(cellsize=0.0015,
                                 phasecentre=self.vis.phasecentre,
                                 frequency=self.frequency)

        beam = create_low_test_beam(beam)

        gleam_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.2)

        pb_gleam_components = apply_beam_to_skycomponent(
            gleam_components, beam)

        actual_components = filter_skycomponents_by_flux(pb_gleam_components,
                                                         flux_min=1.0)

        gaintables = create_gaintable_from_screen(self.vis, actual_components,
                                                  screen)
        assert len(gaintables) == len(actual_components), len(gaintables)
        assert gaintables[0].gain.shape == (3, 94, 1, 1,
                                            1), gaintables[0].gain.shape
    def test_expand_skymodel_by_skycomponents(self):
        beam = create_test_image(cellsize=0.0015,
                                 phasecentre=self.vis.phasecentre,
                                 frequency=self.frequency)

        beam = create_low_test_beam(beam, use_local=False)

        gleam_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.2)

        pb_gleam_components = apply_beam_to_skycomponent(
            gleam_components, beam)

        actual_components = filter_skycomponents_by_flux(pb_gleam_components,
                                                         flux_min=1.0)

        assert len(actual_components) == 37, len(actual_components)
        sm = SkyModel(image=self.model, components=actual_components)
        assert len(sm.components) == len(actual_components)

        scatter_sm = expand_skymodel_by_skycomponents(sm)
        assert len(scatter_sm) == len(actual_components) + 1
        assert len(scatter_sm[0].components) == 1
Esempio n. 4
0
def create_unittest_components(model,
                               flux,
                               applypb=False,
                               telescope='LOW',
                               npixel=None,
                               scale=1.0,
                               single=False,
                               symmetric=False):
    # Fill the visibility with exactly computed point sources.

    if npixel == None:
        _, _, _, npixel = model.data.shape
    spacing_pixels = int(scale * npixel) // 4
    log.info('Spacing in pixels = %s' % spacing_pixels)

    if not symmetric:
        centers = [(0.2, 1.1)]
    else:
        centers = list()

    if not single:
        centers.append([0.0, 0.0])

        for x in numpy.linspace(-1.2, 1.2, 7):
            if abs(x) > 1e-15:
                centers.append([x, x])
                centers.append([x, -x])
    model_pol = model.polarisation_frame
    # Make the list of components
    rpix = model.wcs.wcs.crpix
    components = []
    for center in centers:
        ix, iy = center
        # The phase center in 0-relative coordinates is n // 2 so we centre the grid of
        # components on ny // 2, nx // 2. The wcs must be defined consistently.
        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)
        log.info("Component at (%f, %f) [0-rel] %s" % (p[0], p[1], str(sc)))

        # Channel images
        comp = create_skycomponent(direction=sc,
                                   flux=flux,
                                   frequency=model.frequency,
                                   polarisation_frame=model_pol)
        components.append(comp)

    if applypb:
        beam = create_pb(model, telescope=telescope)
        components = apply_beam_to_skycomponent(components, beam)

    return components
    def test_voronoi_decomposition(self):
        bright_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.5)
        model = create_image(npixel=512,
                             cellsize=0.001,
                             phasecentre=self.phasecentre,
                             frequency=self.frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
        beam = create_low_test_beam(model)
        bright_components = apply_beam_to_skycomponent(bright_components, beam)
        bright_components = filter_skycomponents_by_flux(bright_components,
                                                         flux_min=2.0)

        vor, vor_array = voronoi_decomposition(model, bright_components)
        assert len(bright_components) == (numpy.max(vor_array) + 1)
    def test_expand_skymodel_voronoi(self):
        self.model = create_image(
            npixel=256,
            cellsize=0.001,
            polarisation_frame=PolarisationFrame("stokesI"),
            frequency=self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre)
        beam = create_low_test_beam(self.model, use_local=False)

        gleam_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.1)

        pb_gleam_components = apply_beam_to_skycomponent(
            gleam_components, beam)

        actual_components = filter_skycomponents_by_flux(pb_gleam_components,
                                                         flux_min=1.0)
        _, actual_components = remove_neighbouring_components(
            actual_components, 0.05)

        for imask, mask in enumerate(
                image_voronoi_iter(self.model, actual_components)):
            mask.data *= beam.data
            assert isinstance(mask, Image)
            assert mask.data.dtype == "float"
            assert numpy.sum(mask.data) > 1
            # import matplotlib.pyplot as plt
            # from processing_components.image.operations import show_image
            # show_image(mask)
            # plt.show()

        assert len(actual_components) == 9, len(actual_components)
        sm = SkyModel(image=self.model, components=actual_components)
        assert len(sm.components) == len(actual_components)

        scatter_sm = expand_skymodel_by_skycomponents(sm)
        assert len(scatter_sm) == len(actual_components) + 1
        assert len(scatter_sm[0].components) == 1
    def test_image_voronoi_iter(self):
        bright_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.5)
        model = create_image(npixel=512,
                             cellsize=0.001,
                             phasecentre=self.phasecentre,
                             frequency=self.frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
        model.data[...] = 1.0
        beam = create_low_test_beam(model)
        bright_components = apply_beam_to_skycomponent(bright_components, beam)
        bright_components = filter_skycomponents_by_flux(bright_components,
                                                         flux_min=2.0)

        for im in image_voronoi_iter(model, bright_components):
            assert numpy.sum(im.data) > 1
Esempio n. 8
0
    def test_grid_gaintable_to_screen(self):
        screen = import_image_from_fits(
            arl_path('data/models/test_mpc_screen.fits'))
        beam = create_test_image(cellsize=0.0015,
                                 phasecentre=self.vis.phasecentre,
                                 frequency=self.frequency)

        beam = create_low_test_beam(beam, use_local=False)

        gleam_components = create_low_test_skycomponents_from_gleam(
            flux_limit=1.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.2)

        pb_gleam_components = apply_beam_to_skycomponent(
            gleam_components, beam)

        actual_components = filter_skycomponents_by_flux(pb_gleam_components,
                                                         flux_min=1.0)

        gaintables = create_gaintable_from_screen(self.vis, actual_components,
                                                  screen)
        assert len(gaintables) == len(actual_components), len(gaintables)
        assert gaintables[0].gain.shape == (3, 94, 3, 1,
                                            1), gaintables[0].gain.shape

        newscreen = create_empty_image_like(screen)

        newscreen, weights = grid_gaintable_to_screen(self.vis, gaintables,
                                                      newscreen)
        assert numpy.max(numpy.abs(screen.data)) > 0.0
        if self.persist:
            export_image_to_fits(
                newscreen,
                arl_path('test_results/test_mpc_screen_gridded.fits'))
        if self.persist:
            export_image_to_fits(
                weights,
                arl_path('test_results/test_mpc_screen_gridded_weights.fits'))
    def test_remove_neighbouring_components(self):
        all_components = create_low_test_skycomponents_from_gleam(
            flux_limit=3.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.5)

        model = create_image(npixel=512,
                             cellsize=0.001,
                             phasecentre=self.phasecentre,
                             frequency=self.frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
        beam = create_low_test_beam(model)
        all_components = apply_beam_to_skycomponent(all_components, beam)
        all_components = filter_skycomponents_by_flux(all_components,
                                                      flux_min=0.1)

        idx, comps = remove_neighbouring_components(all_components, 0.1)
        assert idx == [0, 3, 5, 6, 7, 8, 10, 12, 14, 16, 19], idx
        assert comps[0].name == 'GLEAM J234118-581606', comps[0].name
    def test_remove_neighbouring_components(self):
        all_components = create_low_test_skycomponents_from_gleam(
            flux_limit=3.0,
            phasecentre=self.phasecentre,
            frequency=self.frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=0.5)

        model = create_image(npixel=512,
                             cellsize=0.001,
                             phasecentre=self.phasecentre,
                             frequency=self.frequency,
                             polarisation_frame=PolarisationFrame('stokesI'))
        beam = create_low_test_beam(model, use_local=False)
        all_components = apply_beam_to_skycomponent(all_components, beam)
        all_components = filter_skycomponents_by_flux(all_components,
                                                      flux_min=0.1)

        idx, comps = remove_neighbouring_components(all_components, 0.1)
        assert idx == [
            0, 1, 3, 8, 12, 13, 17, 22, 25, 26, 29, 32, 35, 38, 41, 42, 46, 47,
            50, 52, 53, 56, 57, 58, 61, 63, 66, 68, 70
        ], idx
        assert comps[0].name == 'GLEAM J215739-661155', comps[0].name
    def actualSetup(self,
                    vnchan=1,
                    doiso=True,
                    ntimes=5,
                    flux_limit=2.0,
                    zerow=True,
                    fixed=False):

        nfreqwin = vnchan
        rmax = 300.0
        npixel = 512
        cellsize = 0.001
        frequency = numpy.linspace(0.8e8, 1.2e8, nfreqwin)
        if nfreqwin > 1:
            channel_bandwidth = numpy.array(nfreqwin *
                                            [frequency[1] - frequency[0]])
        else:
            channel_bandwidth = [0.4e8]
        times = numpy.linspace(-numpy.pi / 3.0, numpy.pi / 3.0, ntimes)

        phasecentre = SkyCoord(ra=-60.0 * u.deg,
                               dec=-60.0 * u.deg,
                               frame='icrs',
                               equinox='J2000')

        lowcore = create_named_configuration('LOWBD2', rmax=rmax)

        block_vis = create_blockvisibility(
            lowcore,
            times,
            frequency=frequency,
            channel_bandwidth=channel_bandwidth,
            weight=1.0,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"),
            zerow=zerow)

        block_vis.data['uvw'][..., 2] = 0.0
        self.beam = create_image_from_visibility(
            block_vis,
            npixel=npixel,
            frequency=[numpy.average(frequency)],
            nchan=nfreqwin,
            channel_bandwidth=[numpy.sum(channel_bandwidth)],
            cellsize=cellsize,
            phasecentre=phasecentre)

        self.components = create_low_test_skycomponents_from_gleam(
            flux_limit=flux_limit,
            phasecentre=phasecentre,
            frequency=frequency,
            polarisation_frame=PolarisationFrame('stokesI'),
            radius=npixel * cellsize)
        self.beam = create_low_test_beam(self.beam)
        self.components = apply_beam_to_skycomponent(self.components,
                                                     self.beam,
                                                     flux_limit=flux_limit)

        self.vis = copy_visibility(block_vis, zero=True)
        gt = create_gaintable_from_blockvisibility(block_vis, timeslice='auto')
        for i, sc in enumerate(self.components):
            if sc.flux[0, 0] > 10:
                sc.flux[...] /= 10.0
            component_vis = copy_visibility(block_vis, zero=True)
            gt = simulate_gaintable(gt,
                                    amplitude_error=0.0,
                                    phase_error=0.1,
                                    seed=None)
            component_vis = predict_skycomponent_visibility(component_vis, sc)
            component_vis = apply_gaintable(component_vis, gt)
            self.vis.data['vis'][...] += component_vis.data['vis'][...]

        # Do an isoplanatic selfcal
        self.model_vis = copy_visibility(self.vis, zero=True)
        self.model_vis = predict_skycomponent_visibility(
            self.model_vis, self.components)
        if doiso:
            gt = solve_gaintable(self.vis,
                                 self.model_vis,
                                 phase_only=True,
                                 timeslice='auto')
            self.vis = apply_gaintable(self.vis, gt, inverse=True)

        self.model_vis = convert_blockvisibility_to_visibility(self.model_vis)
        self.model_vis, _, _ = weight_visibility(self.model_vis, self.beam)
        self.dirty_model, sumwt = invert_function(self.model_vis,
                                                  self.beam,
                                                  context='2d')
        export_image_to_fits(self.dirty_model,
                             "%s/test_skymodel-model_dirty.fits" % self.dir)

        lvis = convert_blockvisibility_to_visibility(self.vis)
        lvis, _, _ = weight_visibility(lvis, self.beam)
        dirty, sumwt = invert_function(lvis, self.beam, context='2d')
        if doiso:
            export_image_to_fits(
                dirty, "%s/test_skymodel-initial-iso-residual.fits" % self.dir)
        else:
            export_image_to_fits(
                dirty,
                "%s/test_skymodel-initial-noiso-residual.fits" % self.dir)

        self.skymodels = [
            SkyModel(components=[cm], fixed=fixed) for cm in self.components
        ]
 def apply_pb_comp(vt, model, comp):
     telescope = vt.configuration.name
     pb = create_pb(model, telescope)
     return apply_beam_to_skycomponent(comp, pb, flux_limit)
Esempio n. 13
0
def create_low_test_skymodel_from_gleam(npixel=512,
                                        polarisation_frame=PolarisationFrame(
                                            "stokesI"),
                                        cellsize=0.000015,
                                        frequency=numpy.array([1e8]),
                                        channel_bandwidth=numpy.array([1e6]),
                                        phasecentre=None,
                                        kind='cubic',
                                        applybeam=True,
                                        flux_limit=0.1,
                                        flux_max=numpy.inf,
                                        flux_threshold=1.0,
                                        insert_method='Nearest',
                                        telescope='LOW') -> SkyModel:
    """Create LOW test skymodel from the GLEAM survey

    Stokes I is estimated from a cubic spline fit to the measured fluxes. The polarised flux is always zero.

    See http://www.mwatelescope.org/science/gleam-survey The catalog is available from Vizier.

    VIII/100   GaLactic and Extragalactic All-sky MWA survey  (Hurley-Walker+, 2016)

    GaLactic and Extragalactic All-sky Murchison Wide Field Array (GLEAM) survey. I: A low-frequency extragalactic
    catalogue. Hurley-Walker N., et al., Mon. Not. R. Astron. Soc., 464, 1146-1167 (2017), 2017MNRAS.464.1146H

    :param telescope:
    :param npixel: Number of pixels
    :param polarisation_frame: Polarisation frame (default PolarisationFrame("stokesI"))
    :param cellsize: cellsize in radians
    :param frequency:
    :param channel_bandwidth: Channel width (Hz)
    :param phasecentre: phasecentre (SkyCoord)
    :param kind: Kind of interpolation (see scipy.interpolate.interp1d) Default: cubic
    :param applybeam: Apply the primary beam?
    :param flux_limit: Weakest component
    :param flux_max: Maximum strength component to be included in components
    :param flux_threshold: Split between components (brighter) and image (weaker)
    :param insert_method: Nearest | PSWF | Lanczos
    :return:
    :return: SkyModel

    """

    if phasecentre is None:
        phasecentre = SkyCoord(ra=+15.0 * u.deg,
                               dec=-35.0 * u.deg,
                               frame='icrs',
                               equinox='J2000')

    radius = npixel * cellsize

    sc = create_low_test_skycomponents_from_gleam(
        flux_limit=flux_limit,
        polarisation_frame=polarisation_frame,
        frequency=frequency,
        phasecentre=phasecentre,
        kind=kind,
        radius=radius)

    sc = filter_skycomponents_by_flux(sc, flux_max=flux_max)
    if polarisation_frame is None:
        polarisation_frame = PolarisationFrame("stokesI")

    npol = polarisation_frame.npol
    nchan = len(frequency)
    shape = [nchan, npol, npixel, npixel]
    w = WCS(naxis=4)
    # The negation in the longitude is needed by definition of RA, DEC
    w.wcs.cdelt = [
        -cellsize * 180.0 / numpy.pi, cellsize * 180.0 / numpy.pi, 1.0,
        channel_bandwidth[0]
    ]
    w.wcs.crpix = [npixel // 2 + 1, npixel // 2 + 1, 1.0, 1.0]
    w.wcs.ctype = ["RA---SIN", "DEC--SIN", 'STOKES', 'FREQ']
    w.wcs.crval = [phasecentre.ra.deg, phasecentre.dec.deg, 1.0, frequency[0]]
    w.naxis = 4
    w.wcs.radesys = 'ICRS'
    w.wcs.equinox = 2000.0

    model = create_image_from_array(numpy.zeros(shape),
                                    w,
                                    polarisation_frame=polarisation_frame)

    if applybeam:
        beam = create_pb(model, telescope=telescope)
        sc = apply_beam_to_skycomponent(sc, beam)

    weaksc = filter_skycomponents_by_flux(sc, flux_max=flux_threshold)
    brightsc = filter_skycomponents_by_flux(sc, flux_min=flux_threshold)
    model = insert_skycomponent(model, weaksc, insert_method=insert_method)

    log.info(
        'create_low_test_skymodel_from_gleam: %d bright sources above flux threshold %.3f, %d weak sources below '
        % (len(brightsc), flux_threshold, len(weaksc)))

    return SkyModel(components=brightsc,
                    image=model,
                    mask=None,
                    gaintable=None)