Commit 1e6af087 authored by Ultima's avatar Ultima
Browse files

Cleaning up. Making more files PEP8 compliant.

Abstracted the power_indices class from rg_space -> Implemented a lm_power_indices class.
In the middle of space-interface cleaning.
parent 6c50de1e
...@@ -35,7 +35,6 @@ from keepers import about,\ ...@@ -35,7 +35,6 @@ from keepers import about,\
from nifty_cmaps import ncmap from nifty_cmaps import ncmap
from nifty_core import space,\ from nifty_core import space,\
point_space,\ point_space,\
nested_space,\
field field
from nifty_mpi_data import distributed_data_object, d2o_librarian from nifty_mpi_data import distributed_data_object, d2o_librarian
......
...@@ -16,7 +16,11 @@ MAX = np.max ...@@ -16,7 +16,11 @@ MAX = np.max
SUM = np.sum SUM = np.sum
class _COMM_WORLD(): class Comm(object):
pass
class Intracomm(Comm):
def __init__(self): def __init__(self):
self.rank = 0 self.rank = 0
self.size = 1 self.size = 1
...@@ -111,4 +115,4 @@ COMPLEX = _datatype("MPI_COMPLEX") ...@@ -111,4 +115,4 @@ COMPLEX = _datatype("MPI_COMPLEX")
DOUBLE_COMPLEX = _datatype("MPI_DOUBLE_COMPLEX") DOUBLE_COMPLEX = _datatype("MPI_DOUBLE_COMPLEX")
COMM_WORLD = _COMM_WORLD() COMM_WORLD = Intracomm()
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import ConfigParser import ConfigParser
import pprint
class variable(object): class variable(object):
def __init__(self, name, default_value_list, checker, genus='str'): def __init__(self, name, default_value_list, checker, genus='str'):
...@@ -47,7 +49,7 @@ class variable(object): ...@@ -47,7 +49,7 @@ class variable(object):
return self.name return self.name
def __repr__(self): def __repr__(self):
return "<nifty variable '" + str(self.name) + "': " + \ return "<" + str(self.name) + "': " + \
str(self.get_value()) + ">" str(self.get_value()) + ">"
...@@ -161,7 +163,9 @@ class configuration(object): ...@@ -161,7 +163,9 @@ class configuration(object):
item.set_value(temp_value) item.set_value(temp_value)
def __repr__(self): def __repr__(self):
return "<nifty configuration> \n" + self.variable_dict.__repr__() return_string = ("<nifty configuration> \n" +
pprint.pformat(self.variable_dict))
return return_string
......
...@@ -6,6 +6,9 @@ from nifty_dependency_injector import dependency_injector ...@@ -6,6 +6,9 @@ from nifty_dependency_injector import dependency_injector
from nifty_configuration import variable,\ from nifty_configuration import variable,\
configuration configuration
global_dependency_injector = dependency_injector( global_dependency_injector = dependency_injector(
['h5py', ['h5py',
('mpi4py.MPI', 'MPI'), ('mpi4py.MPI', 'MPI'),
...@@ -23,12 +26,12 @@ variable_fft_module = variable('fft_module', ...@@ -23,12 +26,12 @@ variable_fft_module = variable('fft_module',
variable_lm2gl = variable('lm2gl', variable_lm2gl = variable('lm2gl',
[True, False], [True, False],
lambda z: z is True or z is False, lambda z: z is True or z is False,
'boolean') genus = 'boolean')
variable_verbosity = variable('verbosity', variable_verbosity = variable('verbosity',
[1], [1],
lambda z: z == abs(int(z)), lambda z: z == abs(int(z)),
'int') genus = 'int')
variable_mpi_module = variable('mpi_module', variable_mpi_module = variable('mpi_module',
['MPI', 'MPI_dummy'], ['MPI', 'MPI_dummy'],
...@@ -49,3 +52,12 @@ global_configuration = configuration( ...@@ -49,3 +52,12 @@ global_configuration = configuration(
variable_default_distribution_strategy variable_default_distribution_strategy
], ],
path=os.path.expanduser('~') + "/.nifty/global_config") path=os.path.expanduser('~') + "/.nifty/global_config")
variable_default_comm = variable(
'default_comm',
['COMM_WORLD'],
lambda z: hasattr(global_dependency_injector[
global_configuration['mpi_module']], z))
global_configuration.register(variable_default_comm)
This diff is collapsed.
...@@ -21,8 +21,9 @@ ...@@ -21,8 +21,9 @@
#from nifty import * #from nifty import *
import numpy as np import numpy as np
from numpy import pi
from nifty.keepers import about from nifty.keepers import about
from nifty.nifty_core import pi, field from nifty.nifty_core import field
from nifty.nifty_simple_math import sqrt, exp, log from nifty.nifty_simple_math import sqrt, exp, log
......
This diff is collapsed.
...@@ -32,30 +32,6 @@ MPI = gdi[gc['mpi_module']] ...@@ -32,30 +32,6 @@ MPI = gdi[gc['mpi_module']]
h5py = gdi.get('h5py') h5py = gdi.get('h5py')
pyfftw = gdi.get('pyfftw') pyfftw = gdi.get('pyfftw')
## initialize the 'FOUND-packages'-dictionary
#FOUND = {}
#try:
# from mpi4py import MPI
# FOUND['MPI'] = True
#except(ImportError):
# import mpi_dummy as MPI
# FOUND['MPI'] = False
#
#try:
# import pyfftw
# FOUND['pyfftw'] = True
#except(ImportError):
# FOUND['pyfftw'] = False
#
#try:
# import h5py
# FOUND['h5py'] = True
# FOUND['h5py_parallel'] = h5py.get_config().mpi
#except(ImportError):
# FOUND['h5py'] = False
# FOUND['h5py_parallel'] = False
_maybe_fftw = ['fftw'] if ('pyfftw' in gdi) else [] _maybe_fftw = ['fftw'] if ('pyfftw' in gdi) else []
STRATEGIES = { STRATEGIES = {
'all': ['not', 'equal', 'freeform'] + _maybe_fftw, 'all': ['not', 'equal', 'freeform'] + _maybe_fftw,
...@@ -687,7 +663,7 @@ class distributed_data_object(object): ...@@ -687,7 +663,7 @@ class distributed_data_object(object):
if weights is not None: if weights is not None:
local_weights = self.distributor.extract_local_data(weights).\ local_weights = self.distributor.extract_local_data(weights).\
flatten() flatten()
else: else:
local_weights = None local_weights = None
......
...@@ -56,7 +56,7 @@ class point_space_paradict(space_paradict): ...@@ -56,7 +56,7 @@ class point_space_paradict(space_paradict):
class rg_space_paradict(space_paradict): class rg_space_paradict(space_paradict):
def __init__(self, num, complexity=2, zerocenter=False): def __init__(self, num, complexity, zerocenter):
self.ndim = len(np.array(num).flatten()) self.ndim = len(np.array(num).flatten())
space_paradict.__init__( space_paradict.__init__(
self, num=num, complexity=complexity, zerocenter=zerocenter) self, num=num, complexity=complexity, zerocenter=zerocenter)
...@@ -100,7 +100,7 @@ class nested_space_paradict(space_paradict): ...@@ -100,7 +100,7 @@ class nested_space_paradict(space_paradict):
class lm_space_paradict(space_paradict): class lm_space_paradict(space_paradict):
def __init__(self, lmax, mmax=None): def __init__(self, lmax, mmax):
space_paradict.__init__(self, lmax=lmax) space_paradict.__init__(self, lmax=lmax)
if mmax is None: if mmax is None:
mmax = -1 mmax = -1
...@@ -145,7 +145,7 @@ class lm_space_paradict(space_paradict): ...@@ -145,7 +145,7 @@ class lm_space_paradict(space_paradict):
class gl_space_paradict(space_paradict): class gl_space_paradict(space_paradict):
def __init__(self, nlat, nlon=None): def __init__(self, nlat, nlon):
space_paradict.__init__(self, nlat=nlat) space_paradict.__init__(self, nlat=nlat)
if nlon is None: if nlon is None:
nlon = -1 nlon = -1
......
This diff is collapsed.
## NIFTY (Numerical Information Field Theory) has been developed at the # NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics. # Max-Planck-Institute for Astrophysics.
## ##
## Copyright (C) 2013 Max-Planck-Society # Copyright (C) 2013 Max-Planck-Society
## ##
## Author: Marco Selig # Author: Marco Selig
## Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/> # Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
## ##
## This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or # the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version. # (at your option) any later version.
## ##
## This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
## See the GNU General Public License for more details. # See the GNU General Public License for more details.
## ##
## You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import numpy as np import numpy as np
from keepers import about from keepers import about
from nifty_mpi_data import distributed_data_object from nifty_mpi_data import distributed_data_object
##----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
class random(object): class random(object):
""" """
...@@ -39,10 +40,10 @@ class random(object): ...@@ -39,10 +40,10 @@ class random(object):
""" """
__init__ = None __init__ = None
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@staticmethod @staticmethod
def parse_arguments(domain,**kwargs): def parse_arguments(domain, **kwargs):
""" """
Analyses the keyword arguments for supported or necessary ones. Analyses the keyword arguments for supported or necessary ones.
...@@ -91,18 +92,21 @@ class random(object): ...@@ -91,18 +92,21 @@ class random(object):
codomain : nifty.space, *optional* codomain : nifty.space, *optional*
A compatible codomain for power indexing (default: None). A compatible codomain for power indexing (default: None).
log : bool, *optional* log : bool, *optional*
Flag specifying if the spectral binning is performed on logarithmic Flag specifying if the spectral binning is performed on
logarithmic
scale or not; if set, the number of used bins is set scale or not; if set, the number of used bins is set
automatically (if not given otherwise); by default no binning automatically (if not given otherwise); by default no binning
is done (default: None). is done (default: None).
nbin : integer, *optional* nbin : integer, *optional*
Number of used spectral bins; if given `log` is set to ``False``; Number of used spectral bins; if given `log` is set to
``False``;
integers below the minimum of 3 induce an automatic setting; integers below the minimum of 3 induce an automatic setting;
by default no binning is done (default: None). by default no binning is done (default: None).
binbounds : {list, array}, *optional* binbounds : {list, array}, *optional*
User specific inner boundaries of the bins, which are preferred User specific inner boundaries of the bins, which are preferred
over the above parameters; by default no binning is done over the above parameters; by default no binning is done
(default: None). vmin : {scalar, list, ndarray, field}, *optional* (default: None).
vmin : {scalar, list, ndarray, field}, *optional*
Lower limit of the uniform distribution if ``random == "uni"`` Lower limit of the uniform distribution if ``random == "uni"``
(default: 0). (default: 0).
...@@ -121,24 +125,10 @@ class random(object): ...@@ -121,24 +125,10 @@ class random(object):
return [key] return [key]
elif key == "gau": elif key == "gau":
"""
if("mean" in kwargs):
mean = domain.enforce_values(kwargs.get("mean"),extend=False)
else:
mean = None
if("dev" in kwargs):
dev = domain.enforce_values(kwargs.get("dev"),extend=False)
else:
dev = None
if("var" in kwargs):
var = domain.enforce_values(kwargs.get("var"),extend=False)
else:
var = None
"""
mean = kwargs.get('mean', None) mean = kwargs.get('mean', None)
dev = kwargs.get('dev', None) dev = kwargs.get('dev', None)
var = kwargs.get('var', None) var = kwargs.get('var', None)
return [key,mean,dev,var] return [key, mean, dev, var]
elif key == "syn": elif key == "syn":
pindex = kwargs.get('pindex', None) pindex = kwargs.get('pindex', None)
...@@ -150,101 +140,51 @@ class random(object): ...@@ -150,101 +140,51 @@ class random(object):
spec = kwargs.get('spec', 1) spec = kwargs.get('spec', 1)
codomain = kwargs.get('codomain', None) codomain = kwargs.get('codomain', None)
## check which domain should be taken for powerindexing # check which domain should be taken for powerindexing
if domain.check_codomain(codomain) == True and\ if domain.check_codomain(codomain) and codomain.harmonic:
codomain.fourier == True: harmonic_domain = codomain
fourier_domain = codomain elif domain.harmonic:
elif domain.fourier == True: harmonic_domain = domain
fourier_domain = domain
else: else:
fourier_domain = domain.get_codomain() harmonic_domain = domain.get_codomain()
## building kpack # building kpack
if pindex != None and kindex != None: if pindex is not None and kindex is not None:
pindex = distributed_data_object(pindex, pindex = distributed_data_object(pindex,
distribution_strategy='fftw') distribution_strategy='fftw')
kpack = [pindex, kindex] kpack = [pindex, kindex]
else: else:
kpack = None kpack = None
## simply put size and kindex into enforce_power # simply put size and kindex into enforce_power
## if one or both are None, enforce power will fix that # if one or both are None, enforce power will fix that
spec = fourier_domain.enforce_power(spec, size=size, kindex=kindex) spec = harmonic_domain.enforce_power(spec,
size=size,
kindex=kindex)
return [key, spec, kpack, fourier_domain, log, nbin, binbounds]
'''
## explicit power indices
if ("pindex" in kwargs) and ("kindex" in kwargs):
kindex = kwargs.get("kindex")
if(kindex is None):
spec = domain.enforce_power(kwargs.get("spec",1),size=kwargs.get("size",None))
kpack = None
else:
spec = domain.enforce_power(kwargs.get("spec",1),size=len(kindex),kindex=kindex)
pindex = kwargs.get("pindex",None)
if(pindex is None):
kpack = None
else:
kpack = [pindex,kindex]
## implicit power indices
else:
try:
domain.set_power_indices(**kwargs)
except:
codomain = kwargs.get("codomain",None)
if(codomain is None):
spec = domain.enforce_power(kwargs.get("spec",1),size=kwargs.get("size",None))
kpack = None
else:
domain.check_codomain(codomain)
codomain.set_power_indices(**kwargs)
kindex = codomain.power_indices.get("kindex")
spec = domain.enforce_power(kwargs.get("spec",1),size=len(kindex),kindex=kindex,codomain=codomain)
kpack = [codomain.power_indices.get("pindex"),kindex]
else:
kindex = domain.power_indices.get("kindex")
spec = domain.enforce_power(kwargs.get("spec",1),size=len(kindex),kindex=kindex)
kpack = [domain.power_indices.get("pindex"),kindex]
'''
return [key, spec, kpack, harmonic_domain, log, nbin, binbounds]
elif key == "uni": elif key == "uni":
""" vmin = domain.dtype(kwargs.get('vmin', 0))
if("vmin" in kwargs): vmax = domain.dtype(kwargs.get('vmax', 1))
vmin = domain.enforce_values(kwargs.get("vmin"),extend=False) return [key, vmin, vmax]
else:
vmin = 0
if("vmax" in kwargs):
vmax = domain.enforce_values(kwargs.get("vmax"),extend=False)
else:
vmax = 1
"""
vmin = domain.datatype(kwargs.get('vmin', 0))
vmax = domain.datatype(kwargs.get('vmax', 1))
return [key,vmin,vmax]
else: else:
raise KeyError(about._errors.cstring("ERROR: unsupported random key '"+str(key)+"'.")) raise KeyError(about._errors.cstring(
"ERROR: unsupported random key '" + str(key) + "'."))
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@staticmethod @staticmethod
def pm1(datatype=np.int, shape=1): def pm1(dtype=np.dtype('int'), shape=1):
""" """
Generates random field values according to an uniform distribution Generates random field values according to an uniform distribution
over {+1,-1} or {+1,+i,-1,-i}, respectively. over {+1,-1} or {+1,+i,-1,-i}, respectively.
Parameters Parameters
---------- ----------
datatype : type, *optional* dtype : type, *optional*
Data type of the field values (default: np.int). Data type of the field values (default: int).
shape : {integer, tuple, list, ndarray}, *optional* shape : {integer, tuple, list, ndarray}, *optional*
Split up dimension of the space (default: 1). Split up dimension of the space (default: 1).
...@@ -254,26 +194,29 @@ class random(object): ...@@ -254,26 +194,29 @@ class random(object):
Random field values (with correct dtype and shape). Random field values (with correct dtype and shape).
""" """
size = np.prod(shape,axis=0,dtype=np.int,out=None) size = np.prod(shape, axis=0, dtype=np.dtype('int'), out=None)
if(issubclass(datatype,np.complexfloating)): if(issubclass(dtype.type, np.complexfloating)):
x = np.array([1+0j,0+1j,-1+0j,0-1j],dtype=datatype)[np.random.randint(4,high=None,size=size)] x = np.array([1 + 0j, 0 + 1j, -1 + 0j, 0 - 1j],
dtype=dtype)[np.random.randint(4,
high=None,
size=size)]
else: else:
x = 2*np.random.randint(2,high=None,size=size)-1 x = 2 * np.random.randint(2, high=None, size=size) - 1
return x.astype(datatype).reshape(shape,order='C') return x.astype(dtype).reshape(shape, order='C')
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@staticmethod @staticmethod
def gau(datatype=np.float64, shape=1, mean=None, dev=None, var=None): def gau(dtype=np.dtype('float64'), shape=1, mean=None, dev=None, var=None):
""" """
Generates random field values according to a normal distribution. Generates random field values according to a normal distribution.
Parameters Parameters
---------- ----------
datatype : type, *optional* dtype : type, *optional*
Data type of the field values (default: np.float64). Data type of the field values (default: float64).
shape : {integer, tuple, list, ndarray}, *optional* shape : {integer, tuple, list, ndarray}, *optional*
Split up dimension of the space (default: 1). Split up dimension of the space (default: 1).
mean : {scalar, ndarray}, *optional* mean : {scalar, ndarray}, *optional*
...@@ -296,51 +239,57 @@ class random(object): ...@@ -296,51 +239,57 @@ class random(object):
`shape`. `shape`.
""" """
size = np.prod(shape,axis=0,dtype=np.int,out=None) size = np.prod(shape, axis=0, dtype=np.dtype('int'), out=None)
if(issubclass(datatype,np.complexfloating)): if(issubclass(dtype.type, np.complexfloating)):
x = np.empty(size,dtype=datatype,order='C') x = np.empty(size, dtype=dtype, order='C')
x.real = np.random.normal(loc=0,scale=np.sqrt(0.5),size=size) x.real = np.random.normal(loc=0, scale=np.sqrt(0.5), size=size)
x.imag = np.random.normal(loc=0,scale=np.sqrt(0.5),size=size) x.imag = np.random.normal(loc=0, scale=np.sqrt(0.5), size=size)
else: else:
x = np.random.normal(loc=0,scale=1,size=size) x = np.random.normal(loc=0, scale=1, size=size)
if(var is not None): if(var is not None):
if(np.size(var)==1): if(np.size(var) == 1):
x *= np.sqrt(np.abs(var)) x *= np.sqrt(np.abs(var))
elif(np.size(var)==size): elif(np.size(var) == size):
x *= np.sqrt(np.absolute(var).flatten(order='C')) x *= np.sqrt(np.absolute(var).flatten(order='C'))
else: else:
raise ValueError(about._errors.cstring("ERROR: dimension mismatch ( "+str(np.size(var))+" <> "+str(size)+" ).")) raise ValueError(about._errors.cstring(
"ERROR: dimension mismatch ( " + str(np.size(var)) +
" <> " + str(size) + " )."))
elif(dev is not None): elif(dev is not None):
if(np.size(dev)==1): if(np.size(dev) ==