Skip to content

Commit e88c974

Browse files
committed
Added code to compress empty axes, i.e., those with axis shape = (1,)
1 parent 4f23611 commit e88c974

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

cosipy/response/DetectorResponse.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,22 @@ def __init__(self, coord, **kwargs):
4242
self._spec = None
4343
self._aeff = None
4444

45+
def drop_empty_axes(self):
46+
47+
axis_names = self.axes.labels
48+
axes_projection = []
49+
50+
for name in axis_names:
51+
if (name in ['Ei', 'Em', 'eps', 'Eps']) | (self.axes[name].edges.size > 2):
52+
axes_projection.append(name)
53+
54+
spec = self.project(axes_projection)
55+
56+
return DetectorResponse(edges = spec.axes,
57+
contents = spec.contents,
58+
unit = spec.unit,
59+
interpolated_NuLambda = self.interpolated_NuLambda)
60+
4561
def _set_mapping(self):
4662
self.mapping = {}
4763
target_names = ['Ei', 'Em', 'Phi', 'PsiChi']
@@ -165,7 +181,7 @@ def get_interp_response(self, target: dict):
165181
piecewise-linear directional responses.
166182
"""
167183

168-
target = dict(sorted(target.items())) # Sort dictionary by key (XXX: assuming response matrix also sorts in the same way)
184+
# target = dict(sorted(target.items())) # Sort dictionary by key (XXX: assuming response matrix also sorts in the same way)
169185
indices, weights = self._get_all_interp_weights(target)
170186
perm_indices = list(itertools.product(*indices))
171187
perm_weights = list(itertools.product(*weights))
@@ -202,7 +218,7 @@ def get_spectral_response(self, copy = True):
202218

203219
# Cache the spectral response
204220
if self._spec is None:
205-
spec = self.project(['Ei','Em'])
221+
spec = self.project(['Ei', self.mapping['Em']])
206222
self._spec = DetectorResponse(coord = self.coord,
207223
edges = spec.axes,
208224
contents = spec.contents,
@@ -310,7 +326,7 @@ def measured_energy_axis(self):
310326
:py:class:`histpy.Axes`
311327
"""
312328

313-
return self.axes['Em']
329+
return self.axes[self.mapping['Em']]
314330

315331

316332

docs/tutorials/response/LMDR.ipynb

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,18 +1455,20 @@
14551455
},
14561456
{
14571457
"cell_type": "code",
1458-
"execution_count": 2,
1458+
"execution_count": null,
14591459
"metadata": {},
14601460
"outputs": [],
14611461
"source": [
1462-
"# response_path = Path('/Users/penguin/Documents/Grad School/Research/COSI/COSIpy/docs/tutorials/data/SMEXv12.Continuum.HEALPixO3_10bins_log_flat.binnedimaging.imagingresponse.nonsparse_nside8.area.good_chunks_unzip.h5')\n",
1463-
"response_path = Path('/Users/penguin/Documents/Grad School/Research/COSI/COSIpy/docs/tutorials/data/SMEXv12.44Ti.HEALPix04.E_1150_1164keV.binnedimaging.imagingresponse.nonsparse_nside16.area.h5')\n",
1464-
"# response_path = Path('/Users/penguin/Documents/Grad School/Research/COSI/COSIpy/docs/tutorials/data/DC2-Continuum_N-149_17-11-2024_14-32.imagingresponse_nside8.area.h5')"
1462+
"DATA_DIR = Path('/Users/penguin/Documents/Grad School/Research/COSI/COSIpy/docs/tutorials/data/')\n",
1463+
"\n",
1464+
"# response_path = DATA_DIR / Path('SMEXv12.Continuum.HEALPixO3_10bins_log_flat.binnedimaging.imagingresponse.nonsparse_nside8.area.good_chunks_unzip.h5')\n",
1465+
"response_path = DATA_DIR / Path('SMEXv12.44Ti.HEALPix04.E_1150_1164keV.binnedimaging.imagingresponse.nonsparse_nside16.area.h5')\n",
1466+
"# response_path = DATA_DIR / Path('DC2-Continuum_N-149_17-11-2024_14-32.imagingresponse_nside8.area.h5')"
14651467
]
14661468
},
14671469
{
14681470
"cell_type": "code",
1469-
"execution_count": 3,
1471+
"execution_count": null,
14701472
"metadata": {},
14711473
"outputs": [
14721474
{
@@ -1482,11 +1484,20 @@
14821484
"with FullDetectorResponse.open(response_path, unbinned=True) as response:\n",
14831485
" print(f\"Pixel 0 centered at {response.pix2skycoord(0)}\")\n",
14841486
" dr = response[0]\n",
1485-
" data = response._file['DRM']['CONTENTS'][0, ...]\n",
1486-
" # dr = DetectorResponse(edges=response.axes[1:], contents=data, unit=response.unit, interpolated_NuLambda=True)\n",
1487+
" # data = response._file['DRM']['CONTENTS'][0, ...]\n",
1488+
" # dr = DetectorResponse(edges=response.axes[1:], contents=data, unit=response.unit, interpolated_NuLambda=True) # scipy.COO requires special handling\n",
14871489
" dr = response.get_interp_response(SkyCoord(lon=0, lat=0, frame=SpacecraftFrame(), unit=u.deg))"
14881490
]
14891491
},
1492+
{
1493+
"cell_type": "code",
1494+
"execution_count": null,
1495+
"metadata": {},
1496+
"outputs": [],
1497+
"source": [
1498+
"dr = dr.drop_empty_axes()"
1499+
]
1500+
},
14901501
{
14911502
"cell_type": "code",
14921503
"execution_count": 7,
@@ -1533,7 +1544,7 @@
15331544
},
15341545
{
15351546
"cell_type": "code",
1536-
"execution_count": 5,
1547+
"execution_count": null,
15371548
"metadata": {},
15381549
"outputs": [
15391550
{
@@ -1561,12 +1572,23 @@
15611572
}
15621573
],
15631574
"source": [
1564-
"dr.get_spectral_response().get_interp_response({'Ei':Ei0, 'Em':Em0})"
1575+
"dr.get_spectral_response().get_interp_response({'Ei':511*u.keV, 'Em':511*u.keV})"
15651576
]
15661577
},
15671578
{
15681579
"cell_type": "code",
1569-
"execution_count": 15,
1580+
"execution_count": null,
1581+
"metadata": {},
1582+
"outputs": [],
1583+
"source": [
1584+
"dr.mapping['PsiChi'] = None\n",
1585+
"dr.mapping['Theta'] = 'Theta'\n",
1586+
"dr.mapping['Zeta'] = 'Zeta'"
1587+
]
1588+
},
1589+
{
1590+
"cell_type": "code",
1591+
"execution_count": null,
15701592
"metadata": {},
15711593
"outputs": [
15721594
{
@@ -1586,7 +1608,16 @@
15861608
}
15871609
],
15881610
"source": [
1589-
"dr[3,3,2,385]"
1611+
"dr[58,4,20,108,100] # dr[3,3,2,385]"
1612+
]
1613+
},
1614+
{
1615+
"cell_type": "code",
1616+
"execution_count": null,
1617+
"metadata": {},
1618+
"outputs": [],
1619+
"source": [
1620+
"dr.contents.shape"
15901621
]
15911622
},
15921623
{
@@ -1614,7 +1645,8 @@
16141645
"Phi0 = 12*u.deg\n",
16151646
"PsiChi0 = 386\n",
16161647
"\n",
1617-
"target = {'Ei': Ei0, 'Em': Em0, 'Phi': Phi0, 'PsiChi': PsiChi0}\n",
1648+
"# target = {'Ei': Ei0, 'Em': Em0, 'Phi': Phi0, 'PsiChi': PsiChi0}\n",
1649+
"target = {'Ei': Ei0, 'Em': Em0, 'Phi': Phi0, 'Theta': 37*u.deg, 'Zeta': 201*u.deg}\n",
16181650
"interpolated_response_value = dr.get_interp_response(target)\n",
16191651
"\n",
16201652
"interpolated_response_value"
@@ -1699,8 +1731,8 @@
16991731
"target = {'Ei': Ei0, 'Em': Em0, 'Phi': Phi0, 'PsiChi': PsiChi0}\n",
17001732
"\n",
17011733
"fig, ax = plt.subplots()\n",
1702-
"dr.project(label1, label2).draw(ax=ax)\n",
1703-
"ax.scatter(target[label1], target[label2], marker='*')\n",
1734+
"dr.project(label1, dr.mapping[label2]).draw(ax=ax)\n",
1735+
"# ax.scatter(target[label1], target[label2], marker='*')\n",
17041736
"for e1 in dr.neighbors[label1]:\n",
17051737
" for e2 in dr.neighbors[label2]:\n",
17061738
" ax.scatter(e1, e2, c='r')\n",

0 commit comments

Comments
 (0)