/
registry.py
114 lines (79 loc) · 3.19 KB
/
registry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from astropy.table import Table
__all__ = ["source_catalogs", "SourceCatalogRegistry"]
class SourceCatalogRegistry:
"""Source catalog registry.
Provides simple and efficient access to source catalogs
by lazy-loading and caching catalog objects.
You should use these catalogs read-only, if you modify
them you can get non-reproducible results if you access
the modified version later on.
"""
def __init__(self):
self._available_catalogs = {}
self._loaded_catalogs = {}
@classmethod
def builtins(cls):
"""Factory function that make a catalog registry and
registers the built-in catalogs.
"""
source_catalogs = cls()
from .hess import SourceCatalogHGPS
source_catalogs.register("hgps", SourceCatalogHGPS)
from .gammacat import SourceCatalogGammaCat
source_catalogs.register("gamma-cat", SourceCatalogGammaCat)
from .fermi import SourceCatalog3FGL
source_catalogs.register("3fgl", SourceCatalog3FGL)
from .fermi import SourceCatalog1FHL
source_catalogs.register("1fhl", SourceCatalog1FHL)
from .fermi import SourceCatalog2FHL
source_catalogs.register("2fhl", SourceCatalog2FHL)
from .fermi import SourceCatalog3FHL
source_catalogs.register("3fhl", SourceCatalog3FHL)
from .hawc import SourceCatalog2HWC
source_catalogs.register("2hwc", SourceCatalog2HWC)
return source_catalogs
@property
def catalog_names(self):
"""Catalog names (list of str)."""
return list(self._available_catalogs.keys())
def register(self, name, cls, args=()):
"""Register a source catalog.
It must be possible to load it via ``cls(*args)``.
"""
self._available_catalogs[name] = {"cls": cls, "args": args}
def __getitem__(self, name):
if name not in self._available_catalogs:
msg = 'Unknown catalog: "{}". '.format(name)
msg += "Available catalogs: {}".format(self.catalog_names)
raise KeyError(msg)
if name not in self._loaded_catalogs:
cat = self._available_catalogs[name]
self._loaded_catalogs[name] = cat["cls"](*cat["args"])
return self._loaded_catalogs[name]
def info(self):
"""Print summary info about catalogs."""
print("Source catalog registry:")
self.info_table.pprint()
@property
def info_table(self):
"""Summary info table on catalogs.
Loads all catalogs.
"""
rows = []
for name in self._available_catalogs.keys():
cat = self[name]
rows.append(
{
"name": name,
"description": cat.description,
"sources": len(cat.table),
}
)
return Table(rows=rows, names=["name", "description", "sources"])
source_catalogs = SourceCatalogRegistry.builtins()
"""Registry of built-in catalogs in Gammapy.
The main point of the registry is to have one point that
knows about all available catalogs and there's an easy way
to load them.
"""