Skip to content

Commit

Permalink
refactor: cache image_response.contents in image_deconvolution (#3)
Browse files Browse the repository at this point in the history
* hidden: profiling nb and removal of profiling nb
  • Loading branch information
augustus-thomas authored Oct 24, 2024
1 parent 05d813a commit bf79422
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions cosipy/image_deconvolution/dataIF_COSI_DC2.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def load(cls, name, event_binned_data, dict_bkg_binned_data, rsp, coordsys_conv_
elif isinstance(rsp, Histogram):
new._image_response = rsp

new.image_response_contents = new._image_response.contents

# We modify the axes in event, bkg_models, response. This is only for DC2.
new._modify_axes()

Expand Down Expand Up @@ -205,7 +207,7 @@ def _calc_exposure_map(self):

if self._coordsys_conv_matrix is None:
self._exposure_map = Histogram(self._model_axes, unit = self._image_response.unit * u.sr)
self._exposure_map[:] = np.sum(self._image_response.contents, axis = (2,3,4)) * self.model_axes['lb'].pixarea()
self._exposure_map[:] = np.sum(self.image_response_contents, axis = (2,3,4)) * self.model_axes['lb'].pixarea()
else:
self._exposure_map = Histogram(self._model_axes, unit = self._image_response.unit * self._coordsys_conv_matrix.unit * u.sr)
self._exposure_map[:] = np.tensordot(np.sum(self._coordsys_conv_matrix, axis = (0)),
Expand Down Expand Up @@ -248,7 +250,7 @@ def calc_expectation(self, model, dict_bkg_norm = None, almost_zero = 1e-12):
expectation = Histogram(self.data_axes)

if self._coordsys_conv_matrix is None:
expectation[:] = np.tensordot( model.contents, self._image_response.contents, axes = ([0,1],[0,1])) * model.axes['lb'].pixarea()
expectation[:] = np.tensordot( model.contents, self.image_response_contents, axes = ([0,1],[0,1])) * model.axes['lb'].pixarea()
# ['lb', 'Ei'] x [NuLambda(lb), Ei, Em, Phi, PsiChi] -> [Em, Phi, PsiChi]
else:
map_rotated = np.tensordot(self._coordsys_conv_matrix.contents, model.contents, axes = ([1], [0]))
Expand All @@ -257,7 +259,7 @@ def calc_expectation(self, model, dict_bkg_norm = None, almost_zero = 1e-12):
map_rotated *= model.axes['lb'].pixarea()
# data.coordsys_conv_matrix.contents is sparse, so the unit should be restored.
# the unit of map_rotated is 1/cm2 ( = s * 1/cm2/s/sr * sr)
expectation[:] = np.tensordot( map_rotated, self._image_response.contents, axes = ([1,2], [0,1]))
expectation[:] = np.tensordot( map_rotated, self.image_response_contents, axes = ([1,2], [0,1]))
# [Time/ScAtt, NuLambda, Ei] x [NuLambda, Ei, Em, Phi, PsiChi] -> [Time/ScAtt, Em, Phi, PsiChi]

if dict_bkg_norm is not None:
Expand Down Expand Up @@ -293,10 +295,10 @@ def calc_T_product(self, dataspace_histogram):
hist = Histogram(self.model_axes, unit = hist_unit)

if self._coordsys_conv_matrix is None:
hist[:] = np.tensordot(dataspace_histogram.contents, self._image_response.contents, axes = ([0,1,2], [2,3,4])) * self.model_axes['lb'].pixarea()
hist[:] = np.tensordot(dataspace_histogram.contents, self.image_response_contents, axes = ([0,1,2], [2,3,4])) * self.model_axes['lb'].pixarea()
# [Em, Phi, PsiChi] x [NuLambda (lb), Ei, Em, Phi, PsiChi] -> [NuLambda (lb), Ei]
else:
_ = np.tensordot(dataspace_histogram.contents, self._image_response.contents, axes = ([1,2,3], [2,3,4]))
_ = np.tensordot(dataspace_histogram.contents, self.image_response_contents, axes = ([1,2,3], [2,3,4]))
# [Time/ScAtt, Em, Phi, PsiChi] x [NuLambda, Ei, Em, Phi, PsiChi] -> [Time/ScAtt, NuLambda, Ei]

hist[:] = np.tensordot(self._coordsys_conv_matrix.contents, _, axes = ([0,2], [0,1])) \
Expand Down

0 comments on commit bf79422

Please sign in to comment.