def __run_boundary_difference_of_means_test(self, label, gradient, expected_result, msg=''): label = numpy.asarray(label) gradient = numpy.asarray(gradient) graph = GCGraphTest( numpy.unique(label).size, math.pow(numpy.unique(label).size, 2)) boundary_difference_of_means(graph, label, (gradient)) graph.validate_nweights(self, expected_result, msg)
def test_boundary_difference_of_means_3d(self): """Test the @link medpy.graphcut.boundary_difference_of_means() function for 3D.""" # 3D VERSION # the gradient magnitude image original = [[[0., 0., 0., 0.1, 0.1, 0.5], [0., 0., 0., 0.1, 0.1, 0.1], [0., 0.3, 0.3, 0.2, 0.2, 0.2], [0., 0., 0.3, 0.3, 0.3, 0.2], [0., 0., 0.3, 0.2, 0.2, 0.4]], [[0., 0., 0., 0.1, 0.1, 0.6], [0., 0., 0., 0.1, 0.1, 0.1], [0., 0.3, 0.3, 0.2, 0.2, 0.2], [0., 0., 0.3, 0.3, 0.3, 0.2], [0., 0., 0.3, 0.2, 0.2, 0.4]]] # the label image (labels have to start from 0) label = [[[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]], [[0, 0, 0, 1, 1, 6], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]]] # the expected result expected_result = { (0, 1): 0.833333333, (0, 3): 0.5, (1, 2): 0.833333333, (1, 5): 0.333333333, (1, 6): 0.166666667, (2, 3): 0.833333333, (2, 4): 0.666666667, (5, 6): 0.833333333 } # only 3D edge expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_difference_of_means(label, (original)) result = self._reorder_keys(result) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_difference_of_means_3d(self): """Test the @link medpy.graphcut.boundary_difference_of_means() function for 3D.""" # 3D VERSION # the gradient magnitude image original = [ [ [0.0, 0.0, 0.0, 0.1, 0.1, 0.5], [0.0, 0.0, 0.0, 0.1, 0.1, 0.1], [0.0, 0.3, 0.3, 0.2, 0.2, 0.2], [0.0, 0.0, 0.3, 0.3, 0.3, 0.2], [0.0, 0.0, 0.3, 0.2, 0.2, 0.4], ], [ [0.0, 0.0, 0.0, 0.1, 0.1, 0.6], [0.0, 0.0, 0.0, 0.1, 0.1, 0.1], [0.0, 0.3, 0.3, 0.2, 0.2, 0.2], [0.0, 0.0, 0.3, 0.3, 0.3, 0.2], [0.0, 0.0, 0.3, 0.2, 0.2, 0.4], ], ] # the label image (labels have to start from 0) label = [ [[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]], [[0, 0, 0, 1, 1, 6], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]], ] # the expected result expected_result = { (0, 1): 0.833333333, (0, 3): 0.5, (1, 2): 0.833333333, (1, 5): 0.333333333, (1, 6): 0.166666667, (2, 3): 0.833333333, (2, 4): 0.666666667, (5, 6): 0.833333333, } # only 3D edge expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_difference_of_means(label, (original)) result = self._reorder_keys(result) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_difference_of_means_2d(self): """Test the @link medpy.graphcut.boundary_difference_of_means() function for 2D.""" # the original image original = [ [0.0, 0.0, 0.0, 0.1, 0.1, 0.5], [0.0, 0.0, 0.0, 0.1, 0.1, 0.1], [0.0, 0.3, 0.3, 0.2, 0.2, 0.2], [0.0, 0.0, 0.3, 0.3, 0.3, 0.2], [0.0, 0.0, 0.3, 0.2, 0.2, 0.4], ] # the label image (labels have to start from 0) label = [[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]] # the expected result expected_result = {(0, 1): 0.8, (0, 3): 0.4, (1, 2): 0.8, (1, 5): 0.2, (2, 3): 0.8, (2, 4): 0.6} expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_difference_of_means(label, (original)) result = self._reorder_keys(result) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_difference_of_means_2d(self): """Test the @link medpy.graphcut.boundary_difference_of_means() function for 2D.""" # the original image original = [[0., 0., 0., 0.1, 0.1, 0.5], [0., 0., 0., 0.1, 0.1, 0.1], [0., 0.3, 0.3, 0.2, 0.2, 0.2], [0., 0., 0.3, 0.3, 0.3, 0.2], [0., 0., 0.3, 0.2, 0.2, 0.4]] # the label image (labels have to start from 0) label = [[0, 0, 0, 1, 1, 5], [0, 0, 0, 1, 1, 1], [0, 3, 3, 2, 2, 2], [0, 0, 3, 3, 3, 2], [0, 0, 3, 2, 2, 4]] # the expected result expected_result = { (0, 1): 0.8, (0, 3): 0.4, (1, 2): 0.8, (1, 5): 0.2, (2, 3): 0.8, (2, 4): 0.6 } expected_result = self.__to_two_directed(expected_result) # run the function result = boundary_difference_of_means(label, (original)) result = self._reorder_keys(result) # check returned values self.__compare_dictionaries(result, expected_result)
def test_boundary_difference_of_means_borders(self): """Test the @link medpy.graphcut.boundary_difference_of_means() border conditions.""" # TEST1: test for a label image with not continuous label ids not starting from 0 label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test1') # TEST2: test for a label image with negative labels label = [[-1, 4, 8], [-1, 3, 10], [1, -3, 10]] expected_result = { (-1, 1): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (-1, 3): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (-3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (-3, 1): (sys.float_info.min, sys.float_info.min), (-3, 3): (sys.float_info.min, sys.float_info.min), (-1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test2') # TEST3: test for behavior on occurrence of very small (~0) and 1 weights gradient = [[0., 0., 0.], [0., 0., sys.float_info.max]] label = [[0, 1, 2], [0, 1, 3]] expected_result = { (0, 1): (1.0, 1.0), (1, 2): (1.0, 1.0), (1, 3): (sys.float_info.min, sys.float_info.min), (2, 3): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means(label, (gradient)) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test3') # TEST4: check behavior for integer gradient image label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label) expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means( label, (scipy.zeros(label.shape, scipy.int_))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test4') # TEST5: reaction to different array orders label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label, order='C') # C-order, gradient same order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test5 (C,C)') label = scipy.asarray(label, order='F') # Fortran order, gradient same order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test5 (F, F)') label = scipy.asarray(label, order='C') # C-order, gradient different order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means( label, (scipy.zeros(label.shape, order='F'))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test5 (C, F)') label = scipy.asarray(label, order='F') # F-order, gradient different order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min) } result = boundary_difference_of_means( label, (scipy.zeros(label.shape, order='C'))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, 'Test5 (F, C)')
def test_boundary_difference_of_means_borders(self): """Test the @link medpy.graphcut.boundary_difference_of_means() border conditions.""" # TEST1: test for a label image with not continuous label ids not starting from 0 label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test1") # TEST2: test for a label image with negative labels label = [[-1, 4, 8], [-1, 3, 10], [1, -3, 10]] expected_result = { (-1, 1): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (-1, 3): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (-3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (-3, 1): (sys.float_info.min, sys.float_info.min), (-3, 3): (sys.float_info.min, sys.float_info.min), (-1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test2") # TEST3: test for behavior on occurrence of very small (~0) and 1 weights gradient = [[0.0, 0.0, 0.0], [0.0, 0.0, sys.float_info.max]] label = [[0, 1, 2], [0, 1, 3]] expected_result = { (0, 1): (1.0, 1.0), (1, 2): (1.0, 1.0), (1, 3): (sys.float_info.min, sys.float_info.min), (2, 3): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (gradient)) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test3") # TEST4: check behavior for integer gradient image label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label) expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros(label.shape, scipy.int_))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test4") # TEST5: reaction to different array orders label = [[1, 4, 8], [1, 3, 10], [1, 3, 10]] label = scipy.asarray(label, order="C") # C-order, gradient same order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test5 (C,C)") label = scipy.asarray(label, order="F") # Fortran order, gradient same order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros_like(label))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test5 (F, F)") label = scipy.asarray(label, order="C") # C-order, gradient different order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros(label.shape, order="F"))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test5 (C, F)") label = scipy.asarray(label, order="F") # F-order, gradient different order expected_result = { (1, 3): (sys.float_info.min, sys.float_info.min), (4, 8): (sys.float_info.min, sys.float_info.min), (3, 10): (sys.float_info.min, sys.float_info.min), (8, 10): (sys.float_info.min, sys.float_info.min), (1, 4): (sys.float_info.min, sys.float_info.min), (3, 4): (sys.float_info.min, sys.float_info.min), } result = boundary_difference_of_means(label, (scipy.zeros(label.shape, order="C"))) result = self._reorder_keys(result) self.__compare_dictionaries(result, expected_result, "Test5 (F, C)")