def test_add_baseline(): uv1 = UVFlag(test_f_file) uv2 = copy.deepcopy(uv1) uv2.baseline_array += 100 # Arbitrary uv3 = uv1.__add__(uv2, axis='baseline') assert np.array_equal(np.concatenate((uv1.time_array, uv2.time_array)), uv3.time_array) assert np.array_equal( np.concatenate((uv1.baseline_array, uv2.baseline_array)), uv3.baseline_array) assert np.array_equal(np.concatenate((uv1.ant_1_array, uv2.ant_1_array)), uv3.ant_1_array) assert np.array_equal(np.concatenate((uv1.ant_2_array, uv2.ant_2_array)), uv3.ant_2_array) assert np.array_equal(np.concatenate((uv1.lst_array, uv2.lst_array)), uv3.lst_array) assert np.array_equal( np.concatenate((uv1.metric_array, uv2.metric_array), axis=0), uv3.metric_array) assert np.array_equal( np.concatenate((uv1.weights_array, uv2.weights_array), axis=0), uv3.weights_array) assert np.array_equal(uv1.freq_array, uv3.freq_array) assert uv3.type == 'baseline' assert uv3.mode == 'metric' assert np.array_equal(uv1.polarization_array, uv3.polarization_array) assert 'Data combined along baseline axis with ' + pyuvdata_version_str in uv3.history
def test_antenna_to_antenna(): uvc = UVCal() uvc.read_calfits(test_c_file) uvf = UVFlag(uvc) uvf2 = uvf.copy() uvf.to_antenna(uvc) assert uvf == uvf2
def test_inplace_add(): uv1a = UVFlag(test_f_file) uv1b = copy.deepcopy(uv1a) uv2 = copy.deepcopy(uv1a) uv2.time_array += 1 uv1a += uv2 assert uv1a.__eq__(uv1b + uv2)
def test_baseline_to_baseline(): uv = UVData() uv.read_miriad(test_d_file) uvf = UVFlag(uv) uvf2 = uvf.copy() uvf.to_baseline(uv) assert uvf == uvf2
def test_to_waterfall_waterfall(): uvf = UVFlag(test_f_file) uvf.weights_array = np.ones_like(uvf.weights_array) uvf.to_waterfall() uvtest.checkWarnings(uvf.to_waterfall, [], {}, nwarnings=1, message='This object is already a waterfall')
def test_to_flag(): uvf = UVFlag(test_f_file) uvf.to_flag() assert hasattr(uvf, 'flag_array') assert not hasattr(uvf, 'metric_array') assert uvf.mode == 'flag' assert 'Converted to mode "flag"' in uvf.history
def test_copy(): uvf = UVFlag(test_f_file) uvf2 = uvf.copy() assert uvf == uvf2 # Make sure it's a copy and not just pointing to same object uvf.to_waterfall() assert uvf != uvf2
def test_init_list(): uv = UVData() uv.read_miriad(test_d_file) uv.time_array -= 1 uvf = UVFlag([uv, test_f_file]) uvf1 = UVFlag(uv) uvf2 = UVFlag(test_f_file) assert np.array_equal( np.concatenate((uvf1.metric_array, uvf2.metric_array), axis=0), uvf.metric_array) assert np.array_equal( np.concatenate((uvf1.weights_array, uvf2.weights_array), axis=0), uvf.weights_array) assert np.array_equal(np.concatenate((uvf1.time_array, uvf2.time_array)), uvf.time_array) assert np.array_equal( np.concatenate((uvf1.baseline_array, uvf2.baseline_array)), uvf.baseline_array) assert np.array_equal(np.concatenate((uvf1.ant_1_array, uvf2.ant_1_array)), uvf.ant_1_array) assert np.array_equal(np.concatenate((uvf1.ant_2_array, uvf2.ant_2_array)), uvf.ant_2_array) assert uvf.mode == 'metric' assert np.all(uvf.freq_array == uv.freq_array[0]) assert np.all(uvf.polarization_array == uv.polarization_array)
def test_get_antpairs(): uvf = UVFlag(test_f_file) antpairs = uvf.get_antpairs() for a1, a2 in antpairs: ind = np.where((uvf.ant_1_array == a1) & (uvf.ant_2_array == a2))[0] assert len(ind) > 0 for a1, a2 in zip(uvf.ant_1_array, uvf.ant_2_array): assert (a1, a2) in antpairs
def test_to_waterfall_bl(): uvf = UVFlag(test_f_file) uvf.weights_array = np.ones_like(uvf.weights_array) uvf.to_waterfall() assert uvf.type == 'waterfall' assert uvf.metric_array.shape == (len(uvf.time_array), len(uvf.freq_array), len(uvf.polarization_array)) assert uvf.weights_array.shape == uvf.metric_array.shape
def test_collapse_single_pol(): uvf = UVFlag(test_f_file) uvf.weights_array = np.ones_like(uvf.weights_array) uvf2 = uvf.copy() uvtest.checkWarnings(uvf.collapse_pol, [], {}, nwarnings=1, message='Cannot collapse polarization') assert uvf == uvf2
def test_to_metric_waterfall(): uvf = UVFlag(test_f_file) uvf.to_waterfall() uvf.to_flag() uvf.flag_array[:, 10] = True uvf.flag_array[1, :, :] = True uvf.to_metric(convert_wgts=True) assert np.isclose(uvf.weights_array[1], 0.0).all() assert np.isclose(uvf.weights_array[:, 10], 0.0).all()
def test_or_add_history(): uvf = UVFlag(test_f_file) uvf.to_flag() uvf2 = uvf.copy() uvf2.history = 'Different history' uvf3 = uvf | uvf2 assert uvf.history in uvf3.history assert uvf2.history in uvf3.history assert "Flags OR'd with:" in uvf3.history
def test_combine_metrics_wrong_shape(): uvc = UVCal() uvc.read_calfits(test_c_file) uvf = UVFlag(uvc) np.random.seed(44) uvf.metric_array = np.random.normal(size=uvf.metric_array.shape) uvf2 = uvf.copy() uvf2.to_waterfall() pytest.raises(ValueError, uvf.combine_metrics, uvf2)
def test_uvflag_compatibility(self): # Test that UVFlag is able to successfully init from the HERAData object uv = UVData() uv.read_uvh5(self.uvh5_1) uvf1 = UVFlag(uv) hd = HERAData(self.uvh5_1) hd.read() uvf2 = UVFlag(hd) assert uvf1 == uvf2
def test_to_waterfall_ant(): uvc = UVCal() uvc.read_calfits(test_c_file) uvf = UVFlag(uvc) uvf.weights_array = np.ones_like(uvf.weights_array) uvf.to_waterfall() assert uvf.type == 'waterfall' assert uvf.metric_array.shape == (len(uvf.time_array), len(uvf.freq_array), len(uvf.polarization_array)) assert uvf.weights_array.shape == uvf.metric_array.shape assert len(uvf.lst_array) == len(uvf.time_array)
def test_to_antenna_flags(): uvc = UVCal() uvc.read_calfits(test_c_file) uvf = UVFlag(uvc) uvf.to_waterfall() uvf.to_flag() uvf.flag_array[0, 10, 0] = True # Flag time0, chan10 uvf.flag_array[1, 15, 0] = True # Flag time1, chan15 uvf.to_antenna(uvc) assert uvf.type == 'antenna' assert np.all(uvf.ant_array == uvc.ant_array) assert np.all(uvf.time_array == uvc.time_array) assert np.all(uvf.flag_array[:, 0, 10, 0, 0]) assert np.all(uvf.flag_array[:, 0, 15, 1, 0]) assert uvf.flag_array.mean() == 2. * uvc.Nants_data / uvf.flag_array.size
def test_missing_Nants_telescope(): import h5py testfile = os.path.join(DATA_PATH, 'test_missing_Nants.h5') shutil.copyfile(test_f_file, testfile) with h5py.File(testfile, 'r+') as f: del (f['/Header/Nants_telescope']) uvf = uvtest.checkWarnings(UVFlag, [testfile], {}, nwarnings=1, message='Nants_telescope not availabe') uvf2 = UVFlag(test_f_file) uvf2.Nants_telescope = None assert uvf == uvf2 os.remove(testfile)
def test_add_flag(): uv = UVData() uv.read_miriad(test_d_file) uv1 = UVFlag(uv, mode='flag') uv2 = copy.deepcopy(uv1) uv2.time_array += 1 # Add a day uv3 = uv1 + uv2 assert np.array_equal(np.concatenate((uv1.time_array, uv2.time_array)), uv3.time_array) assert np.array_equal( np.concatenate((uv1.baseline_array, uv2.baseline_array)), uv3.baseline_array) assert np.array_equal(np.concatenate((uv1.ant_1_array, uv2.ant_1_array)), uv3.ant_1_array) assert np.array_equal(np.concatenate((uv1.ant_2_array, uv2.ant_2_array)), uv3.ant_2_array) assert np.array_equal(np.concatenate((uv1.lst_array, uv2.lst_array)), uv3.lst_array) assert np.array_equal( np.concatenate((uv1.flag_array, uv2.flag_array), axis=0), uv3.flag_array) assert np.array_equal( np.concatenate((uv1.weights_array, uv2.weights_array), axis=0), uv3.weights_array) assert np.array_equal(uv1.freq_array, uv3.freq_array) assert uv3.type == 'baseline' assert uv3.mode == 'flag' assert np.array_equal(uv1.polarization_array, uv3.polarization_array) assert 'Data combined along time axis with ' + pyuvdata_version_str in uv3.history
def test_add_errors(): uv = UVData() uv.read_miriad(test_d_file) uvc = UVCal() uvc.read_calfits(test_c_file) uv1 = UVFlag(uv) # Mismatched classes pytest.raises(ValueError, uv1.__add__, 3) # Mismatched types uv2 = UVFlag(uvc) pytest.raises(ValueError, uv1.__add__, uv2) # Mismatched modes uv3 = UVFlag(uv, mode='flag') pytest.raises(ValueError, uv1.__add__, uv3) # Invalid axes pytest.raises(ValueError, uv1.__add__, uv1, axis='antenna') pytest.raises(ValueError, uv2.__add__, uv2, axis='baseline')
def __init__(self, input, history='', label='', order=0, mask_file=None, match_events_file=None): """ init function for the INS class. Args: input: See UVFlag documentation history: See UVFlag documentation label: See UVFlag documentation order: Sets the order parameter for the INS object mask_file: A path to an .h5 (UVFlag) file that contains a mask for the metric_array match_events_file: A path to a .yml file that has events caught by the match filter """ super(INS, self).__init__(input, mode='metric', copy_flags=False, waterfall=False, history='', label='') if self.type is 'baseline': # Manually flag autos input.data_array[input.ant_1_array == input.ant_2_array] = np.ma.masked self.metric_array = np.abs(input.data_array) """The baseline-averaged sky-subtracted visibility amplitudes (numpy masked array)""" self.weights_array = np.logical_not(input.data_array.mask) """The number of baselines that contributed to each element of the metric_array""" super(INS, self).to_waterfall(method='mean') if not hasattr(self.metric_array, 'mask'): self.metric_array = np.ma.masked_array(self.metric_array) if mask_file is None: # Only mask elements initially if no baselines contributed self.metric_array.mask = self.weights_array == 0 else: # Read in the flag array flag_uvf = UVFlag(mask_file) self.metric_array.mask = np.copy(flag_uvf.flag_array) del flag_uvf if match_events_file is None: self.match_events = [] """A list of tuples that contain information about events caught during match filtering""" else: self.match_events = self.match_events_read(match_events_file) self.order = order """The order of polynomial fit for each frequency channel during mean-subtraction. Default is 0, which just calculates the mean.""" self.metric_ms = self.mean_subtract() """The incoherent noise spectrum, after mean-subtraction."""
def test_collapse_pol_flag(): uvf = UVFlag(test_f_file) uvf.to_flag() uvf.weights_array = np.ones_like(uvf.weights_array) uvf2 = uvf.copy() uvf2.polarization_array[0] = -4 uvf.__add__(uvf2, inplace=True, axis='pol') # Concatenate to form multi-pol object uvf2 = uvf.copy() uvf2.collapse_pol() assert len(uvf2.polarization_array) == 1 assert uvf2.polarization_array[0] == np.string_(','.join( map(str, uvf.polarization_array))) assert uvf2.mode == 'metric' assert hasattr(uvf2, 'metric_array') assert not hasattr(uvf2, 'flag_array')
def test_read_change_mode(): uv = UVData() uv.read_miriad(test_d_file) uvf = UVFlag(uv, mode='flag') assert hasattr(uvf, 'flag_array') assert not hasattr(uvf, 'metric_array') uvf.write(test_outfile, clobber=True) uvf.read(test_f_file) assert hasattr(uvf, 'metric_array') assert not hasattr(uvf, 'flag_array') uvf.read(test_outfile) assert hasattr(uvf, 'flag_array') assert not hasattr(uvf, 'metric_array')
def test_to_antenna_metric_force_pol(): uvc = UVCal() uvc.read_calfits(test_c_file) uvc.select(jones=-5) uvf = UVFlag(uvc) uvf.to_waterfall() uvf.metric_array[0, 10, 0] = 3.2 # Fill in time0, chan10 uvf.metric_array[1, 15, 0] = 2.1 # Fill in time1, chan15 uvf.polarization_array[0] = -4 uvf.to_antenna(uvc, force_pol=True) assert np.all(uvf.ant_array == uvc.ant_array) assert np.all(uvf.time_array == uvc.time_array) assert np.array_equal(uvf.polarization_array, uvc.jones_array) assert np.all(uvf.metric_array[:, 0, 10, 0, 0] == 3.2) assert np.all(uvf.metric_array[:, 0, 15, 1, 0] == 2.1) assert np.isclose(uvf.metric_array.mean(), (3.2 + 2.1) * uvc.Nants_data / uvf.metric_array.size)
def test_to_baseline_flags(): uv = UVData() uv.read_miriad(test_d_file) uvf = UVFlag(uv) uvf.to_waterfall() uvf.to_flag() uvf.flag_array[0, 10, 0] = True # Flag time0, chan10 uvf.flag_array[1, 15, 0] = True # Flag time1, chan15 uvf.to_baseline(uv) assert uvf.type == 'baseline' assert np.all(uvf.baseline_array == uv.baseline_array) assert np.all(uvf.time_array == uv.time_array) times = np.unique(uvf.time_array) ntrue = 0.0 ind = np.where(uvf.time_array == times[0])[0] ntrue += len(ind) assert np.all(uvf.flag_array[ind, 0, 10, 0]) ind = np.where(uvf.time_array == times[1])[0] ntrue += len(ind) assert np.all(uvf.flag_array[ind, 0, 15, 0]) assert uvf.flag_array.mean() == ntrue / uvf.flag_array.size
def test_to_antenna_metric(): uvc = UVCal() uvc.read_calfits(test_c_file) uvf = UVFlag(uvc) uvf.to_waterfall() uvf.metric_array[0, 10, 0] = 3.2 # Fill in time0, chan10 uvf.metric_array[1, 15, 0] = 2.1 # Fill in time1, chan15 uvf.to_antenna(uvc) assert np.all(uvf.ant_array == uvc.ant_array) assert np.all(uvf.time_array == uvc.time_array) assert np.all(uvf.metric_array[:, 0, 10, 0, 0] == 3.2) assert np.all(uvf.metric_array[:, 0, 15, 1, 0] == 2.1) assert np.isclose(uvf.metric_array.mean(), (3.2 + 2.1) * uvc.Nants_data / uvf.metric_array.size)
def test_add_antenna(): uvc = UVCal() uvc.read_calfits(test_c_file) uv1 = UVFlag(uvc) uv2 = copy.deepcopy(uv1) uv2.ant_array += 100 # Arbitrary uv3 = uv1.__add__(uv2, axis='antenna') assert np.array_equal(np.concatenate((uv1.ant_array, uv2.ant_array)), uv3.ant_array) assert np.array_equal( np.concatenate((uv1.metric_array, uv2.metric_array), axis=0), uv3.metric_array) assert np.array_equal( np.concatenate((uv1.weights_array, uv2.weights_array), axis=0), uv3.weights_array) assert np.array_equal(uv1.freq_array, uv3.freq_array) assert np.array_equal(uv1.time_array, uv3.time_array) assert np.array_equal(uv1.lst_array, uv3.lst_array) assert uv3.type == 'antenna' assert uv3.mode == 'metric' assert np.array_equal(uv1.polarization_array, uv3.polarization_array) assert 'Data combined along antenna axis with ' + pyuvdata_version_str in uv3.history
def test_to_metric_baseline(): uvf = UVFlag(test_f_file) uvf.to_flag() uvf.flag_array[:, :, 10] = True uvf.flag_array[1, :, :] = True assert hasattr(uvf, 'flag_array') assert not hasattr(uvf, 'metric_array') assert uvf.mode == 'flag' uvf.to_metric(convert_wgts=True) assert hasattr(uvf, 'metric_array') assert not hasattr(uvf, 'flag_array') assert uvf.mode == 'metric' assert 'Converted to mode "metric"' in uvf.history assert np.isclose(uvf.weights_array[1], 0.0).all() assert np.isclose(uvf.weights_array[:, :, 10], 0.0).all()
def write_flagparams_rst(write_file=None): test_file = os.path.join(DATA_PATH, "zen.2457698.40355.xx.HH.uvcAA.testuvflag.h5") UV = UVFlag(test_file) out = "UVFlag Parameters\n=================\n" out += ( "These are the standard attributes of UVFlag objects.\n\nUnder the hood " "they are actually properties based on UVParameter objects.\n\n") out += "Required\n----------------\n" out += ( "These parameters are required to have a sensible UVFlag object and \n" "are required for most kinds of uv data files.") out += "\n\n" for thing in UV.required(): obj = getattr(UV, thing) out += "**{name}**\n".format(name=obj.name) out += " {desc}\n".format(desc=obj.description) out += "\n" out += "Optional\n----------------\n" out += ("These parameters are defined by one or more type but are not " "always required.\nSome of them are required depending on the " "type (as noted below).") out += "\n\n" for thing in UV.extra(): obj = getattr(UV, thing) out += "**{name}**\n".format(name=obj.name) out += " {desc}\n".format(desc=obj.description) out += "\n" t = Time.now() t.out_subfmt = "date" out += "last updated: {date}".format(date=t.iso) if write_file is None: write_path = os.path.dirname(os.path.abspath(inspect.stack()[0][1])) write_file = os.path.join(write_path, "uvflag_parameters.rst") F = open(write_file, "w") F.write(out) print("wrote " + write_file)
def test_to_metric_antenna(): uvc = UVCal() uvc.read_calfits(test_c_file) uvf = UVFlag(uvc, mode='flag') uvf.flag_array[10, :, :, 1, :] = True uvf.flag_array[15, :, 3, :, :] = True uvf.to_metric(convert_wgts=True) assert np.isclose(uvf.weights_array[10, :, :, 1, :], 0.0).all() assert np.isclose(uvf.weights_array[15, :, 3, :, :], 0.0).all()