def test_freq_broadcast_whole_band(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) # spoof the metric array ins.metric_array[:] = 1 ins.metric_array[2, 10:20] = 10 ins.metric_array[4, 40:50] = 10 ins.metric_ms = ins.mean_subtract() shape_dict = { 'shape1': [ins.freq_array[10], ins.freq_array[20]], 'shape2': [ins.freq_array[40], ins.freq_array[50]] } mf = MF(ins.freq_array, 5, shape_dict=shape_dict, broadcast_streak=True) mf.apply_match_test(ins, event_record=True, freq_broadcast=True) assert np.all(ins.metric_array.mask[2]) assert np.all(ins.metric_array.mask[4])
def test_sig_plot(): matplotlib = pytest.importorskip("matplotlib") obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs) outdir = os.path.join(DATA_PATH, 'test_plots') prefix = '%s/%s_flagged' % (outdir, obs) dataplotfile = '%s_SSINS.pdf' % prefix outfile = '%s_SSINS_sig.pdf' % prefix ins = INS(insfile) shape_dict = { 'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8], 'TV8': [1.88e8, 1.95e8] } sig_thresh = {'TV6': 5, 'TV7': 5, 'TV8': 5, 'narrow': 5, 'streak': 5} mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict) mf.apply_match_test(ins) xticks = np.arange(0, 384, 96) xticklabels = ['%.1f' % (10**-6 * ins.freq_array[tick]) for tick in xticks] yticks = np.arange(0, 50, 10) yticklabels = ['%i' % (2 * tick) for tick in yticks] cp.INS_plot(ins, prefix) assert os.path.exists(outfile), "The first plot was not made" assert os.path.exists(dataplotfile), "The second plot was not made" os.remove(outfile) os.remove(dataplotfile) os.rmdir(outdir)
def test_time_broadcast_no_new_event(): """ Tests that a new event is not added because the agression threshold is not exceeded. """ obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) # Mock a simple metric_array and freq_array (and weights...) ins.metric_array[:] = 1 ins.weights_array = np.copy(ins.metric_array) ins.weights_square_array = np.copy(ins.weights_array) ins.metric_ms = ins.mean_subtract() ins.sig_array = np.ma.copy(ins.metric_ms) sig_thresh = {'narrow': 5} mf = MF(ins.freq_array, sig_thresh, streak=False, tb_aggro=0.5) # Put in an outlier so it gets to samp_thresh_test ins.metric_array[1, 10] = 100 ins.metric_ms = ins.mean_subtract() mf.apply_match_test(ins, event_record=True, time_broadcast=True) event = mf.time_broadcast(ins, ins.match_events[0], event_record=True) assert event == ins.match_events[0]
def test_freq_broadcast_no_new_event(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) # spoof the metric array ins.metric_array[:] = 1 ins.metric_array[2, 10:20] = 10 ins.metric_ms = ins.mean_subtract() # Slice will go up to 21 since shapes are inclusive at the boundaries # when boundary is on channel center shape_dict = {'shape1': [ins.freq_array[10], ins.freq_array[20]]} broadcast_dict = {'sb2': [ins.freq_array[30], ins.freq_array[59]]} test_event = (slice(2, 3), slice(10, 21), 'shape1', 10) mf = MF(ins.freq_array, 5, shape_dict=shape_dict, broadcast_streak=False, broadcast_dict=broadcast_dict) mf.apply_match_test(ins, event_record=True, freq_broadcast=True) event = mf.freq_broadcast(ins, event=test_event, event_record=True) assert event == test_event assert ins.match_events[0][:-1] == test_event[:-1]
def test_time_broadcast(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) # Mock a simple metric_array and freq_array ins.metric_array[:] = 1 ins.weights_array = np.copy(ins.metric_array) ins.weights_square_array = np.copy(ins.weights_array) ins.metric_ms = ins.mean_subtract() ins.sig_array = np.ma.copy(ins.metric_ms) # Arbitrarily flag enough data in channel 10 sig_thresh = {'narrow': 5} mf = MF(ins.freq_array, sig_thresh, streak=False, tb_aggro=0.5) ins.metric_array[4:, 9] = np.ma.masked ins.metric_array[4:, 10] = np.ma.masked # Put in an outlier so it gets to samp_thresh_test ins.metric_array[2, 9] = 100 ins.metric_array[1, 10] = 100 ins.metric_ms = ins.mean_subtract() bool_ind = np.zeros(ins.metric_array.shape, dtype=bool) bool_ind[:, 10] = 1 bool_ind[:, 9] = 1 mf.apply_match_test(ins, event_record=True, time_broadcast=True) print(ins.match_events) test_match_events = [ (slice(1, 2), slice(10, 11), 'narrow_%.3fMHz' % (ins.freq_array[10] * 10**(-6))), (slice(0, ins.Ntimes), slice(10, 11), 'time_broadcast_narrow_%.3fMHz' % (ins.freq_array[10] * 10**(-6))), (slice(2, 3), slice(9, 10), 'narrow_%.3fMHz' % (ins.freq_array[9] * 10**(-6))), (slice(0, ins.Ntimes), slice(9, 10), 'time_broadcast_narrow_%.3fMHz' % (ins.freq_array[9] * 10**(-6))) ] # Test stuff assert np.all( ins.metric_array.mask == bool_ind), "The right flags were not applied" for i, event in enumerate(test_match_events): assert ins.match_events[ i][:-1] == event, "The events weren't appended correctly" # Test that writing with samp_thresh flags is OK ins.write(out_prefix, output_type='match_events') test_match_events_read = ins.match_events_read(match_outfile) os.remove(match_outfile) assert ins.match_events == test_match_events_read # Test that exception is raised when tb_aggro is too high with pytest.raises(ValueError): mf = MF(ins.freq_array, {'narrow': 5, 'streak': 5}, tb_aggro=100) mf.apply_samp_thresh_test(ins, (slice(1, 2), slice(10, 11), 'narrow'))
def test_calc_occ(): obs = "1061313128_99bl_1pol_half_time_SSINS" testfile = os.path.join(DATA_PATH, f"{obs}.h5") ins = INS(testfile) # Mock some flaggable data ins.select(freq_chans=np.arange(32), times=ins.time_array[:22]) ins.metric_array[:] = 1 ins.weights_array[:] = 10 ins.weights_square_array[:] = 10 # Make some outliers # Narrowband in 1th, 26th, and 31th frequency ins.metric_array[1, 1, :] = 100 ins.metric_array[1, 30, :] = 100 ins.metric_array[3:14, 26, :] = 100 # Arbitrary shape in 2, 3, 4 ins.metric_array[3:14, 2:25, :] = 100 ins.metric_array[[0, -1], :, :] = np.ma.masked ins.metric_array[:, [0, -1], :] = np.ma.masked ins.metric_ms = ins.mean_subtract() num_int_flag = 2 num_chan_flag = 2 num_init_flag = np.sum(ins.metric_array.mask) ch_wid = ins.freq_array[1] - ins.freq_array[0] shape_dict = {"shape": [ins.freq_array[2] + 0.1 * ch_wid, ins.freq_array[24] - 0.1 * ch_wid]} mf = MF(ins.freq_array, 5, tb_aggro=0.5, shape_dict=shape_dict) mf.apply_match_test(ins, time_broadcast=True) occ_dict = util.calc_occ(ins, mf, num_init_flag, num_int_flag=2, lump_narrowband=False) assert occ_dict["streak"] == 0 assert occ_dict["narrow_%.3fMHz" % (ins.freq_array[1] * 10**(-6))] == 0.05 assert occ_dict["narrow_%.3fMHz" % (ins.freq_array[26] * 10**(-6))] == 1 assert occ_dict["narrow_%.3fMHz" % (ins.freq_array[30] * 10**(-6))] == 0.05 assert occ_dict["shape"] == 1 occ_dict = util.calc_occ(ins, mf, num_init_flag, num_int_flag=2, lump_narrowband=True) # total narrow over total valid assert occ_dict["narrow"] == 24 / 600 assert occ_dict["streak"] == 0 assert occ_dict["shape"] == 1 assert "narrow_%.3fMHz" % (ins.freq_array[1] * 10**(-6)) not in occ_dict.keys() assert "narrow_%.3fMHz" % (ins.freq_array[30] * 10**(-6)) not in occ_dict.keys() yml_outpath = os.path.join(DATA_PATH, "test_occ_.yml") with open(yml_outpath, "w") as occ_file: yaml.safe_dump(occ_dict, occ_file) os.remove(yml_outpath)
def test_apply_match_test(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs) ins = INS(insfile) # Mock a simple metric_array and freq_array ins.metric_array = np.ma.ones([10, 20, 1]) ins.weights_array = np.copy(ins.metric_array) ins.freq_array = np.zeros([1, 20]) ins.freq_array = np.arange(20) # Make a shape dictionary for a shape that will be injected later shape = [7.9, 12.1] shape_dict = {'shape': shape} sig_thresh = {'shape': 5, 'narrow': 5, 'streak': 5} mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict) # Inject a shape, narrow, and streak event ins.metric_array[3, 5] = 10 ins.metric_array[5] = 10 ins.metric_array[7, 7:13] = 10 ins.metric_ms = ins.mean_subtract() ins.sig_array = np.ma.copy(ins.metric_ms) mf.apply_match_test(ins, event_record=True) # Check that the right events are flagged test_mask = np.zeros(ins.metric_array.shape, dtype=bool) test_mask[3, 5] = 1 test_mask[5] = 1 test_mask[7, 7:13] = 1 assert np.all(test_mask == ins.metric_array.mask), "Flags are incorrect" test_match_events_slc = [(5, slice(0, 20), 'streak'), (7, slice(7, 13), 'shape'), (3, slice(5, 6), 'narrow')] for i, event in enumerate(test_match_events_slc): assert ins.match_events[i][:-1] == test_match_events_slc[ i], "%ith event is wrong" % i assert not np.any([ins.match_events[i][-1] < 5 for i in range(3) ]), "Some significances were less than 5" # Test a funny if block that is required when the last time in a shape is flagged ins.metric_array[1:, 7:13] = np.ma.masked ins.metric_ms[0, 7:13] = 10 mf.apply_match_test(ins, event_record=True) assert np.all(ins.metric_ms.mask[:, 7:13] ), "All the times were not flagged for the shape"
def test_apply_samp_thresh_dep_error_match_test(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) mf = MF(ins.freq_array, 5, tb_aggro=0.2) with pytest.raises(ValueError, match="apply_samp_thresh has been deprecated"): mf.apply_match_test(ins, apply_samp_thresh=True)
def test_samp_thresh(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs) out_prefix = os.path.join(DATA_PATH, '%s_test' % obs) match_outfile = '%s_SSINS_match_events.yml' % out_prefix ins = INS(insfile) # Mock a simple metric_array and freq_array ins.metric_array = np.ma.ones([10, 20, 1]) ins.weights_array = np.copy(ins.metric_array) ins.metric_ms = ins.mean_subtract() ins.sig_array = np.ma.copy(ins.metric_ms) ins.freq_array = np.zeros([1, 20]) ins.freq_array = np.arange(20) # Arbitrarily flag enough data in channel 10 sig_thresh = {'narrow': 5} mf = MF(ins.freq_array, sig_thresh, streak=False, N_samp_thresh=5) ins.metric_array[3:, 10] = np.ma.masked ins.metric_array[3:, 9] = np.ma.masked # Put in an outlier so it gets to samp_thresh_test ins.metric_array[0, 11] = 10 ins.metric_ms = ins.mean_subtract() bool_ind = np.zeros(ins.metric_array.shape, dtype=bool) bool_ind[:, 10] = 1 bool_ind[:, 9] = 1 bool_ind[0, 11] = 1 mf.apply_match_test(ins, event_record=True, apply_samp_thresh=True) test_match_events = [(0, slice(11, 12), 'narrow')] test_match_events += [(ind, slice(9, 10), 'samp_thresh') for ind in range(3)] test_match_events += [(ind, slice(10, 11), 'samp_thresh') for ind in range(3)] # Test stuff assert np.all( ins.metric_array.mask == bool_ind), "The right flags were not applied" for i, event in enumerate(test_match_events): assert ins.match_events[ i][:-1] == event, "The events weren't appended correctly" # Test that writing with samp_thresh flags is OK ins.write(out_prefix, output_type='match_events') test_match_events_read = ins.match_events_read(match_outfile) os.remove(match_outfile) assert ins.match_events == test_match_events_read # Test that exception is raised when N_samp_thresh is too high with pytest.raises(ValueError): mf = MF(ins.freq_array, {'narrow': 5, 'streak': 5}, N_samp_thresh=100) mf.apply_samp_thresh_test(ins)
def test_freq_broadcast_no_dict(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) mf = MF(ins.freq_array, 5) with pytest.raises(ValueError, match="MF object does not have a broadcast_dict"): mf.apply_match_test(ins, freq_broadcast=True)
def test_freq_broadcast_subbands(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') out_prefix = os.path.join(DATA_PATH, f'{obs}_test') match_outfile = f'{out_prefix}_SSINS_match_events.yml' ins = INS(insfile) # spoof the metric array ins.metric_array[:] = 1 ins.metric_array[2, 10:20] = 10 ins.metric_array[4, 40:50] = 10 ins.metric_ms = ins.mean_subtract() # Slice will go up to 21 since shapes are inclusive at the boundaries # when boundary is on channel center shape_dict = { 'shape1': [ins.freq_array[10], ins.freq_array[20]], 'shape2': [ins.freq_array[40], ins.freq_array[50]] } # boundaries are INCLUSIVE broadcast_dict = { 'sb1': [ins.freq_array[0], ins.freq_array[29]], 'sb2': [ins.freq_array[30], ins.freq_array[59]] } mf = MF(ins.freq_array, 5, shape_dict=shape_dict, broadcast_streak=False, broadcast_dict=broadcast_dict) mf.apply_match_test(ins, event_record=True, freq_broadcast=True) assert np.all(ins.metric_array.mask[2, :30]) assert not np.any(ins.metric_array.mask[2, 30:]) assert np.all(ins.metric_array.mask[4, 30:60]) assert not np.any(ins.metric_array.mask[4, :30]) assert not np.any(ins.metric_array.mask[4, 60:]) print(ins.match_events) test_match_events = [(slice(2, 3), slice(10, 21), 'shape1'), (slice(2, 3), slice(0, 30), 'freq_broadcast_sb1'), (slice(4, 5), slice(40, 51), 'shape2'), (slice(4, 5), slice(30, 60), 'freq_broadcast_sb2')] for event, test_event in zip(ins.match_events, test_match_events): assert event[:3] == test_event
def test_write_meta(): obs = "1061313128_99bl_1pol_half_time" testfile = os.path.join(DATA_PATH, f"{obs}.uvfits") prefix = os.path.join(DATA_PATH, f"{obs}_test") uvd = UVData() uvd.read(testfile, freq_chans=np.arange(32)) ss = SS() ss.read(testfile, freq_chans=np.arange(32), diff=True) uvf = UVFlag(uvd, mode="flag", waterfall=True) ins = INS(ss) ins.metric_array[:] = 1 ins.weights_array[:] = 10 ins.weights_square_array[:] = 10 # Make some outliers # Narrowband in 1th, 26th, and 31th frequency ins.metric_array[1, 1, :] = 100 ins.metric_array[1, 30, :] = 100 ins.metric_array[3:14, 26, :] = 100 # Arbitrary shape in 2, 3, 4 ins.metric_array[3:14, 2:25, :] = 100 ins.metric_array[[0, -1], :, :] = np.ma.masked ins.metric_array[:, [0, -1], :] = np.ma.masked ins.metric_ms = ins.mean_subtract() ch_wid = ins.freq_array[1] - ins.freq_array[0] shape_dict = { "shape": [ins.freq_array[2] + 0.1 * ch_wid, ins.freq_array[24] - 0.1 * ch_wid] } mf = MF(ins.freq_array, 5, tb_aggro=0.5, shape_dict=shape_dict) mf.apply_match_test(ins, time_broadcast=True) util.write_meta(prefix, ins, uvf=uvf, mf=mf, clobber=True) for data_type in ["data", "mask", "flags"]: path = f"{prefix}_SSINS_{data_type}.h5" assert os.path.exists(path) os.remove(path) for data_type in ["match_events", "matchfilter"]: path = f"{prefix}_SSINS_{data_type}.yml" assert os.path.exists(path) os.remove(path)
ylabel='Time (UTC)') # Try to save memory - hope for garbage collector del ss # Set up MF flagging for routine shapes shape_dict = {'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8], 'TV8': [1.88e8, 1.95e8], 'TV9': [1.95e8, 2.02e8]} sig_thresh = {shape: 5 for shape in shape_dict} sig_thresh['narrow'] = 5 sig_thresh['streak'] = 10 mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict, tb_aggro=0.5, broadcast_streak=True) # Do the flagging mf.apply_match_test(ins, event_record=True, time_broadcast=True, freq_broadcast=True) Catalog_Plot.INS_plot(ins, f'{prefix}_flagged', data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5, title=args.obsid, xlabel='Frequency (Mhz)', ylabel='Time (UTC)') # Make the flag object uvd = UVData() uvd.read(args.uvd, read_data=False) uvf = UVFlag(uvd, mode='flag', waterfall=True) uvf.flag_array = ins.mask_to_flags() # Write SSINS outputs util.write_meta(prefix, ins, uvf=uvf, mf=mf) if args.write_uvfits: if (not args.rfi_flag) and (not args.correct):
occ_dict = {sig: {shape: {} for shape in shapes} for sig in sig_list} for obs in obslist: flist = glob.glob('%s/metadata/%s*' % (basedir, obs)) if len(flist): read_paths = util.read_paths_construct(basedir, 'original', obs, 'INS') for sig_thresh in sig_list: ins = INS(read_paths=read_paths, obs=obs, outpath=outdir, flag_choice='original') mf = MF(ins, sig_thresh=sig_thresh, N_thresh=15, shape_dict=shape_dict) mf.apply_match_test(apply_N_thresh=True) occ_dict[sig_thresh]['total'][obs] = np.mean(ins.data.mask[:, 0, :, 0], axis=0) if len(ins.match_events): event_frac = util.event_fraction(ins.match_events, ins.data.shape[0], shapes, 384) for shape in shapes[:-1]: occ_dict[sig_thresh][shape][obs] = event_frac[shape] if obs == '1061312152' and sig_thresh is 5: pickle.dump(mf.slice_dict, open('%s/long_run_shape_dict.pik' % outdir, 'wb')) del ins del mf
ax, ins.data[:, 0, :, 0], cbar_label='Amplitude (UNCALIB)', freq_array=ins.freq_array[0], aspect=aspect) fig.savefig('%s/%s_INS.png' % (outpath, obs)) fig_new, ax_new = plt.subplots(figsize=(16, 9), ncols=2) plot_lib.image_plot(fig, ax_new[0], ins.data_ms[:, 0, :, 0], cbar_label='Deviation ($\hat{\sigma}$)', freq_array=ins.freq_array[0], cmap=cm.coolwarm, aspect=aspect) mf = MF(ins, sig_thresh=5) mf.apply_match_test() plot_lib.image_plot(fig_new, ax_new[1], ins.data_ms[:, 0, :, 0], cbar_label='Deviation ($\hat{\sigma}$)', freq_array=ins.freq_array[0], cmap=cm.coolwarm, aspect=aspect, mask_color='black') fig_new.savefig('%s/%s_INS_MS.png' % (outpath, obs))
action='store', help='The base directory for saving all outputs') args = parser.parse_args() ss = SS() ss.read(args.inpath, ant_str='cross') ins = INS(ss) ins.metric_array[:, :82] = np.ma.masked ins.metric_array[:, :-21] = np.ma.masked ins.order = 1 ins.metric_ms = ins.mean_subtract() shape_dict = { 'TV4': [1.74e8, 1.82e8], 'TV5': [1.82e8, 1.9e8], 'TV6': [1.9e8, 1.98e8], 'dig1': [1.125e8, 1.15625e8], 'dig2': [1.375e8, 1.40625e8], 'dig3': [1.625e8, 1.65625e8], 'dig4': [1.875e8, 1.90625e8] } mf = MF(ins.freq_array, 5, shape_dict=shape_dict, N_samp_thresh=15) mf.apply_match_test(ins, apply_samp_thresh=True) prefix = '%s/%s' % (args.outpath, args.obs) ins.write(prefix) ins.write(prefix, output_type='mask') ins.write(prefix, output_type='match_events')
outpath = '/Users/mike_e_dubs/General/%s' % obs read_paths = util.read_paths_construct(indir, None, obs, 'INS') ins = INS(read_paths=read_paths, obs=obs, outpath=outpath) mf = MF(ins, sig_thresh=5) fig, ax = plt.subplots(figsize=(14, 8), nrows=2) plot_lib.image_plot(fig, ax[0], ins.data_ms[:, 0, :, 0], cmap=cm.coolwarm, freq_array=ins.freq_array[0], title='MWA Soft Breather', cbar_label='Deviation ($\hat{\sigma}$)') mf.apply_match_test() plot_lib.image_plot(fig, ax[1], ins.data_ms[:, 0, :, 0], cmap=cm.coolwarm, vmin=-5, vmax=5, freq_array=ins.freq_array[0], title='MWA Soft Breather', mask_color='black', cbar_label='Deviation ($\hat{\sigma}$)') fig.savefig('%s/figs/%s_Order_0.png' % (outpath, obs)) fig2, ax2 = plt.subplots(figsize=(14, 8)) ins.data.mask = False ins.data_ms = ins.mean_subtract(order=1) mf.apply_match_test(order=1)
def execbody(ins_filepath): slash_ind = ins_filepath.rfind('/') obsid = ins_filepath[slash_ind + 1:slash_ind + 11] ins = INS(ins_filepath) ins.select(times=ins.time_array[3:-3]) ins.metric_ms = ins.mean_subtract() shape_dict = { 'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8], 'TV8': [1.88e8, 1.95e8], 'TV9': [1.95e8, 2.02e8] } sig_thresh = {shape: 5 for shape in shape_dict} sig_thresh['narrow'] = 5 sig_thresh['streak'] = 8 mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict, N_samp_thresh=len(ins.time_array) // 2) ins.metric_array[ins.metric_array == 0] = np.ma.masked ins.metric_ms = ins.mean_subtract() ins.sig_array = np.ma.copy(ins.metric_ms) #write plots if command flagged to do so if args.plots: prefix = '%s/%s_trimmed_zeromask' % (args.outdir, obsid) freqs = np.arange(1.7e8, 2e8, 5e6) xticks, xticklabels = util.make_ticks_labels(freqs, ins.freq_array, sig_fig=0) yticks = [0, 20, 40] yticklabels = [] for tick in yticks: yticklabels.append( Time(ins.time_array[tick], format='jd').iso[:-4]) Catalog_Plot.INS_plot(ins, prefix, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5, title=obsid, xlabel='Frequency (Mhz)', ylabel='Time (UTC)') if args.verbose: print("wrote trimmed zeromask plot for " + obsid) mf.apply_match_test(ins, apply_samp_thresh=False) mf.apply_samp_thresh_test(ins, event_record=True) flagged_prefix = '%s/%s_trimmed_zeromask_MF_s8' % (args.outdir, obsid) #write data/mask/match/ if command flagged to do so if args.write: ins.write(flagged_prefix, output_type='data', clobber=True) ins.write(flagged_prefix, output_type='mask', clobber=True) ins.write(flagged_prefix, output_type='match_events') if args.verbose: print("wrote data/mask/match files for " + obsid) #write plots if command flagged to do so if args.plots: Catalog_Plot.INS_plot(ins, flagged_prefix, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5, title=obsid, xlabel='Frequency (Mhz)', ylabel='Time (UTC)') if args.verbose: print("wrote trimmed zeromask (w/ match filter) for " + obsid) #a hardcoded csv generator for occ_csv if args.gencsv is not None: csv = "" + obsid + "," + flagged_prefix + "_SSINS_data.h5," + flagged_prefix + "_SSINS_mask.h5," + flagged_prefix + "_SSINS_match_events.yml\n" with open(args.gencsv, "a") as csvfile: csvfile.write(csv) print("wrote entry for " + obsid)
num_init_flag = np.sum(ins.metric_array.mask) int_time = uvd.integration_time[0] print(f"Using int_time {int_time}") num_int_flag = (args.start_flag + args.end_flag) / int_time with open(f"{DATA_PATH}/MWA_EoR_Highband_shape_dict.yml", "r") as shape_file: shape_dict = yaml.safe_load(shape_file) sig_thresh = {shape: 5 for shape in shape_dict} sig_thresh["narrow"] = 5 sig_thresh["streak"] = 10 print(f"Flagging these shapes: {shape_dict}") mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict, tb_aggro=0.4) mf.apply_match_test(ins, time_broadcast=True) occ_dict = util.calc_occ(ins, mf, num_init_flag, num_int_flag=num_int_flag, lump_narrowband=True) with open(f"{prefix}_occ.yml", "w") as occ_file: yaml.safe_dump(occ_dict, occ_file) ins.write(prefix, output_type='mask', clobber=True) print(ins.Nfreqs) print(uvf.Nfreqs) ins.write(prefix, output_type='flags', uvf=uvf, clobber=True) ins.write(prefix, output_type='match_events', clobber=True) if args.write_mwaf:
vmax=5, ylabel='Time (10 s)') fig_mf, ax_mf = plt.subplots(figsize=(16, 9), ncols=2) ins.data.mask[:, 0, :82, 0] = True ins.data.mask[:, 0, -21:, 0] = True ins.data_ms = ins.mean_subtract(order=1) mf = MF(ins, sig_thresh=5, N_thresh=0, shape_dict={ 'TV4': [1.74e8, 1.82e8], 'TV5': [1.82e8, 1.9e8], 'TV6': [1.9e8, 1.98e8] }) mf.apply_match_test(apply_N_thresh=False, order=1) plot_lib.image_plot(fig_mf, ax_mf[0], ins.data[:, 0, :, 0], freq_array=ins.freq_array[0], cbar_label='Amplitude (UNCALIB)', aspect=aspect, vmin=0, vmax=0.03, cmap=cm.viridis, mask_color='white', ylabel='Time (10 s)') plot_lib.image_plot(fig_mf, ax[1], ins.data_ms[:, 0, :, 0],
'vmin': -5, 'vmax': 5, 'mask_color': 'black' }] for i in range(2): for k, obj in zip([0, 2], [raw_ins, OR_ins]): pl.image_plot(fig, ax[k, i], getattr(obj, attr[i])[:, 0, :, 0], cbar_label=cbar_label[i], freq_array=obj.freq_array[0], **kwargs[i]) raw_ins.data[:, 0, :82] = np.ma.masked raw_ins.data[:, 0, -21:] = np.ma.masked mf = MF(raw_ins, sig_thresh=5, shape_dict=shape_dict, N_thresh=20) raw_ins.data_ms = raw_ins.mean_subtract(order=1) mf.apply_match_test(apply_N_thresh=True, order=1) raw_ins.data_ms = raw_ins.mean_subtract() for i in range(2): pl.image_plot(fig, ax[1, i], getattr(raw_ins, attr[i])[:, 0, :, 0], cbar_label=cbar_label[i], freq_array=raw_ins.freq_array[0], **kwargs[i]) fig.savefig('%s/%s_flag_comp.png' % (figpath, obs)) raw_ins.save() plt.close(fig)
if args.labels is not None: shape_dict = {label: [min, max] for (label, min, max) in zip(args.labels, args.mins, args.maxs)} else: shape_dict = {} obslist = util.make_obslist(args.obsfile) edges = [0 + 16 * i for i in range(24)] + [15 + 16 * i for i in range(24)] freqs = np.load('/Users/mike_e_dubs/python_stuff/MJW-MWA/Useful_Information/MWA_Highband_Freq_Array.npy') for i, obs in enumerate(obslist): read_paths = util.read_paths_INS(args.basedir, args.flag_choice, obs) ins = INS(read_paths=read_paths, flag_choice=args.flag_choice, obs=obs, outpath=args.outdir, order=args.order) cp.INS_plot(ins) mf = MF(ins, sig_thresh=5, shape_dict=shape_dict) mf.apply_match_test(order=args.order) cp.MF_plot(mf) if not i: occ_num = np.ma.masked_array(mf.INS.data.mask) occ_den = np.ma.masked_array(np.ones(occ_num.shape)) occ_num[:, 0, edges] = np.ma.masked occ_den[:, 0, edges] = np.ma.masked else: occ_num = occ_num + mf.INS.data.mask occ_den = occ_den + np.ones(occ_num.shape) occ_freq = occ_num.sum(axis=(0, 1, 3)) / occ_den.sum(axis=(0, 1, 3)) * 100 occ_total = occ_num.sum() / occ_den.sum() * 100 fig, ax = plt.subplots(figsize=(14, 8)) plot_lib.error_plot(fig, ax, freqs * 10**(-6), occ_freq, title='Golden Set RFI Frequency Post COTTER',
Catalog_Plot.INS_plot(ins, prefix, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5, title=args.obsid, xlabel='Frequency (Mhz)', ylabel='Time (UTC)') # Try to save memory - hope for garbage collector del ss # Set up MF flagging for routine shapes shape_dict = {'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8], 'TV8': [1.88e8, 1.95e8], 'TV9': [1.95e8, 2.02e8]} sig_thresh = {shape: 5 for shape in shape_dict} sig_thresh['narrow'] = 5 sig_thresh['streak'] = 8 mf = MF(ins.freq_array, sig_thresh, shape_dict=shape_dict, N_samp_thresh=len(ins.time_array) // 2) mf.apply_match_test(ins, apply_samp_thresh=False) mf.apply_samp_thresh_test(ins, event_record=True) Catalog_Plot.INS_plot(ins, '%s_flagged' % prefix, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, data_cmap=cm.plasma, ms_vmin=-5, ms_vmax=5, title=args.obsid, xlabel='Frequency (Mhz)', ylabel='Time (UTC)') ins.write(prefix, output_type='mask') uvd = UVData() uvd.read(args.uvd, phase_to_pointing_center=True, correct_cable_len=True) uvf = UVFlag(uvd, mode='flag', waterfall=True) uvf.flag_array = ins.mask_to_flags() utils.apply_uvflag(uvd, uvf, inplace=True) if np.any(uvd.nsample_array == 0):
basedir = '/Users/mike_e_dubs/MWA/INS/Long_Run/All' obs = '1066742016' outpath = '/Users/mike_e_dubs/General/Movie' if not os.path.exists(outpath): os.makedirs(outpath) flag_choice = 'None' read_paths = util.read_paths_construct(basedir, flag_choice, obs, 'INS') shape_dict = { 'TV6': [1.74e8, 1.81e8], 'TV7': [1.81e8, 1.88e8], 'TV8': [1.88e8, 1.95e8] } order = 0 ins = INS(obs=obs, read_paths=read_paths, outpath=outpath, order=order) mf = MF(ins, shape_dict=shape_dict, sig_thresh=5) mf.apply_match_test(order=order) ins.data.mask = False ins.data_ms = ins.mean_subtract(order=order) ins.outpath = '%s_0' % outpath labels = ['', '_ms'] titles = ['', ' (Mean-Subtracted)'] cbar_labels = [ins.vis_units, 'Deviation ($\hat{\sigma}$)'] cmaps = [cm.viridis, cm.coolwarm] for i in range(2): fig, ax = plt.subplots(figsize=(16, 9)) if i: vmin = -4 vmax = 4 else: vmin = None vmax = None