Skip to content

Commit

Permalink
don't require order to be passed by name for reorder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bhazelton committed Nov 13, 2023
1 parent a02b5a3 commit e6bc8b5
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 110 deletions.
4 changes: 2 additions & 2 deletions docs/uvcal_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ specified by passing an index array.
True
>>> # Prepend a ``-`` to the sort string to sort in descending order.
>>> cal.reorder_antennas(order='-number')
>>> cal.reorder_antennas('-number')
>>> print(np.min(np.diff(cal.ant_array)) <= 0)
True
Expand Down Expand Up @@ -671,7 +671,7 @@ array for the time axis.
True
>>> # Prepend a ``-`` to the sort string to sort in descending order.
>>> cal.reorder_times(order='-time')
>>> cal.reorder_times('-time')
>>> print(np.min(np.diff(cal.time_array)) <= 0)
True
Expand Down
10 changes: 5 additions & 5 deletions docs/uvdata_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1600,17 +1600,17 @@ an option to sort the auto visibilities before the cross visibilities (``autos_f
>>> # Explicity sorting by 'time' then 'baseline' gets the same result
>>> uvd2 = uvd.copy()
>>> uvd2.reorder_blts(order='time', minor_order='baseline')
>>> uvd2.reorder_blts('time', minor_order='baseline')
>>> print(uvd == uvd2)
True
>>> uvd.reorder_blts(order='ant1', minor_order='ant2')
>>> uvd.reorder_blts('ant1', minor_order='ant2')
>>> print(np.min(np.diff(uvd.ant_1_array)) >= 0)
True
>>> # You can also sort and conjugate in a single step for the purposes of comparing two objects
>>> uvd.reorder_blts(order='bda', conj_convention='ant1<ant2')
>>> uvd2.reorder_blts(order='bda', conj_convention='ant1<ant2')
>>> uvd.reorder_blts('bda', conj_convention='ant1<ant2')
>>> uvd2.reorder_blts('bda', conj_convention='ant1<ant2')
>>> print(uvd == uvd2)
True
Expand Down Expand Up @@ -1680,7 +1680,7 @@ ordering set by the user.
>>> print(uvutils.polnum2str(uvd.polarization_array))
['rr', 'll', 'rl', 'lr']
>>> uvd.reorder_pols(order='CASA')
>>> uvd.reorder_pols('CASA')
>>> print(uvutils.polnum2str(uvd.polarization_array))
['rr', 'rl', 'lr', 'll']
Expand Down
66 changes: 33 additions & 33 deletions pyuvdata/uvcal/tests/test_uvcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1891,12 +1891,12 @@ def test_reorder_ants(
ant_num_diff = np.diff(calobj2.ant_array)
assert np.all(ant_num_diff > 0)

calobj2.reorder_antennas(order="-number")
calobj2.reorder_antennas("-number")
ant_num_diff = np.diff(calobj2.ant_array)
assert np.all(ant_num_diff < 0)

sorted_names = np.sort(calobj.antenna_names)
calobj.reorder_antennas(order="name")
calobj.reorder_antennas("name")
temp = np.asarray(calobj.antenna_names)
dtype_use = temp.dtype
name_array = np.zeros_like(calobj.ant_array, dtype=dtype_use)
Expand All @@ -1908,10 +1908,10 @@ def test_reorder_ants(
assert np.all(sorted_names == name_array)

# test sorting with an integer array. First resort back to by number
calobj2.reorder_antennas(order="number")
calobj2.reorder_antennas("number")
sorted_nums = [int(name[3:]) for name in sorted_names]
index_array = [np.nonzero(calobj2.ant_array == ant)[0][0] for ant in sorted_nums]
calobj2.reorder_antennas(order=index_array)
calobj2.reorder_antennas(index_array)
assert calobj2 == calobj


Expand All @@ -1921,21 +1921,21 @@ def test_reorder_ants_errors(gain_data):
match="order must be one of 'number', 'name', '-number', '-name' or an "
"index array of length Nants_data",
):
gain_data.reorder_antennas(order="foo")
gain_data.reorder_antennas("foo")

with pytest.raises(
ValueError,
match="If order is an index array, it must contain all indicies for the"
"ant_array, without duplicates.",
):
gain_data.reorder_antennas(order=gain_data.antenna_numbers.astype(float))
gain_data.reorder_antennas(gain_data.antenna_numbers.astype(float))

with pytest.raises(
ValueError,
match="If order is an index array, it must contain all indicies for the"
"ant_array, without duplicates.",
):
gain_data.reorder_antennas(order=gain_data.antenna_numbers[:8])
gain_data.reorder_antennas(gain_data.antenna_numbers[:8])


@pytest.mark.filterwarnings("ignore:The input_flag_array is deprecated")
Expand Down Expand Up @@ -2118,7 +2118,7 @@ def test_reorder_times(
calobj.reorder_times()
assert calobj == calobj2

calobj2.reorder_times(order="-time")
calobj2.reorder_times("-time")
if time_range:
time_diff = np.diff(calobj2.time_range[:, 0])
else:
Expand All @@ -2133,7 +2133,7 @@ def test_reorder_times(
total_quality_diff = np.diff(calobj2.total_quality_array, axis=2)
assert np.all(total_quality_diff < 0)

calobj.reorder_times(order=np.flip(np.arange(calobj.Ntimes)))
calobj.reorder_times(np.flip(np.arange(calobj.Ntimes)))
assert calobj == calobj2


Expand All @@ -2142,21 +2142,21 @@ def test_reorder_times_errors(gain_data):
ValueError,
match="order must be one of 'time', '-time' or an index array of length Ntimes",
):
gain_data.reorder_times(order="foo")
gain_data.reorder_times("foo")

with pytest.raises(
ValueError,
match="If order is an array, it must contain all indicies for the time axis, "
"without duplicates.",
):
gain_data.reorder_times(order=np.arange(gain_data.Ntimes) * 2)
gain_data.reorder_times(np.arange(gain_data.Ntimes) * 2)

with pytest.raises(
ValueError,
match="If order is an array, it must contain all indicies for the time axis, "
"without duplicates.",
):
gain_data.reorder_times(order=np.arange(7))
gain_data.reorder_times(np.arange(7))

gain_data = time_array_to_time_range(gain_data, keep_time_array=True)
with uvtest.check_warnings(
Expand Down Expand Up @@ -2206,11 +2206,11 @@ def test_reorder_jones(
calobj = calobj2.copy()

# this is a no-op because it's already sorted this way
calobj2.reorder_jones(order="-number")
calobj2.reorder_jones("-number")
jnum_diff = np.diff(calobj2.jones_array)
assert np.all(jnum_diff < 0)

calobj2.reorder_jones(order="number")
calobj2.reorder_jones("number")
jnum_diff = np.diff(calobj2.jones_array)
assert np.all(jnum_diff > 0)

Expand All @@ -2234,7 +2234,7 @@ def test_reorder_jones(
# test sorting with an index array. Sort back to number first so indexing works
sorted_nums = uvutils.jstr2num(sorted_names, x_orientation=calobj.x_orientation)
index_array = [np.nonzero(calobj.jones_array == num)[0][0] for num in sorted_nums]
calobj.reorder_jones(order=index_array)
calobj.reorder_jones(index_array)
assert calobj2 == calobj


Expand All @@ -2248,21 +2248,21 @@ def test_reorder_jones_errors(gain_data):
match="order must be one of 'number', 'name', '-number', '-name' or an "
"index array of length Njones",
):
calobj.reorder_jones(order="foo")
calobj.reorder_jones("foo")

with pytest.raises(
ValueError,
match="If order is an array, it must contain all indicies for "
"the jones axis, without duplicates.",
):
calobj.reorder_jones(order=np.arange(gain_data.Njones) * 2)
calobj.reorder_jones(np.arange(gain_data.Njones) * 2)

with pytest.raises(
ValueError,
match="If order is an array, it must contain all indicies for "
"the jones axis, without duplicates.",
):
calobj.reorder_jones(order=np.arange(2))
calobj.reorder_jones(np.arange(2))


@pytest.mark.filterwarnings("ignore:The input_flag_array is deprecated")
Expand Down Expand Up @@ -2325,14 +2325,14 @@ def test_add_different_sorting(
cal2 = calobj.select(jones=np.array([-6, -8]), inplace=False)

if sort_type == "ant":
cal1.reorder_antennas(order="number")
cal2.reorder_antennas(order="-number")
calobj.reorder_antennas(order="name")
cal1.reorder_antennas("number")
cal2.reorder_antennas("-number")
calobj.reorder_antennas("name")
order_check = cal1._ant_array == cal2._ant_array
elif sort_type == "time":
cal1.reorder_times(order="time")
cal2.reorder_times(order="-time")
calobj.reorder_times(order="time")
cal1.reorder_times("time")
cal2.reorder_times("-time")
calobj.reorder_times("time")
order_check = cal1._time_array == cal2._time_array
elif sort_type == "freq":
if wide_band:
Expand All @@ -2346,9 +2346,9 @@ def test_add_different_sorting(
calobj.reorder_freqs(channel_order="freq")
order_check = cal1._freq_array == cal2._freq_array
elif sort_type == "jones":
cal1.reorder_jones(order="name")
cal2.reorder_jones(order="-number")
calobj.reorder_jones(order="number")
cal1.reorder_jones("name")
cal2.reorder_jones("-number")
calobj.reorder_jones("number")
order_check = cal1._jones_array == cal2._jones_array

# Make sure that the order has actually been scrambled
Expand All @@ -2359,11 +2359,11 @@ def test_add_different_sorting(
cal4 = cal2 + cal1

if sort_type == "ant":
cal3.reorder_antennas(order="name")
cal4.reorder_antennas(order="name")
cal3.reorder_antennas("name")
cal4.reorder_antennas("name")
elif sort_type == "time":
cal3.reorder_times(order="time")
cal4.reorder_times(order="time")
cal3.reorder_times("time")
cal4.reorder_times("time")
elif sort_type == "freq":
if wide_band:
cal3.reorder_freqs()
Expand All @@ -2372,8 +2372,8 @@ def test_add_different_sorting(
cal3.reorder_freqs(channel_order="freq")
cal4.reorder_freqs(channel_order="freq")
elif sort_type == "jones":
cal3.reorder_jones(order="number")
cal4.reorder_jones(order="number")
cal3.reorder_jones("number")
cal4.reorder_jones("number")

# Deal with the history separately, since it will be different
assert str.startswith(cal3.history, calobj.history)
Expand Down
12 changes: 6 additions & 6 deletions pyuvdata/uvcal/uvcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,8 +1755,8 @@ def get_time_array(self):

def reorder_antennas(
self,
*,
order="number",
*,
run_check=True,
check_extra=True,
run_check_acceptability=True,
Expand Down Expand Up @@ -2000,8 +2000,8 @@ def reorder_freqs(

def reorder_times(
self,
*,
order="time",
*,
run_check=True,
check_extra=True,
run_check_acceptability=True,
Expand Down Expand Up @@ -2103,8 +2103,8 @@ def reorder_times(

def reorder_jones(
self,
*,
order="name",
*,
run_check=True,
check_extra=True,
run_check_acceptability=True,
Expand Down Expand Up @@ -2647,7 +2647,7 @@ def __add__(
this_ants_ind[other_argsort]
]

this.reorder_antennas(order=temp_ind)
this.reorder_antennas(temp_ind)

if len(this_times_ind) != 0:
this_argsort = np.argsort(this_times_ind)
Expand All @@ -2659,7 +2659,7 @@ def __add__(
this_times_ind[other_argsort]
]

this.reorder_times(order=temp_ind)
this.reorder_times(temp_ind)

if len(this_freq_ind) != 0:
this_argsort = np.argsort(this_freq_ind)
Expand Down Expand Up @@ -2689,7 +2689,7 @@ def __add__(
this_jones_ind[other_argsort]
]

this.reorder_jones(order=temp_ind)
this.reorder_jones(temp_ind)

# Update filename parameter
this.filename = uvutils._combine_filenames(this.filename, other.filename)
Expand Down
2 changes: 1 addition & 1 deletion pyuvdata/uvdata/miriad.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ def read_miriad(
else:
order = self.blt_order[0]
minor_order = None
self.reorder_blts(order=order, minor_order=minor_order)
self.reorder_blts(order, minor_order=minor_order)

# If the data set was recorded using the old phasing method, fix that now.
if fix_old_proj and projected:
Expand Down
2 changes: 1 addition & 1 deletion pyuvdata/uvdata/ms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2332,7 +2332,7 @@ def read_ms(
self.freq_array = np.expand_dims(self.freq_array, 0)

# order polarizations
self.reorder_pols(order=pol_order, run_check=False)
self.reorder_pols(pol_order, run_check=False)

if use_future_array_shapes:
self.use_future_array_shapes()
Expand Down
2 changes: 1 addition & 1 deletion pyuvdata/uvdata/tests/test_miriad.py
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ def test_roundtrip_optional_params(uv_in_paper, tmp_path):
assert uv_in == uv_out

# test with bda as well (single entry in tuple)
uv_in.reorder_blts(order="bda")
uv_in.reorder_blts("bda")

uv_in.write_miriad(testfile, clobber=True)
uv_out.read(testfile, use_future_array_shapes=True)
Expand Down
Loading

0 comments on commit e6bc8b5

Please sign in to comment.