BAccMod 0.1.0.dev0__py3-none-any.whl
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.
- BAccMod-0.1.0.dev0.dist-info/METADATA +108 -0
- BAccMod-0.1.0.dev0.dist-info/RECORD +13 -0
- BAccMod-0.1.0.dev0.dist-info/WHEEL +5 -0
- BAccMod-0.1.0.dev0.dist-info/top_level.txt +1 -0
- baccmod/__init__.py +1 -0
- baccmod/baccmod.py +174 -0
- baccmod/base_acceptance_map_creator.py +1005 -0
- baccmod/bkg_collection.py +91 -0
- baccmod/exception.py +13 -0
- baccmod/grid3d_acceptance_map_creator.py +349 -0
- baccmod/modeling.py +101 -0
- baccmod/radial_acceptance_map_creator.py +204 -0
- baccmod/toolbox.py +202 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# -------------------------------------------------------------------
|
|
3
|
+
# Filename: radial_acceptance_map_creator.py
|
|
4
|
+
# Purpose: Class for creating background model with spatial circular symmetry rotation
|
|
5
|
+
#
|
|
6
|
+
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
7
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
|
8
|
+
# ---------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
from typing import List, Optional, Tuple
|
|
12
|
+
|
|
13
|
+
import astropy.units as u
|
|
14
|
+
import numpy as np
|
|
15
|
+
from gammapy.data import Observation, Observations
|
|
16
|
+
from gammapy.irf import Background2D
|
|
17
|
+
from gammapy.datasets import MapDataset
|
|
18
|
+
from gammapy.maps import MapAxis, WcsNDMap, WcsGeom
|
|
19
|
+
from regions import CircleAnnulusSkyRegion, CircleSkyRegion, SkyRegion
|
|
20
|
+
|
|
21
|
+
from .base_acceptance_map_creator import BaseAcceptanceMapCreator
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class RadialAcceptanceMapCreator(BaseAcceptanceMapCreator):
|
|
25
|
+
|
|
26
|
+
def __init__(self,
|
|
27
|
+
energy_axis: MapAxis,
|
|
28
|
+
offset_axis: MapAxis,
|
|
29
|
+
oversample_map: int = 10,
|
|
30
|
+
exclude_regions: Optional[List[SkyRegion]] = None,
|
|
31
|
+
cos_zenith_binning_method: str = 'min_livetime',
|
|
32
|
+
cos_zenith_binning_parameter_value: int = 3600,
|
|
33
|
+
initial_cos_zenith_binning: float = 0.01,
|
|
34
|
+
max_angular_separation_wobble: u.Quantity = 0.4 * u.deg,
|
|
35
|
+
zenith_binning_run_splitting: bool = False,
|
|
36
|
+
max_fraction_pixel_rotation_fov: float = 0.5,
|
|
37
|
+
time_resolution: u.Quantity = 0.1 * u.s,
|
|
38
|
+
use_mini_irf_computation: bool = False,
|
|
39
|
+
mini_irf_time_resolution: u.Quantity = 1. * u.min,
|
|
40
|
+
interpolation_type: str = 'linear',
|
|
41
|
+
activate_interpolation_cleaning: bool = False,
|
|
42
|
+
interpolation_cleaning_energy_relative_threshold: float = 1e-4,
|
|
43
|
+
interpolation_cleaning_spatial_relative_threshold: float = 1e-2) -> None:
|
|
44
|
+
"""
|
|
45
|
+
Create the class for calculating radial acceptance model
|
|
46
|
+
This class should be use when strict 2D model is good enough
|
|
47
|
+
|
|
48
|
+
Parameters
|
|
49
|
+
----------
|
|
50
|
+
energy_axis : MapAxis
|
|
51
|
+
The energy axis for the acceptance model
|
|
52
|
+
offset_axis : MapAxis
|
|
53
|
+
The offset axis for the acceptance model
|
|
54
|
+
oversample_map : int, optional
|
|
55
|
+
Oversample in number of pixel of the spatial axis used for the calculation
|
|
56
|
+
exclude_regions : list of regions.SkyRegion, optional
|
|
57
|
+
Region with known or putative gamma-ray emission, will be excluded of the calculation of the acceptance map
|
|
58
|
+
cos_zenith_binning_method : str, optional
|
|
59
|
+
The method used for cos zenith binning: 'min_livetime','min_n_observation'
|
|
60
|
+
cos_zenith_binning_parameter_value : int, optional
|
|
61
|
+
Minimum livetime (in seconds) or number of observations per zenith bins
|
|
62
|
+
initial_cos_zenith_binning : float, optional
|
|
63
|
+
Initial bin size for cos zenith binning
|
|
64
|
+
max_angular_separation_wobble : u.Quantity, optional
|
|
65
|
+
The maximum angular separation between identified wobbles, in degrees
|
|
66
|
+
zenith_binning_run_splitting : bool, optional
|
|
67
|
+
If true, will split each run to match zenith binning for the base model computation
|
|
68
|
+
Could be computationally expensive, especially at high zenith with a high resolution zenith binning
|
|
69
|
+
max_fraction_pixel_rotation_fov : float, optional
|
|
70
|
+
For camera frame transformation the maximum size relative to a pixel a rotation is allowed
|
|
71
|
+
time_resolution : astropy.units.Quantity, optional
|
|
72
|
+
Time resolution to use for the computation of the rotation of the FoV and cut as function of the zenith bins
|
|
73
|
+
use_mini_irf_computation : bool, optional
|
|
74
|
+
If true, in case the case of zenith interpolation or binning, each run will be divided in small subrun (the slicing is based on time).
|
|
75
|
+
A model will be computed for each sub run before averaging them to obtain the final model for the run.
|
|
76
|
+
Should improve the accuracy of the model, especially at high zenith angle.
|
|
77
|
+
mini_irf_time_resolution : astropy.units.Quantity, optional
|
|
78
|
+
Time resolution to use for mini irf used for computation of the final background model
|
|
79
|
+
interpolation_type: str, optional
|
|
80
|
+
Select the type of interpolation to be used, could be either "log" or "linear", log tend to provided better results be could more easily create artefact that will cause issue
|
|
81
|
+
activate_interpolation_cleaning: bool, optional
|
|
82
|
+
If true, will activate the cleaning step after interpolation, it should help to eliminate artefact caused by interpolation
|
|
83
|
+
interpolation_cleaning_energy_relative_threshold: float, optional
|
|
84
|
+
To be considered value, the bin in energy need at least one adjacent bin with a relative difference within this range
|
|
85
|
+
interpolation_cleaning_spatial_relative_threshold: float, optional
|
|
86
|
+
To be considered value, the bin in space need at least one adjacent bin with a relative difference within this range
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
# If no exclusion region, default it as an empty list
|
|
90
|
+
if exclude_regions is None:
|
|
91
|
+
exclude_regions = []
|
|
92
|
+
|
|
93
|
+
# Compute parameters for internal map
|
|
94
|
+
self.offset_axis = offset_axis
|
|
95
|
+
self.oversample_map = oversample_map
|
|
96
|
+
spatial_resolution = np.min(
|
|
97
|
+
np.abs(self.offset_axis.edges[1:] - self.offset_axis.edges[:-1])) / self.oversample_map
|
|
98
|
+
max_offset = np.max(self.offset_axis.edges)
|
|
99
|
+
|
|
100
|
+
# Initiate upper instance
|
|
101
|
+
super().__init__(energy_axis=energy_axis,
|
|
102
|
+
max_offset=max_offset,
|
|
103
|
+
spatial_resolution=spatial_resolution,
|
|
104
|
+
exclude_regions=exclude_regions,
|
|
105
|
+
cos_zenith_binning_method=cos_zenith_binning_method,
|
|
106
|
+
cos_zenith_binning_parameter_value=cos_zenith_binning_parameter_value,
|
|
107
|
+
initial_cos_zenith_binning=initial_cos_zenith_binning,
|
|
108
|
+
max_angular_separation_wobble=max_angular_separation_wobble,
|
|
109
|
+
zenith_binning_run_splitting=zenith_binning_run_splitting,
|
|
110
|
+
max_fraction_pixel_rotation_fov=max_fraction_pixel_rotation_fov,
|
|
111
|
+
time_resolution=time_resolution,
|
|
112
|
+
use_mini_irf_computation=use_mini_irf_computation,
|
|
113
|
+
mini_irf_time_resolution=mini_irf_time_resolution,
|
|
114
|
+
interpolation_type=interpolation_type,
|
|
115
|
+
activate_interpolation_cleaning=activate_interpolation_cleaning,
|
|
116
|
+
interpolation_cleaning_energy_relative_threshold=interpolation_cleaning_energy_relative_threshold,
|
|
117
|
+
interpolation_cleaning_spatial_relative_threshold=interpolation_cleaning_spatial_relative_threshold)
|
|
118
|
+
|
|
119
|
+
def create_acceptance_map(self, observations: Observations) -> Background2D:
|
|
120
|
+
"""
|
|
121
|
+
Calculate a radial acceptance map
|
|
122
|
+
|
|
123
|
+
Parameters
|
|
124
|
+
----------
|
|
125
|
+
observations : Observations
|
|
126
|
+
The collection of observations used to make the acceptance map
|
|
127
|
+
|
|
128
|
+
Returns
|
|
129
|
+
-------
|
|
130
|
+
acceptance_map : Background2D
|
|
131
|
+
"""
|
|
132
|
+
count_map_background, exp_map_background, exp_map_background_total, livetime = self._create_base_computation_map(
|
|
133
|
+
observations)
|
|
134
|
+
|
|
135
|
+
data_background = np.zeros((self.energy_axis.nbin, self.offset_axis.nbin)) * u.Unit('s-1 MeV-1 sr-1')
|
|
136
|
+
for i in range(self.offset_axis.nbin):
|
|
137
|
+
if np.isclose(0. * u.deg, self.offset_axis.edges[i]):
|
|
138
|
+
selection_region = CircleSkyRegion(center=self.center_map, radius=self.offset_axis.edges[i + 1])
|
|
139
|
+
else:
|
|
140
|
+
selection_region = CircleAnnulusSkyRegion(center=self.center_map,
|
|
141
|
+
inner_radius=self.offset_axis.edges[i],
|
|
142
|
+
outer_radius=self.offset_axis.edges[i + 1])
|
|
143
|
+
selection_map = self.geom.to_image().region_mask([selection_region])
|
|
144
|
+
for j in range(self.energy_axis.nbin):
|
|
145
|
+
value = u.dimensionless_unscaled * np.sum(count_map_background.data[j, :, :] * selection_map)
|
|
146
|
+
value *= np.sum(exp_map_background_total.data[j, :, :] * selection_map) / np.sum(
|
|
147
|
+
exp_map_background.data[j, :, :] * selection_map)
|
|
148
|
+
|
|
149
|
+
value /= (self.energy_axis.edges[j + 1] - self.energy_axis.edges[j])
|
|
150
|
+
value /= 2. * np.pi * (
|
|
151
|
+
np.cos(self.offset_axis.edges[i]) - np.cos(self.offset_axis.edges[i + 1])) * u.steradian
|
|
152
|
+
value /= livetime
|
|
153
|
+
data_background[j, i] = value
|
|
154
|
+
|
|
155
|
+
acceptance_map = Background2D(axes=[self.energy_axis, self.offset_axis], data=data_background)
|
|
156
|
+
|
|
157
|
+
return acceptance_map
|
|
158
|
+
|
|
159
|
+
def _create_base_computation_map(self, observations: Observation) -> Tuple[WcsNDMap, WcsNDMap, WcsNDMap, u.Quantity]:
|
|
160
|
+
"""
|
|
161
|
+
From a list of observations return a stacked finely binned counts and exposure map in camera frame to compute a
|
|
162
|
+
model
|
|
163
|
+
|
|
164
|
+
Parameters
|
|
165
|
+
----------
|
|
166
|
+
observations : gammapy.data.observations.Observations
|
|
167
|
+
The list of observations
|
|
168
|
+
|
|
169
|
+
Returns
|
|
170
|
+
-------
|
|
171
|
+
count_map_background : gammapy.map.WcsNDMap
|
|
172
|
+
The count map
|
|
173
|
+
exp_map_background : gammapy.map.WcsNDMap
|
|
174
|
+
The exposure map corrected for exclusion regions
|
|
175
|
+
exp_map_background_total : gammapy.map.WcsNDMap
|
|
176
|
+
The exposure map without correction for exclusion regions
|
|
177
|
+
livetime : astropy.unit.Quantity
|
|
178
|
+
The total exposure time for the model
|
|
179
|
+
"""
|
|
180
|
+
count_map_background = WcsNDMap(geom=self.geom)
|
|
181
|
+
exp_map_background = WcsNDMap(geom=self.geom, unit=u.s)
|
|
182
|
+
exp_map_background_total = WcsNDMap(geom=self.geom, unit=u.s)
|
|
183
|
+
livetime = 0. * u.s
|
|
184
|
+
|
|
185
|
+
for obs in observations:
|
|
186
|
+
geom = WcsGeom.create(skydir=obs.pointing.fixed_icrs, npix=(self.n_bins_map, self.n_bins_map),
|
|
187
|
+
binsz=self.spatial_bin_size, frame="icrs", axes=[self.energy_axis])
|
|
188
|
+
count_map_obs, exclusion_mask = self._create_map(obs, geom, self.exclude_regions, add_bkg=False)
|
|
189
|
+
|
|
190
|
+
exp_map_obs = MapDataset.create(geom=count_map_obs.geoms['geom'])
|
|
191
|
+
exp_map_obs_total = MapDataset.create(geom=count_map_obs.geoms['geom'])
|
|
192
|
+
exp_map_obs.counts.data = obs.observation_live_time_duration.value
|
|
193
|
+
exp_map_obs_total.counts.data = obs.observation_live_time_duration.value
|
|
194
|
+
|
|
195
|
+
for i in range(count_map_obs.counts.data.shape[0]):
|
|
196
|
+
count_map_obs.counts.data[i, :, :] = count_map_obs.counts.data[i, :, :] * exclusion_mask
|
|
197
|
+
exp_map_obs.counts.data[i, :, :] = exp_map_obs.counts.data[i, :, :] * exclusion_mask
|
|
198
|
+
|
|
199
|
+
count_map_background.data += count_map_obs.counts.data
|
|
200
|
+
exp_map_background.data += exp_map_obs.counts.data
|
|
201
|
+
exp_map_background_total.data += exp_map_obs_total.counts.data
|
|
202
|
+
livetime += obs.observation_live_time_duration
|
|
203
|
+
|
|
204
|
+
return count_map_background, exp_map_background, exp_map_background_total, livetime
|
baccmod/toolbox.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# -------------------------------------------------------------------
|
|
3
|
+
# Filename: toolbox.py
|
|
4
|
+
# Purpose: Collection of basic functions for other part of the program
|
|
5
|
+
#
|
|
6
|
+
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
7
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
|
8
|
+
# ---------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
import astropy.units as u
|
|
12
|
+
from astropy.coordinates import AltAz, SkyCoord, angular_separation
|
|
13
|
+
from astropy.coordinates.earth import OMEGA_EARTH, EarthLocation
|
|
14
|
+
from astropy.time import Time
|
|
15
|
+
from gammapy.data import Observations, Observation
|
|
16
|
+
from copy import deepcopy
|
|
17
|
+
import logging
|
|
18
|
+
import numpy as np
|
|
19
|
+
|
|
20
|
+
logger = logging.getLogger(__name__)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def compute_rotation_speed_fov(time_evaluation: Time,
|
|
24
|
+
pointing_sky: SkyCoord,
|
|
25
|
+
observatory_earth_location: EarthLocation) -> u.Quantity:
|
|
26
|
+
"""
|
|
27
|
+
Compute the rotation speed of the FOV for a given evaluation time.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
time_evaluation : astropy.time.Time
|
|
32
|
+
The time at which the rotation speed should be evaluated.
|
|
33
|
+
pointing_sky : astropy.coordinates.SkyCoord
|
|
34
|
+
The direction pointed in the sky.
|
|
35
|
+
observatory_earth_location : astropy.coordinates.EarthLocation
|
|
36
|
+
The position of the observatory.
|
|
37
|
+
|
|
38
|
+
Returns
|
|
39
|
+
-------
|
|
40
|
+
rotation_speed : astropy.units.Quantity
|
|
41
|
+
The rotation speed of the FOV at the given time and pointing direction.
|
|
42
|
+
"""
|
|
43
|
+
pointing_altaz = pointing_sky.transform_to(AltAz(obstime=time_evaluation,
|
|
44
|
+
location=observatory_earth_location))
|
|
45
|
+
omega_earth = OMEGA_EARTH * u.rad
|
|
46
|
+
omega = omega_earth * np.cos(observatory_earth_location.lat) * np.cos(pointing_altaz.az) / np.cos(
|
|
47
|
+
pointing_altaz.alt)
|
|
48
|
+
return omega
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def get_unique_wobble_pointings(observations: Observations, max_angular_separation_wobble=0.4 * u.deg):
|
|
52
|
+
"""
|
|
53
|
+
Compute the angular separation between pointings and return a list
|
|
54
|
+
of detected wobbles with their associated similar pointings
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
observations : gammapy.data.observations.Observations
|
|
59
|
+
The list of observations
|
|
60
|
+
max_angular_separation_wobble : u.Quantity
|
|
61
|
+
The maximum angular separation between a wobble position and associated runs, in degrees
|
|
62
|
+
|
|
63
|
+
Returns
|
|
64
|
+
-------
|
|
65
|
+
unique_wobble_list : `numpy.array`
|
|
66
|
+
Array of wobble name associated with each run
|
|
67
|
+
"""
|
|
68
|
+
all_ra_observations = np.array([obs.get_pointing_icrs(obs.tmid).ra.to_value(u.deg) for obs in observations]) * u.deg
|
|
69
|
+
all_dec_observations = np.array(
|
|
70
|
+
[obs.get_pointing_icrs(obs.tmid).dec.to_value(u.deg) for obs in observations]) * u.deg
|
|
71
|
+
ra_observations = deepcopy(all_ra_observations)
|
|
72
|
+
dec_observations = deepcopy(all_dec_observations)
|
|
73
|
+
wobbles = np.empty(shape=len(all_ra_observations), dtype=np.object_)
|
|
74
|
+
wobbles_dict = {}
|
|
75
|
+
i = 0
|
|
76
|
+
mask_all_remaining = np.ones(shape=len(all_ra_observations), dtype=bool)
|
|
77
|
+
while len(ra_observations) > 0:
|
|
78
|
+
i = i + 1
|
|
79
|
+
keywobble = 'W' + str(i)
|
|
80
|
+
mask = (angular_separation(ra_observations[0], dec_observations[0],
|
|
81
|
+
ra_observations, dec_observations) < max_angular_separation_wobble)
|
|
82
|
+
mask_2 = (angular_separation(np.mean(ra_observations[mask]),
|
|
83
|
+
np.mean(dec_observations[mask]),
|
|
84
|
+
all_ra_observations,
|
|
85
|
+
all_dec_observations) < max_angular_separation_wobble)
|
|
86
|
+
wobbles_dict[keywobble] = [np.mean(all_ra_observations[mask_2 & mask_all_remaining]),
|
|
87
|
+
np.mean(all_dec_observations[mask_2 & mask_all_remaining])]
|
|
88
|
+
wobbles[mask_2 & mask_all_remaining] = keywobble
|
|
89
|
+
mask_all_remaining = mask_all_remaining * ~mask_2
|
|
90
|
+
ra_observations = all_ra_observations[mask_all_remaining]
|
|
91
|
+
dec_observations = all_dec_observations[mask_all_remaining]
|
|
92
|
+
|
|
93
|
+
logger.info(f"{len(wobbles_dict)} wobbles were found:")
|
|
94
|
+
for key, value in wobbles_dict.items():
|
|
95
|
+
logger.info(f"{key}: [{value[0].round(2)}, {value[1].round(2)}]")
|
|
96
|
+
return wobbles
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def get_time_mini_irf(observation: Observation, mini_irf_time_resolution: u.Quantity):
|
|
100
|
+
"""
|
|
101
|
+
Compute the evaluation time for each mini irfs
|
|
102
|
+
|
|
103
|
+
Parameters
|
|
104
|
+
----------
|
|
105
|
+
observation : gammapy.data.observations.Observation
|
|
106
|
+
The observation for which mini irf will be generated
|
|
107
|
+
mini_irf_time_resolution : u.Quantity
|
|
108
|
+
The targeted time resolution for the mini irfs
|
|
109
|
+
|
|
110
|
+
Returns
|
|
111
|
+
-------
|
|
112
|
+
evaluation_time : u.Quantity
|
|
113
|
+
An array of the evaluation time of each mini irf, correspond to the central time of the bin
|
|
114
|
+
observation_time : u.Quantity
|
|
115
|
+
The observation time associated with each mini irf
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
total_observation_duration = observation.tstop - observation.tstart
|
|
119
|
+
nb_bin = int(np.ceil((total_observation_duration / mini_irf_time_resolution).to_value(u.dimensionless_unscaled)))
|
|
120
|
+
bin_duration = total_observation_duration / nb_bin
|
|
121
|
+
evaluation_time = Time(np.zeros(nb_bin), format='unix')
|
|
122
|
+
observation_time = np.zeros(nb_bin) * mini_irf_time_resolution.unit
|
|
123
|
+
for i in range(nb_bin):
|
|
124
|
+
evaluation_time[i] = observation.tstart + (i + 0.5) * bin_duration
|
|
125
|
+
observation_time[i] = bin_duration
|
|
126
|
+
|
|
127
|
+
return evaluation_time, observation_time
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def generate_irf_from_mini_irf(data_cube: np.array, observation_time: u.Quantity):
|
|
131
|
+
"""
|
|
132
|
+
Compute the final irf from the collection of mini irfs
|
|
133
|
+
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
data_cube : numpy.array
|
|
137
|
+
The data cube containing all the mini irfs. Axis 0 should be the mini irfs axis
|
|
138
|
+
observation_time : u.Quantity
|
|
139
|
+
The observation time for each bin
|
|
140
|
+
|
|
141
|
+
Returns
|
|
142
|
+
-------
|
|
143
|
+
data_cube_final : u.Quantity
|
|
144
|
+
The data cube containing the final irf
|
|
145
|
+
"""
|
|
146
|
+
|
|
147
|
+
# Compute the scale factor for each mini irf to get a weighted average
|
|
148
|
+
scale_factor_per_bin = (observation_time / np.sum(observation_time)).to_value(u.dimensionless_unscaled)
|
|
149
|
+
|
|
150
|
+
# Reshape the scale factor in order to be able to multiply it to the data cube
|
|
151
|
+
scale_factor_per_bin_reshaped = scale_factor_per_bin.reshape(
|
|
152
|
+
(scale_factor_per_bin.size, *[1 for _ in range(data_cube.ndim - 1)]))
|
|
153
|
+
|
|
154
|
+
# Compute the weighted average of all mini irfs
|
|
155
|
+
data_cube_final = np.sum(data_cube * scale_factor_per_bin_reshaped, axis=0)
|
|
156
|
+
|
|
157
|
+
return data_cube_final
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def compute_neighbour_condition_validation(a, axis, relative_threshold):
|
|
161
|
+
"""
|
|
162
|
+
For each element of the array, will test the number of neighbour value on the given axis that are within the relative range provided
|
|
163
|
+
|
|
164
|
+
Parameters
|
|
165
|
+
----------
|
|
166
|
+
a : numpy.array
|
|
167
|
+
The data block on which perform the test
|
|
168
|
+
axis : int
|
|
169
|
+
The axis on which the calculation will be performed
|
|
170
|
+
relative_threshold: float
|
|
171
|
+
The maximum ratio between neighbour value authorized
|
|
172
|
+
|
|
173
|
+
Returns
|
|
174
|
+
-------
|
|
175
|
+
interp_bkg : numpy.array
|
|
176
|
+
The object that could be call directly for performing the interpolation
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
# Create results table
|
|
180
|
+
count_neighbour_condition_valid = np.zeros(a.shape, dtype=np.uint8)
|
|
181
|
+
|
|
182
|
+
# Define the slice to access array for computation
|
|
183
|
+
slice_down = [slice(None)] * a.ndim
|
|
184
|
+
slice_up = [slice(None)] * a.ndim
|
|
185
|
+
slice_down[axis] = slice(None, -1)
|
|
186
|
+
slice_up[axis] = slice(1, None)
|
|
187
|
+
slice_down = tuple(slice_down)
|
|
188
|
+
slice_up = tuple(slice_up)
|
|
189
|
+
|
|
190
|
+
# Compute the relative difference
|
|
191
|
+
relative_value_up = a[slice_up]/a[slice_down]
|
|
192
|
+
relative_value_down = a[slice_down] / a[slice_up]
|
|
193
|
+
|
|
194
|
+
# Compute if neighbour condition is validated for up and down
|
|
195
|
+
neighbour_condition_up = np.logical_and(relative_value_up > relative_threshold, relative_value_up < (1./relative_threshold))
|
|
196
|
+
neighbour_condition_down = np.logical_and(relative_value_down > relative_threshold, relative_value_down < (1./relative_threshold))
|
|
197
|
+
|
|
198
|
+
# Compute for each case the number of time the condition is valid
|
|
199
|
+
count_neighbour_condition_valid[slice_up] += neighbour_condition_up.astype(np.uint8)
|
|
200
|
+
count_neighbour_condition_valid[slice_down] += neighbour_condition_down.astype(np.uint8)
|
|
201
|
+
|
|
202
|
+
return count_neighbour_condition_valid
|