Skip to content

Commit

Permalink
test: properly conjugate bls to test antpair2ind
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-murray authored and bhazelton committed Feb 8, 2024
1 parent 5db7a9a commit 1f0fc1d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ positions are near surface of whatever celestial body their positions are refere

### Changed
- Improved performance for `antpair2ind` and `_key2inds` by using the
`blts_are_rectangular` parameter, and also by caching the results. To improve performance of the cache, the resulting indices are returned as slices whenever possible.
`blts_are_rectangular` parameter, and also by caching the results. To improve
performance of the cache, the resulting indices are returned as slices whenever possible.
- Changed the way files are passed for reading FHD files. Rather than one list including
all the types of files, each file type (e.g. params, obs, layout) must be passed to
their own parameter. This makes the API for reading FHD data files more similar to the
Expand Down
3 changes: 2 additions & 1 deletion pyuvdata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6285,7 +6285,8 @@ def determine_rectangularity(
return True, False


def slicify(ind):
def slicify(ind: slice | None | IterableType[int]) -> slice | None | IterableType[int]:
"""Convert an iterable of integers into a slice object if possible."""
if ind is None or isinstance(ind, slice):
return ind
if len(ind) == 0:
Expand Down
9 changes: 6 additions & 3 deletions pyuvdata/uvdata/tests/test_uvdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12712,10 +12712,13 @@ def test_antpair2ind_not_rect_not_ordered(hera_uvh5):

def test_antpair2ind_unordered_both_exist(hera_uvh5):
# Make a new object that has conjugated baselines in it as well.
conj = hera_uvh5.copy()
conj.conjugate_bls()
full = conj.concat(hera_uvh5)
hera_uvh5.conjugate_bls("ant1<ant2")
full = hera_uvh5.copy()
full.conjugate_bls("ant2<ant1")
full.fast_concat(hera_uvh5, axis="blt")

print(full.get_antpairs())
print(hera_uvh5.get_antpairs())
inds_ordered = full.antpair2ind((0, 1), ordered=True)
inds_unordered = full.antpair2ind((0, 1), ordered=False)

Expand Down
52 changes: 31 additions & 21 deletions pyuvdata/uvdata/uvdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,6 @@ def _warn_old_phase_attr(__name):
warnings.warn(warn_str, DeprecationWarning)


def _clear_antpair2ind_cache(self):
"""Clear the antpair2ind cache."""
self.__antpair2ind_cache = {}
self.__key2ind_cache = {}


def _clear_key2ind_cache(self):
"""Clear the antpair2ind cache."""
self.__key2ind_cache = {}


class UVData(UVBase):
"""
A class for defining a radio interferometer dataset.
Expand Down Expand Up @@ -276,7 +265,7 @@ def __init__(self):
expected_type=int,
form=("Nblts",),
acceptable_range=(0, 2147483647),
setter=_clear_antpair2ind_cache,
setter=self._clear_antpair2ind_cache,
)

desc = (
Expand All @@ -289,7 +278,7 @@ def __init__(self):
expected_type=int,
form=("Nblts",),
acceptable_range=(0, 2147483647),
setter=_clear_antpair2ind_cache,
setter=self._clear_antpair2ind_cache,
)

desc = (
Expand Down Expand Up @@ -338,7 +327,7 @@ def __init__(self):
expected_type=int,
acceptable_vals=list(np.arange(-8, 5)),
form=("Npols",),
setter=_clear_key2ind_cache,
setter=self._clear_key2ind_cache,
)

desc = (
Expand Down Expand Up @@ -786,6 +775,17 @@ def __init__(self):

super(UVData, self).__init__()

@staticmethod
def _clear_antpair2ind_cache(obj):
"""Clear the antpair2ind cache."""
obj.__antpair2ind_cache = {}
obj.__key2ind_cache = {}

@staticmethod
def _clear_key2ind_cache(obj):
"""Clear the antpair2ind cache."""
obj.__key2ind_cache = {}

@staticmethod
@combine_docstrings(new_uvdata, style=DocstringStyle.NUMPYDOC)
def new(**kwargs): # noqa: D102
Expand Down Expand Up @@ -3670,7 +3670,13 @@ def antnums_to_baseline(
use_miriad_convention=use_miriad_convention,
)

def antpair2ind(self, ant1, ant2=None, *, ordered=True) -> np.ndarray | slice:
def antpair2ind(
self,
ant1: int | tuple[int, int],
ant2: int | None = None,
*,
ordered: bool = True,
) -> np.ndarray | slice | None:
"""
Get indices along the baseline-time axis for a given antenna pair.

Expand All @@ -3691,7 +3697,8 @@ def antpair2ind(self, ant1, ant2=None, *, ordered=True) -> np.ndarray | slice:
inds : ndarray of int-64 or slice
If possible, returns a slice object that can be used to index the blt
axis and get back the data associated with antpair. If not, returns indices
of the antpair along the baseline-time axis.
of the antpair along the baseline-time axis. If the antpair does not exist
in the data, returns None.
"""
# check for expanded antpair or key
if ant2 is None:
Expand Down Expand Up @@ -3795,11 +3802,16 @@ def _key2inds(self, key: str | tuple[int] | tuple[int, int] | tuple[int, int, st
Note if a cross-pol baseline is requested, the polarization will
also be reversed so the appropriate correlations are returned.
e.g. asking for (1, 2, 'xy') may return conj(2, 1, 'yx'), which
is equivalent to the requesting baseline. See utils.conj_pol() for
is equivalent to the requested baseline. See utils.conj_pol() for
complete conjugation mapping.
pol_ind : tuple of ndarray of int
pol_ind : tuple of ndarray of int or slice or None
polarization indices for blt_ind1 and blt_ind2

Raises
------
KeyError
If the requested key is not in the data at all (either in given form
or its conjugate).
"""
orig_key = key

Expand Down Expand Up @@ -4764,9 +4776,7 @@ def conjugate_bls(self, convention="ant1<ant2", *, use_enu=True, uvw_tol=0.0):
self.ant_1_array[index_array], self.ant_2_array[index_array]
)
self.Nbls = np.unique(self.baseline_array).size

self.__antpair2ind_cache = {}
self._key2ind_cache = {}
self._clear_antpair2ind_cache(self)

def reorder_pols(
self,
Expand Down

0 comments on commit 1f0fc1d

Please sign in to comment.