Exemplo n.º 1
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'))
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)
    
    t0 = time.time()
    meta_omnical, sol_omnical = info.omnical(d, sol0, 
                                             gain=.5, maxiter=500, check_after=30, check_every=6)
    print('OMNICAL no noise: %4.1f s' % (time.time() - t0), 
Exemplo n.º 7
0

# Overhead
RCOND = 1e-6
HEXNUM = 3
reds, antpos = build_reds_hex(HEXNUM)
nants = len(antpos)
antpos_omni = np.empty((nants, 3), dtype=np.float)
for i in antpos:
    antpos_omni[i] = antpos[i]
print '# Antennas:', nants

dtype = np.complex64
#dtype = np.complex128
gains, true_vis, d = redcal.sim_red_data(reds, ['xx'],
                                         shape=(30, 30),
                                         gain_scatter=.01)
d = {k: dk.astype(dtype) for k, dk in d.items()}
w = {k: 1. for k in d.keys()}
sol0 = {k: np.ones(v.shape, dtype=dtype) for k, v in gains.items()}

rc = redcal.RedundantCalibrator(reds, antpos)
sol0.update(rc.compute_ubls(d, sol0))
info = RedundantInfo()
info.init_from_reds(reds, antpos_omni)

cProfile.run(
    'meta1a, sol1a = rc.lincal(d, sol0, conv_crit=RCOND, sparse=False, verbose=False)',
    'redcal_hex%d_redcal.prf' % HEXNUM)
cProfile.run(
    'meta2b, gains1b, vis1b = omnical.calib.lincal(d, info, gains=sol0)',
Exemplo n.º 8
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)
Exemplo n.º 9
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)
Exemplo n.º 10
0
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))
    data = {k:v+0.5*redcal.noise((1,1)) for k,v in data.items()}

    redcalibrator = redcal.RedundantCalibrator(reds)
    sol_degen = redcalibrator.logcal(data)
    sol = redcalibrator.remove_degen(antpos, sol_degen, degen_sol=gains)

    subredcalib = redcal.RedundantCalibrator(subreds)
    subsol_degen = subredcalib.logcal(data)
    subsol = subredcalib.remove_degen(antpos, subsol_degen, degen_sol=gains)

    for a in range(Nant):
        input_gains[(a,'x')].append(gains[(a,'x')][0][0])
        subbl_gains[(a,'x')].append(subsol[(a,'x')][0][0])    
        allbl_gains[(a,'x')].append(sol[(a,'x')][0][0])