sample.py 文件源码

python
阅读 38 收藏 0 点赞 0 评论 0

项目:groundfailure 作者: usgs 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号