def generateAngularRandoms(hpix, nrand, nside, nest=True): fmask = np.zeros(12 * nside**2) fmask[hpix] = 1 if nest: pmap = hu.DensityMap('nest', fmask) else: pmap = hu.DensityMap('ring', fmask) rand_ra, rand_dec = pmap.genrand(nrand, system='eq') return rand_ra, rand_dec
def generateAngularRandoms(aza, pla, z=None, urand_factor=20, rand_factor=10, nside=8, nest=True): """ Generate a set of randoms from a catalog by pixelating the input catalog and uniformly distributing random points within the pixels occupied by galaxies. Also option to assign redshifts with the same distribution as the input catalog to allow for generation of randoms once for all z bins. """ dt = aza.dtype if z is not None: rdtype = np.dtype([('azim_ang', dt), ('polar_ang', dt), ('redshift', dt)]) else: rdtype = np.dtype([('azim_ang', dt), ('polar_ang', dt)]) rsize = len(aza) * urand_factor cpix = hp.ang2pix(nside, (90 - pla) * np.pi / 180., aza * np.pi / 180., nest=nest) cpix = np.unique(cpix) pmap = np.zeros(12 * nside**2) pmap[cpix] = 1 if nest: pmap = hu.DensityMap('nest', pmap) else: pmap = hu.DensityMap('ring', pmap) grand = np.zeros(len(aza) * rand_factor, dtype=rdtype) grand['azim_ang'], grand['polar_ang'] = pmap.genrand(len(aza) * rand_factor, system='eq') if z is not None: grand['redshift'] = np.random.choice(z, size=len(aza) * rand_factor) zidx = grand['redshift'].argsort() grand = grand[zidx] return grand
def generate_jk_centers_from_mask(outfile, regionfile, nrand=1e5): with h5py.File(outfile, 'r') as f: mask = f['index/mask/hpix'][:] nside = 4096 pmap = np.zeros(12 * nside**2) pmap[mask] = 1 pmap = hu.DensityMap('nest', pmap) rand_ra, rand_dec = pmap.genrand(int(nrand), system='eq') centers = KMeans(n_clusters=1000, random_state=0).fit(np.vstack([rand_ra, rand_dec]).T) centers = centers.cluster_centers_ kdt = spatial.cKDTree(centers) dist, idx = kdt.query(centers, 2) centers_dist = np.zeros((1000, 3)) centers_dist[:, :2] = centers centers_dist[:, 2] = dist[:, 1] fitsio.write(regionfile, centers_dist)
def generate_random_points(mask, max_ndraw, ra_min=0, ra_max=360, dec_min=-90, dec_max=90, factor=2): """ Generate random points within the footprint This can be used to generate points distributed uniformly over the entire footprint for generating a CDF from which to draw mock galaxy positions. It does not generate the CDF, it merely draws positions from a uniform density field with the mask applied. You should make sure that ``factor`` is large enough that the number of points generated here, ``int(max_ndraw * factor)``, is enough to sample the CDF well when ``max_ndraw`` is the maximum final number of mock galaxies to be drawn in any redshift bin. It needs to be larger than 1, but there is no guarantee that the default (2) is enough. :param mask: The pixel coverage mask :type mask: :class:`lsssys.Mask` or :class:`lsssys.HealMask` :param max_ndraw: The maximum number of mock galaxies that will be drawn in any redshift bin. This way, the set of points generated here can be used to generate the CDF for all of the redshift bins :type max_ndraw: ``int`` :param ra_min: The minimum right ascension (in degrees) in which the data will be, to prevent drawing points far from the region of interest. Default 0.0 :type ra_min: ``float``, optional :param ra_max: The maximum right ascension (in degrees) in which the data will be, to prevent drawing points far from the region of interest. Default 360.0 :type ra_max: ``float``, optional :param dec_min: The minimum declination (in degrees) in which the data will be, to prevent drawing points far from the region of interest. Default -90.0 :type dec_min: ``float``, optional :param dec_max: The maximum declination (in degrees) in which the data will be, to prevent drawing points far from the region of interest. Default 90.0 :type dec_max: ``float``, optional :param factor: The multiplicative factor on ``max_ndraw`` that sets how many points should be generated here. This should probably be at least 2, but that is not checked. Default 2 :type factor: ``int`` or ``float``, optional :return: An array of right ascension and an array of declination that can be used with weights to draw mock galaxy positions from the non-uniform density field with or without systematics :rtype: 2-``tuple`` of (``int(factor * max_ndraw)``,) :class:`numpy.ndarray` of ``float`` """ dmap = hu.DensityMap("RING", mask.fracdet) ra, dec = dmap.genrand(10 * int(factor * max_ndraw), ra_range=[ra_min, ra_max], dec_range=[dec_min, dec_max]) pix = dmap.hpix.eq2pix(ra, dec) ra = ra[~mask.mask[pix]] dec = dec[~mask.mask[pix]] while ra.size < int(factor * max_ndraw): new_ra, new_dec = dmap.genrand(10 * int(factor * max_ndraw), ra_range=[ra_min, ra_max], dec_range=[dec_min, dec_max]) new_pix = dmap.hpix.eq2pix(new_ra, new_dec) ra = np.append(ra, new_ra[~mask.mask[new_pix]]) dec = np.append(dec, new_dec[~mask.mask[new_pix]]) return ra[:int(factor * max_ndraw)], dec[:int(factor * max_ndraw)]
def generateCartesianRandoms(x, y, z, rand_nside, rand_nest, rand_ra_lims, rand_dec_lims, rand_fact=10): rsize = len(x) * rand_fact if rand_nside: rdtype = np.dtype([('px', np.float32), ('py', np.float32), ('pz', np.float32), ('redshift', np.float32)]) else: rdtype = np.dtype([('px', np.float32), ('py', np.float32), ('pz', np.float32)]) gr = np.zeros(rsize, dtype=rdtype) if rand_nside is None: gr['px'] = np.random.uniform(low=np.min(x), high=np.max(x), size=rsize) gr['py'] = np.random.uniform(low=np.min(y), high=np.max(y), size=rsize) gr['pz'] = np.random.uniform(low=np.min(z), high=np.max(z), size=rsize) else: print('generating randoms using nside {}'.format(rand_nside)) print('Nest ordering : {}'.format(rand_nest)) r = np.sqrt(x**2 + y**2 + z**2) rr = np.random.choice(r, size=rsize) pix = hp.vec2pix(rand_nside, x, y, z, nest=rand_nest) pix = np.unique(pix) hmap = np.zeros(12 * rand_nside**2) hmap[pix] = 1 if rand_nest: hmap = hu.DensityMap('nest', hmap) else: hmap = hu.DensityMap('ring', hmap) theta, phi = hmap.genrand(rsize, system='ang') if (rand_ra_lims is not None): ra = phi * 180. / np.pi idx = (rand_ra_lims[0] < ra) & (ra < rand_ra_lims[1]) theta = theta[idx] phi = phi[idx] rr = rr[idx] gr = gr[idx] del ra, idx if (rand_dec_lims is not None): dec = 90. - theta * 180. / np.pi idx = (rand_dec_lims[0] < dec) & (dec < rand_dec_lims[1]) print(gr.shape) theta = theta[idx] phi = phi[idx] rr = rr[idx] gr = gr[idx] del dec, idx print(gr.shape) pos = hp.ang2vec(theta, phi) pos *= rr.reshape(-1, 1) gr['px'] = pos[:, 0] gr['py'] = pos[:, 1] gr['pz'] = pos[:, 2] gr['redshift'] = rr return gr