Skip to content

Commit b4749eb

Browse files
authored
Merge pull request #339 from israelmcmc/PR307_merge_v0.3
PR307 and v0.3.x merge conflict resolution. Follow up on "Initial patches to make cosipy work with histpy 2.x"
2 parents 2c3dee8 + bd86d92 commit b4749eb

25 files changed

+245
-6162
lines changed

cosipy/response/FullDetectorResponse.py

Lines changed: 103 additions & 105 deletions
Large diffs are not rendered by default.

cosipy/source_injector/source_injector.py

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import copy
1010

1111
class SourceInjector():
12-
12+
1313
def __init__(self, response_path, response_frame = "spacecraftframe"):
1414

1515
"""
@@ -24,23 +24,23 @@ def __init__(self, response_path, response_frame = "spacecraftframe"):
2424
"""
2525

2626
self.response_path = response_path
27-
27+
2828
if response_frame == "spacecraftframe" or response_frame == "galactic":
29-
29+
3030
self.response_frame = response_frame
31-
31+
3232
else:
3333
raise ValueError("The response frame can only be `spacecraftframe` or `galactic`!")
3434

3535

3636

3737
@staticmethod
3838
def get_psr_in_galactic(coordinate, response_path, spectrum):
39-
39+
4040
"""
4141
Get the point source response (psr) in galactic. Please be aware that you must use a galactic response!
4242
To do: to make the weight parameter not hardcoded
43-
43+
4444
Parameters
4545
----------
4646
coordinate : astropy.coordinates.SkyCoord
@@ -49,39 +49,27 @@ def get_psr_in_galactic(coordinate, response_path, spectrum):
4949
The path to the response.
5050
spectrum : astromodels.functions
5151
The spectrum of the source to be placed at the hypothesis coordinate.
52-
52+
5353
Returns
5454
-------
5555
psr : histpy.Histogram
5656
The point source response of the spectrum at the hypothesis coordinate.
5757
"""
58-
59-
# Open the response
60-
# Notes from Israel: Inside it contains a single histogram with all the regular axes for a Compton Data Space (CDS) analysis, in galactic coordinates. Since there is no class yet to handle it, this is how to read in the HDF5 manually.
61-
62-
with h5.File(response_path) as f:
6358

64-
axes_group = f['hist/axes']
65-
axes = []
66-
for axis in axes_group.values():
67-
# Get class. Backwards compatible with version
68-
# with only Axis
69-
axis_cls = Axis
70-
if '__class__' in axis.attrs:
71-
class_module, class_name = axis.attrs['__class__']
72-
axis_cls = getattr(sys.modules[class_module], class_name)
73-
axes += [axis_cls._open(axis)]
74-
axes = Axes(axes)
75-
76-
# get the pixel number of the hypothesis coordinate
77-
map_temp = HealpixMap(base = axes[0])
78-
coordinate_pix_number = map_temp.ang2pix(coordinate)
79-
80-
# get the expectation for the hypothesis coordinate (a point source)
59+
# Open the response Histogram, but don't read the whole thing into memory --
60+
# just get the axes, the specific contents pixel(s), and its unit
8161
with h5.File(response_path) as f:
62+
63+
axes = Axes.open(f['hist/axes'])
64+
65+
# get the pixel number of the hypothesis coordinate
66+
hp_axis = axes[0]
67+
coordinate_pix_number = hp_axis.ang2pix(coordinate)
68+
69+
# get the expectation for the hypothesis coordinate (a point source)
8270
pix = coordinate_pix_number
83-
psr = PointSourceResponse(axes[1:], f['hist/contents'][pix+1], unit = f['hist'].attrs['unit'])
84-
71+
psr = PointSourceResponse(axes[1:], f['hist/contents'][pix], unit = f['hist'].attrs['unit'])
72+
8573
return psr
8674

8775

@@ -107,26 +95,28 @@ def inject_point_source(self, spectrum, coordinate, orientation = None, source_n
10795
The path to save the injected data to a `.h5` file. This should include the file name. (the default is `None`, which means the injected data won't be saved.
10896
project_axes : list, optional
10997
The axes to project before saving the data file (the default is `None`, which means the data won't be projected).
110-
98+
11199
Returns
112100
-------
113101
histpy.Histogram
114102
The `Histogram object of the injected spectrum.`
115103
"""
116-
117-
104+
105+
118106
# get the point source response in local frame
119107
if self.response_frame == "spacecraftframe":
120108

121109
if orientation == None:
122110
raise TypeError("The when the data are binned in spacecraftframe frame, orientation must be provided to compute the expected counts.")
123111

112+
113+
124114
with FullDetectorResponse.open(self.response_path) as response:
125-
115+
126116
scatt_map = orientation.get_scatt_map(coordinate, response.nside*2, coordsys = 'galactic', earth_occ = True)
127-
117+
128118
psr = response.get_point_source_response(coord=coordinate, scatt_map=scatt_map)
129-
119+
130120
# get the point source response in galactic frame
131121
elif self.response_frame == "galactic":
132122

@@ -223,62 +213,61 @@ def inject_extended_source(
223213
injected.write(data_save_path)
224214

225215
return injected
226-
216+
227217
def inject_model(self, model, orientation = None, make_spectrum_plot = False, data_save_path = None, project_axes = None):
228-
218+
229219
if self.response_frame == "spacecraftframe":
230220
if orientation == None:
231221
raise TypeError("The when the data are binned in spacecraftframe frame, orientation must be provided to compute the expected counts.")
232-
222+
233223
self.components = {}
234-
224+
235225
# first inject point sources
236226
point_sources = model.point_sources
237-
227+
238228
# iterate through all point sources
239229
for name, source in point_sources.items():
240-
230+
241231
injected = self.inject_point_source(spectrum = source.spectrum.main.shape, coordinate = source.position.sky_coord,
242232
orientation = orientation, source_name = name)
243-
233+
244234
injected.axes["Em"].axis_scale = "log" # set to log scale manually. This inconsistency is from the detector response module
245-
235+
246236
self.components[name] = injected
247-
237+
248238
# second inject extended sources
249239
extended_sources = model.extended_sources
250-
240+
251241
# iterate through all extended sources
252242
for name, source in extended_sources.items():
253-
243+
254244
injected = self.inject_extended_source(source_model = source, source_name = name)
255245
self.components[name] = injected
256-
257-
246+
247+
258248
if len(self.components) == 1:
259-
249+
260250
# if there is only one component, the injected all is just the only component
261251
injected_all = list(self.components.values())[0]
262-
252+
263253
if data_save_path is not None:
264254
injected_all.write(data_save_path)
265-
255+
266256
elif len(self.components) > 1:
267-
257+
268258
injected_list = list(self.components.values())
269-
259+
270260
injected_all = copy.deepcopy(injected_list[0])
271-
261+
272262
# add the rest of the injected sources
273263
for i in injected_list[1:]:
274264
injected_all += i
275-
265+
276266
if make_spectrum_plot:
277267
ax, plot = injected_all.project("Em").draw(color="green", linewidth=2)
278268
ax.set_xscale("log")
279269
ax.set_yscale("log")
280270
ax.set_xlabel("Em [keV]", fontsize=14, fontweight="bold")
281271
ax.set_ylabel("Counts", fontsize=14, fontweight="bold")
282-
272+
283273
return injected_all
284-

cosipy/test_data/bkg_pl.h5

-354 KB
Binary file not shown.
Binary file not shown.

cosipy/test_data/data_pl.h5

-354 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

cosipy/test_data/image_deconvolution/exposure_table_test_nside1_ring.fits

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ SIMPLE = T / conforms to FITS standard
44
P
55
66
7-
@@k�O�z�@1ѝ�t@D�U��-K@R8K�#�?������?������?�s@?������?������?�s@?������?������?�s@?������@k����^�@k���Q�@k�b�x�5@k�(�:�@k��U��,@k���)�<@k�x1m��@k�=ʙ��@k����b@k��T2�@0�1�m$@0�`�wv@0�Ta��Z@1
8-
4Wq~@1�ح�@1%�Nh�@@13��Q8@1AM�,"*@1O��]�@1\�N��@D�c�{f@D�w��F@D����4�@D��KD�)@D��V� �@D��Ԧ<�@E�Ŷw@E�*fW�@E?��@E %P�X8@RG���@RD]�΢$@R@�痓j@R=w��#�@R: Խ�@R6�,e�0@R3Z���@R/����u@R,9ڏ�@R(�,J�
7+
@@k�O�z�@1ѝ�t@D�U��-L@R8K�#�?������?������?�s@?������?������?�s@?������?������?�s@?������@k����^�@k���Q�@k�b�x�5@k�(�:�@k��U��,@k���)�<@k�x1m��@k�=ʙ��@k����b@k��T2�@0�1�m%@0�`�wv@0�Ta��Z@1
8+
4Wq~@1�ح�@1%�Nh�@@13��Q8@1AM�,"*@1O��]�@1\�N��@D�c�{d@D�w��F@D����4�@D��KD�)@D��V� �@D��Ԧ<�@E�Ŷw@E�*fW�@E?��@E %P�X8@RG���@RD]�΢#@R@�痓j@R=w��#�@R: Խ�@R6�,e�0@R3Z���@R/����u@R,9ڏ�@R(�,J�
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)