docstr: Calculate a threshold for this image
docstr_tok: ["Calculate","a","threshold","for","this","image"]
code: def calculate_thresholds(self, image_group, workspace):
      """Calculate a threshold for this image"""
      result = []
      all_threshold_groups = self.get_all_threshold_groups(image_group)
  
      for image_name in self.images_to_process(image_group, workspace):
          image = workspace.image_set.get_image(
              image_name, must_be_grayscale=True
          )
  
          # TODO: works on 2D slice of image, i suspect the thresholding methods in centrosome aren't working in 3D
          pixel_data = image.pixel_data.astype(numpy.float32)
  
          for threshold_group in all_threshold_groups:
              threshold_method = threshold_group.threshold_algorithm
              object_fraction = threshold_group.object_fraction.value
              two_class_otsu = (
                  threshold_group.two_class_otsu.value == O_TWO_CLASS
              )
              use_weighted_variance = (
                  threshold_group.use_weighted_variance.value
                  == O_WEIGHTED_VARIANCE
              )
              assign_middle_to_foreground = (
                  threshold_group.assign_middle_to_foreground.value
                  == O_FOREGROUND
              )
              (local_threshold, global_threshold) = (
                  centrosome.threshold.get_threshold(
                      threshold_method,
                      centrosome.threshold.TM_GLOBAL,
                      pixel_data,
                      mask=image.mask,
                      object_fraction=object_fraction,
                      two_class_otsu=two_class_otsu,
                      use_weighted_variance=use_weighted_variance,
                      assign_middle_to_foreground=assign_middle_to_foreground,
                  )
                  if image.has_mask
                  else centrosome.threshold.get_threshold(
                      threshold_method,
                      centrosome.threshold.TM_GLOBAL,
                      pixel_data,
                      object_fraction=object_fraction,
                      two_class_otsu=two_class_otsu,
                      use_weighted_variance=use_weighted_variance,
                      assign_middle_to_foreground=assign_middle_to_foreground,
                  )
              )
  
              scale = threshold_group.threshold_scale
              if scale is None:
                  threshold_description = threshold_method
              else:
                  threshold_description = threshold_method + " " + scale
              workspace.add_measurement(
                  "Image",
                  threshold_group.threshold_feature_name(image_name),
                  global_threshold,
              )
              result += [
                  [
                      "{} {} threshold".format(
                          image_name, threshold_description
                      ),
                      str(global_threshold),
                  ]
              ]
  
      return result
  
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 34 for calling the MeasureImageQuality obj's underlying member method code with example usage: obj.calculate_thresholds(image_group, workspace) and returns: result