def test_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.ones_like(ins.metric_array) ins.weights_array = np.copy(ins.metric_array) ins.weights_square_array = np.copy(ins.weights_array) # Make a shape dictionary for a shape that will be injected later ch_wid = ins.freq_array[1] - ins.freq_array[0] shape = [ ins.freq_array[8] - 0.2 * ch_wid, ins.freq_array[12] + 0.2 * ch_wid ] 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() t_max, f_max, shape_max, sig_max = mf.match_test(ins) assert t_max == slice(5, 6), "Wrong time" assert f_max == slice(0, ins.Nfreqs), "Wrong freq" assert shape_max == 'streak', "Wrong shape"
def test_polyfit(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile, diff=True) ins = INS(ss, order=1) # Mock some data for which the polyfit is exact x = np.arange(1, ins.Ntimes + 1) for ind in range(ins.Nfreqs): ins.metric_array[:, ind, 0] = 3 * x + 5 ins.metric_array.mask = np.zeros(ins.metric_array.shape, dtype=bool) ins.weights_array = np.ones(ins.metric_array.shape) ins.weights_square_array = np.copy(ins.weights_array) ins.metric_ms, coeffs = ins.mean_subtract(return_coeffs=True) test_coeffs = np.zeros((ins.order + 1, ) + ins.metric_ms.shape[1:]) test_coeffs[0, :] = 3 test_coeffs[1, :] = 5 assert np.all(np.allclose(ins.metric_ms, np.zeros( ins.metric_ms.shape))), "The polyfit was not exact" assert np.all(np.allclose( coeffs, test_coeffs)), "The polyfit got the wrong coefficients" ins.metric_array[:] = np.ma.masked ins.metric_ms = ins.mean_subtract() assert np.all(ins.metric_ms.mask), "The metric_ms array was not all masked"
def test_combine_ins_use_nsample(): obs = "1061313128_99bl_1pol_half_time" testfile = os.path.join(DATA_PATH, f"{obs}.uvfits") ss = SS() ss.read(testfile, diff=True) whole_ins = INS(ss, use_integration_weights=True) all_bls = ss.get_antpairs() first_50 = all_bls[:50] remaining = all_bls[50:] ss_first_50 = ss.select(bls=first_50, inplace=False) ss_remaining = ss.select(bls=remaining, inplace=False) ins_first_50 = INS(ss_first_50, use_integration_weights=True) ins_remaining = INS(ss_remaining, use_integration_weights=True) test_ins = util.combine_ins(ins_first_50, ins_remaining) # Metric arrays are off by 10^-14 (1 part in 10^16) for attr in whole_ins._data_params: assert np.all( np.isclose(getattr(whole_ins, attr), getattr( test_ins, attr))), f"{attr} is not equal between the two INS"
def test_spectrum_type_bl_init(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, f'{obs}.uvfits') ss = SS() ss.read(testfile, diff=True) ins = INS(ss) assert "Initialized spectrum_type:cross from visibility data." in ins.history with pytest.raises(ValueError, match="Requested spectrum type is 'auto', but no autos exist."): ins = INS(ss, spectrum_type="auto")
def test_select(): obs = '1061313128_99bl_1pol_half_time_SSINS' testfile = os.path.join(DATA_PATH, '%s.h5' % obs) ins = INS(testfile) Ntimes = len(ins.time_array) ins.select(times=ins.time_array[3:-3], freq_chans=np.arange(24)) assert ins.metric_array.shape[0] == Ntimes - 6 assert ins.metric_array.shape[1] == 24 for param in ins._data_params: assert getattr(ins, param).shape == ins.metric_array.shape
def test_combine_ins_errors(): obs = "1061313128_99bl_1pol_half_time" testfile = os.path.join(DATA_PATH, f"{obs}.uvfits") autofile = os.path.join(DATA_PATH, "1061312640_autos.uvfits") mixfile = os.path.join(DATA_PATH, "1061312640_mix.uvfits") ss = SS() ss.read(testfile, diff=True) all_bls = ss.get_antpairs() first_50 = all_bls[:50] remaining = all_bls[50:] ss_first_50 = ss.select(bls=first_50, inplace=False) ss_remaining = ss.select(bls=remaining, inplace=False) ins_first_50 = INS(ss_first_50, use_integration_weights=True) ins_remaining = INS(ss_remaining, use_integration_weights=True) ins_sig_arr = np.ma.copy(ins_first_50.sig_array) ins_first_50.sig_array = ins_first_50.sig_array + 1 with pytest.warns(UserWarning, match="sig_array attribute"): new_ins = util.combine_ins(ins_first_50, ins_remaining) ss_autos = SS() ss_autos.read(autofile, diff=True) ss_cross = SS() ss_cross.read(mixfile, diff=True) auto_ins = INS(ss_autos, spectrum_type="auto") cross_ins = INS(ss_cross, spectrum_type="cross") with pytest.raises(ValueError, match="ins1 is of type"): new_ins = util.combine_ins(auto_ins, cross_ins) ins_remaining.polarization_array = np.array([-6]) with pytest.raises(ValueError, match="The spectra do not have the same pols"): new_ins = util.combine_ins(ins_first_50, ins_remaining) ins_remaining.freq_array = ins_remaining.freq_array + 1 with pytest.raises(ValueError, match="The spectra do not have the same frequencies"): new_ins = util.combine_ins(ins_first_50, ins_remaining) ins_remaining.time_array = ins_remaining.time_array + 1 with pytest.raises(ValueError, match="The spectra do not have matching time"): new_ins = util.combine_ins(ins_first_50, ins_remaining)
def test_MF_write(): obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') prefix = os.path.join(DATA_PATH, f'{obs}') outfile = f"{prefix}_test_SSINS_matchfilter.yml" ins = INS(insfile) sig_thresh = 5 broadcast_dict = {"TV7": [174e6, 181e6]} shape_dict = {"TV7": [174e6, 181e6]} mf = MF(ins.freq_array, sig_thresh, broadcast_dict=broadcast_dict, broadcast_streak=True) mf.write(f"{prefix}_test", clobber=True) assert os.path.exists( outfile), "Outfile was not written or has the wrong name." with open(f"{prefix}_control_SSINS_matchfilter.yml", 'r') as control_file: control_dict = yaml.safe_load(control_file) control_dict.pop("version") test_dict = mf._make_yaml_dict() test_dict.pop("version") assert test_dict == control_dict with pytest.raises(ValueError, match="matchfilter file with prefix"): mf.write(f"{prefix}_test", clobber=False) os.remove(outfile)
def test_init(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile, flag_choice='original', diff=True) # Needs to be in time order for averaging comparison to work ss.reorder_blts(order='time') ins = INS(ss) # Mock the averaging method new_shape = [ss.Ntimes, ss.Nbls, ss.Nfreqs, ss.Npols] test_dat = np.mean(np.abs(ss.data_array).reshape(new_shape), axis=1) # Mock the weights array test_weights = np.sum(np.logical_not( ss.data_array.mask).reshape(new_shape), axis=1) # Check that the data array averaged correctly # Weights are floating-point, which introdices itty bitty errors compared to masked average. assert np.all(np.isclose(test_dat, ins.metric_array, rtol=1e-6, atol=1e-7)), "Averaging did not work as intended." # Check that the weights summed correctly assert np.all( test_weights == ins.weights_array), "Weights did not sum properly"
def make_sig_plots(outdir, dlist, elist, freqs, sig_fig, obslist): if not os.path.exists(outdir): os.makedirs(outdir) for dpath, epath, obs in zip(dlist, elist, obslist): ins = INS(dpath, match_events_file=epath) shape_sig_arr = np.ma.copy(ins.metric_ms) tcolor_arr = np.ma.zeros(ins.metric_ms.shape) Nevent = len(ins.match_events) for event_ind, event in enumerate(ins.match_events): shape_sig_arr[event[:2]] = event[-1] tcolor_arr[event[:2]] = event_ind tcolor_arr[tcolor_arr == 0] = np.ma.masked fig, ax = plt.subplots(nrows=4, ncols=2, figsize=(16, 9)) xticks, xticklabels = util.make_ticks_labels(freqs, ins.freq_array, sig_fig=0) for pol_ind in range(4): plot_lib.image_plot(fig, ax[pol_ind][0], shape_sig_arr[:, :, pol_ind], ylabel='Time (2 s)', xlabel='Frequency (Mhz)', xticks=xticks, xticklabels=xticklabels, cbar_label='Deviation ($\hat{\sigma}$)', vmin=vmin, vmax=vmax, cmap=cm.coolwarm, symlog=True, title=pol_dict[ins.polarization_array[pol_ind]]) plot_lib.image_plot(fig, ax[pol_ind][1], tcolor_arr[:, :, pol_ind], ylabel='Time (2 s)', xlabel='Frequency (Mhz)', xticks=xticks, xticklabels=xticklabels, cbar_label='Flagging Iteration', cmap=cm.viridis_r, mask_color='white', title=pol_dict[ins.polarization_array[pol_ind]]) fig.savefig('%s/%s_flag_metaplot.pdf' % (outdir, obs)) plt.close(fig)
def test_INS_plot(): 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' % (outdir, obs) outfile = '%s_SSINS.pdf' % prefix log_prefix = '%s/%s_log' % (outdir, obs) log_outfile = '%s_SSINS.pdf' % log_prefix ins = INS(insfile) 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) cp.INS_plot(ins, log_prefix, log=True, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, title='Title') assert os.path.exists(outfile), "The first plot was not made" assert os.path.exists(log_outfile), "The second plot was not made" os.remove(outfile) os.remove(log_outfile) os.rmdir(outdir)
def test_init(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile, flag_choice='original') ins = INS(ss) # Mock the averaging method new_shape = [ss.Ntimes, ss.Nbls, ss.Nfreqs, ss.Npols] test_dat = np.mean(np.abs(ss.data_array).reshape(new_shape), axis=1) # Mock the weights array test_weights = np.sum(np.logical_not( ss.data_array.mask).reshape(new_shape), axis=1) # Check that the data array averaged correctly assert np.all( test_dat == ins.metric_array), "Averaging did not work as intended." # Check that the weights summed correctly assert np.all( test_weights == ins.weights_array), "Weights did not sum properly"
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_mix_spectrum(): obs = "1061312640_mix" testfile = os.path.join(DATA_PATH, f'{obs}.uvfits') ss = SS() ss.read(testfile, diff=True) with pytest.warns(UserWarning, match="Requested spectrum type is 'cross'. Removing autos before averaging."): ins = INS(ss) with pytest.warns(UserWarning, match="Requested spectrum type is 'auto'. Removing"): ins = INS(ss, spectrum_type="auto") # Hack polarization array to check error ss.polarization_array[0] = 1 with pytest.raises(ValueError, match="SS input has pseudo-Stokes data. SSINS does not"): ins = INS(ss, spectrum_type="auto")
def test_data_params(): obs = '1061313128_99bl_1pol_half_time_SSINS' testfile = os.path.join(DATA_PATH, '%s.h5' % obs) ins = INS(testfile) test_params = ['metric_array', 'weights_array', 'weights_square_array', 'metric_ms', 'sig_array'] assert ins._data_params == test_params
def test_select(): obs = '1061313128_99bl_1pol_half_time_SSINS' testfile = os.path.join(DATA_PATH, '%s.h5' % obs) ins = INS(testfile) ins.metric_array.mask[7, :12] = True Ntimes = len(ins.time_array) ins.select(times=ins.time_array[3:-3], freq_chans=np.arange(24)) assert ins.metric_array.shape[0] == Ntimes - 6 assert ins.metric_array.shape[1] == 24 for param in ins._data_params: assert getattr(ins, param).shape == ins.metric_array.shape # Check that the mask is propagated assert np.all(ins.metric_array.mask[4, :12]) assert np.count_nonzero(ins.metric_array.mask) == 12
def test_INS_plot(): matplotlib = pytest.importorskip("matplotlib") obs = '1061313128_99bl_1pol_half_time' insfile = os.path.join(DATA_PATH, f'{obs}_SSINS.h5') outdir = os.path.join(DATA_PATH, 'test_plots') prefix = f'{outdir}/{obs}_raw' outfile = f'{prefix}_SSINS.pdf' log_prefix = f'{outdir}/{obs}_log' log_outfile = f'{log_prefix}_SSINS.pdf' symlog_prefix = f'{outdir}/{obs}_symlog' symlog_outfile = f'{symlog_prefix}_SSINS.pdf' ins = INS(insfile) 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] # Make lst straddle the 2pi boundary to trigger the warning mean_lst = np.mean(ins.lst_array) ins.lst_array += (2 * np.pi) - mean_lst ins.lst_array[ins.lst_array > (2 * np.pi)] -= 2 * np.pi with pytest.warns(UserWarning, match="LSTs appear to cross"): cp.INS_plot(ins, prefix, backend='Agg', use_extent=True, extent_time_format='lst') with pytest.warns(UserWarning, match="Plotting keyword"): cp.INS_plot(ins, log_prefix, log=True, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, title='Title') cp.INS_plot(ins, symlog_prefix, symlog=True, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, use_extent=False) assert os.path.exists(outfile), "The first plot was not made" assert os.path.exists(log_outfile), "The second plot was not made" assert os.path.exists(symlog_outfile), "The third plot was not made" os.remove(outfile) os.remove(log_outfile) os.remove(symlog_outfile) os.rmdir(outdir)
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_mask_to_flags(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile) ins = INS(ss) freq_inds_1 = np.arange(0, len(ins.freq_array), 2) freq_inds_2 = np.arange(1, len(ins.freq_array), 2) ins.metric_array[1, freq_inds_1] = np.ma.masked ins.metric_array[3, freq_inds_1] = np.ma.masked ins.metric_array[7, freq_inds_2] = np.ma.masked ins.metric_array[-2, freq_inds_2] = np.ma.masked flags = ins.mask_to_flags() test_flags = np.zeros(flags.shape, dtype=bool) test_flags[1:5, freq_inds_1] = True test_flags[7, freq_inds_2] = True test_flags[8, freq_inds_2] = True test_flags[-3:-1, freq_inds_2] = True assert np.all( flags == test_flags), "Test flags were not equal to calculated flags."
def test_no_cross_auto_spectrum(): obs = "1061312640_autos" testfile = os.path.join(DATA_PATH, f'{obs}.uvfits') ss = SS() ss.read(testfile, diff=True) with pytest.raises(ValueError, match="Requested spectrum type is 'cross', but no cross"): ins = INS(ss)
def test_N_samp_thresh_dep_error(): 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) with pytest.raises(ValueError, match="The N_samp_thresh parameter is now deprected."): mf = MF(ins.freq_array, 5, N_samp_thresh=10)
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_no_diff_start(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' # Don't diff - will fail to mask data array ss = SS() ss.read(testfile, flag_choice='original', diff=False) ins = INS(ss) assert ss.flag_choice is None
def test_mean_subtract(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile, diff=True) ins = INS(ss, order=0) old_dat = np.copy(ins.metric_ms) # Mask the first five frequencies and last two at the first and second times ins.metric_array[0, :5] = np.ma.masked # Calculate the new mean-subtracted spectrum only over the first few masked frequencies ins.metric_ms[:, :5] = ins.mean_subtract(freq_slice=slice(0, 5)) # See if a new mean was calculated over the first five frequencies assert not np.all(old_dat[1:, :5] == ins.metric_ms[1:, :5]), "All elements of the ms array are still equal"
def row_routine(obs): obsid = obs['obsid'] ins = INS(obs['ins_file'], mask_file=obs['mask_file'], match_events_file=obs['yml_file']) # if enabled, do fine channel/ time ignore for parts of the data if fine_channels_ignore is not None: freq_chans = np.arange(len(ins.freq_array)) freq_chans = np.delete(freq_chans, fine_channels_ignore) ins.select(freq_chans=freq_chans) if time_ignore is not None: times = ins.time_array times = np.delete(times, time_ignore) ins.select(times=times) # need the total occ of the obs for accepting/rejecting based on --limit flag total_occ = np.mean(ins.metric_array.mask[:, :, 0]) if total_occ >= inputargs.limit: #if inputargs.limit is passed, filter obs by total_occ obs_occ_dict = {'obsid': obsid, 'total_occ': total_occ} if inputargs.shapes is not None: for shape in inputargs.shapes: obs_occ_dict[shape] = len([ event for event in ins.match_events if event[2] == shape ]) / ins.metric_array.shape[0] occ_dict_list.append(obs_occ_dict) print("+ accepted " + obs['ins_file'] ) #prints + accepted or - rejected on left side of conout else: print("- rejected " + obs['ins_file'])
def test_spectrum_type_file_init(): obs = "1061313128_99bl_1pol_half_time_SSINS" auto_obs = "1061312640_mix_auto_SSINS_data" cross_obs = "1061312640_mix_cross_SSINS_data" testfile = os.path.join(DATA_PATH, f"{obs}.h5") auto_testfile = os.path.join(DATA_PATH, f"{auto_obs}.h5") cross_testfile = os.path.join(DATA_PATH, f"{cross_obs}.h5") ins = INS(testfile) assert ins.spectrum_type == "cross" with pytest.raises(ValueError, match="Reading in a 'cross' spectrum as 'auto'."): ins = INS(testfile, spectrum_type="auto") with pytest.raises( ValueError, match="Requested spectrum type disagrees with saved spectrum. "): ins = INS(auto_testfile, spectrum_type="cross") with pytest.raises( ValueError, match="Requested spectrum type disagrees with saved spectrum. "): ins = INS(cross_testfile, spectrum_type="auto") del ins ins = INS(cross_testfile) del ins ins = INS(auto_testfile, spectrum_type="auto")
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_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_use_integration_weights(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' ss = SS() ss.read(testfile, flag_choice='original', diff=True) ins = INS(ss, use_integration_weights=True) # These will not be equal if weights are not binary to begin with # The accuracy of return_weights_square is already checked in pyuvdata assert not np.all(ins.weights_array == ins.weights_square_array)
def test_apply_flags(): obs = '1061313128_99bl_1pol_half_time' testfile = os.path.join(DATA_PATH, '%s.uvfits' % obs) file_type = 'uvfits' insfile = os.path.join(DATA_PATH, '%s_SSINS.h5' % obs) ss = SS() ss.read(testfile) # Make sure no flags are applied to start with assert not np.any( ss.data_array.mask), "There are some flags to start with." # Apply flags, test equality, test attribute change ss.apply_flags(flag_choice='original') assert np.all( ss.flag_array == ss.data_array.mask), "Flag arrays are not equal" assert ss.flag_choice is 'original', "Flag choice attribute was not changed" # Revert flags back, test equality, test attribute change ss.apply_flags(flag_choice=None) assert not np.any(ss.data_array.mask), "Flags did not revert back back" assert ss.flag_choice is None, "Flag choice attribute did not revert back" # Make a custom flag array where everything is flagged, check application custom = np.ones_like(ss.flag_array) ss.apply_flags(flag_choice='custom', custom=custom) assert np.all(ss.data_array.mask), "The custom flag array was not applied" assert ss.flag_choice is 'custom', "The flag choice attribute was not changed" # Read an INS in (no flags by default) and flag a channel, test if it applies correctly ins = INS(insfile) ins.metric_array.mask[:, 0] = True ss.apply_flags(flag_choice='INS', INS=ins) assert np.all( ss.data_array.mask[:, 0, 0]), "Not all of the 0th channel was flagged." assert not np.any(ss.data_array.mask[:, 0, 1:] ), "Some of the channels other than the 0th were flagged" assert ss.flag_choice is 'INS' # Make flag_choice custom but do not provide array - should keep old flags ss.apply_flags(flag_choice='custom', custom=None) assert not np.any(ss.data_array.mask ), "Some of the channels other than the 0th were flagged" assert ss.flag_choice is None with pytest.raises(ValueError): ss.apply_flags(flag_choice='bad_choice')
def test_INS_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_raw' % (outdir, obs) outfile = '%s_SSINS.pdf' % prefix log_prefix = '%s/%s_log' % (outdir, obs) log_outfile = '%s_SSINS.pdf' % log_prefix symlog_prefix = '%s/%s_symlog' % (outdir, obs) symlog_outfile = '%s_SSINS.pdf' % symlog_prefix ins = INS(insfile) 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, backend='Agg') cp.INS_plot(ins, log_prefix, log=True, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, title='Title') cp.INS_plot(ins, symlog_prefix, symlog=True, xticks=xticks, yticks=yticks, xticklabels=xticklabels, yticklabels=yticklabels, use_extent=False) assert os.path.exists(outfile), "The first plot was not made" assert os.path.exists(log_outfile), "The second plot was not made" assert os.path.exists(symlog_outfile), "The third plot was not made" os.remove(outfile) os.remove(log_outfile) os.remove(symlog_outfile) os.rmdir(outdir)