Commit ecbf3378 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

step 3

parent 52e16b70
......@@ -25,8 +25,7 @@ class PowerIndices(object):
Given the shape and the density of a underlying rectangular grid this
class provides the user
with the pindex, kindex and rho. The indices are binned
according to the supplied parameter scheme. If wanted, computed
results are stored for future reuse.
according to the supplied parameter scheme.
Parameters
----------
......@@ -44,24 +43,11 @@ class PowerIndices(object):
self.k_array = self.domain.get_distance_array(distribution_strategy)
def _cast_config(self, **kwargs):
def _cast_config(self, logarithmic, nbin, binbounds):
"""
internal helper function which casts the various combinations of
possible parameters into a properly defaulted dictionary
"""
temp_logarithmic = kwargs.get("logarithmic")
temp_nbin = kwargs.get("nbin")
temp_binbounds = kwargs.get("binbounds")
return self._cast_config_helper(logarithmic=temp_logarithmic,
nbin=temp_nbin,
binbounds=temp_binbounds)
def _cast_config_helper(self, logarithmic, nbin, binbounds):
"""
internal helper function which sets the defaults for the
_cast_config function
"""
try:
temp_logarithmic = bool(logarithmic)
......@@ -78,12 +64,9 @@ class PowerIndices(object):
except(TypeError):
temp_binbounds = None
temp_dict = {"logarithmic": temp_logarithmic,
"nbin": temp_nbin,
"binbounds": temp_binbounds}
return temp_dict
return temp_logarithmic, temp_nbin, temp_binbounds
def get_index_dict(self, **kwargs):
def get_index_dict(self, logarithmic, nbin, binbounds):
"""
Returns a dictionary containing the pindex, kindex and rho
binned according to the supplied parameter scheme and a
......@@ -105,30 +88,19 @@ class PowerIndices(object):
Contains the keys: 'config', 'pindex', 'kindex' and 'rho'
"""
# Cast the input arguments
temp_config_dict = self._cast_config(**kwargs)
# Compute a hashable identifier from the config which will be used
# as dict key
temp_key = self._freeze_config(temp_config_dict)
temp_index_dict = self._compute_index_dict(temp_config_dict)
loarithmic, nbin, binbounds = self._cast_config(logarithmic, nbin, binbounds)
pindex, kindex, rho, k_array = self._compute_index_dict(logarithmic, nbin, binbounds)
# Return the plain result.
return temp_index_dict
return pindex, kindex, rho, k_array
def _freeze_config(self, config_dict):
"""
a helper function which forms a hashable identifying object from
a config dictionary which can be used as key of a dict
"""
return frozenset(config_dict.items())
def _compute_index_dict(self, config_dict):
def _compute_index_dict(self, logarithmic, nbin, binbounds):
"""
Internal helper function which takes a config_dict, asks for the
pindex/kindex/rho set, and bins them according to the config
"""
# if no binning is requested, compute the indices, build the dict,
# and return it straight.
if not config_dict["logarithmic"] and config_dict["nbin"] is None and \
config_dict["binbounds"] is None:
if not logarithmic and nbin is None and binbounds is None:
(temp_pindex, temp_kindex, temp_rho) =\
self._compute_indices(self.k_array)
temp_k_array = self.k_array
......@@ -137,24 +109,18 @@ class PowerIndices(object):
# indices, bin them, and then return everything.
else:
# Get the unbinned indices
temp_unbinned_indices = self.get_index_dict(nbin=None,
pindex, kindex, rho, dummy = self.get_index_dict(nbin=None,
binbounds=None,
logarithmic=False,
store=False)
logarithmic=False)
# Bin them
(temp_pindex, temp_kindex, temp_rho) = \
self._bin_power_indices(
temp_unbinned_indices, **config_dict)
pindex, kindex, rho, logarithmic, nbin, binbounds)
# Make a binned version of k_array
temp_k_array = self._compute_k_array_from_pindex_kindex(
temp_pindex, temp_kindex)
temp_index_dict = {"config": config_dict,
"pindex": temp_pindex,
"kindex": temp_kindex,
"rho": temp_rho,
"k_array": temp_k_array}
return temp_index_dict
return temp_pindex, temp_kindex, temp_rho, temp_k_array
def _compute_k_array_from_pindex_kindex(self, pindex, kindex):
tempindex = pindex.copy(dtype=kindex.dtype)
......@@ -192,7 +158,7 @@ class PowerIndices(object):
return global_pindex, global_kindex, global_rho
def _bin_power_indices(self, index_dict, **kwargs):
def _bin_power_indices(self, pindex, kindex, rho, logarithmic, nbin, binbounds):
"""
Returns the binned power indices associated with the Fourier grid.
......@@ -219,16 +185,6 @@ class PowerIndices(object):
The (re)binned power indices.
"""
# Cast the given config
temp_config_dict = self._cast_config(**kwargs)
logarithmic = temp_config_dict['logarithmic']
nbin = temp_config_dict['nbin']
binbounds = temp_config_dict['binbounds']
# Extract the necessary indices from the supplied index dict
pindex = index_dict["pindex"]
kindex = index_dict["kindex"]
rho = index_dict["rho"]
# boundaries
if(binbounds is not None):
......
......@@ -94,20 +94,16 @@ class PowerSpace(Space):
"harmonic_partner must be a harmonic space.")
self._harmonic_partner = harmonic_partner
self._logarithmic = logarithmic
self._nbin = nbin
self._binbounds = binbounds
tmp = PowerIndices(self.harmonic_partner, distribution_strategy)
power_index = tmp.get_index_dict(logarithmic=logarithmic,
self._pindex, self._kindex, self._rho, self._k_array = tmp.get_index_dict(logarithmic=logarithmic,
nbin=nbin,
binbounds=binbounds)
self._config = power_index['config']
self._pindex = power_index['pindex']
self._kindex = power_index['kindex']
self._rho = power_index['rho']
self._k_array = power_index['k_array']
if self.config['nbin'] is not None:
if self.config['nbin'] > len(self.kindex):
if nbin is not None:
if nbin > len(self.kindex):
self.logger.warn("nbin was set to a value being larger than "
"the length of kindex!")
......@@ -145,8 +141,7 @@ class PowerSpace(Space):
return ("PowerSpace(harmonic_partner=%r, distribution_strategy=%r, "
"logarithmic=%r, nbin=%r, binbounds=%r)"
% (self.harmonic_partner, self.pindex.distribution_strategy,
self.config['logarithmic'], self.config['nbin'],
self.config['binbounds']))
self._logarithmic, self._nbin, self._binbounds))
@property
def harmonic(self):
......@@ -169,9 +164,9 @@ class PowerSpace(Space):
distribution_strategy = self.pindex.distribution_strategy
return self.__class__(harmonic_partner=self.harmonic_partner,
distribution_strategy=distribution_strategy,
logarithmic=self.config["logarithmic"],
nbin=self.config["nbin"],
binbounds=self.config["binbounds"])
logarithmic=self._logarithmic,
nbin=self._nbin,
binbounds=self._binbounds)
def weight(self, x, power=1, axes=None, inplace=False):
reshaper = [1, ] * len(x.shape)
......@@ -209,11 +204,16 @@ class PowerSpace(Space):
return self._harmonic_partner
@property
def config(self):
""" Returns the configuration which was used for `logarithmic`, `nbin`
and `binbounds` during initialization.
"""
return self._config
def logarithmic(self):
return self._logarithmic
@property
def nbin(self):
return self._nbin
@property
def binbounds(self):
return self._binbounds
@property
def pindex(self):
......@@ -246,10 +246,10 @@ class PowerSpace(Space):
def _to_hdf5(self, hdf5_group):
hdf5_group['kindex'] = self.kindex
hdf5_group['rho'] = self.rho
hdf5_group['logarithmic'] = self.config["logarithmic"]
hdf5_group['logarithmic'] = self._logarithmic
# Store nbin as string, since it can be None
hdf5_group.attrs['nbin'] = str(self.config["nbin"])
hdf5_group.attrs['binbounds'] = str(self.config["binbounds"])
hdf5_group.attrs['nbin'] = str(self._nbin)
hdf5_group.attrs['binbounds'] = str(self._binbounds)
#MR FIXME: why not "return None" as happens everywhere else?
return {
......@@ -270,10 +270,9 @@ class PowerSpace(Space):
new_ps._harmonic_partner = repository.get('harmonic_partner',
hdf5_group)
new_ps._config = {}
new_ps._config['logarithmic'] = hdf5_group['logarithmic'][()]
exec("new_ps._config['nbin'] = " + hdf5_group.attrs['nbin'])
exec("new_ps._config['binbounds'] = " + hdf5_group.attrs['binbounds'])
new_ps._logarithmic = hdf5_group['logarithmic'][()]
exec("new_ps._nbin = " + hdf5_group.attrs['nbin'])
exec("new_ps._binbounds = " + hdf5_group.attrs['binbounds'])
new_ps._pindex = repository.get('pindex', hdf5_group)
new_ps._kindex = hdf5_group['kindex'][:]
......
......@@ -64,7 +64,9 @@ CONSTRUCTOR_CONFIGS = [
'dim': 5,
'total_volume': 8.0,
'harmonic_partner': RGSpace((8,), harmonic=True),
'config': {'logarithmic': False, 'nbin': None, 'binbounds': None},
'logarithmic': False,
'nbin': None,
'binbounds': None,
'pindex': distributed_data_object([0, 1, 2, 3, 4, 3, 2, 1]),
'kindex': np.array([0., 1., 2., 3., 4.]),
'rho': np.array([1, 2, 2, 2, 1]),
......@@ -76,7 +78,9 @@ CONSTRUCTOR_CONFIGS = [
'dim': 2,
'total_volume': 8.0,
'harmonic_partner': RGSpace((8,), harmonic=True),
'config': {'logarithmic': True, 'nbin': None, 'binbounds': None},
'logarithmic': True,
'nbin': None,
'binbounds': None,
'pindex': distributed_data_object([0, 1, 1, 1, 1, 1, 1, 1]),
'kindex': np.array([0., 2.28571429]),
'rho': np.array([1, 7]),
......@@ -114,7 +118,9 @@ def get_weight_configs():
class PowerSpaceInterfaceTest(unittest.TestCase):
@expand([
['harmonic_partner', Space],
['config', dict],
['logarithmic', bool],
['nbin', NoneType],
['binbounds', NoneType],
['pindex', distributed_data_object],
['kindex', np.ndarray],
['rho', np.ndarray],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment