BAccMod 0.3.0__tar.gz → 0.4.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.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.4
2
2
  Name: BAccMod
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Calculate 2D and 3d acceptance model for creating maps with gammapy (IACT analysis)
5
5
  Home-page: https://github.com/mdebony/BAccMod
6
6
  Author: Mathieu de Bony de Lavergne, Gabriel Emery, Marie-Sophie Carrasco
@@ -8,12 +8,21 @@ Author-email: debony@cppm.in2p3.fr
8
8
  License: LGPL v3
9
9
  Description-Content-Type: text/markdown
10
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
11
+ Requires-Dist: gammapy<2.1,>=1.1
12
+ Requires-Dist: numpy<2.4
13
+ Requires-Dist: scipy<1.17
14
14
  Requires-Dist: iminuit<3.0,>=2.0
15
15
  Requires-Dist: astropy<8.0,>=4.0
16
16
  Requires-Dist: regions<0.11,>=0.7
17
+ Dynamic: author
18
+ Dynamic: author-email
19
+ Dynamic: description
20
+ Dynamic: description-content-type
21
+ Dynamic: home-page
22
+ Dynamic: license
23
+ Dynamic: license-file
24
+ Dynamic: requires-dist
25
+ Dynamic: summary
17
26
 
18
27
  # Description
19
28
 
@@ -39,7 +48,14 @@ Dependencies :
39
48
 
40
49
  ## Basic use
41
50
 
42
- You could first create the acceptance (or background) model
51
+ User interface is through two main functions available in any of the modelisation classes.
52
+ One function allows for the creation of a background IRF model or collection of models which can be used in later steps.
53
+ The second function applies a model, either created dynamically or created before, to observations to associate a new
54
+ background IRF to each one.
55
+
56
+ ### Example
57
+ Below is an example of the creation of a single background model with radial symmetry using directly the observations of
58
+ interest, and the application of this model to all the observations. This is not the adviced usage of the package.
43
59
 
44
60
  ```python
45
61
  from gammapy.maps import MapAxis
@@ -62,12 +78,14 @@ e_min, e_max = 0.1 * u.TeV, 10. * u.TeV
62
78
  size_fov = 2.5 * u.deg
63
79
  offset_axis_acceptance = MapAxis.from_bounds(0. * u.deg, size_fov, nbin=6, name='offset')
64
80
  energy_axis_acceptance = MapAxis.from_energy_bounds(e_min, e_max, nbin=6, name='energy')
65
-
81
+ ```
82
+ First step is to create an instance of one of the model creation class. Here the `RadialAcceptanceMapCreator` to which
83
+ we provide minimal inputs : the axis binning and exclusion region to remove the Crab Nebula.
84
+ ```python
66
85
  acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
67
86
  offset_axis_acceptance,
68
- exclude_regions=exclude_regions,
69
- oversample_map=10)
70
- acceptance_model = acceptance_model_creator.create_acceptance_map(obs_collection)
87
+ exclude_regions=exclude_regions)
88
+ acceptance_model = acceptance_model_creator.create_model(obs_collection)
71
89
 
72
90
  ```
73
91
 
@@ -85,7 +103,8 @@ hdu_acceptance.writeto('acceptance.fits', overwrite=True)
85
103
  ```
86
104
 
87
105
  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.
106
+ You would need then to recreate your gammapy Observations object in order than the acceptance model is taken into
107
+ account for the analysis.
89
108
 
90
109
  ```python
91
110
  data_store.hdu_table.remove_rows(data_store.hdu_table['HDU_TYPE']=='bkg')
@@ -108,6 +127,132 @@ data_store.hdu_table
108
127
  > 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
128
 
110
129
 
130
+ ### Available model
131
+
132
+ All models have an identical interface. You just need to change the class used to change the model created.
133
+
134
+ There are two model currently available :
135
+
136
+ - A 2D model with hypothesis of a radial symmetry of the background across the FoV. This is the class `RadialAcceptanceMapCreator`.
137
+ ````python
138
+ from baccmod import RadialAcceptanceMapCreator
139
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
140
+ offset_axis_acceptance,
141
+ exclude_regions=exclude_regions)
142
+ ````
143
+ - A 3D model with a regular grid describing the FoV. This is the class `Grid3DAcceptanceMapCreator`. This class support
144
+ the use of either a direct event stacking or the fitting of spatial models to create the IRF.
145
+ ````python
146
+ from baccmod import Grid3DAcceptanceMapCreator
147
+ acceptance_model_creator = Grid3DAcceptanceMapCreator(energy_axis_acceptance,
148
+ offset_axis_acceptance,
149
+ exclude_regions=exclude_regions,
150
+ method='stack' or 'fit' (default is 'stack'))
151
+ ````
152
+
153
+ Each class accepts a set of optionnal parameters described in its docstring. The majority are common and allow to
154
+ activate or control options such as splitting of the data in azimuth or in zenith bins, and interpolation. Some are
155
+ described later in this document.
156
+
157
+ ### Model creation
158
+
159
+ Once the acceptance_model_creator is initated, the method `create_acceptance_model` can be used to create and output
160
+ a background model. While the stteings of the zenith binning or interpolation are defined during the class object
161
+ creation, the decision to use one or the other is done at the time of the call :
162
+
163
+ ````python
164
+ model = acceptance_model_creator.create_acceptance_model(# observations used to create the models
165
+ off_observations,
166
+ # Activates the zenith binning
167
+ zenith_binning = False
168
+ )
169
+ ````
170
+
171
+ A model created with zenith binning will be valid for application with the zenith interpolation later.
172
+
173
+ ### Observation-wise IRF application
174
+
175
+ To associate a background IRF to a set of observations, the user should use the function
176
+ `create_acceptance_map_per_observation`. It can either, and in this order of priority, apply a provided model,
177
+ create a model from off observations and apply it, or create a model fron the target observation and apply it.
178
+
179
+ ````python
180
+ # acceptance_map will be a dictionnary of obs_id to BackgroundIRF
181
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(
182
+ observations, # Target observations
183
+ zenith_binning = False, # Activates the zenith binning (ignored if interpolating)
184
+ zenith_interpolation = False, # Activates the zenith interpolation
185
+ runwise_normalisation = True, # If True, the IRF for the target observations is renormalised to the observed counts
186
+ off_observations = None, # observations to use to create a model if none is provided. If None, `observations`is used.
187
+ base_model = None # model to apply if provided
188
+ )
189
+
190
+ ````
191
+
192
+ ## Logging setup
193
+
194
+ The module uses `logging` to output various information to the user. Standard `logging` functionalities are supported.
195
+
196
+ In addition, some helper functions are implemented and BAccMod uses a logging level **MOREINFO** with value 15 between the `logging` INFO and DEBUG levels.
197
+
198
+ Logging functions can be used as follow :
199
+
200
+ ```python
201
+ from baccmod.logging import MOREINFO, set_log_level
202
+
203
+ set_log_level(level= MOREINFO, # Supports both int and string representation, e.g.
204
+ # 15, MOREINFO and 'MOREINFO' would be equivalent
205
+ module= 'baccmod' # default 'baccmod', 'baccmod.' is automatically
206
+ # added if missing so users can provide a
207
+ # submodule directly
208
+ )
209
+
210
+ # Example usage :
211
+ set_log_level(logging.INFO)
212
+ set_log_level('WARNING')
213
+ set_log_level(20, 'grid3d_acceptance_map_creator')
214
+ # equivalent to
215
+ set_log_level(level=logging.INFO,
216
+ module='baccmod.grid3d_acceptance_map_creator')
217
+
218
+ ```
219
+
220
+ The `set_log_level` function has some simple usages show below but takes a number of optionnal parameters for more advance configuration. Its signature is :
221
+ ```python
222
+ setup_logging_output(output_file=None, use_terminal=True,
223
+ handlers=None, module='baccmod',
224
+ fmode='w', term_level=logging.NOTSET,
225
+ log_format='%(levelname)s:%(name)s: %(message)s')
226
+ ```
227
+
228
+ Some usages :
229
+
230
+ ```python
231
+ from baccmod.logging import set_log_level, setup_logging_output
232
+
233
+ # Setup the output of logging to the provided file. Keep output in terminal.
234
+ setup_logging_output(output_file='/path/to/log_file.log')
235
+
236
+ # Setup the output of logging to the provided file. No output in terminal.
237
+ setup_logging_output(output_file='/path/to/log_file.log', use_terminal=False)
238
+
239
+ # Setup a file output with a global log level of INFO,
240
+ # but only output WARNING and above levels to the terminal
241
+ set_log_level('INFO')
242
+ setup_logging_output(output_file='/path/to/log_file.log',
243
+ use_terminal=True,
244
+ term_level='WARNING')
245
+ ```
246
+ `module` can be used to define the logging for a submodule only.
247
+
248
+ `fmode` defines the writing strategy for the log file (default overwrites).
249
+
250
+ `log_format` can be used to change the format of the messages.
251
+
252
+ `handlers` (advanced users) allow to directly provide a list of Handlers instead of the one created by the `output_file` and `use_terminal` options.
253
+
254
+
255
+
111
256
  ## Telescope position
112
257
 
113
258
  **The observations should contain the telescope position in order to have the algorithm working.**
@@ -127,25 +272,19 @@ for i in obs_collection:
127
272
  obs_collection[i]._location = loc
128
273
  ```
129
274
 
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
275
  ## Zenith binned model
143
276
 
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.
277
+ It's also possible to create model binned per cos zenith. For this in the methods create_acceptance_model or
278
+ create_acceptance_map_per_observation set the option `zenith_binning` at True.
279
+ The minimum width of zenith bin can be controlled at the creation of the object with the parameter
280
+ `initial_cos_zenith_binning`. You can chose the `cos_zenith_binning_method`: `min_livetime` or `min_n_observation`
281
+ to give a condition on minimum livetime or number of observations for each bin. The algorithm will then automatically
282
+ rebin to larger bin in order to have in each bin at least `cos_zenith_binning_parameter_value` livetime (in seconds) or
283
+ observation per bin. If you add `_per_wobble` to the method name, wobbles will be identified and the binning will
284
+ require the condition to be fulfilled for each identified wobble.
285
+ (with the observation ID as index). Set `verbose` to True to get the binning result and a plot of the binned livetime.
286
+
287
+ Example :
149
288
  ```python
150
289
  acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
151
290
  offset_axis_acceptance,
@@ -153,37 +292,37 @@ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
153
292
  oversample_map=10,
154
293
  cos_zenith_binning_method='min_livetime_per_wobble',
155
294
  cos_zenith_binning_parameter_value=3600,
156
- initial_cos_zenith_binning=0.01,
157
- verbose=True)
295
+ initial_cos_zenith_binning=0.01)
158
296
  acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
159
297
  zenith_binning=True)
160
298
  ```
161
299
 
162
300
  ## Zenith interpolated model
163
301
 
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.
302
+ It's also possible to create model interpolated between the binned model per cos zenith.
303
+ For this use the method create_acceptance_map_per_observation but with the option `zenith_interpolation` set at True.
304
+ All the parameters controlling the cos zenith binning remain active.
165
305
  ```python
166
306
  acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
167
307
  offset_axis_acceptance,
168
308
  exclude_regions=exclude_regions,
169
309
  oversample_map=10,
170
310
  min_run_per_cos_zenith_bin=3,
171
- initial_cos_zenith_binning=0.01)
311
+ initial_cos_zenith_binning=0.01,
312
+ interpolation_zenith_type = 'linear' or 'log')
172
313
  acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
173
- zenith_binning=True,
314
+ zenith_binning=True, # ignored
174
315
  zenith_interpolation=True)
175
316
  ```
176
317
 
177
318
  ## Using OFF runs for background model
178
319
 
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.
320
+ It is possible to create a model from OFF runs and to apply in ON runs you want to analyse.
321
+ The exclusions regions should cover potential sources both in the ON and OFF runs at the same time,
322
+ or the model can be created beforehand.
323
+ The OFF runs don't need to be spatially connected.
180
324
  ```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)
325
+
187
326
  acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection,
188
327
  off_observations=obs_collection_off,
189
328
  zenith_binning=True,
@@ -210,7 +349,8 @@ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
210
349
  oversample_map=10,
211
350
  min_run_per_cos_zenith_bin=3,
212
351
  initial_cos_zenith_binning=0.01)
213
- base_model = acceptance_model_creator.create_model_cos_zenith_binned(obs_collection)
352
+ base_model = acceptance_model_creator.create_acceptance_model(obs_collection,
353
+ zenith_binning=True)
214
354
  ```
215
355
 
216
356
  ### Storing and loading a model with pickle
@@ -262,25 +402,17 @@ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observati
262
402
  zenith_interpolation=True)
263
403
  ```
264
404
 
265
- # Available model
405
+ ## Compute background model with a non regular energy binning
266
406
 
267
- All models have an identical interface. You just need to change the class used to change the model created.
407
+ For all models it's possible to compute model on a non regular energy binning. The output model will then be interpolated to the energy_axis provided. The main advantage of this feature is too allow for a finner binning at low energy where they are a high statistics available and coarser one at high energy where less events statistics is available. This energy binning should be passed through the energy_axis_computation argument when creating object.
408
+ An example :
409
+ ```python
410
+ energy_axis_computation = MapAxis.from_energy_edges((list(np.geomspace(0.05, 0.5, 6)) + list(np.geomspace(0.5, 5, 3)[1:])) * u.TeV, name='energy')
411
+ acceptance_model_creator = RadialAcceptanceMapCreator(energy_axis_acceptance,
412
+ offset_axis_acceptance,
413
+ exclude_regions=exclude_regions,
414
+ oversample_map=10)
415
+ acceptance_models = acceptance_model_creator.create_acceptance_map_per_observation(obs_collection)
416
+ ```
268
417
 
269
- There are two model currently available :
270
418
 
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
- ````
@@ -11,6 +11,7 @@ baccmod/base_acceptance_map_creator.py
11
11
  baccmod/bkg_collection.py
12
12
  baccmod/exception.py
13
13
  baccmod/grid3d_acceptance_map_creator.py
14
+ baccmod/logging.py
14
15
  baccmod/modeling.py
15
16
  baccmod/radial_acceptance_map_creator.py
16
17
  baccmod/toolbox.py
@@ -1,6 +1,6 @@
1
- gammapy<1.4,>=1.1
2
- numpy<2.3
3
- scipy<1.15
1
+ gammapy<2.1,>=1.1
2
+ numpy<2.4
3
+ scipy<1.17
4
4
  iminuit<3.0,>=2.0
5
5
  astropy<8.0,>=4.0
6
6
  regions<0.11,>=0.7