Skip to content

Commit

Permalink
Add features in BigFileGrid DataSource.
Browse files Browse the repository at this point in the history
These shall probably go into the painter ... But the transfer function
syntax we currently have looks more clumsy that this..
  • Loading branch information
rainwoodman committed Aug 8, 2016
1 parent 3637b7d commit bf209a9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 3 additions & 0 deletions examples/paint/test_grid.params
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ DataSource:
plugin: BigFileGrid
path: ${NBKIT_CACHE}/data/bigfile_grid
dataset: PaintGrid
normalize : true
fk : exp(- 0.5 * (k * 8) ** 2)
frho : (rho - 1) ** 2
Painter:
plugin: DefaultPainter

38 changes: 37 additions & 1 deletion nbodykit/plugins/datasource/BigFileGrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BigFileGridSource(GridSource):
"""
plugin_name = "BigFileGrid"

def __init__(self, path, dataset):
def __init__(self, path, dataset, frho=None, normalize=False, fk=None):
import bigfile
f = bigfile.BigFileMPI(self.comm, self.path)
self.dataset = dataset
Expand All @@ -39,6 +39,9 @@ def register(cls):

s.add_argument("path", type=str, help="the file path to load the data from")
s.add_argument("dataset", type=str, help="the file path to load the data from")
s.add_argument("frho", type=str, help="A python expresion for transforming the real space density field. variables: rho. example: 1 + (rho - 1)**2")
s.add_argument("fk", type=str, help="A python expresion for transforming the fourier space density field. variables: k. example: exp(-(k * 0.5)**2) ")
s.add_argument("normalize", type=bool, help="Normalize the field to set mean == 1")

def read(self, pm):
import bigfile
Expand All @@ -48,4 +51,37 @@ def read(self, pm):
f = bigfile.BigFileMPI(self.comm, self.path)
with f[self.dataset] as ds:
resampler.read(pm, ds, self.Nmesh, self.isfourier)
mean = self.comm.allreduce(pm.real.sum(dtype='f8')) / pm.Nmesh ** 3.

if self.comm.rank == 0:
self.logger.info("Mean = %g" % mean)

if self.normalize:
pm.real *= 1. / mean
mean = self.comm.allreduce(pm.real.sum(dtype='f8')) / pm.Nmesh ** 3.
if self.comm.rank == 0:
self.logger.info("Renormalized mean = %g" % mean)

if self.fk:
if self.comm.rank == 0:
self.logger.info("applying transformation fk %s" % self.fk)

def function(rho):
return eval(self.frho)
pm.r2c()
k = (pm.k[0] ** 2 + pm.k[1] ** 2 + pm.k[2] ** 2) ** 0.5
pm.complex[...] *= function(k)
pm.c2r()

if self.frho:
if self.comm.rank == 0:
self.logger.info("applying transformation frho %s" % self.frho)

def function(rho):
return eval(self.frho)
if self.comm.rank == 0:
self.logger.info("example value before frho %g" % pm.real.flat[0])
pm.real[...] = function(pm.real)
if self.comm.rank == 0:
self.logger.info("example value after frho %g" % pm.real.flat[0])

0 comments on commit bf209a9

Please sign in to comment.