Пример #1
0
 def test_same(self):
     antpos = linear_array(NANTS)
     reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     info = redcal.RedundantCalibratorGPU(reds)
     shape = (NDATA, 1)
     gains, true_vis, d = sim_red_data(reds,
                                       gain_scatter=.0099999,
                                       shape=shape)
     w = dict([(k, 1.) for k in d.keys()])
     sol0 = dict([(k, np.ones_like(v)) for k, v in gains.items()])
     sol0.update(info.compute_ubls(d, sol0))
     for precision in (1, 2):
         conv_crit = 1e-12
         kwargs = {
             'maxiter': 100,
             'check_after': 10,
             'check_every': 4,
             'gain': 0.3,
             'conv_crit': conv_crit
         }
         meta_gpu, sol_gpu = info.omnical_gpu(d,
                                              sol0,
                                              precision=precision,
                                              **kwargs)
         meta_cpu, sol_cpu = info.omnical(d, sol0, **kwargs)
         for k in sol_cpu:
             np.testing.assert_almost_equal(sol_gpu[k],
                                            sol_cpu[k],
                                            decimal=5 * precision)
Пример #2
0
 def test_wrap(self):
     shape = (NDATA, 1)
     antpos = linear_array(NANTS)
     reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     info = redcal.RedundantCalibratorGPU(reds)
     gains, true_vis, d = sim_red_data(reds,
                                       gain_scatter=.0099999,
                                       shape=shape)
     w = dict([(k, 1.) for k in d.keys()])
     sol0 = dict([(k, np.ones_like(v)) for k, v in gains.items()])
     sol0.update(info.compute_ubls(d, sol0))
     meta, sol = info.omnical_gpu(d,
                                  sol0,
                                  conv_crit=1e-12,
                                  gain=.5,
                                  maxiter=500,
                                  check_after=30,
                                  check_every=6)
     # Leaving a reference to standard (non-GPU) omnical here in
     # case we want to compare GPU vs non-GPU for speed/accuracy.
     #meta, sol = info.omnical(d, sol0, conv_crit=1e-12, gain=.5, maxiter=500, check_after=30, check_every=6)
     for i in range(NANTS):
         assert sol[(i, 'Jxx')].shape == shape
     for bls in reds:
         ubl = sol[bls[0]]
         assert ubl.shape == shape
         for bl in bls:
             d_bl = d[bl]
             mdl = sol[(bl[0], 'Jxx')] * sol[(bl[1], 'Jxx')].conj() * ubl
             np.testing.assert_almost_equal(np.abs(d_bl),
                                            np.abs(mdl),
                                            decimal=10)
             np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()),
                                            0,
                                            decimal=10)
Пример #3
0
def generate_residual_IDR2_2(uvh5_file,
                             omni_vis,
                             omni_calfits,
                             abs_calfits,
                             outfile,
                             clobber=False):
    # reading uvh5 data file
    hd = HERAData(uvh5_file)
    data, flags, nsamples = hd.read(polarizations=['ee', 'nn'])

    # reading omnical model visibilities
    hd_oc = HERAData(omni_vis)
    omnivis, omnivis_flags, _ = hd_oc.read()

    uvo = pyuvdata.UVData()
    uvo.read_uvh5(omni_vis)

    # reading calfits file
    hc = HERACal(omni_calfits)
    oc_gains, oc_flags, oc_quals, oc_total_quals = hc.read()

    hc = HERACal(abs_calfits)
    ac_gains, ac_flags, ac_quals, ac_total_quals = hc.read()

    # calibrating the data
    abscal_data, abscal_flags = copy.deepcopy(data), copy.deepcopy(flags)
    calibrate_in_place(abscal_data,
                       ac_gains,
                       data_flags=abscal_flags,
                       cal_flags=ac_flags)

    res_data, res_flags = copy.deepcopy(hd.data_array), copy.deepcopy(
        hd.flag_array)
    resdata, resflags = copy.deepcopy(abscal_data), copy.deepcopy(abscal_flags)
    for i, p in enumerate(['ee', 'nn']):
        # reading omnical model visibilities
        hd_oc = HERAData(omni_vis)
        omnivis, omnivis_flags, _ = hd_oc.read(polarizations=[p])
        mod_bls = list(omnivis.keys())
        red_bls = get_reds(hd.antpos, pols=p)
        red = gr.RBL(red_bls)
        for mbl in mod_bls:
            bl_grp = red[tuple(mbl[0:2]) + ('J{}'.format(p), )]
            for blp in bl_grp:
                bl = (blp[0], blp[1], p)
                inds = hd.antpair2ind(bl)
                omnivis_scaled = omnivis[mbl] * oc_gains[(blp[0], 'J{}'.format(
                    p))] * np.conj(oc_gains[(blp[1], 'J{}'.format(p))])
                omnivis_scaled /= (
                    ac_gains[(blp[0], 'J{}'.format(p))] *
                    np.conj(ac_gains[(blp[1], 'J{}'.format(p))]))
                resdata[bl] = abscal_data[bl] - omnivis_scaled
                resflags[bl] = abscal_flags[bl]
                res_data[inds, 0, :, i] = resdata[bl]
                res_flags[inds, 0, :, i] = resflags[bl]

    # writing to file
    hd.data_array = res_data
    hd.flag_array = res_flags
    hd.write_uvh5(outfile, clobber=clobber)
Пример #4
0
def build_reds_hex(hexNum, sep=14.7):
    antpos, i = {}, 0
    for row in range(hexNum - 1, -(hexNum), -1):
        for col in range(2 * hexNum - abs(row) - 1):
            xPos = ((-(2 * hexNum - abs(row)) + 2) / 2.0 + col) * sep
            yPos = row * sep * 3**.5 / 2
            antpos[i] = np.array([xPos, yPos, 0])
            i += 1
    return redcal.get_reds(antpos), antpos
Пример #5
0
    def test_build_eq(self):
        antpos = build_linear_array(3)
        reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
        gains, true_vis, data = om.sim_red_data(reds)
        info = om.RedundantCalibrator(reds)
        eqs = info.build_eqs(data.keys())
        self.assertEqual(len(eqs), 3)
        self.assertEqual(eqs['g0x * g1x_ * u0xx'], (0, 1, 'xx'))
        self.assertEqual(eqs['g1x * g2x_ * u0xx'], (1, 2, 'xx'))
        self.assertEqual(eqs['g0x * g2x_ * u1xx'], (0, 2, 'xx'))

        reds = om.get_reds(antpos,
                           pols=['xx', 'yy', 'xy', 'yx'],
                           pol_mode='4pol')
        gains, true_vis, data = om.sim_red_data(reds)
        info = om.RedundantCalibrator(reds)
        eqs = info.build_eqs(data.keys())
        self.assertEqual(len(eqs), 3 * 4)
        self.assertEqual(eqs['g0x * g1y_ * u4xy'], (0, 1, 'xy'))
        self.assertEqual(eqs['g1x * g2y_ * u4xy'], (1, 2, 'xy'))
        self.assertEqual(eqs['g0x * g2y_ * u5xy'], (0, 2, 'xy'))
        self.assertEqual(eqs['g0y * g1x_ * u6yx'], (0, 1, 'yx'))
        self.assertEqual(eqs['g1y * g2x_ * u6yx'], (1, 2, 'yx'))
        self.assertEqual(eqs['g0y * g2x_ * u7yx'], (0, 2, 'yx'))

        reds = om.get_reds(antpos,
                           pols=['xx', 'yy', 'xy', 'yx'],
                           pol_mode='4pol_minV')
        gains, true_vis, data = om.sim_red_data(reds)
        info = om.RedundantCalibrator(reds)
        eqs = info.build_eqs(data.keys())
        self.assertEqual(len(eqs), 3 * 4)
        self.assertEqual(eqs['g0x * g1y_ * u4xy'], (0, 1, 'xy'))
        self.assertEqual(eqs['g1x * g2y_ * u4xy'], (1, 2, 'xy'))
        self.assertEqual(eqs['g0x * g2y_ * u5xy'], (0, 2, 'xy'))
        self.assertEqual(eqs['g0y * g1x_ * u4xy'], (0, 1, 'yx'))
        self.assertEqual(eqs['g1y * g2x_ * u4xy'], (1, 2, 'yx'))
        self.assertEqual(eqs['g0y * g2x_ * u5xy'], (0, 2, 'yx'))
Пример #6
0
 def test_logcal(self):
     NANTS = 18
     antpos = build_linear_array(NANTS)
     reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     info = om.RedundantCalibrator(reds)
     gains, true_vis, d = om.sim_red_data(reds, gain_scatter=.05)
     w = dict([(k, 1.) for k in d.keys()])
     sol = info.logcal(d)
     for i in xrange(NANTS):
         self.assertEqual(sol[(i, 'x')].shape, (10, 10))
     for bls in reds:
         ubl = sol[bls[0]]
         self.assertEqual(ubl.shape, (10, 10))
         for bl in bls:
             d_bl = d[bl]
             mdl = sol[(bl[0], 'x')] * sol[(bl[1], 'x')].conj() * ubl
             np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 10)
             np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0,
                                            10)
Пример #7
0
    def test_solver(self):
        antpos = build_linear_array(3)
        reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
        info = om.RedundantCalibrator(reds)
        gains, true_vis, d = om.sim_red_data(reds)
        w = {}
        w = dict([(k, 1.) for k in d.keys()])

        def solver(data, wgts, sparse, **kwargs):
            np.testing.assert_equal(data['g0x * g1x_ * u0xx'], d[0, 1, 'xx'])
            np.testing.assert_equal(data['g1x * g2x_ * u0xx'], d[1, 2, 'xx'])
            np.testing.assert_equal(data['g0x * g2x_ * u1xx'], d[0, 2, 'xx'])
            if len(wgts) == 0: return
            np.testing.assert_equal(wgts['g0x * g1x_ * u0xx'], w[0, 1, 'xx'])
            np.testing.assert_equal(wgts['g1x * g2x_ * u0xx'], w[1, 2, 'xx'])
            np.testing.assert_equal(wgts['g0x * g2x_ * u1xx'], w[0, 2, 'xx'])
            return

        info._solver(solver, d)
        info._solver(solver, d, w)
Пример #8
0
 def test_omnilogcal(self):
     NANTS = 18
     antpos = build_linear_array(NANTS)
     hcreds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     pols = ['x']
     antpos_ideal = np.array(antpos.values())
     xs, ys, zs = antpos_ideal.T
     layout = np.arange(len(xs))
     antpos = -np.ones((NANTS * len(pols), 3))
     for ant, x, y in zip(layout.flatten(), xs.flatten(), ys.flatten()):
         for z, pol in enumerate(pols):
             z = 2**z  # exponential ensures diff xpols aren't redundant w/ each other
             i = hera_cal.omni.Antpol(ant, pol, NANTS)
             antpos[int(i), 0], antpos[int(i), 1], antpos[int(i), 2] = x, y, z
     reds = hera_cal.omni.compute_reds(NANTS, pols, antpos[:NANTS], tol=.01)
     # reds = hera_cal.omni.filter_reds(reds, **kwargs)
     info = hera_cal.omni.RedundantInfo(NANTS)
     info.init_from_reds(reds, antpos_ideal)
     # info = om.RedundantCalibrator(reds)
     # gains, true_vis, d = om.sim_red_data(hcreds, shape=SHAPE, gain_scatter=.05)
     data = {}
     for key in d.keys():
         if not data.has_key(key[:2]):
             data[key[:2]] = {}
         data[key[:2]][key[-1]] = d[key].astype(np.complex64)
     t0 = time.time()
     for i in xrange(1):
         m1, g1, v1 = omnical.calib.logcal(data, info)
     # print('omnilogcal', time.time() - t0)
     for i in xrange(NANTS):
         self.assertEqual(g1['x'][i].shape, SHAPE)
     for bls in reds:
         ubl = v1['xx'][(int(bls[0][0]), int(bls[0][1]))]
         self.assertEqual(ubl.shape, SHAPE)
         for bl in bls:
             d_bl = data[(int(bl[0]), int(bl[1]))]['xx']
             mdl = g1['x'][bl[0]] * g1['x'][bl[1]].conj() * ubl
             np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 5)
             np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0, 5)
Пример #9
0
def calfits_to_flags(JD_time, cal_type, pol='ee', add_bad_ants=None):
    """Returns flags array from calfits file

    :param JD_time: Fractional Julian date
    :type JD_time: float, str
    :param cal_type: Calibration process that produced the calfits file {"first",
    "omni", "abs", "flagged_abs", "smooth_abs"}
    :type cal_type: str
    :param pol: Polarization of data
    :type pol: str
    :param add_bad_ants: Additional bad antennas
    :type add_bad_ants: None, int, list, ndarray

    :return: Flags array
    :rtype: ndarray
    """

    zen_fn = find_zen_file(JD_time)
    flags_fn = find_flag_file(JD_time, cal_type)
    bad_ants = get_bad_ants(zen_fn)
    if add_bad_ants is not None:
        bad_ants = numpy.sort(numpy.append(bad_ants,
                                           numpy.array(add_bad_ants)))

    hc = HERACal(flags_fn)
    _, cal_flags, _, _ = hc.read()

    hd = HERAData(zen_fn)
    reds = get_reds(hd.antpos, pols=[pol])
    reds = fltBad(reds, bad_ants)
    redg = groupBls(reds)

    antpairs = redg[:, 1:]
    cflag = numpy.empty((hd.Nfreqs, hd.Ntimes, redg.shape[0]), dtype=bool)
    for g in range(redg.shape[0]):
        cflag[:, :, g] = cal_flags[(int(antpairs[g, 0]), 'J{}'.format(pol)) or \
                                   (int(antpairs[g, 1]), 'J{}'.format(pol))].transpose()

    return cflag
Пример #10
0
 def test_lincal(self):
     NANTS = 18
     antpos = build_linear_array(NANTS)
     reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     info = om.RedundantCalibrator(reds)
     gains, true_vis, d = om.sim_red_data(reds, gain_scatter=.0099999)
     w = dict([(k, 1.) for k in d.keys()])
     sol0 = dict([(k, np.ones_like(v)) for k, v in gains.items()])
     sol0.update(info.compute_ubls(d, sol0))
     #sol0 = info.logcal(d)
     #for k in sol0: sol0[k] += .01*capo.oqe.noise(sol0[k].shape)
     meta, sol = info.lincal(d, sol0)
     for i in xrange(NANTS):
         self.assertEqual(sol[(i, 'x')].shape, (10, 10))
     for bls in reds:
         ubl = sol[bls[0]]
         self.assertEqual(ubl.shape, (10, 10))
         for bl in bls:
             d_bl = d[bl]
             mdl = sol[(bl[0], 'x')] * sol[(bl[1], 'x')].conj() * ubl
             np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 10)
             np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0,
                                            10)
Пример #11
0
 def test_logcal(self):
     NANTS = 18
     antpos = build_linear_array(NANTS)
     reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     info = om.RedundantCalibrator(reds)
     gains, true_vis, d = om.sim_red_data(reds, shape=SHAPE, gain_scatter=.05)
     d = {key: value.astype(np.complex64) for key, value in d.items()}
     w = dict([(k, 1.) for k in d.keys()])
     t0 = time.time()
     for i in xrange(1):
         sol = info.logcal(d)
     # print('logcal', time.time() - t0)
     for i in xrange(NANTS):
         self.assertEqual(sol[(i, 'x')].shape, SHAPE)
     for bls in reds:
         ubl = sol[bls[0]]
         self.assertEqual(ubl.shape, SHAPE)
         for bl in bls:
             d_bl = d[bl]
             mdl = sol[(bl[0], 'x')] * sol[(bl[1], 'x')].conj() * ubl
             # np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 10)
             # np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0, 10)
             np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 5)
             np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0, 5)
Пример #12
0
 def test_omnical(self):
     NANTS = 18
     antpos = build_linear_array(NANTS)
     reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
     info = om.RedundantCalibrator(reds)
     # gains, true_vis, d = om.sim_red_data(reds, shape=SHAPE, gain_scatter=.0099999)
     # d = {key:value.astype(np.complex64) for key,value in d.items()}
     w = dict([(k, 1.) for k in d.keys()])
     sol0 = dict([(k, np.ones_like(v)) for k, v in gains.items()])
     sol0.update(info.compute_ubls(d, sol0))
     sol0 = {k: v.astype(np.complex64) for k, v in sol0.items()}
     meta, sol = info.omnical(d, sol0, gain=.5, maxiter=500, check_after=30, check_every=6)
     # meta, sol = info.omnical(d, sol0, gain=.5, maxiter=50, check_after=1, check_every=1)
     # print(meta)
     for i in xrange(NANTS):
         self.assertEqual(sol[(i, 'x')].shape, SHAPE)
     for bls in reds:
         ubl = sol[bls[0]]
         self.assertEqual(ubl.shape, SHAPE)
         for bl in bls:
             d_bl = d[bl]
             mdl = sol[(bl[0], 'x')] * sol[(bl[1], 'x')].conj() * ubl
             np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 5)
             np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0, 5)
Пример #13
0
import numpy as np
import cPickle as cp
from hera_cal import redcal

print 'Initializing variables..'
fqs  = .15 #GHz
lsts = np.pi/4
Nsim = 2**16
Nant = 37 

# Setup all variables
ants   = np.loadtxt('antenna_positions_37.dat')
antpos = {k:v for k,v in zip(range(37),ants)}
reds   = redcal.get_reds(antpos)
subreds = [reds[0], reds[1], reds[2]]

input_gains = {}
allbl_gains = {}
subbl_gains = {}

for a in range(Nant):
    input_gains[(a,'x')] = []
    allbl_gains[(a,'x')] = []
    subbl_gains[(a,'x')] = []

# Run simulation

print 'Starting simulation..'
for i in range(Nsim):
    print i
    gains, vis, data = redcal.sim_red_data(reds, shape=(1,1))
Пример #14
0
import numpy as np
import time

np.random.seed(0)
SHAPE = (10,2048)
NOISE = 1e-3

for Nants in [37, 128, 243, 350]:

    ants = np.loadtxt('antenna_positions_%d.dat'%Nants)
    idxs = np.arange(Nants)
    antpos = {}
    for k,v in zip(idxs,ants):
        antpos[k] = v

    reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')

    info = om.RedundantCalibrator(reds)

    gains, true_vis, d = om.sim_red_data(reds, shape=SHAPE, gain_scatter=.01)
    d = {key: value.astype(np.complex64) for key,value in d.items()}
    d_nos = {key: value + NOISE * om.noise(value.shape) for key,value in d.items()}
    d_nos = {key: value.astype(np.complex64) for key,value in d_nos.items()}

    w = dict([(k, np.float32(1.)) for k in d.keys()])
    sol0 = dict([(k, np.ones_like(v)) for k, v in gains.items()])
    sol0.update(info.compute_ubls(d, sol0))
    sol0 = {k:v.astype(np.complex64) for k,v in sol0.items()}
    
    print('NANTS: %d'%Nants)
    
Пример #15
0
    def test_sim_red_data(self):
        antpos = build_linear_array(10)
        reds = om.get_reds(antpos, pols=['xx'], pol_mode='1pol')
        gains, true_vis, data = om.sim_red_data(reds)
        self.assertEqual(len(gains), 10)
        self.assertEqual(len(data), 45)
        for bls in reds:
            bl0 = bls[0]
            ai, aj, pol = bl0
            ans0 = data[bl0] / (gains[(ai, 'x')] * gains[(aj, 'x')].conj())
            for bl in bls[1:]:
                ai, aj, pol = bl
                ans = data[bl] / (gains[(ai, 'x')] * gains[(aj, 'x')].conj())
                np.testing.assert_almost_equal(ans0, ans, 7)

        reds = om.get_reds(antpos,
                           pols=['xx', 'yy', 'xy', 'yx'],
                           pol_mode='4pol')
        gains, true_vis, data = om.sim_red_data(reds)
        self.assertEqual(len(gains), 20)
        self.assertEqual(len(data), 4 * (45))
        for bls in reds:
            bl0 = bls[0]
            ai, aj, pol = bl0
            ans0xx = data[(
                ai,
                aj,
                'xx',
            )] / (gains[(ai, 'x')] * gains[(aj, 'x')].conj())
            ans0xy = data[(
                ai,
                aj,
                'xy',
            )] / (gains[(ai, 'x')] * gains[(aj, 'y')].conj())
            ans0yx = data[(
                ai,
                aj,
                'yx',
            )] / (gains[(ai, 'y')] * gains[(aj, 'x')].conj())
            ans0yy = data[(
                ai,
                aj,
                'yy',
            )] / (gains[(ai, 'y')] * gains[(aj, 'y')].conj())
            for bl in bls[1:]:
                ai, aj, pol = bl
                ans_xx = data[(
                    ai,
                    aj,
                    'xx',
                )] / (gains[(ai, 'x')] * gains[(aj, 'x')].conj())
                ans_xy = data[(
                    ai,
                    aj,
                    'xy',
                )] / (gains[(ai, 'x')] * gains[(aj, 'y')].conj())
                ans_yx = data[(
                    ai,
                    aj,
                    'yx',
                )] / (gains[(ai, 'y')] * gains[(aj, 'x')].conj())
                ans_yy = data[(
                    ai,
                    aj,
                    'yy',
                )] / (gains[(ai, 'y')] * gains[(aj, 'y')].conj())
                np.testing.assert_almost_equal(ans0xx, ans_xx, 7)
                np.testing.assert_almost_equal(ans0xy, ans_xy, 7)
                np.testing.assert_almost_equal(ans0yx, ans_yx, 7)
                np.testing.assert_almost_equal(ans0yy, ans_yy, 7)

        reds = om.get_reds(antpos,
                           pols=['xx', 'yy', 'xy', 'yx'],
                           pol_mode='4pol_minV')
        gains, true_vis, data = om.sim_red_data(reds)
        self.assertEqual(len(gains), 20)
        self.assertEqual(len(data), 4 * (45))
        for bls in reds:
            bl0 = bls[0]
            ai, aj, pol = bl0
            ans0xx = data[(
                ai,
                aj,
                'xx',
            )] / (gains[(ai, 'x')] * gains[(aj, 'x')].conj())
            ans0xy = data[(
                ai,
                aj,
                'xy',
            )] / (gains[(ai, 'x')] * gains[(aj, 'y')].conj())
            ans0yx = data[(
                ai,
                aj,
                'yx',
            )] / (gains[(ai, 'y')] * gains[(aj, 'x')].conj())
            ans0yy = data[(
                ai,
                aj,
                'yy',
            )] / (gains[(ai, 'y')] * gains[(aj, 'y')].conj())
            np.testing.assert_almost_equal(ans0xy, ans0yx, 7)
            for bl in bls[1:]:
                ai, aj, pol = bl
                ans_xx = data[(
                    ai,
                    aj,
                    'xx',
                )] / (gains[(ai, 'x')] * gains[(aj, 'x')].conj())
                ans_xy = data[(
                    ai,
                    aj,
                    'xy',
                )] / (gains[(ai, 'x')] * gains[(aj, 'y')].conj())
                ans_yx = data[(
                    ai,
                    aj,
                    'yx',
                )] / (gains[(ai, 'y')] * gains[(aj, 'x')].conj())
                ans_yy = data[(
                    ai,
                    aj,
                    'yy',
                )] / (gains[(ai, 'y')] * gains[(aj, 'y')].conj())
                np.testing.assert_almost_equal(ans0xx, ans_xx, 7)
                np.testing.assert_almost_equal(ans0xy, ans_xy, 7)
                np.testing.assert_almost_equal(ans0yx, ans_yx, 7)
                np.testing.assert_almost_equal(ans0yy, ans_yy, 7)
Пример #16
0
    def test_sim_red_data(self):
        # Test that redundant baselines are redundant up to the gains in single pol mode
        from hera_cal import redcal as om
        antpos = linear_array(5)
        reds = om.get_reds(antpos, pols=['nn'], pol_mode='1pol')
        gains, true_vis, data = vis.sim_red_data(reds)
        assert len(gains) == 5
        assert len(data) == 10
        for bls in reds:
            bl0 = bls[0]
            ai, aj, pol = bl0
            ans0 = data[bl0] / (gains[(ai, 'Jnn')] * gains[(aj, 'Jnn')].conj())
            for bl in bls[1:]:
                ai, aj, pol = bl
                ans = data[bl] / (gains[(ai, 'Jnn')] *
                                  gains[(aj, 'Jnn')].conj())
                # compare calibrated visibilities knowing the input gains
                np.testing.assert_almost_equal(ans0, ans, decimal=7)

        # Test that redundant baselines are redundant up to the gains in 4-pol mode
        reds = om.get_reds(antpos,
                           pols=['xx', 'yy', 'xy', 'yx'],
                           pol_mode='4pol')
        gains, true_vis, data = vis.sim_red_data(reds)
        assert len(gains) == 2 * (5)
        assert len(data) == 4 * (10)
        for bls in reds:
            bl0 = bls[0]
            ai, aj, pol = bl0
            ans0xx = data[(
                ai,
                aj,
                'xx',
            )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jxx')].conj())
            ans0xy = data[(
                ai,
                aj,
                'xy',
            )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jyy')].conj())
            ans0yx = data[(
                ai,
                aj,
                'yx',
            )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jxx')].conj())
            ans0yy = data[(
                ai,
                aj,
                'yy',
            )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jyy')].conj())
            for bl in bls[1:]:
                ai, aj, pol = bl
                ans_xx = data[(
                    ai,
                    aj,
                    'xx',
                )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jxx')].conj())
                ans_xy = data[(
                    ai,
                    aj,
                    'xy',
                )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jyy')].conj())
                ans_yx = data[(
                    ai,
                    aj,
                    'yx',
                )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jxx')].conj())
                ans_yy = data[(
                    ai,
                    aj,
                    'yy',
                )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jyy')].conj())
                # compare calibrated visibilities knowing the input gains
                np.testing.assert_almost_equal(ans0xx, ans_xx, decimal=7)
                np.testing.assert_almost_equal(ans0xy, ans_xy, decimal=7)
                np.testing.assert_almost_equal(ans0yx, ans_yx, decimal=7)
                np.testing.assert_almost_equal(ans0yy, ans_yy, decimal=7)

        # Test that redundant baselines are redundant up to the gains in 4-pol minV mode (where
        # Vxy = Vyx)
        reds = om.get_reds(antpos,
                           pols=['xx', 'yy', 'xy', 'yX'],
                           pol_mode='4pol_minV')
        gains, true_vis, data = vis.sim_red_data(reds)
        assert len(gains) == 2 * (5)
        assert len(data) == 4 * (10)
        for bls in reds:
            bl0 = bls[0]
            ai, aj, pol = bl0
            ans0xx = data[(
                ai,
                aj,
                'xx',
            )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jxx')].conj())
            ans0xy = data[(
                ai,
                aj,
                'xy',
            )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jyy')].conj())
            ans0yx = data[(
                ai,
                aj,
                'yx',
            )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jxx')].conj())
            ans0yy = data[(
                ai,
                aj,
                'yy',
            )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jyy')].conj())
            np.testing.assert_almost_equal(ans0xy, ans0yx, decimal=7)
            for bl in bls[1:]:
                ai, aj, pol = bl
                ans_xx = data[(
                    ai,
                    aj,
                    'xx',
                )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jxx')].conj())
                ans_xy = data[(
                    ai,
                    aj,
                    'xy',
                )] / (gains[(ai, 'Jxx')] * gains[(aj, 'Jyy')].conj())
                ans_yx = data[(
                    ai,
                    aj,
                    'yx',
                )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jxx')].conj())
                ans_yy = data[(
                    ai,
                    aj,
                    'yy',
                )] / (gains[(ai, 'Jyy')] * gains[(aj, 'Jyy')].conj())
                # compare calibrated visibilities knowing the input gains
                np.testing.assert_almost_equal(ans0xx, ans_xx, decimal=7)
                np.testing.assert_almost_equal(ans0xy, ans_xy, decimal=7)
                np.testing.assert_almost_equal(ans0yx, ans_yx, decimal=7)
                np.testing.assert_almost_equal(ans0yy, ans_yy, decimal=7)
Пример #17
0
    def test_lincal_hex_end_to_end_2pol_with_remove_degen_and_firstcal(self):

        antpos = build_hex_array(3)
        reds = om.get_reds(antpos, pols=['xx', 'yy'], pol_mode='2pol')
        rc = om.RedundantCalibrator(reds)
        freqs = np.linspace(.1, .2, 10)
        gains, true_vis, d = om.sim_red_data(reds,
                                             gain_scatter=.1,
                                             shape=(1, len(freqs)))
        fc_delays = {ant: 100 * np.random.randn()
                     for ant in gains.keys()}  #in ns
        fc_gains = {
            ant: np.reshape(np.exp(-2.0j * np.pi * freqs * delay),
                            (1, len(freqs)))
            for ant, delay in fc_delays.items()
        }
        for ant1, ant2, pol in d.keys():
            d[(ant1, ant2, pol)] *= fc_gains[(ant1, pol[0])] * np.conj(
                fc_gains[(ant2, pol[1])])
        for ant in gains.keys():
            gains[ant] *= fc_gains[ant]

        w = dict([(k, 1.) for k in d.keys()])
        sol0 = rc.logcal(d, sol0=fc_gains, wgts=w)
        meta, sol = rc.lincal(d, sol0, wgts=w)

        np.testing.assert_array_less(meta['iter'],
                                     50 * np.ones_like(meta['iter']))
        np.testing.assert_almost_equal(meta['chisq'],
                                       np.zeros_like(meta['chisq']),
                                       decimal=10)

        np.testing.assert_almost_equal(meta['chisq'], 0, 10)
        for i in xrange(len(antpos)):
            self.assertEqual(sol[(i, 'x')].shape, (1, len(freqs)))
            self.assertEqual(sol[(i, 'y')].shape, (1, len(freqs)))
        for bls in reds:
            for bl in bls:
                ubl = sol[bls[0]]
                self.assertEqual(ubl.shape, (1, len(freqs)))
                d_bl = d[bl]
                mdl = sol[(bl[0], bl[2][0])] * sol[(bl[1],
                                                    bl[2][1])].conj() * ubl
                np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 10)
                np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0,
                                               10)

        sol_rd = rc.remove_degen(antpos, sol)

        ants = [key for key in sol_rd.keys() if len(key) == 2]
        gainPols = np.array([ant[1] for ant in ants])
        bl_pairs = [key for key in sol.keys() if len(key) == 3]
        visPols = np.array([[bl[2][0], bl[2][1]] for bl in bl_pairs])
        bl_vecs = np.array(
            [antpos[bl_pair[0]] - antpos[bl_pair[1]] for bl_pair in bl_pairs])
        gainSols = np.array([sol_rd[ant] for ant in ants])
        g, v = om.get_gains_and_vis_from_sol(sol_rd)

        meanSqAmplitude = np.mean([
            np.abs(g[key1] * g[key2]) for key1 in g.keys()
            for key2 in g.keys()
            if key1[1] == 'x' and key2[1] == 'x' and key1[0] != key2[0]
        ],
                                  axis=0)
        np.testing.assert_almost_equal(meanSqAmplitude, 1, 10)
        meanSqAmplitude = np.mean([
            np.abs(g[key1] * g[key2]) for key1 in g.keys()
            for key2 in g.keys()
            if key1[1] == 'y' and key2[1] == 'y' and key1[0] != key2[0]
        ],
                                  axis=0)
        np.testing.assert_almost_equal(meanSqAmplitude, 1, 10)
        #np.testing.assert_almost_equal(np.mean(np.angle(gainSols[gainPols=='x']), axis=0), 0, 10)
        #np.testing.assert_almost_equal(np.mean(np.angle(gainSols[gainPols=='y']), axis=0), 0, 10)

        for bls in reds:
            for bl in bls:
                ubl = sol_rd[bls[0]]
                self.assertEqual(ubl.shape, (1, len(freqs)))
                d_bl = d[bl]
                mdl = sol_rd[(bl[0], bl[2][0])] * sol_rd[
                    (bl[1], bl[2][1])].conj() * ubl
                np.testing.assert_almost_equal(np.abs(d_bl), np.abs(mdl), 10)
                np.testing.assert_almost_equal(np.angle(d_bl * mdl.conj()), 0,
                                               10)

        sol_rd = rc.remove_degen(antpos, sol, degen_sol=gains)
        g, v = om.get_gains_and_vis_from_sol(sol_rd)
        gainSols = np.array([sol_rd[ant] for ant in ants])
        degenGains = np.array([gains[ant] for ant in ants])

        meanSqAmplitude = np.mean([
            np.abs(g[key1] * g[key2]) for key1 in g.keys()
            for key2 in g.keys()
            if key1[1] == 'x' and key2[1] == 'x' and key1[0] != key2[0]
        ],
                                  axis=0)
        degenMeanSqAmplitude = np.mean([
            np.abs(gains[key1] * gains[key2]) for key1 in g.keys()
            for key2 in g.keys()
            if key1[1] == 'x' and key2[1] == 'x' and key1[0] != key2[0]
        ],
                                       axis=0)
        np.testing.assert_almost_equal(meanSqAmplitude, degenMeanSqAmplitude,
                                       10)
        meanSqAmplitude = np.mean([
            np.abs(g[key1] * g[key2]) for key1 in g.keys()
            for key2 in g.keys()
            if key1[1] == 'y' and key2[1] == 'y' and key1[0] != key2[0]
        ],
                                  axis=0)
        degenMeanSqAmplitude = np.mean([
            np.abs(gains[key1] * gains[key2]) for key1 in g.keys()
            for key2 in g.keys()
            if key1[1] == 'y' and key2[1] == 'y' and key1[0] != key2[0]
        ],
                                       axis=0)
        np.testing.assert_almost_equal(meanSqAmplitude, degenMeanSqAmplitude,
                                       10)

        np.testing.assert_almost_equal(
            np.mean(np.angle(gainSols[gainPols == 'x']), axis=0),
            np.mean(np.angle(degenGains[gainPols == 'x']), axis=0), 10)
        np.testing.assert_almost_equal(
            np.mean(np.angle(gainSols[gainPols == 'y']), axis=0),
            np.mean(np.angle(degenGains[gainPols == 'y']), axis=0), 10)

        for key, val in sol_rd.items():
            if len(key) == 2:
                np.testing.assert_almost_equal(val, gains[key], 10)
            if len(key) == 3:
                np.testing.assert_almost_equal(val, true_vis[key], 10)