Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MaskBaselines): mask baselines under an average weight threshold #195

Merged
merged 2 commits into from
Sep 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 32 additions & 13 deletions draco/analysis/flagging.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ class MaskBaselines(task.SingleTask):
mask_short_ew = config.Property(proptype=float, default=None)
mask_short_ns = config.Property(proptype=float, default=None)

weight_threshold = config.Property(proptype=float, default=None)
missing_threshold = config.Property(proptype=float, default=None)

zero_data = config.Property(proptype=bool, default=False)
Expand Down Expand Up @@ -233,23 +234,36 @@ def process(self, ss):
ss.redistribute("freq")

baselines = self.telescope.baselines
mask = np.ones_like(ss.weight[:], dtype=bool)

# The masking array. True will *retain* a sample
ljgray marked this conversation as resolved.
Show resolved Hide resolved
mask = np.zeros_like(ss.weight[:], dtype=bool)

if self.mask_long_ns is not None:
long_ns_mask = np.abs(baselines[:, 1]) < self.mask_long_ns
mask *= long_ns_mask[np.newaxis, :, np.newaxis]
long_ns_mask = np.abs(baselines[:, 1]) > self.mask_long_ns
mask |= long_ns_mask[np.newaxis, :, np.newaxis]

if self.mask_short is not None:
short_mask = np.sum(baselines**2, axis=1) ** 0.5 > self.mask_short
mask *= short_mask[np.newaxis, :, np.newaxis]
short_mask = np.sum(baselines**2, axis=1) ** 0.5 < self.mask_short
mask |= short_mask[np.newaxis, :, np.newaxis]

if self.mask_short_ew is not None:
short_ew_mask = np.abs(baselines[:, 0]) > self.mask_short_ew
mask *= short_ew_mask[np.newaxis, :, np.newaxis]
short_ew_mask = np.abs(baselines[:, 0]) < self.mask_short_ew
mask |= short_ew_mask[np.newaxis, :, np.newaxis]

if self.mask_short_ns is not None:
short_ns_mask = np.abs(baselines[:, 1]) > self.mask_short_ns
mask *= short_ns_mask[np.newaxis, :, np.newaxis]
short_ns_mask = np.abs(baselines[:, 1]) < self.mask_short_ns
mask |= short_ns_mask[np.newaxis, :, np.newaxis]

if self.weight_threshold is not None:
# Get the sum of the weights over frequencies
weight_sum_local = ss.weight[:].local_array.sum(axis=0)
weight_sum_tot = np.zeros_like(weight_sum_local)
self.comm.Allreduce(weight_sum_local, weight_sum_tot, op=MPI.SUM)

# Retain only baselines with average weights larger than the threshold
mask |= weight_sum_tot[np.newaxis, :, :] < self.weight_threshold * len(
ss.freq
)

if self.missing_threshold is not None:
# Get the total number of samples for each baseline accumulated onto each
Expand All @@ -260,9 +274,9 @@ def process(self, ss):

# Mask out baselines with more that `missing_threshold` samples missing
baseline_missing_ratio = 1 - nsamp_tot / nsamp_tot.max()
mask &= (
mask |= (
baseline_missing_ratio[np.newaxis, :, np.newaxis]
< self.missing_threshold
> self.missing_threshold
)

if self.share == "all":
Expand All @@ -273,10 +287,15 @@ def process(self, ss):
ssc = ss.copy()

# Apply the mask to the weight
ssc.weight[:] *= mask
np.multiply(
ssc.weight[:].local_array, 0.0, where=mask, out=ssc.weight[:].local_array
)

# Apply the mask to the data
if self.zero_data:
ssc.vis[:] *= mask
np.multiply(
ssc.vis[:].local_array, 0.0, where=mask, out=ssc.vis[:].local_array
)

return ssc

Expand Down