docstr: Upgrade from previous versions of setting formats
docstr_tok: ["Upgrade","from","previous","versions","of","setting","formats"]
code: def upgrade_settings(
      self, setting_values, variable_revision_number, module_name
  ):
      """Upgrade from previous versions of setting formats"""
  
      if variable_revision_number == 1:
          # add power spectrum calculations
          assert len(setting_values) % 7 == 0
          num_images = len(setting_values) / 7
          new_settings = []
          for idx in range(num_images):
              new_settings += setting_values[(idx * 7) : (idx * 7 + 7)]
              new_settings += ["Yes"]
          setting_values = new_settings
          variable_revision_number = 2
  
      if variable_revision_number == 2:
          # add otsu threshold settings
          assert len(setting_values) % 8 == 0
          num_images = len(setting_values) / 8
          new_settings = []
          for idx in range(num_images):
              new_settings += setting_values[(idx * 8) : (idx * 8 + 8)]
              new_settings += [
                  O_TWO_CLASS,
                  O_WEIGHTED_VARIANCE,
                  O_FOREGROUND,
              ]
          setting_values = new_settings
          variable_revision_number = 3
  
      if variable_revision_number == 3:
          # Rearrangement/consolidation of settings
          assert len(setting_values) % SETTINGS_PER_GROUP_V3 == 0
          num_images = len(setting_values) // SETTINGS_PER_GROUP_V3
  
          """Since some settings are new/consolidated and can be repeated, handle
              the old settings by using a dict"""
          # Initialize the dictionary by image name
          d = {}
          unique_image_names = []
          for idx in range(num_images):
              # Get the settings belonging to each image
              im_settings = setting_values[
                  (idx * SETTINGS_PER_GROUP_V3) : (
                      idx * SETTINGS_PER_GROUP_V3
                      + SETTINGS_PER_GROUP_V3
                  )
              ]
              unique_image_names += [im_settings[0]]
          unique_image_names = sorted(
              set(unique_image_names), key=unique_image_names.index
          )
          # Assume that the user doesn't want blur and thresholds
          for image_name in unique_image_names:
              d[image_name] = {}
              d[image_name]["wants_scaling"] = True
              d[image_name]["wants_saturation"] = False
              d[image_name]["wants_blur"] = False
              d[image_name]["blur_scales"] = []
              d[image_name]["wants_intensity"] = True
              d[image_name]["wants_threshold"] = False
              d[image_name]["threshold_methods"] = []
  
          for idx in range(num_images):
              im_settings = setting_values[
                  (idx * SETTINGS_PER_GROUP_V3) : (
                      idx * SETTINGS_PER_GROUP_V3
                      + SETTINGS_PER_GROUP_V3
                  )
              ]
              image_name = im_settings[0]
              # Set blur and thresholds if the user sets any of the setting groups.
              d[image_name]["wants_saturation"] = d[image_name][
                  "wants_saturation"
              ] or (im_settings[3] == "Yes")
              d[image_name]["wants_blur"] = d[image_name][
                  "wants_blur"
              ] or (im_settings[1] == "Yes" or im_settings[7] == "Yes")
              d[image_name]["wants_threshold"] = d[image_name][
                  "wants_threshold"
              ] or (im_settings[4] == "Yes")
              #  Collect blur scales and threshold methods
              d[image_name]["blur_scales"] += [im_settings[2]]
              d[image_name]["threshold_methods"] += [
                  im_settings[5:7] + im_settings[8:]
              ]
  
          # Uniquify the scales and threshold methods
          import itertools
  
          for image_name in list(d.keys()):
              d[image_name]["blur_scales"] = list(
                  set(d[image_name]["blur_scales"])
              )
              d[image_name]["threshold_methods"] = [
                  k
                  for k, v in itertools.groupby(
                      sorted(d[image_name]["threshold_methods"])
                  )
              ]
  
          # Create the new settings
          new_settings = [
              O_SELECT,
              str(len(unique_image_names)),
          ]  # images_choice, image_count
          new_settings += [
              str(len(d[image_name]["blur_scales"]))
              for image_name in unique_image_names
          ]  # scale_count
          new_settings += [
              str(len(d[image_name]["threshold_methods"]))
              for image_name in unique_image_names
          ]  # threshold_count
          for image_name in unique_image_names:
              new_settings += [
                  image_name,  # image_name
                  "Yes"
                  if d[image_name]["wants_scaling"]
                  else "No",  # include_image_scalings
                  "Yes" if d[image_name]["wants_blur"] else "No",
              ]  # check_blur
              new_settings += [
                  k for k in d[image_name]["blur_scales"]
              ]  # scale
              new_settings += [
                  "Yes" if d[image_name]["wants_saturation"] else "No",
                  # check_saturation
                  "Yes" if d[image_name]["wants_intensity"] else "No",
                  # check_intensity
                  "Yes" if d[image_name]["wants_threshold"] else "No",
                  # calculate_threshold,
                  "No",
              ]  # use_all_threshold_methods
              for k in d[image_name]["threshold_methods"]:
                  # threshold_method, object_fraction, two_class_otsu,
                  # use_weighted_variance, assign_middle_to_foreground
                  new_settings += k
  
          setting_values = new_settings
          variable_revision_number = 4
  
      if variable_revision_number == 4:
          # Thresholding method name change: Strip off "Global"
          thresh_dict = dict(
              list(
                  zip(
                      centrosome.threshold.TM_GLOBAL_METHODS,
                      centrosome.threshold.TM_METHODS,
                  )
              )
          )
          # Naturally, this method assumes that the user didn't name their images "Otsu Global" or something similar
          setting_values = [
              thresh_dict[x]
              if x in centrosome.threshold.TM_GLOBAL_METHODS
              else x
              for x in setting_values
          ]
          variable_revision_number = 5
      if variable_revision_number == 5:
          if setting_values[0] == "Select...":
              num_images = setting_values[1]
              metadata_end = int(num_images) * 2
              num_settings = [
                  int(setting_values[i + 2])
                  + int(setting_values[i + 3] * 5)
                  for i in range(0, metadata_end, 2)
              ]
  
              to_unpack = setting_values[2 + metadata_end :]
              new_setting_values = setting_values[: 2 + metadata_end]
              while to_unpack:
                  image_names = to_unpack[0]
                  split_image_names = image_names.split(",")
                  new_image_names = ", ".join(
                      map(str, split_image_names)
                  )
                  num_moresettings = num_settings.pop(0)
                  new_setting_values.append(new_image_names)
                  new_setting_values += to_unpack[
                      1 : 2 + num_moresettings
                  ]
                  to_unpack = to_unpack[2 + num_moresettings :]
              setting_values = new_setting_values
          variable_revision_number = 6
      return setting_values, variable_revision_number
  
imports: import itertools
  import logging
  import cellprofiler_core.utilities.image
  import centrosome.cpmorphology
  import centrosome.haralick
  import centrosome.radial_power_spectrum
  import centrosome.threshold
  import centrosome.threshold
  import numpy
  import scipy.linalg.basic
  import scipy.ndimage
  from cellprofiler_core.constants.image import C_SCALING
  from cellprofiler_core.constants.measurement import COLTYPE_FLOAT, EXPERIMENT, MCA_AVAILABLE_POST_RUN
  from cellprofiler_core.constants.module._identify import O_WEIGHTED_VARIANCE, O_ENTROPY, O_FOREGROUND, O_BACKGROUND
  from cellprofiler_core.module import Module
  from cellprofiler_core.preferences import get_headless
  from cellprofiler_core.setting import Divider, HiddenCount, SettingsGroup, Binary, ValidationError
  from cellprofiler_core.setting.choice import Choice
  from cellprofiler_core.setting.do_something import DoSomething, RemoveSettingButton
  from cellprofiler_core.setting.subscriber import ImageListSubscriber
  from cellprofiler_core.setting.text import ImageName, Integer, Float
  from cellprofiler.modules.threshold import O_THREE_CLASS, O_TWO_CLASS
label_desc: Use image node_id 39 for calling the MeasureImageQuality obj's underlying member method code with example usage: obj.upgrade_settings(setting_values, variable_revision_number, module_name) and returns: setting_values,  variable_revision_number