コード例 #1
0
ファイル: trap_analysis.py プロジェクト: avoldsund/watershed
def get_boundary_pairs_in_watersheds(watersheds, num_of_cols, num_of_rows):
    """
    Returns a list of arrays, where each array holds all boundary pairs for a watershed.
    :param watersheds: All watersheds.
    :param num_of_cols: Number of nodes in the x-direction.
    :param num_of_rows: Number of nodes in the y-direction.
    :return boundary_pairs: All boundary pairs in the landscape.
    """

    boundary_pairs = []  # List of arrays where the arrays have tuples

    for watershed in watersheds:
        watershed = np.sort(watershed)
        neighbors_for_watershed = util.get_neighbors_for_indices_array(watershed, num_of_cols, num_of_rows)
        neighbors_for_watershed_1d = np.concatenate(neighbors_for_watershed)
        not_in_watershed_arr = np.in1d(neighbors_for_watershed_1d, watershed, invert=True)
        not_in_watershed = np.split(not_in_watershed_arr, len(watershed))

        ext_nbrs = [(watershed[i], neighbors_for_watershed[i][not_in_watershed[i]]
                    [neighbors_for_watershed[i][not_in_watershed[i]] != -1])
                    for i in range(len(neighbors_for_watershed))
                    if len(neighbors_for_watershed[i][not_in_watershed[i]]
                    [neighbors_for_watershed[i][not_in_watershed[i]] != -1]) > 0]
        ext_bordering_nodes = [el[1] for el in ext_nbrs]
        repeat_nr = np.array([len(el[1]) for el in ext_nbrs], dtype=int)
        bordering_nodes = np.array([el[0] for el in ext_nbrs])
        nr_of_pairs = np.sum(repeat_nr)
        pair_arr = np.empty((2, nr_of_pairs), dtype=int)
        pair_arr[0, :] = np.repeat(bordering_nodes, repeat_nr)
        pair_arr[1, :] = np.concatenate(ext_bordering_nodes)
        boundary_pairs.append(pair_arr)

    return boundary_pairs
コード例 #2
0
ファイル: trap_analysis.py プロジェクト: avoldsund/watershed
def get_minimums_in_watersheds(minimum_indices, num_of_cols, num_of_rows):
    """
    Returns a list of sets where each set is a collection of all minimums in each watershed.
    :param minimum_indices: The indices of the minimums in the landscape.
    :param num_of_cols: Number of columns in the 2d-grid.
    :param num_of_rows: Number of rows in the 2d-grid.
    :return minimums_in_watershed: All minimums in each watershed
    """

    neighbors = util.get_neighbors_for_indices_array(minimum_indices, num_of_cols, num_of_rows)
    neighbor_is_a_min_bool = np.in1d(neighbors, minimum_indices)
    neighbor_is_a_min_bool = neighbor_is_a_min_bool.reshape(len(minimum_indices), 8)
    min_neighbors = np.multiply(neighbors, neighbor_is_a_min_bool).tolist()

    for i in range(len(minimum_indices)):
        min_neighbors[i] = [value for value in min_neighbors[i] if value != 0]
        min_neighbors[i].append(minimum_indices[i])

    min_neighbors = filter(None, min_neighbors)  # Remove the lists without neighbors being minimums

    graph = networkx.Graph()
    for l in min_neighbors:
        graph.add_nodes_from(l)
        edges = zip(l[:-1], l[1:])
        graph.add_edges_from(edges)

    minimums_in_watershed = networkx.connected_components(graph)
    # networkx.draw(graph)
    # plt.show()

    return minimums_in_watershed
コード例 #3
0
ファイル: test_util.py プロジェクト: avoldsund/watershed
def test_get_neighbors_for_indices_array():

    num_of_cols = 6
    num_of_rows = 5
    indices = np.array([5, 7, 13, 22, 23, 28, 29])
    neighbors = np.array([[4, 10, 11, -1, -1, -1, -1, -1],
                         [0, 1, 2, 6, 8, 12, 13, 14],
                         [6, 7, 8, 12, 14, 18, 19, 20],
                         [15, 16, 17, 21, 23, 27, 28, 29],
                         [16, 17, 22, 28, 29, -1, -1, -1],
                         [21, 22, 23, 27, 29, -1, -1, -1],
                         [22, 23, 28, -1, -1, -1, -1, -1]])

    result_neighbors = util.get_neighbors_for_indices_array(indices, num_of_cols, num_of_rows)

    assert np.array_equal(result_neighbors, neighbors)
コード例 #4
0
ファイル: trap_analysis.py プロジェクト: avoldsund/watershed
def get_boundary_nodes_in_watersheds(watersheds, num_of_cols, num_of_rows):
    """
    Return the boundary nodes of the watershed, i.e. nodes bordering to other watersheds or to the landscape border.
    :param watersheds: Nodes of each watershed.
    :param num_of_cols: Number of nodes in the x-direction.
    :param num_of_rows: Number of nodes in the y-direction.
    :return boundary_nodes: The boundary nodes for each watershed. List of arrays.
    """

    boundary_nodes = []

    for watershed in watersheds:
        watershed = np.sort(watershed)
        neighbors_for_watershed = util.get_neighbors_for_indices_array(watershed, num_of_cols, num_of_rows)
        neighbors_for_watershed_1d = np.concatenate(neighbors_for_watershed)
        not_in_watershed_arr = np.in1d(neighbors_for_watershed_1d, watershed, invert=True)
        not_in_watershed = np.split(not_in_watershed_arr, len(watershed))

        bnd_nodes = np.asarray([watershed[i] for i in range(len(neighbors_for_watershed))
                                if len(neighbors_for_watershed[i][not_in_watershed[i]]) != 0])
        boundary_nodes.append(bnd_nodes)

    return boundary_nodes