예제 #1
0
    def test_load_vis(self):
        # inheretied testing from the old abscal_funcs.UVData2AbsCalDict

        # load into pyuvdata object
        self.data_file = os.path.join(DATA_PATH, "zen.2458043.12552.xx.HH.uvORA")
        self.uvd = UVData()
        self.uvd.read_miriad(self.data_file)
        self.freq_array = np.unique(self.uvd.freq_array)
        self.antpos, self.ants = self.uvd.get_ENU_antpos(center=True, pick_data_ants=True)
        self.antpos = odict(list(zip(self.ants, self.antpos)))
        self.time_array = np.unique(self.uvd.time_array)

        fname = os.path.join(DATA_PATH, "zen.2458043.12552.xx.HH.uvORA")
        data, flags = io.load_vis(fname, pop_autos=False)
        assert data[(24, 25, 'ee')].shape == (60, 64)
        assert flags[(24, 25, 'ee')].shape == (60, 64)
        assert (24, 24, parse_polstr('EE', x_orientation=self.uvd.x_orientation)) in data
        data, flags = io.load_vis([fname])
        assert data[(24, 25, 'ee')].shape == (60, 64)

        # test pop autos
        data, flags = io.load_vis(fname, pop_autos=True)
        assert (24, 24, parse_polstr('EE', x_orientation=self.uvd.x_orientation)) not in data

        # test uvd object
        uvd = UVData()
        uvd.read_miriad(fname)
        data, flags = io.load_vis(uvd)
        assert data[(24, 25, 'ee')].shape == (60, 64)
        data, flags = io.load_vis([uvd])
        assert data[(24, 25, 'ee')].shape == (60, 64)

        # test multiple
        fname2 = os.path.join(DATA_PATH, "zen.2458043.13298.xx.HH.uvORA")
        data, flags = io.load_vis([fname, fname2])
        assert data[(24, 25, 'ee')].shape == (120, 64)
        assert flags[(24, 25, 'ee')].shape == (120, 64)

        # test w/ meta
        d, f, ap, a, f, t, l, p = io.load_vis([fname, fname2], return_meta=True)
        assert len(ap[24]) == 3
        assert len(f) == len(self.freq_array)

        with pytest.raises(NotImplementedError):
            d, f = io.load_vis(fname, filetype='not_a_real_filetype')
        with pytest.raises(NotImplementedError):
            d, f = io.load_vis(['str1', 'str2'], filetype='not_a_real_filetype')

        # test w/ meta pick_data_ants
        fname = os.path.join(DATA_PATH, "zen.2458043.12552.xx.HH.uvORA")
        d, f, ap, a, f, t, l, p = io.load_vis(fname, return_meta=True, pick_data_ants=False)
        assert len(ap[24]) == 3
        assert len(a) == 47
        assert len(f) == len(self.freq_array)

        with pytest.raises(TypeError):
            d, f = io.load_vis(1.0)
예제 #2
0
def test_pol_funcs_x_orientation():
    """ Test utility functions to convert between polarization strings and numbers with x_orientation """

    pol_nums = [-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4]

    x_orient1 = 'e'
    pol_str = [
        'ne', 'en', 'nn', 'ee', 'lr', 'rl', 'll', 'rr', 'pI', 'pQ', 'pU', 'pV'
    ]
    assert pol_nums == uvutils.polstr2num(pol_str, x_orientation=x_orient1)
    assert pol_str == uvutils.polnum2str(pol_nums, x_orientation=x_orient1)
    # Check individuals
    assert -6 == uvutils.polstr2num('NN', x_orientation=x_orient1)
    assert 'pV' == uvutils.polnum2str(4)
    # Check errors
    pytest.raises(KeyError, uvutils.polstr2num, 'foo', x_orientation=x_orient1)
    pytest.raises(ValueError, uvutils.polstr2num, 1, x_orientation=x_orient1)
    pytest.raises(ValueError, uvutils.polnum2str, 7.3, x_orientation=x_orient1)
    # Check parse
    assert uvutils.parse_polstr("eE", x_orientation=x_orient1) == 'ee'
    assert uvutils.parse_polstr("xx", x_orientation=x_orient1) == 'ee'
    assert uvutils.parse_polstr("NN", x_orientation=x_orient1) == 'nn'
    assert uvutils.parse_polstr("yy", x_orientation=x_orient1) == 'nn'
    assert uvutils.parse_polstr('i', x_orientation=x_orient1) == 'pI'

    x_orient2 = 'n'
    pol_str = [
        'en', 'ne', 'ee', 'nn', 'lr', 'rl', 'll', 'rr', 'pI', 'pQ', 'pU', 'pV'
    ]
    assert pol_nums == uvutils.polstr2num(pol_str, x_orientation=x_orient2)
    assert pol_str == uvutils.polnum2str(pol_nums, x_orientation=x_orient2)
    # Check individuals
    assert -6 == uvutils.polstr2num('EE', x_orientation=x_orient2)
    assert 'pV' == uvutils.polnum2str(4)
    # Check errors
    pytest.raises(KeyError, uvutils.polstr2num, 'foo', x_orientation=x_orient2)
    pytest.raises(ValueError, uvutils.polstr2num, 1, x_orientation=x_orient2)
    pytest.raises(ValueError, uvutils.polnum2str, 7.3, x_orientation=x_orient2)
    # Check parse
    assert uvutils.parse_polstr("nN", x_orientation=x_orient2) == 'nn'
    assert uvutils.parse_polstr("xx", x_orientation=x_orient2) == 'nn'
    assert uvutils.parse_polstr("EE", x_orientation=x_orient2) == 'ee'
    assert uvutils.parse_polstr("yy", x_orientation=x_orient2) == 'ee'
    assert uvutils.parse_polstr('i', x_orientation=x_orient2) == 'pI'

    # check warnings for non-recognized x_orientation
    assert uvtest.checkWarnings(uvutils.polstr2num, ['xx'],
                                {'x_orientation': 'foo'},
                                message='x_orientation not recognized') == -5
    assert uvtest.checkWarnings(uvutils.polnum2str, [-6],
                                {'x_orientation': 'foo'},
                                message='x_orientation not recognized') == 'yy'
예제 #3
0
    def test_partial_write(self):
        hd = HERAData(self.uvh5_1)
        assert hd._writers == {}
        d, f, n = hd.read(bls=hd.bls[0])
        assert hd.last_read_kwargs['bls'] == (53, 53, parse_polstr('XX', x_orientation=hd.x_orientation))
        d[(53, 53, 'EE')] *= (2.0 + 1.0j)
        hd.partial_write('out.h5', data=d, clobber=True)
        assert 'out.h5' in hd._writers
        assert isinstance(hd._writers['out.h5'], HERAData)
        for meta in hd.HERAData_metas:
            try:
                assert np.all(getattr(hd, meta) == getattr(hd._writers['out.h5'], meta))
            except BaseException:
                for k in getattr(hd, meta).keys():
                    assert np.all(getattr(hd, meta)[k] == getattr(hd._writers['out.h5'], meta)[k])

        d, f, n = hd.read(bls=hd.bls[1])
        assert hd.last_read_kwargs['bls'] == (53, 54, parse_polstr('XX', x_orientation=hd.x_orientation))
        d[(53, 54, 'EE')] *= (2.0 + 1.0j)
        hd.partial_write('out.h5', data=d, clobber=True)

        d, f, n = hd.read(bls=hd.bls[2])
        assert hd.last_read_kwargs['bls'] == (54, 54, parse_polstr('XX', x_orientation=hd.x_orientation))
        d[(54, 54, 'EE')] *= (2.0 + 1.0j)
        hd.partial_write('out.h5', data=d, clobber=True, inplace=True)
        d_after, _, _ = hd.build_datacontainers()
        np.testing.assert_array_almost_equal(d[(54, 54, 'EE')], d_after[(54, 54, 'EE')])

        hd = HERAData(self.uvh5_1)
        d, f, n = hd.read()
        hd2 = HERAData('out.h5')
        d2, f2, n2 = hd2.read()
        for bl in hd.bls:
            np.testing.assert_array_almost_equal(d[bl] * (2.0 + 1.0j), d2[bl])
            np.testing.assert_array_equal(f[bl], f2[bl])
            np.testing.assert_array_equal(n[bl], n2[bl])
        os.remove('out.h5')

        # test errors
        hd = HERAData(self.miriad_1, filetype='miriad')
        with pytest.raises(NotImplementedError):
            hd.partial_write('out.uv')
        hd = HERAData([self.uvh5_1, self.uvh5_2])
        with pytest.raises(NotImplementedError):
            hd.partial_write('out.h5')
        hd = HERAData(self.uvh5_1)
예제 #4
0
 def test_check_noise_variance(self):
     nos = vis_metrics.check_noise_variance(self.data)
     for bl in self.data.get_antpairs():
         inds = self.data.antpair2ind(*bl)
         n = nos[bl + (uvutils.parse_polstr('xx'), )]
         self.assertEqual(n.shape, (self.data.Nfreqs - 1, ))
         nsamp = self.data.channel_width * self.data.integration_time[inds][
             0]
         np.testing.assert_almost_equal(n,
                                        np.ones_like(n) * nsamp,
                                        -np.log10(nsamp))
예제 #5
0
def test_pol_funcs():
    """ Test utility functions to convert between polarization strings and numbers """

    pol_nums = [-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4]
    pol_str = [
        'yx', 'xy', 'yy', 'xx', 'lr', 'rl', 'll', 'rr', 'pI', 'pQ', 'pU', 'pV'
    ]
    assert pol_nums == uvutils.polstr2num(pol_str)
    assert pol_str == uvutils.polnum2str(pol_nums)
    # Check individuals
    assert -6 == uvutils.polstr2num('YY')
    assert 'pV' == uvutils.polnum2str(4)
    # Check errors
    pytest.raises(KeyError, uvutils.polstr2num, 'foo')
    pytest.raises(ValueError, uvutils.polstr2num, 1)
    pytest.raises(ValueError, uvutils.polnum2str, 7.3)
    # Check parse
    assert uvutils.parse_polstr("xX") == 'xx'
    assert uvutils.parse_polstr("XX") == 'xx'
    assert uvutils.parse_polstr('i') == 'pI'
예제 #6
0
def test_pol_funcs():
    """ Test utility functions to convert between polarization strings and numbers """

    pol_nums = [-8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4]
    pol_str = [
        'yx', 'xy', 'yy', 'xx', 'lr', 'rl', 'll', 'rr', 'pI', 'pQ', 'pU', 'pV'
    ]
    nt.assert_equal(pol_nums, uvutils.polstr2num(pol_str))
    nt.assert_equal(pol_str, uvutils.polnum2str(pol_nums))
    # Check individuals
    nt.assert_equal(-6, uvutils.polstr2num('YY'))
    nt.assert_equal('pV', uvutils.polnum2str(4))
    # Check errors
    nt.assert_raises(KeyError, uvutils.polstr2num, 'foo')
    nt.assert_raises(ValueError, uvutils.polstr2num, 1)
    nt.assert_raises(ValueError, uvutils.polnum2str, 7.3)
    # Check parse
    nt.assert_equal(uvutils.parse_polstr("xX"), 'xx')
    nt.assert_equal(uvutils.parse_polstr("XX"), 'xx')
    nt.assert_equal(uvutils.parse_polstr('i'), 'pI')
    nt.assert_equal(uvutils.parse_jpolstr('x'), 'Jxx')
    nt.assert_equal(uvutils.parse_jpolstr('xy'), 'Jxy')
    nt.assert_equal(uvutils.parse_jpolstr('XY'), 'Jxy')
예제 #7
0
    def test_load_flags_npz(self):
        npzfile = os.path.join(DATA_PATH, "test_input/zen.2458101.45361.xx.HH.uvOCR_53x_54x_only.flags.applied.npz")
        flags = io.load_flags(npzfile, filetype='npz')
        assert (53, 54, parse_polstr('XX')) in flags
        for f in flags.values():
            assert f.shape == (60, 1024)
            np.testing.assert_array_equal(f[:, 0:50], True)
            np.testing.assert_array_equal(f[:, -50:], True)
            assert not np.all(f)

        flags, meta = io.load_flags(npzfile, filetype='npz', return_meta=True)
        assert len(meta['freqs']) == 1024
        assert len(meta['times']) == 60
        assert 'history' in meta
예제 #8
0
    def test_iterate_over_bls(self):
        hd = HERAData(self.uvh5_1)
        for (d, f, n) in hd.iterate_over_bls(Nbls=2):
            for dc in (d, f, n):
                assert len(dc.keys()) == 1 or len(dc.keys()) == 2
                assert list(dc.values())[0].shape == (60, 1024)

        hd = HERAData([self.uvh5_1, self.uvh5_2])
        for (d, f, n) in hd.iterate_over_bls():
            for dc in (d, f, n):
                assert len(d.keys()) == 1
                assert list(d.values())[0].shape == (120, 1024)

        hd = HERAData(self.miriad_1, filetype='miriad')
        d, f, n = next(hd.iterate_over_bls(bls=[(52, 53, 'xx')]))
        assert list(d.keys()) == [(52, 53, parse_polstr('XX', x_orientation=hd.x_orientation))]
        with pytest.raises(NotImplementedError):
            next(hd.iterate_over_bls())
예제 #9
0
    def test_get_slice(self):
        hd = HERAData(self.uvh5_1)
        hd.read()
        for bl in hd.bls:
            np.testing.assert_array_equal(hd._get_slice(hd.data_array, bl), hd.get_data(bl))
        np.testing.assert_array_equal(hd._get_slice(hd.data_array, (54, 53, 'EE')),
                                      hd.get_data((54, 53, 'EE')))
        np.testing.assert_array_equal(hd._get_slice(hd.data_array, (53, 54))[parse_polstr('XX', x_orientation=hd.x_orientation)],
                                      hd.get_data((53, 54, 'EE')))
        np.testing.assert_array_equal(hd._get_slice(hd.data_array, 'EE')[(53, 54)],
                                      hd.get_data((53, 54, 'EE')))
        with pytest.raises(KeyError):
            hd._get_slice(hd.data_array, (1, 2, 3, 4))

        hd = HERAData(self.four_pol, filetype='miriad')
        d, f, n = hd.read(bls=[(80, 81)])
        for p in d.pols():
            np.testing.assert_array_almost_equal(hd._get_slice(hd.data_array, (80, 81, p)),
                                                 hd.get_data((80, 81, p)))
            np.testing.assert_array_almost_equal(hd._get_slice(hd.data_array, (81, 80, p)),
                                                 hd.get_data((81, 80, p)))
예제 #10
0
    def test_read(self):
        # uvh5
        hd = HERAData(self.uvh5_1)
        d, f, n = hd.read(bls=(53, 54, 'xx'), frequencies=hd.freqs[0:100], times=hd.times[0:10])
        assert hd.last_read_kwargs['bls'] == (53, 54, parse_polstr('XX'))
        assert hd.last_read_kwargs['polarizations'] is None
        for dc in [d, f, n]:
            assert len(dc) == 1
            assert list(dc.keys()) == [(53, 54, parse_polstr('XX', x_orientation=hd.x_orientation))]
            assert dc[53, 54, 'ee'].shape == (10, 100)
        with pytest.raises(ValueError):
            d, f, n = hd.read(polarizations=['xy'])

        # assert return data = False
        o = hd.read(bls=[(53, 53), (53, 54)], return_data=False)
        assert o is None

        # assert __getitem__ isn't a read when key exists
        o = hd[(53, 53, 'ee')]
        assert len(hd._blt_slices) == 2

        # assert __getitem__ is a read when key does not exist
        o = hd[(54, 54, 'ee')]
        assert len(hd._blt_slices) == 1

        # test read list
        hd = HERAData([self.uvh5_1, self.uvh5_2])
        d, f, n = hd.read(axis='blt')
        for dc in [d, f, n]:
            assert len(dc) == 3
            assert len(dc.times) == 120
            assert len(dc.lsts) == 120
            assert len(dc.freqs) == 1024
            for i in [53, 54]:
                for j in [53, 54]:
                    assert (i, j, 'ee') in dc
            for bl in dc:
                assert dc[bl].shape == (120, 1024)

        # miriad
        hd = HERAData(self.miriad_1, filetype='miriad')
        d, f, n = hd.read()
        hd = HERAData(self.miriad_1, filetype='miriad')
        d, f, n = hd.read(bls=(52, 53), polarizations=['XX'], frequencies=d.freqs[0:30], times=d.times[0:10])
        assert hd.last_read_kwargs['polarizations'] == ['XX']
        for dc in [d, f, n]:
            assert len(dc) == 1
            assert list(dc.keys()) == [(52, 53, parse_polstr('XX', x_orientation=hd.x_orientation))]
            assert dc[52, 53, 'ee'].shape == (10, 30)
        with pytest.raises(NotImplementedError):
            d, f, n = hd.read(read_data=False)

        # uvfits
        hd = HERAData(self.uvfits, filetype='uvfits')
        d, f, n = hd.read(bls=(0, 1, 'xx'), freq_chans=list(range(10)))
        assert hd.last_read_kwargs['freq_chans'] == list(range(10))
        for dc in [d, f, n]:
            assert len(dc) == 1
            assert list(dc.keys()) == [(0, 1, parse_polstr('XX', x_orientation=hd.x_orientation))]
            assert dc[0, 1, 'ee'].shape == (60, 10)
        with pytest.raises(NotImplementedError):
            d, f, n = hd.read(read_data=False)