-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4853 from AtreyeeS/dataset_meta
Add MapDatasetMetaData container
- Loading branch information
Showing
9 changed files
with
293 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from typing import ClassVar, Literal, Optional, Union | ||
import numpy as np | ||
from astropy.coordinates import SkyCoord | ||
from pydantic import ConfigDict | ||
from gammapy.utils.metadata import ( | ||
METADATA_FITS_KEYS, | ||
CreatorMetaData, | ||
MetaData, | ||
ObsInfoMetaData, | ||
PointingInfoMetaData, | ||
) | ||
|
||
__all__ = ["MapDatasetMetaData"] | ||
|
||
MapDataset_METADATA_FITS_KEYS = { | ||
"MapDataset": { | ||
"event_types": "EVT_TYPE", | ||
"optional": "OPTIONAL", | ||
}, | ||
} | ||
|
||
METADATA_FITS_KEYS.update(MapDataset_METADATA_FITS_KEYS) | ||
|
||
|
||
class MapDatasetMetaData(MetaData): | ||
"""Metadata containing information about the Dataset. | ||
Parameters | ||
---------- | ||
creation : `~gammapy.utils.CreatorMetaData`, optional | ||
The creation metadata. | ||
obs_info : list of `~gammapy.utils.ObsInfoMetaData` | ||
info about the observation. | ||
event_types : list of int or str | ||
Event types used in analysis. | ||
pointing: list of `~gammapy.utils.PointingInfoMetaData` | ||
Telescope pointing directions. | ||
optional : dict | ||
Additional optional metadata. | ||
""" | ||
|
||
model_config = ConfigDict(coerce_numbers_to_str=True) | ||
|
||
_tag: ClassVar[Literal["MapDataset"]] = "MapDataset" | ||
creation: Optional[CreatorMetaData] = CreatorMetaData() | ||
obs_info: Optional[Union[ObsInfoMetaData, list[ObsInfoMetaData]]] = None | ||
pointing: Optional[Union[PointingInfoMetaData, list[PointingInfoMetaData]]] = None | ||
event_type: Optional[Union[str, list[str]]] = None | ||
optional: Optional[dict] = None | ||
|
||
def stack(self, other): | ||
kwargs = {} | ||
kwargs["creation"] = self.creation | ||
return self.__class__(**kwargs) | ||
|
||
@classmethod | ||
def _from_meta_table(cls, table): | ||
"""Create MapDatasetMetaData from MapDataset.meta_table | ||
Parameters | ||
---------- | ||
table: `~astropy.table.Table` | ||
""" | ||
kwargs = {} | ||
kwargs["creation"] = CreatorMetaData() | ||
telescope = np.atleast_1d(table["TELESCOP"].data[0]) | ||
obs_id = np.atleast_1d(table["OBS_ID"].data[0].astype(str)) | ||
observation_mode = np.atleast_1d(table["OBS_MODE"].data[0]) | ||
|
||
obs_info = [] | ||
for i in range(len(obs_id)): | ||
obs_meta = ObsInfoMetaData( | ||
**{ | ||
"telescope": telescope[i], | ||
"obs_id": obs_id[i], | ||
"observation_mode": observation_mode[i], | ||
} | ||
) | ||
obs_info.append(obs_meta) | ||
kwargs["obs_info"] = obs_info | ||
|
||
pointing_radec, pointing_altaz = None, None | ||
if "RA_PNT" in table.colnames: | ||
pointing_radec = SkyCoord( | ||
ra=table["RA_PNT"].data[0], dec=table["DEC_PNT"].data[0], unit="deg" | ||
) | ||
if "ALT_PNT" in table.colnames: | ||
pointing_altaz = SkyCoord( | ||
alt=table["ALT_PNT"].data[0], | ||
az=table["AZ_PNT"].data[0], | ||
unit="deg", | ||
frame="altaz", | ||
) | ||
pointings = [] | ||
for pra, paz in zip( | ||
np.atleast_1d(pointing_radec), np.atleast_1d(pointing_altaz) | ||
): | ||
pointings.append(PointingInfoMetaData(radec_mean=pra, altaz_mean=paz)) | ||
kwargs["pointing"] = pointings | ||
return cls(**kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import pytest | ||
from numpy.testing import assert_allclose | ||
from astropy.coordinates import SkyCoord | ||
from pydantic import ValidationError | ||
from gammapy.datasets import MapDatasetMetaData | ||
from gammapy.utils.metadata import ObsInfoMetaData, PointingInfoMetaData | ||
|
||
|
||
def test_meta_default(): | ||
meta = MapDatasetMetaData() | ||
assert meta.creation.creator.split()[0] == "Gammapy" | ||
assert meta.obs_info is None | ||
|
||
|
||
def test_mapdataset_metadata(): | ||
position = SkyCoord(83.6287, 22.0147, unit="deg", frame="icrs") | ||
obs_info_input = { | ||
"telescope": "cta-north", | ||
"instrument": "lst", | ||
"observation_mode": "wobble", | ||
"obs_id": 112, | ||
} | ||
input = { | ||
"obs_info": ObsInfoMetaData(**obs_info_input), | ||
"pointing": PointingInfoMetaData(radec_mean=position), | ||
"optional": dict(test=0.5, other=True), | ||
} | ||
meta = MapDatasetMetaData(**input) | ||
|
||
assert meta.obs_info.telescope == "cta-north" | ||
assert meta.obs_info.instrument == "lst" | ||
assert meta.obs_info.observation_mode == "wobble" | ||
assert_allclose(meta.pointing.radec_mean.dec.value, 22.0147) | ||
assert_allclose(meta.pointing.radec_mean.ra.deg, 83.6287) | ||
assert meta.obs_info.obs_id == 112 | ||
assert meta.optional["other"] is True | ||
assert meta.creation.creator.split()[0] == "Gammapy" | ||
assert meta.event_type is None | ||
|
||
with pytest.raises(ValidationError): | ||
meta.pointing = 2.0 | ||
|
||
input_bad = input.copy() | ||
input_bad["bad"] = position | ||
|
||
with pytest.raises(ValueError): | ||
MapDatasetMetaData(**input_bad) | ||
|
||
|
||
def test_mapdataset_metadata_lists(): | ||
obs_info_input1 = { | ||
"telescope": "cta-north", | ||
"instrument": "lst", | ||
"observation_mode": "wobble", | ||
"obs_id": 111, | ||
} | ||
obs_info_input2 = { | ||
"telescope": "cta-north", | ||
"instrument": "lst", | ||
"observation_mode": "wobble", | ||
"obs_id": 112, | ||
} | ||
input = { | ||
"obs_info": [ | ||
ObsInfoMetaData(**obs_info_input1), | ||
ObsInfoMetaData(**obs_info_input2), | ||
], | ||
"pointing": [ | ||
PointingInfoMetaData( | ||
radec_mean=SkyCoord(83.6287, 22.0147, unit="deg", frame="icrs") | ||
), | ||
PointingInfoMetaData( | ||
radec_mean=SkyCoord(83.1287, 22.5147, unit="deg", frame="icrs") | ||
), | ||
], | ||
} | ||
meta = MapDatasetMetaData(**input) | ||
assert meta.obs_info[0].telescope == "cta-north" | ||
assert meta.obs_info[0].instrument == "lst" | ||
assert meta.obs_info[0].observation_mode == "wobble" | ||
assert_allclose(meta.pointing[0].radec_mean.dec.value, 22.0147) | ||
assert_allclose(meta.pointing[1].radec_mean.ra.deg, 83.1287) | ||
assert meta.obs_info[0].obs_id == 111 | ||
assert meta.obs_info[1].obs_id == 112 | ||
assert meta.optional is None | ||
assert meta.event_type is None | ||
|
||
|
||
def test_mapdataset_metadata_stack(): | ||
input1 = { | ||
"obs_info": ObsInfoMetaData(**{"obs_id": 111}), | ||
"pointing": PointingInfoMetaData( | ||
radec_mean=SkyCoord(83.6287, 22.5147, unit="deg", frame="icrs") | ||
), | ||
"optional": dict(test=0.5, other=True), | ||
} | ||
|
||
input2 = { | ||
"obs_info": ObsInfoMetaData(**{"instrument": "H.E.S.S.", "obs_id": 112}), | ||
"pointing": PointingInfoMetaData( | ||
radec_mean=SkyCoord(83.6287, 22.0147, unit="deg", frame="icrs") | ||
), | ||
"optional": dict(test=0.1, other=False), | ||
} | ||
|
||
meta1 = MapDatasetMetaData(**input1) | ||
meta2 = MapDatasetMetaData(**input2) | ||
|
||
meta = meta1.stack(meta2) | ||
assert meta.creation.creator.split()[0] == "Gammapy" | ||
assert meta.obs_info is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.