docstr: Create your settings by subclassing this function
  
  create_settings is called at the end of initialization.
docstr_tok: ["Create","your","settings","by","subclassing","this","function","create_settings","is","called","at","the","end","of","initialization","."]
code: def create_settings(self):
      """Create your settings by subclassing this function
  
      create_settings is called at the end of initialization.
      """
      self.grid_image = GridName(
          "Name the grid",
          doc="""\
  This is the name of the grid. You can use this name to
  retrieve the grid in subsequent modules.""",
      )
  
      self.grid_rows = Integer(
          "Number of rows",
          8,
          1,
          doc="""Along the height of the grid, define the number of rows.""",
      )
  
      self.grid_columns = Integer(
          "Number of columns",
          12,
          1,
          doc="""Along the width of the grid, define the number of columns.""",
      )
  
      self.origin = Choice(
          "Location of the first spot",
          [
              NUM_TOP_LEFT,
              NUM_BOTTOM_LEFT,
              NUM_TOP_RIGHT,
              NUM_BOTTOM_RIGHT,
          ],
          doc="""\
  Grid cells are numbered consecutively; this option identifies the
  origin for the numbering system and the direction for numbering.
  For instance, if you choose "*%(NUM_TOP_LEFT)s*", the top left cell is
  cell #1 and cells to the right and bottom are indexed with
  larger numbers."""
          % globals(),
      )
  
      self.ordering = Choice(
          "Order of the spots",
          [NUM_BY_ROWS, NUM_BY_COLUMNS],
          doc="""\
  Grid cells can either be numbered by rows, then columns or by columns,
  then rows. For instance, if you asked to start numbering a 96-well
  plate at the top left (by specifying the location of the first spot), then:
  
  -  *%(NUM_BY_ROWS)s:* this option will give well A01 the index 1, B01
     the index 2, and so on up to H01 which receives the index 8. Well A02
     will be assigned the index 9.
  -  *%(NUM_BY_COLUMNS)s:* with this option, the well A02 will be
     assigned 2, well A12 will be assigned 12 and well B01 will be
     assigned 13.
  """
          % globals(),
      )
  
      self.each_or_once = Choice(
          "Define a grid for which cycle?",
          [EO_EACH, EO_ONCE],
          doc="""\
  The setting allows you choose when you want to define a new grid:
  
  -  *%(EO_ONCE)s:* If all of your images are perfectly aligned with each
     other (due to very consistent image acquisition, consistent grid
     location within the plate, and/or automatic cropping precisely within
     each plate), you can define the location of the marker spots once for
     all of the image cycles.
  -  *%(EO_EACH)s:* If the location of the grid will vary from one image
     cycle to the next then you should define the location of the marker
     spots for each cycle independently.
  """
          % globals(),
      )
  
      self.auto_or_manual = Choice(
          "Select the method to define the grid",
          [AM_AUTOMATIC, AM_MANUAL],
          doc="""\
  Select whether you would like to define the grid automatically (based on
  objects you have identified in a previous module) or manually. This
  setting controls how the grid is defined:
  
  -  *%(AM_MANUAL)s:* In manual mode, you manually indicate known
     locations of marker spots in the grid and have the rest of the
     positions calculated from those marks, no matter what the image
     itself looks like. You can define the grid either by clicking on the
     image with a mouse or by entering coordinates.
  -  *%(AM_AUTOMATIC)s:* If you would like the grid to be defined
     automatically, an **IdentifyPrimaryObjects** module must be run prior
     to this module to identify the objects that will be used to define
     the grid. The left-most, right-most, top-most, and bottom-most object
     will be used to define the edges of the grid, and the rows and
     columns will be evenly spaced between these edges. Note that
     Automatic mode requires that the incoming objects are nicely defined:
     for example, if there is an object at the edge of the images that is
     not really an object that ought to be in the grid, a skewed grid will
     result. You might wish to use a **FilterObjects** module to clean up
     badly identified objects prior to defining the grid. If the spots are
     slightly out of alignment with each other from one image cycle to the
     next, this allows the identification to be a bit flexible and adapt
     to the real location of the spots.
  """
          % globals(),
      )
  
      self.object_name = LabelSubscriber(
          "Select the previously identified objects",
          "None",
          doc="""\
  *(Used only if you selected "%(AM_AUTOMATIC)s" to define the grid)*
  
  Select the previously identified objects you want to use to define the
  grid. Use this setting to specify the name of the objects that will be
  used to define the grid.
  """
          % globals(),
      )
  
      self.manual_choice = Choice(
          "Select the method to define the grid manually",
          [MAN_MOUSE, MAN_COORDINATES],
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" to define the grid)*
  
  Specify whether you want to define the grid using the mouse or by
  entering the coordinates of the cells.
  
  -  *%(MAN_MOUSE)s:* The user interface displays the image you specify.
     You will be asked to click in the center of two of the grid cells and
     specify the row and column for each. The grid coordinates will be
     computed from this information.
  -  *%(MAN_COORDINATES)s:* Enter the X and Y coordinates of the grid
     cells directly. You can display an image of your grid to find the
     locations of the centers of the cells, then enter the X and Y
     position and cell coordinates for each of two cells.
  """
          % globals(),
      )
  
      self.manual_image = ImageSubscriber(
          "Select the image to display when drawing",
          "None",
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_MOUSE)s" to define
  the grid)*
  
  Specify the image you want to display when defining the grid. This
  setting lets you choose the image to display in the grid definition user
  interface.
  """
          % globals(),
      )
  
      self.first_spot_coordinates = Coordinates(
          "Coordinates of the first cell",
          (0, 0),
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_COORDINATES)s" to
  define the grid)*
  
  Enter the coordinates of the first cell on your grid. This setting
  defines the location of the first of two cells in your grid. You should
  enter the coordinates of the center of the cell. You can display an
  image of your grid and use the pixel coordinate display to determine the
  coordinates of the center of your cell.
  """
          % globals(),
      )
  
      self.first_spot_row = Integer(
          "Row number of the first cell",
          1,
          minval=1,
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_COORDINATES)s" to
  define the grid)*
  
  Enter the row index for the first cell here. Rows are numbered starting
  at the origin. For instance, if you chose "*%(NUM_TOP_LEFT)s*" as your
  origin, well A01 will be row number 1 and H01 will be row number 8. If
  you chose "*%(NUM_BOTTOM_LEFT)s*", A01 will be row number 8 and H01 will
  be row number 12.
  """
          % globals(),
      )
  
      self.first_spot_col = Integer(
          "Column number of the first cell",
          1,
          minval=1,
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_COORDINATES)s" to
  define the grid)*
  
  Enter the column index for the first cell here. Columns are numbered
  starting at the origin. For instance, if you chose "*%(NUM_TOP_LEFT)s*"
  as your origin, well A01 will be column number *1* and A12 will be
  column number *12*. If you chose "*%(NUM_TOP_RIGHT)s*", A01 and A12 will
  be *12* and *1*, respectively.
  """
          % globals(),
      )
  
      self.second_spot_coordinates = Coordinates(
          "Coordinates of the second cell",
          (0, 0),
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_COORDINATES)s" to
  define the grid)*
  
  This setting defines the location of the second of two cells in your
  grid. You should enter the coordinates of the center of the cell. You
  can display an image of your grid and use the pixel coordinate
  display to determine the coordinates (X,Y) of the center of your cell.
  """
          % globals(),
      )
  
      self.second_spot_row = Integer(
          "Row number of the second cell",
          1,
          minval=1,
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_COORDINATES)s" to
  define the grid)*
  
  Enter the row index for the second cell here. Rows are numbered starting
  at the origin. For instance, if you chose "*%(NUM_TOP_LEFT)s*" as your
  origin, well A01 will be row number 1 and H01 will be row number 8. If
  you chose "*%(NUM_BOTTOM_LEFT)s*", A01 will be row number 8 and H01 will
  be row number 12.
  """
          % globals(),
      )
  
      self.second_spot_col = Integer(
          "Column number of the second cell",
          1,
          minval=1,
          doc="""\
  *(Used only if you selected "%(AM_MANUAL)s" and "%(MAN_COORDINATES)s" to
  define the grid)*
  
  Enter the column index for the second cell here. Columns are numbered
  starting at the origin. For instance, if you chose "*%(NUM_TOP_LEFT)s*"
  as your origin, well A01 will be column number 1 and A12 will be column
  number 12. If you chose "*%(NUM_TOP_RIGHT)s*", A01 and A12 will be 12
  and 1, respectively.
  """
          % globals(),
      )
  
      self.wants_image = Binary(
          "Retain an image of the grid?",
          False,
          doc="""\
  Select "*Yes*" to retain an image of the grid for use later in the
  pipeline. This module can create an annotated image of the grid that can
  be saved using the **SaveImages** module.
  """
          % globals(),
      )
  
      self.display_image_name = ImageSubscriber(
          "Select the image on which to display the grid",
          "Leave blank",
          can_be_blank=True,
          doc="""\
  *(Used only if saving an image of the grid)*
  
  Enter the name of the image that should be used as the background for
  annotations (grid lines and grid indexes). This image will be used for
  the figure and for the saved image.
  """,
      )
  
      self.save_image_name = ImageName(
          "Name the output image",
          "Grid",
          doc="""\
  *(Used only if retaining an image of the grid for use later in the
  pipeline)*
  
  Enter the name you want to use for the output image. You can save this
  image using the **SaveImages** module.
  """,
      )
  
      self.failed_grid_choice = Choice(
          "Use a previous grid if gridding fails?",
          [FAIL_NO, FAIL_ANY_PREVIOUS, FAIL_FIRST],
          doc="""\
  If the gridding fails, this setting allows you to control how the module
  responds to the error:
  
  -  *%(FAIL_NO)s:* The module will stop the pipeline if gridding fails.
  -  *%(FAIL_ANY_PREVIOUS)s:* The module will use the the most recent
     successful gridding.
  -  *%(FAIL_FIRST)s:* The module will use the first gridding.
  
  Note that the pipeline will stop in all cases if gridding fails on the
  first image.
  """
          % globals(),
      )
  
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 1 for calling the DefineGrid obj's underlying member method code with example usage: obj.create_settings() without return types