Exemple #1
0
 def __init__(self, N, BoxSize, max_speed=500., mu_logM=13.5, sigma_logM=0.5, seed=None):        
     
     self.N          = N
     self.BoxSize    = BoxSize
     self.max_speed  = max_speed
     self.mu_logM    = mu_logM
     self.sigma_logM = sigma_logM
     self.seed       = seed
     
     # create the local random seed from the global seed and comm size
     self.local_seed = utils.local_random_seed(self.seed, self.comm)
Exemple #2
0
 def __init__(self, datasource, collision_radius=62/60./60., seed=None):
     
     # set the input parameters
     self.datasource       = datasource
     self.collision_radius = collision_radius
     self.seed             = seed
     
     # store collision radius in radians
     self._collision_radius_rad = self.collision_radius * numpy.pi/180.
     if self.comm.rank == 0: 
         self.logger.info("collision radius in degrees = %.4f" %collision_radius)
         
     # create the local random seed from the global seed and comm size
     self.local_seed = utils.local_random_seed(self.seed, self.comm)
     self.logger.info("local_seed = %d" %self.local_seed)
Exemple #3
0
 def __init__(self, nbar, redshift, BoxSize, Nmesh, bias=2., rsd=None, seed=None):        
     
     self.nbar     = nbar
     self.redshift = redshift
     self.BoxSize  = BoxSize
     self.Nmesh    = Nmesh
     self.bias     = bias
     self.rsd      = rsd
     self.seed     = seed
     
     # create the local random seed from the global seed and comm size
     self.local_seed = utils.local_random_seed(self.seed, self.comm)
     
     # crash if no cosmology provided
     if self.cosmo is None:
         raise ValueError("a cosmology must be specified via the `cosmo` keyword to use %s" %self.plugin_name)
Exemple #4
0
    def __init__(self,
                 N,
                 BoxSize,
                 max_speed=500.,
                 mu_logM=13.5,
                 sigma_logM=0.5,
                 seed=None):

        self.N = N
        self.BoxSize = BoxSize
        self.max_speed = max_speed
        self.mu_logM = mu_logM
        self.sigma_logM = sigma_logM
        self.seed = seed

        # create the local random seed from the global seed and comm size
        self.local_seed = utils.local_random_seed(self.seed, self.comm)
Exemple #5
0
    def run(self):
        """
        Run the Subsample algorithm
        """
        import mpsort
        from astropy.utils.misc import NumpyRNGContext

        if self.smoothing is None:
            self.smoothing = self.datasource.BoxSize[0] / self.Nmesh[0]
        elif (self.datasource.BoxSize / self.Nmesh > self.smoothing).any():
            raise ValueError("smoothing is too small")

        def Smoothing(pm, complex):
            k = pm.k
            k2 = 0
            for ki in k:
                ki2 = ki**2
                complex[:] *= numpy.exp(-0.5 * ki2 * self.smoothing**2)

        def NormalizeDC(pm, complex):
            """ removes the DC amplitude. This effectively
                divides by the mean
            """
            w = pm.w
            comm = pm.comm
            ind = []
            value = 0.0
            found = True
            for wi in w:
                if (wi != 0).all():
                    found = False
                    break
                ind.append((wi == 0).nonzero()[0][0])
            if found:
                ind = tuple(ind)
                value = numpy.abs(complex[ind])
            value = comm.allreduce(value)
            complex[:] /= value

        # open the datasource and keep the cache
        with self.datasource.keep_cache():

            painter = Painter.create("DefaultPainter", paintbrush='cic')
            real, stats = painter.paint(self.pm, self.datasource)
            complex = real.r2c()

            for t in [Smoothing, NormalizeDC]:
                t(self.pm, complex)

            complex.c2r(real)

            columns = ['Position', 'ID', 'Velocity']
            local_seed = utils.local_random_seed(self.seed, self.comm)

            dtype = numpy.dtype([
                ('Position', ('f4', 3)),
                ('Velocity', ('f4', 3)),
                ('ID', 'u8'),
                ('Density', 'f4'),
            ])

            subsample = [numpy.empty(0, dtype=dtype)]

            with self.datasource.open() as stream:
                for Position, ID, Velocity in stream.read(columns):

                    with NumpyRNGContext(local_seed):
                        u = numpy.random.uniform(size=len(ID))
                    keep = u < self.ratio
                    Nkeep = keep.sum()
                    if Nkeep == 0: continue
                    data = numpy.empty(Nkeep, dtype=dtype)
                    data['Position'][:] = Position[keep]
                    data['Velocity'][:] = Velocity[keep]
                    data['ID'][:] = ID[keep]

                    layout = self.pm.decompose(data['Position'])
                    pos1 = layout.exchange(data['Position'])
                    density1 = real.readout(pos1)
                    density = layout.gather(density1)

                    # normalize the position after reading out density!
                    data['Position'][:] /= self.datasource.BoxSize
                    data['Velocity'][:] /= self.datasource.BoxSize
                    data['Density'][:] = density
                    subsample.append(data)

        subsample = numpy.concatenate(subsample)
        mpsort.sort(subsample, orderby='ID', comm=self.comm)

        return subsample
Exemple #6
0
    def run(self):
        """
        Run the Subsample algorithm
        """
        import mpsort
        from astropy.utils.misc import NumpyRNGContext

        if self.smoothing is None:
            self.smoothing = self.datasource.BoxSize[0] / self.Nmesh[0]
        elif (self.datasource.BoxSize / self.Nmesh > self.smoothing).any():
            raise ValueError("smoothing is too small")

        def Smoothing(pm, complex):
            k = pm.k
            k2 = 0
            for ki in k:
                ki2 = ki ** 2
                complex[:] *= numpy.exp(-0.5 * ki2 * self.smoothing ** 2)

        def NormalizeDC(pm, complex):
            """ removes the DC amplitude. This effectively
                divides by the mean
            """
            w = pm.w
            comm = pm.comm
            ind = []
            value = 0.0
            found = True
            for wi in w:
                if (wi != 0).all():
                    found = False
                    break
                ind.append((wi == 0).nonzero()[0][0])
            if found:
                ind = tuple(ind)
                value = numpy.abs(complex[ind])
            value = comm.allreduce(value)
            complex[:] /= value

        # open the datasource and keep the cache
        with self.datasource.keep_cache():

            painter = Painter.create("DefaultPainter", paintbrush="cic")
            real, stats = painter.paint(self.pm, self.datasource)
            complex = real.r2c()

            for t in [Smoothing, NormalizeDC]:
                t(self.pm, complex)

            complex.c2r(real)

            columns = ["Position", "ID", "Velocity"]
            local_seed = utils.local_random_seed(self.seed, self.comm)

            dtype = numpy.dtype([("Position", ("f4", 3)), ("Velocity", ("f4", 3)), ("ID", "u8"), ("Density", "f4")])

            subsample = [numpy.empty(0, dtype=dtype)]

            with self.datasource.open() as stream:
                for Position, ID, Velocity in stream.read(columns):

                    with NumpyRNGContext(local_seed):
                        u = numpy.random.uniform(size=len(ID))
                    keep = u < self.ratio
                    Nkeep = keep.sum()
                    if Nkeep == 0:
                        continue
                    data = numpy.empty(Nkeep, dtype=dtype)
                    data["Position"][:] = Position[keep]
                    data["Velocity"][:] = Velocity[keep]
                    data["ID"][:] = ID[keep]

                    layout = self.pm.decompose(data["Position"])
                    pos1 = layout.exchange(data["Position"])
                    density1 = real.readout(pos1)
                    density = layout.gather(density1)

                    # normalize the position after reading out density!
                    data["Position"][:] /= self.datasource.BoxSize
                    data["Velocity"][:] /= self.datasource.BoxSize
                    data["Density"][:] = density
                    subsample.append(data)

        subsample = numpy.concatenate(subsample)
        mpsort.sort(subsample, orderby="ID", comm=self.comm)

        return subsample