diff --git a/nifty/spaces/power_space/power_index_factory.py b/nifty/spaces/power_space/power_index_factory.py index 04958569703848839cc244b32b072a561c19d6e1..ab14e5662f2ec221d0cd652e71b8809be6e54cb0 100644 --- a/nifty/spaces/power_space/power_index_factory.py +++ b/nifty/spaces/power_space/power_index_factory.py @@ -30,7 +30,9 @@ class _PowerIndexFactory(object): if key not in self.power_indices_storage: self.power_indices_storage[key] = \ PowerIndices(domain, distribution_strategy, - logarithmic=logarithmic, nbin=nbin, binbounds=binbounds) + logarithmic=logarithmic, + nbin=nbin, + binbounds=binbounds) power_indices = self.power_indices_storage[key] power_index = power_indices.get_index_dict(logarithmic=logarithmic, nbin=nbin, diff --git a/nifty/spaces/power_space/power_indices.py b/nifty/spaces/power_space/power_indices.py index 73c88e4f70a1d257358c7f04fc1bc3614482948f..64018cd6b1b561caae559afc8d7af2d5a00d4690 100644 --- a/nifty/spaces/power_space/power_indices.py +++ b/nifty/spaces/power_space/power_indices.py @@ -113,7 +113,8 @@ class PowerIndices(object): return self._cast_config_helper(**temp_config_dict) else: defaults = self.default_parameters - temp_logarithmic = kwargs.get("logarithmic", defaults['logarithmic']) + temp_logarithmic = kwargs.get("logarithmic", + defaults['logarithmic']) temp_nbin = kwargs.get("nbin", defaults['nbin']) temp_binbounds = kwargs.get("binbounds", defaults['binbounds']) diff --git a/nifty/spaces/power_space/power_space.py b/nifty/spaces/power_space/power_space.py index efb9b716f25f2742c6d83b5a69f520f1781d0590..223404ee94467387f958a6c3fa011d7a012b6bce 100644 --- a/nifty/spaces/power_space/power_space.py +++ b/nifty/spaces/power_space/power_space.py @@ -27,45 +27,55 @@ from nifty.spaces.rg_space import RGSpace class PowerSpace(Space): + """ NIFTY class for spaces of power spectra. + + Parameters + ---------- + harmonic_partner : Space + The harmonic Space of which this is the power space. + distribution_strategy : str *optional* + The distribution strategy used for the distributed_data_objects + derived from this PowerSpace, e.g. the pindex. + (default : 'not') + logarithmic : bool *optional* + True if logarithmic binning should be used (default : False). + nbin : {int, None} *optional* + 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} *optional* + 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. + + Attributes + ---------- + pindex : distributed_data_object + TODO add description + kindex : numpy.ndarray + TODO add description + pundex : numpy.ndarray + TODO add description + rho : numpy.ndarray + The amount of k-modes that get mapped to one power bin is given by + rho. + + + Notes + ----- + A power space is the result of a projection of a harmonic space where + k-modes of equal length get mapped to one power index. + + """ # ---Overwritten properties and methods--- def __init__(self, harmonic_partner=RGSpace((1,)), distribution_strategy='not', logarithmic=False, nbin=None, binbounds=None): - """Sets the attributes for a PowerSpace class instance. - - Parameters - ---------- - harmonic_partner : Space - The harmonic Space of which this is the power space. - distribution_strategy : str *optional* - The distribution strategy of a d2o-object represeting a field over this PowerSpace. - (default : 'not') - logarithmic : bool *optional* - True if logarithmic binning should be used. - (default : False) - nbin : {int, None} *optional* - The number of bins this space has. - (default : None) if nbin == None : It takes the nbin from its harmonic_partner - binbounds : {list, array} *optional* - Array-like inner boundaries of the used bins of the default - indices. - (default : None) if binbounds == None : Calculates the bounds from the kindex and corrects for logartihmic scale - Notes - ----- - A power space is the result of a projection of a harmonic space where multiple k-modes get mapped to one power index. - This can be regarded as a response operator :math:`R` going from harmonic space to power space. - An array giving this map is stored in pindex (array which says in which power box a k-mode gets projected) - An array for the adjoint of :math:`R` is given by kindex, which is an array of arrays stating which k-mode got mapped to a power index - The a right-inverse to :math:`R` is given by the pundex which is an array giving one k-mode that maps to a power bin for every power bin. - The amount of k-modes that get mapped to one power bin is given by rho. This is :math:`RR^\dagger` in the language of this projection operator - Returns - ------- - None. - - """ - #FIXME: default probably not working for log and normal scale super(PowerSpace, self).__init__() self._ignore_for_hash += ['_pindex', '_kindex', '_rho', '_pundex', '_k_array'] @@ -97,22 +107,28 @@ class PowerSpace(Space): self._k_array = power_index['k_array'] def pre_cast(self, x, axes): - """Casts power spectra to discretized power spectra. - - This function takes an array or a function. If it is an array it does nothing, - otherwise it intepretes the function as power spectrum and evaluates it at every - k-mode. + """ Casts power spectrum functions to discretized power spectra. + + This function takes an array or a function. If it is an array it does + nothing, otherwise it interpretes the function as power spectrum and + evaluates it at every k-mode. + Parameters ---------- x : {array-like, function array-like -> array-like} - power spectrum given either in discretized form or implicitly as a function - axes : {tuple, int} *optional* - does nothing - (default : None) + power spectrum given either in discretized form or implicitly as a + function + axes : tuple of ints + Specifies the axes of x which correspond to this space. For + explicifying the power spectrum function, this is ignored. + Returns ------- - array-like : discretized power spectrum + array-like + discretized power spectrum + """ + if callable(x): return x(self.kindex) else: @@ -176,86 +192,60 @@ class PowerSpace(Space): @property def harmonic_partner(self): - """Returns the Space of which this is the power space. - Returns - ------- - Space : The harmonic Space of which this is the power space. + """ Returns the Space of which this is the power space. """ return self._harmonic_partner @property def logarithmic(self): - """Returns a True if logarithmic binning is used. - Returns - ------- - Bool : True if for this PowerSpace logarithmic binning is used. + """ Returns True if logarithmic binning is used. """ return self._logarithmic @property def nbin(self): - """Returns the number of power bins. - Returns - ------- - int : The number of bins this space has. + """ Returns the number of power bins if specfied during initialization. """ return self._nbin @property def binbounds(self): - """ Inner boundaries of the used bins of the default - indices. - Returns - ------- - {list, array} : the inner boundaries of the used bins in the used scale, as they were - set in __init__ or computed. + """ Inner boundaries of the used bins if specfied during initialization. """ - # FIXME check wether this returns something sensible if 'None' was set in __init__ return self._binbounds @property def pindex(self): - """Index of the Fourier grid points that belong to a specific power index - Returns - ------- - distributed_data_object : Index of the Fourier grid points in a distributed_data_object. - """ + """ A distributed_data_objects having the shape of the harmonic partner + space containing the indices of the power bin a pixel belongs to. + """ return self._pindex @property def kindex(self): - """Array of all k-vector lengths. - Returns - ------- - ndarray : Array which states for each k-mode which power index it maps to (adjoint to pindex) - """ + """ Sorted array of all k-modes. + """ return self._kindex @property def rho(self): - """Degeneracy factor of the individual k-vectors. - - ndarray : Array stating how many k-modes are mapped to one power index for every power index - """ + """Degeneracy factor of the individual k-vectors. + """ return self._rho @property def pundex(self): - """List of one k-mode per power bin which is in the bin. - Returns - ------- - array-like : An array for which the n-th entry is an example one k-mode which belongs to the n-th power bin - """ + """ An array for which the n-th entry gives the flat index of the + first occurence of a k-vector with length==kindex[n] in the + k_array. + """ return self._pundex @property def k_array(self): - """This contains distances to zero for every k-mode of the harmonic partner. - - Returns - ------- - array-like : An array containing distances to the zero mode for every k-mode of the harmonic partner. - """ + """ An array containing distances to the grid center (i.e. zero-mode) + for every k-mode in the grid of the harmonic partner space. + """ return self._k_array # ---Serialization--- @@ -284,7 +274,8 @@ class PowerSpace(Space): # call instructor so that classes are properly setup super(PowerSpace, new_ps).__init__() # set all values - new_ps._harmonic_partner = repository.get('harmonic_partner', hdf5_group) + new_ps._harmonic_partner = repository.get('harmonic_partner', + hdf5_group) new_ps._logarithmic = hdf5_group['logarithmic'][()] exec('new_ps._nbin = ' + hdf5_group.attrs['nbin']) exec('new_ps._binbounds = ' + hdf5_group.attrs['binbounds']) diff --git a/test/test_spaces/test_power_space.py b/test/test_spaces/test_power_space.py index 50664d1e13ec4cecdf88f4cf9130a8ba24509174..20fba9a7dd34c77e2b3271c24e724d135f401c8b 100644 --- a/test/test_spaces/test_power_space.py +++ b/test/test_spaces/test_power_space.py @@ -29,7 +29,7 @@ from types import NoneType from test.common import expand # [harmonic_domain, distribution_strategy, -# log, nbin, binbounds, expected] +# logarithmic, nbin, binbounds, expected] CONSTRUCTOR_CONFIGS = [ [1, 'not', False, None, None, {'error': ValueError}], [RGSpace((8,)), 'not', False, None, None, {'error': ValueError}], @@ -95,7 +95,7 @@ def get_weight_configs(): class PowerSpaceInterfaceTest(unittest.TestCase): @expand([ ['harmonic_domain', Space], - ['log', bool], + ['logarithmic', bool], ['nbin', (int, NoneType)], ['binbounds', (list, NoneType)], ['pindex', distributed_data_object], @@ -112,17 +112,19 @@ class PowerSpaceInterfaceTest(unittest.TestCase): class PowerSpaceFunctionalityTest(unittest.TestCase): @expand(CONSTRUCTOR_CONFIGS) - def test_constructor(self, harmonic_domain, distribution_strategy, log, - nbin, binbounds, expected): + def test_constructor(self, harmonic_domain, distribution_strategy, + logarithmic, nbin, binbounds, expected): if 'error' in expected: with assert_raises(expected['error']): PowerSpace(harmonic_domain=harmonic_domain, distribution_strategy=distribution_strategy, - log=log, nbin=nbin, binbounds=binbounds) + logarithmic=logarithmic, nbin=nbin, + binbounds=binbounds) else: p = PowerSpace(harmonic_domain=harmonic_domain, distribution_strategy=distribution_strategy, - log=log, nbin=nbin, binbounds=binbounds) + logarithmic=logarithmic, nbin=nbin, + binbounds=binbounds) for key, value in expected.iteritems(): if isinstance(value, np.ndarray): assert_almost_equal(getattr(p, key), value)