Flagging algorithms and helper functions

The hera_stats.flag module contains flagging algorithms and utilities, including a way to randomly flag frequency channels (apply_random_flags), a convenience function to flag whole ranges of channels at once (flag_channels), and an implementation of a ‘greedy’ flagging algorithm (construct_factorizable_mask) that can construct factorizable (in time and frequency) masks that flag as small a total fraction of the data as possible.

hera_stats.flag.apply_random_flags(uvd, flag_frac, seed=None, inplace=False, zero_flagged_data=False)[source]

Randomly flag a set of frequency channels. Flags are applied on top of any existing flags, and are applied to all baselines, times, and polarizations.

Parameters:
  • uvd (UVData object) – Input UVData object to be flagged.
  • flag_frac (float) – Fraction of channels to flag. This is the fraction of channels to apply flags to; the actual fraction of flagged channels may be greater than this, depending on if there were already flagged channels in the input UVData object.
  • seed (int, optional) – Random seed to use. Default: None.
  • inplace (bool, optional) – Whether to apply the flags to the input UVData object in-place, or return a copy that includes the new flags. Default: False.
  • zero_flagged_data (bool, optional) – Whether to set the flagged channels in the data_array to zero. This is useful for identifying functions that are ignoring the mask. All flagged data will be zeroed, not just the new flags added by this function.
Returns:

uvd – Returns UVData object with flags applied.

Return type:

UVData object

hera_stats.flag.construct_factorizable_mask(uvd_list, spw_ranges, first='col', greedy_threshold=0.3, n_threshold=1, retain_flags=True, unflag=False, greedy=True, inplace=False)[source]

Generates a factorizable mask using a ‘greedy’ flagging algorithm, run on a list of UVData objects. In this context, factorizable means that the flag array can be written as F(freq, time) = f(freq) * g(time), i.e. entire rows or columns are flagged.

First, flags are added to the mask based on the minimum number of samples available for each data point. Next, depending on the first argument, either full columns or full rows that have flag fractions exceeding the greedy_threshold are flagged. Finally, any rows or columns with remaining flags are fully flagged. (Unflagging the entire array is also an option.)

Parameters:
  • uvd_list (list) – list of UVData objects to operate on
  • spw_ranges (list) – list of tuples of the form (min_channel, max_channel) defining which spectral window (channel range) to flag. min_channel is inclusive, but max_channel is exclusive.
  • first (str, optional) – Either ‘col’ or ‘row’, defines which axis is flagged first based on the greedy_threshold. Default: ‘col’.
  • greedy_threshold (float, optional) – The flag fraction beyond which a given row or column is flagged in the first stage of greedy flagging. Default: 0.3.
  • n_threshold (float, optional) – The minimum number of samples needed for a pixel to remain unflagged. Default: 1.
  • retain_flags (bool, optional) – If True, then data points that were originally flagged in the input data remain flagged, even if they meet the n_threshold. Default: True.
  • unflag (bool, optional) – If True, the entire mask is unflagged. No other operations (e.g. greedy flagging) will be performed. Default: False.
  • greedy (bool, optional) – If True, greedy flagging takes place. If False, only n_threshold flagging is performed (so the resulting mask will not necessarily be factorizable). Default: True.
  • inplace (bool, optional) – Whether to return a new copy of the input UVData objects, or modify them in-place. Default: False (return copies).
Returns:

uvdlist_new – if inplace=False, a new list of UVData objects with updated flags

Return type:

list

hera_stats.flag.flag_channels(uvd, spw_ranges, inplace=False)[source]

Flags a given range of channels entirely for a list of UVData objects

Parameters:
  • uvd (UVData) – UVData object to be flagged.
  • spw_ranges (list) – list of tuples of the form (min_channel, max_channel) defining which channels to flag.
  • inplace (bool, optional) – If True, then the input UVData objects’ flag arrays are modified, and if False, new UVData objects identical to the inputs but with updated flags are created and returned (default is False).
  • Returns
  • ——-
  • uvd_new (list) – Flagged UVData object.