docstr: Filter labels by proximity to edges of grid
docstr_tok: ["Filter","labels","by","proximity","to","edges","of","grid"]
code: def filtered_labels(self, workspace, gridding):
      """Filter labels by proximity to edges of grid"""
      #
      # A label might slightly graze a grid other than its own or
      # a label might be something small in a corner of the grid.
      # This function filters out those parts of the guide labels matrix
      #
      assert isinstance(gridding, Grid)
      guide_labels = self.get_guide_labels(workspace)
      labels = self.fill_grid(workspace, gridding)
  
      centers = numpy.zeros((2, numpy.max(guide_labels) + 1))
      centers[:, 1:] = centers_of_labels(guide_labels)
      bad_centers = (
          (~numpy.isfinite(centers[0, :]))
          | (~numpy.isfinite(centers[1, :]))
          | (centers[0, :] >= labels.shape[0])
          | (centers[1, :] >= labels.shape[1])
      )
      centers = numpy.round(centers).astype(int)
      masked_labels = labels.copy()
      x_border = int(numpy.ceil(gridding.x_spacing / 10))
      y_border = int(numpy.ceil(gridding.y_spacing / 10))
      #
      # erase anything that's not like what's next to it
      #
      ymask = labels[y_border:, :] != labels[:-y_border, :]
      masked_labels[y_border:, :][ymask] = 0
      masked_labels[:-y_border, :][ymask] = 0
      xmask = labels[:, x_border:] != labels[:, :-x_border]
      masked_labels[:, x_border:][xmask] = 0
      masked_labels[:, :-x_border][xmask] = 0
      #
      # Find out the grid that each center falls into. If a center falls
      # into the border region, it will get a grid number of 0 and be
      # erased. The guide objects may fall below or to the right of the
      # grid or there may be gaps in numbering, so we set the center label
      # of bad centers to 0.
      #
      centers[:, bad_centers] = 0
      lcenters = masked_labels[centers[0, :], centers[1, :]]
      lcenters[bad_centers] = 0
      #
      # Use the guide labels to look up the corresponding center for
      # each guide object pixel. Mask out guide labels that don't match
      # centers.
      #
      mask = numpy.zeros(guide_labels.shape, bool)
      ii_labels = numpy.index_exp[
          0 : labels.shape[0], 0 : labels.shape[1]
      ]
      mask[ii_labels] = lcenters[guide_labels[ii_labels]] != labels
      mask[guide_labels == 0] = True
      mask[lcenters[guide_labels] == 0] = True
      filtered_guide_labels = guide_labels.copy()
      filtered_guide_labels[mask] = 0
      return filtered_guide_labels
  
imports: from cellprofiler_core.constants.module import HELP_ON_MEASURING_DISTANCES
  from cellprofiler_core.setting.choice import Choice
  from cellprofiler_core.setting.subscriber import LabelSubscriber, GridSubscriber
  from cellprofiler_core.setting.text import LabelName, Integer
  from cellprofiler_core.utilities.core.module.identify import add_object_location_measurements, add_object_count_measurements, get_object_measurement_columns
  from cellprofiler.modules import _help
  import numpy
  from centrosome.cpmorphology import centers_of_labels
  from cellprofiler_core.utilities.grid import Grid
  from cellprofiler_core.module import Module
  from cellprofiler_core.object import Objects
label_desc: Use image node_id 14 for calling the IdentifyObjectsInGrid obj's underlying member method code with example usage: obj.filtered_labels(workspace, gridding) and returns: filtered_guide_labels