I use a moving window to buffer raster data (numpy array). It is very slow and I am wondering if it is possible to improve the code to make it faster: My actual arrays have the shape (1349, 1368) and consist of zeros and ones.
import numpy as np
clouds = np.array([[[0, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 1],
[0, 0, 0, 0, 1],
]])
cloudShadow = np.array([[[1, 0, 0, 1, 1],
[0, 1, 1, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1],
]])
row_up_cloudShadow = 1
row_low_cloudShadow = 0
col_left_cloudShadow = 1
col_right_cloudShadow = 0
row = []
for i in range(len(np.where(clouds == 1)[0])):
row_xy = list(range((np.where(clouds == 1)[0][i] - row_up_cloudShadow), (np.where(clouds == 1)[0][i] row_low_cloudShadow) 1))
row.append(row_xy)
col = []
for i in range(len(np.where(clouds == 1)[1])):
col_xy = list(range((np.where(clouds == 1)[1][i] - col_left_cloudShadow), (np.where(clouds == 1)[1][i] col_right_cloudShadow) 1))
col.append(col_xy)
buffer = []
for i in range(0, np.count_nonzero(clouds == 1)):
for j in range(len(row[0])):
z = row[i][j]
for u in range(len(col[0])):
s = col[i][u]
buffer.append(np.array([z,s]))
buffer = np.asarray(buffer)
buffer = np.where(buffer < 0, 0, buffer)
data_buff_cloudShadow = np.zeros(clouds.shape)
for i in range(len(buffer)):
data_buff_cloudShadow[buffer[i][0]][buffer[i][1]] = 1
cloudShadow_buff = np.where(data_buff_cloudShadow == 1, cloudShadow, 0)
CodePudding user response:
Here are some specific guidelines to make your code faster:
- Avoid repeating the same calculation: In your first two loops you do the same calculation (
np.where(clouds == 1)
) many times, so that you could refactor to:
row_idxs, col_idxs = np.where(clouds == 1)
for ridx in row_idxs:
row_xy = list(
range(ridx - row_up_cloudShadow, ridx row_low_cloudShadow 1)
)
row.append(row_xy)
for cidx in col_idxs:
col_xy = list(
range(cidx - col_left_cloudShadow, cidx col_right_cloudShadow 1)
)
col.append(col_xy)
- Remove loops wherever possible: Anything you can do with only Numpy functionality will be faster. Here, the last for-loop can be avoided using Numpy indexing:
data_buff_cloudShadow = np.zeros(clouds.shape)
data_buff_cloudShadow[buffer[:, 0], buffer[:, 1]] = 1
- Avoid conversions between Pythons
list
datatype and Numpy'sndarray
whenever possible. This specifically applies to intermediate results. Try to re-imagine your problem in terms of full arrays, as quite a number of problems can be represented that way. Here, you might be able to usenp.meshgrid
to represent coordinates easier.
For moving windows, you might benefit from looking into numpy.lib.stride_tricks.sliding_window_view for a moving window directly built into Numpy.
For further very helpful information, see https://numpy.org/learn/.