-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoise_model2.py
More file actions
255 lines (165 loc) · 4.76 KB
/
noise_model2.py
File metadata and controls
255 lines (165 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from astropy.stats import biweight_midvariance, biweight_location
from astropy.io import fits
from matplotlib import pyplot as plt
import os
from scipy.signal import fftconvolve
import numpy as np
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--threshold", type=float, default=0.03,
help="Threshold for continuum signal masking.")
parser.add_argument("--mask_grow", type=float, default=10.,
help="Size for growing the continuum signal mask.")
parser.add_argument('-i', '--infile', type=str, metavar='infile',
help='Input cube.')
parser.add_argument('-o', '--outfile', type=str, metavar='outfile',
help='Output cube.')
args = parser.parse_args(sys.argv[1:])
hdu = fits.open(args.infile)
mask_grow = args.mask_grow
threshold = args.threshold
fcube = args.infile
fnew = args.outfile
c = fits.getdata(fcube)
header = fits.getheader(fcube)
# In[61]:
# build continuum signal mask
# compute median in spectral direction
m = np.nanmedian(c, axis=0)
# initial mask
zero_mask = m == 0.
mask = np.array( m > threshold, dtype=float)
# grow the mask
r = int( np.ceil(mask_grow/2.) )
xx = np.arange(-r-1,r+2,dtype=float)
X,Y = np.meshgrid(xx,xx)
dd = np.sqrt( X **2. + Y **2.)
kernel = np.array( dd < r, dtype=float)
kernel = kernel/np.sum(kernel)
smask = fftconvolve(mask, kernel, mode='same')
smask = smask > .1
# In[62]:
mm = np.zeros(c.shape[0])
ll = np.zeros(c.shape[0])
ss = np.zeros(c.shape[0])
vv = np.zeros(c.shape[0])
for i in range(c.shape[0]):
nanmask = ~ np.isnan(c[i])
vv[i] = np.sqrt( biweight_midvariance( c[i][smask * nanmask] ) )
ss[i] = np.std( c[i][smask * nanmask] )
mm[i] = np.mean(c[i][smask * nanmask] )
ll[i] = biweight_location(c[i][smask * nanmask] )
# In[63]:
zz = np.arange(c.shape[0])
ii = ~np.isnan(vv)
p = np.polyfit(zz[ii], vv[ii], deg = 17)
# In[64]:
from scipy.interpolate import UnivariateSpline
spl = UnivariateSpline(zz[ii], vv[ii], k=5, s=2.)
# In[65]:
f = plt.figure(figsize=[15,10])
plt.subplot(2,1,1)
plt.plot(mm)
plt.plot(ll)
plt.ylim([-.1,.1])
plt.grid()
plt.subplot(2,1,2)
plt.plot(ss)
plt.plot(vv)
plt.plot(spl(zz))
plt.ylim([0.,1.])
plt.grid()
# In[66]:
from numpy import random
random.seed(42)
nc = np.zeros_like(c)
N = nc.shape[1] * nc.shape[2]
for i,z in enumerate(zz):
ns = random.normal(scale = spl(zz[i]), size = N)
nc[i] = ns.reshape(nc[0].shape)
# In[67]:
# In[69]:
w = 31
m = 9
xc,yc = 48.,65.
ss = []
for i in range(c.shape[0]):
subim = c[i,int(yc-w/2):int(yc+w/2),int(xc-w/2):int(xc+w/2)]
M = []
for j in range(0,w-m):
for k in range(0,w-m):
M.append(subim[j:j+m,k:k+m].flatten() )
M = np.array(M)
cov = np.cov(M.T)
ss.append( cov[m**2//2].reshape([m,m]) )
ss=np.array(ss)
# In[70]:
w = 20
i = 45
plt.imshow( np.mean(ss[i*w:i*w+w], axis=0) )
# In[71]:
kernel = np.mean(ss, axis=0)
kernel = kernel/np.sum(kernel)
# In[72]:
from scipy.signal import fftconvolve
# In[73]:
cnc = fftconvolve(kernel,nc[0])
# In[74]:
plt.imshow(cnc)
# In[109]:
maxiter = 15
f = 0.5
DEBUG = True
from numpy import random
random.seed(42)
nc = np.zeros_like(c)
N = nc.shape[1] * nc.shape[2]
for i,z in enumerate(zz):
#i = 100
#print(i)
#i = 500
# this is the target sigma we need to reach
# after taking the covariance into account
target_sigma = spl(zz[i])
scale = target_sigma * 3.53
iter = 0
while True:
if DEBUG:
print("Iteration {}".format(iter))
print("target_sigma {}".format(target_sigma))
if iter > maxiter:
break
# generate random noise image (w/o) covaraince
ns = random.normal(scale = scale, size = N)
ns = ns.reshape(nc[0].shape)
# convolve with interpical covariance kernel
cns = fftconvolve(ns, kernel, mode='same')
# compute resulting standard deviation
s = np.std(cns)
if DEBUG:
print("Scale {}".format(scale))
print("sigma {}".format(s))
print("scale/target_sigma = {}".format(scale/target_sigma))
print("sigma/target_sigma = {}".format(s/target_sigma))
if 0.99 < s/target_sigma < 1.01:
break
# adjust noise scale by relative difference
dscale = scale / (s/target_sigma) - scale
if DEBUG:
print("dscale {}".format(dscale))
scale = scale + f * dscale
if DEBUG:
print("New scale {}".format(scale))
print("")
iter += 1
nc[i] = cns
#break
# In[110]:
plt.imshow(nc[-30])
# In[111]:
h = fits.PrimaryHDU(nc, header=header)
#for i in range(h.data.shape[0]):
# h.data[i][c[0] == 0.] = 0.
h.writeto(fnew, overwrite=True)
# In[ ]: