getting_started_2.py 2.68 KB
Newer Older
Natalia Porqueres's avatar
Natalia Porqueres committed
1
2
3
4
import nifty5 as ift
import numpy as np


Jakob Knollmueller's avatar
Jakob Knollmueller committed
5
6
def get_2D_exposure():
    x_shape, y_shape = position_space.shape
Natalia Porqueres's avatar
Natalia Porqueres committed
7

Jakob Knollmueller's avatar
Jakob Knollmueller committed
8
9
10
11
12
13
14
    exposure = np.ones(position_space.shape)
    exposure[x_shape/3:x_shape/2,:] *= 2.
    exposure[x_shape*4/5:x_shape,:] *= .1
    exposure[x_shape/2:x_shape*3/2,:] *=3.
    exposure[:,x_shape / 3:x_shape / 2] *= 2.
    exposure[:,x_shape * 4 / 5:x_shape] *= .1
    exposure[:,x_shape / 2:x_shape * 3 / 2] *= 3.
Natalia Porqueres's avatar
Natalia Porqueres committed
15

Jakob Knollmueller's avatar
Jakob Knollmueller committed
16
17
    exposure = ift.Field(position_space, val=exposure)
    return exposure
Natalia Porqueres's avatar
Natalia Porqueres committed
18

Natalia Porqueres's avatar
Natalia Porqueres committed
19

Jakob Knollmueller's avatar
Jakob Knollmueller committed
20
21
if __name__ == '__main__':
    # ABOUT THIS CODE
22
    np.random.seed(41)
Natalia Porqueres's avatar
Natalia Porqueres committed
23

Jakob Knollmueller's avatar
Jakob Knollmueller committed
24
    # Set up the position space of the signal
25
    #
Jakob Knollmueller's avatar
Jakob Knollmueller committed
26
27
28
    # # One dimensional regular grid with uniform exposure
    # position_space = ift.RGSpace(1024)
    # exposure = np.ones(position_space.shape)
Natalia Porqueres's avatar
Natalia Porqueres committed
29

Natalia Porqueres's avatar
Natalia Porqueres committed
30

Jakob Knollmueller's avatar
Jakob Knollmueller committed
31
32
33
    # Two dimensional regular grid with inhomogeneous exposure
    position_space = ift.RGSpace([512, 512])
    exposure = get_2D_exposure()
Natalia Porqueres's avatar
Natalia Porqueres committed
34

Jakob Knollmueller's avatar
Jakob Knollmueller committed
35
36
37
    # # Sphere with with uniform exposure
    # position_space = ift.HPSpace(128)
    # exposure = np.ones(position_space.shape)
Natalia Porqueres's avatar
Natalia Porqueres committed
38

Jakob Knollmueller's avatar
Jakob Knollmueller committed
39
40
41
    # Defining harmonic space and transform
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, position_space)
Natalia Porqueres's avatar
Natalia Porqueres committed
42

Jakob Knollmueller's avatar
Jakob Knollmueller committed
43
44
    domain = ift.MultiDomain.make({'xi': harmonic_space})
    position = ift.from_random('normal', domain)
Natalia Porqueres's avatar
Natalia Porqueres committed
45

Jakob Knollmueller's avatar
Jakob Knollmueller committed
46
47
48
    # Define power spectrum and amplitudes
    def sqrtpspec(k):
        return 1. / (20. + k**2)
Natalia Porqueres's avatar
Natalia Porqueres committed
49

Jakob Knollmueller's avatar
Jakob Knollmueller committed
50
51
52
53
    p_space = ift.PowerSpace(harmonic_space)
    pd = ift.PowerDistributor(harmonic_space, p_space)
    a = ift.PS_field(p_space, sqrtpspec)
    A = pd(a)
Natalia Porqueres's avatar
Natalia Porqueres committed
54

Jakob Knollmueller's avatar
Jakob Knollmueller committed
55
56
57
58
59
    # Set up a sky model
    xi = ift.Variable(position)['xi']
    logsky_h = xi * A
    logsky = HT(logsky_h)
    sky = ift.PointwiseExponential(logsky)
Natalia Porqueres's avatar
Natalia Porqueres committed
60

Jakob Knollmueller's avatar
Jakob Knollmueller committed
61
62
63
64
65
    exposure = ift.Field(position_space, val=exposure)
    M = ift.DiagonalOperator(exposure)
    GR = ift.GeometryRemover(position_space)
    # Set up instrumental response
    R = GR * M
Natalia Porqueres's avatar
Natalia Porqueres committed
66

Jakob Knollmueller's avatar
Jakob Knollmueller committed
67
68
69
70
71
72
    # Generate mock data
    d_space = R.target[0]
    lamb = R(sky)
    mock_position = ift.from_random('normal', lamb.position.domain)
    data = np.random.poisson(lamb.at(mock_position).value.val.astype(np.float64))
    data = ift.Field.from_local_data(d_space, data)
Natalia Porqueres's avatar
Natalia Porqueres committed
73

Jakob Knollmueller's avatar
Jakob Knollmueller committed
74
75
76
77
78
79
    # Compute likelihood and Hamiltonian
    position = ift.from_random('normal', lamb.position.domain)
    likelihood = ift.PoissonianEnergy(lamb, data)
    ic_cg = ift.GradientNormController(iteration_limit=50)
    ic_newton = ift.GradientNormController(name='Newton',iteration_limit=50, tol_abs_gradnorm=1e-3)
    minimizer = ift.RelaxedNewton(ic_newton)
Natalia Porqueres's avatar
Natalia Porqueres committed
80

Jakob Knollmueller's avatar
Jakob Knollmueller committed
81
82
83
    # Minimize the Hamiltonian
    H = ift.Hamiltonian(likelihood, ic_cg)
    H, convergence = minimizer(H)
Natalia Porqueres's avatar
Natalia Porqueres committed
84

Jakob Knollmueller's avatar
Jakob Knollmueller committed
85
86
    # Plot results
    result_sky = sky.at(H.position).value
87
    ##PLOTTING
Natalia Porqueres's avatar
Natalia Porqueres committed
88
89