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,\
from nifty_cmaps import ncmap
from nifty_core import space,\
point_space,\
nested_space,\
field
from nifty_mpi_data import distributed_data_object, d2o_librarian
......
......@@ -16,7 +16,11 @@ MAX = np.max
SUM = np.sum
class _COMM_WORLD():
class Comm(object):
pass
class Intracomm(Comm):
def __init__(self):
self.rank = 0
self.size = 1
......@@ -111,4 +115,4 @@ COMPLEX = _datatype("MPI_COMPLEX")
DOUBLE_COMPLEX = _datatype("MPI_DOUBLE_COMPLEX")
COMM_WORLD = _COMM_WORLD()
COMM_WORLD = Intracomm()
# -*- coding: utf-8 -*-
import ConfigParser
import pprint
class variable(object):
def __init__(self, name, default_value_list, checker, genus='str'):
......@@ -47,7 +49,7 @@ class variable(object):
return self.name
def __repr__(self):
return "<nifty variable '" + str(self.name) + "': " + \
return "<" + str(self.name) + "': " + \
str(self.get_value()) + ">"
......@@ -161,7 +163,9 @@ class configuration(object):
item.set_value(temp_value)
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
from nifty_configuration import variable,\
configuration
global_dependency_injector = dependency_injector(
['h5py',
('mpi4py.MPI', 'MPI'),
......@@ -23,12 +26,12 @@ variable_fft_module = variable('fft_module',
variable_lm2gl = variable('lm2gl',
[True, False],
lambda z: z is True or z is False,
'boolean')
genus = 'boolean')
variable_verbosity = variable('verbosity',
[1],
lambda z: z == abs(int(z)),
'int')
genus = 'int')
variable_mpi_module = variable('mpi_module',
['MPI', 'MPI_dummy'],
......@@ -49,3 +52,12 @@ global_configuration = configuration(
variable_default_distribution_strategy
],
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 @@
#from nifty import *
import numpy as np
from numpy import pi
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
......
This diff is collapsed.
......@@ -32,30 +32,6 @@ MPI = gdi[gc['mpi_module']]
h5py = gdi.get('h5py')
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 []
STRATEGIES = {
'all': ['not', 'equal', 'freeform'] + _maybe_fftw,
......@@ -687,7 +663,7 @@ class distributed_data_object(object):
if weights is not None:
local_weights = self.distributor.extract_local_data(weights).\
flatten()
flatten()
else:
local_weights = None
......
......@@ -56,7 +56,7 @@ class point_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())
space_paradict.__init__(
self, num=num, complexity=complexity, zerocenter=zerocenter)
......@@ -100,7 +100,7 @@ class nested_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)
if mmax is None:
mmax = -1
......@@ -145,7 +145,7 @@ class lm_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)
if nlon is None:
nlon = -1
......
This diff is collapsed.
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
# NIFTY (Numerical Information Field Theory) has been developed at the
# Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2013 Max-Planck-Society
# Copyright (C) 2013 Max-Planck-Society
##
## Author: Marco Selig
## Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
# Author: Marco Selig
# Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
##
## 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
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
# 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
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
## See the GNU General Public License for more details.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
from keepers import about
from nifty_mpi_data import distributed_data_object
##-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
class random(object):
"""
......@@ -39,10 +40,10 @@ class random(object):
"""
__init__ = None
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@staticmethod
def parse_arguments(domain,**kwargs):
def parse_arguments(domain, **kwargs):
"""
Analyses the keyword arguments for supported or necessary ones.
......@@ -91,18 +92,21 @@ class random(object):
codomain : nifty.space, *optional*
A compatible codomain for power indexing (default: None).
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
automatically (if not given otherwise); by default no binning
is done (default: None).
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;
by default no binning is done (default: None).
binbounds : {list, array}, *optional*
User specific inner boundaries of the bins, which are preferred
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"``
(default: 0).
......@@ -121,24 +125,10 @@ class random(object):
return [key]
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)
dev = kwargs.get('dev', None)
var = kwargs.get('var', None)
return [key,mean,dev,var]
return [key, mean, dev, var]
elif key == "syn":
pindex = kwargs.get('pindex', None)
......@@ -150,101 +140,51 @@ class random(object):
spec = kwargs.get('spec', 1)
codomain = kwargs.get('codomain', None)
## check which domain should be taken for powerindexing
if domain.check_codomain(codomain) == True and\
codomain.fourier == True:
fourier_domain = codomain
elif domain.fourier == True:
fourier_domain = domain
# check which domain should be taken for powerindexing
if domain.check_codomain(codomain) and codomain.harmonic:
harmonic_domain = codomain
elif domain.harmonic:
harmonic_domain = domain
else:
fourier_domain = domain.get_codomain()
harmonic_domain = domain.get_codomain()
## building kpack
if pindex != None and kindex != None:
# building kpack
if pindex is not None and kindex is not None:
pindex = distributed_data_object(pindex,
distribution_strategy='fftw')
kpack = [pindex, kindex]
else:
kpack = None
## simply put size and kindex into enforce_power
## if one or both are None, enforce power will fix that
spec = fourier_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]
'''
# simply put size and kindex into enforce_power
# if one or both are None, enforce power will fix that
spec = harmonic_domain.enforce_power(spec,
size=size,
kindex=kindex)
return [key, spec, kpack, harmonic_domain, log, nbin, binbounds]
elif key == "uni":
"""
if("vmin" in kwargs):
vmin = domain.enforce_values(kwargs.get("vmin"),extend=False)
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]
vmin = domain.dtype(kwargs.get('vmin', 0))
vmax = domain.dtype(kwargs.get('vmax', 1))
return [key, vmin, vmax]
else:
raise KeyError(about._errors.cstring("ERROR: unsupported random key '"+str(key)+"'."))
raise KeyError(about._errors.cstring(
"ERROR: unsupported random key '" + str(key) + "'."))
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@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
over {+1,-1} or {+1,+i,-1,-i}, respectively.
Parameters
----------
datatype : type, *optional*
Data type of the field values (default: np.int).
dtype : type, *optional*
Data type of the field values (default: int).
shape : {integer, tuple, list, ndarray}, *optional*
Split up dimension of the space (default: 1).
......@@ -254,26 +194,29 @@ class random(object):
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)):
x = np.array([1+0j,0+1j,-1+0j,0-1j],dtype=datatype)[np.random.randint(4,high=None,size=size)]
if(issubclass(dtype.type, np.complexfloating)):
x = np.array([1 + 0j, 0 + 1j, -1 + 0j, 0 - 1j],
dtype=dtype)[np.random.randint(4,
high=None,
size=size)]
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
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.
Parameters
----------
datatype : type, *optional*
Data type of the field values (default: np.float64).
dtype : type, *optional*
Data type of the field values (default: float64).
shape : {integer, tuple, list, ndarray}, *optional*
Split up dimension of the space (default: 1).
mean : {scalar, ndarray}, *optional*
......@@ -296,51 +239,57 @@ class random(object):
`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)):
x = np.empty(size,dtype=datatype,order='C')
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)
if(issubclass(dtype.type, np.complexfloating)):
x = np.empty(size, dtype=dtype, order='C')
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)
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(np.size(var)==1):
if(np.size(var) == 1):
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'))
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):
if(np.size(dev)==1):
if(np.size(dev) == 1):
x *= np.abs(dev)
elif(np.size(dev)==size):
elif(np.size(dev) == size):
x *= np.absolute(dev).flatten(order='C')
else:
raise ValueError(about._errors.cstring("ERROR: dimension mismatch ( "+str(np.size(dev))+" <> "+str(size)+" )."))
raise ValueError(about._errors.cstring(
"ERROR: dimension mismatch ( " + str(np.size(dev)) +
" <> " + str(size) + " )."))
if(mean is not None):
if(np.size(mean)==1):
if(np.size(mean) == 1):
x += mean
elif(np.size(mean)==size):
elif(np.size(mean) == size):
x += np.array(mean).flatten(order='C')
else:
raise ValueError(about._errors.cstring("ERROR: dimension mismatch ( "+str(np.size(mean))+" <> "+str(size)+" )."))
raise ValueError(about._errors.cstring(
"ERROR: dimension mismatch ( " + str(np.size(mean)) +
" <> " + str(size) + " )."))
return x.astype(datatype).reshape(shape,order='C')
return x.astype(dtype).reshape(shape, order='C')
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@staticmethod
def uni(datatype=np.float64,shape=1,vmin=0,vmax=1):
def uni(dtype=np.dtype('float64'), shape=1, vmin=0, vmax=1):
"""
Generates random field values according to an uniform distribution
over [vmin,vmax[.
Parameters
----------
datatype : type, *optional*
Data type of the field values (default: np.float64).
dtype : type, *optional*
Data type of the field values (default: float64).
shape : {integer, tuple, list, ndarray}, *optional*
Split up dimension of the space (default: 1).
......@@ -355,27 +304,28 @@ class random(object):
Random field values (with correct dtype and shape).
"""
size = np.prod(shape,axis=0,dtype=np.int,out=None)
if(np.size(vmin)>1):
size = np.prod(shape, axis=0, dtype=np.dtype('int'), out=None)
if(np.size(vmin) > 1):
vmin = np.array(vmin).flatten(order='C')
if(np.size(vmax)>1):
if(np.size(vmax) > 1):
vmax = np.array(vmax).flatten(order='C')
if(datatype in [np.complex64,np.complex128]):
x = np.empty(size,dtype=datatype,order='C')
x.real = (vmax-vmin)*np.random.random(size=size)+vmin
x.imag = (vmax-vmin)*np.random.random(size=size)+vmin
elif(datatype in [np.int8,np.int16,np.int32,np.int64]):
x = np.random.randint(min(vmin,vmax),high=max(vmin,vmax),size=size)
if(dtype in [np.dtype('complex64'), np.dtype('complex128')]):
x = np.empty(size, dtype=dtype, order='C')
x.real = (vmax - vmin) * np.random.random(size=size) + vmin
x.imag = (vmax - vmin) * np.random.random(size=size) + vmin
elif(dtype in [np.dtype('int8'), np.dtype('int16'), np.dtype('int32'),
np.dtype('int64')]):
x = np.random.randint(
min(vmin, vmax), high=max(vmin, vmax), size=size)
else:
x = (vmax-vmin)*np.random.random(size=size)+vmin
x = (vmax - vmin) * np.random.random(size=size) + vmin
return x.astype(datatype).reshape(shape,order='C')
return x.astype(dtype).reshape(shape, order='C')
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def __repr__(self):
return "<nifty_core.random>"
##-----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
......@@ -253,12 +253,12 @@ class explicit_operator(operator):
# about.infos.cprint("INFO: matrix size > 2 ** 20.")
self.target = target
# check datatype
# check dtype
if(np.any(np.iscomplex(val))):
datatype, purelyreal = max(min(val.dtype, self.domain.datatype), min(
val.dtype, self.target.datatype)), False
dtype, purelyreal = max(min(val.dtype, self.domain.dtype), min(
val.dtype, self.target.dtype)), False
else:
datatype, purelyreal = max(min(val.dtype, self.domain.vol.dtype), min(
dtype, purelyreal = max(min(val.dtype, self.domain.vol.dtype), min(
val.dtype, self.target.vol.dtype)), True
# weight if ... (given `domain` and `target`)
if(isinstance(bare, tuple)):
......@@ -271,9 +271,9 @@ class explicit_operator(operator):
elif(not bare):
val = self._calc_weight_rows(val, -1)
if(purelyreal):
self.val = np.real(val).astype(datatype)
self.val = np.real(val).astype(dtype)
else:
self.val = val.astype(datatype)
self.val = val.astype(dtype)
# check hidden degrees of freedom
self._hidden = np.array([self.domain.dim(split=False) < self.domain.dof(
......@@ -455,12 +455,12 @@ class explicit_operator(operator):
raise ValueError(about._errors.cstring("ERROR: dimension mismatch ( " + str(np.size(
newmatrix, axis=None)) + " <> " + str(self.nrow()) + " x " + str(self.ncol()) + " )."))
# check datatype
# check dtype
if(np.any(np.iscomplex(val))):
datatype, purelyreal = max(min(val.dtype, self.domain.datatype), min(
val.dtype, self.target.datatype)), False