Exemple #1
0
    def setup(self):
        etrue = np.logspace(-1, 1, 10) * u.TeV
        self.e_true = etrue
        ereco = np.logspace(-1, 1, 5) * u.TeV
        elo = ereco[:-1]
        ehi = ereco[1:]
        self.e_reco = ereco
        self.aeff = EffectiveAreaTable(etrue[:-1], etrue[1:], np.ones(9) * u.cm ** 2)
        self.edisp = EDispKernel.from_diagonal_response(etrue, ereco)

        start = u.Quantity([0], "s")
        stop = u.Quantity([1000], "s")
        time_ref = Time("2010-01-01 00:00:00.0")
        self.gti = GTI.create(start, stop, time_ref)
        self.livetime = self.gti.time_sum

        self.on_region = make_region("icrs;circle(0.,1.,0.1)")
        off_region = make_region("icrs;box(0.,1.,0.1, 0.2,30)")
        self.off_region = off_region.union(
            make_region("icrs;box(-1.,-1.,0.1, 0.2,150)")
        )
        self.wcs = WcsGeom.create(npix=300, binsz=0.01, frame="icrs").wcs

        data = np.ones(elo.shape)
        data[-1] = 0  # to test stats calculation with empty bins

        axis = MapAxis.from_edges(ereco, name="energy", interp="log")
        self.on_counts = RegionNDMap.create(
            region=self.on_region, wcs=self.wcs, axes=[axis]
        )
        self.on_counts.data += 1
        self.on_counts.data[-1] = 0

        self.off_counts = RegionNDMap.create(
            region=self.off_region, wcs=self.wcs, axes=[axis]
        )
        self.off_counts.data += 10

        acceptance = RegionNDMap.from_geom(self.on_counts.geom)
        acceptance.data += 1

        data = np.ones(elo.shape)
        data[-1] = 0

        acceptance_off = RegionNDMap.from_geom(self.off_counts.geom)
        acceptance_off.data += 10

        self.dataset = SpectrumDatasetOnOff(
            counts=self.on_counts,
            counts_off=self.off_counts,
            aeff=self.aeff,
            edisp=self.edisp,
            livetime=self.livetime,
            acceptance=acceptance,
            acceptance_off=acceptance_off,
            name="test",
            gti=self.gti,
        )
Exemple #2
0
def test_compound_region_center():
    region_1 = make_region("galactic;circle(1,1,0.1)")
    region_2 = make_region("galactic;circle(-1,1,0.1)")
    region_3 = make_region("galactic;circle(1,-1,0.1)")
    region_4 = make_region("galactic;circle(-1,-1,0.1)")

    for region in [region_2, region_3, region_4]:
        region_1 = region_1.union(region)

    center = compound_region_center(region_1)

    assert_allclose(center.galactic.l.wrap_at("180d"), 0 * u.deg, atol=1e-6)
    assert_allclose(center.galactic.b, 0 * u.deg, atol=1e-6)
Exemple #3
0
def test_make_region():
    reg = make_region("image;circle(10,20,3)")
    assert isinstance(reg, regions.CirclePixelRegion)
    assert reg.center.x == 9
    assert reg.center.y == 19
    assert reg.radius == 3

    reg = make_region("galactic;circle(10,20,3)")
    assert reg.center.l.deg == 10
    assert reg.center.b.deg == 20
    assert reg.radius.to_value("deg") == 3

    # Existing regions should pass through
    reg2 = make_region(reg)
    assert reg is reg2

    with pytest.raises(TypeError):
        make_pixel_region([reg])
Exemple #4
0
def test_edispkernel_from_diagonal_response():
    energy_axis_true = MapAxis.from_energy_bounds(
        "0.3 TeV", "10 TeV", nbin=11, name="energy_true"
    )
    energy_axis = MapAxis.from_energy_bounds(
        "0.3 TeV", "10 TeV", nbin=11, name="energy"
    )

    region = make_region("fk5;circle(0.,0., 10.")
    geom = RegionGeom(region)
    region_edisp = EDispKernelMap.from_diagonal_response(
        energy_axis, energy_axis_true, geom=geom
    )
    sum_kernel = np.sum(region_edisp.edisp_map.data[..., 0, 0], axis=1)

    # We exclude the first and last bin, where there is no
    # e_reco to contribute to
    assert_allclose(sum_kernel[1:-1], 1)
Exemple #5
0
    def select_region(self, region, wcs=None):
        """Select events in given region.

        Parameters
        ----------
        region : `~regions.SkyRegion` or str
            Sky region or string defining a sky region
        wcs : `~astropy.wcs.WCS`
            World coordinate system transformation

        Returns
        -------
        event_list : `EventList`
            Copy of event list with selection applied.
        """
        region = make_region(region)
        mask = region.contains(self.radec, wcs)
        return self.select_row_subset(mask)
Exemple #6
0
def test_edispkernel_from_1D():
    energy_axis_true = MapAxis.from_energy_bounds(
        "0.5 TeV", "5 TeV", nbin=31, name="energy_true"
    )
    energy_axis = MapAxis.from_energy_bounds(
        "0.1 TeV", "10 TeV", nbin=11, name="energy"
    )

    edisp = EDispKernel.from_gauss(energy_axis_true.edges, energy_axis.edges, 0.1, 0.0)
    region = make_region("fk5;circle(0.,0., 10.")
    geom = RegionGeom(region)
    region_edisp = EDispKernelMap.from_edisp_kernel(edisp, geom=geom)
    sum_kernel = np.sum(region_edisp.edisp_map.data[..., 0, 0], axis=1)
    assert_allclose(sum_kernel, 1, rtol=1e-5)

    allsky_edisp = EDispKernelMap.from_edisp_kernel(edisp)
    sum_kernel = np.sum(allsky_edisp.edisp_map.data[..., 0, 0], axis=1)
    assert allsky_edisp.edisp_map.data.shape == (31, 11, 1, 2)
    assert_allclose(sum_kernel, 1, rtol=1e-5)
Exemple #7
0
    def create(cls, region, **kwargs):
        """Create region.

        Parameters
        ----------
        region : str or `~regions.SkyRegion`
            Region
        axes : list of `MapAxis`
            Non spatial axes.

        Returns
        -------
        geom : `RegionGeom`
            Region geometry
        """
        if isinstance(region, str):
            region = make_region(region)

        return cls(region, **kwargs)
Exemple #8
0
    def from_regions(cls, regions, **kwargs):
        """Create region geom from list of regions

        The regions are combined with union to a compound region.

        Parameters
        ----------
        regions : `~regions.SkyRegion`
            Regions
        **kwargs: dict
            Keyword arguments forwarded to `RegionGeom`

        Returns
        -------
        geom : `RegionGeom`
            Region map geometry
        """
        if isinstance(regions, (SkyRegion, str)):
            regions = [make_region(regions)]

        region = list_to_compound_region(regions)
        return cls(region, **kwargs)
Exemple #9
0
    def setup(self):
        etrue = np.logspace(-1, 1, 10) * u.TeV
        self.e_true = MapAxis.from_energy_edges(etrue, name="energy_true")
        ereco = np.logspace(-1, 1, 5) * u.TeV
        elo = ereco[:-1]
        ehi = ereco[1:]
        self.e_reco = MapAxis.from_energy_edges(ereco, name="energy")

        start = u.Quantity([0], "s")
        stop = u.Quantity([1000], "s")
        time_ref = Time("2010-01-01 00:00:00.0")
        self.gti = GTI.create(start, stop, time_ref)
        self.livetime = self.gti.time_sum

        self.on_region = make_region("icrs;circle(0.,1.,0.1)")
        off_region = make_region("icrs;box(0.,1.,0.1, 0.2,30)")
        self.off_region = off_region.union(
            make_region("icrs;box(-1.,-1.,0.1, 0.2,150)"))
        self.wcs = WcsGeom.create(npix=300, binsz=0.01, frame="icrs").wcs

        self.aeff = RegionNDMap.create(region=self.on_region,
                                       wcs=self.wcs,
                                       axes=[self.e_true],
                                       unit="cm2")
        self.aeff.data += 1

        data = np.ones(elo.shape)
        data[-1] = 0  # to test stats calculation with empty bins

        axis = MapAxis.from_edges(ereco, name="energy", interp="log")
        self.on_counts = RegionNDMap.create(
            region=self.on_region,
            wcs=self.wcs,
            axes=[axis],
            meta={"EXPOSURE": self.livetime.to_value("s")},
        )
        self.on_counts.data += 1
        self.on_counts.data[-1] = 0

        self.off_counts = RegionNDMap.create(region=self.off_region,
                                             wcs=self.wcs,
                                             axes=[axis])
        self.off_counts.data += 10

        acceptance = RegionNDMap.from_geom(self.on_counts.geom)
        acceptance.data += 1

        data = np.ones(elo.shape)
        data[-1] = 0

        acceptance_off = RegionNDMap.from_geom(self.off_counts.geom)
        acceptance_off.data += 10

        self.edisp = EDispKernelMap.from_diagonal_response(
            self.e_reco, self.e_true, self.on_counts.geom.to_image())

        exposure = self.aeff * self.livetime
        exposure.meta["livetime"] = self.livetime

        mask_safe = RegionNDMap.from_geom(self.on_counts.geom, dtype=bool)
        mask_safe.data += True

        self.dataset = SpectrumDatasetOnOff(
            counts=self.on_counts,
            counts_off=self.off_counts,
            exposure=exposure,
            edisp=self.edisp,
            acceptance=acceptance,
            acceptance_off=acceptance_off,
            name="test",
            gti=self.gti,
            mask_safe=mask_safe,
        )