def test_redcal_xtalk(self): antpos = np.array([[0., 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0]]) d = { (1, 2): np.array([[1.]], dtype=np.complex64), (2, 3): np.array([[1. + 1j]], dtype=np.complex64) } x = { (1, 2): np.array([[0.]], dtype=np.complex64), (2, 3): np.array([[0. + 1j]], dtype=np.complex64) } reds = [[(1, 2), (2, 3)]] info = Oc.RedundantInfo() info.init_from_reds(reds, antpos) m, g, v = Oc.redcal(d, info, xtalk=x, uselogcal=False) self.assertEqual(g[1][0, 0], 1.) self.assertEqual(g[2][0, 0], 1.) self.assertEqual(g[3][0, 0], 1.) #2D array testing d = { (1, 2): np.array([[1., 2], [3., 4]], dtype=np.complex64), (2, 3): np.array([[1. + 1j, 2 + 2j], [3 + 3j, 4 + 4j]], dtype=np.complex64) } x = { (1, 2): np.array([[0., 2], [2., 3]], dtype=np.complex64), (2, 3): np.array([[0. + 1j, 1 + 2j], [2 + 3j, 3 + 4j]], dtype=np.complex64) } m, g, v = Oc.redcal(d, info, xtalk=x, uselogcal=False) self.assertEqual(g[1][0, 0], 1.) self.assertEqual(g[2][0, 0], 1.) self.assertEqual(g[3][0, 0], 1.) self.assertEqual(m['res'][(2, 3)][0][0], 0.)
def test_redcal(self): #check that logcal give 0 chi2 for all 20 testinfos for index in range(20): arrayinfopath = os.path.dirname( os.path.realpath(__file__)) + '/testinfo/test' + str( index + 1) + '_array_info.txt' c = Oc.RedundantCalibrator(56) c.compute_redundantinfo(arrayinfopath, tol=.1) info = Oc.RedundantInfo() info.init_from_reds(c.Info.get_reds(), c.Info.get_antpos()) npz = np.load(testdata % index) bls = [tuple(bl) for bl in npz['bls']] dd = dict(zip(bls, npz['vis'])) m, g, v = Oc.redcal(dd, info, removedegen=True, maxiter=50, stepsize=.2, conv=1e-5, uselogcal=True) calparpath = os.path.dirname(os.path.realpath( __file__)) + '/testinfo/test' + str(index + 1) + '_calpar.txt' with open(calparpath) as f: rawinfo = [[float(x) for x in line.split()] for line in f] temp = np.array(rawinfo[:-1]) correctcalpar = (np.array(temp[:, 0]) + 1.0j * np.array(temp[:, 1])) i = list(g.keys())[0] scalar = correctcalpar[i].real / g[i].real for i in range(56): if not i in g: continue np.testing.assert_almost_equal( np.abs(correctcalpar[i] - g[i] * scalar), 0, 4)
def setUp(self): self.info = Oi.RedundantInfoLegacy(filename=redinfo_psa32, txtmode=True) self.info2 = Oc.RedundantInfo() self.info2.init_from_reds([[ (0, 4), (1, 5), (2, 6), (3, 7), (4, 8), (5, 9) ], [(0, 3), (1, 4), (2, 5), (3, 6), (4, 7), (5, 8), (6, 9)], [(0, 6), (1, 7), (2, 8), (3, 9)], [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)], [(0, 8), (1, 9)], [(0, 7), (1, 8), (2, 9)], [(0, 2), (1, 3), (2, 4), (3, 5), (4, 6), (5, 7), (6, 8), (7, 9)], [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9)]], np.array([[0., 0., 1.], [0., 50., 1.], [0., 100., 1.], [0., 150., 1.], [0., 200., 1.], [0., 250., 1.], [0., 300., 1.], [0., 350., 1.], [0., 400., 1.], [0., 450., 1.]])) self.freqs = np.linspace(.1, .2, 16) self.times = np.arange(4) self.reds = self.info2.get_reds() self.true_vis = {} for i, rg in enumerate(self.reds): rd = np.array( np.random.randn(self.times.size, self.freqs.size) + 1j * np.random.randn(self.times.size, self.freqs.size), dtype=np.complex64) self.true_vis[rg[0]] = rd self.true_gains = { i: np.ones((self.times.size, self.freqs.size), dtype=np.complex64) for i in self.info2.subsetant } self.data = {} self.bl2red = {} for rg in self.reds: for r in rg: self.bl2red[r] = rg[0] for redgp in self.reds: for ai, aj in redgp: self.data[ai, aj] = self.true_vis[self.bl2red[ ai, aj]] * self.true_gains[ai] * np.conj( self.true_gains[aj]) self.unitgains = { ant: np.ones((self.times.size, self.freqs.size), dtype=np.complex64) for ant in self.info2.subsetant } self.unitdata = {(ai, aj): np.ones((self.times.size, self.freqs.size), dtype=np.complex64) for ai, aj in self.info2.bl_order()}
def test_order_data(self): antpos = np.array([[0., 0, 0], [1, 0, 0], [2, 0, 0], [3, 0, 0]]) reds = [[(0, 1), (1, 2), (2, 3)], [(0, 2), (1, 3)]] i = Oc.RedundantInfo() i.init_from_reds(reds, antpos) dd = { (0, 1): np.array([[0, 1j]]), (1, 2): np.array([[0, 1j]]), (2, 3): np.array([[0, 1j]]), (2, 0): np.array([[0, 1j]]), (1, 3): np.array([[0, 1j]]), } d = i.order_data(dd) self.assertTrue(np.all(d[..., 0] == np.array([[0, 1j]]))) self.assertTrue(np.all(d[..., 1] == np.array([[0, 1j]]))) self.assertTrue(np.all(d[..., 2] == np.array([[0, 1j]]))) self.assertTrue(np.all(d[..., 3] == np.array([[0, 1j]]).conj())) self.assertTrue(np.all(d[..., 4] == np.array([[0, 1j]])))
def test_full_functionality(self): antpos = np.array([[14.60000038, -25.28794098, 1.], [21.89999962, -12.64397049, 1.], [14.60000038, 25.28794098, 1.], [-21.89999962, -12.64397049, 1.], [-14.60000038, 0., 1.], [21.89999962, 12.64397049, 1.], [29.20000076, 0., 1.], [-14.60000038, -25.28794098, 1.], [0., 25.28794098, 1.], [0., -25.28794098, 1.], [0., 0., 1.], [-7.30000019, -12.64397049, 1.], [-7.30000019, 12.64397049, 1.], [-21.89999962, 12.64397049, 1.], [-29.20000076, 0., 1.], [14.60000038, 0., 1.], [-14.60000038, 25.28794098, 1.], [7.30000019, -12.64397049, 1.]]) reds = [[(0, 8), (9, 16)], [(13, 15), (14, 17), (3, 0), (4, 1), (16, 5), (12, 6)], [(3, 17), (4, 15), (7, 0), (11, 1), (16, 2), (12, 5), (10, 6), (14, 10)], [(3, 6), (14, 5)], [(0, 9), (1, 17), (2, 8), (4, 14), (6, 15), (8, 16), (12, 13), (11, 3), (10, 4), (9, 7), (15, 10), (17, 11)], [(3, 8), (11, 2), (9, 5)], [(3, 9), (4, 17), (12, 15), (11, 0), (10, 1), (8, 5), (13, 10), (14, 11)], [(0, 13), (1, 16)], [(0, 4), (1, 12), (6, 8), (9, 14), (15, 16), (17, 13)], [(0, 5), (3, 16), (7, 12), (17, 2), (11, 8)], [(0, 10), (7, 14), (10, 16), (11, 13), (6, 2), (9, 4), (15, 8), (17, 12)], [(1, 9), (2, 12), (5, 10), (6, 17), (8, 13), (12, 14), (10, 3), (17, 7), (15, 11)], [(2, 3), (5, 7)], [(16, 17), (12, 0), (8, 1), (13, 9)], [(0, 17), (1, 15), (3, 14), (4, 13), (9, 11), (10, 12), (12, 16), (5, 2), (7, 3), (11, 4), (6, 5), (17, 10)], [(3, 15), (4, 5), (7, 1), (13, 2), (11, 6)], [(5, 15), (8, 12), (10, 11), (13, 14), (15, 17), (1, 0), (6, 1), (4, 3), (12, 4), (11, 7), (17, 9), (16, 13)], [(0, 15), (1, 5), (3, 13), (4, 16), (9, 10), (11, 12), (15, 2), (7, 4), (10, 8)], [(0, 6), (3, 12), (4, 8), (7, 10), (9, 15), (14, 16), (10, 2), (17, 5)], [(8, 17), (2, 1), (13, 7), (12, 9), (16, 11)], [(0, 2), (7, 16), (9, 8)], [(4, 6), (14, 15), (3, 1), (13, 5)], [(0, 14), (1, 13), (6, 16)], [(2, 14), (6, 7), (5, 3)], [(2, 9), (8, 7)], [(2, 4), (5, 11), (6, 9), (8, 14), (15, 7)], [(1, 14), (6, 13)], [(0, 8), (9, 16)], [(13, 15), (14, 17), (3, 0), (4, 1), (16, 5), (12, 6)], [(3, 17), (4, 15), (7, 0), (11, 1), (16, 2), (12, 5), (10, 6), (14, 10)], [(3, 6), (14, 5)], [(0, 9), (1, 17), (2, 8), (4, 14), (6, 15), (8, 16), (12, 13), (11, 3), (10, 4), (9, 7), (15, 10), (17, 11)], [(3, 8), (11, 2), (9, 5)], [(3, 9), (4, 17), (12, 15), (11, 0), (10, 1), (8, 5), (13, 10), (14, 11)], [(0, 13), (1, 16)], [(0, 4), (1, 12), (6, 8), (9, 14), (15, 16), (17, 13)], [(0, 5), (3, 16), (7, 12), (17, 2), (11, 8)], [(0, 10), (7, 14), (10, 16), (11, 13), (6, 2), (9, 4), (15, 8), (17, 12)], [(1, 9), (2, 12), (5, 10), (6, 17), (8, 13), (12, 14), (10, 3), (17, 7), (15, 11)], [(2, 3), (5, 7)], [(16, 17), (12, 0), (8, 1), (13, 9)], [(0, 17), (1, 15), (3, 14), (4, 13), (9, 11), (10, 12), (12, 16), (5, 2), (7, 3), (11, 4), (6, 5), (17, 10)], [(3, 15), (4, 5), (7, 1), (13, 2), (11, 6)], [(5, 15), (8, 12), (10, 11), (13, 14), (15, 17), (1, 0), (6, 1), (4, 3), (12, 4), (11, 7), (17, 9), (16, 13)], [(0, 15), (1, 5), (3, 13), (4, 16), (9, 10), (11, 12), (15, 2), (7, 4), (10, 8)], [(0, 6), (3, 12), (4, 8), (7, 10), (9, 15), (14, 16), (10, 2), (17, 5)], [(8, 17), (2, 1), (13, 7), (12, 9), (16, 11)], [(0, 2), (7, 16), (9, 8)], [(4, 6), (14, 15), (3, 1), (13, 5)], [(0, 14), (1, 13), (6, 16)], [(2, 14), (6, 7), (5, 3)], [(2, 9), (8, 7)], [(2, 4), (5, 11), (6, 9), (8, 14), (15, 7)], [(1, 14), (6, 13)], [(0, 8), (9, 16)], [(13, 15), (14, 17), (3, 0), (4, 1), (16, 5), (12, 6)], [(3, 17), (4, 15), (7, 0), (11, 1), (16, 2), (12, 5), (10, 6), (14, 10)], [(3, 6), (14, 5)], [(0, 9), (1, 17), (2, 8), (4, 14), (6, 15), (8, 16), (12, 13), (11, 3), (10, 4), (9, 7), (15, 10), (17, 11)], [(3, 8), (11, 2), (9, 5)], [(3, 9), (4, 17), (12, 15), (11, 0), (10, 1), (8, 5), (13, 10), (14, 11)], [(0, 13), (1, 16)], [(0, 4), (1, 12), (6, 8), (9, 14), (15, 16), (17, 13)], [(0, 5), (3, 16), (7, 12), (17, 2), (11, 8)], [(0, 10), (7, 14), (10, 16), (11, 13), (6, 2), (9, 4), (15, 8), (17, 12)], [(1, 9), (2, 12), (5, 10), (6, 17), (8, 13), (12, 14), (10, 3), (17, 7), (15, 11)], [(2, 3), (5, 7)], [(16, 17), (12, 0), (8, 1), (13, 9)], [(0, 17), (1, 15), (3, 14), (4, 13), (9, 11), (10, 12), (12, 16), (5, 2), (7, 3), (11, 4), (6, 5), (17, 10)], [(3, 15), (4, 5), (7, 1), (13, 2), (11, 6)], [(5, 15), (8, 12), (10, 11), (13, 14), (15, 17), (1, 0), (6, 1), (4, 3), (12, 4), (11, 7), (17, 9), (16, 13)], [(0, 15), (1, 5), (3, 13), (4, 16), (9, 10), (11, 12), (15, 2), (7, 4), (10, 8)], [(0, 6), (3, 12), (4, 8), (7, 10), (9, 15), (14, 16), (10, 2), (17, 5)], [(8, 17), (2, 1), (13, 7), (12, 9), (16, 11)], [(0, 2), (7, 16), (9, 8)], [(4, 6), (14, 15), (3, 1), (13, 5)], [(0, 14), (1, 13), (6, 16)], [(2, 14), (6, 7), (5, 3)], [(2, 9), (8, 7)], [(2, 4), (5, 11), (6, 9), (8, 14), (15, 7)], [(1, 14), (6, 13)], [(0, 8), (9, 16)], [(13, 15), (14, 17), (3, 0), (4, 1), (16, 5), (12, 6)], [(3, 17), (4, 15), (7, 0), (11, 1), (16, 2), (12, 5), (10, 6), (14, 10)], [(3, 6), (14, 5)], [(0, 9), (1, 17), (2, 8), (4, 14), (6, 15), (8, 16), (12, 13), (11, 3), (10, 4), (9, 7), (15, 10), (17, 11)], [(3, 8), (11, 2), (9, 5)], [(3, 9), (4, 17), (12, 15), (11, 0), (10, 1), (8, 5), (13, 10), (14, 11)], [(0, 13), (1, 16)], [(0, 4), (1, 12), (6, 8), (9, 14), (15, 16), (17, 13)], [(0, 5), (3, 16), (7, 12), (17, 2), (11, 8)], [(0, 10), (7, 14), (10, 16), (11, 13), (6, 2), (9, 4), (15, 8), (17, 12)], [(1, 9), (2, 12), (5, 10), (6, 17), (8, 13), (12, 14), (10, 3), (17, 7), (15, 11)], [(2, 3), (5, 7)], [(16, 17), (12, 0), (8, 1), (13, 9)], [(0, 17), (1, 15), (3, 14), (4, 13), (9, 11), (10, 12), (12, 16), (5, 2), (7, 3), (11, 4), (6, 5), (17, 10)], [(3, 15), (4, 5), (7, 1), (13, 2), (11, 6)], [(5, 15), (8, 12), (10, 11), (13, 14), (15, 17), (1, 0), (6, 1), (4, 3), (12, 4), (11, 7), (17, 9), (16, 13)], [(0, 15), (1, 5), (3, 13), (4, 16), (9, 10), (11, 12), (15, 2), (7, 4), (10, 8)], [(0, 6), (3, 12), (4, 8), (7, 10), (9, 15), (14, 16), (10, 2), (17, 5)], [(8, 17), (2, 1), (13, 7), (12, 9), (16, 11)], [(0, 2), (7, 16), (9, 8)], [(4, 6), (14, 15), (3, 1), (13, 5)], [(0, 14), (1, 13), (6, 16)], [(2, 14), (6, 7), (5, 3)], [(2, 9), (8, 7)], [(2, 4), (5, 11), (6, 9), (8, 14), (15, 7)], [(1, 14), (6, 13)]] freqs = np.linspace(.1, .2, 64) times = np.arange(11) ants = np.arange(len(antpos)) info = Oc.RedundantInfo() info.init_from_reds(reds, antpos) # Simulate unique "true" visibilities np.random.seed(21) vis_true = {} i = 0 for rg in reds: vis_true[rg[0]] = np.array( 1.0 * np.random.randn(len(times), len(freqs)) + 1.0j * np.random.randn(len(times), len(freqs)), dtype=np.complex64) # Smulate true gains and then remove degeneracies from true gains so that removedegen will produce exact answers gain_true = {} for i in ants: gain_true[i] = np.array( 1. + (.1 * np.random.randn(len(times), len(freqs)) + .1j * np.random.randn(len(times), len(freqs))), dtype=np.complex64) g0 = {i: np.ones_like(gain_true[i]) for i in ants} _, gain_true, _ = self.removedegen2(info, gain_true, vis_true, g0) # Generate and apply firstcal gains fcgains = {} for i in ants: fcspectrum = np.exp(2.0j * np.pi * 5.0 * np.random.randn() * freqs) fcgains[i] = np.array([fcspectrum for t in times], dtype=np.complex64) for i in ants: gain_true[i] *= fcgains[i] # Generate fake data bl2ublkey = {bl: rg[0] for rg in reds for bl in rg} data = {} for rg in reds: for (i, j) in rg: data[(i, j)] = np.array(np.conj(gain_true[i]) * gain_true[j] * vis_true[rg[0]], dtype=np.complex64) # Run logcal, lincal, and removedegen m1, g1, v1 = Oc.logcal(data, info, gains=fcgains) m2, g2, v2 = Oc.lincal(data, info, gains=g1, vis=v1) _g2 = {} for k in g2: _g2[k] = g2[k] / fcgains[k] _, g3, v3 = Oc.removedegen(data, info, _g2, v2) for k in g3: g3[k] *= fcgains[k] #Test that lincal actually converged np.testing.assert_array_less(m2['iter'], 50 * np.ones_like(m2['iter'])) #Test that chisq is 0 after lincal in the gains/vis/data and in the meta chiSqBeforeRemoveDegen = self.chisq(data, g2, v2, reds) np.testing.assert_almost_equal(chiSqBeforeRemoveDegen, np.zeros_like(chiSqBeforeRemoveDegen), decimal=10) np.testing.assert_almost_equal(m2['chisq'], np.zeros_like(m2['chisq']), decimal=10) #Test that chisq is 0 after lincal and remove degen chiSqAfterRemoveDegen = self.chisq(data, g3, v3, reds) np.testing.assert_almost_equal(chiSqAfterRemoveDegen, np.zeros_like(chiSqAfterRemoveDegen), decimal=10) #Test that the solution has degeneracies removed properly Rgains = np.array([np.append(ai, 1) for ai in info.antloc]) Mgains = np.linalg.pinv(Rgains.T.dot(Rgains)).dot(Rgains.T) ntimes, nfreqs = list(g3.values())[0].shape for t in range(ntimes): for f in range(nfreqs): gainSols = np.array([ g3[ai][t, f] / fcgains[ai][t, f] for ai in info.subsetant ]) np.testing.assert_almost_equal(np.mean(np.abs(gainSols)), 1.0, decimal=5) np.testing.assert_almost_equal(Mgains.dot(np.angle(gainSols)), [0.0, 0.0, 0.0, 0.0], decimal=5) #Test that the correct gains and visibilities are recovered for ai in info.subsetant: np.testing.assert_array_almost_equal(g3[ai], gain_true[ai], decimal=5) for bl in vis_true.keys(): np.testing.assert_array_almost_equal(v3[bl], vis_true[bl], decimal=5)
def setUp(self): self.legacy_info = Oi.RedundantInfoLegacy(filename=redinfo_psa32, txtmode=True) self.info = Oc.RedundantInfo() self.info.init_from_reds(self.legacy_info.get_reds(), self.legacy_info.antloc)