Scheduled maintenance on Monday 2019-06-24 between 10:00-11:00 CEST

Commit 4cc0f412 authored by M Selig's avatar M Selig

Merge pull request #9 from mselig/develop

version update
parents d4d18b1c 40a174f8
......@@ -99,7 +99,7 @@ Requirements
Download
........
The latest release is tagged **v0.7.0** and is available as a source package
The latest release is tagged **v0.8.0** and is available as a source package
at `<https://github.com/mselig/nifty/tags>`_. The current version can be
obtained by cloning the repository::
......
This diff is collapsed.
......@@ -47,24 +47,11 @@ about.infos.off()
##-----------------------------------------------------------------------------
## spaces
h = hp_space(128)
l = lm_space(383)
g = gl_space(384) ## nlon == 767
g_ = gl_space(384, nlon=768)
r = rg_space([768, 384], dist=[1/360, 1/180])
r_ = rg_space([256, 128], dist=[1/120, 1/60])
## map
m = field(h, val=np.load("demo_faraday_map.npy"))
## projection operator
Sk = None
# (global) Faraday map
m = field(hp_space(128), val=np.load("demo_faraday_map.npy"))
##-----------------------------------------------------------------------------
##=============================================================================
def run(projection=False, power=False):
......@@ -74,47 +61,71 @@ def run(projection=False, power=False):
Parameters
----------
projection : bool, *optional*
Whether to additionaly include projections in the demo or not. If
``projection == True`` the projection operator `Sk` will be
defined. (default: False)
Whether to additionaly show projections or not (default: False).
power : bool, *optional*
Whether to additionaly show power spectra in the demo or not.
(default: False)
Whether to additionaly show power spectra or not (default: False).
"""
global Sk
## start in hp_space
nicely = {"vmin":-4, "vmax":4, "cmap":ncmap.fm()}
# (a) representation on HEALPix grid
m0 = m
m0.plot(title=r"$m$ on a HEALPix grid", **nicely)
nicely.update({"cmap":ncmap.fm()}) # healpy bug workaround
## transform to lm_space
m1 = m0.transform(l)
# (b) representation in spherical harmonics
k_space = m0.target # == lm_space(383, mmax=383)
m1 = m0.transform(k_space) # == m.transform()
# m1.plot(title=r"$m$ in spherical harmonics")
if(power):
m1.plot(title=r"angular power spectrum of $m$", vmin=1E-2, vmax=1E+1, mono=False)
if(projection):
## define projection operator
Sk = projection_operator(l)
## project quadrupole
# define projection operator
Sk = projection_operator(m1.domain)
# project quadrupole
m2 = Sk(m0, band=2)
m2.plot(title=r"angular quadrupole of $m$ on a HEALPix grid", **nicely)
## transform to gl_space
m3 = m1.transform(g)
# (c) representation on Gauss-Legendre grid
y_space = m.target.get_codomain(coname="gl") # == gl_space(384, nlon=767)
m3 = m1.transform(y_space) # == m0.transform().transform(y_space)
m3.plot(title=r"$m$ on a spherical Gauss-Legendre grid", **nicely)
## transform to rg_space
m4 = m1.transform(g_) ## auxiliary gl_space
m4.cast_domain(r) ## rg_space cast
m4.set_val(np.roll(m4.val[::-1, ::-1], g.nlon()//2, axis=1)) ## rearrange
if(power):
## restrict to central window
m5 = field(r_, val=m4[128:256, 256:512]).transform()
if(projection):
m4 = Sk(m3, band=2)
m4.plot(title=r"angular quadrupole of $m$ on a Gauss-Legendre grid", **nicely)
# (d) representation on regular grid
y_space = gl_space(384, nlon=768) # auxiliary gl_space
z_space = rg_space([768, 384], dist=[1/360, 1/180])
m5 = m1.transform(y_space)
m5.cast_domain(z_space)
m5.set_val(np.roll(m5.val[::-1, ::-1], y_space.nlon()//2, axis=1)) # rearrange value array
m5.plot(title=r"$m$ on a regular 2D grid", **nicely)
## plots
m0.plot(title=r"$m$ on a HEALPix grid", vmin=-4, vmax=4, cmap=ncmap.fm())
if(power):
m1.plot(title=r"angular power spectrum of $m$", vmin=1E-2, vmax=1E+1, mono=False)
m5.target.set_power_indices(log=False)
m5.plot(power=True, title=r"Fourier power spectrum of $m$", vmin=1E-3, vmax=1E+0, mono=False)
if(projection):
m2.plot(title=r"quadrupole of $m$ on a HEALPix grid", vmin=-4, vmax=4, cmap=ncmap.fm())
m3.plot(title=r"$m$ on a spherical Gauss-Legendre grid", vmin=-4, vmax=4, cmap=ncmap.fm())
m4.plot(title=r"$m$ on a regular 2D grid", vmin=-4, vmax=4, cmap=ncmap.fm())
if(power):
m5.plot(title=r"(restricted, binned) Fourier power spectrum of $m$", vmin=1E-3, vmax=1E+0, mono=False, log=True)
m5.target.set_power_indices(log=False)
# define projection operator
Sk = projection_operator(m5.target)
# project quadrupole
m6 = Sk(m5, band=2)
m6.plot(title=r"Fourier quadrupole of $m$ on a regular 2D grid", **nicely)
##=============================================================================
##-----------------------------------------------------------------------------
if(__name__=="__main__"):
# pl.close("all")
# run demo
run(projection=False, power=False)
# define projection operator
Sk = projection_operator(m.target)
##-----------------------------------------------------------------------------
......@@ -32,36 +32,39 @@
"""
from __future__ import division
from nifty import * # version 0.6.0
from nifty import * # version 0.8.0
# some signal space; e.g., a two-dimensional regular grid
x_space = rg_space([256, 256]) # define signal space
x_space = rg_space([128, 128]) # define signal space
#x_space = rg_space(512)
#x_space = hp_space(32)
#x_space = gl_space(96)
k_space = x_space.get_codomain() # get conjugate space
k_space = x_space.get_codomain() # get conjugate space
# some power spectrum
power = (lambda k: 42 / (k + 1) ** 3)
S = power_operator(k_space, spec=power) # define signal covariance
s = S.get_random_field(domain=x_space) # generate signal
S = power_operator(k_space, spec=power) # define signal covariance
s = S.get_random_field(domain=x_space) # generate signal
R = response_operator(x_space, sigma=0.0, mask=1.0, assign=None) # define response
d_space = R.target # get data space
R = response_operator(x_space, sigma=0.0, mask=1.0, assign=None) # define response
d_space = R.target # get data space
# some noise variance; e.g., 100
N = diagonal_operator(d_space, diag=100, bare=True) # define noise covariance
n = N.get_random_field(domain=d_space) # generate noise
# some noise variance; e.g., signal-to-noise ratio of 1
N = diagonal_operator(d_space, diag=s.var(), bare=True) # define noise covariance
n = N.get_random_field(domain=d_space) # generate noise
d = R(s) + n # compute data
d = R(s) + n # compute data
j = R.adjoint_times(N.inverse_times(d)) # define information source
D = propagator_operator(S=S, N=N, R=R) # define information propagator
j = R.adjoint_times(N.inverse_times(d)) # define information source
D = propagator_operator(S=S, N=N, R=R) # define information propagator
m = D(j, tol=1E-4, note=True) # reconstruct map
m = D(j, W=S, tol=1E-3, note=True) # reconstruct map
s.plot(title="signal") # plot signal
s.plot(title="signal") # plot signal
d_ = field(x_space, val=d.val, target=k_space)
d_.plot(title="data", vmin=s.val.min(), vmax=s.val.max()) # plot data
m.plot(title="reconstructed map", vmin=s.val.min(), vmax=s.val.max()) # plot map
d_.plot(title="data", vmin=s.min(), vmax=s.max()) # plot data
m.plot(title="reconstructed map", vmin=s.min(), vmax=s.max()) # plot map
......@@ -32,31 +32,31 @@
"""
from __future__ import division
from nifty import * # version 0.6.0
from nifty import * # version 0.8.0
# some signal space; e.g., a two-dimensional regular grid
x_space = rg_space([256, 256]) # define signal space
x_space = rg_space([128, 128]) # define signal space
k_space = x_space.get_codomain() # get conjugate space
k_space = x_space.get_codomain() # get conjugate space
# some power spectrum
power = (lambda k: 42 / (k + 1) ** 3)
S = power_operator(k_space, spec=power) # define signal covariance
s = S.get_random_field(domain=x_space) # generate signal
S = power_operator(k_space, spec=power) # define signal covariance
s = S.get_random_field(domain=x_space) # generate signal
R = response_operator(x_space, sigma=0.0, mask=1.0, assign=None) # define response
d_space = R.target # get data space
R = response_operator(x_space, sigma=0.0, mask=1.0, assign=None) # define response
d_space = R.target # get data space
# some noise variance; e.g., 100
N = diagonal_operator(d_space, diag=100, bare=True) # define noise covariance
n = N.get_random_field(domain=d_space) # generate noise
# some noise variance; e.g., signal-to-noise ratio of 1
N = diagonal_operator(d_space, diag=s.var(), bare=True) # define noise covariance
n = N.get_random_field(domain=d_space) # generate noise
d = R(s) + n # compute data
d = R(s) + n # compute data
j = R.adjoint_times(N.inverse_times(d)) # define information source
D = propagator_operator(S=S, N=N, R=R) # define information propagator
j = R.adjoint_times(N.inverse_times(d)) # define information source
D = propagator_operator(S=S, N=N, R=R) # define information propagator
def eggs(x):
......@@ -65,16 +65,16 @@ def eggs(x):
"""
DIx = D.inverse_times(x)
H = 0.5 * DIx.dot(x) - j.dot(x) # compute information Hamiltonian
g = DIx - j # compute its gradient
H = 0.5 * DIx.dot(x) - j.dot(x) # compute information Hamiltonian
g = DIx - j # compute its gradient
return H, g
m = field(x_space, target=k_space) # reconstruct map
m,convergence = steepest_descent(eggs=eggs, note=True)(m, tol=1E-4, clevel=3)
m = field(x_space, target=k_space) # reconstruct map
m, convergence = steepest_descent(eggs=eggs, note=True)(m, tol=1E-3, clevel=3)
s.plot(title="signal") # plot signal
s.plot(title="signal") # plot signal
d_ = field(x_space, val=d.val, target=k_space)
d_.plot(title="data", vmin=s.val.min(), vmax=s.val.max()) # plot data
m.plot(title="reconstructed map", vmin=s.val.min(), vmax=s.val.max()) # plot map
d_.plot(title="data", vmin=s.min(), vmax=s.max()) # plot data
m.plot(title="reconstructed map", vmin=s.min(), vmax=s.max()) # plot map
......@@ -33,7 +33,6 @@
"""
from __future__ import division
from nifty import * # version 0.7.0
from nifty.nifty_explicit import *
# some signal space; e.g., a one-dimensional regular grid
......
This diff is collapsed.
This diff is collapsed.
......@@ -42,8 +42,12 @@
"""
from __future__ import division
from scipy.interpolate import interp1d as ip ## FIXME: conflicts with sphinx's autodoc
#import numpy as np
from nifty_core import *
#import numpy as np
#from nifty_core import about, \
# space, \
# field, \
# projection_operator
import smoothing as gs
......@@ -210,16 +214,16 @@ def smooth_power(spec,domain=None,kindex=None,mode="2s",exclude=1,sigma=-1,**kwa
## check power spectrum
if(isinstance(spec,field)):
spec = spec.val.astype(kindex.dtype)
spec = spec.val
elif(callable(spec)):
try:
spec = np.array(spec(kindex),dtype=kindex.dtype)
spec = np.array(spec(kindex),dtype=None)
except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)):
spec = np.array([spec],dtype=kindex.dtype)
spec = np.array([spec],dtype=None)
else:
spec = np.array(spec,dtype=kindex.dtype)
spec = np.array(spec,dtype=None)
## drop imaginary part
spec = np.real(spec)
## check finiteness and positivity (excluding null)
......@@ -663,13 +667,13 @@ def interpolate_power(spec,mode="linear",domain=None,kindex=None,newkindex=None,
spec = spec.val.astype(kindex.dtype)
elif(callable(spec)):
try:
spec = np.array(spec(kindex),dtype=kindex.dtype)
spec = np.array(spec(kindex),dtype=None)
except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)):
spec = np.array([spec],dtype=kindex.dtype)
spec = np.array([spec],dtype=None)
else:
spec = np.array(spec,dtype=kindex.dtype)
spec = np.array(spec,dtype=None)
## drop imaginary part
spec = np.real(spec)
## check finiteness and positivity (excluding null)
......
......@@ -37,8 +37,12 @@
"""
from __future__ import division
#import numpy as np
from nifty_core import *
#import numpy as np
#from nifty_core import notification,about, \
# space, \
# field, \
# operator,diagonal_operator
##-----------------------------------------------------------------------------
......@@ -1035,11 +1039,14 @@ class steepest_descent(object):
raise TypeError(about._errors.cstring("ERROR: invalid input."))
self.x = x0
clevel = int(clevel)
clevel = max(1,int(clevel))
limii = int(limii)
E,g = self.eggs(self.x) ## energy and gradient
norm = g.norm() ## gradient norm
if(norm==0):
self.note.cprint("\niteration : 00000000 alpha = 0.0E+00 delta = 0.0E+00\n... done.")
return self.x,clevel+2
convergence = 0
ii = 1
......@@ -1060,6 +1067,10 @@ class steepest_descent(object):
if(ii==limii):
self.note.cprint("\n... quit.")
break
elif(delta==0):
convergence = clevel+2
self.note.cprint(" convergence level : %u\n... done."%convergence)
break
elif(delta<tol):
convergence += 1
self.note.cflush(" convergence level : %u"%convergence)
......
......@@ -23,7 +23,7 @@ from distutils.core import setup
import os
setup(name="nifty",
version="0.7.0",
version="0.8.0",
description="Numerical Information Field Theory",
author="Marco Selig",
author_email="mselig@mpa-garching.mpg.de",
......
......@@ -23,8 +23,8 @@
## TODO: doc strings
from __future__ import division
#import gfft as gf
import numpy as np
#import gfft as gf
def smooth_power(power, k, exclude=1, smooth_length=None):
......
Markdown is supported
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