Commit 9961a7f7 authored by Martin Reinecke's avatar Martin Reinecke
Browse files

Merge branch 'concise_offset_parametrization' into 'NIFTy_6'

CorrelatedFieldMaker: offset parametrization in one place

See merge request !415
parents 86a5890b 7cb08b8f
Pipeline #71968 passed with stages
in 19 minutes and 41 seconds
......@@ -55,9 +55,32 @@ if __name__ == '__main__':
position_space = ift.RGSpace([128, 128])
cfmaker = ift.CorrelatedFieldMaker.make(1e-3, 1e-6, '')
cfmaker.add_fluctuations(position_space,
1., 1e-2, 1, .5, .1, .5, -3, 0.5, '')
cfmaker = ift.CorrelatedFieldMaker.make(
offset_mean = 0.0, # 0.
offset_std_mean = 1e-3, # 1e-3
offset_std_std = 1e-6, # 1e-6
prefix = '')
fluctuations_dict = {
# Amplitude of the fluctuations
'fluctuations_mean': 2.0, # 1.0
'fluctuations_stddev': 1.0, # 1e-2
# Smooth variation speed
'flexibility_mean': 2.5, # 1.0
'flexibility_stddev': 1.0, # 0.5
# How strong the ragged component of the spectrum is
# (Ratio of Wiener process and integrated Wiener process ?)
'asperity_mean': 0.5, # 0.1
'asperity_stddev': 0.5, # 0.5
# Slope of linear spectrum component
'loglogavgslope_mean': -2.0, # -3.0
'loglogavgslope_stddev': 0.5 # 0.5
}
cfmaker.add_fluctuations(position_space, **fluctuations_dict)
correlated_field = cfmaker.finalize()
A = cfmaker.amplitude
......
......@@ -73,7 +73,7 @@ if __name__ == '__main__':
sp2 = ift.RGSpace(npix2)
# Set up signal model
cfmaker = ift.CorrelatedFieldMaker.make(1e-2, 1e-6, '')
cfmaker = ift.CorrelatedFieldMaker.make(0., 1e-2, 1e-6, '')
cfmaker.add_fluctuations(sp1, 0.1, 1e-2, 1, .1, .01, .5, -2, 1., 'amp1')
cfmaker.add_fluctuations(sp2, 0.1, 1e-2, 1, .1, .01, .5,
-1.5, .5, 'amp2')
......
......@@ -355,32 +355,50 @@ class _Amplitude(Operator):
class CorrelatedFieldMaker:
def __init__(self, amplitude_offset, prefix, total_N):
if not isinstance(amplitude_offset, Operator):
raise TypeError("amplitude_offset needs to be an operator")
def __init__(self, offset_mean, offset_fluctuations_op, prefix, total_N):
if not isinstance(offset_fluctuations_op, Operator):
raise TypeError("offset_fluctuations_op needs to be an operator")
self._a = []
self._position_spaces = []
self._azm = amplitude_offset
self._offset_mean = offset_mean
self._azm = offset_fluctuations_op
self._prefix = prefix
self._total_N = total_N
@staticmethod
def make(offset_amplitude_mean, offset_amplitude_stddev, prefix,
def make(offset_mean, offset_std_mean, offset_std_std, prefix,
total_N=0,
dofdex=None):
"""Returns a CorrelatedFieldMaker object.
Parameters
----------
offset_mean : float
Mean offset from zero of the correlated field to be made.
offset_std_mean : float
Mean standard deviation of the offset value.
offset_std_std : float
Standard deviation of the stddev of the offset value.
prefix : string
Prefix to the names of the domains of the cf operator to be made.
total_N : integer
?
dofdex : np.array
?
"""
if dofdex is None:
dofdex = np.full(total_N, 0)
elif len(dofdex) != total_N:
raise ValueError("length of dofdex needs to match total_N")
N = max(dofdex) + 1 if total_N > 0 else 0
zm = _LognormalMomentMatching(offset_amplitude_mean,
offset_amplitude_stddev,
zm = _LognormalMomentMatching(offset_std_mean,
offset_std_std,
prefix + 'zeromode',
N)
if total_N > 0:
zm = _Distributor(dofdex, zm.target, UnstructuredDomain(total_N)) @ zm
return CorrelatedFieldMaker(zm, prefix, total_N)
return CorrelatedFieldMaker(offset_mean, zm, prefix, total_N)
def add_fluctuations(self,
position_space,
......@@ -471,12 +489,13 @@ class CorrelatedFieldMaker:
corr = reduce(mul, a)
return ht(azm*corr*ducktape(hspace, None, self._prefix + 'xi'))
def finalize(self, offset=None, prior_info=100):
def finalize(self, prior_info=100):
"""
offset vs zeromode: volume factor
"""
op = self._finalize_from_op()
if offset is not None:
if self._offset_mean is not None:
offset = self._offset_mean
# Deviations from this offset must not be considered here as they
# are learned by the zeromode
if isinstance(offset, (Field, MultiField)):
......
......@@ -22,17 +22,16 @@ import nifty6 as ift
from ..common import setup_function, teardown_function
@pytest.mark.parametrize('sspace', [
ift.RGSpace(4),
ift.RGSpace((4, 4), (0.123, 0.4)),
ift.HPSpace(8),
ift.GLSpace(4)
])
@pytest.mark.parametrize('sspace', [ift.RGSpace(4),
ift.RGSpace((4, 4), (0.123, 0.4)),
ift.HPSpace(8),
ift.GLSpace(4)])
@pytest.mark.parametrize('rseed', [13, 2])
@pytest.mark.parametrize('Astds', [[1., 3.], [0.2, 1.4]])
@pytest.mark.parametrize('offset_std', [1., 10.])
@pytest.mark.parametrize('N', [0,2])
def testAmplitudesConsistency(rseed, sspace, Astds, offset_std, N):
@pytest.mark.parametrize('offset_std_mean', [1., 10.])
@pytest.mark.parametrize('N', [0, 2])
@pytest.mark.parametrize('zm_mean', [0, 1.])
def testAmplitudesConsistency(rseed, sspace, Astds, offset_std_mean, N, zm_mean):
def stats(op, samples):
sc = ift.StatCalculator()
for s in samples:
......@@ -50,7 +49,7 @@ def testAmplitudesConsistency(rseed, sspace, Astds, offset_std, N):
else:
dofdex1, dofdex2, dofdex3 = None, None, None
fa = ift.CorrelatedFieldMaker.make(offset_std, 1E-8, '', N, dofdex1)
fa = ift.CorrelatedFieldMaker.make(zm_mean, offset_std_mean, 1E-8, '', N, dofdex1)
fa.add_fluctuations(sspace, Astds[0], 1E-8, 1.1, 2., 2.1, .5, -2, 1.,
'spatial', dofdex = dofdex2)
fa.add_fluctuations(fsspace, Astds[1], 1E-8, 3.1, 1., .5, .1, -4, 1.,
......@@ -81,8 +80,7 @@ def testAmplitudesConsistency(rseed, sspace, Astds, offset_std, N):
assert_allclose(slice_fluct_std0, sl_fluct_space, rtol=0.5)
assert_allclose(slice_fluct_std1, sl_fluct_freq, rtol=0.5)
fa = ift.CorrelatedFieldMaker.make(offset_std, .1, '', N, dofdex1)
fa = ift.CorrelatedFieldMaker.make(0., offset_std_mean, .1, '', N, dofdex1)
fa.add_fluctuations(fsspace, Astds[1], 1., 3.1, 1., .5, .1, -4, 1., 'freq', dofdex = dofdex3)
m = 3.
x = fa.moment_slice_to_average(m)
......
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