BAccMod 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,286 @@
1
+ Metadata-Version: 2.1
2
+ Name: BAccMod
3
+ Version: 0.3.0
4
+ Summary: Calculate 2D and 3d acceptance model for creating maps with gammapy (IACT analysis)
5
+ Home-page: https://github.com/mdebony/BAccMod
6
+ Author: Mathieu de Bony de Lavergne, Gabriel Emery, Marie-Sophie Carrasco
7
+ Author-email: debony@cppm.in2p3.fr
8
+ License: LGPL v3
9
+ Description-Content-Type: text/markdown
10
+ License-File: LICENCE.txt
11
+ Requires-Dist: gammapy<1.4,>=1.1
12
+ Requires-Dist: numpy<2.3
13
+ Requires-Dist: scipy<1.15
14
+ Requires-Dist: iminuit<3.0,>=2.0
15
+ Requires-Dist: astropy<8.0,>=4.0
16
+ Requires-Dist: regions<0.11,>=0.7
17
+
18
+ # Description
19
+
20
+ This package create background model (or acceptance model) to be used for IACT analysis with gammapy
21
+ BAccMod is licensed under the GNU Lesser General Public License (LGPL) v3.0.
22
+
23
+ # Installation
24
+
25
+
26
+ ```bash
27
+ pip install BAccMod
28
+ ```
29
+
30
+ Dependencies :
31
+ - numpy
32
+ - scipy
33
+ - astropy
34
+ - iminuit >= 2.0
35
+ - gammapy >= 1.1
36
+ - regions >= 0.7
37
+
38
+ # Example of use
39
+
40
+ ## Basic use
41
+
42
+ You could first create the acceptance (or background) model
43
+
44
+ ```python
45
+ from gammapy.maps import MapAxis
46
+ from gammapy.data import DataStore
47
+ from regions import CircleSkyRegion
48
+ import astropy.units as u
49
+ from astropy.coordinates import SkyCoord
50
+ from baccmod import RadialAcceptanceMapCreator
51
+
52
+ # The observations to use for creating the acceptance model
53
+ data_store = DataStore.from_dir("$GAMMAPY_DATA/hess-dl3-dr1")
54
+ obs_collection = data_store.get_observations([23523, 23526, 23559, 23592])
55
+
56
+ # The exclusion regions to apply during acceptance model calculation
57
+ exclude_regions = [CircleSkyRegion(center=SkyCoord.from_name('Crab'),
58
+ radius=0.2 * u.deg), ]
59
+
60
+ # Define the binning of the model
61
+ e_min, e_max = 0.1 * u.TeV, 10. * u.TeV
62
+ size_fov = 2.5 * u.deg
63
+ offset_axis_acceptance = MapAxis.from_bounds(0. * u.deg, size_fov, nbin=6, name='offset')
64
+ energy_axis_acceptance = MapAxis.from_energy_bounds(e_min, e_max, nbin=6, name='energy')
65
+
66
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
67
+ offset_axis_acceptance,
68
+ exclude_regions=exclude_regions,
69
+ oversample_map=10)
70
+ acceptance_model = acceptance_model_creator.create_acceptance_map(obs_collection)
71
+
72
+ ```
73
+
74
+ You can then check the acceptance model by plotting it using
75
+
76
+ ```python
77
+ acceptance_model.peek()
78
+ ```
79
+
80
+ To use it with gammapy, you could first save it on a FITS file
81
+
82
+ ```python
83
+ hdu_acceptance = acceptance_model.to_table_hdu()
84
+ hdu_acceptance.writeto('acceptance.fits', overwrite=True)
85
+ ```
86
+
87
+ It's then possible to load the acceptance model in the current gammapy DataStore with this code.
88
+ You would need then to recreate you gammapy Observations object in order than the acceptance model is taken into account for the analysis.
89
+
90
+ ```python
91
+ data_store.hdu_table.remove_rows(data_store.hdu_table['HDU_TYPE']=='bkg')
92
+ for obs_id in np.unique(data_store.hdu_table['OBS_ID']):
93
+ data_store.hdu_table.add_row({'OBS_ID': obs_id,
94
+ 'HDU_TYPE': 'bkg',
95
+ "HDU_CLASS": "bkg_2d",
96
+ "FILE_DIR": "",
97
+ "FILE_NAME": 'acceptance.fits',
98
+ "HDU_NAME": "BACKGROUND",
99
+ "SIZE": hdu_acceptance.size})
100
+ data_store.hdu_table = data_store.hdu_table.copy()
101
+
102
+ obs_collection = data_store.get_observations([23523, 23526, 23559, 23592])
103
+
104
+ data_store.hdu_table
105
+ ```
106
+
107
+ > [!WARNING]
108
+ > Due to some change in gammapy 1.3, it's strongly suggested to use only use models that have been created with the same version of gammapy used
109
+
110
+
111
+ ## Telescope position
112
+
113
+ **The observations should contain the telescope position in order to have the algorithm working.**
114
+ If the information is missing in the DL3, you could either add it or it possible to add it directly to the observation as shown in the example below.
115
+ This snippet of code may require some adaptation depending on the version of gammapy you are using.
116
+ ```python
117
+ from astropy.coordinates import EarthLocation
118
+
119
+ # Your telescope position in an EarthLocation object
120
+ loc = EarthLocation.of_site('Roque de los Muchachos')
121
+
122
+ # Add telescope position to observations
123
+ for i in obs_collection:
124
+ obs_collection[i].obs_info['GEOLON'] = loc.lon.value
125
+ obs_collection[i].obs_info['GEOLAT'] = loc.lat.value
126
+ obs_collection[i].obs_info['GEOALT'] = loc.height.value
127
+ obs_collection[i]._location = loc
128
+ ```
129
+
130
+ ## Runwise norm of the model
131
+
132
+ It's also possible to fit the normalisation of the model per run. For this use the method create_acceptance_map_per_observation .
133
+ In that case the output is a dictionary containing the acceptance model of each observations (with the observation Id as index).
134
+ ```python
135
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
136
+ offset_axis_acceptance,
137
+ exclude_regions=exclude_regions,
138
+ oversample_map=10)
139
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection)
140
+ ```
141
+
142
+ ## Zenith binned model
143
+
144
+ It's also possible to create model binned per cos zenith. For this use the method create_acceptance_map_per_observation but with the option `zenith_binning` set at True.
145
+ The width of zenith bin could be control at the creation of the object with the parameter `initial_cos_zenith_binning`.
146
+ You can chose the `cos_zenith_binning_method`: `min_livetime` or `min_n_observation` to give a condition on minimum livetime or number of observations for each bin. The algorithm will then automatically rebin to larger bin in order to have in each bin at least `cos_zenith_binning_parameter_value` livetime (in seconds) or observation per bin. If you add `_per_wobble` to the method name, wobbles will be identified and the binning will require the condition to be fulfilled for each identified wobble.
147
+ In that case the output is a dictionary containing the acceptance model of each observations (with the observation ID as index).
148
+ Set `verbose` to True to get the binning result and a plot of the binned livetime.
149
+ ```python
150
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
151
+ offset_axis_acceptance,
152
+ exclude_regions=exclude_regions,
153
+ oversample_map=10,
154
+ cos_zenith_binning_method='min_livetime_per_wobble',
155
+ cos_zenith_binning_parameter_value=3600,
156
+ initial_cos_zenith_binning=0.01,
157
+ verbose=True)
158
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
159
+ zenith_binning=True)
160
+ ```
161
+
162
+ ## Zenith interpolated model
163
+
164
+ It's also possible to create model interpolated between the binned model per cos zenith. For this use the method create_acceptance_map_per_observation but with the option `zenith_interpolation` set at True. All the parameters controlling the cos zenith binning remain active.
165
+ ```python
166
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
167
+ offset_axis_acceptance,
168
+ exclude_regions=exclude_regions,
169
+ oversample_map=10,
170
+ min_run_per_cos_zenith_bin=3,
171
+ initial_cos_zenith_binning=0.01)
172
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
173
+ zenith_binning=True,
174
+ zenith_interpolation=True)
175
+ ```
176
+
177
+ ## Using OFF runs for background model
178
+
179
+ It is also possible to create a model from OFF runs and to apply in ON runs you want to analyse. The exclusions regions should cover potential sources both in the ON and OFF runs at the same time. The OFF runs don't need to be spatially connected.
180
+ ```python
181
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
182
+ offset_axis_acceptance,
183
+ exclude_regions=exclude_regions,
184
+ oversample_map=10,
185
+ min_run_per_cos_zenith_bin=3,
186
+ initial_cos_zenith_binning=0.01)
187
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
188
+ off_observations=obs_collection_off,
189
+ zenith_binning=True,
190
+ zenith_interpolation=True)
191
+ ```
192
+
193
+ ## Store background model for later application
194
+
195
+ It could be in some case useful to precompute a model and to apply it on data later. The example below cover the case where you want to use a model per run using zenith interpolation and OFF runs.
196
+
197
+ However, it's possible to use this functionality without OFF runs or zenith interpolation. In the last case you just need to provide a model in a gammapy format.
198
+
199
+ There are at this stage no define file format for storing the intermediate results, we suggest to store the BackgroundCollectionZenith object created directly using pickle.
200
+
201
+ ### Creating the model
202
+
203
+ The example below creates the `BackgroundCollectionZenith` object containing the zenith binned model.
204
+ The `obs_collection` provided could either be your ON runs if you want to compute background directly from the data or OFF runs if you want to use other data for the background model.
205
+
206
+ ```python
207
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
208
+ offset_axis_acceptance,
209
+ exclude_regions=exclude_regions,
210
+ oversample_map=10,
211
+ min_run_per_cos_zenith_bin=3,
212
+ initial_cos_zenith_binning=0.01)
213
+ base_model = acceptance_model_creator.create_model_cos_zenith_binned(obs_collection)
214
+ ```
215
+
216
+ ### Storing and loading a model with pickle
217
+
218
+ The `BackgroundCollectionZenith` object containing the models could then be store using pickle.
219
+ ```python
220
+ import pickle
221
+ with open('my_bkg_model.pck', mode='wb') as f:
222
+ pickle.dump(base_model, f)
223
+ ```
224
+
225
+ It is then possible to retrieve it later using pickle.
226
+ ```python
227
+ import pickle
228
+ with open('my_bkg_model.pck', mode='rb') as f:
229
+ base_model = pickle.load(f)
230
+ ```
231
+
232
+ ### Applying a model in memory
233
+
234
+ When you have a precomputed model in memory, it is possible to apply it directly on a given set of runs by using the `base_mode` parameter.
235
+ ```python
236
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
237
+ offset_axis_acceptance,
238
+ exclude_regions=exclude_regions)
239
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
240
+ base_model=base_model,
241
+ zenith_binning=True,
242
+ zenith_interpolation=True)
243
+ ```
244
+
245
+ ## Compute background model with a higher time resolution than the observation run
246
+
247
+ If the background evolve quickly, like at high zenith angle, you could compute in the case of zenith binned or interpolated background the model at a smaller time scale than the observation run. The background model for the run will then correspond to the average of all the model computed for each part of the run.
248
+ It should improve accuracy of the model at the expanse of a larger compute time.
249
+ For this you need to set `use_mini_irf_computation = True` and you could control the time resolution used for computation with the parameter `mini_irf_time_resolution`.
250
+
251
+ ```python
252
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
253
+ offset_axis_acceptance,
254
+ exclude_regions=exclude_regions,
255
+ oversample_map=10,
256
+ min_run_per_cos_zenith_bin=3,
257
+ initial_cos_zenith_binning=0.01,
258
+ use_mini_irf_computation = True,
259
+ mini_irf_time_resolution = 1. * u.min)
260
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
261
+ zenith_binning=True,
262
+ zenith_interpolation=True)
263
+ ```
264
+
265
+ # Available model
266
+
267
+ All models have an identical interface. You just need to change the class used to change the model created.
268
+
269
+ There are two model currently available :
270
+
271
+ - A 2D model with hypothesis of a radial symmetry of the background across the FoV. This is the class `RadialAcceptanceMapCreator`.
272
+ ````python
273
+ from baccmod import RadialAcceptanceMapCreator
274
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
275
+ offset_axis_acceptance,
276
+ exclude_regions=exclude_regions)
277
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection)
278
+ ````
279
+ - A 3D model with a regular grid describing the FoV. This is the class `Grid3DAcceptanceMapCreator`.
280
+ ````python
281
+ from baccmod import Grid3DAcceptanceMapCreator
282
+ acceptance_model_creator = Grid3DAcceptanceMapCreator(energy_axis_acceptance,
283
+ offset_axis_acceptance,
284
+ exclude_regions=exclude_regions)
285
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection)
286
+ ````
@@ -0,0 +1,16 @@
1
+ LICENCE.txt
2
+ README.md
3
+ setup.py
4
+ BAccMod.egg-info/PKG-INFO
5
+ BAccMod.egg-info/SOURCES.txt
6
+ BAccMod.egg-info/dependency_links.txt
7
+ BAccMod.egg-info/requires.txt
8
+ BAccMod.egg-info/top_level.txt
9
+ baccmod/__init__.py
10
+ baccmod/base_acceptance_map_creator.py
11
+ baccmod/bkg_collection.py
12
+ baccmod/exception.py
13
+ baccmod/grid3d_acceptance_map_creator.py
14
+ baccmod/modeling.py
15
+ baccmod/radial_acceptance_map_creator.py
16
+ baccmod/toolbox.py
@@ -0,0 +1,6 @@
1
+ gammapy<1.4,>=1.1
2
+ numpy<2.3
3
+ scipy<1.15
4
+ iminuit<3.0,>=2.0
5
+ astropy<8.0,>=4.0
6
+ regions<0.11,>=0.7
@@ -0,0 +1 @@
1
+ baccmod