Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi #564

Merged
merged 6 commits into from Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions nbodykit/algorithms/convpower/tests/test_catalogmesh.py
Expand Up @@ -37,16 +37,18 @@ def test_paint(comm):

# the meshes
mesh = cat.to_mesh(Nmesh=32, BoxSize=512)
mesh1 = source1.to_mesh(Nmesh=32, BoxSize=512)
mesh2 = source2.to_mesh(Nmesh=32, BoxSize=512)

# update weights for source1 and source2
mesh1['Weight'] = source1['Weight'] * source1['FKPWeight']
mesh2['Weight'] = source2['Weight'] * source2['FKPWeight']
source1['FULLWeight'] = source1['Weight'] * source1['FKPWeight']
source2['FULLWeight'] = source2['Weight'] * source2['FKPWeight']

# paint the re-centered Position
mesh1['Position'] = source1['Position'] - mesh.attrs['BoxCenter']
mesh2['Position'] = source2['Position'] - mesh.attrs['BoxCenter']
source1['CPosition'] = source1['Position'] - mesh.attrs['BoxCenter']
source2['CPosition'] = source2['Position'] - mesh.attrs['BoxCenter']


mesh1 = source1.to_mesh(Nmesh=32, BoxSize=512, position='CPosition', weight='FULLWeight')
mesh2 = source2.to_mesh(Nmesh=32, BoxSize=512, position='CPosition', weight='FULLWeight')

# alpha is the sum of Weight
alpha = 1. * source1.csize * WEIGHT1 / (source2.csize * WEIGHT2)
Expand Down
3 changes: 0 additions & 3 deletions nbodykit/source/catalog/species.py
Expand Up @@ -56,9 +56,6 @@ def __repr__(self):

def __init__(self, names, *species, **kwargs):

# input checks
if len(species) < 2:
raise ValueError("need at least 2 particle species to initialize MultipleSpeciesCatalog")
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):
Expand Down
4 changes: 0 additions & 4 deletions nbodykit/source/catalog/tests/test_species.py
Expand Up @@ -47,10 +47,6 @@ def test_bad_input(comm):
source1 = UniformCatalog(nbar=3e-5, BoxSize=512., seed=42, comm=comm)
source2 = UniformCatalog(nbar=3e-5, BoxSize=512., seed=84, comm=comm)

# need 2 species
with pytest.raises(ValueError):
cat = MultipleSpeciesCatalog(['data'], source1)

# non-unique names
with pytest.raises(ValueError):
cat = MultipleSpeciesCatalog(['data', 'data'], source1, source2)
Expand Down
2 changes: 1 addition & 1 deletion nbodykit/source/mesh/array.py
Expand Up @@ -51,7 +51,7 @@ def __init__(self, array, BoxSize, comm=None, root=0, **kwargs):

MeshSource.__init__(self, comm, Nmesh, BoxSize, empty.real.dtype)

self.field = self.pm.create(mode='real')
self.field = self.pm.create(type='real')

if comm.rank != root:
array = empty # ignore data from other ranks.
Expand Down
23 changes: 8 additions & 15 deletions nbodykit/source/mesh/catalog.py
Expand Up @@ -86,22 +86,15 @@ def __init__(self, source, Nmesh, BoxSize,
# store others as straight attributes
self.dtype = dtype

self['Position'] = Position
self['Weight'] = Weight
self['Value'] = Value
self['Selection'] = Selection
self.Position = Position
self.Weight = Weight
self.Value = Value
self.Selection = Selection

self.attrs['interlaced'] = interlaced
self.attrs['compensated'] = compensated
self.attrs['resampler'] = str(resampler)

def __getitem__(self, key):
return self._columns[key]
def __setitem__(self, key, value):
self._columns[key] = value
def __contains__(self, key, value):
return key in self._columns

@property
def interlaced(self):
"""
Expand Down Expand Up @@ -220,10 +213,10 @@ def to_real_field(self, out=None, normalize=True):
real2 = RealField(pm)
real2[:] = 0

Position = self['Position']
Weight = self['Weight']
Value = self['Value']
Selection = self['Selection']
Position = self.Position
Weight = self.Weight
Value = self.Value
Selection = self.Selection

# ensure the slices are synced, since decomposition is collective
Nlocalmax = max(pm.comm.allgather(len(Position)))
Expand Down
34 changes: 17 additions & 17 deletions nbodykit/source/mesh/species.py
@@ -1,4 +1,4 @@
from nbodykit.base.mesh import MeshSource
from nbodykit.source.mesh.catalog import CatalogMesh
from nbodykit import _global_options
import numpy
import logging
Expand All @@ -8,7 +8,7 @@
from pmesh.pm import RealField

from nbodykit.utils import attrs_to_dict
class MultipleSpeciesCatalogMesh(MeshSource):
class MultipleSpeciesCatalogMesh(CatalogMesh):
"""
A subclass of :class:`~nbodykit.base.catalogmesh.CatalogMesh`
designed to paint the density field from a sum of multiple types
Expand Down Expand Up @@ -51,19 +51,24 @@ def __init__(self, source, Nmesh, BoxSize, dtype,
raise TypeError(("the input source for MultipleSpeciesCatalogMesh "
"must be a MultipleSpeciesCatalog"))

MeshSource.__init__(self, Nmesh=Nmesh, BoxSize=BoxSize, dtype=dtype, comm=source.comm)
CatalogMesh.__init__(self,
source=source,
Nmesh=Nmesh, BoxSize=BoxSize, dtype=dtype,
Position=None,
Selection=None,
Weight=None,
Value=None,
resampler=resampler,
compensated=compensated,
interlaced=interlaced,
comm=source.comm)

self.species = source.species

self.source = source
self.weight = weight
self.position = position
self.value = value
self.selection = selection
self.interlaced = interlaced
self.compensated = compensated
self.resampler = resampler
self.dtype = dtype

self.species = source.species
self.weight = weight
self.value = value

def __iter__(self):
return iter(self.species)
Expand Down Expand Up @@ -175,8 +180,3 @@ def to_real_field(self, normalize=True):
real.attrs['shotnoise'] += (this_weight/total_weight)**2 * this_Pshot

return real

def _get_compensation(self):
from nbodykit.source.mesh.catalog import get_compensation
return get_compensation(self.interlaced, self.resampler)

2 changes: 1 addition & 1 deletion nbodykit/source/mesh/tests/test_array.py
Expand Up @@ -13,7 +13,7 @@ def test_paint(comm):

# initialize a random white noise field in real space
pm = ParticleMesh(Nmesh=(8, 8, 8), BoxSize=(128, 128, 128.), comm=comm)
real = pm.generate_whitenoise(mode='real', seed=3333) # a RealField
real = pm.generate_whitenoise(type='real', seed=3333) # a RealField

# FFT to a ComplexField
complex = real.r2c()
Expand Down
2 changes: 1 addition & 1 deletion nbodykit/source/mesh/tests/test_field.py
Expand Up @@ -10,7 +10,7 @@ def test_paint(comm):
from pmesh.pm import ParticleMesh

pm = ParticleMesh(Nmesh=(8, 8, 8), BoxSize=(128, 128, 128.), comm=comm)
real = pm.generate_whitenoise(mode='real', seed=3333)
real = pm.generate_whitenoise(type='real', seed=3333)
complex = real.r2c()

realmesh = FieldMesh(real)
Expand Down
17 changes: 17 additions & 0 deletions nbodykit/source/mesh/tests/test_species.py
Expand Up @@ -71,6 +71,23 @@ def test_compute(comm):
# must be the same
assert_allclose(combined.value, (real1.value + real2.value)/norm, atol=1e-5)

@MPITest([1, 4])
def test_actions_compensated(comm):

# the test case fails only if there is enough particles to trigger
# the second loop of the interlaced painter; these parameters will do it.

# the catalog
source1 = UniformCatalog(nbar=1e-0, BoxSize=111, seed=111, comm=comm)
source2 = UniformCatalog(nbar=1e-0, BoxSize=111, seed=111, comm=comm)
source1['Weight'] = 1.0
source2['Weight'] = 0.1
cat = MultipleSpeciesCatalog(['data', 'randoms'], source1, source2)

# the meshes
mesh = cat.to_mesh(Nmesh=32, compensated=True)
assert len(mesh.actions) > 0

@MPITest([1, 4])
def test_paint_interlaced(comm):

Expand Down