docstr: Automatically define a grid based on objects
  
  Returns a CPGridInfo object
docstr_tok: ["Automatically","define","a","grid","based","on","objects","Returns","a","CPGridInfo","object"]
code: def run_automatic(self, workspace):
      """Automatically define a grid based on objects
  
      Returns a CPGridInfo object
      """
      objects = workspace.object_set.get_objects(self.object_name.value)
      centroids = centrosome.cpmorphology.centers_of_labels(
          objects.segmented
      )
      try:
          if centroids.shape[1] < 2:
              #
              # Failed if too few objects
              #
              raise RuntimeError(
                  "%s has too few grid cells" % self.object_name.value
              )
          #
          # Artificially swap these to match the user's orientation
          #
          first_row, second_row = (1, self.grid_rows.value)
          if self.origin in (NUM_BOTTOM_LEFT, NUM_BOTTOM_RIGHT):
              first_row, second_row = (second_row, first_row)
          first_column, second_column = (1, self.grid_columns.value)
          if self.origin in (NUM_TOP_RIGHT, NUM_BOTTOM_RIGHT):
              first_column, second_column = (
                  second_column,
                  first_column,
              )
          first_x = numpy.min(centroids[1, :])
          first_y = numpy.min(centroids[0, :])
          second_x = numpy.max(centroids[1, :])
          second_y = numpy.max(centroids[0, :])
          result = self.build_grid_info(
              first_x,
              first_y,
              first_row,
              first_column,
              second_x,
              second_y,
              second_row,
              second_column,
              objects.segmented.shape,
          )
      except Exception:
          if self.failed_grid_choice != FAIL_NO:
              result = self.get_good_gridding(workspace)
              if result is None:
                  raise RuntimeError(
                      "%s has too few grid cells and there is no previous successful grid"
                      % self.object_name.value
                  )
          raise
      return result
  
imports: import logging
  import centrosome.cpmorphology
  import numpy
  from cellprofiler_core.constants.measurement import COLTYPE_FLOAT
  from cellprofiler_core.constants.measurement import COLTYPE_INTEGER
  from cellprofiler_core.constants.measurement import IMAGE
  from cellprofiler_core.image import Image
  from cellprofiler_core.module import Module
  from cellprofiler_core.setting import Binary
  from cellprofiler_core.setting import Coordinates
  from cellprofiler_core.setting import ValidationError
  from cellprofiler_core.setting.choice import Choice
  from cellprofiler_core.setting.subscriber import ImageSubscriber
  from cellprofiler_core.setting.subscriber import LabelSubscriber
  from cellprofiler_core.setting.text import GridName
  from cellprofiler_core.setting.text import ImageName
  from cellprofiler_core.setting.text import Integer
  from cellprofiler_core.utilities.grid import Grid
label_desc: Use image node_id 6 for calling the DefineGrid obj's underlying member method code with example usage: obj.run_automatic(workspace) and returns: result