Skip to content

Commit 3ddeff1

Browse files
mmccrackanMichael McCrackan
andauthored
check if wafer and band exist for qa (#1362)
Co-authored-by: Michael McCrackan <mmccrackan@compute11.simonsobs.org>
1 parent 545d818 commit 3ddeff1

File tree

3 files changed

+96
-96
lines changed

3 files changed

+96
-96
lines changed

sotodlib/preprocess/pcore.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -586,32 +586,34 @@ def gen_metric(
586586
frac_flagged = np.array([
587587
np.dot(r.ranges(), [-1, 1]).sum() for r in proc_aman[key1][key2][subset]
588588
], dtype=float)
589-
num_valid = np.array([
590-
np.dot(r.ranges(), [-1, 1]).sum() for r in proc_aman[key1].valid[subset]
591-
])
592-
with np.errstate(divide="ignore"):
593-
frac_flagged *= np.where(num_valid > 0, 1 / num_valid, 0)
594-
595-
# record percentiles over detectors and fraction of samples flagged
596-
perc = np.percentile(frac_flagged, percentiles)
597-
mean = frac_flagged.mean()
598-
599-
# get the tags for this wafer (all detectors in this subset share these)
600-
tags_base = {
601-
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
602-
}
603-
tags_base["telescope"] = meta.obs_info.telescope
604-
605-
# add tags and values to respective lists in order
606-
tags_perc = [tags_base.copy() for i in range(perc.size)]
607-
for i, t in enumerate(tags_perc):
608-
t["det_stat"] = f"percentile_{percentiles[i]}"
609-
vals += list(perc)
610-
tags += tags_perc
611-
tags_mean = tags_base.copy()
612-
tags_mean["det_stat"] = "mean"
613-
vals.append(mean)
614-
tags.append(tags_mean)
589+
590+
if len(frac_flagged) > 0:
591+
num_valid = np.array([
592+
np.dot(r.ranges(), [-1, 1]).sum() for r in proc_aman[key1].valid[subset]
593+
])
594+
with np.errstate(divide="ignore"):
595+
frac_flagged *= np.where(num_valid > 0, 1 / num_valid, 0)
596+
597+
# record percentiles over detectors and fraction of samples flagged
598+
perc = np.percentile(frac_flagged, percentiles)
599+
mean = frac_flagged.mean()
600+
601+
# get the tags for this wafer (all detectors in this subset share these)
602+
tags_base = {
603+
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
604+
}
605+
tags_base["telescope"] = meta.obs_info.telescope
606+
607+
# add tags and values to respective lists in order
608+
tags_perc = [tags_base.copy() for i in range(perc.size)]
609+
for i, t in enumerate(tags_perc):
610+
t["det_stat"] = f"percentile_{percentiles[i]}"
611+
vals += list(perc)
612+
tags += tags_perc
613+
tags_mean = tags_base.copy()
614+
tags_mean["det_stat"] = "mean"
615+
vals.append(mean)
616+
tags.append(tags_mean)
615617

616618
obs_time = [meta.obs_info.timestamp] * len(vals)
617619
return {

sotodlib/preprocess/processes.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -826,46 +826,47 @@ def gen_metric(cls, meta, proc_aman):
826826
(meta.det_info.wafer_slot == ws) & (meta.det_info.wafer.bandpass == bp)
827827
)[0]
828828

829-
# get the coefficients for every detector
830-
coeff = proc_aman.hwpss_stats.coeffs[subset]
831-
# mask those that were not set
832-
nonzero = np.any(coeff != 0.0, axis=1)
833-
834-
# calculate amplitude of each mode
835-
mode_labels = list(proc_aman.hwpss_stats.modes.vals)
836-
num_re = re.compile("^[SC](\d+)$")
837-
nums = sorted(list(set([num_re.match(l).group(1) for l in mode_labels])))
838-
coeff_amp = np.zeros((coeff.shape[0], len(nums)), coeff.dtype)
839-
amp_labels = []
840-
for i, n in enumerate(nums):
841-
c_ind = mode_labels.index(f"C{n}")
842-
s_ind = mode_labels.index(f"S{n}")
843-
coeff_amp[:, i] = np.sqrt(coeff[:, c_ind]**2 + coeff[:, s_ind]**2)
844-
amp_labels.append(f"A{n}")
845-
846-
# record percentiles over detectors and fraction of samples flagged
847-
perc = np.percentile(coeff_amp[nonzero], cls._influx_percentiles, axis=0)
848-
mean = coeff_amp[nonzero].mean(axis=0)
849-
850-
tags_base = {
851-
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
852-
}
853-
tags_base["telescope"] = meta.obs_info.telescope
854-
855-
# loop over percentiles and coefficient labels
856-
for pi, p in enumerate(cls._influx_percentiles):
829+
if len(subset) > 0:
830+
# get the coefficients for every detector
831+
coeff = proc_aman.hwpss_stats.coeffs[subset]
832+
# mask those that were not set
833+
nonzero = np.any(coeff != 0.0, axis=1)
834+
835+
# calculate amplitude of each mode
836+
mode_labels = list(proc_aman.hwpss_stats.modes.vals)
837+
num_re = re.compile("^[SC](\d+)$")
838+
nums = sorted(list(set([num_re.match(l).group(1) for l in mode_labels])))
839+
coeff_amp = np.zeros((coeff.shape[0], len(nums)), coeff.dtype)
840+
amp_labels = []
841+
for i, n in enumerate(nums):
842+
c_ind = mode_labels.index(f"C{n}")
843+
s_ind = mode_labels.index(f"S{n}")
844+
coeff_amp[:, i] = np.sqrt(coeff[:, c_ind]**2 + coeff[:, s_ind]**2)
845+
amp_labels.append(f"A{n}")
846+
847+
# record percentiles over detectors and fraction of samples flagged
848+
perc = np.percentile(coeff_amp[nonzero], cls._influx_percentiles, axis=0)
849+
mean = coeff_amp[nonzero].mean(axis=0)
850+
851+
tags_base = {
852+
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
853+
}
854+
tags_base["telescope"] = meta.obs_info.telescope
855+
856+
# loop over percentiles and coefficient labels
857+
for pi, p in enumerate(cls._influx_percentiles):
858+
for l in amp_labels:
859+
t_new = tags_base.copy()
860+
t_new.update({"mode": l, "det_stat": f"percentile_{p}"})
861+
tags.append(t_new)
862+
vals += list(perc[pi])
863+
864+
# finally also record the mean
857865
for l in amp_labels:
858866
t_new = tags_base.copy()
859-
t_new.update({"mode": l, "det_stat": f"percentile_{p}"})
867+
t_new.update({"mode": l, "det_stat": "mean"})
860868
tags.append(t_new)
861-
vals += list(perc[pi])
862-
863-
# finally also record the mean
864-
for l in amp_labels:
865-
t_new = tags_base.copy()
866-
t_new.update({"mode": l, "det_stat": "mean"})
867-
tags.append(t_new)
868-
vals += list(mean)
869+
vals += list(mean)
869870

870871
obs_time = [meta.obs_info.timestamp] * len(tags)
871872
return {

sotodlib/qa/metrics.py

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,25 @@ def _process(self, meta):
212212
(meta.det_info.wafer_slot == ws) & (meta.det_info.wafer.bandpass == bp)
213213
)[0]
214214

215-
# Compute the number of samples that are valid
216-
frac_valid = np.array([
217-
np.dot(r.ranges(), [-1, 1]).sum() / len(subset)
218-
for r in meta.preprocess[self._key].valid[subset]
219-
])
220-
221-
# Count detectors with fraction valid above threshold
222-
n_good = (frac_valid > self._thresh).sum()
223-
224-
# get the tags for this wafer (all detectors in this subset share these)
225-
tags_i = {
226-
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
227-
}
228-
tags_i["telescope"] = meta.obs_info.telescope
229-
230-
# add tags and values to respective lists in order
231-
vals.append(n_good)
232-
tags.append(tags_i)
215+
if len(subset) > 0:
216+
# Compute the number of samples that are valid
217+
frac_valid = np.array([
218+
np.dot(r.ranges(), [-1, 1]).sum() / len(subset)
219+
for r in meta.preprocess[self._key].valid[subset]
220+
])
221+
222+
# Count detectors with fraction valid above threshold
223+
n_good = (frac_valid > self._thresh).sum()
224+
225+
# get the tags for this wafer (all detectors in this subset share these)
226+
tags_i = {
227+
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
228+
}
229+
tags_i["telescope"] = meta.obs_info.telescope
230+
231+
# add tags and values to respective lists in order
232+
vals.append(n_good)
233+
tags.append(tags_i)
233234

234235
obs_time = [meta.obs_info.timestamp] * len(vals)
235236
return {
@@ -290,14 +291,12 @@ def _process(self, meta):
290291
good_indices = np.nonzero(mask)[0]
291292
if good_indices.size > 0:
292293
vals.append(np.sqrt(1.0 / np.nansum(1.0 / (white_noise[good_indices])**2)))
293-
else:
294-
vals.append(0.0)
295294

296-
tags_base = {
297-
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
298-
}
299-
tags_base["telescope"] = meta.obs_info.telescope
300-
tags.append(tags_base)
295+
tags_base = {
296+
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
297+
}
298+
tags_base["telescope"] = meta.obs_info.telescope
299+
tags.append(tags_base)
301300

302301
obs_time = [meta.obs_info.timestamp] * len(tags)
303302
return {
@@ -358,14 +357,12 @@ def _process(self, meta):
358357
good_indices = np.nonzero(mask)[0]
359358
if good_indices.size > 0:
360359
vals.append(np.sqrt(1.0 / np.nansum(1.0 / (white_noise[good_indices])**2)) * np.sqrt(len(good_indices)))
361-
else:
362-
vals.append(0.0)
363-
364-
tags_base = {
365-
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
366-
}
367-
tags_base["telescope"] = meta.obs_info.telescope
368-
tags.append(tags_base)
360+
361+
tags_base = {
362+
k: _get_tag(meta.det_info, k, subset[0]) for k in tag_keys if _has_tag(meta.det_info, k)
363+
}
364+
tags_base["telescope"] = meta.obs_info.telescope
365+
tags.append(tags_base)
369366

370367
obs_time = [meta.obs_info.timestamp] * len(tags)
371368
return {

0 commit comments

Comments
 (0)