Skip to content

Commit

Permalink
work on @return_dict_for_bundle to increase speed
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-simpson committed Oct 27, 2023
1 parent 1c733b4 commit 95a27d5
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions gemini_instruments/ghost/adclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,27 @@ def return_dict_for_bundle(desc_fn):
Its behaviour on multi-extension slices is unclear.
"""
def wrapper(self, *args, **kwargs):
def confirm_single_valued(_list):
try:
return _list[0] if _list == _list[::-1] else None
except IndexError:
return None

if not self.is_single and 'BUNDLE' in self.tags:
ret_dict = {k: confirm_single_valued(
[desc_fn(self[i+1:i+1+ext.hdr['NAMPS']], *args, **kwargs)
for i, ext in enumerate(self)
if ext.arm() == k and not ext.shape])
for k in ('blue', 'red')}
ret_dict['slitv'] = confirm_single_valued(
[desc_fn(self[i:i+1], *args, **kwargs)
for i, ext in enumerate(self) if ext.arm() == 'slitv'])
ret_dict = dict()
cameras = self.hdr.get('CAMERA', '')
namps = self.hdr.get('NAMPS')
i = 0
while i < len(namps):
camera = cameras[i].lower()
# We've found the nascent PHU of an arm exposure
if namps[i] > 1:
ret_value = desc_fn(self[i+1:i+namps[i]+1], *args, **kwargs)
i += namps[i] + 1
else: # it's a slitviewer
ret_value = desc_fn(self[i], *args, **kwargs)
i += 1
# Check for single-valuedness of all returns from this camera
try:
if ret_dict[camera] != ret_value:
ret_dict[camera] = None
except KeyError:
ret_dict[camera] = ret_value
return ret_dict
# This is the debugging version of the above code
#final_return = {}
#for k in ('BLUE', 'RED'):
# print(f"Looking for {k}")
# ret_values = []
# for i, ext in enumerate(self):
# if ext.hdr.get('CAMERA') == k and not ext.shape:
# print("BLAH", i)
# tmp = self[i+1:i+1+ext.hdr['NAMPS']]
# print("created tmp", len(tmp), tmp.tags)
# ret_value = desc_fn(tmp, *args, **kwargs)
# print("return_dict", i, ret_value, tmp.detector_name())
# ret_values.append(ret_value)
# final_return[k.lower()] = confirm_single_valued(ret_values)
#return final_return
return desc_fn(self, *args, **kwargs)
return wrapper

Expand All @@ -71,7 +61,8 @@ def use_nascent_phu_for_bundle(desc_fn):
"""
def wrapper(self, *args, **kwargs):
if 'BUNDLE' in self.tags:
phu_index = min(i for i, camera in enumerate(self.hdr['CAMERA']) if camera in ('BLUE', 'RED'))
phu_index = min(i for i, camera in enumerate(self.hdr['CAMERA'])
if camera in ('BLUE', 'RED'))
nascent_phu = self[phu_index]
return desc_fn(nascent_phu, *args, **kwargs)
return desc_fn(self, *args, **kwargs)
Expand Down Expand Up @@ -106,7 +97,6 @@ def __getitem__(self, idx):
2) Prevent creation of a new "Frankenstein" AD with data from more
than one camera, as only original bundles should have this.
"""
#print("Entering getitem()", type(self._dataprov), len(self), idx)
obj = super().__getitem__(idx)
if 'BUNDLE' in self.tags:
#print("BUNDLE", idx, obj._mapping)
Expand All @@ -124,9 +114,7 @@ def __getitem__(self, idx):
phu['EXPTIME'] = objhdr['EXPTIME']
obj.phu = phu
return obj
#print("Tested", type(self._dataprov), obj._mapping)
for i in range(min(obj.indices), -1, -1):
#print("***", i, obj._mapping, len(self._dataprov.nddata))
ndd = self._all_nddatas[i]
if not ndd.shape:
phu = ndd.meta['header']
Expand All @@ -140,7 +128,6 @@ def __getitem__(self, idx):
if len(set(ext.shape for ext in obj)) > 1:
raise ValueError("Bundles must be sliced from the same camera")
#print("RETURNING", len(obj))
#print("Returning", type(obj), obj._dataprov)
return obj

@astro_data_descriptor
Expand Down Expand Up @@ -270,7 +257,7 @@ def _tag_spect(self):
"""
camera = self.phu.get('CAMERA')
if camera in ('BLUE', 'RED'):
return TagSet(['SPECT'], blocks=['BUNDLE'])
return TagSet(['SPECT', 'XD'], blocks=['BUNDLE'])

@astro_data_tag
def _status_processed_ghost_cals(self):
Expand Down Expand Up @@ -623,9 +610,14 @@ def number_of_exposures(self):
"""
if 'BUNDLE' not in self.tags:
return len(self) if 'SLITV' in self.tags else 1 # probably
ret_value = {k.lower(): sum(ext.hdr.get('CAMERA') == k and not ext.shape
for ext in self) for k in ('BLUE', 'RED')}
ret_value['slitv'] = sum(ext.hdr.get('CAMERA') == 'SLITV' for ext in self)
if self.is_single:
return 1
cameras = self.hdr.get('CAMERA')
namps = self.hdr.get('NAMPS')
ret_value = {'blue': 0, 'red': 0, 'slitv': 0}
for camera, namp in zip(cameras, namps):
if namp is not None:
ret_value[camera.lower()] += 1
return ret_value

@astro_data_descriptor
Expand Down Expand Up @@ -696,7 +688,7 @@ def saturation_level(self):
return retval
if self.is_single and retval == 0:
return 65535
return [v if v > 0 else 65535 for v in retval]
return [None if v is None else v if v > 0 else 65535 for v in retval]

@astro_data_descriptor
@use_nascent_phu_for_bundle
Expand Down

0 comments on commit 95a27d5

Please sign in to comment.