Skip to content

Commit

Permalink
changed blpair int and bl int convention to 100 + antnum
Browse files Browse the repository at this point in the history
  • Loading branch information
nkern committed Jul 18, 2018
1 parent b1b5dd7 commit 0c883ba
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 77 deletions.
4 changes: 2 additions & 2 deletions hera_pspec/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def build_vanilla_uvpspec(beam=None):
Nspwdlys = 1 * Ndlys

# [((1, 2), (1, 2)), ((2, 3), (2, 3)), ((1, 3), (1, 3))]
blpairs = [1002001002, 2003002003, 1003001003]
bls = [1002, 2003, 1003]
blpairs = [101102101102, 102103102103, 101103101103]
bls = [101102, 102103, 101103]
Nbls = len(bls)
Nblpairs = len(blpairs)
Nblpairts = Nblpairs * Ntimes
Expand Down
2 changes: 1 addition & 1 deletion hera_pspec/tests/test_grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_bootstrap_average_blpairs(self):
self.assertRaises(AssertionError, grouping.bootstrap_average_blpairs,
[np.arange(5),], blpair_groups, time_avg=False)
self.assertRaises(KeyError, grouping.bootstrap_average_blpairs,
[self.uvp,], [[100100100100,],], time_avg=False)
[self.uvp,], [[200200200200,],], time_avg=False)

# Reduce UVPSpec to only 3 blpairs and set them all to the same values
_blpairs = list(np.unique(self.uvp.blpair_array)[:3])
Expand Down
4 changes: 2 additions & 2 deletions hera_pspec/tests/test_pspecdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,7 @@ def test_pspec(self):
nt.assert_equal(uvp.Nspws, 3)
nt.assert_equal(uvp.Nspwdlys, 43)
nt.assert_equal(uvp.data_array[0].shape, (240, 14, 1))
nt.assert_equal(uvp.get_data((0, 24025024025, 'xx')).shape, (60, 14))
nt.assert_equal(uvp.get_data((0, 124125124125, 'xx')).shape, (60, 14))

# check select
uvp.select(spws=[1])
Expand Down Expand Up @@ -1274,7 +1274,7 @@ def test_pspec_run():
nt.assert_equal(psc.spectra('foo_bar'), [u'foo_x_bar', u'foo_x_foo'])
uvp = psc.get_pspec("foo_bar", "foo_x_bar")
nt.assert_true(uvp.vis_units, "mK")
nt.assert_equal(uvp.bl_array.tolist(), [37038, 52053])
nt.assert_equal(uvp.bl_array.tolist(), [137138, 152153])
nt.assert_equal(uvp.pol_array.tolist(), [-5, -5])
nt.assert_equal(uvp.cosmo, cosmo)
nt.assert_true(hasattr(uvp, 'cov_array'))
Expand Down
8 changes: 4 additions & 4 deletions hera_pspec/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_uvpspec_from_data():
# test basic execution
uvp = testing.uvpspec_from_data(fname, [(37, 38), (38, 39), (52, 53), (53, 54)], beam=beam, spw_ranges=[(50, 100)])
nt.assert_equal(uvp.Nfreqs, 50)
nt.assert_equal(np.unique(uvp.blpair_array).tolist(), [37038038039, 37038052053, 37038053054, 38039052053, 38039053054, 52053053054])
nt.assert_equal(np.unique(uvp.blpair_array).tolist(), [137138138139, 137138152153, 137138153154, 138139152153, 138139153154, 152153153154])
uvp2 = testing.uvpspec_from_data(uvd, [(37, 38), (38, 39), (52, 53), (53, 54)], beam=beamfile, spw_ranges=[(50, 100)])
uvp.history = ''
uvp2.history = ''
Expand All @@ -40,9 +40,9 @@ def test_uvpspec_from_data():
antpos, ants = uvd.get_ENU_antpos(pick_data_ants=True)
reds = redcal.get_pos_reds(dict(zip(ants, antpos)), low_hi=True)
uvp = testing.uvpspec_from_data(fname, reds[:3], beam=beam, spw_ranges=[(50, 100)])
nt.assert_equal(len(set(uvp.bl_array) - set([37038, 37051, 37052, 38039, 38052, 38053, 39053, 39054,
51052, 51067, 52053, 52067, 52068, 53054, 53068, 53069,
54069, 67068, 68069])), 0)
nt.assert_equal(len(set(uvp.bl_array) - set([137138, 137151, 137152, 138139, 138152, 138153, 139153, 139154,
151152, 151167, 152153, 152167, 152168, 153154, 153168, 153169,
154169, 167168, 168169])), 0)
nt.assert_equal(uvp.Nblpairs, 51)

# test exceptions
Expand Down
78 changes: 39 additions & 39 deletions hera_pspec/tests/test_uvpspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def test_get_funcs(self):
d = self.uvp.get_data((0, ((1, 2), (1, 2)), 'xx'))
nt.assert_equal(d.shape, (10, 30))
nt.assert_true(d.dtype == np.complex)
nt.assert_almost_equal(d[0,0], (1.0020010020000001+0j))
nt.assert_almost_equal(d[0,0], (101.1021011020000001+0j))
d = self.uvp.get_data((0, ((1, 2), (1, 2)), -5))
nt.assert_almost_equal(d[0,0], (1.0020010020000001+0j))
d = self.uvp.get_data((0, 1002001002, -5))
nt.assert_almost_equal(d[0,0], (1.0020010020000001+0j))
nt.assert_almost_equal(d[0,0], (101.1021011020000001+0j))
d = self.uvp.get_data((0, 101102101102, -5))
nt.assert_almost_equal(d[0,0], (101.1021011020000001+0j))
# get_wgts
w = self.uvp.get_wgts((0, ((1, 2), (1, 2)), 'xx'))
nt.assert_equal(w.shape, (10, 30, 2))
Expand Down Expand Up @@ -137,25 +137,25 @@ def test_convert_deltasq(self):

def test_blpair_conversions(self):
# test blpair -> antnums
an = self.uvp.blpair_to_antnums(1002001002)
an = self.uvp.blpair_to_antnums(101102101102)
nt.assert_equal(an, ((1, 2), (1, 2)))
# test antnums -> blpair
bp = self.uvp.antnums_to_blpair(((1, 2), (1, 2)))
nt.assert_equal(bp, 1002001002)
nt.assert_equal(bp, 101102101102)
# test bl -> antnums
an = self.uvp.bl_to_antnums(1002)
an = self.uvp.bl_to_antnums(101102)
nt.assert_equal(an, (1, 2))
# test antnums -> bl
bp = self.uvp.antnums_to_bl((1, 2))
nt.assert_equal(bp, 1002)
nt.assert_equal(bp, 101102)

def test_indices_funcs(self):
# key to indices
spw, blpairts, pol = self.uvp.key_to_indices( (0, ((1,2),(1,2)), -5) )
nt.assert_equal(spw, 0)
nt.assert_equal(pol, 0)
nt.assert_true(np.isclose(blpairts, np.array([0,3,6,9,12,15,18,21,24,27])).min())
spw, blpairts, pol = self.uvp.key_to_indices( (0, 1002001002, 'xx') )
spw, blpairts, pol = self.uvp.key_to_indices( (0, 101102101102, 'xx') )
nt.assert_equal(spw, 0)
nt.assert_equal(pol, 0)
nt.assert_true(np.isclose(blpairts, np.array([0,3,6,9,12,15,18,21,24,27])).min())
Expand All @@ -177,14 +177,14 @@ def test_indices_funcs(self):
nt.assert_equal(len(pol), 1)

# test blpair to indices
inds = self.uvp.blpair_to_indices(1002001002)
inds = self.uvp.blpair_to_indices(101102101102)
nt.assert_true(np.isclose(inds, np.array([0,3,6,9,12,15,18,21,24,27])).min())
inds = self.uvp.blpair_to_indices(((1,2),(1,2)))
nt.assert_true(np.isclose(inds, np.array([0,3,6,9,12,15,18,21,24,27])).min())

# test time to indices
time = self.uvp.time_avg_array[5]
blpair = 1002001002
blpair = 101102101102
inds = self.uvp.time_to_indices(time=time)
nt.assert_equal(len(inds), 3)
nt.assert_true(np.isclose(self.uvp.time_avg_array[inds], time, rtol=1e-10).all())
Expand All @@ -198,7 +198,7 @@ def test_select(self):
uvp.select(bls=[(1, 2)], inplace=True)
nt.assert_equal(uvp.Nblpairs, 1)
nt.assert_equal(uvp.data_array[0].shape, (10, 30, 1))
nt.assert_almost_equal(uvp.data_array[0][0,0,0], (1.0020010020000001+0j))
nt.assert_almost_equal(uvp.data_array[0][0,0,0], (101.1021011020000001+0j))
# inplace vs not inplace, spw selection
uvd = UVData()
uvd.read_miriad(os.path.join(DATA_PATH, 'zen.even.xx.LST.1.28828.uvOCRSA'))
Expand All @@ -213,7 +213,7 @@ def test_select(self):
nt.assert_almost_equal(uvp2.data_array[0][0,0,0], (-3831605.3903496987+8103523.9604128916j))
# blpair select
uvp = copy.deepcopy(self.uvp)
uvp2 = uvp.select(blpairs=[1002001002, 2003002003], inplace=False)
uvp2 = uvp.select(blpairs=[101102101102, 102103102103], inplace=False)
nt.assert_equal(uvp2.Nblpairs, 2)
# pol select
uvp2 = uvp.select(pols=[-5], inplace=False)
Expand Down Expand Up @@ -275,37 +275,37 @@ def test_sense(self):

# test generate noise spectra
P_N = uvp.generate_noise_spectra(0, -5, 500, form='Pk', real=True)
nt.assert_equal(P_N[1002001002].shape, (10, 30))
nt.assert_equal(P_N[101102101102].shape, (10, 30))

# test smaller system temp
P_N2 = uvp.generate_noise_spectra(0, -5, 400, form='Pk', real=True)
nt.assert_true((P_N[1002001002] > P_N2[1002001002]).all())
nt.assert_true((P_N[101102101102] > P_N2[101102101102]).all())

# test complex
P_N2 = uvp.generate_noise_spectra(0, -5, 500, form='Pk', real=False)
nt.assert_true((P_N[1002001002] < P_N2[1002001002]).all())
nt.assert_true((P_N[101102101102] < P_N2[101102101102]).all())

# test Dsq
Dsq = uvp.generate_noise_spectra(0, -5, 500, form='DelSq', real=True)
nt.assert_equal(Dsq[1002001002].shape, (10, 30))
nt.assert_true(Dsq[1002001002][0, 1] < P_N[1002001002][0, 1])
nt.assert_equal(Dsq[101102101102].shape, (10, 30))
nt.assert_true(Dsq[101102101102][0, 1] < P_N[101102101102][0, 1])

# test a blpair selection
P_N = uvp.generate_noise_spectra(0, -5, 500, form='Pk', real=True)

def test_average_spectra(self):
uvp = copy.deepcopy(self.uvp)
# test blpair averaging
blpairs = uvp.get_blpair_groups_from_bl_groups([[1002, 2003, 1003]], only_pairs_in_bls=False)
blpairs = uvp.get_blpair_groups_from_bl_groups([[101102, 102103, 101103]], only_pairs_in_bls=False)
uvp2 = uvp.average_spectra(blpair_groups=blpairs, time_avg=False, inplace=False)
nt.assert_equal(uvp2.Nblpairs, 1)
nt.assert_true(np.isclose(uvp2.get_nsamples((0, 1002001002, 'xx')), 3.0).all())
nt.assert_equal(uvp2.get_data((0, 1002001002, 'xx')).shape, (10, 30))
nt.assert_true(np.isclose(uvp2.get_nsamples((0, 101102101102, 'xx')), 3.0).all())
nt.assert_equal(uvp2.get_data((0, 101102101102, 'xx')).shape, (10, 30))

# Test blpair averaging (with baseline-pair weights)
# Results should be identical with different weights here, as the data
# are all the same)
blpairs = [[1002001002, 1002001002]]
blpairs = [[101102101102, 101102101102]]
blpair_wgts = [[2., 0.,]]
uvp3a = uvp.average_spectra(blpair_groups=blpairs, time_avg=False,
blpair_weights=None,
Expand All @@ -314,19 +314,19 @@ def test_average_spectra(self):
blpair_weights=blpair_wgts,
inplace=False)
#nt.assert_equal(uvp2.Nblpairs, 1)
nt.assert_true(np.isclose(uvp3a.get_data((0, 1002001002, 'xx')),
uvp3b.get_data((0, 1002001002, 'xx'))).all())
#nt.assert_equal(uvp2.get_data((0, 1002001002, 'xx')).shape, (10, 30))
nt.assert_true(np.isclose(uvp3a.get_data((0, 101102101102, 'xx')),
uvp3b.get_data((0, 101102101102, 'xx'))).all())
#nt.assert_equal(uvp2.get_data((0, 101102101102, 'xx')).shape, (10, 30))


# test time averaging
uvp2 = uvp.average_spectra(time_avg=True, inplace=False)
nt.assert_true(uvp2.Ntimes, 1)
nt.assert_true(np.isclose(uvp2.get_nsamples((0, 1002001002, 'xx')), 10.0).all())
nt.assert_true(uvp2.get_data((0, 1002001002, 'xx')).shape, (1, 30))
nt.assert_true(np.isclose(uvp2.get_nsamples((0, 101102101102, 'xx')), 10.0).all())
nt.assert_true(uvp2.get_data((0, 101102101102, 'xx')).shape, (1, 30))
# ensure averaging works when multiple repeated baselines are present, but only
# if time_avg = True
uvp.blpair_array[uvp.blpair_to_indices(2003002003)] = 1002001002
uvp.blpair_array[uvp.blpair_to_indices(102103102103)] = 101102101102
nt.assert_raises(ValueError, uvp.average_spectra, blpair_groups=[list(np.unique(uvp.blpair_array))], time_avg=False, inplace=False)
uvp.average_spectra(blpair_groups=[list(np.unique(uvp.blpair_array))], time_avg=True)
nt.assert_equal(uvp.Ntimes, 1)
Expand Down Expand Up @@ -532,18 +532,18 @@ def test_combine_uvpspec_std(self):
nt.assert_equal(out.Npols, 3)

def test_conj_blpair_int():
conj_blpair = uvputils._conj_blpair_int(1002003004)
nt.assert_equal(conj_blpair, 3004001002)
conj_blpair = uvputils._conj_blpair_int(101102103104)
nt.assert_equal(conj_blpair, 103104101102)

def test_conj_bl_int():
conj_bl = uvputils._conj_bl_int(1002)
nt.assert_equal(conj_bl, 2001)
conj_bl = uvputils._conj_bl_int(101102)
nt.assert_equal(conj_bl, 102101)

def test_conj_blpair():
blpair = uvputils._conj_blpair(1002003004, which='first')
nt.assert_equal(blpair, 2001003004)
blpair = uvputils._conj_blpair(1002003004, which='second')
nt.assert_equal(blpair, 1002004003)
blpair = uvputils._conj_blpair(1002003004, which='both')
nt.assert_equal(blpair, 2001004003)
nt.assert_raises(ValueError, uvputils._conj_blpair, 2001003004, which='foo')
blpair = uvputils._conj_blpair(101102103104, which='first')
nt.assert_equal(blpair, 102101103104)
blpair = uvputils._conj_blpair(101102103104, which='second')
nt.assert_equal(blpair, 101102104103)
blpair = uvputils._conj_blpair(101102103104, which='both')
nt.assert_equal(blpair, 102101104103)
nt.assert_raises(ValueError, uvputils._conj_blpair, 102101103104, which='foo')
38 changes: 19 additions & 19 deletions hera_pspec/tests/test_uvpspec_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,45 @@
from hera_pspec import uvpspec_utils as uvputils

def test_conj_blpair_int():
conj_blpair = uvputils._conj_blpair_int(1002003004)
nt.assert_equal(conj_blpair, 3004001002)
conj_blpair = uvputils._conj_blpair_int(101102103104)
nt.assert_equal(conj_blpair, 103104101102)

def test_conj_bl_int():
conj_bl = uvputils._conj_bl_int(1002)
nt.assert_equal(conj_bl, 2001)
conj_bl = uvputils._conj_bl_int(101102)
nt.assert_equal(conj_bl, 102101)

def test_conj_blpair():
blpair = uvputils._conj_blpair(1002003004, which='first')
nt.assert_equal(blpair, 2001003004)
blpair = uvputils._conj_blpair(1002003004, which='second')
nt.assert_equal(blpair, 1002004003)
blpair = uvputils._conj_blpair(1002003004, which='both')
nt.assert_equal(blpair, 2001004003)
nt.assert_raises(ValueError, uvputils._conj_blpair, 2001003004, which='foo')
blpair = uvputils._conj_blpair(101102103104, which='first')
nt.assert_equal(blpair, 102101103104)
blpair = uvputils._conj_blpair(101102103104, which='second')
nt.assert_equal(blpair, 101102104103)
blpair = uvputils._conj_blpair(101102103104, which='both')
nt.assert_equal(blpair, 102101104103)
nt.assert_raises(ValueError, uvputils._conj_blpair, 102101103104, which='foo')

def test_fast_is_in():
blps = [ 2001003004, 2001003004, 2001003004, 2001003004,
1002004003, 1002004003, 1002004003, 1002004003,
2001004003, 2001004003, 1002004003, 2001003004 ]
blps = [ 102101103104, 102101103104, 102101103104, 102101103104,
101102104103, 101102104103, 101102104103, 101102104103,
102101104103, 102101104103, 102101104103, 102101104103 ]
times = [ 0.1, 0.15, 0.2, 0.25,
0.1, 0.15, 0.2, 0.25,
0.1, 0.15, 0.3, 0.3, ]
src_blpts = np.array(zip(blps, times))

nt.assert_true(uvputils._fast_is_in(src_blpts, [(1002004003, 0.2)])[0])
nt.assert_true(uvputils._fast_is_in(src_blpts, [(101102104103, 0.2)])[0])

def test_fast_lookup_blpairts():
# Construct array of blpair-time tuples (including some out of order)
blps = [ 2001003004, 2001003004, 2001003004, 2001003004,
1002004003, 1002004003, 1002004003, 1002004003,
2001004003, 2001004003, 1002004003, 2001003004 ]
blps = [ 102101103104, 102101103104, 102101103104, 102101103104,
101102104103, 101102104103, 101102104103, 101102104103,
102101104103, 102101104103, 102101104103, 102101104103 ]
times = [ 0.1, 0.15, 0.2, 0.25,
0.1, 0.15, 0.2, 0.25,
0.1, 0.15, 0.3, 0.3, ]
src_blpts = np.array(zip(blps, times))

# List of blpair-times to look up
query_blpts = [(2001003004, 0.1), (1002004003, 0.1), (1002004003, 0.25)]
query_blpts = [(102101103104, 0.1), (101102104103, 0.1), (101102104103, 0.25)]

# Look up indices, compare with expected result
idxs = uvputils._fast_lookup_blpairts(src_blpts, np.array(query_blpts))
Expand Down
33 changes: 23 additions & 10 deletions hera_pspec/uvpspec_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def _get_blpairs_from_bls(uvp, bls, only_pairs_in_bls=False):
# get blpair baselines in integer form
bl1 = np.floor(uvp.blpair_array / 1e6)
blpair_bls = np.vstack([bl1, uvp.blpair_array - bl1*1e6]).astype(np.int32).T

# ensure bls is in integer form
if isinstance(bls, tuple):
assert isinstance(bls[0], (int, np.integer)), "bls must be fed as a list of baseline tuples Ex: [(1, 2), ...]"
Expand Down Expand Up @@ -241,6 +242,10 @@ def _blpair_to_antnums(blpair):
ant2 = int(np.floor(blpair / 1e6 - ant1*1e3))
ant3 = int(np.floor(blpair / 1e3 - ant1*1e6 - ant2*1e3))
ant4 = int(np.floor(blpair - ant1*1e9 - ant2*1e6 - ant3*1e3))
ant1 -= 100
ant2 -= 100
ant3 -= 100
ant4 -= 100

# form antnums tuple
antnums = ((ant1, ant2), (ant3, ant4))
Expand All @@ -250,6 +255,9 @@ def _blpair_to_antnums(blpair):
def _antnums_to_blpair(antnums):
"""
Convert nested tuple of antenna numbers to baseline-pair integer.
A baseline-pair integer is an i12 integer that is the antenna numbers
+ 100 directly concatenated (i.e. string contatenation).
Ex: ((1, 2), (3, 4)) --> 101 + 102 + 103 + 104 --> 101102103014.
Parameters
----------
Expand All @@ -263,10 +271,10 @@ def _antnums_to_blpair(antnums):
baseline-pair integer
"""
# get antennas
ant1 = antnums[0][0]
ant2 = antnums[0][1]
ant3 = antnums[1][0]
ant4 = antnums[1][1]
ant1 = antnums[0][0] + 100
ant2 = antnums[0][1] + 100
ant3 = antnums[1][0] + 100
ant4 = antnums[1][1] + 100

# form blpair
blpair = int(ant1*1e9 + ant2*1e6 + ant3*1e3 + ant4)
Expand All @@ -290,6 +298,8 @@ def _bl_to_antnums(bl):
# get antennas
ant1 = int(np.floor(bl / 1e3))
ant2 = int(np.floor(bl - ant1*1e3))
ant1 -= 100
ant2 -= 100

# form antnums tuple
antnums = (ant1, ant2)
Expand All @@ -299,6 +309,9 @@ def _bl_to_antnums(bl):
def _antnums_to_bl(antnums):
"""
Convert tuple of antenna numbers to baseline integer.
A baseline integer is the two antenna numbers + 100
directly (i.e. string) concatenated. Ex: (1, 2) -->
101 + 102 --> 101102.
Parameters
----------
Expand All @@ -308,17 +321,17 @@ def _antnums_to_bl(antnums):
Returns
-------
blpair : <i6 integer
bl : <i6 integer
baseline integer
"""
# get antennas
ant1 = antnums[0]
ant2 = antnums[1]
ant1 = antnums[0] + 100
ant2 = antnums[1] + 100

# form blpair
blpair = int(ant1*1e3 + ant2)
# form bl
bl = int(ant1*1e3 + ant2)

return blpair
return bl

def _blpair_to_bls(blpair):
"""
Expand Down

0 comments on commit 0c883ba

Please sign in to comment.