Skip to content

Commit

Permalink
Deprecate unnecessary freq_range/freq_array setting on UVCal
Browse files Browse the repository at this point in the history
  • Loading branch information
bhazelton committed Nov 28, 2023
1 parent bd03009 commit ecde9af
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 31 deletions.
5 changes: 2 additions & 3 deletions pyuvdata/uvcal/calfits.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,10 @@ def read_calfits(
self.gain_scale = hdr.pop("GNSCALE", None)
self.x_orientation = hdr.pop("XORIENT")
self.cal_type = hdr.pop("CALTYPE")

# old files might have a freq range for gain types but we don't want them
if self.cal_type == "delay":
self.freq_range = list(map(float, hdr.pop("FRQRANGE").split(",")))
else:
if "FRQRANGE" in hdr:
self.freq_range = list(map(float, hdr.pop("FRQRANGE").split(",")))

self.cal_style = hdr.pop("CALSTYLE")
if self.cal_style == "sky":
Expand Down
88 changes: 62 additions & 26 deletions pyuvdata/uvcal/tests/test_uvcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,10 @@ def test_convert_to_gain(future_shapes, convention, same_freqs, delay_data_input
"Nfreqs will be required to be 1 for wide_band cals (including all "
"delay cals) starting in version 3.0",
"The input_flag_array is deprecated and will be removed in version 2.5",
"The freq_array attribute should not be set if wide_band=True. This will "
"become an error in version 3.0.",
"The channel_width attribute should not be set if wide_band=True. This "
"will become an error in version 3.0.",
],
):
delay_obj.check()
Expand Down Expand Up @@ -1114,6 +1118,8 @@ def test_select_times(
@pytest.mark.filterwarnings("ignore:This method will be removed in version 3.0 when")
@pytest.mark.filterwarnings("ignore:When converting a delay-style cal to future array")
@pytest.mark.filterwarnings("ignore:Nfreqs will be required to be 1 for wide_band")
@pytest.mark.filterwarnings("ignore:The freq_array attribute should not be set if")
@pytest.mark.filterwarnings("ignore:The channel_width attribute should not be set if")
@pytest.mark.parametrize("future_shapes", [True, False])
@pytest.mark.parametrize("caltype", ["gain", "delay"])
def test_select_frequencies(
Expand Down Expand Up @@ -1178,13 +1184,21 @@ def test_select_frequencies(
freqs_to_keep = calobj.freq_array[0, [0, 2, 4, 6, 8]]
warn_type = [UserWarning]
msg = ["Selected frequencies are not contiguous."]
extra_warn_type = []
extra_msg = []
if caltype == "delay":
warn_type += [DeprecationWarning]
msg += ["The input_flag_array is deprecated and will be removed in version 2.5"]
extra_warn_type += [DeprecationWarning]
extra_msg += [
"The input_flag_array is deprecated and will be removed in version 2.5"
]
if future_shapes:
warn_type += [DeprecationWarning]
msg += ["Nfreqs will be required to be 1 for wide_band cals"]
with uvtest.check_warnings(warn_type, match=msg):
extra_warn_type += [DeprecationWarning] * 3
extra_msg += [
"Nfreqs will be required to be 1 for wide_band cals",
"The freq_array attribute should not be set if wide_band=True",
"The channel_width attribute should not be set if wide_band=True",
]
with uvtest.check_warnings(warn_type + extra_warn_type, match=msg + extra_msg):
calobj2.select(frequencies=freqs_to_keep)
calobj2.write_calfits(write_file_calfits, clobber=True)

Expand All @@ -1203,13 +1217,7 @@ def test_select_frequencies(
freqs_to_keep = calobj2.freq_array[0, [0, 5, 6]]
warn_type = [UserWarning]
msg = ["Selected frequencies are not evenly spaced."]
if caltype == "delay":
warn_type += [DeprecationWarning]
msg += ["The input_flag_array is deprecated and will be removed in version 2.5"]
if future_shapes:
warn_type += [DeprecationWarning]
msg += ["Nfreqs will be required to be 1 for wide_band cals"]
with uvtest.check_warnings(warn_type, match=msg):
with uvtest.check_warnings(warn_type + extra_warn_type, match=msg + extra_msg):
calobj2.select(frequencies=freqs_to_keep)

with pytest.raises(
Expand All @@ -1221,6 +1229,7 @@ def test_select_frequencies(

@pytest.mark.filterwarnings("ignore:The input_flag_array is deprecated")
@pytest.mark.filterwarnings("ignore:This method will be removed in version 3.0 when")
@pytest.mark.filterwarnings("ignore:The freq_range attribute should not be set if")
@pytest.mark.filterwarnings("ignore:" + _future_array_shapes_warning)
@pytest.mark.parametrize("future_shapes", [True, False])
def test_select_frequencies_multispw(future_shapes, multi_spw_gain, tmp_path):
Expand Down Expand Up @@ -1266,7 +1275,15 @@ def test_select_frequencies_multispw(future_shapes, multi_spw_gain, tmp_path):
calobj2.freq_range[index, 1] = np.max(
np.squeeze(calobj2.freq_array)[spw_inds]
)
calobj2.check()
with uvtest.check_warnings(
DeprecationWarning,
match=[
"The freq_range attribute should not be set if cal_type='gain' and "
"wide_band=False. This will become an error in version 3.0.",
"The input_flag_array is deprecated and will be removed in version 2.5",
],
):
calobj2.check()

calobj2.select(frequencies=freqs_to_keep)

Expand Down Expand Up @@ -1335,6 +1352,8 @@ def test_select_frequencies_multispw(future_shapes, multi_spw_gain, tmp_path):
assert calobj3 == calobj2


@pytest.mark.filterwarnings("ignore:The freq_array attribute should not be set if")
@pytest.mark.filterwarnings("ignore:The channel_width attribute should not be set if")
@pytest.mark.filterwarnings("ignore:The input_flag_array is deprecated")
@pytest.mark.filterwarnings("ignore:This method will be removed in version 3.0 when")
@pytest.mark.filterwarnings("ignore:When converting a delay-style cal to future array")
Expand Down Expand Up @@ -1520,6 +1539,8 @@ def test_select_polarizations(
pytest.raises(ValueError, calobj.write_calfits, write_file_calfits)


@pytest.mark.filterwarnings("ignore:The freq_array attribute should not be set if")
@pytest.mark.filterwarnings("ignore:The channel_width attribute should not be set if")
@pytest.mark.filterwarnings("ignore:The input_flag_array is deprecated")
@pytest.mark.filterwarnings("ignore:This method will be removed in version 3.0 when")
@pytest.mark.filterwarnings("ignore:Nfreqs will be required to be 1 for wide_band")
Expand Down Expand Up @@ -2298,6 +2319,7 @@ def test_add_antennas_multispw(future_shapes, multi_spw_gain, quality, method):
assert calobj == calobj_full


@pytest.mark.filterwarnings("ignore:The freq_range attribute should not be set if")
@pytest.mark.filterwarnings("ignore:The input_flag_array is deprecated")
@pytest.mark.filterwarnings("ignore:This method will be removed in version 3.0 when")
@pytest.mark.parametrize("future_shapes", [True, False])
Expand Down Expand Up @@ -2346,6 +2368,9 @@ def test_add_frequencies(future_shapes, gain_data, method):
)[np.newaxis, :]
else:
calobj.freq_range = None
calobj2.freq_range = np.array(
[np.min(calobj2.freq_array), np.max(calobj2.freq_array)]
)
tqa = np.ones(calobj._total_quality_array.expected_shape(calobj))
tqa2 = np.zeros(calobj2._total_quality_array.expected_shape(calobj2))
if future_shapes:
Expand All @@ -2355,8 +2380,11 @@ def test_add_frequencies(future_shapes, gain_data, method):
calobj.total_quality_array = tqa
msg = [
"flex_spw_id_array is not set. It will be required starting in version 3.0 "
"for non-wide-band objects"
"for non-wide-band objects",
"The freq_range attribute should not be set if cal_type='gain' and "
"wide_band=False. This will become an error in version 3.0.",
]
warn_type = [DeprecationWarning, DeprecationWarning]
if method == "fast_concat":
msg.extend(
[
Expand All @@ -2366,6 +2394,7 @@ def test_add_frequencies(future_shapes, gain_data, method):
"Combined object will not have it set.",
]
)
warn_type.extend([UserWarning, UserWarning])
else:
msg.extend(
[
Expand All @@ -2375,10 +2404,9 @@ def test_add_frequencies(future_shapes, gain_data, method):
"object will not have it set.",
]
)
warn_type.extend([UserWarning, UserWarning])

with uvtest.check_warnings(
[DeprecationWarning, UserWarning, UserWarning], match=msg
):
with uvtest.check_warnings(warn_type, match=msg):
getattr(calobj, method)(calobj2, **kwargs)
assert np.allclose(
calobj.total_quality_array,
Expand All @@ -2398,13 +2426,13 @@ def test_add_frequencies(future_shapes, gain_data, method):
tot_tqa = np.concatenate([tqa, tqa2], axis=1)
calobj.total_quality_array = None
calobj2.total_quality_array = tqa2
calobj.freq_range = np.array([np.min(calobj.freq_array), np.max(calobj.freq_array)])
calobj2.freq_range = np.array(
[np.min(calobj2.freq_array), np.max(calobj2.freq_array)]
)
if future_shapes:
calobj.freq_range = np.array(
[np.min(calobj.freq_array), np.max(calobj.freq_array)]
)[np.newaxis, :]
calobj2.freq_range = np.array(
[np.min(calobj2.freq_array), np.max(calobj2.freq_array)]
)[np.newaxis, :]
calobj.freq_range = calobj.freq_range[np.newaxis, :]
calobj2.freq_range = calobj2.freq_range[np.newaxis, :]

getattr(calobj, method)(calobj2, **kwargs)
assert np.allclose(
Expand Down Expand Up @@ -2505,6 +2533,7 @@ def test_add_frequencies(future_shapes, gain_data, method):

@pytest.mark.filterwarnings("ignore:This method will be removed in version 3.0 when")
@pytest.mark.filterwarnings("ignore:One object has the freq_range set and one does not")
@pytest.mark.filterwarnings("ignore:The freq_range attribute should not be set if")
@pytest.mark.filterwarnings("ignore:Some objects have the freq_range set")
@pytest.mark.parametrize("future_shapes", [True, False])
@pytest.mark.parametrize("split_f_ind", [3, 5])
Expand Down Expand Up @@ -2540,15 +2569,22 @@ def test_add_frequencies_multispw(future_shapes, split_f_ind, method, multi_spw_
)[np.newaxis, :]
else:
calobj2.freq_range = None
calobj.freq_range = np.array(
[np.min(calobj.freq_array), np.max(calobj.freq_array)]
)
calobj_full.freq_range = None
warn_type = UserWarning
warn_type = [DeprecationWarning, UserWarning]
msg = [
"The freq_range attribute should not be set if cal_type='gain' and "
"wide_band=False. This will become an error in version 3.0."
]
if method == "fast_concat":
msg = (
msg.append(
"Some objects have the freq_range set and some do not. "
"Combined object will not have it set."
)
else:
msg = (
msg.append(
"One object has the freq_range set and one does not. Combined "
"object will not have it set."
)
Expand Down
30 changes: 28 additions & 2 deletions pyuvdata/uvcal/uvcal.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def __init__(self):
"Array of frequencies, center of the channel, "
"shape (1, Nfreqs) or (Nfreqs,) if future_array_shapes=True, units Hz."
"Not required if future_array_shapes=True and wide_band=True."
"Should not be set if future_array_shapes=True and wide_band=True."
)
# TODO: Spw axis to be collapsed in future release
self._freq_array = uvp.UVParameter(
Expand All @@ -209,6 +210,7 @@ def __init__(self):
"solutions are valid for. If future_array_shapes is False it is a "
"length 2 array with [start_frequency, end_frequency], otherwise it is an "
"array of shape (Nspws, 2). Units are Hz."
"Should not be set if cal_type='gain' and wide_band=False."
)
self._freq_range = uvp.UVParameter(
"freq_range",
Expand Down Expand Up @@ -1012,8 +1014,6 @@ def use_current_array_shapes(self):
self._freq_range.form = (2,)
if self.freq_range is not None:
self.freq_range = self.freq_range[0, :].tolist()
else:
self.freq_range = [np.min(self.freq_array), np.max(self.freq_array)]

def set_telescope_params(self, overwrite=False):
"""
Expand Down Expand Up @@ -1307,6 +1307,31 @@ def check(
"All values in the flex_spw_id_array must exist in the "
"spw_array."
)
# warn if freq_range or freq_array set when it shouldn't be
if (
self.cal_type == "gain"
and not self.wide_band
and self.freq_range is not None
):
warnings.warn(
"The freq_range attribute should not be set if cal_type='gain' "
"and wide_band=False. This will become an error in version 3.0.",
DeprecationWarning,
)
if self.wide_band:
if self.freq_array is not None:
warnings.warn(
"The freq_array attribute should not be set if wide_band=True. "
"This will become an error in version 3.0.",
DeprecationWarning,
)

if self.channel_width is not None:
warnings.warn(
"The channel_width attribute should not be set if wide_band=True. "
"This will become an error in version 3.0.",
DeprecationWarning,
)

if self.input_flag_array is not None:
warnings.warn(
Expand Down Expand Up @@ -2159,6 +2184,7 @@ def convert_to_gain(
self._set_gain()
self._set_wide_band(wide_band=False)
self.channel_width = channel_width
self.freq_range = None
self.gain_array = gain_array
self.delay_array = None
if self.quality_array is not None:
Expand Down

0 comments on commit ecde9af

Please sign in to comment.