import numpy as np
from numpy.testing import assert_equal, assert_array_equal
from nose.tools import assert_greater
from skimage.segmentation import felzenszwalb


def test_grey():
    # very weak tests. This algorithm is pretty unstable.
    img = np.zeros((20, 21))
    img[:10, 10:] = 0.2
    img[10:, :10] = 0.4
    img[10:, 10:] = 0.6
    seg = felzenszwalb(img, sigma=0)
    # we expect 4 segments:
    assert_equal(len(np.unique(seg)), 4)
    # that mostly respect the 4 regions:
    for i in range(4):
        hist = np.histogram(img[seg == i], bins=[0, 0.1, 0.3, 0.5, 1])[0]
        assert_greater(hist[i], 40)


def test_color():
    # very weak tests. This algorithm is pretty unstable.
    img = np.zeros((20, 21, 3))
    img[:10, :10, 0] = 1
    img[10:, :10, 1] = 1
    img[10:, 10:, 2] = 1
    seg = felzenszwalb(img, sigma=0)
    # we expect 4 segments:
    assert_equal(len(np.unique(seg)), 4)
    assert_array_equal(seg[:10, :10], 0)
    assert_array_equal(seg[10:, :10], 2)
    assert_array_equal(seg[:10, 10:], 1)
    assert_array_equal(seg[10:, 10:], 3)


if __name__ == '__main__':
    from numpy import testing
    testing.run_module_suite()

[((9, 10, 9, 28), 'numpy.zeros', 'np.zeros', ({(9, 19, 9, 27): '(20, 21)'}, {}), '((20, 21))', True, 'import numpy as np\n'), ((13, 10, 13, 36), 'skimage.segmentation.felzenszwalb', 'felzenszwalb', (), '', False, 'from skimage.segmentation import felzenszwalb\n'), ((24, 10, 24, 31), 'numpy.zeros', 'np.zeros', ({(24, 19, 24, 30): '(20, 21, 3)'}, {}), '((20, 21, 3))', True, 'import numpy as np\n'), ((28, 10, 28, 36), 'skimage.segmentation.felzenszwalb', 'felzenszwalb', (), '', False, 'from skimage.segmentation import felzenszwalb\n'), ((31, 4, 31, 40), 'numpy.testing.assert_array_equal', 'assert_array_equal', ({(31, 23, 31, 36): 'seg[:10, :10]', (31, 38, 31, 39): '(0)'}, {}), '(seg[:10, :10], 0)', False, 'from numpy.testing import assert_equal, assert_array_equal\n'), ((32, 4, 32, 40), 'numpy.testing.assert_array_equal', 'assert_array_equal', ({(32, 23, 32, 36): 'seg[10:, :10]', (32, 38, 32, 39): '(2)'}, {}), '(seg[10:, :10], 2)', False, 'from numpy.testing import assert_equal, assert_array_equal\n'), ((33, 4, 33, 40), 'numpy.testing.assert_array_equal', 'assert_array_equal', ({(33, 23, 33, 36): 'seg[:10, 10:]', (33, 38, 33, 39): '(1)'}, {}), '(seg[:10, 10:], 1)', False, 'from numpy.testing import assert_equal, assert_array_equal\n'), ((34, 4, 34, 40), 'numpy.testing.assert_array_equal', 'assert_array_equal', ({(34, 23, 34, 36): 'seg[10:, 10:]', (34, 38, 34, 39): '(3)'}, {}), '(seg[10:, 10:], 3)', False, 'from numpy.testing import assert_equal, assert_array_equal\n'), ((39, 4, 39, 30), 'numpy.testing.run_module_suite', 'testing.run_module_suite', ({}, {}), '()', False, 'from numpy import testing\n'), ((19, 8, 19, 35), 'nose.tools.assert_greater', 'assert_greater', ({(19, 23, 19, 30): 'hist[i]', (19, 32, 19, 34): '(40)'}, {}), '(hist[i], 40)', False, 'from nose.tools import assert_greater\n'), ((15, 21, 15, 35), 'numpy.unique', 'np.unique', ({(15, 31, 15, 34): 'seg'}, {}), '(seg)', True, 'import numpy as np\n'), ((18, 15, 18, 70), 'numpy.histogram', 'np.histogram', (), '', True, 'import numpy as np\n'), ((30, 21, 30, 35), 'numpy.unique', 'np.unique', ({(30, 31, 30, 34): 'seg'}, {}), '(seg)', True, 'import numpy as np\n')]