def __init__(self, Plin, BoxSize, Nmesh, seed=None, unitary_amplitude=False, inverted_phase=False, remove_variance=None, comm=None): self.Plin = Plin # cosmology and communicator self.comm = comm self.attrs.update(attrs_to_dict(Plin, 'plin.')) # set the seed randomly if it is None if seed is None: if self.comm.rank == 0: seed = numpy.random.randint(0, 4294967295) seed = self.comm.bcast(seed) self.attrs['seed'] = seed if remove_variance is not None: unitary_amplitude = remove_variance self.attrs['unitary_amplitude'] = unitary_amplitude self.attrs['inverted_phase'] = inverted_phase MeshSource.__init__(self, BoxSize=BoxSize, Nmesh=Nmesh, dtype='f4', comm=comm)
def __init__(self, names, *species, **kwargs): if len(set(names)) != len(names): raise ValueError("each species must have a unique name") if not all(cat.comm is species[0].comm for cat in species): raise ValueError("communicator mismatch in MultipleSpeciesCatalog") if len(names) != len(species): raise ValueError("a name must be provided for each species catalog provided") CatalogSourceBase.__init__(self, species[0].comm) self.attrs['species'] = names # update the dictionary with data/randoms attrs for cat, name in zip(species, names): self.attrs.update(attrs_to_dict(cat, name + '.')) # update the rest of meta-data self.attrs.update(kwargs) # no size! self.size = NotImplemented self.csize = NotImplemented # store copies of the original input catalogs as (name:catalog) dict self._sources = {name:cat.copy() for name,cat in zip(names, species)}
def __init__(self, names, *species, **kwargs): if len(set(names)) != len(names): raise ValueError("each species must have a unique name") if not all(cat.comm is species[0].comm for cat in species): raise ValueError("communicator mismatch in MultipleSpeciesCatalog") if len(names) != len(species): raise ValueError( "a name must be provided for each species catalog provided") CatalogSourceBase.__init__(self, species[0].comm) self.attrs['species'] = names # update the dictionary with data/randoms attrs for cat, name in zip(species, names): self.attrs.update(attrs_to_dict(cat, name + '.')) # update the rest of meta-data self.attrs.update(kwargs) # no size! self.size = NotImplemented self.csize = NotImplemented # store copies of the original input catalogs as (name:catalog) dict self._sources = {name: cat.copy() for name, cat in zip(names, species)}
def __init__(self, Plin, BoxSize, Nmesh, seed=None, unitary_amplitude=False, inverted_phase=False, remove_variance=None, comm=None): self.Plin = Plin # cosmology and communicator self.comm = comm self.attrs.update(attrs_to_dict(Plin, 'plin.')) # set the seed randomly if it is None if seed is None: if self.comm.rank == 0: seed = numpy.random.randint(0, 4294967295) seed = self.comm.bcast(seed) self.attrs['seed'] = seed if remove_variance is not None: unitary_amplitude = remove_variance self.attrs['unitary_amplitude'] = unitary_amplitude self.attrs['inverted_phase'] = inverted_phase MeshSource.__init__(self, BoxSize=BoxSize, Nmesh=Nmesh, dtype='f4', comm=comm)
def to_real_field(self): r""" Paint the FKP density field, returning a ``RealField``. Given the ``data`` and ``randoms`` catalogs, this paints: .. math:: F(x) = w_\mathrm{fkp}(x) * [w_\mathrm{comp}(x)*n_\mathrm{data}(x) - \alpha * w_\mathrm{comp}(x)*n_\mathrm{randoms}(x)] This computes the following meta-data attributes in the process of painting, returned in the :attr:`attrs` attributes of the returned RealField object: - randoms.W, data.W : the weighted sum of randoms and data objects; see :func:`weighted_total` - alpha : float the ratio of ``data.W`` to ``randoms.W`` - randoms.norm, data.norm : float the power spectrum normalization; see :func:`normalization` - randoms.shotnoise, data.shotnoise: float the shot noise for each sample; see :func:`shotnoise` - shotnoise : float the total shot noise, equal to the sum of ``randoms.shotnoise`` and ``data.shotnoise`` - randoms.num_per_cell, data.num_per_cell : float the mean number of weighted objects per cell for each sample - num_per_cell : float the mean number of weighted objects per cell For further details on the meta-data, see :ref:`the documentation <fkp-meta-data>`. Returns ------- :class:`~pmesh.pm.RealField` : the field object holding the FKP density field in real space """ attrs = {} # determine alpha, the weighted number ratio for name in self.source.species: attrs[name + '.W'] = self.weighted_total(name) attrs['alpha'] = attrs['data.W'] / attrs['randoms.W'] # paint the data real = self['data'].to_real_field(normalize=False) real.attrs.update(attrs_to_dict(real, 'data.')) if self.comm.rank == 0: self.logger.info("data painted.") if self.source['randoms'].csize > 0: # paint the randoms real2 = self['randoms'].to_real_field(normalize=False) # normalize the randoms by alpha real2[:] *= -1. * attrs['alpha'] if self.comm.rank == 0: self.logger.info("randoms painted.") real[:] += real2[:] real.attrs.update(attrs_to_dict(real2, 'randoms.')) # divide by volume per cell to go from number to number density vol_per_cell = (self.pm.BoxSize / self.pm.Nmesh).prod() real[:] /= vol_per_cell if self.comm.rank == 0: self.logger.info("volume per cell is %g" % vol_per_cell) # remove shot noise estimates (they are inaccurate in this case) real.attrs.update(attrs) real.attrs.pop('data.shotnoise', None) real.attrs.pop('randoms.shotnoise', None) return real
def to_real_field(self, normalize=True): r""" Paint the density field holding the sum of all particle species, returning a :class:`~pmesh.pm.RealField` object. Meta-data computed for each particle is stored in the :attr:`attrs` attribute of the returned RealField, with keys that are prefixed by the species name. In particular, the total shot noise for the mesh is defined as: .. math:: P_\mathrm{shot} = \sum_i (W_i/W_\mathrm{tot})^2 P_{\mathrm{shot},i}, where the sum is over all species in the catalog, ``W_i`` is the sum of the ``Weight`` column for the :math:`i^\mathrm{th}` species, and :math:`W_\mathrm{tot}` is the sum of :math:`W_i` across all species. Parameters ---------- normalize : bool, optional if ``True``, normalize the density field as :math:`1+\delta`, dividing by the total mean number of objects per cell, as given by the ``num_per_cell`` meta-data value in :attr:`attrs` Returns ------- RealField : the RealField holding the painted density field, with a :attr:`attrs` dictionary attribute holding the meta-data """ # track the sum of the mean number of objects per cell across species attrs = {'num_per_cell': 0., 'N': 0} # initialize an empty real field real = self.pm.create(type='real', value=0) # loop over each species for name in self.source.species: if self.pm.comm.rank == 0: self.logger.info("painting the '%s' species" % name) # get a CatalogMesh for this species species_mesh = self[name] # paint (in-place) the un-normalized density field for this species species_mesh.to_real_field(out=real, normalize=False) # add to the mean number of objects per cell and total number attrs['num_per_cell'] += real.attrs['num_per_cell'] attrs['N'] += real.attrs['N'] # store the meta-data for this species, with a prefix attrs.update(attrs_to_dict(real, name + '.')) # # normalize the field by nbar -> this is now 1+delta if normalize: real[:] /= attrs['num_per_cell'] # update the meta-data real.attrs.clear() real.attrs.update(attrs) # compute total shot noise by summing of shot noise each species real.attrs['shotnoise'] = 0 total_weight = sum(real.attrs['%s.W' % name] for name in self.source.species) for name in self.source.species: this_Pshot = real.attrs['%s.shotnoise' % name] this_weight = real.attrs['%s.W' % name] real.attrs['shotnoise'] += (this_weight / total_weight)**2 * this_Pshot return real
def to_real_field(self): r""" Paint the FKP density field, returning a ``RealField``. Given the ``data`` and ``randoms`` catalogs, this paints: .. math:: F(x) = w_\mathrm{fkp}(x) * [w_\mathrm{comp}(x)*n_\mathrm{data}(x) - \alpha * w_\mathrm{comp}(x)*n_\mathrm{randoms}(x)] This computes the following meta-data attributes in the process of painting, returned in the :attr:`attrs` attributes of the returned RealField object: - randoms.W, data.W : the weighted sum of randoms and data objects; see :func:`weighted_total` - alpha : float the ratio of ``data.W`` to ``randoms.W`` - randoms.norm, data.norm : float the power spectrum normalization; see :func:`normalization` - randoms.shotnoise, data.shotnoise: float the shot noise for each sample; see :func:`shotnoise` - shotnoise : float the total shot noise, equal to the sum of ``randoms.shotnoise`` and ``data.shotnoise`` - randoms.num_per_cell, data.num_per_cell : float the mean number of weighted objects per cell for each sample - num_per_cell : float the mean number of weighted objects per cell For further details on the meta-data, see :ref:`the documentation <fkp-meta-data>`. Returns ------- :class:`~pmesh.pm.RealField` : the field object holding the FKP density field in real space """ attrs = {} # determine alpha, the weighted number ratio for name in self.source.species: attrs[name+'.W'] = self.weighted_total(name) attrs['alpha'] = attrs['data.W'] / attrs['randoms.W'] # paint the data real = self['data'].to_real_field(normalize=False) real.attrs.update(attrs_to_dict(real, 'data.')) if self.comm.rank == 0: self.logger.info("data painted.") if self.source['randoms'].csize > 0: # paint the randoms real2 = self['randoms'].to_real_field(normalize=False) # normalize the randoms by alpha real2[:] *= -1. * attrs['alpha'] if self.comm.rank == 0: self.logger.info("randoms painted.") real[:] += real2[:] real.attrs.update(attrs_to_dict(real2, 'randoms.')) # divide by volume per cell to go from number to number density vol_per_cell = (self.pm.BoxSize/self.pm.Nmesh).prod() real[:] /= vol_per_cell if self.comm.rank == 0: self.logger.info("volume per cell is %g" % vol_per_cell) # remove shot noise estimates (they are inaccurate in this case) real.attrs.update(attrs) real.attrs.pop('data.shotnoise', None) real.attrs.pop('randoms.shotnoise', None) return real
def to_real_field(self): r""" Paint the FKP density field, returning a ``RealField``. Given the ``data`` and ``randoms`` catalogs, this paints: .. math:: F(x) = w_\mathrm{fkp}(x) * [w_\mathrm{comp}(x)*n_\mathrm{data}(x) - \alpha * w_\mathrm{comp}(x)*n_\mathrm{randoms}(x)] This computes the following meta-data attributes in the process of painting, returned in the :attr:`attrs` attributes of the returned RealField object: - randoms.W, data.W : the weighted sum of randoms and data objects; see :func:`weighted_total` - alpha : float the ratio of ``data.W`` to ``randoms.W`` - randoms.norm, data.norm : float the power spectrum normalization; see :func:`normalization` - randoms.shotnoise, data.shotnoise: float the shot noise for each sample; see :func:`shotnoise` - shotnoise : float the total shot noise, equal to the sum of ``randoms.shotnoise`` and ``data.shotnoise`` - randoms.num_per_cell, data.num_per_cell : float the mean number of weighted objects per cell for each sample - num_per_cell : float the mean number of weighted objects per cell For further details on the meta-data, see :ref:`the documentation <fkp-meta-data>`. Returns ------- :class:`~pmesh.pm.RealField` : the field object holding the FKP density field in real space """ # add necessary FKP columns for INTERNAL use for name in self.base.species: # a total weight for the mesh is completeness weight x FKP weight self[name]['_TotalWeight'] = self.TotalWeight(name) # position on the mesh is re-centered to [-BoxSize/2, BoxSize/2] self[name]['_RecenteredPosition'] = self.RecenteredPosition(name) attrs = {} # determine alpha, the weighted number ratio for name in self.base.species: attrs[name + '.W'] = self.weighted_total(name) attrs['alpha'] = attrs['data.W'] / attrs['randoms.W'] # paint the randoms real = self['randoms'].to_real_field(normalize=False) real.attrs.update(attrs_to_dict(real, 'randoms.')) # normalize the randoms by alpha real[:] *= -1. * attrs['alpha'] # paint the data real2 = self['data'].to_real_field(normalize=False) real[:] += real2[:] real.attrs.update(attrs_to_dict(real2, 'data.')) # divide by volume per cell to go from number to number density vol_per_cell = (self.pm.BoxSize / self.pm.Nmesh).prod() real[:] /= vol_per_cell # remove shot noise estimates (they are inaccurate in this case) real.attrs.update(attrs) real.attrs.pop('data.shotnoise', None) real.attrs.pop('randoms.shotnoise', None) # delete internal columns for name in self.base.species: del self[name + '/_RecenteredPosition'] del self[name + '/_TotalWeight'] return real
def to_real_field(self, normalize=True): r""" Paint the density field holding the sum of all particle species, returning a :class:`~pmesh.pm.RealField` object. Meta-data computed for each particle is stored in the :attr:`attrs` attribute of the returned RealField, with keys that are prefixed by the species name. In particular, the total shot noise for the mesh is defined as: .. math:: P_\mathrm{shot} = \sum_i (W_i/W_\mathrm{tot})^2 P_{\mathrm{shot},i}, where the sum is over all species in the catalog, ``W_i`` is the sum of the ``Weight`` column for the :math:`i^\mathrm{th}` species, and :math:`W_\mathrm{tot}` is the sum of :math:`W_i` across all species. Parameters ---------- normalize : bool, optional if ``True``, normalize the density field as :math:`1+\delta`, dividing by the total mean number of objects per cell, as given by the ``num_per_cell`` meta-data value in :attr:`attrs` Returns ------- RealField : the RealField holding the painted density field, with a :attr:`attrs` dictionary attribute holding the meta-data """ # track the sum of the mean number of objects per cell across species attrs = {'num_per_cell':0., 'N':0} # initialize an empty real field real = self.pm.create(type='real', value=0) # loop over each species for name in self.source.species: if self.pm.comm.rank == 0: self.logger.info("painting the '%s' species" %name) # get a CatalogMesh for this species species_mesh = self[name] # paint (in-place) the un-normalized density field for this species species_mesh.to_real_field(out=real, normalize=False) # add to the mean number of objects per cell and total number attrs['num_per_cell'] += real.attrs['num_per_cell'] attrs['N'] += real.attrs['N'] # store the meta-data for this species, with a prefix attrs.update(attrs_to_dict(real, name+'.')) # # normalize the field by nbar -> this is now 1+delta if normalize: real[:] /= attrs['num_per_cell'] # update the meta-data real.attrs.clear() real.attrs.update(attrs) # compute total shot noise by summing of shot noise each species real.attrs['shotnoise'] = 0 total_weight = sum(real.attrs['%s.W' %name] for name in self.source.species) for name in self.source.species: this_Pshot = real.attrs['%s.shotnoise' %name] this_weight = real.attrs['%s.W' %name] real.attrs['shotnoise'] += (this_weight/total_weight)**2 * this_Pshot return real