@@ -36,13 +36,10 @@ def __init__(self, name = None):
3636 self ._image_response = None # histpy.Histogram (dense)
3737
3838 # None if using Galactic CDS, mandotary if using local CDS
39- self ._coordsys_conv_matrix = None
40-
41- # optional
42- self .is_miniDC2_format = False #should be removed in the future
39+ self ._coordsys_conv_matrix = None
4340
4441 @classmethod
45- def load (cls , name , event_binned_data , dict_bkg_binned_data , rsp , coordsys_conv_matrix = None , is_miniDC2_format = False ):
42+ def load (cls , name , event_binned_data , dict_bkg_binned_data , rsp , coordsys_conv_matrix = None ):
4643 """
4744 Load data
4845
@@ -57,9 +54,7 @@ def load(cls, name, event_binned_data, dict_bkg_binned_data, rsp, coordsys_conv_
5754 rsp : :py:class:`histpy.Histogram` or :py:class:`cosipy.response.FullDetectorResponse`
5855 Response
5956 coordsys_conv_matrix : :py:class:`cosipy.image_deconvolution.CoordsysConversionMatrix`, default False
60- Coordsys conversion matrix
61- is_miniDC2_format : bool, default False
62- Whether the file format is for mini-DC2. It will be removed in the future.
57+ Coordsys conversion matrix
6358
6459 Returns
6560 -------
@@ -87,24 +82,22 @@ def load(cls, name, event_binned_data, dict_bkg_binned_data, rsp, coordsys_conv_
8782 if new ._coordsys_conv_matrix is not None and \
8883 new ._coordsys_conv_matrix .is_sparse :
8984 new ._coordsys_conv_matrix .contents .enable_caching ()
90-
91- new .is_miniDC2_format = is_miniDC2_format
9285
9386 if isinstance (rsp , FullDetectorResponse ):
9487 logger .info ('Loading the response matrix onto your computer memory...' )
95- new ._load_full_detector_response_on_memory ( rsp , is_miniDC2_format )
88+ new ._image_response = rsp . to_dr ( )
9689 logger .info ('Finished' )
9790 elif isinstance (rsp , Histogram ):
9891 new ._image_response = rsp
99-
92+
10093 # We modify the axes in event, bkg_models, response. This is only for DC2.
10194 new ._modify_axes ()
102-
95+
10396 new ._data_axes = new ._event .axes
104-
97+
10598 if new ._coordsys_conv_matrix is None :
10699 axes = (new ._image_response .axes ['NuLambda' ].copy (), new ._image_response .axes ['Ei' ]) # will mutate axes[0]
107- axes [0 ].label = 'lb'
100+ axes [0 ].label = 'lb'
108101 # The gamma-ray direction of pre-computed response in DC2 is in the galactic coordinate, not in the local coordinate.
109102 # Actually, it is labeled as 'NuLambda'. So I replace it with 'lb'.
110103 new ._model_axes = Axes (axes , copy_axes = False )
@@ -134,39 +127,35 @@ def _modify_axes(self):
134127 for name in axis_name :
135128
136129 logger .info (f"... checking the axis { name } of the event and background files..." )
137-
130+
138131 event_edges , event_unit = self ._event .axes [name ].edges , self ._event .axes [name ].unit
139132
140133 for key in self ._bkg_models :
141134
142135 bkg_edges , bkg_unit = self ._bkg_models [key ].axes [name ].edges , self ._bkg_models [key ].axes [name ].unit
143136
144137 if np .all (event_edges == bkg_edges ):
145- logger .info (f" --> pass (edges)" )
138+ logger .info (f" --> pass (edges)" )
146139 else :
147140 logger .error (f"Warning: the edges of the axis { name } are not consistent between the event and the background model { key } !" )
148141 logger .error (f" event : { event_edges } " )
149142 logger .error (f" background : { bkg_edges } " )
150143 raise ValueError
151144
152- # check the axes of the event/response files.
145+ # check the axes of the event/response files.
153146 # Note that currently (2023-08-29) no unit is stored in the binned data. So only the edges are compared. This should be modified in the future.
154147
155148 axis_name = ['Em' , 'Phi' , 'PsiChi' ]
156-
149+
157150 for name in axis_name :
158151
159152 logger .info (f"...checking the axis { name } of the event and response files..." )
160153
161154 event_edges , event_unit = self ._event .axes [name ].edges , self ._event .axes [name ].unit
162155 response_edges , response_unit = self ._image_response .axes [name ].edges , self ._image_response .axes [name ].unit
163-
164- # if type(response_edges) == u.quantity.Quantity and self.is_miniDC2_format == True:
165- if event_unit is None and response_unit is not None and self .is_miniDC2_format == True : # this is only for the old data in the miniDC2 challenge. I will remove them in the near future (or in the final dataIF).
166- response_edges = response_edges .value
167156
168157 if np .all (event_edges == response_edges ):
169- logger .info (f" --> pass (edges)" )
158+ logger .info (f" --> pass (edges)" )
170159 else :
171160 logger .error (f"Warning: the edges of the axis { name } are not consistent between the event and background!" )
172161 logger .error (f" event : { event_edges } " )
@@ -184,12 +173,12 @@ def _modify_axes(self):
184173 self ._image_response .axes ["Phi" ], \
185174 self ._image_response .axes ["PsiChi" ]),
186175 copy_axes = False )
187-
176+
188177 self ._event = Histogram (axes_cds ,
189178 contents = self ._event .contents ,
190179 unit = self ._event .unit ,
191180 copy_contents = False ) # overwrite axes of existing Histogram
192-
181+
193182 for key in self ._bkg_models :
194183 bkg_model = self ._bkg_models [key ]
195184 self ._bkg_models [key ] = Histogram (axes_cds ,
@@ -201,36 +190,13 @@ def _modify_axes(self):
201190
202191 return True
203192
204- def _load_full_detector_response_on_memory (self , full_detector_response , is_miniDC2_format ):
205- """
206- Load a response file on the computer memory.
207- """
208-
209- axes_image_response = Axes ((full_detector_response .axes ["NuLambda" ],
210- full_detector_response .axes ["Ei" ],
211- full_detector_response .axes ["Em" ],
212- full_detector_response .axes ["Phi" ],
213- full_detector_response .axes ["PsiChi" ]),
214- copy_axes = False )
215-
216- if is_miniDC2_format :
217- npix = axes_image_response ["NuLambda" ].npix
218- slices = [ np .sum (full_detector_response [ipix ].to_dense (), axis = (4 ,5 )) for ipix in tqdm (range (npix )) ] #Ei, Em, Phi, ChiPsi
219- contents = np .stack (slices )
220- else :
221- contents = full_detector_response ._file ['DRM' ]['CONTENTS' ]
222-
223- self ._image_response = Histogram (axes_image_response , contents = contents ,
224- unit = full_detector_response .unit ,
225- copy_contents = False )
226-
227193 def _calc_exposure_map (self ):
228194 """
229195 Calculate exposure_map, which is an intermediate matrix used in RL algorithm.
230196 """
231197
232198 logger .info ("Calculating an exposure map..." )
233-
199+
234200 if self ._coordsys_conv_matrix is None :
235201 exposure_map = np .sum (self ._image_response .contents , axis = (2 ,3 ,4 ))
236202 else :
@@ -275,7 +241,7 @@ def calc_expectation(self, model, dict_bkg_norm = None, almost_zero = 1e-12):
275241 # This is just because in DC2 the rotate response for galactic coordinate CDS does not have an axis for time/scatt binning.
276242 # However it is likely that it will have such an axis in the future in order to consider background variability depending on time and pointing direction etc.
277243 # Then, the implementation here will not work. Thus, keep in mind that we need to modify it once the response format is fixed.
278-
244+
279245 if self ._coordsys_conv_matrix is None :
280246 expectation = np .tensordot ( model .contents , self ._image_response .contents , axes = ((0 ,1 ),(0 ,1 )))
281247 # ['lb', 'Ei'] x [NuLambda(lb), Ei, Em, Phi, PsiChi] -> [Em, Phi, PsiChi]
@@ -293,12 +259,12 @@ def calc_expectation(self, model, dict_bkg_norm = None, almost_zero = 1e-12):
293259 expectation *= model .axes ['lb' ].pixarea ()
294260 expectation += almost_zero
295261
296- if dict_bkg_norm is not None :
262+ if dict_bkg_norm is not None :
297263 for key in self .keys_bkg_models ():
298264 expectation += self .bkg_model (key ).contents * dict_bkg_norm [key ]
299265
300266 return Histogram (self .data_axes , contents = expectation , copy_contents = False )
301-
267+
302268 def calc_T_product (self , dataspace_histogram ):
303269 """
304270 Calculate the product of the input histogram with the transonse matrix of the response function.
@@ -375,4 +341,4 @@ def calc_log_likelihood(self, expectation):
375341 """
376342 log_likelihood = np .sum ( self .event * np .log (expectation ) ) - np .sum (expectation )
377343
378- return log_likelihood
344+ return log_likelihood
0 commit comments