예제 #1
0
def test_sky_shell():
    width = 2 * u.deg
    rad = 2 * u.deg
    model = ShellSpatialModel(lon_0="1 deg", lat_0="45 deg", radius=rad, width=width)
    lon = [1, 2, 4] * u.deg
    lat = 45 * u.deg
    val = model(lon, lat)
    assert val.unit == "deg-2"
    desired = [55.979449, 57.831651, 94.919895]
    assert_allclose(val.to_value("sr-1"), desired)
    radius = model.evaluation_radius
    assert radius.unit == "deg"
    assert_allclose(radius.value, rad.value + width.value)
    assert isinstance(model.to_region(), CircleAnnulusSkyRegion)
예제 #2
0
    def spatial_model(self):
        """Spatial model (`~gammapy.modeling.models.SpatialModel`).

        One of the following models (given by ``Spatial_Model`` in the catalog):

        - ``Point-Like`` or has a size upper limit : `~gammapy.modeling.models.PointSpatialModel`
        - ``Gaussian``: `~gammapy.modeling.models.GaussianSpatialModel`
        - ``2-Gaussian`` or ``3-Gaussian``: composite model (using ``+`` with Gaussians)
        - ``Shell``: `~gammapy.modeling.models.ShellSpatialModel`
        """
        d = self.data
        glon = d["GLON"]
        glat = d["GLAT"]

        spatial_type = self.spatial_model_type

        if self.is_pointlike:
            model = PointSpatialModel(lon_0=glon, lat_0=glat, frame="galactic")
        elif spatial_type == "gaussian":
            model = GaussianSpatialModel(lon_0=glon,
                                         lat_0=glat,
                                         sigma=d["Size"],
                                         frame="galactic")
            model.parameters.set_error(lon_0=d["GLON_Err"],
                                       lat_0=d["GLAT_Err"],
                                       sigma=d["Size_Err"])
        elif spatial_type in {"2-gaussian", "3-gaussian"}:
            raise ValueError(
                "For Gaussian or Multi-Gaussian models, use sky_model()!")
        elif spatial_type == "shell":
            # HGPS contains no information on shell width
            # Here we assume a 5% shell width for all shells.
            r_out = d["Size"]
            radius = 0.95 * r_out
            width = r_out - radius
            model = ShellSpatialModel(lon_0=glon,
                                      lat_0=glat,
                                      width=width,
                                      radius=radius,
                                      frame="galactic")
            model.parameters.set_error(lon_0=d["GLON_Err"],
                                       lat_0=d["GLAT_Err"],
                                       radius=d["Size_Err"])
        else:
            raise ValueError(f"Invalid spatial_type: {spatial_type}")

        return model
예제 #3
0
파일: gammacat.py 프로젝트: mstrzys/gammapy
    def spatial_model(self):
        """Source spatial model (`~gammapy.modeling.models.SpatialModel`).

        TODO: add parameter errors!
        """
        d = self.data
        morph_type = d["morph_type"]

        glon = d["glon"]
        glat = d["glat"]

        if morph_type == "point":
            model = PointSpatialModel(lon_0=glon, lat_0=glat, frame="galactic")
        elif morph_type == "gauss":
            # TODO: add infos back once model support elongation
            # pars['x_stddev'] = d['morph_sigma']
            # pars['y_stddev'] = d['morph_sigma']
            # if not np.isnan(d['morph_sigma2']):
            #     pars['y_stddev'] = d['morph_sigma2']
            # if not np.isnan(d['morph_pa']):
            #     # TODO: handle reference frame for rotation angle
            #     pars['theta'] = Angle(d['morph_pa'], 'deg').rad
            model = GaussianSpatialModel(
                lon_0=glon, lat_0=glat, sigma=d["morph_sigma"], frame="galactic"
            )
        elif morph_type == "shell":
            model = ShellSpatialModel(
                lon_0=glon,
                lat_0=glat,
                # TODO: probably we shouldn't guess a shell width here!
                radius=0.8 * d["morph_sigma"],
                width=0.2 * d["morph_sigma"],
                frame="galactic",
            )
        elif morph_type == "none":
            return None
        else:
            raise ValueError(f"Invalid morph_type: {morph_type!r}")

        lat_err = self.data["pos_err"].to("deg")
        lon_err = self.data["pos_err"].to("deg") / np.cos(self.data["glat"].to("rad"))
        model.parameters.set_error(lon_0=lon_err, lat_0=lat_err)
        # TODO: check if pos_err is really 1sigma
        return model
예제 #4
0
    def spatial_model(self):
        """Source spatial model (`~gammapy.modeling.models.SpatialModel`).

        TODO: add parameter errors!
        """
        d = self.data
        morph_type = d["morph_type"]

        glon = d["glon"]
        glat = d["glat"]

        if morph_type == "point":
            return PointSpatialModel(lon_0=glon, lat_0=glat, frame="galactic")
        elif morph_type == "gauss":
            # TODO: add infos back once model support elongation
            # pars['x_stddev'] = d['morph_sigma']
            # pars['y_stddev'] = d['morph_sigma']
            # if not np.isnan(d['morph_sigma2']):
            #     pars['y_stddev'] = d['morph_sigma2']
            # if not np.isnan(d['morph_pa']):
            #     # TODO: handle reference frame for rotation angle
            #     pars['theta'] = Angle(d['morph_pa'], 'deg').rad
            return GaussianSpatialModel(lon_0=glon,
                                        lat_0=glat,
                                        sigma=d["morph_sigma"],
                                        frame="galactic")
        elif morph_type == "shell":
            return ShellSpatialModel(
                lon_0=glon,
                lat_0=glat,
                # TODO: probably we shouldn't guess a shell width here!
                radius=0.8 * d["morph_sigma"],
                width=0.2 * d["morph_sigma"],
                frame="galactic",
            )
        elif morph_type == "none":
            raise NoDataAvailableError(
                f"No spatial model available: {self.name}")
        else:
            raise NotImplementedError(f"Unknown spatial model: {morph_type!r}")
예제 #5
0
"""

# %%
# Example plot
# ------------
# Here is an example plot of the model:

from gammapy.modeling.models import (
    Models,
    PowerLawSpectralModel,
    ShellSpatialModel,
    SkyModel,
)

model = ShellSpatialModel(
    lon_0="10 deg", lat_0="20 deg", radius="2 deg", width="0.5 deg", frame="galactic",
)

model.plot(add_cbar=True)

# %%
# YAML representation
# -------------------
# Here is an example YAML file using the model:

pwl = PowerLawSpectralModel()
shell = ShellSpatialModel()

model = SkyModel(spectral_model=pwl, spatial_model=shell, name="pwl-shell-model")
models = Models([model])