def sampleNo(xvar, yvar, N, avoididx):
"""
Sample from pixels in mesh, excluding yes pixels and already sampled no
pixels.
Args:
xvar: Numpy array of centers of all columns in mesh.
yvar: Numpy array of centers of all rows in mesh.
N: Number of no pixels to sample.
avoididx: 1D array of indices from mesh that should NOT be sampled
from. Initially this will be the array of indices where the yes
pixels are.
Returns:
Randomly chosen list of tuples of (x,y) coordinate points that are
outside polygons.
"""
# flattened array of all indices in mesh
allidx = np.arange(0, len(xvar) * len(yvar))
noidx = np.setxor1d(allidx, avoididx) # allidx - avoididx
nosampleidx = np.random.choice(noidx, size=N, replace=False)
newavoididx = np.sort(np.hstack((avoididx, nosampleidx)))
rowidx, colidx = np.unravel_index(nosampleidx, (len(yvar), len(xvar)))
samples = []
for row, col in zip(rowidx, colidx):
xp = xvar[col]
yp = yvar[row]
samples.append((xp, yp))
return (samples, newavoididx)
评论列表
文章目录