def test_threshold_otsu_partial_mask_uniform_data(): numpy.random.seed(73) data = numpy.random.rand(10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[2:5, 2:5] = True data[mask] = 0.2 workspace, module = make_workspace(data, mask=mask) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS module.adaptive_window_size.value = 3 t_local, t_global = module.get_threshold(image, workspace) t_global_expected = 0.2 t_local_expected = 0.7 * t_global_expected * numpy.ones_like(data) numpy.testing.assert_array_almost_equal(t_local, t_local_expected) numpy.testing.assert_almost_equal(t_global, t_global_expected)
def test_threshold_otsu_uniform_data(): data = numpy.ones((10, 10), dtype=numpy.float32) data *= 0.2 workspace, module = make_workspace(data) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS module.adaptive_window_size.value = 3 t_local, t_global = module.get_threshold(image, workspace) t_global_expected = 0.2 t_local_expected = 0.7 * t_global_expected * numpy.ones_like(data) numpy.testing.assert_array_almost_equal(t_local, t_local_expected) numpy.testing.assert_almost_equal(t_global, t_global_expected)
def run_local_otsu_2(self, image, workspace, module): module.threshold_scope.value = "Adaptive" module.global_operation.value = "Otsu" module.two_class_otsu.value = "Two classes" module.adaptive_window_size.value = self.adaptive_window_size.value t_final, t_orig = module.get_threshold(image, workspace) return t_final, t_orig
def run_global_otsu_3b(self, image, workspace, module): module.threshold_scope.value = "Global" module.global_operation.value = "Otsu" module.two_class_otsu.value = "Three classes" module.assign_middle_to_foreground.value = "Background" t_final, t_orig = module.get_threshold(image, workspace) return t_final, t_orig
def test_threshold_otsu_full_mask(): numpy.random.seed(73) data = numpy.random.rand(10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) workspace, module = make_workspace(data, mask=mask) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS module.adaptive_window_size.value = 3 t_local, t_global = module.get_threshold(image, workspace) t_local_expected = numpy.zeros_like(data) t_global_expected = 0.0 numpy.testing.assert_array_equal(t_local, t_local_expected) assert t_global == t_global_expected
def test_threshold_li_image_automatic(): numpy.random.seed(73) data = numpy.random.rand(10, 10) workspace, module = make_workspace(data) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_GLOBAL module.global_operation.value = cellprofiler.modules.threshold.TM_LI module.threshold_range.maximum = 0.0 # expected to be ignored t_local, t_global = module.get_threshold(image, workspace, automatic=True) expected = skimage.filters.threshold_li(data) assert t_local != 0.0 assert t_global != 0.0 numpy.testing.assert_almost_equal(t_local, expected) numpy.testing.assert_almost_equal(t_global, expected)
def test_threshold_otsu3_volume(): numpy.random.seed(73) data = numpy.random.rand(10, 10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[1:-1, 1:-1, 1:-1] = True workspace, module = make_workspace(data, mask=mask, dimensions=3) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_THREE_CLASS module.assign_middle_to_foreground.value = ( cellprofiler.modules.threshold.O_FOREGROUND) module.adaptive_window_size.value = 3 t_local, t_global = module.get_threshold(image, workspace) t_global_expected = centrosome.threshold.get_otsu_threshold( image.pixel_data, image.mask, two_class_otsu=False, assign_middle_to_foreground=True, ) t_local_expected = numpy.zeros_like(data) for index, plane in enumerate(data): t_local_expected[index] = centrosome.threshold.get_adaptive_threshold( centrosome.threshold.TM_OTSU, plane, t_global_expected, mask=mask[index], adaptive_window_size=3, two_class_otsu=False, assign_middle_to_foreground=True, ) t_min = t_global_expected * 0.7 t_max = min(1.0, t_global_expected * 1.5) t_local_expected[t_local_expected < t_min] = t_min t_local_expected[t_local_expected > t_max] = t_max numpy.testing.assert_almost_equal(t_global, t_global_expected) assert t_local.ndim == 3 numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
def run_local_otsu_3b(self, image, workspace, module): module.threshold_scope.value = "Adaptive" module.global_operation.value = "Otsu" module.two_class_otsu.value = "Three classes" module.assign_middle_to_foreground.value = "Background" module.adaptive_window_size.value = self.adaptive_window_size.value t_final, t_orig = module.get_threshold(image, workspace) return t_final, t_orig
def run_robustbackground(self, image, workspace, module): module.threshold_scope.value = "Global" module.global_operation.value = "RobustBackground" module.averaging_method.value = self.averaging_method.value module.variance_method.value = self.variance_method.value module.number_of_deviations.value = self.number_of_deviations.value module.lower_outlier_fraction.value = self.lower_outlier_fraction.value module.upper_outlier_fraction.value = self.upper_outlier_fraction.value t_final, t_orig = module.get_threshold(image, workspace) return t_final, t_orig
def test_threshold_li_uniform_image(): workspace, module = make_workspace(0.1 * numpy.ones((10, 10))) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_GLOBAL module.global_operation.value = cellprofiler.modules.threshold.TM_LI t_local, t_global = module.get_threshold(image, workspace) numpy.testing.assert_almost_equal(t_local, 0.1) numpy.testing.assert_almost_equal(t_global, 0.1)
def test_threshold_otsu_volume(): numpy.random.seed(73) data = numpy.random.rand(10, 10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[1:-1, 1:-1, 1:-1] = True workspace, module = make_workspace(data, mask=mask, dimensions=3) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS module.adaptive_window_size.value = 3 t_local, t_global = module.get_threshold(image, workspace) t_global_expected = skimage.filters.threshold_otsu(data[mask]) data = skimage.img_as_ubyte(data) t_local_expected = numpy.zeros_like(data) for index, plane in enumerate(data): t_local_expected[index] = skimage.filters.rank.otsu( plane, skimage.morphology.square(3), mask=mask[index]) t_local_expected = skimage.img_as_float(t_local_expected) t_min = 0.7 * t_global_expected t_max = min(1.0, 1.5 * t_global_expected) t_local_expected[t_local_expected < t_min] = t_min t_local_expected[t_local_expected > t_max] = t_max numpy.testing.assert_almost_equal(t_global, t_global_expected) numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
def test_threshold_otsu3_image(): numpy.random.seed(73) data = numpy.random.rand(10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) mask[1:-1, 1:-1] = True workspace, module = make_workspace(data, mask=mask) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE module.global_operation.value = centrosome.threshold.TM_OTSU module.two_class_otsu.value = cellprofiler.modules.threshold.O_THREE_CLASS module.assign_middle_to_foreground.value = ( cellprofiler.modules.threshold.O_FOREGROUND) module.adaptive_window_size.value = 3 t_local, t_global = module.get_threshold(image, workspace) t_local_expected, t_global_expected = centrosome.threshold.get_threshold( centrosome.threshold.TM_OTSU, cellprofiler.modules.threshold.TS_ADAPTIVE, data, mask=mask, adaptive_window_size=3, threshold_range_min=0.0, threshold_range_max=1.0, threshold_correction_factor=1.0, two_class_otsu=False, assign_middle_to_foreground=True, ) numpy.testing.assert_almost_equal(t_global, t_global_expected) numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
def test_threshold_li_volume(): numpy.random.seed(73) data = numpy.random.rand(10, 10, 10) workspace, module = make_workspace(data, dimensions=3) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_GLOBAL module.global_operation.value = cellprofiler.modules.threshold.TM_LI t_local, t_global = module.get_threshold(image, workspace) expected = skimage.filters.threshold_li(data) numpy.testing.assert_almost_equal(t_local, expected) numpy.testing.assert_almost_equal(t_global, expected)
def test_threshold_li_full_mask(): numpy.random.seed(73) data = numpy.random.rand(10, 10) mask = numpy.zeros_like(data, dtype=numpy.bool) workspace, module = make_workspace(data, mask) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_GLOBAL module.global_operation.value = cellprofiler.modules.threshold.TM_LI t_local, t_global = module.get_threshold(image, workspace) numpy.testing.assert_almost_equal(t_local, 0.0) numpy.testing.assert_almost_equal(t_global, 0.0)
def test_threshold_robust_background_mean_mad_volume(): numpy.random.seed(73) data = numpy.random.rand(10, 10, 10) workspace, module = make_workspace(data, dimensions=3) image = workspace.image_set.get_image(INPUT_IMAGE_NAME) module.threshold_scope.value = cellprofiler.modules.threshold.TS_GLOBAL module.global_operation.value = centrosome.threshold.TM_ROBUST_BACKGROUND module.averaging_method.value = cellprofiler.modules.threshold.RB_MEAN module.variance_method.value = cellprofiler.modules.threshold.RB_MAD t_local, t_global = module.get_threshold(image, workspace) t_local_expected, t_global_expected = centrosome.threshold.get_threshold( centrosome.threshold.TM_ROBUST_BACKGROUND, cellprofiler.modules.threshold.TS_GLOBAL, data, threshold_range_min=0.0, threshold_range_max=1.0, threshold_correction_factor=1.0, lower_outlier_fraction=0.05, upper_outlier_fraction=0.05, deviations_above_average=2, average_fn=numpy.mean, variance_fn=centrosome.threshold.mad, ) numpy.testing.assert_almost_equal(t_local, t_local_expected) numpy.testing.assert_almost_equal(t_global, t_global_expected)
def run_mce(self, image, workspace, module): module.threshold_scope.value = "Global" module.global_operation.value = "Minimum cross entropy" t_final, t_orig = module.get_threshold(image, workspace) return t_final, t_orig
def run_global_otsu_2(self, image, workspace, module): module.threshold_scope.value = "Global" module.global_operation.value = "Otsu" module.two_class_otsu.value = "Two classes" t_final, t_orig = module.get_threshold(image, workspace) return t_final, t_orig