def test_residual_image_bag(self):
        context = 'wstack'
        vis_slices = {'wstack': 101}
        dirty_bag = invert_bag(self.vis_bag,
                               self.empty_model_bag,
                               dopsf=False,
                               context=context,
                               normalize=True,
                               vis_slices=vis_slices[context])
        psf_bag = invert_bag(self.vis_bag,
                             self.empty_model_bag,
                             dopsf=True,
                             context=context,
                             normalize=True,
                             vis_slices=vis_slices[context])
        dirty_bag = reify(dirty_bag)
        psf_bag = reify(psf_bag)
        model_bag = deconvolve_bag(dirty_bag,
                                   psf_bag,
                                   self.empty_model_bag,
                                   niter=1000,
                                   gain=0.1,
                                   algorithm='msclean',
                                   threshold=0.01,
                                   window_shape=None)
        residual_bag = residual_image_bag(self.vis_bag,
                                          model_bag,
                                          context=context,
                                          vis_slices=vis_slices[context])
        final = residual_bag.compute()[0]['image'][0]
        export_image_to_fits(
            final, '%s/test_bags_%s_residual.fits' % (self.dir, context))

        qa = qa_image(final, context=context)
        assert qa.data['max'] < 15.0, str(qa)
    def test_deconvolve_bag(self):
        context = 'wstack'
        vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101}
        dirty_bag = invert_bag(self.vis_bag,
                               self.model_bag,
                               dopsf=False,
                               context=context,
                               normalize=True,
                               vis_slices=vis_slices[context])
        psf_bag = invert_bag(self.vis_bag,
                             self.model_bag,
                             dopsf=True,
                             context=context,
                             normalize=True,
                             vis_slices=vis_slices[context])
        dirty_bag = reify(dirty_bag)
        psf_bag = reify(psf_bag)
        model_bag = deconvolve_bag(dirty_bag,
                                   psf_bag,
                                   self.empty_model_bag,
                                   niter=1000,
                                   gain=0.7,
                                   algorithm='msclean',
                                   threshold=0.01,
                                   window_shape=None)
        model = model_bag.compute()[0]['image']
        qa = qa_image(model, context=context)

        export_image_to_fits(
            model, '%s/test_bags_%s_deconvolve.fits' % (self.dir, context))

        assert numpy.abs(qa.data['max'] - 60.5) < 0.1, str(qa)
    def test_predict_bag(self):
        errors = {'2d': 28.0, 'timeslice': 30.0, 'wstack': 2.3}
        vis_slices = {'2d': None, 'timeslice': 'auto', 'wstack': 101}
        for context in ['wstack', 'timeslice']:
            model_vis_bag = predict_bag(self.vis_bag,
                                        self.model_bag,
                                        context,
                                        vis_slices=vis_slices[context])

            model_vis_bag = reify(model_vis_bag)
            error_vis_bag = self.vis_bag.map(predict_record_subtract,
                                             model_vis_bag)
            error_vis_bag = reify(error_vis_bag)
            error_image_bag = invert_bag(error_vis_bag,
                                         self.model_bag,
                                         dopsf=False,
                                         context=context,
                                         normalize=True,
                                         vis_slices=vis_slices[context])
            result = error_image_bag.compute()
            error_image = result[0]['image'][0]
            export_image_to_fits(
                error_image, '%s/test_bags_%s_predict_error_image.fits' %
                (self.dir, context))
            qa = qa_image(error_image, context='error image for %s' % context)
            assert qa.data['max'] < errors[context], str(qa)
示例#4
0
def selfcal_record(block_vis_bag, model_vis_bag, **kwargs):
    assert isinstance(block_vis_bag, bag.Bag), block_vis_bag
    assert isinstance(model_vis_bag, bag.Bag), model_vis_bag
    block_model_vis_bag = reify(
        model_vis_bag.map(map_record, convert_visibility_to_blockvisibility))
    block_vis_bag = calibrate_bag(block_vis_bag, block_model_vis_bag, **kwargs)
    vis_bag = block_vis_bag.map(map_record,
                                convert_blockvisibility_to_visibility)
    vis_bag = reify(vis_bag)
    return vis_bag
    def test_restore_bag(self):

        peaks = {'wstack': 98.8113067286}
        vis_slices = {'wstack': 101}
        context = 'wstack'
        dirty_bag = invert_bag(self.vis_bag,
                               self.model_bag,
                               dopsf=False,
                               context=context,
                               normalize=True,
                               vis_slices=vis_slices[context])
        psf_bag = invert_bag(self.vis_bag,
                             self.model_bag,
                             dopsf=True,
                             context=context,
                             normalize=True,
                             vis_slices=vis_slices[context])
        dirty_bag = reify(dirty_bag)
        psf_bag = reify(psf_bag)
        model_bag = deconvolve_bag(dirty_bag,
                                   psf_bag,
                                   self.empty_model_bag,
                                   niter=1000,
                                   gain=0.7,
                                   algorithm='msclean',
                                   threshold=0.01,
                                   window_shape=None)
        model_bag = reify(model_bag)

        res_image_bag = residual_image_bag(self.vis_bag,
                                           model_bag,
                                           context=context,
                                           vis_slices=vis_slices[context])
        res_image_bag = reify(res_image_bag)
        residual = res_image_bag.compute()[0]['image'][0]
        export_image_to_fits(
            residual, '%s/test_bags_%s_residual.fits' % (self.dir, context))

        final_bag = restore_bag(model_bag, psf_bag, res_image_bag)
        final = final_bag.compute()[0]
        qa = qa_image(final, context=context)
        export_image_to_fits(
            final, '%s/test_bags_%s_restored.fits' % (self.dir, context))
        assert numpy.abs(qa.data['max'] - peaks[context]) < 0.1, str(qa)
    def setupVis(self, add_errors=False, block=True, freqwin=3):
        self.freqwin = freqwin
        self.ntimes = 5
        self.times = numpy.linspace(-3.0, +3.0, self.ntimes) * numpy.pi / 12.0
        self.frequency = numpy.linspace(0.8e8, 1.2e8, self.freqwin)
        self.vis_bag = \
            bag.from_sequence([{'freqwin': f,
                                'vis': self.ingest_visibility([freq], times=self.times,
                                                              add_errors=add_errors,
                                                              block=block)}
                               for f, freq in enumerate(self.frequency)])

        self.vis_bag = reify(self.vis_bag)
        self.model_bag = image_to_records_bag(self.freqwin, self.model)
        self.empty_model_bag = image_to_records_bag(self.freqwin,
                                                    self.empty_model)
示例#7
0
def ical_pipeline_bag(block_vis_bag,
                      model_bag,
                      context='2d',
                      first_selfcal=None,
                      **kwargs) -> bag:
    """Create bag for ICAL pipeline
    
    :param vis_bag:
    :param model_bag:
    :param context: Imaging context
    :param first_selfcal: First cycle for phase only selfcal
    :param kwargs: Parameters for functions in bags
    :return:
    """
    assert isinstance(block_vis_bag, bag.Bag), block_vis_bag
    assert isinstance(model_bag, bag.Bag), model_bag

    vis_bag = block_vis_bag.map(map_record,
                                convert_blockvisibility_to_visibility)
    psf_bag = invert_bag(vis_bag,
                         model_bag,
                         context=context,
                         dopsf=True,
                         **kwargs)
    psf_bag = reify(psf_bag)

    # Make the predicted visibilities, selfcalibrate against it correcting the gains, then
    # form the residual visibility, then make the residual image
    model_vis_bag = reify(
        predict_bag(vis_bag, model_bag, context=context, **kwargs))
    if first_selfcal is not None and first_selfcal == 0:
        vis_bag = reify(
            selfcal_record(block_vis_bag,
                           model_vis_bag=model_vis_bag,
                           **kwargs))
    res_vis_bag = reify(residual_vis_bag(vis_bag, model_vis_bag))
    res_bag = invert_bag(res_vis_bag,
                         model_bag,
                         context=context,
                         dopsf=False,
                         **kwargs)
    res_bag = reify(res_bag)

    deconvolve_model_bag = reify(
        deconvolve_bag(res_bag, psf_bag, model_bag, **kwargs))

    nmajor = get_parameter(kwargs, "nmajor", 5)
    if nmajor > 1:
        for cycle in range(nmajor):
            model_vis_bag = reify(
                predict_bag(vis_bag,
                            deconvolve_model_bag,
                            context=context,
                            **kwargs))
            if first_selfcal is not None and cycle >= first_selfcal:
                vis_bag = reify(
                    selfcal_record(block_vis_bag,
                                   model_vis_bag=model_vis_bag,
                                   **kwargs))
            res_vis_bag = reify(residual_vis_bag(vis_bag, model_vis_bag))
            res_bag = invert_bag(res_vis_bag,
                                 model_bag,
                                 context=context,
                                 dopsf=False,
                                 **kwargs)
            res_bag = reify(res_bag)
            deconvolve_model_bag = reify(
                deconvolve_bag(res_bag, psf_bag, deconvolve_model_bag,
                               **kwargs))

    res_bag = residual_image_bag(vis_bag,
                                 deconvolve_model_bag,
                                 context=context,
                                 **kwargs)
    res_bag = reify(res_bag)
    deconvolve_model_bag = reify(deconvolve_model_bag)
    rest_bag = restore_bag(deconvolve_model_bag, psf_bag, res_bag, **kwargs)
    rest_bag = reify(rest_bag)
    return bag.from_sequence([deconvolve_model_bag, res_bag, rest_bag])