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

step 4

parent ecbf3378
......@@ -22,9 +22,8 @@ from d2o import distributed_data_object
class PowerIndices(object):
"""Computes helpful quantities to deal with power spectra.
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
Given the shape and the density of a underlying grid this class provides
the user with the pindex, kindex and rho. The indices are binned
according to the supplied parameter scheme.
Parameters
......@@ -43,29 +42,6 @@ class PowerIndices(object):
self.k_array = self.domain.get_distance_array(distribution_strategy)
def _cast_config(self, logarithmic, nbin, binbounds):
"""
internal helper function which casts the various combinations of
possible parameters into a properly defaulted dictionary
"""
try:
temp_logarithmic = bool(logarithmic)
except(TypeError):
temp_logarithmic = False
try:
temp_nbin = int(nbin)
except(TypeError):
temp_nbin = None
try:
temp_binbounds = tuple(np.array(binbounds))
except(TypeError):
temp_binbounds = None
return temp_logarithmic, temp_nbin, temp_binbounds
def get_index_dict(self, logarithmic, nbin, binbounds):
"""
Returns a dictionary containing the pindex, kindex and rho
......@@ -82,22 +58,8 @@ class PowerIndices(object):
binbounds : {list, array}
Array-like inner boundaries of the used bins.
Returns
-------
index_dict : dict
Contains the keys: 'config', 'pindex', 'kindex' and 'rho'
"""
# Cast the input arguments
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 pindex, kindex, rho, k_array
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 logarithmic and nbin is None and binbounds is None:
......@@ -117,17 +79,12 @@ class PowerIndices(object):
self._bin_power_indices(
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)
tempindex = temp_pindex.copy(dtype=temp_kindex.dtype)
temp_k_array = tempindex.apply_scalar_function(
lambda x: temp_kindex[x.astype(np.dtype('int'))])
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)
result = tempindex.apply_scalar_function(
lambda x: kindex[x.astype(np.dtype('int'))])
return result
def _compute_indices(self, k_array):
"""
Internal helper function which computes pindex, kindex and rho
......
......@@ -67,9 +67,19 @@ class PowerSpace(Space):
The total volume of the space.
shape : tuple of np.ints
The shape of the space's data array.
config : {logarithmic, nbin, binbounds}
Dictionary storing the values for `logarithmic`, `nbin`, and
`binbounds` that were used during initialization.
logarithmic : bool
True if logarithmic binning should be used.
nbin : {int, None}
The number of bins that should be used for power spectrum binning
(default : None).
if nbin == None, then nbin is set to the length of kindex.
binbounds : {list, array-like}
Array-like inner boundaries of the used bins of the default
indices.
(default : None)
if binbounds == None :
Calculates the bounds from the kindex while applying the
logarithmic and nbin keywords.
Notes
-----
......@@ -98,9 +108,8 @@ class PowerSpace(Space):
self._nbin = nbin
self._binbounds = binbounds
tmp = PowerIndices(self.harmonic_partner, distribution_strategy)
self._pindex, self._kindex, self._rho, self._k_array = tmp.get_index_dict(logarithmic=logarithmic,
nbin=nbin,
binbounds=binbounds)
self._pindex, self._kindex, self._rho, self._k_array = tmp.get_index_dict(logarithmic,
nbin, binbounds)
if nbin is not None:
if nbin > len(self.kindex):
......
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