Commit 6b859466 authored by M Selig's avatar M Selig
Browse files

Merge pull request #13 from mselig/develop

Develop to Master
parents b47f67fd 9da739a7
...@@ -12,8 +12,21 @@ ...@@ -12,8 +12,21 @@
.spyderproject .spyderproject
build build
rg/*
!rg/__init__.py
!rg/nifty_rg.py
!rg/nifty_power_conversion_rg.py
!rg/gfft_rg.py
!rg/powerspectrum.py
lm/*
!lm/__init__.py
!lm/nifty_lm.py
!lm/nifty_power_conversion_lm.py
demos/* demos/*
!demos/__init__.py !demos/__init__.py
!demos/demos_core.py
!demos/demo_faraday.py !demos/demo_faraday.py
!demos/demo_faraday_map.npy !demos/demo_faraday_map.npy
!demos/demo_excaliwir.py !demos/demo_excaliwir.py
......
...@@ -87,19 +87,21 @@ Requirements ...@@ -87,19 +87,21 @@ Requirements
(standard library) (standard library)
* `GFFT <https://github.com/mrbell/gfft>`_ (v0.1.0) - Generalized Fast * `GFFT <https://github.com/mrbell/gfft>`_ (v0.1.0) - Generalized Fast
Fourier Transformations for Python Fourier Transformations for Python - **optional**
* `HEALPy <https://github.com/healpy/healpy>`_ (v1.4 without openmp) - A * `HEALPy <https://github.com/healpy/healpy>`_ (v1.4 without openmp) - A
Python wrapper for `HEALPix <http://sourceforge.net/projects/healpix/>`_ Python wrapper for `HEALPix <http://sourceforge.net/projects/healpix/>`_ -
**optional**
* `libsharp-wrapper <https://github.com/mselig/libsharp-wrapper>`_ (v0.1.2 * `libsharp-wrapper <https://github.com/mselig/libsharp-wrapper>`_ (v0.1.2
without openmp) - A Python wrapper for the without openmp) - A Python wrapper for the
`libsharp <http://sourceforge.net/projects/libsharp/>`_ library `libsharp <http://sourceforge.net/projects/libsharp/>`_ library -
**optional**
Download Download
........ ........
The latest release is tagged **v0.9.0** and is available as a source package The latest release is tagged **v1.0.6** and is available as a source package
at `<https://github.com/mselig/nifty/tags>`_. The current version can be at `<https://github.com/mselig/nifty/tags>`_. The current version can be
obtained by cloning the repository:: obtained by cloning the repository::
...@@ -109,14 +111,25 @@ obtained by cloning the repository:: ...@@ -109,14 +111,25 @@ obtained by cloning the repository::
Installation Installation
............ ............
NIFTY is installed using Distutils by running the following command:: * NIFTY can be installed using `PyPI <https://pypi.python.org/pypi>`_ and
**pip** by running the following command::
python setup.py install pip install nifty
Alternatively, a private or user specific installation can be done by:: Alternatively, a private or user specific installation can be done by::
python setup.py install --user pip install --user nifty
python setup.py install --install-lib=/SOMEWHERE
* NIFTY can be installed using **Distutils** by running the following
command::
python setup.py install
Alternatively, a private or user specific installation can be done by::
python setup.py install --user
python setup.py install --install-lib=/SOMEWHERE
Acknowledgement Acknowledgement
--------------- ---------------
......
...@@ -20,43 +20,24 @@ ...@@ -20,43 +20,24 @@
## along with this program. If not, see <http://www.gnu.org/licenses/>. ## along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import division from __future__ import division
from nifty_core import * from nifty_core import * ## imports `about`
from nifty_cmaps import * from nifty_cmaps import *
from nifty_power import * from nifty_power import *
from nifty_tools import * from nifty_tools import *
from nifty_explicit import * from nifty_explicit import *
#from nifty_power_conversion import * ## optional submodule `rg`
try:
from rg import *
except(ImportError):
pass
## optional submodule `lm`
try:
from lm import *
except(ImportError):
pass
##----------------------------------------------------------------------------- from demos import *
from pickling import *
import copy_reg as cr
from types import MethodType as mt
def _pickle_method(method):
fct_name = method.im_func.__name__
obj = method.im_self
cl = method.im_class
## handle mangled function name
if(fct_name.startswith("__"))and(not fct_name.endswith("__")):
cl_name = cl.__name__.lstrip("_")
fct_name = "_" + cl_name + fct_name
return _unpickle_method, (fct_name, obj, cl)
def _unpickle_method(fct_name, obj, cl):
for oo in cl.__mro__:
try:
fct = oo.__dict__[fct_name]
except(KeyError):
pass
else:
break
return fct.__get__(obj, cl)
## enable instance methods pickling
cr.pickle(mt, _pickle_method, _unpickle_method)
##-----------------------------------------------------------------------------
\ No newline at end of file
...@@ -19,5 +19,5 @@ ...@@ -19,5 +19,5 @@
## 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/>.
pass from demos_core import *
...@@ -48,7 +48,7 @@ about.infos.off() ...@@ -48,7 +48,7 @@ about.infos.off()
##----------------------------------------------------------------------------- ##-----------------------------------------------------------------------------
# (global) Faraday map # (global) Faraday map
m = field(hp_space(128), val=np.load("demo_faraday_map.npy")) m = field(hp_space(128), val=np.load(os.path.join(get_demo_dir(),"demo_faraday_map.npy")))
##----------------------------------------------------------------------------- ##-----------------------------------------------------------------------------
......
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2015 Max-Planck-Society
##
## 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 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/>.
"""
NIFTY provides a number of demonstrations that you can run either using
``execfile`` provided with (absolute or relative) file names or using
``run -m`` providing the module name. You can retrieve the directory of
the NIFTY demos calling :py:func:`get_demo_dir`.
"""
import os
import nifty as nt
def get_demo_dir():
"""
Returns the path of the NIFTY demos directory.
"""
return os.path.split(nt.demos.__file__)[0]
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2015 Max-Planck-Society
##
## 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 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/>.
from __future__ import division
from nifty import about
#from nifty_lm import *
try:
import libsharp_wrapper_gl as gl
except(ImportError):
try:
import healpy as hp
except(ImportError):
about.infos.cprint("INFO: neither libsharp_wrapper_gl nor healpy available.")
pass ## import nothing
else:
from nifty_lm import lm_space,hp_space ## import lm & hp
## TODO: change about
else:
try:
import healpy as hp
except(ImportError):
from nifty_lm import lm_space,gl_space ## import lm & gl
else:
from nifty_lm import lm_space,gl_space,hp_space ## import all 3
from nifty_power_conversion_lm import *
This diff is collapsed.
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2014 Max-Planck-Society
##
## Author: Maksim Greiner, 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 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/>.
#from nifty import *
import numpy as np
from nifty import pi, \
about, \
field, \
sqrt,exp,log
def power_backward_conversion_lm(k_space,p,mean=None):
"""
This function is designed to convert a theoretical/statistical power
spectrum of a log-normal field to the theoretical power spectrum of
the underlying Gaussian field.
The function only works for power spectra defined for lm_spaces
Parameters
----------
k_space : nifty.rg_space,
a regular grid space with the attribute `Fourier = True`
p : np.array,
the power spectrum of the log-normal field.
Needs to have the same number of entries as
`k_space.get_power_indices()[0]`
mean : float, *optional*
specifies the mean of the log-normal field. If `mean` is not
specified the function will use the monopole of the power spectrum.
If it is specified the function will NOT use the monopole of the
spectrum. (default: None)
WARNING: a mean that is too low can violate positive definiteness
of the log-normal field. In this case the function produces an
error.
Returns
-------
mean : float,
the recovered mean of the underlying Gaussian distribution.
p1 : np.array,
the power spectrum of the underlying Gaussian field, where the
monopole has been set to zero. Eventual monopole power has been
shifted to the mean.
References
----------
.. [#] M. Greiner and T.A. Ensslin, "Log-transforming the matter power spectrum";
`arXiv:1312.1354 <http://arxiv.org/abs/1312.1354>`_
"""
p = np.copy(p)
if(mean is not None):
p[0] = 4*pi*mean**2
klen = k_space.get_power_indices()[0]
C_0_Omega = field(k_space,val=0)
C_0_Omega.val[:len(klen)] = p*sqrt(2*klen+1)/sqrt(4*pi)
C_0_Omega = C_0_Omega.transform()
if(np.any(C_0_Omega.val<0.)):
raise ValueError(about._errors.cstring("ERROR: spectrum or mean incompatible with positive definiteness.\n Try increasing the mean."))
return None
lC = log(C_0_Omega)
Z = lC.transform()
spec = Z.val[:len(klen)]
mean = (spec[0]-0.5*sqrt(4*pi)*log((p*(2*klen+1)/(4*pi)).sum()))/sqrt(4*pi)
spec[0] = 0.
spec = spec*sqrt(4*pi)/sqrt(2*klen+1)
spec = np.real(spec)
if(np.any(spec<0.)):
spec = spec*(spec>0.)
about.warnings.cprint("WARNING: negative modes set to zero.")
return mean.real,spec
def power_forward_conversion_lm(k_space,p,mean=0):
"""
This function is designed to convert a theoretical/statistical power
spectrum of a Gaussian field to the theoretical power spectrum of
the exponentiated field.
The function only works for power spectra defined for lm_spaces
Parameters
----------
k_space : nifty.rg_space,
a regular grid space with the attribute `Fourier = True`
p : np.array,
the power spectrum of the Gaussian field.
Needs to have the same number of entries as
`k_space.get_power_indices()[0]`
m : float, *optional*
specifies the mean of the Gaussian field (default: 0).
Returns
-------
p1 : np.array,
the power spectrum of the exponentiated Gaussian field.
References
----------
.. [#] M. Greiner and T.A. Ensslin, "Log-transforming the matter power spectrum";
`arXiv:1312.1354 <http://arxiv.org/abs/1312.1354>`_
"""
m = mean
klen = k_space.get_power_indices()[0]
C_0_Omega = field(k_space,val=0)
C_0_Omega.val[:len(klen)] = p*sqrt(2*klen+1)/sqrt(4*pi)
C_0_Omega = C_0_Omega.transform()
C_0_0 = (p*(2*klen+1)/(4*pi)).sum()
exC = exp(C_0_Omega+C_0_0+2*m)
Z = exC.transform()
spec = Z.val[:len(klen)]
spec = spec*sqrt(4*pi)/sqrt(2*klen+1)
spec = np.real(spec)
if(np.any(spec<0.)):
spec = spec*(spec>0.)
about.warnings.cprint("WARNING: negative modes set to zero.")
return spec
\ No newline at end of file
This diff is collapsed.
...@@ -38,20 +38,20 @@ ...@@ -38,20 +38,20 @@
""" """
from __future__ import division from __future__ import division
from nifty_core import * #from nifty_core import *
#from sys import stdout as so from sys import stdout as so
#import numpy as np import numpy as np
#import pylab as pl import pylab as pl
#from matplotlib.colors import LogNorm as ln from matplotlib.colors import LogNorm as ln
#from matplotlib.ticker import LogFormatter as lf from matplotlib.ticker import LogFormatter as lf
#from multiprocessing import Pool as mp from multiprocessing import Pool as mp
#from multiprocessing import Value as mv from multiprocessing import Value as mv
#from multiprocessing import Array as ma from multiprocessing import Array as ma
#from nifty_core import about, \ from nifty_core import about, \
# space, \ space, \
# field, \ field, \
# operator,diagonal_operator,identity,vecvec_operator, \ operator,diagonal_operator,identity,vecvec_operator, \
# probing probing
##----------------------------------------------------------------------------- ##-----------------------------------------------------------------------------
...@@ -691,7 +691,7 @@ class explicit_operator(operator): ...@@ -691,7 +691,7 @@ class explicit_operator(operator):
If it is no square matrix. If it is no square matrix.
""" """
return self._inverse_adjoint_times(x,**kwargs) return self.inverse_adjoint_times(x,**kwargs)
def inverse_adjoint_times(self,x,**kwargs): def inverse_adjoint_times(self,x,**kwargs):
""" """
......
...@@ -42,12 +42,12 @@ ...@@ -42,12 +42,12 @@
""" """
from __future__ import division from __future__ import division
from scipy.interpolate import interp1d as ip ## FIXME: conflicts with sphinx's autodoc from scipy.interpolate import interp1d as ip ## FIXME: conflicts with sphinx's autodoc
from nifty_core import * #from nifty_core import *
#import numpy as np import numpy as np
#from nifty_core import about, \ from nifty_core import about, \
# space, \ space, \
# field, \ field, \
# projection_operator projection_operator
import smoothing as gs import smoothing as gs
......
...@@ -37,12 +37,12 @@ ...@@ -37,12 +37,12 @@
""" """
from __future__ import division from __future__ import division
from nifty_core import * #from nifty_core import *
#import numpy as np import numpy as np
#from nifty_core import notification,about, \ from nifty_core import notification,about, \
# space, \ space, \
# field, \ field, \
# operator,diagonal_operator operator,diagonal_operator
##----------------------------------------------------------------------------- ##-----------------------------------------------------------------------------
...@@ -1006,6 +1006,8 @@ class steepest_descent(object): ...@@ -1006,6 +1006,8 @@ class steepest_descent(object):
self.c = c ## 0 < c1 < c2 < 1 self.c = c ## 0 < c1 < c2 < 1
self.note = notification(default=bool(note)) self.note = notification(default=bool(note))
self._alpha = None ## last alpha
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def __call__(self,x0,alpha=1,tol=1E-4,clevel=8,limii=100000): def __call__(self,x0,alpha=1,tol=1E-4,clevel=8,limii=100000):
...@@ -1041,6 +1043,13 @@ class steepest_descent(object): ...@@ -1041,6 +1043,13 @@ class steepest_descent(object):
raise TypeError(about._errors.cstring("ERROR: invalid input.")) raise TypeError(about._errors.cstring("ERROR: invalid input."))
self.x = x0 self.x = x0
## check for exsisting alpha
if(alpha is None):
if(self._alpha is not None):
alpha = self._alpha
else:
alpha = 1
clevel = max(1,int(clevel)) clevel = max(1,int(clevel))
limii = int(limii) limii = int(limii)
...@@ -1091,6 +1100,10 @@ class steepest_descent(object): ...@@ -1091,6 +1100,10 @@ class steepest_descent(object):
if(self.spam is not None): if(self.spam is not None):
self.spam(self.x,ii) self.spam(self.x,ii)
## memorise last alpha
if(alpha is not None):
self._alpha = alpha/a ## undo update
return self.x,convergence return self.x,convergence
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
......
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2013 Max-Planck-Society
##
## 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 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/>.
import copy_reg as cr
from types import MethodType as mt
##-----------------------------------------------------------------------------
def _pickle_method(method):
fct_name = method.im_func.__name__
obj = method.im_self
cl = method.im_class
## handle mangled function name
if(fct_name.startswith("__"))and(not fct_name.endswith("__")):
cl_name = cl.__name__.lstrip("_")
fct_name = "_" + cl_name + fct_name
return _unpickle_method, (fct_name, obj, cl)
##-----------------------------------------------------------------------------
def _unpickle_method(fct_name, obj, cl):
for oo in cl.__mro__:
try:
fct = oo.__dict__[fct_name]
except(KeyError):
pass
else:
break
return fct.__get__(obj, cl)