def test_flag(self): # setup uv = UVData() uv.read_miriad(test_d_file) uvm = UVFlag(uv, history='I made this') # initialize array with specific values uvm.metric_array = np.zeros_like(uvm.metric_array) uvm.metric_array[0, 0, 0, 0] = 7. uvf = xrfi.flag(uvm, nsig_p=6.) nt.assert_true(uvf.mode == 'flag') flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[0, 0, 0, 0] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test channel flagging in baseline type uvm.metric_array = np.zeros_like(uvm.metric_array) uvm.metric_array[:, :, 0, :] = 7. uvm.metric_array[:, :, 1, :] = 3. uvf = xrfi.flag(uvm, nsig_p=6., nsig_f=2.) 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 time flagging in baseline type uvm.metric_array = np.zeros_like(uvm.metric_array) times = np.unique(uvm.time_array) inds1 = np.where(uvm.time_array == times[0])[0] inds2 = np.where(uvm.time_array == times[1])[0] uvm.metric_array[inds1, :, :, :] = 7. uvm.metric_array[inds2, :, :, :] = 3. uvf = xrfi.flag(uvm, nsig_p=6., nsig_t=2.) flag_array = np.zeros_like(uvf.flag_array, dtype=np.bool) flag_array[inds1, :, :, :] = True flag_array[inds2, :, :, :] = True nt.assert_true(np.allclose(uvf.flag_array, flag_array)) # test channel flagging in antenna type uv = UVCal() uv.read_calfits(test_c_file) uvm = UVFlag(uv, history='I made this') uvm.metric_array = np.zeros_like(uvm.metric_array) uvm.metric_array[:, :, 0, :, :] = 7. uvm.metric_array[:, :, 1, :, :] = 3. uvf = xrfi.flag(uvm, nsig_p=7., nsig_f=2.) 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 time flagging in antenna type uvm.metric_array = np.zeros_like(uvm.metric_array) uvm.metric_array[:, :, :, 0, :] = 7. uvm.metric_array[:, :, :, 1, :] = 3. uvf = xrfi.flag(uvm, nsig_p=6., nsig_t=2.) 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 channel flagging in waterfall type uv = UVData() uv.read_miriad(test_d_file) uvm = UVFlag(uv, history='I made this', waterfall=True) uvm.metric_array = np.zeros_like(uvm.metric_array) uvm.metric_array[:, 0, :] = 7. uvm.metric_array[:, 1, :] = 3. uvf = xrfi.flag(uvm, nsig_p=6., nsig_f=2.) 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 time flagging in waterfall type uvm.metric_array = np.zeros_like(uvm.metric_array) uvm.metric_array[0, :, :] = 7. uvm.metric_array[1, :, :] = 3. uvf = xrfi.flag(uvm, nsig_p=6., nsig_t=2.) 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)) # catch errors nt.assert_raises(ValueError, xrfi.flag, 2) nt.assert_raises(KeyError, xrfi.flag, uvm, avg_method='blah') uvm.type = 'blah' nt.assert_raises(ValueError, xrfi.flag, uvm)
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))