getting_started_1.py 2.64 KB
Newer Older
1
2
import nifty5 as ift
import numpy as np
3
4
5
6
7
8
9


def make_chess_mask():
    mask = np.ones(position_space.shape)
    for i in range(4):
        for j in range(4):
            if (i+j)%2 == 0:
Jakob Knollmueller's avatar
stuff    
Jakob Knollmueller committed
10
<<<<<<< HEAD
Jakob Knollmueller's avatar
Jakob Knollmueller committed
11
                mask[i*512/4:(i+1)*512/4, j*512/4:(j+1)*512/4] = 0
Jakob Knollmueller's avatar
stuff    
Jakob Knollmueller committed
12
=======
Philipp Arras's avatar
Philipp Arras committed
13
                mask[i*128//4:(i+1)*128//4, j*128//4:(j+1)*128//4] = 0
Jakob Knollmueller's avatar
stuff    
Jakob Knollmueller committed
14
>>>>>>> 2b5d58fda1926161b86883a9e639f969c3c7e4fb
15
16
    return mask

Philipp Arras's avatar
Philipp Arras committed
17

18
19
20
21
22
def make_random_mask():
    mask = ift.from_random('pm1',position_space)
    mask = (mask+1)/2
    return mask.val

Philipp Arras's avatar
Philipp Arras committed
23

24
if __name__ == '__main__':
25
    ## describtion of the tutorial ###
26

27
    # Choose problem geometry and masking
28

29
30
31
    # # One dimensional regular grid
    # position_space = ift.RGSpace([1024])
    # mask = np.ones(position_space.shape)
32

Jakob Knollmueller's avatar
stuff    
Jakob Knollmueller committed
33
<<<<<<< HEAD
34
    # # Two dimensional regular grid with chess mask
Jakob Knollmueller's avatar
Jakob Knollmueller committed
35
    position_space = ift.RGSpace([512,512])
Jakob Knollmueller's avatar
stuff    
Jakob Knollmueller committed
36
=======
Philipp Arras's avatar
Philipp Arras committed
37
38
    # Two dimensional regular grid with chess mask
    position_space = ift.RGSpace([128,128])
Jakob Knollmueller's avatar
stuff    
Jakob Knollmueller committed
39
>>>>>>> 2b5d58fda1926161b86883a9e639f969c3c7e4fb
Jakob Knollmueller's avatar
Jakob Knollmueller committed
40
    mask = make_chess_mask()
41

42
    # # Sphere with half of its locations randomly masked
Jakob Knollmueller's avatar
Jakob Knollmueller committed
43
44
    # position_space = ift.HPSpace(128)
    # mask = make_random_mask()
45

46
47
48
    # set up corresponding harmonic transform and space
    harmonic_space = position_space.get_default_codomain()
    HT = ift.HarmonicTransformOperator(harmonic_space, target=position_space)
49

50
51
52
53
54
55
    # set correlation structure with a power spectrum and build prior correlation covariance
    def power_spectrum(k):
        return 100. / (20.+k**3)
    power_space = ift.PowerSpace(harmonic_space)
    PD = ift.PowerDistributor(harmonic_space, power_space)
    prior_correlation_structure = PD(ift.PS_field(power_space, power_spectrum))
56

57
    S = ift.DiagonalOperator(prior_correlation_structure)
58

59
60
61
62
63
64
65
66
67
68
69
    # build instrument response consisting of a discretization, mask and harmonic transformaion
    GR = ift.GeometryRemover(position_space)
    mask = ift.Field(position_space,val=mask)
    Mask = ift.DiagonalOperator(mask)
    R = GR * Mask * HT

    data_space = GR.target

    # setting the noise covariance
    noise = 5.
    N = ift.ScalingOperator(noise, data_space)
70

71
72
73
74
    # creating mock data
    MOCK_SIGNAL = S.draw_sample()
    MOCK_NOISE = N.draw_sample()
    data = R(MOCK_SIGNAL) + MOCK_NOISE
75

76
77
78
79
    # building propagator D and information source j
    j = R.adjoint_times(N.inverse_times(data))
    D_inv = R.adjoint * N.inverse * R + S.inverse
    # make it invertible
80
    IC = ift.GradientNormController(iteration_limit=500, tol_abs_gradnorm=1e-3)
81
82
83
84
85
86
87
    D = ift.InversionEnabler(D_inv,IC,approximation=S.inverse).inverse

    # WIENER FILTER
    m = D(j)

    ##PLOTTING
    #Truth, data, reconstruction, residuals