def test_2d_rfi_removal(cal_data, tmpdir, caplog): cache = tmpdir / "cal-coeff-cache-new" calobs = cc.CalibrationObservation( cal_data, load_kwargs={ "rfi_removal": "2D", "cache_dir": cache, "rfi_kernel_width_time": 2, "rfi_kernel_width_freq": 2, }, compile_from_def=False, ) assert calobs.ambient.spectrum.averaged_Q.ndim == 1 assert calobs.ambient.spectrum.averaged_p0.ndim == 1 assert calobs.ambient.spectrum.averaged_p1.ndim == 1 assert calobs.ambient.spectrum.averaged_p2.ndim == 1 assert calobs.ambient.spectrum.variance_Q.ndim == 1 assert calobs.ambient.spectrum.variance_p0.ndim == 1 assert calobs.ambient.spectrum.variance_p1.ndim == 1 assert calobs.ambient.spectrum.variance_p2.ndim == 1 assert calobs.ambient.spectrum.variance_spectrum.ndim == 1 assert isinstance(calobs.ambient.spectrum.ancillary, list) assert isinstance(calobs.ambient.spectrum.ancillary[0], dict)
def test_calibration_init(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation(cal_data, load_kwargs={"cache_dir": cache}, cterms=5, compile_from_def=False) calobs.write(tmpdir / "calfile.h5") cal = cc.Calibration(tmpdir / "calfile.h5") assert np.allclose(cal.lna_s11(), calobs.lna.s11_model(calobs.freq.freq)) assert np.allclose(cal.C1(), calobs.C1()) assert np.allclose(cal.C2(), calobs.C2()) assert np.allclose(cal.Tunc(), calobs.Tunc()) assert np.allclose(cal.Tcos(), calobs.Tcos()) assert np.allclose(cal.Tsin(), calobs.Tsin()) temp = calobs.ambient.averaged_spectrum s11 = calobs.ambient.reflections.s11_model(calobs.freq.freq) cal_temp = cal.calibrate_temp(calobs.freq.freq, temp, s11) mask = ~np.isnan(cal_temp) assert np.allclose(cal_temp[mask], calobs.calibrate("ambient")[mask]) mask = ~np.isnan(temp) assert np.allclose( cal.decalibrate_temp(calobs.freq.freq, cal_temp, s11)[mask], temp[mask]) with h5py.File(tmpdir / "calfile.h5", "a") as fl: fl.attrs["switch_path"] = "/doesnt/exist" cal = cc.Calibration(tmpdir / "calfile.h5") assert cal.internal_switch is None
def test_new_load(cal_data: Path): calobs = cc.CalibrationObservation(cal_data, compile_from_def=False) new_open = calobs.new_load("open", spec_kwargs={"ignore_times_percent": 50.0}) assert len(new_open.spectrum.thermistor_temp) < len( calobs.open.spectrum.thermistor_temp)
def run(config, path, out, cache_dir, plot, simulators): """Calibrate using lab measurements in PATH, and make all relevant plots.""" out = Path(out) with open(config, "r") as fl: settings = yaml.load(fl, Loader=yaml.FullLoader) if cache_dir != ".": settings.update(load_kwargs={"cache_dir": cache_dir}) obs = cc.CalibrationObservation(path=path, **settings) if plot: # Plot Calibrator properties fig = obs.plot_raw_spectra() fig.savefig(out / "raw_spectra.png") figs = obs.plot_s11_models() for kind, fig in figs.items(): fig.savefig(out / f"{kind}_s11_model.png") fig = obs.plot_calibrated_temps(bins=256) fig.savefig(out / "calibrated_temps.png") fig = obs.plot_coefficients() fig.savefig(out / "calibration_coefficients.png") # Calibrate and plot antsim for name in simulators: antsim = obs.new_load(load_name=name) fig = obs.plot_calibrated_temp(antsim, bins=256) fig.savefig(out / f"{name}_calibrated_temp.png") # Write out data obs.write(out / obs.path.parent.name)
def sweep( config, path, max_cterms, max_wterms, repeats, runs, delta_rms_thresh, out, cache_dir, ): """Perform a sweep of number of terms to obtain the best parameter set.""" with open(config, "r") as fl: settings = yaml.load(fl, Loader=yaml.FullLoader) if cache_dir != ".": settings.update(cache_dir=cache_dir) obs = cc.CalibrationObservation(path=path, **settings) cc.perform_term_sweep( obs, direc=out, verbose=True, max_cterms=max_cterms, max_wterms=max_wterms, explore_repeat_nums=repeats, explore_run_nums=runs, delta_rms_thresh=delta_rms_thresh, )
def test_1d_rfi_removal(cal_data, tmpdir, caplog): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation( cal_data, load_kwargs={ "rfi_removal": "1D", "cache_dir": cache }, compile_from_def=False, include_previous=False, ) assert calobs.ambient.spectrum.averaged_Q.ndim == 1 assert calobs.ambient.spectrum.averaged_p0.ndim == 1 assert calobs.ambient.spectrum.averaged_p1.ndim == 1 assert calobs.ambient.spectrum.averaged_p2.ndim == 1 assert calobs.ambient.spectrum.variance_Q.ndim == 1 assert calobs.ambient.spectrum.variance_p0.ndim == 1 assert calobs.ambient.spectrum.variance_p1.ndim == 1 assert calobs.ambient.spectrum.variance_p2.ndim == 1 assert calobs.ambient.spectrum.variance_spectrum.ndim == 1 assert isinstance(calobs.ambient.spectrum.ancillary, list) assert isinstance(calobs.ambient.spectrum.ancillary[0], dict) calobs2 = cc.CalibrationObservation( cal_data, load_kwargs={ "rfi_removal": "1D", "cache_dir": cache }, compile_from_def=False, include_previous=False, ) print(list(cache.glob("*"))) # Access an averaged quantity caplog.set_level(logging.INFO) prev_level = cc.logger.getEffectiveLevel() cc.logger.setLevel(logging.INFO) assert np.allclose(calobs.ambient.spectrum.averaged_Q, calobs2.ambient.spectrum.averaged_Q) assert "Reading in previously-created integrated ambient" in caplog.text cc.logger.setLevel(prev_level)
def test_rms(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation(cal_data, load_kwargs={"cache_dir": cache}, compile_from_def=False) rms = calobs.get_rms() assert isinstance(rms, dict) assert isinstance(rms["ambient"], float)
def test_write_coefficients(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation(cal_data, load_kwargs={"cache_dir": cache}, compile_from_def=False) calobs.write_coefficients(tmpdir) assert any( path.name.startswith("calibration_parameters") for path in tmpdir.glob("*"))
def test_update(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation(cal_data, load_kwargs={"cache_dir": cache}, wterms=5, compile_from_def=False) assert len(calobs.Tcos_poly) == 4 calobs.update(wterms=7) assert len(calobs.Tcos_poly) == 6
def test_load_resids(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation(cal_data, load_kwargs={"cache_dir": cache}, compile_from_def=False) cal = calobs.calibrate("ambient") out = calobs.get_load_residuals() mask = ~np.isnan(cal) assert np.allclose(out["ambient"][mask], cal[mask] - calobs.ambient.temp_ave)
def test_cal_uncal_round_trip(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation(cal_data, load_kwargs={"cache_dir": cache}, compile_from_def=False) tcal = calobs.calibrate("ambient") raw = calobs.decalibrate(tcal, "ambient") mask = ~np.isnan(raw) assert np.allclose(raw[mask], calobs.ambient.averaged_spectrum[mask]) with pytest.warns(UserWarning): calobs.decalibrate(tcal, "ambient", freq=np.linspace(30, 120, 50))
def test_term_sweep(cal_data: Path, tmpdir: Path): cache = tmpdir / "cal-coeff-cache" calobs = cc.CalibrationObservation( cal_data, load_kwargs={"cache_dir": cache}, cterms=5, wterms=7, f_low=60, f_high=80, compile_from_def=False, ) calobs_opt = cc.perform_term_sweep( calobs, max_cterms=6, max_wterms=8, explore_run_nums=True, explore_repeat_nums=True, direc=tmpdir, ) assert isinstance(calobs_opt, cc.CalibrationObservation)
def test_bad_fminmax(cal_data: Path): with pytest.raises(ValueError): cc.CalibrationObservation(cal_data, f_low=100, f_high=50)
def test_calobs_bad_input(cal_data: Path): with pytest.raises(TypeError): cc.CalibrationObservation(cal_data, load_spectra={"ambient": "derp"}) with pytest.raises(ValueError): cc.CalibrationObservation(cal_data, f_low=100, f_high=40)
from os import path from edges_cal import cal_coefficients as cc dataIn = "ExampleData/25C" dataOut = "output" obs = cc.CalibrationObservation( path=dataIn, correction_path=dataIn, f_low=50, f_high=190, run_num=2, ignore_times_percent=5, resistance_f=50.0002, resistance_m=50.166, cterms=11, wterms=12, ) obs.plot_calibrated_temps(bins=40) antsim = cc.LoadSpectrum( "antsim", dataIn, f_low=50, f_high=190, run_num=2, ignore_times_percent=5 ) obs.plot_calibrated_temp(antsim) obs.write_coefficients() obs.plot_coefficients() obs.write()