def test_get_neighbors_derivatives_dictionary(): nx = 3 ny = 3 heights = np.array([9, 8, 7, 6, 5, 4, 3, 2, 1]) b = 2/math.sqrt(200) d = 4/math.sqrt(200) result_neighbors_derivatives_dictionary = {0: (np.array([1, 3, 4]), np.array([0.1, 0.3, d], dtype=float)), 1: (np.array([0, 2, 3, 4, 5]), np.array([-0.1, 0.1, b, 0.3, d], dtype=float)), 2: (np.array([1, 4, 5]), np.array([-0.1, b, 0.3], dtype=float)), 3: (np.array([0, 1, 4, 6, 7]), np.array([-0.3, -b, 0.1, 0.3, d], dtype=float)), 4: (np.array([0, 1, 2, 3, 5, 6, 7, 8]), np.array([-d, -0.3, -b, -0.1, 0.1, b, 0.3, d], dtype=float)), 5: (np.array([1, 2, 4, 7, 8]), np.array([-d, -0.3, -0.1, b, 0.3], dtype=float)), 6: (np.array([3, 4, 7]), np.array([-0.3, -b, 0.1], dtype=float)), 7: (np.array([3, 4, 5, 6, 8]), np.array([-d, -0.3, -b, -0.1, 0.1], dtype=float)), 8: (np.array([4, 5, 7]), np.array([-d, -0.3, -0.1], dtype=float))} neighbors_derivatives_dictionary = util.get_neighbors_derivatives_dictionary(heights, nx, ny) are_equal = compare_methods.compare_two_dictionaries_where_values_are_tuples_with_two_arrays( neighbors_derivatives_dictionary, result_neighbors_derivatives_dictionary) assert are_equal
def merge_sub_traps(watersheds, heights, nx, ny): changes = True iterations = 0 der_dict = util.get_neighbors_derivatives_dictionary(heights, nx, ny) while changes: print iterations changes = False boundary_pairs = get_boundary_pairs_in_watersheds(watersheds, nx, ny) min_of_max_in_each_watershed, max_heights_of_pairs = get_min_height_of_max_of_all_pairs(boundary_pairs, heights) spill_pairs = get_spill_pairs(max_heights_of_pairs, min_of_max_in_each_watershed) steepest_spill_pairs = get_steepest_spill_pair(boundary_pairs, spill_pairs, der_dict) merged_ws_indices = merge_watersheds_based_on_steepest_pairs(steepest_spill_pairs, watersheds, heights, nx, ny) new_watersheds = merge_watersheds_using_merged_indices(watersheds, merged_ws_indices) if 1 < len(new_watersheds) < len(watersheds): changes = True watersheds = new_watersheds iterations += 1 return watersheds, iterations