docstr: Reorder the image sets and assign group number and index
docstr_tok: ["Reorder","the","image","sets","and","assign","group","number","and","index"]
code: def prepare_run(self, workspace, changed_setting=False):
      """Reorder the image sets and assign group number and index"""
      if workspace.pipeline.in_batch_mode():
          return True
  
      if not self.wants_groups:
          return True
  
      for group in self.grouping_metadata:
          if group.metadata_choice.value == "None":
              return False
  
      if len(workspace.measurements.get_image_numbers()) == 0:
          # Refresh image set to make sure it's actually empty, if and only if there was a settings change
          if changed_setting:
              workspace.refresh_image_set()
              if len(workspace.measurements.get_image_numbers()) == 0:
                  return False
          else:
              return False
  
      result = self.get_groupings(workspace)
      if result is None:
          return False
      key_list, groupings = result
      #
      # Create arrays of group number, group_index and image_number
      #
      group_numbers = numpy.hstack(
          [
              numpy.ones(len(image_numbers), int) * (i + 1)
              for i, (keys, image_numbers) in enumerate(groupings)
          ]
      )
      group_indexes = numpy.hstack(
          [
              numpy.arange(len(image_numbers)) + 1
              for keys, image_numbers in groupings
          ]
      )
      group_lens = numpy.hstack(
          [
              numpy.ones(len(image_numbers), int) * (len(image_numbers))
              for i, (keys, image_numbers) in enumerate(groupings)
          ]
      )
      image_numbers = numpy.hstack(
          [image_numbers for keys, image_numbers in groupings]
      )
      order = numpy.lexsort((group_indexes, group_numbers))
      group_numbers = group_numbers[order]
      group_indexes = group_indexes[order]
      group_lens = group_lens[order]
  
      m = workspace.measurements
      assert isinstance(m, Measurements)
      #
      # Downstream processing requires that image sets be ordered by
      # increasing group number, then increasing group index.
      #
      new_image_numbers = numpy.zeros(numpy.max(image_numbers) + 1, int)
      new_image_numbers[image_numbers[order]] = (
          numpy.arange(len(image_numbers)) + 1
      )
      m.reorder_image_measurements(new_image_numbers)
      m.add_all_measurements(
          "Image",
          GROUP_NUMBER,
          group_numbers,
      )
      m.add_all_measurements(
          "Image",
          GROUP_INDEX,
          group_indexes,
      )
      m.add_all_measurements(
          "Image",
          GROUP_LENGTH,
          group_lens,
      )
      m.set_grouping_tags(self.get_grouping_tags_or_metadata())
      return True
  
imports: import numpy
  from ..constants.image import CT_OBJECTS
  from ..constants.measurement import COLTYPE_INTEGER, COLTYPE_VARCHAR, IMAGE
  from ..constants.measurement import C_FILE_NAME
  from ..constants.measurement import C_METADATA
  from ..constants.measurement import C_OBJECTS_FILE_NAME
  from ..constants.measurement import C_OBJECTS_PATH_NAME
  from ..constants.measurement import C_PATH_NAME
  from ..constants.measurement import EXPERIMENT
  from ..constants.measurement import GROUP_INDEX
  from ..constants.measurement import GROUP_NUMBER
  from ..constants.measurement import GROUP_LENGTH
  from ..constants.measurement import M_GROUPING_TAGS
  from ..measurement import Measurements
  from ..module import Module
  from ..pipeline import Pipeline
  from ..setting import Binary
  from ..setting import Divider
  from ..setting import HTMLText
  from ..setting import HiddenCount
  from ..setting import SettingsGroup
  from ..setting import Table
  from ..setting.choice import Choice
  from ..setting.do_something import DoSomething
  from ..setting.do_something import RemoveSettingButton
  from ..utilities.image import image_resource
label_desc: Use image node_id 17 for calling the Groups obj's underlying member method code with example usage: obj.prepare_run(workspace, changed_setting) and returns: True, True, True, False, False, False, False