Skip to content

Commit

Permalink
Merge pull request #461 from HERA-Team/fix_pyuvdata_v3
Browse files Browse the repository at this point in the history
Fix warning handling for pyuvdata v3.0
  • Loading branch information
steven-murray authored May 22, 2024
2 parents f3b4a97 + 154b6cc commit 75b4b24
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 94 deletions.
7 changes: 6 additions & 1 deletion hera_qm/firstcal_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import utils, metrics_io
import copy
import os
import warnings

try:
from sklearn import gaussian_process as gp
Expand Down Expand Up @@ -343,7 +344,11 @@ def __init__(self, calfits_files, use_gp=True):
"""
# Instantiate UVCal and read calfits
self.UVC = UVCal()
self.UVC.read_calfits(calfits_files, use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "Future array shapes are now always used"
)
self.UVC.read_calfits(calfits_files, use_future_array_shapes=True)

if hasattr(self.UVC, "telescope"):
x_orientation = self.UVC.telescope.x_orientation
Expand Down
11 changes: 7 additions & 4 deletions hera_qm/omnical_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,9 @@ def load_firstcal_gains(fc_file):
uvc = UVCal()
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "The shapes of several attributes"
"ignore", "Future array shapes are now always used"
)
uvc.read_calfits(fc_file)
uvc.use_future_array_shapes()
uvc.read_calfits(fc_file, use_future_array_shapes=True)
fc_gains = np.moveaxis(uvc.gain_array, 1, 2)[:, :, :, 0]
d_nu = np.mean(uvc.freq_array[1:] - uvc.freq_array[:-1])
d_phi = np.abs(np.mean(np.angle(fc_gains)[:, :, 1:] - np.angle(fc_gains)[:, :, :-1], axis=2))
Expand Down Expand Up @@ -454,7 +453,11 @@ def __init__(self, omni_calfits, history=''):

# Instantiate Data Object
self.uv = UVCal()
self.uv.read_calfits(omni_calfits, use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "Future array shapes are now always used"
)
self.uv.read_calfits(omni_calfits, use_future_array_shapes=True)

# Get relevant metadata
self.Nants = self.uv.Nants_data
Expand Down
4 changes: 4 additions & 0 deletions hera_qm/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ def test_strip_extension_return_ext_extension():
root, ext = utils.strip_extension(path, return_ext=True)
assert ext == path[-3:]

@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.parametrize(
"filein",
["a_priori_flags_integrations.yaml",
Expand Down Expand Up @@ -351,6 +352,7 @@ def test_apply_yaml_flags_uvdata(tmpdir, filein, flag_freqs, flag_times, flag_an
# this top one can be removed when we require pyuvdata >= 3.0
@pytest.mark.filterwarnings("ignore:Cannot preserve total_quality_array when")
@pytest.mark.filterwarnings("ignore:Changing number of antennas, but preserving")
@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.parametrize(
"filein",
["a_priori_flags_integrations.yaml",
Expand Down Expand Up @@ -417,6 +419,8 @@ def test_apply_yaml_flags_uvcal(filein):
pytest.raises(NotImplementedError, utils.apply_yaml_flags, uvc, a_priori_flag_yaml=test_flag,
throw_away_flagged_ants=True, ant_indices_only=False)


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
def test_apply_yaml_flags_errors():
test_flag_jds = os.path.join(DATA_PATH, 'a_priori_flags_jds.yaml')
test_c_file = os.path.join(DATA_PATH, 'zen.2457698.40355.xx.HH.uvcAA.omni.calfits')
Expand Down
18 changes: 12 additions & 6 deletions hera_qm/tests/test_vis_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import copy
import pytest
import warnings

pytestmark = pytest.mark.filterwarnings(
"ignore:The uvw_array does not match the expected values given the antenna positions.",
Expand All @@ -18,7 +19,9 @@
def vismetrics_data():
data = UVData()
filename = os.path.join(DATA_PATH, 'zen.2457698.40355.xx.HH.uvcAA')
data.read_miriad(filename, use_future_array_shapes=True, check_autos=False)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
data.read_miriad(filename, use_future_array_shapes=True, check_autos=False)

# massage the object to make it work with check_noise_variance
data.select(antenna_nums=data.get_ants()[0:10])
Expand Down Expand Up @@ -75,11 +78,13 @@ def test_check_noise_variance_inttime_error(vismetrics_data):
@pytest.fixture(scope='function')
def uvd():
uvd = UVData()
uvd.read_miriad(
os.path.join(DATA_PATH, 'zen.2458002.47754.xx.HH.uvA'),
projected=False, use_future_array_shapes=True,
check_autos=False
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvd.read_miriad(
os.path.join(DATA_PATH, 'zen.2458002.47754.xx.HH.uvA'),
projected=False, use_future_array_shapes=True,
check_autos=False
)
return uvd

def test_vis_bl_cov(uvd):
Expand Down Expand Up @@ -147,6 +152,7 @@ def test_plot_bl_bl_scatter(uvd):
plt.close('all')


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
def test_sequential_diff():
uvd = UVData()
uvd.read_miriad(os.path.join(DATA_PATH, 'zen.2457698.40355.xx.HH.uvcAA'), use_future_array_shapes=True)
Expand Down
77 changes: 53 additions & 24 deletions hera_qm/tests/test_xrfi.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@

@pytest.fixture(scope="session")
def uvdata_miriad_main():
uv = UVData.from_file(test_d_file, use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uv = UVData.from_file(test_d_file, use_future_array_shapes=True)
yield uv

# clean up when done
Expand All @@ -74,9 +76,11 @@ def uvdata_miriad(uvdata_miriad_main):

@pytest.fixture(scope="session")
def uvflag_metric_miriad_main(uvdata_miriad_main):
uvm = UVFlag(
uvdata_miriad_main, history='I made this', use_future_array_shapes=True
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvm = UVFlag(
uvdata_miriad_main, history='I made this', use_future_array_shapes=True
)
yield uvm
# clean up when done
del uvm
Expand All @@ -96,12 +100,14 @@ def uvflag_metric_miriad(uvflag_metric_miriad_main):

@pytest.fixture(scope="session")
def uvflag_metric_waterfall_miriad_main(uvdata_miriad_main):
uvm = UVFlag(
uvdata_miriad_main,
history='I made this',
waterfall=True,
use_future_array_shapes=True
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvm = UVFlag(
uvdata_miriad_main,
history='I made this',
waterfall=True,
use_future_array_shapes=True
)
yield uvm
# clean up when done
del uvm
Expand All @@ -121,7 +127,9 @@ def uvflag_metric_waterfall_miriad(uvflag_metric_waterfall_miriad_main):

@pytest.fixture(scope="session")
def uvflag_flag_miriad_main(uvdata_miriad_main):
uvf = UVFlag(uvdata_miriad_main, mode='flag', use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvf = UVFlag(uvdata_miriad_main, mode='flag', use_future_array_shapes=True)
yield uvf
# clean up when done
del uvf
Expand All @@ -141,12 +149,14 @@ def uvflag_flag_miriad(uvflag_flag_miriad_main):

@pytest.fixture(scope="session")
def uvflag_flag_waterfall_miriad_main(uvdata_miriad_main):
uvf = UVFlag(
uvdata_miriad_main,
mode='flag',
waterfall=True,
use_future_array_shapes=True
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvf = UVFlag(
uvdata_miriad_main,
mode='flag',
waterfall=True,
use_future_array_shapes=True
)
yield uvf
# clean up when done
del uvf
Expand All @@ -166,7 +176,9 @@ def uvflag_flag_waterfall_miriad(uvflag_flag_waterfall_miriad_main):

@pytest.fixture(scope="session")
def uvcal_calfits_main():
uvc = UVCal.from_file(test_c_file, use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvc = UVCal.from_file(test_c_file, use_future_array_shapes=True)
yield uvc

# clean up when done
Expand All @@ -188,9 +200,11 @@ def uvcal_calfits(uvcal_calfits_main):

@pytest.fixture(scope="session")
def uvflag_metric_calfits_main(uvcal_calfits_main):
uvm = UVFlag(
uvcal_calfits_main, history='I made this', use_future_array_shapes=True
)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvm = UVFlag(
uvcal_calfits_main, history='I made this', use_future_array_shapes=True
)
yield uvm

# clean up when done
Expand All @@ -212,7 +226,9 @@ def uvflag_metric_calfits(uvflag_metric_calfits_main):

@pytest.fixture(scope="session")
def uvflag_flag_calfits_main(uvcal_calfits_main):
uvf = UVFlag(uvcal_calfits_main, mode='flag', use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvf = UVFlag(uvcal_calfits_main, mode='flag', use_future_array_shapes=True)
yield uvf

# clean up when done
Expand All @@ -234,7 +250,9 @@ def uvflag_flag_calfits(uvflag_flag_calfits_main):

@pytest.fixture(scope="session")
def uvflag_f_main():
uvf = UVFlag(test_f_file, use_future_array_shapes=True)
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Future array shapes are now always used")
uvf = UVFlag(test_f_file, use_future_array_shapes=True)
yield uvf

# clean up when done
Expand Down Expand Up @@ -1042,7 +1060,8 @@ def test_flag(

# test time flagging in baseline type
# convert to current shapes for test coverage
uvm.use_current_array_shapes()
if hasattr(uvm, "use_current_array_shapes"):
uvm.use_current_array_shapes()
uvm.metric_array = np.zeros_like(uvm.metric_array)
times = np.unique(uvm.time_array)
inds1 = np.where(uvm.time_array == times[0])[0]
Expand Down Expand Up @@ -1113,6 +1132,7 @@ def test_unflag():
assert True


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:instrument is not the same")
def test_flag_apply(
uvdata_miriad,
Expand Down Expand Up @@ -1405,6 +1425,7 @@ def test_xrfi_run_step(tmpdir):


# TODO: check whether invalid value encountered in subtract warning is expected
@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:invalid value encountered in subtract:RuntimeWarning")
@pytest.mark.filterwarnings("ignore:Mean of empty slice:RuntimeWarning")
@pytest.mark.filterwarnings("ignore:Degrees of freedom <= 0 for slice:RuntimeWarning")
Expand Down Expand Up @@ -1813,6 +1834,7 @@ def test_xrfi_run(tmpdir):
for fname in [ocal_file, acal_file, model_file, raw_dfile]:
os.remove(fname)

@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:This object is already a waterfall")
@pytest.mark.filterwarnings("ignore:x_orientation is not the same")
def test_xrfi_run_edgeflag(tmpdir):
Expand Down Expand Up @@ -1932,6 +1954,7 @@ def test_xrfi_run_edgeflag(tmpdir):



@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:This object is already a waterfall")
@pytest.mark.filterwarnings("ignore:x_orientation is not the same")
def test_xrfi_run_multifile(tmpdir):
Expand Down Expand Up @@ -2040,6 +2063,7 @@ def test_xrfi_run_multifile(tmpdir):
if ext == 'flags2.h5':
assert not np.all(uvf.flag_array)

@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:This object is already a waterfall")
def test_day_threshold_run(tmpdir):
# The warnings are because we use UVFlag.to_waterfall() on the total chisquareds
Expand Down Expand Up @@ -2110,6 +2134,7 @@ def test_day_threshold_run(tmpdir):
assert os.path.exists(calfile)


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:This object is already a waterfall")
def test_day_threshold_run_yaml(tmpdir):
# The warnings are because we use UVFlag.to_waterfall() on the total chisquareds
Expand Down Expand Up @@ -2164,6 +2189,7 @@ def test_day_threshold_run_yaml(tmpdir):

@pytest.mark.filterwarnings("ignore:All-NaN slice encountered")
@pytest.mark.filterwarnings("ignore:instrument is not the same")
@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
def test_day_threshold_run_data_only(tmpdir):
# The warnings are because we use UVFlag.to_waterfall() on the total chisquareds
# This doesn't hurt anything, and lets us streamline the pipe
Expand Down Expand Up @@ -2217,6 +2243,7 @@ def test_day_threshold_run_data_only(tmpdir):
assert os.path.exists(calfile)


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:This object is already a waterfall")
def test_day_threshold_run_cal_only(tmpdir):
# The warnings are because we use UVFlag.to_waterfall() on the total chisquareds
Expand Down Expand Up @@ -2288,6 +2315,7 @@ def test_day_threshold_run_cal_only(tmpdir):
assert os.path.exists(calfile)


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:instrument is not the same")
def test_day_threshold_run_omnivis_only(tmpdir):
# The warnings are because we use UVFlag.to_waterfall() on the total chisquareds
Expand Down Expand Up @@ -2655,6 +2683,7 @@ def test_threshold_wf_exceptions(uvflag_f):
pytest.raises(ValueError, xrfi.threshold_wf, 'foo') # not a UVFlag object


@pytest.mark.filterwarnings("ignore:Future array shapes are now always used")
@pytest.mark.filterwarnings("ignore:This object is already a waterfall")
@pytest.mark.filterwarnings("ignore:instrument is not the same")
def test_xrfi_h3c_idr2_1_run(tmp_path, uvcal_calfits):
Expand Down
6 changes: 5 additions & 1 deletion hera_qm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,11 @@ def apply_yaml_flags(uv, a_priori_flag_yaml, lat_lon_alt_degrees=None, telescope
raise NotImplementedError("uv must be a UVData, UVCal, or UVFlag object.")

# this is a no-op if it's already using future shapes
uv.use_future_array_shapes()
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "Future array shapes are now always used"
)
uv.use_future_array_shapes()

# if UVCal provided and lst_array is None, get lst_array from times.
# If lat_lon_alt is not specified, try to infer it from the telescope name, which calfits files generally carry around
Expand Down
Loading

0 comments on commit 75b4b24

Please sign in to comment.