def test_flag_apply(self): # test applying to UVData uv = UVData() uv.read_miriad(test_d_file) uv.flag_array = np.zeros_like(uv.flag_array, dtype=np.bool) uvf = UVFlag(uv, mode='flag') uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvf.flag_array[:, :, 0, :] = True uvf2 = xrfi.flag_apply(uvf, uv, return_net_flags=True) nt.assert_true(np.allclose(uv.flag_array, uvf2.flag_array)) # test applying to UVCal uv = UVCal() uv.read_calfits(test_c_file) uv.flag_array = np.zeros_like(uv.flag_array, dtype=np.bool) uvf = UVFlag(uv, mode='flag') uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvf.flag_array[:, :, 0, :, :] = True uvf2 = xrfi.flag_apply(uvf, uv, return_net_flags=True) nt.assert_true(np.allclose(uv.flag_array, uvf2.flag_array)) # test applying to waterfalls uv = UVData() uv.read_miriad(test_d_file) uv.flag_array = np.zeros_like(uv.flag_array, dtype=np.bool) uvf = UVFlag(uv, mode='flag', waterfall=True) uvf.flag_array[:, 0, :] = True xrfi.flag_apply(uvf, uv) nt.assert_true(np.allclose(uv.flag_array[:, :, 0, :], True)) nt.assert_true(np.allclose(uv.flag_array[:, :, 1:, :], False)) uv = UVCal() uv.read_calfits(test_c_file) uv.flag_array = np.zeros_like(uv.flag_array, dtype=np.bool) uvf = UVFlag(uv, mode='flag', waterfall=True) uvf.flag_array[:, 0, :] = True xrfi.flag_apply(uvf, uv) nt.assert_true(np.allclose(uv.flag_array[:, :, 0, :, :], True)) nt.assert_true(np.allclose(uv.flag_array[:, :, 1:, :, :], False)) # catch errors nt.assert_raises(ValueError, xrfi.flag_apply, uvf, 2) nt.assert_raises(ValueError, xrfi.flag_apply, 2, uv) uvf.mode = 'metric' nt.assert_raises(ValueError, xrfi.flag_apply, uvf, uv)
def test_clear_unused_attributes(): uv = UVFlag(test_f_file) nt.assert_true( hasattr(uv, 'baseline_array') & hasattr(uv, 'ant_1_array') & hasattr(uv, 'ant_2_array')) uv.type = 'antenna' uv.clear_unused_attributes() nt.assert_false( hasattr(uv, 'baseline_array') | hasattr(uv, 'ant_1_array') | hasattr(uv, 'ant_2_array')) uv.mode = 'flag' nt.assert_true(hasattr(uv, 'metric_array')) uv.clear_unused_attributes() nt.assert_false(hasattr(uv, 'metric_array')) # Start over uv = UVFlag(test_f_file) uv.ant_array = np.array([4]) uv.flag_array = np.array([5]) uv.clear_unused_attributes() nt.assert_false(hasattr(uv, 'ant_array')) nt.assert_false(hasattr(uv, 'flag_array'))
def test_watershed_flag(self): # generate a metrics and flag UVFlag object uv = UVData() uv.read_miriad(test_d_file) uvm = UVFlag(uv, history='I made this') uvf = UVFlag(uv, mode='flag') # set metric and flag arrays to specific values uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[0, 0, 1, 0] = 7. uvf.flag_array[0, 0, 0, 0] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[0, 0, :2, 0] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test flagging channels adjacent to fully flagged ones uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[:, :, 1, :] = 1. uvf.flag_array[:, :, 0, :] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., nsig_f=0.5, inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[:, :, :2, :] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test flagging times adjacent to fully flagged ones uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) times = np.unique(uv.time_array) inds1 = np.where(uv.time_array == times[0])[0] inds2 = np.where(uv.time_array == times[1])[0] uvm.metric_array[inds2, 0, :, 0] = 1. uvf.flag_array[inds1, 0, :, 0] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., nsig_t=0.5, inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[inds1, 0, :, 0] = True flag_array[inds2, 0, :, 0] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test antenna type objects uvc = UVCal() uvc.read_calfits(test_c_file) uvm = UVFlag(uvc, history='I made this') uvf = UVFlag(uvc, mode='flag') # set metric and flag arrays to specific values uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[0, 0, 0, 1, 0] = 7. uvf.flag_array[0, 0, 0, 0, 0] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[0, 0, 0, :2, 0] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test flagging channels adjacent to fully flagged ones uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[:, :, 1, :, :] = 1. uvf.flag_array[:, :, 0, :, :] = True # run watershed flag uvf2 = xrfi.watershed_flag(uvm, uvf, nsig_p=2., nsig_f=0.5, inplace=False) # check answer flag_array = np.zeros_like(uvf2.flag_array, dtype=np.bool) flag_array[:, :, :2, :, :] = True nt.assert_true(np.allclose(uvf2.flag_array, flag_array)) del(uvf2) # test flagging times adjacent to fully flagged ones uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[:, :, :, 1, :] = 1. uvf.flag_array[:, :, :, 0, :] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., nsig_t=0.5, inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[:, :, :, :2, :] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test waterfall types uv = UVData() uv.read_miriad(test_d_file) uvm = UVFlag(uv, history='I made this', waterfall=True) uvf = UVFlag(uv, mode='flag', waterfall=True) # set metric and flag arrays to specific values uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[0, 1, 0] = 7. uvf.flag_array[0, 0, 0] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[0, :2, 0] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test flagging channels adjacent to fully flagged ones uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[:, 1, :] = 1. uvf.flag_array[:, 0, :] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., nsig_f=0.5, inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[:, :2, :] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test flagging times adjacent to fully flagged ones uvm.metric_array = np.zeros_like(uvm.metric_array) uvf.flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) uvm.metric_array[1, :, :] = 1. uvf.flag_array[0, :, :] = True # run watershed flag xrfi.watershed_flag(uvm, uvf, nsig_p=2., nsig_t=0.5, inplace=True) # check answer flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[:2, :, :] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array))