def test_write_calibration(self): path = tempfile.mkdtemp() file_fmt = "test{date}.txt" params = Struct(output_path=path, calibration_file_fmt=file_fmt) date = datetime(year=2015, month=1, day=1) calibration = { date.strftime('%Y-%m-%d'): [ScheduleEntry(date, 0., 1., 'Calibration: Test')] } ctx = Struct(params=params, calibration=calibration) plugin = write_calibration.Plugin(ctx) plugin() cal_path = os.path.join(params.output_path, date.strftime('%Y'), date.strftime('%m'), date.strftime('%d'), "test20150101.txt") with open(cal_path, 'r') as f: lines = list(f) assert len(lines) == 4 cal = lines[-1].split(',') assert cal[0] == date.strftime('%H:%M:%S') assert np.isclose(float(cal[1]), 0.) assert np.isclose(float(cal[2]), np.degrees(1.)) assert cal[3] == 'Calibration: Test\n'
def test_scheduler(): # constant elevation model params = Struct(scheduler_file=os.path.join(ROOT_PATH, "schedule.txt"), strategy_step_size=60, time_range=15 * 60, telescope_latitude=47.344192, telescope_longitude=8.114368) strategy_start = parse_datetime("2015-12-21-02:00:00") strategy_end = parse_datetime("2015-12-22-09:00:00") ctx = Struct(params=params, strategy_start=strategy_start, strategy_end=strategy_end) strategy = scheduler.load_strategy(ctx) assert len(ctx.calibration["2015-12-22"]) == 1 assert len(strategy) == int( (strategy_end - strategy_start).total_seconds()) / 60 assert strategy[0].az == np.radians(200.0) assert strategy[-1].az == np.radians(56.1684772811) assert strategy[0].alt == np.radians(36.0) assert strategy[-1].alt == np.radians(25.0529699511)
def test_transform(self): nside = 2**4 params = Struct(beam_nside=nside) strategy_coord = CoordSpec(0, 0, 0, 0, 0) beam_size = 0.041 angles = np.arange(-1, 1, 10) * beam_size / 2 beam_spec = BeamSpec(angles, angles, 0) time_steps = 2 idx = np.arange(hp.nside2npix(nside)) thetas, phis = hp.pix2ang(nside, idx) tree = sphere.ArcKDTree(thetas, phis) ctx = Struct( params=params, strategy_coords=[strategy_coord for _ in range(time_steps)], beam_spec=beam_spec, arc_tree=tree) plugin = coord_transform.Plugin(ctx) plugin() assert ctx.beams is not None assert len(ctx.beams) == time_steps
def test_M9703A(): frequencies = np.arange(980, 1300) tod_vx = np.ones((len(frequencies), 100)) # constant elevation model params = Struct(elevation_model = [1]) ctx = Struct(params=params, tod_vx=tod_vx, frequencies = frequencies) gain_ = np.linspace(0, 10, len(ctx.frequencies)) with patch("numpy.loadtxt") as load_txt_mock: load_txt_mock.return_value = np.vstack([ctx.frequencies, gain_, 2*gain_, 3*gain_]).T gain = M9703A.get_gain(frequencies) bg = M9703A.get_background(frequencies, params.elevation_model)(0) wn_scale, cn_amp, cn_beta = M9703A.get_noise_params(frequencies) frac, amp = M9703A.get_rfi_params(frequencies) assert np.all(gain_ == gain) assert np.all(bg == gain.reshape(-1,1)) assert np.all(wn_scale == gain) assert np.all(cn_amp == 2*gain) assert np.all(cn_beta == 3*gain) assert np.all(frac == gain) assert np.all(amp == 2*gain)
def test_add_rfi(self): tod = np.zeros((300, 3600)) params = Struct( strategy_step_size=1, #bursts max_rfi_count=20, coeff_freq=[0.179, 1.191], coeff_time=[0.144, -1.754, 63.035], sigma_range=3.0, amp_scale=3, amp_loc=0, #constant rfi_freqs=[25, 120], min_amp=1., max_amp=5., rfi_width=8, ) ctx = Struct(params=params, tod_vx=tod, tod_vy=tod.copy()) plugin = add_rfi.Plugin(ctx) plugin() assert np.sum(ctx.tod_vx - ctx.tod_vy) != 0
def test_map_file_path(self): current_path = os.path.dirname(__file__) params = Struct( data_file_prefix="SKYMAP_", data_file_suffix=".hdf", file_date_format="{0}%Y-%m-%d-%H:%M:%S{1}", chunk_size=1, ) full_path = os.path.join(current_path, NAME) ctx = Struct( params=params, data_file_paths=[ [full_path], # day 1 [full_path] # day 2 ]) plugin = map_file_paths.Plugin(ctx) for idx, ctx in enumerate(plugin.getWorkload()): assert ctx is not None assert ctx.file_paths[0] == full_path assert idx == 1 ctx.data_file_paths = [ [full_path, full_path], # day 1 [full_path] ] # day 2 ctx.params.chunk_size = 2 plugin = map_file_paths.Plugin(ctx) ctxs = list(plugin.getWorkload()) assert len(ctxs[0].file_paths) == 2 assert len(ctxs[1].file_paths) == 1
def test_load_signal_normal(self): params = Struct(beam_nside=static_gsm.GSM_NSIDE) ctx = Struct(params=params) astro_signal = static_gsm.load_signal(ctx) assert astro_signal is not None assert hp.get_nside(astro_signal) == ctx.params.beam_nside
def test_scheduler(): # constant elevation model params = Struct(scheduler_file=os.path.join(ROOT_PATH, "schedule.txt"), strategy_step_size=60, time_range=15 * 60, telescope_latitude=47.344192, telescope_longitude=8.114368) strategy_start = parse_datetime("2015-12-21-02:00:00") strategy_end = parse_datetime("2015-12-22-09:00:00") ctx = Struct(params=params, strategy_start=strategy_start, strategy_end=strategy_end) strategy = scheduler_virtual.load_strategy(ctx) assert len(ctx.calibration["2015-12-22"]) == 1 calib_src = ctx.calibration["2015-12-22"][0] assert calib_src.src == "CygA" assert len(strategy) == int( (strategy_end - strategy_start).total_seconds()) / 60 assert strategy[0].az == np.radians(200.0) assert strategy[0].alt == np.radians(36.0) source = gsm_point_src.SOURCES["Virtual_%s" % calib_src.src] assert np.allclose( strategy[-int(4 * 3600 / 2 / params.strategy_step_size)].ra, source.ra) assert np.allclose( strategy[-int(4 * 3600 / 2 / params.strategy_step_size)].dec, source.dec)
def setup(self): n_frequencies = 10 params = Struct(file_fmt="TEST_{mode}_{polarization}_{date}.h5", strategy_start="2015-01-01-00:00:00", strategy_step_size=1, time_range=5, mode='MP', polarizations=['PXX'], instrument="hide.spectrometer.M9703A") tod = np.zeros((n_frequencies, 10)) strategy_start = parse_datetime(params.strategy_start) self.ctx = Struct(params=params, tod_vx=tod, tod_vy=tod, frequencies=np.arange(n_frequencies), strategy_idx=0, strategy_start=strategy_start, strategy_end=parse_datetime("2015-01-01-00:00:10"), batch_start_date=strategy_start) dummy_strategy = center.load_strategy(self.ctx) self.ctx.strategy_coords = dummy_strategy self.plugin = write_tod_phaseswitch.Plugin(self.ctx)
def test_make_map(self): params = Struct(map_maker="seek.mapmaking.healpy_mapper", nside=2, variance=False) npix = hp.nside2npix(params.nside) ind = 1 theta, phi = hp.pix2ang(params.nside, ind) dec = np.pi * .5 - theta ra = phi times = np.array([[0, 0, 0, ra, dec], [0, 0, 0, ra, dec]]) rfi_mask = np.array([[False, False]], dtype=bool) tod_vx = np.ma.array([[1., 2.]], mask=rfi_mask) tod_vy = np.ma.array([[2., 4.]], mask=rfi_mask) frequencies = np.array([1420.40575177]) ctx = Struct(params=params, frequencies=frequencies, times=times, tod_vx=tod_vx, tod_vy=tod_vy, coords=Coords(times[:, -2], times[:, -1], None, None, None)) plugin = make_maps.Plugin(ctx) plugin() assert len(ctx.map_idxs) == 3 assert ctx.maps[0] == tod_vx.sum() assert ctx.maps[1] == tod_vy.sum() assert ctx.counts[0] == tod_vx.shape[1] assert ctx.counts[1] == tod_vy.shape[1]
def test_load_signal(self): params = Struct(beam_nside=GSM_NSIDE) ctx = Struct(params=params) ctx.frequency = 980.0 astro_signal = gsm.load_signal(ctx) assert astro_signal is not None assert hp.get_nside(astro_signal) == ctx.params.beam_nside root_file_path = resource_filename(hide.__name__, gsm.GSM_FILE_PATH) file_path = os.path.join(root_file_path, str(params.beam_nside), "gsm_%s.fits" % (ctx.frequency)) gsm_map = hp.read_map(file_path) assert np.all(gsm_map == astro_signal) ctx.frequency = 1000.0 astro_signal = gsm.load_signal(ctx) assert astro_signal is not None assert hp.get_nside(astro_signal) == ctx.params.beam_nside file_path = os.path.join(root_file_path, str(params.beam_nside), "gsm_%s.fits" % (ctx.frequency)) gsm_map = hp.read_map(file_path) assert np.all(gsm_map == astro_signal) ctx.frequency = 1280.0 astro_signal = gsm.load_signal(ctx) assert astro_signal is not None assert hp.get_nside(astro_signal) == ctx.params.beam_nside file_path = os.path.join(root_file_path, str(params.beam_nside), "gsm_%s.fits" % (ctx.frequency)) gsm_map = hp.read_map(file_path) assert np.all(gsm_map == astro_signal)
def test_find_file_in_range(self): current_path = os.path.dirname(__file__) file_prefix = os.path.join(current_path, DATA_PATH) params = Struct(strategy_start="2015-01-01-00:00:00", strategy_end="2015-01-01-18:00:10", data_file_prefix="HIMap_", data_file_suffix="_02.fit.gz", file_date_format="%Y%m%d_%H%M%S", file_prefix=file_prefix, coord_prefix="coord5m", verbose=True) ctx = Struct(params=params) plugin = find_nested_files.Plugin(ctx) with pytest.raises(AssertionError): plugin() params.strategy_start = "2015-05-04-00:00:00" params.strategy_end = "2015-05-05-18:00:10" plugin() assert ctx.calibrations_paths is not None assert len(ctx.calibrations_paths) == 0 assert ctx.data_file_paths is not None assert len(ctx.data_file_paths) == 2 assert len(ctx.data_file_paths[0]) == 1 assert len(ctx.data_file_paths[1]) == 1 assert ctx.coords_paths is not None assert len(ctx.coords_paths) == 2
def test_load_signal(self): astro_flux = 1 params = Struct(astro_signal_provider="hide.astro.static", beam_nside=32, astro_flux=astro_flux, cache_astro_signals=False) ctx = Struct(params=params, strategy_idx=0, frequency=0) plugin = astro_signal.Plugin(ctx) plugin() assert ctx.astro_signal is not None assert np.all(ctx.astro_signal == params.astro_flux) #same frequency plugin() assert ctx.astro_signal is not None assert np.all(ctx.astro_signal == params.astro_flux) ctx.frequency = 1 astro_flux = 2 ctx.params.astro_flux = astro_flux plugin = astro_signal.Plugin(ctx) plugin() assert ctx.astro_signal is not None assert np.all(ctx.astro_signal == params.astro_flux)
def test_load_signal_rescale(self): params = Struct(beam_nside=2**6) ctx = Struct(params=params) ctx.frequency = 980 astro_signal = gsm.load_signal(ctx) assert astro_signal is not None assert hp.get_nside(astro_signal) == ctx.params.beam_nside
def test_map_indicies(self): params = Struct(cpu_count=2) ctx = Struct(params=params, restructured_tod_pixels=[1, 2, 3, 4]) plugin = map_indicies.Plugin(ctx) for i, ctx in enumerate(plugin.getWorkload()): assert len(ctx.map_pixels) == 2 assert i == 1
def test_load_signal(self): params = Struct(astro_flux=255, beam_nside=32) ctx = Struct(params=params, ) signal = static.load_signal(ctx) assert signal is not None assert len(signal) == hp.nside2npix(params.beam_nside) assert np.all(signal == params.astro_flux)
def test_bkg_removal_median(self): tod = np.ones((5, 9)) * np.arange(9) ctx = Struct(params=Struct(background_model="median"), tod_vx=np.ma.array(tod), tod_vy=np.ma.array(tod)) plugin = background_removal.Plugin(ctx) plugin() assert np.all(ctx.tod_vx.sum(axis=1) == 0) assert np.all(ctx.tod_vy.sum(axis=1) == 0)
def setup(self): self.maps = np.zeros((1,2)) self.redshifts = np.array([0.1, 0.2]) self.frequencies = np.array([0.1, 0.2]) self.params = Struct() ctx = Struct(params = self.params, maps = self.maps, redshifts = self.redshifts, counts = np.ones_like(self.maps), frequencies = self.frequencies) self.plugin = write_maps.Plugin(ctx)
def test_add_point_sources(self): freq = 1.0 nside = 16 params = Struct(beam_nside=nside) Map = np.zeros(hp.nside2npix(nside)) ctx = Struct(params=params, frequency=freq, astro_signal=Map) plugin = add_point_sources.Plugin(ctx) plugin() assert np.any(Map != 0.0)
def test_load_signal(self): params = Struct(beam_nside=2**4, vmin=-np.pi / 2, vmax=np.pi / 2, fit_coeffs=[1], log_base=1) ctx = Struct(params=params, ) earth_signal = horizon.load_signal(ctx) assert earth_signal is not None assert np.all(earth_signal == params.log_base)
def test_load_strategy(self): params = Struct(scanning_strategy_provider="hide.strategy.center", strategy_step_size=1) ctx = Struct(params=params, strategy_start=parse_datetime("2015-01-01-00:00:00"), strategy_end=parse_datetime("2015-01-01-00:00:10")) plugin = scanning_strategy.Plugin(ctx) plugin() assert ctx.strategy is not None assert len(ctx.strategy) == 10
def ctx(): params = Struct(telescope_latitude = 47.344192, telescope_longitude = 8.114368, telescope_elevation = 500, integration_time = 10, integration_frequency = 10, source_dec = 0.0, calibration_chi1 = 1e10, calibration_sources = 'CasA', gain_file_sun = 'data/sun_gain_template.dat') ctx = Struct(params=params) return ctx
def test_apply_gain(self): freq_no = 100 tod_vx = np.random.uniform(1, 250, (freq_no, 10)) params = Struct(instrument="hide.spectrometer.M9703A") freq = np.arange(freq_no) gain = np.arange(freq_no) ctx = Struct(params=params, tod_vx=tod_vx.copy(), frequencies=freq) with patch("numpy.loadtxt") as load_txt_mock: load_txt_mock.return_value = np.vstack([freq, gain]).T plugin = Plugin(ctx) plugin() assert np.allclose(tod_vx * gain.reshape(-1, 1), ctx.tod_vx)
def test_load_signal(self): params = Struct(beam_nside=32, earth_signal_provider="hide.earth.constant", earth_signal_flux=1) ctx = Struct(params = params, ) earth_signal.earth_signal = None plugin = earth_signal.Plugin(ctx) plugin() assert ctx.earth_signal is not None assert np.all(ctx.earth_signal==params.earth_signal_flux)
def _parseArgs(self, argv): if (argv is None or len(argv) < 1): raise InvalidAttributeException() if isinstance(argv, dict): return Struct(**argv) else: config = loadConfigs(argv[-1]) # overwrite parameters by command line options optlist, positional = getopt( argv, '', [name.replace('_', '-') + '=' for name in list(config.keys())]) if len(positional) != 1: raise InvalidAttributeException('only one config file is allowed') for opt in optlist: if opt[0][:2] != '--': raise InvalidAttributeException( 'invalid option name: {:}'.format(opt[0])) elif not opt[0][2:].replace('-', '_') in config: raise InvalidAttributeException('unknown option: {:}'.format( opt[0][2:])) else: name = opt[0][2:].replace('-', '_') config[name] = TYPE_MAP[type(config[name]).__name__](opt[1]) return config
def loadConfigs(configs): """ Loads key-value configurations from Python modules. :param configs: string or list of strings with absolute module declaration e.g. "ivy.config.base_config :return config: a :py:class:`Struct` instance with the config attributes """ if configs is None: raise InvalidAttributeException('Invalid configuration passed') if not isinstance(configs, list): configs = [configs] if len(configs) < 1: raise InvalidAttributeException('Invalid configuration passed') args = {} for configName in configs: config = importlib.import_module(configName) attrs = [] for name in dir(config): if not name.startswith("__"): attr = getattr(config, name) if not isinstance(attr, types.ModuleType): attrs.append((name, attr) ) args.update(attrs) return Struct(**args)
def test_post_processing(): tod_vx = np.random.uniform(1, 250, (100, 100)) params = Struct(log_base=10, offset_baseline=10, model_slope=0, model_sw=[0.], model_fmin=1, model_fmax=2, model_nf=1) ctx = Struct(params=params, tod_vx=tod_vx, frequencies=np.arange(100)) callisto.apply_gain(ctx) assert np.allclose(np.log10(tod_vx) + params.offset_baseline, ctx.tod_vx) assert ctx.tod_vy is not None
def test_load_strategy(self): params = Struct(telescope_latitude=47.344192, telescope_longitude=8.114368, strategy_step_size=1) ctx = Struct(params=params, strategy_start=parse_datetime("2015-01-01-00:00:00"), strategy_end=parse_datetime("2015-01-01-00:00:10")) strategy = crosshair.load_strategy(ctx) assert strategy is not None assert len(strategy) == 10 for coord in strategy: assert 0 <= coord.alt <= 2 * np.pi assert 0 <= coord.az <= 2 * np.pi
def test_struct(self): a = Struct() a['x'] = 1 assert a.x == 1 a.y = 2 assert a['y'] == 2
def testLoadCoords(self): params = Struct(telescope_latitude=47.2000007629395, telescope_longitude=8.5, telescope_elevation=500, integration_time=1) ctx = Struct(params=params) coords_path = os.path.join(COORDS_ROOT, "coord5m20141121.txt") ctx.coords_paths = {"2014-11-22": coords_path} ctx.strategy_start = datetime(2014, 11, 22, 17, 0, 0) ctx.tod_vx = np.empty((1, 3)) ctx.time_axis = np.arange(3) plugin = process_coords.Plugin(ctx) plugin() assert ctx.coords is not None assert len(ctx.coords.ra) == 3 assert len(ctx.coords.dec) == 3