Commit f2669556 by Philipp Haim

### Merge branch 'normalized_amplitudes_pp' into normalized_amplitudes_DomT

parents 4fe143dd 05dde3a1
 # 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 . # # Copyright(C) 2013-2019 Max-Planck-Society # Author: Philipp Arras # # NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik. import numpy as np import nifty5 as ift import matplotlib.pyplot as plt def _default_pspace(dom): return ift.PowerSpace(dom.get_default_codomain()) if __name__ == '__main__': np.random.seed(42) fa = ift.CorrelatedFieldMaker() n_samps = 20 slope_means = [-2, -3] fa.add_fluctuations(ift.RGSpace(128, 0.1), 10, 2, 1, 1e-6, 2, 1e-6, slope_means[0], 0.2, 'spatial') # fa.add_fluctuations(_default_pspace(ift.RGSpace((128, 64))), 10, 2, 1, # 1e-6, 2, 1e-6, slope_means[0], 0.2, 'spatial') fa.add_fluctuations(ift.RGSpace(32), 10, 5, 1, 1e-6, 2, 1e-6, slope_means[1], 1, 'freq') correlated_field = fa.finalize(10, 0.1, '') amplitudes = fa.amplitudes plt.style.use('seaborn-notebook') tgt = correlated_field.target if len(tgt.shape) == 1: fig, axes = plt.subplots(nrows=1, ncols=2) fig.set_size_inches(20, 10) else: fig, axes = plt.subplots(nrows=3, ncols=3) fig.set_size_inches(20, 16) axs = (ax for ax in axes.ravel()) for ii, aa in enumerate(amplitudes): ax = next(axs) pspec = aa**2 ax.set_xscale('log') ax.set_yscale('log') for _ in range(n_samps): fld = pspec(ift.from_random('normal', pspec.domain)) klengths = fld.domain[0].k_lengths ycoord = fld.to_global_data_rw() ycoord[0] = ycoord[1] ax.plot(klengths, ycoord, alpha=1) ymin, ymax = ax.get_ylim() color = plt.rcParams['axes.prop_cycle'].by_key()['color'][0] lbl = 'Mean slope (k^{})'.format(2*slope_means[ii]) for fac in np.linspace(np.log(ymin), np.log(ymax**2/ymin)): xs = np.linspace(np.amin(klengths[1:]), np.amax(klengths[1:])) ys = xs**(2*slope_means[ii])*np.exp(fac) xs = np.insert(xs, 0, 0) ys = np.insert(ys, 0, ys[0]) ax.plot(xs, ys, zorder=1, color=color, linewidth=0.3, label=lbl) lbl = None ax.set_ylim([ymin, ymax]) ax.set_xlim([None, np.amax(klengths)]) ax.legend() if len(tgt.shape) == 2: foo = [] for ax in axs: pos = ift.from_random('normal', correlated_field.domain) fld = correlated_field(pos).to_global_data() foo.append((ax, fld)) mi, ma = np.inf, -np.inf for _, fld in foo: mi = min([mi, np.amin(fld)]) ma = max([ma, np.amax(fld)]) nxdx, nydy = tgt.shape if len(tgt) == 2: nxdx *= tgt[0].distances[0] nydy *= tgt[1].distances[0] else: nxdx *= tgt[0].distances[0] nydy *= tgt[0].distances[1] for ax, fld in foo: im = ax.imshow(fld.T, extent=[0, nxdx, 0, nydy], aspect='auto', origin='lower', vmin=mi, vmax=ma) fig.colorbar(im, ax=axes.ravel().tolist()) elif len(tgt.shape) == 1: ax = next(axs) flds = [] for _ in range(n_samps): pos = ift.from_random('normal', correlated_field.domain) ax.plot(correlated_field(pos).to_global_data()) plt.savefig('correlated_fields.png') plt.close()
 ... ... @@ -56,10 +56,10 @@ if __name__ == '__main__': filename = "getting_started_3_mode_{}_".format(mode) + "{}.png" position_space = ift.RGSpace([128, 128]) power_space = ift.PowerSpace(position_space.get_default_codomain()) cfmaker = ift.CorrelatedFieldMaker() cfmaker.add_fluctuations(power_space, 1, 1e-2, 1, .5, .1, .5, -3, 0.5, '') cfmaker.add_fluctuations(position_space, 1, 1e-2, 1, .5, .1, .5, -3, 0.5, '') correlated_field = cfmaker.finalize(1e-3, 1e-6, '') A = cfmaker.amplitudes[0] ... ... @@ -88,8 +88,8 @@ if __name__ == '__main__': minimizer = ift.NewtonCG(ic_newton) # Set up likelihood and information Hamiltonian likelihood = ift.GaussianEnergy(mean=data, inverse_covariance=N.inverse)(signal_response) likelihood = (ift.GaussianEnergy(mean=data, inverse_covariance=N.inverse) @ signal_response) H = ift.StandardHamiltonian(likelihood, ic_sampling) initial_mean = ift.MultiField.full(H.domain, 0.) ... ...
 ... ... @@ -211,3 +211,18 @@ Consequently, the inverse covariance operator will automatically have lower indi ensuring that the whole log-likelihood expression does not scale with resolution. **This upper-lower index convention is not coded into NIFTy**, in order to not reduce user freedom. One should however have this in mind when constructing log-likelihoods in order to ensure resolution independence. Harmonic Transform Convention ............................. In NIFTy the convention for the harmonic transformations is set by requiring the zero mode of the transformed field to be the integral over the original field. This choice is convenient for the Fourier transformation and used throughout the literature. Note that for the spherical harmonics this convention is only rarely used and might result in unexpected factors in the transformed field. To be specific, for the spherical harmonics transformation this means that a monopole of unit amplitude in position-space which is transformed to the spherical harmonics domain and back to the original position space via the adjoint transformation will have a non-unit amplitude afterwards. The factor between the input field and the twice transformed field is :math:1/4\pi. In comparison to the convention used in HEALPix, this corresponds to dividing the output of the HEALPix transformed field by :math:\sqrt{4\pi} in each transformation. Depending on the use-case, additional volume factors must be accounted for. This is especially true if one wants to define the inverse transformation. Note that the convention for the harmonic transformations used in NIFTy results in non-unitary operators.
 ... ... @@ -19,6 +19,8 @@ from functools import reduce from . import utilities from .domains.domain import Domain import numpy as np class DomainTuple(object): """Ordered sequence of Domain objects. ... ... @@ -125,6 +127,58 @@ class DomainTuple(object): """ return self._size def scalar_weight(self, spaces=None): """Returns the uniform volume element for a sub-domain of self. Parameters ---------- spaces : int, tuple of int or None Indices of the sub-domains to be considered. If None, the entire domain is used. Returns ------- float or None If the requested sub-domain has a uniform volume element, it is returned. Otherwise, None is returned. """ if np.isscalar(spaces): return self._dom[spaces].scalar_dvol if spaces is None: spaces = range(len(self._dom)) res = 1. for i in spaces: tmp = self._dom[i].scalar_dvol if tmp is None: return None res *= tmp return res def total_volume(self, spaces=None): """Returns the total volume of self or of a subspace of it.