def test_rad_max_roundtrip(tmp_path): n_energy = 10 energy_axis = MapAxis.from_energy_bounds(50 * u.GeV, 100 * u.TeV, n_energy, name="energy") n_offset = 5 offset_axis = MapAxis.from_bounds(0, 2, n_offset, unit=u.deg, name="offset") shape = (n_energy, n_offset) rad_max = np.linspace(0.1, 0.5, n_energy * n_offset).reshape(shape) rad_max_2d = RadMax2D( axes=[ energy_axis, offset_axis, ], data=rad_max, unit=u.deg, ) rad_max_2d.write(tmp_path / "rad_max.fits") rad_max_read = RadMax2D.read(tmp_path / "rad_max.fits") assert np.all(rad_max_read.data == rad_max) assert np.all(rad_max_read.data == rad_max_read.data)
def test_create_irf_point_like_energy_dependent_cuts( temp_dir_observed_files, simulated_dl2_file ): """ Generating point-like IRF file from a test DL2 files, using energy-dependent cuts """ from lstchain.tools.lstchain_create_irf_files import IRFFITSWriter from gammapy.irf import RadMax2D irf_file = temp_dir_observed_files / "pnt_irf.fits.gz" assert ( run_tool( IRFFITSWriter(), argv=[ f"--input-gamma-dl2={simulated_dl2_file}", f"--input-proton-dl2={simulated_dl2_file}", f"--input-electron-dl2={simulated_dl2_file}", f"--output-irf-file={irf_file}", "--overwrite", "--energy-dependent-gh", "--point-like", "--energy-dependent-theta", "--DL3Cuts.max_theta_cut=1", "--DL3Cuts.fill_theta_cut=1" ], cwd=temp_dir_observed_files, ) == 0 ) assert RadMax2D.read(irf_file, hdu="RAD_MAX")
def rad_max(self): # prevent circular import from gammapy.irf import RadMax2D if self._rad_max is not None: return self._rad_max # load once to avoid trigger lazy loading it three times aeff = self.aeff if aeff is not None and aeff.is_pointlike: self._rad_max = RadMax2D.from_irf(aeff) return self._rad_max edisp = self.edisp if edisp is not None and edisp.is_pointlike: self._rad_max = RadMax2D.from_irf(self.edisp) return self._rad_max
def test_rad_max_from_irf(): e_bins = 3 o_bins = 2 energy_axis = MapAxis.from_energy_bounds(1 * u.TeV, 10 * u.TeV, nbin=e_bins, name='energy_true') offset_axis = MapAxis.from_bounds(0 * u.deg, 3 * u.deg, nbin=o_bins, name='offset') aeff = EffectiveAreaTable2D( data=u.Quantity(np.ones((e_bins, o_bins)), u.m**2, copy=False), axes=[energy_axis, offset_axis], is_pointlike=True, ) with pytest.raises(ValueError): # not a point-like IRF RadMax2D.from_irf(aeff) with pytest.raises(ValueError): # missing rad_max RadMax2D.from_irf(aeff) aeff.meta['RAD_MAX'] = '0.2 deg' with pytest.raises(ValueError): # invalid format RadMax2D.from_irf(aeff) aeff.meta['RAD_MAX'] = 0.2 rad_max = RadMax2D.from_irf(aeff) assert rad_max.axes['energy'].nbin == 1 assert rad_max.axes['offset'].nbin == 1 assert rad_max.axes['energy'].edges[0] == aeff.axes['energy_true'].edges[0] assert rad_max.axes['energy'].edges[1] == aeff.axes['energy_true'].edges[ -1] assert rad_max.axes['offset'].edges[0] == aeff.axes['offset'].edges[0] assert rad_max.axes['offset'].edges[1] == aeff.axes['offset'].edges[-1] assert rad_max.quantity.shape == (1, 1) assert rad_max.quantity[0, 0] == 0.2 * u.deg
def test_rad_max_single_bin(): energy_axis = MapAxis.from_energy_bounds(0.01, 100, 1, unit="TeV") offset_axis = MapAxis.from_bounds( 0., 5, 1, unit="deg", name="offset", ) rad_max = RadMax2D(data=[[0.1]] * u.deg, axes=[energy_axis, offset_axis], interp_kwargs={ "method": "nearest", "fill_value": None }) value = rad_max.evaluate(energy=1 * u.TeV, offset=1 * u.deg) assert_allclose(value, 0.1 * u.deg) value = rad_max.evaluate(energy=[1, 2, 3] * u.TeV, offset=[[1]] * u.deg) assert value.shape == (1, 3) assert_allclose(value, 0.1 * u.deg)