Commit 2f6d4a97 authored by Marco Selig's avatar Marco Selig

bugfix in powerspectrum.draw_nd; callable spec.

parent 1482483d
...@@ -2383,28 +2383,14 @@ class rg_space(space): ...@@ -2383,28 +2383,14 @@ class rg_space(space):
(default: 0). (default: 0).
""" """
if(isinstance(spec,field)):
spec = spec.val.astype(self.datatype)
elif(np.isscalar(spec)):
spec = np.array([spec],dtype=self.datatype)
else:
spec = np.array(spec,dtype=self.datatype)
## check finiteness
if(not np.all(np.isfinite(spec))):
about.warnings.cprint("WARNING: infinite value(s).")
## check positivity (excluding null)
if(np.any(spec<0)):
raise ValueError(about._errors.cstring("ERROR: nonpositive value(s)."))
elif(np.any(spec==0)):
about.warnings.cprint("WARNING: nonpositive value(s).")
if(size is None): if(size is None)or(callable(spec)):
## explicit kindex ## explicit kindex
if("kindex" in kwargs): if("kindex" in kwargs):
size = len(kwargs.get("kindex")) kindex = kwargs.get("kindex")
## quick kindex ## quick kindex
elif(self.fourier)and(not hasattr(self,"power_indices"))and(len(kwargs)==0): elif(self.fourier)and(not hasattr(self,"power_indices"))and(len(kwargs)==0):
size = gp.nklength(gp.nkdict(self.para[:(np.size(self.para)-1)//2],self.vol,fourier=True)) kindex = gp.nklength(gp.nkdict(self.para[:(np.size(self.para)-1)//2],self.vol,fourier=True))
## implicit kindex ## implicit kindex
else: else:
try: try:
...@@ -2412,11 +2398,33 @@ class rg_space(space): ...@@ -2412,11 +2398,33 @@ class rg_space(space):
except: except:
codomain = kwargs.get("codomain",self.get_codomain()) codomain = kwargs.get("codomain",self.get_codomain())
codomain.set_power_indices(**kwargs) codomain.set_power_indices(**kwargs)
size = len(codomain.power_indices.get("kindex")) kindex = codomain.power_indices.get("kindex")
else:
kindex = self.power_indices.get("kindex")
size = len(kindex)
if(isinstance(spec,field)):
spec = spec.val.astype(self.datatype)
elif(callable(spec)):
try:
spec = np.array(spec(kindex),dtype=self.datatype)
except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)):
spec = np.array([spec],dtype=self.datatype)
else: else:
size = len(self.power_indices.get("kindex")) spec = np.array(spec,dtype=self.datatype)
## drop imaginary part ## drop imaginary part
spec = np.real(spec) spec = np.real(spec)
## check finiteness
if(not np.all(np.isfinite(spec))):
about.warnings.cprint("WARNING: infinite value(s).")
## check positivity (excluding null)
if(np.any(spec<0)):
raise ValueError(about._errors.cstring("ERROR: nonpositive value(s)."))
elif(np.any(spec==0)):
about.warnings.cprint("WARNING: nonpositive value(s).")
## extend ## extend
if(np.size(spec)==1): if(np.size(spec)==1):
...@@ -3482,10 +3490,18 @@ class lm_space(space): ...@@ -3482,10 +3490,18 @@ class lm_space(space):
""" """
if(isinstance(spec,field)): if(isinstance(spec,field)):
spec = spec.val.astype(self.datatype) spec = spec.val.astype(self.datatype)
elif(callable(spec)):
try:
spec = np.array(spec(np.arange(self.para[0]+1,dtype=np.int)),dtype=self.datatype)
except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)): elif(np.isscalar(spec)):
spec = np.array([spec],dtype=self.datatype) spec = np.array([spec],dtype=self.datatype)
else: else:
spec = np.array(spec,dtype=self.datatype) spec = np.array(spec,dtype=self.datatype)
## drop imaginary part
spec = np.real(spec)
## check finiteness ## check finiteness
if(not np.all(np.isfinite(spec))): if(not np.all(np.isfinite(spec))):
about.warnings.cprint("WARNING: infinite value(s).") about.warnings.cprint("WARNING: infinite value(s).")
...@@ -3496,9 +3512,6 @@ class lm_space(space): ...@@ -3496,9 +3512,6 @@ class lm_space(space):
about.warnings.cprint("WARNING: nonpositive value(s).") about.warnings.cprint("WARNING: nonpositive value(s).")
size = self.para[0]+1 ## lmax+1 size = self.para[0]+1 ## lmax+1
## drop imaginary part
spec = np.real(spec)
## extend ## extend
if(np.size(spec)==1): if(np.size(spec)==1):
spec = spec*np.ones(size,dtype=spec.dtype,order='C') spec = spec*np.ones(size,dtype=spec.dtype,order='C')
...@@ -4303,10 +4316,16 @@ class gl_space(space): ...@@ -4303,10 +4316,16 @@ class gl_space(space):
""" """
if(isinstance(spec,field)): if(isinstance(spec,field)):
spec = spec.val.astype(self.datatype) spec = spec.val.astype(self.datatype)
elif(callable(spec)):
try:
spec = np.array(spec(np.arange(self.para[0],dtype=np.int)),dtype=self.datatype)
except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)): elif(np.isscalar(spec)):
spec = np.array([spec],dtype=self.datatype) spec = np.array([spec],dtype=self.datatype)
else: else:
spec = np.array(spec,dtype=self.datatype) spec = np.array(spec,dtype=self.datatype)
## check finiteness ## check finiteness
if(not np.all(np.isfinite(spec))): if(not np.all(np.isfinite(spec))):
about.warnings.cprint("WARNING: infinite value(s).") about.warnings.cprint("WARNING: infinite value(s).")
...@@ -4317,7 +4336,6 @@ class gl_space(space): ...@@ -4317,7 +4336,6 @@ class gl_space(space):
about.warnings.cprint("WARNING: nonpositive value(s).") about.warnings.cprint("WARNING: nonpositive value(s).")
size = self.para[0] ## nlat size = self.para[0] ## nlat
## extend ## extend
if(np.size(spec)==1): if(np.size(spec)==1):
spec = spec*np.ones(size,dtype=spec.dtype,order='C') spec = spec*np.ones(size,dtype=spec.dtype,order='C')
...@@ -4930,10 +4948,16 @@ class hp_space(space): ...@@ -4930,10 +4948,16 @@ class hp_space(space):
""" """
if(isinstance(spec,field)): if(isinstance(spec,field)):
spec = spec.val.astype(self.datatype) spec = spec.val.astype(self.datatype)
elif(callable(spec)):
try:
spec = np.array(spec(np.arange(3*self.para[0],dtype=np.int)),dtype=self.datatype)
except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)): elif(np.isscalar(spec)):
spec = np.array([spec],dtype=self.datatype) spec = np.array([spec],dtype=self.datatype)
else: else:
spec = np.array(spec,dtype=self.datatype) spec = np.array(spec,dtype=self.datatype)
## check finiteness ## check finiteness
if(not np.all(np.isfinite(spec))): if(not np.all(np.isfinite(spec))):
about.warnings.cprint("WARNING: infinite value(s).") about.warnings.cprint("WARNING: infinite value(s).")
...@@ -4944,7 +4968,6 @@ class hp_space(space): ...@@ -4944,7 +4968,6 @@ class hp_space(space):
about.warnings.cprint("WARNING: nonpositive value(s).") about.warnings.cprint("WARNING: nonpositive value(s).")
size = 3*self.para[0] ## 3*nside size = 3*self.para[0] ## 3*nside
## extend ## extend
if(np.size(spec)==1): if(np.size(spec)==1):
spec = spec*np.ones(size,dtype=spec.dtype,order='C') spec = spec*np.ones(size,dtype=spec.dtype,order='C')
......
...@@ -213,7 +213,7 @@ def smooth_power(spec,domain=None,kindex=None,mode="2s",exclude=1,sigma=-1,**kwa ...@@ -213,7 +213,7 @@ def smooth_power(spec,domain=None,kindex=None,mode="2s",exclude=1,sigma=-1,**kwa
spec = spec.val.astype(kindex.dtype) spec = spec.val.astype(kindex.dtype)
elif(callable(spec)): elif(callable(spec)):
try: try:
spec = spec(kindex) spec = np.array(spec(kindex),dtype=kindex.dtype)
except: except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)`` TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)): elif(np.isscalar(spec)):
...@@ -640,7 +640,7 @@ def interpolate_power(spec,mode="linear",domain=None,kindex=None,newkindex=None, ...@@ -640,7 +640,7 @@ def interpolate_power(spec,mode="linear",domain=None,kindex=None,newkindex=None,
spec = spec.val.astype(kindex.dtype) spec = spec.val.astype(kindex.dtype)
elif(callable(spec)): elif(callable(spec)):
try: try:
spec = spec(kindex) spec = np.array(spec(kindex),dtype=kindex.dtype)
except: except:
TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)`` TypeError(about._errors.cstring("ERROR: invalid power spectra function.")) ## exception in ``spec(kindex)``
elif(np.isscalar(spec)): elif(np.isscalar(spec)):
......
...@@ -68,7 +68,7 @@ def draw_vector_nd(axes,dgrid,ps,symtype=0,fourier=False,zerocentered=False,kpac ...@@ -68,7 +68,7 @@ def draw_vector_nd(axes,dgrid,ps,symtype=0,fourier=False,zerocentered=False,kpac
kdict = np.fft.fftshift(nkdict(axes,dgrid,fourier)) kdict = np.fft.fftshift(nkdict(axes,dgrid,fourier))
klength = nklength(kdict) klength = nklength(kdict)
else: else:
kdict = kpack[1][kpack[0]] kdict = kpack[1][np.fft.ifftshift(kpack[0],axes=shiftaxes(zerocentered,st_to_zero_mode=False))]
klength = kpack[1] klength = kpack[1]
#output is in position space #output is in position space
......
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