Пример #1
0
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)
Пример #2
0
def test_threshold_li_adaptive_image():
    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_ADAPTIVE

    module.local_operation.value = cellprofiler.modules.threshold.TM_LI

    module.adaptive_window_size.value = 3

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    t_guide_expected = skimage.filters.threshold_li(data)

    t_local_expected = module._get_adaptive_threshold(data,
                                                      skimage.filters.threshold_li)

    t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected)

    numpy.testing.assert_almost_equal(t_guide, t_guide_expected)

    numpy.testing.assert_almost_equal(t_local, t_local_expected)
Пример #3
0
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)
Пример #4
0
def test_threshold_robust_background_median_sd_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_ROBUST_BACKGROUND

    module.averaging_method.value = cellprofiler.modules.threshold.RB_MEDIAN

    module.variance_method.value = cellprofiler.modules.threshold.RB_SD

    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.median,
        variance_fn=numpy.std,
    )

    numpy.testing.assert_almost_equal(t_local, t_local_expected)
Пример #5
0
def test_threshold_robust_background_adaptive():
    numpy.random.seed(73)

    data = numpy.random.rand(10, 10)

    workspace, module = make_workspace(data, dimensions=2)

    image = workspace.image_set.get_image(INPUT_IMAGE_NAME)

    module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE

    module.adaptive_window_size.value = 3

    module.local_operation.value = cellprofiler.modules.threshold.TM_ROBUST_BACKGROUND

    module.averaging_method.value = cellprofiler.modules.threshold.RB_MEAN

    module.variance_method.value = cellprofiler.modules.threshold.RB_SD

    t_local, t_uncorrected, t_guide = module.get_threshold(image, workspace)

    t_guide_expected = module.get_threshold_robust_background(data)

    t_guide_expected = module._correct_global_threshold(t_guide_expected)

    t_local_expected = module._get_adaptive_threshold(data,
                                                      module.get_threshold_robust_background)

    t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected)

    numpy.testing.assert_almost_equal(t_guide, t_guide_expected)

    numpy.testing.assert_almost_equal(t_local, t_local_expected)
Пример #6
0
def test_threshold_li_image_log():
    numpy.random.seed(73)

    data = numpy.random.rand(10, 10)

    mask = numpy.zeros_like(data, dtype=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_GLOBAL

    module.global_operation.value = cellprofiler.modules.threshold.TM_LI

    module.log_transform.value = True

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    transformed_data, d = centrosome.threshold.log_transform(data)

    t_expected = skimage.filters.threshold_li(transformed_data[mask])

    t_expected = centrosome.threshold.inverse_log_transform(t_expected, d)

    t_expected = module._correct_global_threshold(
        t_expected
    )

    numpy.testing.assert_almost_equal(t_global, t_expected, decimal=5)
Пример #7
0
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, t_guide = module.get_threshold(image, workspace)

    t_guide_expected = 0.2

    t_local_expected = module._get_adaptive_threshold(data,
                                                      skimage.filters.threshold_otsu)

    t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected)

    numpy.testing.assert_array_almost_equal(t_local, t_local_expected)

    numpy.testing.assert_almost_equal(t_guide, t_guide_expected)
Пример #8
0
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 numpy.all(t_global == t_global_expected)

    assert numpy.all(t_local == t_local_expected)
Пример #9
0
 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
Пример #10
0
 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
Пример #11
0
def test_threshold_otsu_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.local_operation.value = cellprofiler.modules.threshold.TM_OTSU

    module.two_class_otsu.value = cellprofiler.modules.threshold.O_TWO_CLASS

    module.adaptive_window_size.value = 3

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    t_guide_expected = skimage.filters.threshold_otsu(data[mask])

    t_local_expected = module._get_adaptive_threshold(numpy.where(mask, data, numpy.nan),
                                                      skimage.filters.threshold_otsu,)

    t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected)


    numpy.testing.assert_almost_equal(t_guide, t_guide_expected)

    numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
Пример #12
0
def test_threshold_sauvola_image_masked():
    numpy.random.seed(73)

    data = numpy.random.rand(10, 10)

    mask = numpy.zeros_like(data, dtype=numpy.bool)

    mask[1:3, 1:3] = True

    workspace, module = make_workspace(data, mask)

    image = workspace.image_set.get_image(INPUT_IMAGE_NAME)

    module.threshold_scope.value = cellprofiler.modules.threshold.TS_ADAPTIVE

    module.local_operation.value = cellprofiler.modules.threshold.TM_SAUVOLA

    module.adaptive_window_size.value = 3

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    t_guide_expected = skimage.filters.threshold_li(data[mask])

    t_local_expected = skimage.filters.threshold_sauvola(numpy.where(mask, data, 0), window_size=3)

    t_local_expected = module._correct_local_threshold(t_local_expected, t_guide_expected)

    numpy.testing.assert_almost_equal(t_guide, t_guide_expected)

    numpy.testing.assert_almost_equal(t_local, t_local_expected)
Пример #13
0
def test_threshold_otsu3_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_THREE_CLASS

    module.assign_middle_to_foreground.value = (
        cellprofiler.modules.threshold.O_FOREGROUND
    )

    module.adaptive_window_size.value = 3

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    t_local_expected = numpy.zeros_like(data)

    t_guide_expected = 0

    numpy.testing.assert_almost_equal(t_guide, t_guide_expected)

    numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
Пример #14
0
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)
Пример #15
0
 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
Пример #16
0
def test_threshold_otsu3_volume_log():
    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.local_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

    module.log_transform.value = True

    module.log_transform.value = True

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    transformed_data, d = centrosome.threshold.log_transform(data)

    t_guide_expected = skimage.filters.threshold_multiotsu(
        transformed_data[mask], nbins=128)[0]

    t_local_expected = numpy.zeros_like(data)
    masked = numpy.where(mask, transformed_data, numpy.nan)
    for index, plane in enumerate(masked):
        t_local_expected[index] = module._get_adaptive_threshold(
            plane,
            skimage.filters.threshold_multiotsu,
            nbins=128,
        )

    t_guide_expected = centrosome.threshold.inverse_log_transform(
        t_guide_expected, d)
    t_local_expected = centrosome.threshold.inverse_log_transform(
        t_local_expected, d)

    t_local_expected = module._correct_local_threshold(t_local_expected,
                                                       t_guide_expected)

    numpy.testing.assert_almost_equal(t_guide, t_guide_expected, decimal=5)

    assert t_local.ndim == 3

    numpy.testing.assert_array_almost_equal(t_local, t_local_expected)
Пример #17
0
 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
Пример #18
0
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)
Пример #19
0
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)
Пример #20
0
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)
Пример #21
0
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)
Пример #22
0
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)
Пример #23
0
def test_threshold_otsu3_image_log():
    numpy.random.seed(73)

    data = numpy.random.rand(10, 10)

    mask = numpy.zeros_like(data, dtype=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_GLOBAL

    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.log_transform.value = True

    t_local, t_global, t_guide = module.get_threshold(image, workspace)

    transformed_data, d = centrosome.threshold.log_transform(data)

    t_expected = skimage.filters.threshold_multiotsu(transformed_data[mask], nbins=128)[0]

    t_expected = centrosome.threshold.inverse_log_transform(t_expected, d)

    t_expected = module._correct_global_threshold(
        t_expected
    )

    numpy.testing.assert_almost_equal(t_global, t_expected, decimal=5)
Пример #24
0
 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
Пример #25
0
 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