edges_cal.xrfi.flagged_filter

edges_cal.xrfi.flagged_filter(data: ndarray, size: int | tuple[int], kind: str = 'median', flags: ndarray | None = None, mode: str | None = None, interp_flagged: bool = True, **kwargs)[source]

Perform an n-dimensional filter operation on optionally flagged data.

Parameters:
  • data (np.ndarray) – The data to filter. Can be of arbitrary dimension.

  • size (int or tuple) – The size of the filtering convolution kernel. If tuple, one entry per dimension in data.

  • kind (str, optional) – The function to apply in each window. Typical options are mean and median. For this function to work, the function kind chosen here must have a corresponding nan<function> implementation in numpy.

  • flags (np.ndarray, optional) – A boolean array specifying data to omit from the filtering.

  • mode (str, optional) – The mode of the filter. See scipy.ndimage.generic_filter for details. By default, ‘nearest’ if size < data.size otherwise ‘reflect’.

  • interp_flagged (bool, optional) – Whether to fill in flagged entries with its filtered value. Otherwise, flagged entries are set to their original value.

  • kwargs – Other options to pass to the generic filter function.

Returns:

np.ndarray – The filtered array, of the same shape and type as data.

Notes

This function can typically be used to implement a flagged median filter. It does have some limitations in this regard, which we will now describe.

It would be expected that a perfectly smooth monotonic function, after median filtering, should remain identical to the input. This is only the case for the default ‘nearest’ mode. For the alternative ‘reflect’ mode, the edge-data will be corrupted from the input. On the other hand, it may be expected that if the kernel width is equal to or larger than the data size, that the operation is merely to perform a full collapse over that dimension. This is the case only for mode ‘reflect’, while again mode ‘nearest’ will continue to yield (a very slow) identity operation. By default, the mode will be set to ‘reflect’ if the size is >= the data size, with an emitted warning.

Furthermore, a median filter is not an identity operation, even on monotonic functions, for an even-sized kernel (in this case it’s the average of the two central values).

Also, even for an odd-sized kernel, if using flags, some of the windows will contain an odd number of useable data, in which case the data surrounding the flag will not be identical to the input.

Finally, flags near the edges can have strange behaviour, depending on the mode.