def test_li_camera_image(): image = util.img_as_ubyte(camerad) threshold = threshold_li(image) ce_actual = _cross_entropy(image, threshold) assert 62 < threshold_li(image) < 63 assert ce_actual < _cross_entropy(image, threshold + 1) assert ce_actual < _cross_entropy(image, threshold - 1)
def test_li_arbitrary_start_point(): cell = celld max_stationary_point = threshold_li(cell) low_stationary_point = threshold_li(cell, initial_guess=float( cp.percentile(cell, 5))) optimum = threshold_li(cell, initial_guess=float(cp.percentile(cell, 95))) assert 67 < max_stationary_point < 68 assert 48 < low_stationary_point < 49 assert 111 < optimum < 112
def test_li_coins_image(): image = util.img_as_ubyte(coinsd) threshold = threshold_li(image) ce_actual = _cross_entropy(image, threshold) assert 94 < threshold_li(image) < 95 assert ce_actual < _cross_entropy(image, threshold + 1) # in the case of the coins image, the minimum cross-entropy is achieved one # threshold below that found by the iterative method. Not sure why that is # but `threshold_li` does find the stationary point of the function (ie the # tolerance can be reduced arbitrarily but the exact same threshold is # found), so my guess is some kind of histogram binning effect. assert ce_actual < _cross_entropy(image, threshold - 2)
def test_li_astro_image(): image = util.img_as_ubyte(astronautd) threshold = threshold_li(image) ce_actual = _cross_entropy(image, threshold) assert 64 < threshold < 65 assert ce_actual < _cross_entropy(image, threshold + 1) assert ce_actual < _cross_entropy(image, threshold - 1)
def test_li_pathological_arrays(): # See https://github.com/scikit-image/scikit-image/issues/4140 a = cp.asarray([0, 0, 1, 0, 0, 1, 0, 1]) b = cp.asarray([0, 0, 0.1, 0, 0, 0.1, 0, 0.1]) c = cp.asarray([0, 0, 0.1, 0, 0, 0.1, 0.01, 0.1]) d = cp.asarray([0, 0, 1, 0, 0, 1, 0.5, 1]) e = cp.asarray([1, 1]) f = cp.asarray([1, 2]) arrays = [a, b, c, d, e, f] thresholds = cp.asarray([float(threshold_li(arr)) for arr in arrays]) assert cp.all(cp.isfinite(thresholds))
def test_li_constant_image(self): assert threshold_li(cp.ones((10, 10))) == 1.0
def test_li_float_image(self): image = self.image.astype(float) assert 2 < threshold_li(image) < 3
def test_li_negative_int(self): image = self.image - 2 assert 0 < threshold_li(image) < 1
def test_li(self): assert 2 < threshold_li(self.image) < 3
def test_li_negative_inital_guess(): with testing.raises(ValueError): threshold_li(coinsd, initial_guess=-5)
def test_li_constant_image_with_nan(): image = cp.asarray([8, 8, 8, 8, cp.nan]) assert threshold_li(image) == 8
def test_li_inf_minus_inf(): image = cp.array([cp.inf, -cp.inf]) assert threshold_li(image) == 0
def test_li_inf_image(): image = cp.array([cp.inf, cp.nan]) assert threshold_li(image) == cp.inf
def test_li_nan_image(): image = cp.full((5, 5), cp.nan) assert cp.isnan(threshold_li(image))
def test_li_coins_image_as_float(): coins_float = util.img_as_float(coinsd) assert 94 / 255 < threshold_li(coins_float) < 95 / 255