Skip to content

Commit 2c420b6

Browse files
author
Yoshinori Sueno
committed
add find_common_edge_idx
1 parent 4272211 commit 2c420b6

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

sotodlib/core/flagman.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,23 @@ def sparse_to_ranges_matrix(arr, buffer=0, close_gaps=0, val=True):
354354
x[i].close_gaps(close_gaps)
355355
return x
356356

357+
def find_common_edge_idx(flags):
358+
"""Find the common valid range across multiple RangesMatrix objects.
359+
360+
Args:
361+
flags (RangesMatrix): An instance of so3g.proj.RangesMatrix indicating flagged time ranges.
362+
Returns:
363+
min_idx, max_idx: minmum and maximum indices that has False flag across all detectros.
364+
"""
365+
max_val = max(arr.complement().ranges()[:, 1].max() for arr in flags)
366+
common_mask = np.ones(max_val, dtype=bool)
367+
for arr in flags:
368+
mask = np.zeros(max_val, dtype=bool)
369+
for start, end in arr.complement().ranges():
370+
mask[start:end] = True
371+
common_mask &= mask
372+
373+
valid_indices = np.where(common_mask)[0]
374+
if len(valid_indices) == 0:
375+
raise ValueError("No common valid range found across all flags.")
376+
return valid_indices[0], valid_indices[-1]

sotodlib/preprocess/processes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,13 +2066,14 @@ class TrimFlagEdge(_Preprocess):
20662066
- name: "trim_flag_edge"
20672067
process:
20682068
flags: "pca_exclude"
2069+
2070+
.. autofunction:: sotodlib.core.flagman.find_common_edge_idx
20692071
"""
20702072
name = 'trim_flag_edge'
20712073

20722074
def process(self, aman, proc_aman, sim=False):
20732075
flags = aman.flags.get(self.process_cfgs.get('flags'))
2074-
trimst = np.where(~np.any(flags.mask(), axis = 0))[0][0]
2075-
trimen = np.where(~np.any(flags.mask(), axis = 0))[0][-1]
2076+
trimst, trimen = core.flagman.find_common_edge_idx(flags)
20762077
aman.restrict('samps', (aman.samps.offset + trimst,
20772078
aman.samps.offset + trimen))
20782079
proc_aman.restrict('samps', (proc_aman.samps.offset + trimst,

0 commit comments

Comments
 (0)