def weighted_region_space_sampling(discretizor, n, weights=None): """Select points weighting regions. Parameters ---------- discretizor: pySpatialTools.Discretization.BaseDiscretizor object the discretization information. weights: numpy.ndarray the weights of each region. n: int the number of points we want to sample. Returns ------- points_s: numpy.ndarray, shape (n, 2) the coordinates of the sampled points. """ ## 0. Compute variables needed regions = discretizor.regions_id # regions.shape[0] == weights.shape[0] ## 1. Compute regions selected regions = weighted_sampling_with_repetition(regions, n, weights) ## 2. Compute uniform selection from the selected points t_regions = np.unique(regions) points_s = -1 * np.ones((n, 2)) # Region by region for r in t_regions: # Compute limits of the region limits_r = discretizor.get_limits(r) # Compute how many points of this region you want logi = regions == r inds = np.where(logi)[0] n_r = logi.sum() j = 0 while j != n_r: # Generate points randomly in the limits point_j = uniform_points_sampling(limits_r, 1) # Reject if there is not in the region, accept otherwise boolean = discretizor.belong_region(point_j, r) if boolean: points_s[inds[j]] = point_j j += 1 return points_s
def weighted_region_points_sampling(discretizor, points, n, weights=None): """Select points weighting regions. Parameters ---------- discretizor: pySpatialTools.Neighbourhood.discretizor object the discretization information. weights: numpy.ndarray the weights of each region. n: int the number of points we want to sample. Returns ------- indices: numpy.ndarray, shape(n) the indices of the samples. """ ## 0. Compute variables needed points_c = discretizor.discretize(points) p_cats = np.unique(points_c) if weights is not None: if len(p_cats) < len(weights): weights = weights[p_cats] elif len(p_cats) < len(weights): pass ## 1. Compute regions selected regions = weighted_sampling_with_repetition(p_cats, n, weights) ## 2. Compute uniform selection from the selected points indices = -1*np.ones(n) for i in xrange(n): logi = points_c == regions[i] n_i = logi.sum() j = np.random.randint(0, n_i) indices[i] = np.where(logi)[0][j] indices = indices.astype(int) return indices