def test_readwritegaintable(self):
        self.vis = create_blockvisibility(
            self.lowcore,
            self.times,
            self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
            polarisation_frame=PolarisationFrame("linear"),
            weight=1.0)
        gt = create_gaintable_from_blockvisibility(self.vis, timeslice='auto')
        gt = simulate_gaintable(gt, phase_error=1.0, amplitude_error=0.1)

        config = {
            "buffer": {
                "directory": self.dir
            },
            "gaintable": {
                "name": "test_buffergaintable.hdf",
                "data_model": "GainTable"
            }
        }
        bdm = BufferGainTable(config["buffer"], config["gaintable"], gt)
        bdm.sync()
        new_bdm = BufferGainTable(config["buffer"], config["gaintable"])
        new_bdm.sync()
        newgt = bdm.memory_data_model

        assert gt.data.shape == newgt.data.shape
        assert numpy.max(numpy.abs(gt.gain - newgt.gain)) < 1e-15
示例#2
0
 def core_solve(self,
                spf,
                dpf,
                phase_error=0.1,
                amplitude_error=0.0,
                leakage=0.0,
                phase_only=True,
                niter=200,
                crosspol=False,
                residual_tol=1e-6,
                f=None,
                vnchan=3):
     if f is None:
         f = [100.0, 50.0, -10.0, 40.0]
     self.actualSetup(spf, dpf, f=f, vnchan=vnchan)
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt,
                             phase_error=phase_error,
                             amplitude_error=amplitude_error,
                             leakage=leakage)
     original = copy_visibility(self.vis)
     vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis,
                             original,
                             phase_only=phase_only,
                             niter=niter,
                             crosspol=crosspol,
                             tol=1e-6)
     vis = apply_gaintable(vis, gtsol, inverse=True)
     residual = numpy.max(gtsol.residual)
     assert residual < residual_tol, "%s %s Max residual = %s" % (spf, dpf,
                                                                  residual)
     log.debug(qa_gaintable(gt))
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
    def test_readwriteskymodel_no_image(self):
        vis = create_blockvisibility(
            self.midcore,
            self.times,
            self.frequency,
            channel_bandwidth=self.channel_bandwidth,
            phasecentre=self.phasecentre,
            polarisation_frame=PolarisationFrame("linear"),
            weight=1.0)
        gt = create_gaintable_from_blockvisibility(vis, timeslice='auto')
        gt = simulate_gaintable(gt, phase_error=1.0, amplitude_error=0.1)
        sm = SkyModel(components=[self.comp], gaintable=gt)

        config = {
            "buffer": {
                "directory": self.dir
            },
            "skymodel": {
                "name": "test_bufferskymodel.hdf",
                "data_model": "SkyModel"
            }
        }
        bdm = BufferSkyModel(config["buffer"], config["skymodel"], sm)
        bdm.sync()
        new_bdm = BufferSkyModel(config["buffer"], config["skymodel"])
        new_bdm.sync()
        newsm = bdm.memory_data_model

        assert newsm.components[0].flux.shape == self.comp.flux.shape
        assert newsm.gaintable.data.shape == gt.data.shape
 def test_apply_gaintable_only(self):
     for spf, dpf in[('stokesI', 'stokesI'), ('stokesIQUV', 'linear'), ('stokesIQUV', 'circular')]:
         self.actualSetup(spf, dpf)
         gt = create_gaintable_from_blockvisibility(self.vis, timeslice='auto')
         log.info("Created gain table: %s" % (gaintable_summary(gt)))
         gt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.01)
         original = copy_visibility(self.vis)
         vis = apply_gaintable(self.vis, gt)
         error = numpy.max(numpy.abs(vis.vis - original.vis))
         assert error > 10.0, "Error = %f" % (error)
 def test_solve_gaintable_scalar_timeslice(self):
     self.actualSetup('stokesI', 'stokesI', f=[100.0], ntimes=10)
     gt = create_gaintable_from_blockvisibility(self.vis, timeslice=120.0)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt, phase_error=10.0, amplitude_error=0.0)
     original = copy_visibility(self.vis)
     self.vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis, original, phase_only=True, niter=200)
     residual = numpy.max(gtsol.residual)
     assert residual < 3e-8, "Max residual = %s" % (residual)
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
 def test_create_gaintable_from_visibility_interval(self):
     for timeslice in [10.0, 'auto', 1e5]:
         for spf, dpf in[('stokesIQUV', 'linear')]:
             self.actualSetup(spf, dpf)
             gt = create_gaintable_from_blockvisibility(self.vis, timeslice=timeslice)
             log.info("Created gain table: %s" % (gaintable_summary(gt)))
             gt = simulate_gaintable(gt, phase_error=1.0)
             original = copy_visibility(self.vis)
             vis = apply_gaintable(self.vis, gt)
             assert numpy.max(numpy.abs(original.vis)) > 0.0
             assert numpy.max(numpy.abs(vis.vis)) > 0.0
             assert numpy.max(numpy.abs(vis.vis - original.vis)) > 0.0
 def test_readwritegaintable(self):
     self.vis = create_blockvisibility(self.lowcore, self.times, self.frequency,
                                       channel_bandwidth=self.channel_bandwidth,
                                       phasecentre=self.phasecentre,
                                       polarisation_frame=PolarisationFrame("linear"),
                                       weight=1.0)
     gt = create_gaintable_from_blockvisibility(self.vis, timeslice='auto')
     gt = simulate_gaintable(gt, phase_error=1.0, amplitude_error=0.1)
     export_gaintable_to_hdf5(gt, '%s/test_data_model_helpers_gaintable.hdf' % self.dir)
     newgt = import_gaintable_from_hdf5('%s/test_data_model_helpers_gaintable.hdf' % self.dir)
     
     assert gt.data.shape == newgt.data.shape
     assert numpy.max(numpy.abs(gt.gain - newgt.gain)) < 1e-15
 def test_plot_gaintable_scalar(self):
     self.actualSetup('stokesI', 'stokesI', f=[100.0])
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt, phase_error=0.1, amplitude_error=0.1)
     plt.clf()
     fig, ax = plt.subplots(1, 1)
     gaintable_plot(gt, ax, value='amp')
     plt.show(block=False)
     fig, ax = plt.subplots(1, 1)
     gaintable_plot(gt, ax, value='phase')
     plt.show(block=False)
     fig, ax = plt.subplots(1, 1)
     gaintable_plot(gt, ax, value='residual')
     plt.show(block=False)
 def test_calibrate_T_function(self):
     self.actualSetup('stokesI', 'stokesI', f=[100.0])
     # Prepare the corrupted visibility data_models
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt, phase_error=10.0, amplitude_error=0.0)
     original = copy_visibility(self.vis)
     self.vis = apply_gaintable(self.vis, gt, vis_slices=None)
     # Now get the control dictionary and calibrate
     controls = create_calibration_controls()
     controls['T']['first_selfcal'] = 0
     calibrated_vis, gaintables = calibrate_function(
         self.vis, original, calibration_context='T', controls=controls)
     residual = numpy.max(gaintables['T'].residual)
     assert residual < 1e-8, "Max T residual = %s" % (residual)
 def test_solve_gaintable_scalar_normalise(self):
     self.actualSetup('stokesI', 'stokesI', f=[100.0])
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt, phase_error=0.0, amplitude_error=0.1)
     gt.data['gain'] *= 2.0
     original = copy_visibility(self.vis)
     self.vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis,
                             original,
                             phase_only=False,
                             niter=200,
                             normalise_gains=True)
     residual = numpy.max(gtsol.residual)
     assert residual < 3e-8, "Max residual = %s" % (residual)
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
 def test_solve_gaintable_scalar_bandpass(self):
     self.actualSetup('stokesI', 'stokesI', f=[100.0], vnchan=128)
     gt = create_gaintable_from_blockvisibility(self.vis)
     log.info("Created gain table: %s" % (gaintable_summary(gt)))
     gt = simulate_gaintable(gt,
                             phase_error=10.0,
                             amplitude_error=0.01,
                             smooth_channels=8)
     original = copy_visibility(self.vis)
     self.vis = apply_gaintable(self.vis, gt)
     gtsol = solve_gaintable(self.vis,
                             original,
                             phase_only=False,
                             niter=200)
     residual = numpy.max(gtsol.residual)
     assert residual < 3e-8, "Max residual = %s" % (residual)
     assert numpy.max(numpy.abs(gtsol.gain - 1.0)) > 0.1
 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_arlexecute(self.model_vis, self.beam, context='2d')
     export_image_to_fits(self.dirty_model, "%s/test_modelpartition-model_dirty.fits" % self.dir)
     
     lvis = convert_blockvisibility_to_visibility(self.vis)
     lvis, _, _ = weight_visibility(lvis, self.beam)
     dirty, sumwt = invert_arlexecute(lvis, self.beam, context='2d')
     if doiso:
         export_image_to_fits(dirty, "%s/test_modelpartition-initial-iso-residual.fits" % self.dir)
     else:
         export_image_to_fits(dirty, "%s/test_modelpartition-initial-noiso-residual.fits" % self.dir)
     
     self.skymodels = [SkyModel(components=[cm], fixed=fixed) for cm in self.components]
                                 context='wstack')
#print("np.sum(predicted_vis.data): ", numpy.sum(predicted_vis.data['vis']))
block_vis = convert_visibility_to_blockvisibility(predicted_vis)
#print("np.sum(block_vis.data): ", numpy.sum(block_vis.data['vis']))

#print("nchan npol nants ", block_vis.nchan, block_vis.npol, block_vis.nants)
#print("uvw", block_vis.uvw, numpy.sum(block_vis.uvw))
#print("vis", block_vis.vis, numpy.sum(block_vis.vis))
#print("weight", block_vis.weight, numpy.sum(block_vis.weight))
#print("time", block_vis.time, numpy.sum(block_vis.time))
#print("integration_time", block_vis.integration_time, numpy.sum(block_vis.integration_time))
#print("nvis, size", block_vis.nvis, block_vis.size())

gt = create_gaintable_from_blockvisibility(block_vis)
#print("np.sum(gt.data): ", numpy.sum(gt.data['gain']))
gt = simulate_gaintable(gt, phase_error=1.0)
#print("np.sum(gt.data): ", numpy.sum(gt.data['gain']))
blockvis = apply_gaintable(block_vis, gt)
#print("np.sum(blockvis.data): ", numpy.sum(blockvis.data['vis']))

model = create_image_from_visibility(
    block_vis,
    npixel=npixel,
    frequency=[numpy.average(frequency)],
    nchan=1,
    channel_bandwidth=[numpy.sum(channel_bandwidth)],
    cellsize=cellsize,
    phasecentre=phasecentre)

#print("model sum, min, max, shape: ", numpy.sum(model.data), numpy.amin(model.data), numpy.amax(model.data), model.shape)
示例#14
0
 def corrupt_vis(vis, gt, **kwargs):
     if gt is None:
         gt = create_gaintable_from_blockvisibility(vis, **kwargs)
         gt = simulate_gaintable(gt, **kwargs)
     return apply_gaintable(vis, gt)
    def ingest_visibility(self,
                          freq=None,
                          chan_width=None,
                          times=None,
                          add_errors=False,
                          block=True,
                          bandpass=False):
        if freq is None:
            freq = [1e8]
        if chan_width is None:
            chan_width = [1e6]
        if times is None:
            times = (numpy.pi / 12.0) * numpy.linspace(-3.0, 3.0, 5)

        lowcore = create_named_configuration('LOWBD2', rmax=750.0)
        frequency = numpy.array(freq)
        channel_bandwidth = numpy.array(chan_width)

        phasecentre = SkyCoord(ra=+180.0 * u.deg,
                               dec=-60.0 * u.deg,
                               frame='icrs',
                               equinox='J2000')
        if block:
            vt = create_blockvisibility(
                lowcore,
                times,
                frequency,
                channel_bandwidth=channel_bandwidth,
                weight=1.0,
                phasecentre=phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"))
        else:
            vt = create_visibility(
                lowcore,
                times,
                frequency,
                channel_bandwidth=channel_bandwidth,
                weight=1.0,
                phasecentre=phasecentre,
                polarisation_frame=PolarisationFrame("stokesI"))
        cellsize = 0.001
        model = create_image_from_visibility(
            vt,
            npixel=self.npixel,
            cellsize=cellsize,
            npol=1,
            frequency=frequency,
            phasecentre=phasecentre,
            polarisation_frame=PolarisationFrame("stokesI"))
        nchan = len(self.frequency)
        flux = numpy.array(nchan * [[100.0]])
        facets = 4

        rpix = model.wcs.wcs.crpix - 1.0
        spacing_pixels = self.npixel // facets
        centers = [-1.5, -0.5, 0.5, 1.5]
        comps = list()
        for iy in centers:
            for ix in centers:
                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)
                comp = create_skycomponent(
                    direction=sc,
                    flux=flux,
                    frequency=frequency,
                    polarisation_frame=PolarisationFrame("stokesI"))
                comps.append(comp)
        if block:
            predict_skycomponent_visibility(vt, comps)
        else:
            predict_skycomponent_visibility(vt, comps)
        insert_skycomponent(model, comps)
        self.comps = comps
        self.model = copy_image(model)
        self.empty_model = create_empty_image_like(model)
        export_image_to_fits(
            model, '%s/test_pipeline_functions_model.fits' % (self.dir))

        if add_errors:
            # These will be the same for all calls
            numpy.random.seed(180555)
            gt = create_gaintable_from_blockvisibility(vt)
            gt = simulate_gaintable(gt, phase_error=1.0, amplitude_error=0.0)
            vt = apply_gaintable(vt, gt)

            if bandpass:
                bgt = create_gaintable_from_blockvisibility(vt, timeslice=1e5)
                bgt = simulate_gaintable(bgt,
                                         phase_error=0.01,
                                         amplitude_error=0.01,
                                         smooth_channels=4)
                vt = apply_gaintable(vt, bgt)

        return vt