Example #1
0
    def __init__(self, GD, npix, cell_size_rad):
        self._slvr_cfg = slvr_cfg = montblanc.rime_solver_cfg(
            data_source=Options.DATA_SOURCE_DEFAULT,
            tf_server_target=GD["Montblanc"]["TensorflowServerTarget"],
            mem_budget=2 * 1024 * 1024 * 1024,
            dtype='double',
            auto_correlations=False,
            version='tf')

        self._solver = montblanc.rime_solver(slvr_cfg)

        self._cell_size_rad = cell_size_rad
        self._npix = npix
        self._mgr = DataDictionaryManager()

        # Configure the Beam upfront
        if GD["Beam"]["Model"] == "FITS":
            fits_file_spec = GD["Beam"]["FITSFile"]
            l_axis = GD["Beam"]["FITSLAxis"]
            m_axis = GD["Beam"]["FITSMAxis"]
            self._beam_prov = FitsBeamSourceProvider(fits_file_spec,
                                                     l_axis=l_axis,
                                                     m_axis=m_axis)
        else:
            self._beam_prov = None
Example #2
0
def simulate(src_provs, snk_provs, opts):
    """
    Convenience function which creates and executes a Montblanc solver for the given source and 
    sink providers.

    Args:
        src_provs (list): 
            List of :obj:`~montblanc.impl.rime.tensorflow.sources.SourceProvider` objects. See
            Montblanc's documentation.
        snk_provs (list):
            List of :obj:`~montblanc.impl.rime.tensorflow.sinks.SinkProvider` objects. See
            Montblanc's documentation. 
        opts (dict):
            Montblanc simulation options (see [montblanc] section in DefaultParset.cfg).
    """

    global _mb_slvr

    if _mb_slvr is None:
        slvr_cfg = montblanc.rime_solver_cfg(
            mem_budget=opts["mem-budget"] * 1024 * 1024,
            dtype=opts["dtype"],
            polarisation_type=opts["feed-type"],
            device_type=opts["device-type"])

        _mb_slvr = montblanc.rime_solver(slvr_cfg)

    _mb_slvr.solve(source_providers=src_provs, sink_providers=snk_provs)
Example #3
0
    def get_v5_output(self, slvr_cfg, **kwargs):
        # Get visibilities from the v5 solver
        slvr_cfg = slvr_cfg.copy()
        slvr_cfg.update(**kwargs)
        slvr_cfg[Options.VERSION] = Options.VERSION_FIVE

        with FitsBeam(base_beam_file) as fb:
            beam_shape = fb.shape
            slvr_cfg[Options.E_BEAM_WIDTH] = beam_shape[0]
            slvr_cfg[Options.E_BEAM_HEIGHT] = beam_shape[1]
            slvr_cfg[Options.E_BEAM_DEPTH] = beam_shape[2]

            with montblanc.rime_solver(slvr_cfg) as slvr:
                slvr.lm[:,0] = _L
                slvr.lm[:,1] = _M

                slvr.stokes[:,:,0] = _I
                slvr.stokes[:,:,1] = _Q
                slvr.stokes[:,:,2] = _U
                slvr.stokes[:,:,3] = _V
                slvr.alpha[:] = _ALPHA

                slvr.ref_frequency[:] = _REF_FREQ

                pa_sin = np.sin(slvr.parallactic_angles)[np.newaxis,:]
                pa_cos = np.cos(slvr.parallactic_angles)[np.newaxis,:]

                l = slvr.lm[:,0,np.newaxis]*pa_cos - slvr.lm[:,1,np.newaxis]*pa_sin;
                m = slvr.lm[:,0,np.newaxis]*pa_sin + slvr.lm[:,1,np.newaxis]*pa_cos;

                fb.reconfigure_frequency_axes(slvr.frequency)

                # Configure the beam from the FITS file
                slvr.E_beam.real[:] = fb.real()
                slvr.E_beam.imag[:] = fb.imag()

                # Configure the beam extents
                ll, lm, lf, ul, um, uf = fb.beam_extents

                slvr.set_beam_ll(ll)
                slvr.set_beam_lm(lm)
                slvr.set_beam_lfreq(lf)
                slvr.set_beam_ul(ul)
                slvr.set_beam_um(um)
                slvr.set_beam_ufreq(uf)

                slvr.solve()

            import pyrap.tables as pt

            query = " ".join(["FIELD_ID=0",
                "" if slvr.is_autocorrelated() else "AND ANTENNA1 != ANTENNA2",
                "ORDERBY TIME, ANTENNA1, ANTENNA2, "
                "[SELECT SPECTRAL_WINDOW_ID FROM ::DATA_DESCRIPTION][DATA_DESC_ID]"])

            # Dump visibilities in CORRECTED_DATA
            with pt.table(msfile, ack=False, readonly=False).query(query) as ms:
                ms.putcol('CORRECTED_DATA', slvr.model_vis.reshape(-1, slvr.dim_global_size('nchan'), 4))

            return slvr.X2, slvr.model_vis.copy()
Example #4
0
    def get_v2_output(self, slvr_cfg, **kwargs):
        # Get visibilities from the v2 solver
        slvr_cfg = slvr_cfg.copy()
        slvr_cfg.update(**kwargs)
        slvr_cfg[Options.VERSION] = Options.VERSION_TWO
        with montblanc.rime_solver(slvr_cfg) as slvr:
            # Create and transfer lm to the solver
            lm = np.empty(shape=slvr.lm.shape, dtype=slvr.lm.dtype)
            l, m = lm[0,:], lm[1,:]
            l[:] = _L
            m[:] = _M
            slvr.transfer_lm(lm)

            # Create and transfer brightness to the solver
            B = np.empty(shape=slvr.brightness.shape, dtype=slvr.brightness.dtype)
            I, Q, U, V, alpha = B[0,:,:], B[1,:,:], B[2,:,:], B[3,:,:], B[4,:,:]
            I[:] = _I
            Q[:] = _Q
            U[:] = _U
            V[:] = _V
            alpha[:] = _ALPHA
            slvr.transfer_brightness(B)

            # Set the reference wavelength
            ref_wave = montblanc.constants.C / np.full(slvr.ref_wavelength.shape,
            	_REF_FREQ)
            slvr.transfer_ref_wavelength(ref_wave.astype(slvr.ref_wavelength.dtype))

            slvr.solve()

            return slvr.X2, slvr.retrieve_model_vis().transpose(1,2,3,0)
    def __init__(self, GD, npix, cell_size_rad, MS, pointing_sols):
        shndlrs = filter(lambda x: isinstance(x, logging.StreamHandler),
                         montblanc.log.handlers)
        montblanc.log.propagate = False
        log_levels = {
            "NOTSET": logging.NOTSET,
            "DEBUG": logging.DEBUG,
            "INFO": logging.INFO,
            "WARNING": logging.WARNING,
            "ERROR": logging.ERROR,
            "CRITICAL": logging.CRITICAL
        }
        for s in shndlrs:
            s.level = log_levels.get(GD["Montblanc"]["LogLevel"],
                                     logging.WARNING)

        apnd = "a" if GD["Log"]["Append"] else "w"
        lgname = GD["Output"]["Name"] + ".montblanc.log" \
                if GD["Montblanc"]["LogFile"] is None else GD["Montblanc"]["LogFile"]
        fhndlr = logging.FileHandler(lgname, mode=apnd)
        fhndlr.level = logging.DEBUG
        montblanc.log.addHandler(fhndlr)

        # configure solver
        self._mgr = DataDictionaryManager(MS, pointing_sols)
        self._slvr_cfg = slvr_cfg = montblanc.rime_solver_cfg(
            data_source="default",
            polarisation_type=self._mgr._solver_polarization_type,
            mem_budget=int(
                np.ceil(GD["Montblanc"]["MemoryBudget"] * 1024 * 1024 * 1024)),
            dtype=GD["Montblanc"]["SolverDType"],
            auto_correlations=True,
            version=GD["Montblanc"]["DriverVersion"])

        self._solver = montblanc.rime_solver(slvr_cfg)

        self._cell_size_rad = cell_size_rad
        self._npix = npix

        # Configure the Beam upfront
        if GD["Beam"]["Model"] == "FITS":
            fits_file_spec = GD["Beam"]["FITSFile"]
            l_axis = GD["Beam"]["FITSLAxis"]
            m_axis = GD["Beam"]["FITSMAxis"]
            self._beam_prov = FitsBeamSourceProvider(fits_file_spec,
                                                     l_axis=l_axis,
                                                     m_axis=m_axis)
        else:
            self._beam_prov = None
Example #6
0
    import argparse

    parser = argparse.ArgumentParser(description='RIME MS test script')
    parser.add_argument('msfile', help='Measurement Set File')
    parser.add_argument('-v','--version',dest='version', type=str,
        default=Options.VERSION_FOUR, choices=Options.VALID_VERSIONS,
        help='RIME Pipeline Version.')

    args = parser.parse_args(sys.argv[1:])

    # Get the solver.
    slvr_cfg = montblanc.rime_solver_cfg(msfile=args.msfile,
        sources=montblanc.sources(point=1, gaussian=0, sersic=0),
        dtype='double', version=args.version)

    with montblanc.rime_solver(slvr_cfg) as slvr:
        if args.version in [Options.VERSION_TWO]:
            lm = np.empty(shape=slvr.lm.shape, dtype=slvr.lm.dtype)
            l, m = lm[0,:], lm[1,:]
            l[:] = 0.1
            m[:] = 0.25

            slvr.transfer_lm(lm)

            B = np.empty(shape=slvr.brightness.shape, dtype=slvr.brightness.dtype)
            I, Q, U, V, alpha = B[0,:,:], B[1,:,:], B[2,:,:], B[3,:,:], B[4,:,:]
            I[:] = 2
            Q[:] = 1
            U[:] = 1
            V[:] = 1
            alpha[:] = 0.5
Example #7
0
    def get_v4_output(self, slvr_cfg, **kwargs):
        # Get visibilities from the v4 solver
        gpu_slvr_cfg = slvr_cfg.copy()
        gpu_slvr_cfg.update(**kwargs)
        gpu_slvr_cfg[Options.VERSION] = Options.VERSION_FOUR

        with FitsBeam(base_beam_file) as fb:
            beam_shape = fb.shape
            gpu_slvr_cfg[Options.E_BEAM_WIDTH] = beam_shape[0]
            gpu_slvr_cfg[Options.E_BEAM_HEIGHT] = beam_shape[1]
            gpu_slvr_cfg[Options.E_BEAM_DEPTH] = beam_shape[2]

            from montblanc.impl.rime.v4.cpu.CPUSolver import CPUSolver

            with montblanc.rime_solver(gpu_slvr_cfg) as slvr:

                cpu_slvr_cfg = slvr.config().copy()
                cpu_slvr_cfg[Options.DATA_SOURCE] = Options.DATA_SOURCE_EMPTY
                cpu_slvr = CPUSolver(cpu_slvr_cfg)

                # Create and transfer lm to the solver
                lm = np.empty(shape=slvr.lm.shape, dtype=slvr.lm.dtype)
                l, m = lm[:,0], lm[:,1]
                l[:] = _L
                m[:] = _M
                slvr.transfer_lm(lm)

                # Create and transfer stoke and alpha to the solver
                stokes = np.empty(shape=slvr.stokes.shape, dtype=slvr.stokes.dtype)
                alpha = np.empty(shape=slvr.alpha.shape, dtype=slvr.alpha.dtype)
                I, Q, U, V = stokes[:,:,0], stokes[:,:,1], stokes[:,:,2], stokes[:,:,3]
                I[:] = _I
                Q[:] = _Q
                U[:] = _U
                V[:] = _V
                alpha[:] = _ALPHA
                slvr.transfer_stokes(stokes)
                slvr.transfer_alpha(alpha)

                fb.reconfigure_frequency_axes(slvr.retrieve_frequency())

                # Create the beam from FITS file
                ebeam = np.zeros(shape=slvr.E_beam.shape, dtype=slvr.E_beam.dtype)
                ebeam.real[:] = fb.real()
                ebeam.imag[:] = fb.imag()
                slvr.transfer_E_beam(ebeam)

                # Configure the beam extents
                ll, lm, lf, ul, um, uf = fb.beam_extents

                slvr.set_beam_ll(ll)
                slvr.set_beam_lm(lm)
                slvr.set_beam_lfreq(lf)
                slvr.set_beam_ul(ul)
                slvr.set_beam_um(um)
                slvr.set_beam_ufreq(uf)

                # Set the reference frequency
                ref_freq = np.full(slvr.ref_frequency.shape, _REF_FREQ, dtype=slvr.ref_frequency.dtype)
                slvr.transfer_ref_frequency(ref_freq)

                from montblanc.solvers import copy_solver

                copy_solver(slvr, cpu_slvr)
                cpu_slvr.compute_E_beam()

                slvr.solve()

                return slvr.X2, slvr.retrieve_model_vis()
Example #8
0
def run_test(msfile, pol_type, **kwargs):
    """
    Parameters
    ----------
    msfile : str
        Name of the Measurement Set
    pol_type : str
        'linear' or 'circular'
    beam_file_schema (optional) : str
        Beam filename schema. Defaults to 'test_beam_$(corr)_$(reim).fits'
    overwrite_beams (optional) : bool
        If ``True`` create new beams using the cos**3 beam
    """


    #=========================================
    # Directory and Script Configuration
    #=========================================

    # Directory in which we expect our measurement set to be located
    meq_vis_column = 'MODEL_DATA'
    mb_vis_column = 'CORRECTED_DATA'

    # Directory in which meqtree-related files are read/written
    meq_dir = 'meqtrees'
    # Scripts
    meqpipe = 'meqtree-pipeliner.py'
    # Meqtree profile and script
    cfg_file = os.path.join(meq_dir, 'tdlconf.profiles')
    sim_script = os.path.join(meq_dir, 'turbo-sim.py')
    tigger_sky_file = os.path.join(meq_dir, 'sky_model.txt')

    # Is the beam enabled
    beam_on = kwargs.get('beam_on', True)
    beam_on = 1 if beam_on is True else 0

    # Directory in which we expect our beams to be located
    beam_file_schema = 'test_beam_$(corr)_$(reim).fits'

    # Beam file pattern
    beam_file_schema = kwargs.get("beam_file_schema", beam_file_schema)

    l_axis = kwargs.get('l_axis', '-X')
    m_axis = kwargs.get('m_axis', 'Y')

    # Find the location of the meqtree pipeliner script
    meqpipe_actual = subprocess.check_output(['which', meqpipe]).strip()
    cfg_section = '-'.join(('montblanc', 'compare', pol_type))

    #======================================================
    # Configure the beam files with frequencies from the MS
    #======================================================

    from montblanc.impl.rime.tensorflow.sources.fits_beam_source_provider import (
        _create_filenames, _open_fits_files)

    # Zero the visibility data
    with pt.table(msfile, ack=False, readonly=False) as T:
        data_desc = T.getcoldesc('DATA')

        try:
            shape = data_desc['shape'].tolist()
        except KeyError:
            shape = list(T.getcol('DATA', startrow=0, nrow=1).shape[1:])

        shape = [T.nrows()] + shape
        T.putcol(mb_vis_column, np.zeros(shape, dtype=np.complex64))
        T.putcol(meq_vis_column, np.zeros(shape, dtype=np.complex64))

    # Extract frequencies from the MS
    with pt.table(msfile + '::SPECTRAL_WINDOW', ack=False) as SW:
        frequency = SW.getcol('CHAN_FREQ')[0]

    bandwidth = frequency[-1] - frequency[0]

    overwrite_beams = kwargs.get('overwrite_beams', False)

    # Get filenames from pattern and open the files
    filenames = beam_factory(polarisation_type=pol_type,
                            frequency=frequency,
                            schema=beam_file_schema,
                            overwrite=overwrite_beams)

    #=========================================
    # Source Configuration
    #=========================================

    np.random.seed(0)
    dtype = np.float64
    ctype = np.complex128 if dtype == np.float64 else np.complex64

    def get_point_sources(nsrc):
        source_coords = np.empty(shape=(nsrc, 2), dtype=dtype)
        stokes = np.empty(shape=(nsrc, 4), dtype=dtype)
        I, Q, U, V = stokes[:,0], stokes[:,1], stokes[:,2], stokes[:,3]
        alphas = np.empty(shape=(nsrc,), dtype=dtype)
        ref_freq = np.empty(shape=(nsrc,), dtype=dtype)

        # Source coordinates between -45 and 45 degrees
        source_coords[:] = (rf(size=source_coords.shape) - 0.5)*90.0
        Q[:] = rf(size=Q.shape)*0.1
        U[:] = rf(size=U.shape)*0.1
        V[:] = rf(size=V.shape)*0.1
        I[:] = np.sqrt(Q**2 + U**2 + V**2)*1.5 + rf(size=I.shape)*0.1

        # Zero and invert selected stokes parameters
        if nsrc > 0:
            zero_srcs = np.random.randint(nsrc, size=(2,))
            source_coords[zero_srcs,:] = 0

            # Create sources with both positive and negative flux
            sign = 2*np.random.randint(2, size=I.shape) - 1
            I[:] *= sign

        alphas[:] = 2*(np.random.random(size=alphas.size) - 0.5)

        ref_freq[:] = 1.3e9 + np.random.random(ref_freq.size)*0.2e9

        return (np.deg2rad(source_coords), np.asarray(stokes),
                np.asarray(alphas), np.asarray(ref_freq))

    def get_gaussian_sources(nsrc):
        c, s, a, r= get_point_sources(nsrc)
        gauss_shape = np.empty(shape=(3, nsrc), dtype=np.float64)
        gauss_shape[:] = rf(size=gauss_shape.shape)
        return c, s, a, r, gauss_shape

    npsrc, ngsrc = 10, 10

    pt_lm, pt_stokes, pt_alpha, pt_ref_freq = get_point_sources(npsrc)

    assert pt_lm.shape == (npsrc, 2), pt_lm.shape
    assert pt_stokes.shape == (npsrc, 4), pt_stokes.shape
    assert pt_alpha.shape == (npsrc,), pt_alpha.shape
    assert pt_ref_freq.shape == (npsrc,), pt_ref_freq.shape

    g_lm, g_stokes, g_alpha, g_ref_freq, g_shape = get_gaussian_sources(ngsrc)

    #=========================================
    # Create Tigger ASCII sky model
    #=========================================

    from Tigger.Models.Formats.AIPSCCFITS import lm_to_radec

    # Need the phase centre for lm_to_radec
    with pt.table(msfile + '::FIELD', ack=False, readonly=True) as F:
        ra0, dec0 = F.getcol('PHASE_DIR')[0][0]

    # Create the tigger sky model
    with open(tigger_sky_file, 'w') as f:
        f.write('#format: ra_d dec_d i q u v spi freq0 emaj_s emin_s pa_d\n')
        it = enumerate(itertools.izip(pt_lm, pt_stokes, pt_alpha, pt_ref_freq))
        for i, ((l, m), (I, Q, U, V), alpha, ref_freq) in it:
            ra, dec = lm_to_radec(l, m, ra0, dec0)
            l, m = np.rad2deg([ra,dec])

            f.write('{l:.20f} {m:.20f} {i} {q} {u} {v} {spi} {rf:.20f}\n'.format(
                l=l, m=m, i=I, q=Q, u=U, v=V, spi=alpha, rf=ref_freq))

        it = enumerate(itertools.izip(g_lm, g_stokes, g_alpha, g_ref_freq, g_shape.T))
        for i, ((l, m), (I, Q, U, V), alpha, ref_freq, (emaj, emin, pa)) in it:
            ra, dec = lm_to_radec(l, m, ra0, dec0)
            l, m = np.rad2deg([ra,dec])
            # Convert to seconds
            emaj, emin = np.asarray([emaj, emin])*648000./np.pi
            # Convert to degrees
            pa *= 180.0/np.pi

            f.write('{l:.20f} {m:.20f} {i} {q} {u} {v} {spi} {rf:.20f} '
                    '{emaj} {emin} {pa}\n'.format(
                        l=l, m=m, i=I, q=Q, u=U, v=V, spi=alpha, rf=ref_freq,
                        emaj=emaj, emin=emin, pa=pa))


    #=========================================
    # Call MeqTrees
    #=========================================

    cmd_list = ['python',
        # Meqtree Pipeline script
        meqpipe_actual,
        # Configuration File
        '-c', cfg_file,
        # Configuration section
        '[{section}]'.format(section=cfg_section),
        # Enable the beam?
        'me.e_enable = {e}'.format(e=beam_on),
        # Measurement Set
        'ms_sel.msname={ms}'.format(ms=msfile),
        # Tigger sky file
        'tiggerlsm.filename={sm}'.format(sm=tigger_sky_file),
        # Output column
        'ms_sel.output_column={c}'.format(c=meq_vis_column),
        # Imaging Column
        'img_sel.imaging_column={c}'.format(c=meq_vis_column),
        # Beam FITS file pattern
        'pybeams_fits.filename_pattern={p}'.format(p=beam_file_schema),
        # FITS L and M AXIS
        'pybeams_fits.l_axis={l}'.format(l=l_axis),
        'pybeams_fits.m_axis={m}'.format(m=m_axis),
        sim_script,
        '=simulate'
        ]

    import montblanc

    from montblanc.impl.rime.tensorflow.ms import MeasurementSetManager
    from montblanc.impl.rime.tensorflow.sources import (SourceProvider,
        MSSourceProvider,
        FitsBeamSourceProvider,
        CachedSourceProvider)

    from montblanc.impl.rime.tensorflow.sinks import MSSinkProvider

    class RadioSourceProvider(SourceProvider):
        def name(self):
            return "RadioSourceProvider"

        def point_lm(self, context):
            lp, up = context.dim_extents('npsrc')
            return pt_lm[lp:up, :]

        def point_stokes(self, context):
            (lp, up), (lt, ut) = context.dim_extents('npsrc', 'ntime')
            return np.tile(pt_stokes[lp:up, np.newaxis, :], [1, ut-lt, 1])

        def point_alpha(self, context):
            (lp, up), (lt, ut) = context.dim_extents('npsrc', 'ntime')
            return np.tile(pt_alpha[lp:up, np.newaxis], [1, ut-lt])

        def point_ref_freq(self, context):
            (lp, up) = context.dim_extents('npsrc')
            return pt_ref_freq[lp:up]

        def gaussian_lm(self, context):
            lg, ug = context.dim_extents('ngsrc')
            return g_lm[lg:ug, :]

        def gaussian_stokes(self, context):
            (lg, ug), (lt, ut) = context.dim_extents('ngsrc', 'ntime')
            return np.tile(g_stokes[lg:ug, np.newaxis, :], [1, ut-lt, 1])

        def gaussian_alpha(self, context):
            (lg, ug), (lt, ut) = context.dim_extents('ngsrc', 'ntime')
            return np.tile(g_alpha[lg:ug, np.newaxis], [1, ut-lt])

        def gaussian_ref_freq(self, context):
            (lg, ug) = context.dim_extents('ngsrc')
            return g_ref_freq[lg:ug]

        def gaussian_shape(self, context):
            (lg, ug) = context.dim_extents('ngsrc')
            gauss_shape = g_shape[:,lg:ug]
            emaj = gauss_shape[0]
            emin = gauss_shape[1]
            pa = gauss_shape[2]

            gauss = np.empty(context.shape, dtype=context.dtype)

            gauss[0,:] = emaj * np.sin(pa)
            gauss[1,:] = emaj * np.cos(pa)
            emaj[emaj == 0.0] = 1.0
            gauss[2,:] = emin / emaj

            return gauss

        def updated_dimensions(self):
            return [('npsrc', pt_lm.shape[0]), ('ngsrc', g_lm.shape[0])]

    slvr_cfg = montblanc.rime_solver_cfg(
        mem_budget=1024*1024*1024,
        data_source='default',
        dtype='double' if dtype == np.float64 else 'float',
        polarisation_type=pol_type,
        auto_correlations=False,
        version='tf')

    slvr = montblanc.rime_solver(slvr_cfg)

    ms_mgr = MeasurementSetManager(msfile, slvr_cfg)

    source_providers = []
    source_providers.append(MSSourceProvider(ms_mgr))

    if beam_on == 1:
        beam_prov = FitsBeamSourceProvider(beam_file_schema,
            l_axis=l_axis, m_axis=m_axis)
        source_providers.append(beam_prov)

    source_providers.append(RadioSourceProvider())
    cache_prov = CachedSourceProvider(source_providers)
    source_providers = [cache_prov]

    sink_providers = [MSSinkProvider(ms_mgr, mb_vis_column)]
    slvr.solve(source_providers=source_providers,
        sink_providers=sink_providers)

    import time
    time.sleep(1)

    for obj in source_providers + sink_providers + [ms_mgr]:
        obj.close()

    # Call the meqtrees simulation script, dumping visibilities into MODEL_DATA
    subprocess.call(cmd_list)

    # Compare MeqTree and Montblanc visibilities
    with pt.table(msfile, ack=False, readonly=True) as MS:
        ntime, nbl, nchan = slvr.hypercube.dim_global_size('ntime', 'nbl', 'nchan')
        shape = (ntime, nbl, nchan, 4)
        meq_vis = MS.getcol(meq_vis_column).reshape(shape)
        mb_vis = MS.getcol(mb_vis_column).reshape(shape)

        # Compare
        close = np.isclose(meq_vis, mb_vis)
        not_close = np.invert(close)
        problems = np.nonzero(not_close)

        # Everything agrees, exit
        if problems[0].size == 0:
            print 'Montblanc and MeqTree visibilities agree'
            sys.exit(1)

        bad_vis_file = 'bad_visibilities.txt'

        # Some visibilities differ, do some analysis
        print ("Montblanc differs from MeqTrees by {nc}/{t} visibilities. "
            "Writing them out to '{bvf}'").format(
            nc=problems[0].size, t=not_close.size, bvf=bad_vis_file)

        abs_diff = np.abs(meq_vis - mb_vis)
        rmsd = np.sqrt(np.sum(abs_diff**2)/abs_diff.size)
        nrmsd = rmsd / (np.max(abs_diff) - np.min(abs_diff))
        print 'RMSD {rmsd} NRMSD {nrmsd}'.format(rmsd=rmsd, nrmsd=nrmsd)

        # Plot a histogram of the difference
        try:
            import matplotlib
            matplotlib.use('pdf')
            import matplotlib.pyplot as plt
        except:
            print "Exception importing matplotlib %s" % sys.exc_info()[2]
        else:
            try:
                nr_of_bins = 100
                n, bins, patches = plt.hist(abs_diff.flatten(),
                    bins=np.logspace(np.log10(1e-10), np.log10(1.0), nr_of_bins))

                plt.gca().set_xscale("log")
                plt.xlabel('Magnitude Difference')
                plt.ylabel('Counts')
                plt.grid(True)

                plt.savefig('histogram.pdf')
            except:
                print "Error plotting histogram %s" % sys.exc_info()[2]

        mb_problems = mb_vis[problems]
        meq_problems = meq_vis[problems]
        difference = mb_problems - meq_problems
        amplitude = np.abs(difference)

        # Create an iterator over the first 100 problematic visibilities
        t = (np.asarray(problems).T, mb_problems, meq_problems, difference, amplitude)
        it = enumerate(itertools.izip(*t))
        it = itertools.islice(it, 0, 1000, 1)

        # Write out the problematic visibilities to file
        with open(bad_vis_file, 'w') as f:
            for i, (p, mb, meq, d, amp) in it:
                f.write("{i} {t} Montblanc: {mb} MeqTrees: {meq} "
                    "Difference {d} Absolute Difference {ad} \n".format(
                        i=i, t=p, mb=mb, meq=meq, d=d, ad=amp))
Example #9
0
# ----------------------------------------------------------------------------------------------------------------------
# Begin Montblanc configuration
global slvr, stokes

slvr_cfg = montblanc.rime_solver_cfg(msfile=args.msfile,
                                     sources=montblanc.sources(point=0,
                                                               gaussian=0,
                                                               sersic=1),
                                     init_weights=None,
                                     weight_vector=False,
                                     sersic_gradient=False,
                                     dtype='double',
                                     version='v4')

with montblanc.rime_solver(slvr_cfg) as slvr:  # Read in observed visibilities
    ntime = slvr.dim_local_size('ntime')
    stokes = np.empty(shape=slvr.stokes.shape, dtype=slvr.stokes.dtype)
    I = stokes[:, :, 0]
    alpha = slvr.ft(np.ones(1 * ntime) * (-0.7)).reshape(1, ntime)
    slvr.transfer_alpha(alpha)

    # Let slvr know noise on visibilities, set visibility noise variance (muJy)
    time_acc = 60
    efficiency = 0.9
    channel_bandwidth_hz = 240e6
    SEFD = 400e6
    sigma = (SEFD * SEFD) / (2. * time_acc * channel_bandwidth_hz *
                             efficiency * efficiency)
    slvr.set_sigma_sqrd(sigma)