fire2a.adjacency

👋🌎 🌲🔥 This is the raster module docstring

  1#!python3
  2"""👋🌎 🌲🔥
  3This is the raster module docstring
  4"""
  5__author__ = "Rodrigo Mahaluf Recasens"
  6__revision__ = "$Format:%H$"
  7
  8from scipy.sparse import lil_matrix
  9
 10
 11def adjacent_cells(grid: lil_matrix, connectivity: int = 4) -> list:
 12    """
 13    Get adjacent cells for each cell in a grid.
 14
 15    Parameters:
 16        grid (lil_matrix): Sparse matrix representing a landscape.
 17        connectivity (int, optional): The type of connectivity (4 or 8). Defaults to 4.
 18
 19    Returns:
 20        list: List of lists containing neighbors for each cell.
 21    """  # fmt: skip
 22    if connectivity == 4:
 23        adj_cells = adjacent_cells4(grid)
 24    elif connectivity == 8:
 25        adj_cells = adjacent_cells8(grid)
 26    else:
 27        raise ValueError("Invalid connectivity value. Use 4 or 8.")
 28
 29    return adj_cells
 30
 31
 32def adjacent_cells4(grid: lil_matrix) -> list:
 33    """
 34    Get 4-connected adjacent cells for each cell in the forest grid.
 35
 36    Parameters:
 37        grid (lil_matrix): Sparse matrix representing a landscape.
 38
 39    Returns:
 40        list: List of lists containing 4-connected neighbors for each cell.
 41    """  # fmt: skip
 42    nrows, ncols = grid.shape
 43    AdjCells = []
 44
 45    for i in range(nrows):
 46        for j in range(ncols):
 47            neighbors = []
 48
 49            if i > 0:
 50                neighbors.append(grid[i - 1, j])  # Up
 51
 52            if i < nrows - 1:
 53                neighbors.append(grid[i + 1, j])  # Down
 54
 55            if j > 0:
 56                neighbors.append(grid[i, j - 1])  # Left
 57
 58            if j < ncols - 1:
 59                neighbors.append(grid[i, j + 1])  # Right
 60
 61            AdjCells.append(neighbors)
 62    return AdjCells
 63
 64
 65def adjacent_cells8(grid: lil_matrix) -> list:
 66    """
 67    Get 8-connected adjacent cells for each cell in the forest grid.
 68
 69    Parameters:
 70        grid (lil_matrix): Sparse matrix representing a landscape.
 71
 72    Returns:
 73        list: List of lists containing 8-connected neighbors for each cell.
 74    """  # fmt: skip
 75    nrows, ncols = grid.shape
 76    AdjCells = []
 77
 78    for i in range(nrows):
 79        for j in range(ncols):
 80            neighbors = []
 81
 82            for x in range(max(0, i - 1), min(nrows, i + 2)):
 83                for y in range(max(0, j - 1), min(ncols, j + 2)):
 84                    if x != i or y != j:
 85                        neighbors.append(grid[x, y])
 86
 87            AdjCells.append(neighbors)
 88    return AdjCells
 89
 90
 91if __name__ == "__main__":
 92
 93    # Create a sparse forest grid (5x5) with random values
 94    nrows = 5
 95    ncols = 5
 96    ncells = nrows * ncols
 97    id_pixel = list(range(1, ncells + 1))
 98
 99    # Create a sparse forest grid from the id_pixel list
100    grid = lil_matrix((nrows, ncols), dtype=int)
101    for idx, value in enumerate(id_pixel):
102        row = idx // ncols
103        col = idx % ncols
104        grid[row, col] = value
105
106    print("grid:")
107    for row in grid.toarray():
108        print(row)
109
110    # Call the adjacent_cells function to get 4-connected neighbors
111    adj_cells_4 = adjacent_cells(grid, connectivity=4)
112    print("\n4-Connected Neighbors:")
113    for row in adj_cells_4:
114        print(row)
115
116    # Call the adjacent_cells function to get 8-connected neighbors
117    adj_cells_8 = adjacent_cells(grid, connectivity=8)
118    print("\n8-Connected Neighbors:")
119    for row in adj_cells_8:
120        print(row)
def adjacent_cells(grid: scipy.sparse._lil.lil_matrix, connectivity: int = 4) -> list:
12def adjacent_cells(grid: lil_matrix, connectivity: int = 4) -> list:
13    """
14    Get adjacent cells for each cell in a grid.
15
16    Parameters:
17        grid (lil_matrix): Sparse matrix representing a landscape.
18        connectivity (int, optional): The type of connectivity (4 or 8). Defaults to 4.
19
20    Returns:
21        list: List of lists containing neighbors for each cell.
22    """  # fmt: skip
23    if connectivity == 4:
24        adj_cells = adjacent_cells4(grid)
25    elif connectivity == 8:
26        adj_cells = adjacent_cells8(grid)
27    else:
28        raise ValueError("Invalid connectivity value. Use 4 or 8.")
29
30    return adj_cells

Get adjacent cells for each cell in a grid.

Parameters: grid (lil_matrix): Sparse matrix representing a landscape. connectivity (int, optional): The type of connectivity (4 or 8). Defaults to 4.

Returns: list: List of lists containing neighbors for each cell.

def adjacent_cells4(grid: scipy.sparse._lil.lil_matrix) -> list:
33def adjacent_cells4(grid: lil_matrix) -> list:
34    """
35    Get 4-connected adjacent cells for each cell in the forest grid.
36
37    Parameters:
38        grid (lil_matrix): Sparse matrix representing a landscape.
39
40    Returns:
41        list: List of lists containing 4-connected neighbors for each cell.
42    """  # fmt: skip
43    nrows, ncols = grid.shape
44    AdjCells = []
45
46    for i in range(nrows):
47        for j in range(ncols):
48            neighbors = []
49
50            if i > 0:
51                neighbors.append(grid[i - 1, j])  # Up
52
53            if i < nrows - 1:
54                neighbors.append(grid[i + 1, j])  # Down
55
56            if j > 0:
57                neighbors.append(grid[i, j - 1])  # Left
58
59            if j < ncols - 1:
60                neighbors.append(grid[i, j + 1])  # Right
61
62            AdjCells.append(neighbors)
63    return AdjCells

Get 4-connected adjacent cells for each cell in the forest grid.

Parameters: grid (lil_matrix): Sparse matrix representing a landscape.

Returns: list: List of lists containing 4-connected neighbors for each cell.

def adjacent_cells8(grid: scipy.sparse._lil.lil_matrix) -> list:
66def adjacent_cells8(grid: lil_matrix) -> list:
67    """
68    Get 8-connected adjacent cells for each cell in the forest grid.
69
70    Parameters:
71        grid (lil_matrix): Sparse matrix representing a landscape.
72
73    Returns:
74        list: List of lists containing 8-connected neighbors for each cell.
75    """  # fmt: skip
76    nrows, ncols = grid.shape
77    AdjCells = []
78
79    for i in range(nrows):
80        for j in range(ncols):
81            neighbors = []
82
83            for x in range(max(0, i - 1), min(nrows, i + 2)):
84                for y in range(max(0, j - 1), min(ncols, j + 2)):
85                    if x != i or y != j:
86                        neighbors.append(grid[x, y])
87
88            AdjCells.append(neighbors)
89    return AdjCells

Get 8-connected adjacent cells for each cell in the forest grid.

Parameters: grid (lil_matrix): Sparse matrix representing a landscape.

Returns: list: List of lists containing 8-connected neighbors for each cell.