Planned maintenance on Wednesday, 2021-01-20, 17:00-18:00. Expect some interruptions during that time

Commit 92638ed6 authored by Philipp Arras's avatar Philipp Arras

Remove old files

parent 346cb2a2
Pipeline #63019 failed with stages
in 4 minutes and 31 seconds
# 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/>.
#
# Copyright(C) 2013-2019 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.
import nifty5 as ift
if __name__ == '__main__':
import numpy as np
np.random.seed(42)
nspecs = 2
a = []
spaces = []
for _ in range(nspecs):
ndim = 2
sspace = ift.RGSpace(
np.linspace(16, 20, num=ndim).astype(np.int),
np.linspace(2.3, 7.99, num=ndim))
hspace = sspace.get_default_codomain()
spaces.append(sspace)
target = ift.PowerSpace(hspace)
a.append(ift.NormalizedAmplitude(target, 16, 1, 1, -3, 1, 0, 1, 0, 1))
tgt = ift.makeDomain(tuple(spaces))
op = ift.CorrelatedFieldNormAmplitude(tgt, a, 0., 1.)
fld = ift.from_random('normal', op.domain)
ift.extra.check_jacobian_consistency(op, fld)
import numpy as np
import nifty5 as ift
if __name__ == '__main__':
np.random.seed(42)
ndim = 1
sspace = ift.RGSpace(
np.linspace(16, 20, num=ndim).astype(np.int),
np.linspace(2.3, 7.99, num=ndim))
hspace = sspace.get_default_codomain()
target = ift.PowerSpace(hspace)
op = ift.NormalizedAmplitude(target, 16, 1, 1, -3, 1, 0, 1, 0, 1)
if not isinstance(op.target[0], ift.PowerSpace):
raise RuntimeError
fld = ift.from_random('normal', op.domain)
ift.extra.check_jacobian_consistency(op, fld)
pd = ift.PowerDistributor(hspace, power_space=target)
ht = ift.HartleyOperator(pd.target)
if np.testing.assert_allclose((pd @ op)(fld).integrate(), 1):
raise RuntimeError
if np.testing.assert_allclose(
(ht @ pd @ op)(fld).to_global_data()[ndim*(0,)], 1):
raise RuntimeError
import nifty5 as ift
import numpy as np
def mfplot(fld, A1, A2, name):
p = ift.Plot()
dom = fld.domain
mi, ma = np.min(fld.val), np.max(fld.val)
for ii in range(dom.shape[-1]):
extr = ift.DomainTupleFieldInserter(op.target, 1, (ii,)).adjoint
p.add(extr(fld), zmin=mi, zmax=ma)
p.add(A1)
p.add(A2)
p.add(fld)
p.output(name=name, xsize=14, ysize=14)
if __name__ == '__main__':
np.random.seed(42)
sspace = ift.RGSpace((512, 512))
hspace = sspace.get_default_codomain()
target = ift.PowerSpace(hspace,
ift.PowerSpace.useful_binbounds(hspace, True))
# A = Amplitude(target, [5, -3, 1, 0], [1, 1, 1, 0.5],
# ['rest', 'smooth', 'wienersigma'])
# fld = ift.from_random('normal', A.domain)
# ift.extra.check_jacobian_consistency(A, fld)
# op = CorrelatedFieldNormAmplitude(sspace, A, 3, 2)
# plts = []
# p = ift.Plot()
# for _ in range(25):
# fld = ift.from_random('normal', op.domain)
# plts.append(A.force(fld))
# p.add(op(fld))
# p.output(name='debug1.png', xsize=20, ysize=20)
# ift.single_plot(plts, name='debug.png')
# Multifrequency
sspace1 = ift.RGSpace((512, 512))
hspace1 = sspace1.get_default_codomain()
target1 = ift.PowerSpace(hspace1,
ift.PowerSpace.useful_binbounds(hspace1, True))
A1 = ift.WPAmplitude(target1, [0, -2, 0], [1E-5, 1, 1], 1, 0.99,
['rest1', 'smooth1', 'wienersigma1'])
sspace2 = ift.RGSpace((20,), distances=(1e7))
hspace2 = sspace2.get_default_codomain()
target2 = ift.PowerSpace(hspace2,
ift.PowerSpace.useful_binbounds(hspace2, True))
A2 = ift.WPAmplitude(target2, [0, -2, 0], [1E-5, 1, 1], 1, 0.99,
['rest2', 'smooth2', 'wienersigma2'])
op = ift.CorrelatedFieldNormAmplitude((sspace1, sspace2), (A1, A2), 3, 2)
for jj in range(10):
fld = ift.from_random('normal', op.domain)
mfplot(op(fld), A1.force(fld), A2.force(fld), 'debug{}.png'.format(jj))
......@@ -76,9 +76,6 @@ from .minimization.metric_gaussian_kl import MetricGaussianKL
from .sugar import *
from .plot import Plot
from .library.smooth_linear_amplitude import (
SLAmplitude, LinearSLAmplitude, CepstrumOperator, WPAmplitude)
from .library.normalized_amplitude import NormalizedAmplitude
from .library.inverse_gamma_operator import InverseGammaOperator
from .library.los_response import LOSResponse
from .library.dynamic_operator import (dynamic_operator,
......@@ -86,11 +83,10 @@ from .library.dynamic_operator import (dynamic_operator,
from .library.light_cone_operator import LightConeOperator
from .library.wiener_filter_curvature import WienerFilterCurvature
from .library.correlated_fields import CorrelatedField, MfCorrelatedField, CorrelatedFieldNormAmplitude
from .library.adjust_variances import (make_adjust_variances_hamiltonian,
do_adjust_variances)
from .library.gridder import GridderMaker
from .library.final_amplitude import FinalAmplitude
from .library.correlated_fields import CorrelatedFieldMaker
from . import extra
......
This diff is collapsed.
This diff is collapsed.
import nifty5 as ift
import numpy as np
class SymmetrizingOperator(ift.EndomorphicOperator):
def __init__(self, domain):
self._domain = ift.DomainTuple.make(domain)
if len(self._domain.shape) != 1:
raise TypeError
if not isinstance(self._domain[0], ift.RGSpace):
raise TypeError
self._capability = self.TIMES | self.ADJOINT_TIMES
def apply(self, x, mode):
self._check_input(x, mode)
res = x.to_global_data_rw()
res -= res[::-1]
res /= 2
return ift.from_global_data(self._tgt(mode), res)
def CepstrumOperator(target, a, k0):
a = float(a)
target = ift.DomainTuple.make(target)
if a <= 0:
raise ValueError
if len(target) > 1 or target[0].harmonic:
raise TypeError
if isinstance(k0, (float, int)):
k0 = np.array([k0]*len(target.shape))
else:
k0 = np.array(k0)
if len(k0) != len(target.shape):
raise ValueError
if np.any(np.array(k0) <= 0):
raise ValueError
if not isinstance(target[0], ift.RGSpace):
raise TypeError
dom = target[0].get_default_codomain()
qht = ift.HartleyOperator(dom, target=target[0])
sym = SymmetrizingOperator(target)
pspace = ift.PowerSpace(dom)
ceps_field = ift.PS_field(pspace, lambda k: a/(1 + (k/k0)**2))
ceps_field = ift.PowerDistributor(dom, power_space=pspace)(ceps_field)
return sym @ qht @ ift.makeOp(ceps_field)
class SlopeOperator(ift.LinearOperator):
def __init__(self, target):
self._target = ift.DomainTuple.make(target)
if len(self._target) > 1:
raise TypeError
if len(self._target[0].shape) > 1:
raise TypeError
if not isinstance(self._target[0], ift.RGSpace):
raise TypeError
self._domain = ift.DomainTuple.make(ift.UnstructuredDomain((2,)))
self._capability = self.TIMES | self.ADJOINT_TIMES
self._pos = np.fft.fftfreq(
self._target.size)*self._target.size*self._target[0].distances[0]
def apply(self, x, mode):
self._check_input(x, mode)
inp = x.to_global_data()
if mode == self.TIMES:
res = inp[1] + inp[0]*self._pos
else:
res = np.array([np.sum(self._pos*inp), np.sum(inp)], dtype=x.dtype)
return ift.Field.from_global_data(self._tgt(mode), res)
class ExpTransform(ift.LinearOperator):
def __init__(self, domain, target):
self._target = ift.makeDomain(target)
self._domain = ift.makeDomain(domain)
if not isinstance(self._domain, ift.MultiDomain):
raise TypeError
if set(self._domain.keys()) != set(['zeromode', 'nonzeromodes']):
raise ValueError
# Zeromode domain is Unstr(1)
# Nonzeromode domain is RGSpace nonharmonic, 1D, even number of pixs
self._capability = self.TIMES | self.ADJOINT_TIMES
tgt = self._target[0]
if not isinstance(tgt, ift.PowerSpace):
raise ValueError("Target must be a power space.")
log_k_array = np.log(tgt.k_lengths[1:])
t_min = np.amin(log_k_array)
bindistances = self._domain['nonzeromodes'][0].distances[0]
coord = (log_k_array - t_min)/bindistances
self._bindex = np.floor(coord).astype(int)
self._frac = coord - self._bindex
def apply(self, x, mode):
self._check_input(x, mode)
wgt = self._frac
if mode == self.ADJOINT_TIMES:
x = x.to_global_data()
xnew = {}
xnew['zeromode'] = ift.full(self._domain['zeromode'], x[0])
foo = np.zeros(self._domain['nonzeromodes'].shape[0])
np.add.at(foo, self._bindex, x[1:]*(1. - wgt))
np.add.at(foo, self._bindex + 1, x[1:]*wgt)
xnew['nonzeromodes'] = ift.from_global_data(
self._domain['nonzeromodes'], foo)
return ift.MultiField.from_dict(xnew, self._domain)
else: # TIMES
xnew = np.empty(self._target.shape)
zm = x['zeromode'].to_global_data()
nzm = x['nonzeromodes'].to_global_data()
xnew[1:] = nzm[self._bindex]*(1. - wgt)
xnew[1:] += nzm[self._bindex + 1]*wgt
xnew[0] = zm
return ift.from_global_data(self._target, xnew)
def NormalizedAmplitude(target,
n_pix,
a,
k0,
sm,
sv,
im,
iv,
zmmean,
zmsig,
keys=['tau', 'phi', 'zeromode']):
if not (isinstance(n_pix, int) and isinstance(target, ift.PowerSpace)):
raise TypeError
a, k0 = float(a), float(k0)
sm, sv, im, iv = float(sm), float(sv), float(im), float(iv)
zmmean, zmsig = float(zmmean), float(zmsig)
if sv <= 0 or iv <= 0 or zmsig <= 0:
raise ValueError
dct = {'a': a, 'k0': k0}
karray = target.k_lengths
logkarray = np.log(karray[1:])
t_0 = np.amin(logkarray)
dst = (np.amax(logkarray) - t_0)/(n_pix - 1)
dom = ift.RGSpace(2*n_pix, distances=dst)
smooth = CepstrumOperator(dom, **dct).ducktape(keys[0])
sl = SlopeOperator(dom)
mean = np.array([sm, im + sm*t_0])
sig = np.array([sv, iv])
mean = ift.Field.from_global_data(sl.domain, mean)
sig = ift.Field.from_global_data(sl.domain, sig)
linear = sl @ ift.Adder(mean) @ ift.makeOp(sig).ducktape(keys[1])
nonzeromodes = 0.5*(smooth + linear)
ust = ift.UnstructuredDomain(1)
zmmean = ift.full(ust, zmmean)
zmsig = ift.full(ust, zmsig)
zeromode = ift.Adder(zmmean) @ ift.makeOp(zmsig) @ ift.ducktape(
ust, None, keys[2])
allmodes = zeromode.ducktape_left('zeromode') + nonzeromodes.ducktape_left(
'nonzeromodes')
et = ExpTransform(allmodes.target, target)
ift.extra.consistency_check(et)
ift.extra.consistency_check(sl)
ift.extra.consistency_check(smooth)
return Normalization(et.target) @ (et @ allmodes).exp()
class Normalization(ift.Operator):
def __init__(self, domain):
self._domain = self._target = ift.makeDomain(domain)
hspace = self._domain[0].harmonic_partner
pd = ift.PowerDistributor(hspace, power_space=self._domain[0])
# TODO Does not work on sphere yet
self._cst = pd.adjoint(ift.full(pd.target, hspace.scalar_dvol))
self._specsum = SpecialSum(self._domain)
def apply(self, x):
self._check_input(x)
return self._specsum(self._cst*x).one_over()*x
class SpecialSum(ift.EndomorphicOperator):
def __init__(self, domain):
self._domain = ift.makeDomain(domain)
self._capability = self.TIMES | self.ADJOINT_TIMES
def apply(self, x, mode):
self._check_input(x, mode)
return ift.full(self._tgt(mode), x.sum())
This diff is collapsed.
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