def test_comparison(beam_objs): beamlist = pyuvsim.BeamList(beam_objs) beamlist.set_str_mode() beamlist2 = pyuvsim.BeamList(beamlist._str_beam_list) assert beamlist == beamlist2 beamlist.set_obj_mode() beamlist2.set_obj_mode() assert beamlist == beamlist2
def test_jones_set_interp(cst_beam, hera_loc): # check setting the interpolation method array_location = hera_loc beam = cst_beam.copy() beam.freq_interp_kind = None beam.interpolation_function = 'az_za_simple' beam_list = pyuvsim.BeamList([beam]) antenna1 = pyuvsim.Antenna('ant1', 1, np.array([0, 10, 0]), 0) array = pyuvsim.Telescope('telescope_name', array_location, beam_list) source_altaz = np.array([[0.0], [np.pi / 4.]]) freq = 123e6 * units.Hz with pytest.raises(ValueError, match='freq_interp_kind must be set'): antenna1.get_beam_jones(array, source_altaz, freq) jones = antenna1.get_beam_jones(array, source_altaz, freq, freq_interp_kind='cubic') assert beam.freq_interp_kind == 'cubic' jones0 = antenna1.get_beam_jones(array, source_altaz, freq) jones1 = antenna1.get_beam_jones(array, source_altaz, freq, freq_interp_kind='linear') assert beam.freq_interp_kind == 'linear' jones2 = antenna1.get_beam_jones(array, source_altaz, freq) assert (np.all(jones2 == jones0) and np.all(jones1 == jones) and np.all(jones1 == jones0))
def test_object_mode(beam_objs): beams = beam_objs newbeams = copy.deepcopy(beams) beamlist = pyuvsim.BeamList(newbeams) beamlist[0].freq_interp_kind = 'cubic' uvb = copy.deepcopy(newbeams[0]) uvb.freq_interp_kind = 'quartic' # Warn if inserted object mismatches. with pytest.warns(UserWarning) as confwarn: beamlist.append(uvb) assert str(confwarn.pop().message).startswith("Conflicting settings for") assert len(beamlist) == 7 # Error if converting to string mode with mismatched keywords: with pytest.raises(ValueError, match='Conflicting settings '): beamlist.set_str_mode() beamlist._set_params_on_uvbeams(beamlist._obj_beam_list) # Error if converting to string mode without beam_paths: beamlist[0].extra_keywords.pop('beam_path') with pytest.raises(ValueError, match='Need to set '): beamlist.set_str_mode() # Insert string -- Converts to object new_anabeam = 'analytic_gaussian_sig=3.0' beamlist[-1] = new_anabeam assert isinstance(beamlist[-1], pyuvsim.AnalyticBeam) assert beamlist[-1].sigma == 3.0
def test_no_overwrite(beam_objs): # Ensure UVBeam keywords are not overwritten by BeamList.uvb_params # while in object mode. newbeams = copy.deepcopy(beam_objs) beamlist = pyuvsim.BeamList(newbeams) assert beamlist.uvb_params['freq_interp_kind'] == 'cubic' uvb = copy.deepcopy(newbeams[0]) uvb.freq_interp_kind = 'quintic' beamlist.append(uvb) assert uvb.freq_interp_kind == 'quintic' assert beamlist.uvb_params['freq_interp_kind'] == 'cubic'
def test_beamlist_errors(beam_objs): newbeams = copy.deepcopy(beam_objs) beamlist = pyuvsim.BeamList(newbeams) # Try to make a BeamList with a mixture of strings and objects. newlist = copy.deepcopy(beamlist._obj_beam_list) newlist[2] = beamlist._obj_to_str(newlist[2]) with pytest.raises(ValueError, match='Invalid beam list:'): pyuvsim.BeamList(newlist) # Try to append an invalid beam path while in object mode. beam_path = 'invalid_file.uvbeam' with pytest.raises(ValueError, match='Invalid file path'): beamlist.append(beam_path) # Compare Telescopes with beamlists of different lengths del newbeams[0] array_location = EarthLocation(lat='-30d43m17.5s', lon='21d25m41.9s', height=1073.) tel0 = pyuvsim.Telescope('tel0', array_location, newbeams) tel1 = pyuvsim.Telescope('tel1', array_location, beam_objs) assert tel0 != tel1
def test_convert_loop(beam_objs): beams = beam_objs beams[0].freq_interp_kind = 'linear' beams[1].freq_interp_kind = 'cubic' # Should warn about inconsistent params on UVBeams. with pytest.warns(UserWarning) as confwarn: beamlist = pyuvsim.BeamList(beams) assert str(confwarn.pop().message).startswith("Conflicting settings for") # Convert beams to strings: # Fail, because UVBeams are inconsistent. with pytest.raises(ValueError, match='Conflicting settings for'): beamlist.set_str_mode() beams[1].freq_interp_kind = 'linear' beamlist.set_str_mode() assert beamlist.uvb_params['freq_interp_kind'] == 'linear' for bs in beamlist: assert isinstance(bs, str) assert beamlist._obj_beam_list == [] # Convert strings to beams. Need to set additional parameters for comparison. beamlist._set_params_on_uvbeams(beams) beamlist.set_obj_mode() for bi, b in enumerate(beamlist): assert b == beams[bi] assert beamlist._str_beam_list == [] # Reset UVBeams beams[0].freq_interp_kind = None beams[1].freq_interp_kind = None
def test_string_mode(beam_objs): newbeams = copy.deepcopy(beam_objs) beamlist = pyuvsim.BeamList(newbeams) beamlist.set_str_mode() uvb = newbeams[0] uvb.freq_interp_kind = 'quartic' with pytest.raises(ValueError, match='UVBeam parameters do not'): beamlist.append(uvb) uvb.freq_interp_kind = beamlist.uvb_params['freq_interp_kind'] beamlist.append(uvb) assert isinstance(beamlist[-1], str) beamlist.set_obj_mode() # Check that parameters are set properly. try: new_pars = beamlist._scrape_uvb_params(beamlist._obj_beam_list, strict=True) assert new_pars == beamlist.uvb_params except ValueError: assert False
def test_param_reader(): pytest.importorskip('mpi4py') # Reading in various configuration files param_filename = os.path.join(SIM_DATA_PATH, "test_config", "param_10time_10chan_0.yaml") hera_uv = UVData() hera_uv.read_uvfits(triangle_uvfits_file) hera_uv.unphase_to_drift() hera_uv.telescope_name = 'HERA' time = Time(hera_uv.time_array[0], scale='utc', format='jd') sources, _ = pyuvsim.create_mock_catalog(time, arrangement='zenith', return_data=True) beam0 = UVBeam() beam0.read_beamfits(herabeam_default) beam0.extra_keywords['beam_path'] = herabeam_default beam1 = pyuvsim.AnalyticBeam('uniform') beam2 = pyuvsim.AnalyticBeam('gaussian', sigma=0.02) beam3 = pyuvsim.AnalyticBeam('airy', diameter=14.6) beam_list = pyuvsim.BeamList([beam0, beam1, beam2, beam3]) # To fill out other parameters in the UVBeam. beam_list.set_str_mode() beam_list.set_obj_mode() beam_dict = {'ANT1': 0, 'ANT2': 1, 'ANT3': 2, 'ANT4': 3} # Error conditions: params_bad = pyuvsim.simsetup._config_str_to_dict(param_filename) bak_params_bad = copy.deepcopy(params_bad) # Missing config file info params_bad['config_path'] = os.path.join( SIM_DATA_PATH, 'nonexistent_directory', 'nonexistent_file' ) with pytest.raises(ValueError, match="nonexistent_directory is not a directory"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['config_path'] = os.path.join(SIM_DATA_PATH, "test_config") params_bad['telescope']['array_layout'] = 'nonexistent_file' with pytest.raises(ValueError, match="nonexistent_file from yaml does not exist"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['telescope']['telescope_config_name'] = 'nonexistent_file' with pytest.raises(ValueError, match="telescope_config_name file from yaml does not exist"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) # Missing beam keywords params_bad = copy.deepcopy(bak_params_bad) params_bad['config_path'] = os.path.join(SIM_DATA_PATH, "test_config") params_bad = copy.deepcopy(bak_params_bad) params_bad['telescope']['telescope_config_name'] = os.path.join( SIM_DATA_PATH, 'test_config', '28m_triangle_10time_10chan_gaussnoshape.yaml' ) with pytest.raises(KeyError, match="Missing shape parameter for gaussian beam"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['telescope']['telescope_config_name'] = os.path.join( SIM_DATA_PATH, 'test_config', '28m_triangle_10time_10chan_nodiameter.yaml' ) with pytest.raises(KeyError, match="Missing diameter for airy beam."): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) params_bad['telescope']['telescope_config_name'] = os.path.join( SIM_DATA_PATH, 'test_config', '28m_triangle_10time_10chan_nofile.yaml' ) with pytest.raises(ValueError, match="Undefined beam model"): pyuvsim.simsetup.initialize_uvdata_from_params(params_bad) # Check default configuration uv_obj, new_beam_list, new_beam_dict = pyuvsim.initialize_uvdata_from_params(param_filename) new_beam_list.set_obj_mode() pyuvsim.simsetup._complete_uvdata(uv_obj, inplace=True) with open(param_filename, 'r') as fhandle: param_dict = yaml.safe_load(fhandle) expected_ofilepath = pyuvsim.utils.write_uvdata( uv_obj, param_dict, return_filename=True, dryrun=True ) assert './sim_results.uvfits' == expected_ofilepath # Spoof attributes that won't match. uv_obj.antenna_names = uv_obj.antenna_names.tolist() uv_obj.antenna_diameters = hera_uv.antenna_diameters uv_obj.history = hera_uv.history uvfits_required_extra = [ "_antenna_positions", "_gst0", "_rdate", "_earth_omega", "_dut1", "_timesys", ] for attr in uvfits_required_extra: param = getattr(uv_obj, attr) if param.value is None: param.value = param.spoof_val setattr(uv_obj, attr, param) assert new_beam_dict == beam_dict assert new_beam_list == beam_list assert uv_obj == hera_uv