-
Notifications
You must be signed in to change notification settings - Fork 0
/
noisemk.py
executable file
·351 lines (291 loc) · 9.56 KB
/
noisemk.py
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#! /usr/bin/env python
import ctypes
import scipy as sp
import numpy as np
from numpy.fft import *
import scipy.linalg as linalg
import multiprocessing as mp
import random
from core import algebra, hist
from kiyopy import parse_ini
import kiyopy.utils
import kiyopy.custom_exceptions as ce
from scipy import integrate
from math import *
from sys import *
import matplotlib.pyplot as plt
import MakePower
pi = 3.1415926
deg2rad = pi/180.
params_init = {
'processes' : 1,
'plot' : True,
'input_root' : '../newmaps/',
'hr' : ('15hr_40-41-43_','15hr_42_',),
'mid' : ('dirty_map_','noise_inv_diag_'),
'polarizations' : ('I',),
'output_root' : './',
'last' : (),
'sigma' : 0.1,
'mu' : 0.0,
'boxshape' : (60,12,6),
'boxunit' : 15., # in unit Mpc h-1
'discrete' : 3,
'Xrange' : (1400,),
'Yrange' : (-6*15,6*15),
'Zrange' : (0.,6*15),
}
prefix = 'nmk_'
class MakeNoise(object):
""" Make Gausian Noise for the simulation maps"""
def __init__(self, parameter_file_or_dict=None, feedback=2):
# Read in the parameters.
self.params = parse_ini.parse(
parameter_file_or_dict, params_init, prefix=prefix)
self.feedback=feedback
self.plot = bool(self.params['plot'])
def execute(self, nprocesses=1):
params = self.params
# Make parent directory and write parameter file.
kiyopy.utils.mkparents(params['output_root'])
parse_ini.write_params(params, params['output_root']+'params.ini',prefix='pk_')
hr = params['hr']
mid = params['mid']
last = params['last']
all_out_fname_list = []
all_in_fname_list = []
pol_str = params['polarizations'][0]
n_processes = params['processes']
#### Process ####
n_new = n_processes -1
n_map = len(hr)
if n_new <=0:
for hr_str, ii in zip(params['hr'],range(len(params['hr']))):
end = pol_str
if len(last)!=0:
end = end + last[ii]
#imap_fname = in_root + hr_str + 'dirty_map_' + pol_str + '.npy'
#imap_fname = in_root + hr_str + mid + pol_str + '.npy'
imap_fname = hr_str + mid[0] + end + '.npy'
nmap_fname = hr_str + mid[1] + end + '.npy'
self.process_map(imap_fname, nmap_fname, ii)
elif n_new >32:
raise ValueError("Processes limit is 32")
else:
process_list = range(n_new)
for ii in xrange(n_map + n_new):
if ii >= n_new:
process_list[ii%n_new].join()
if process_list[ii%n_new].exitcode != 0:
raise RuntimeError("A thred faild with exit code"
+ str(process_list[ii%n_new].exitcode))
if ii < n_map:
end = pol_str
if len(last)!=0:
end = end + last[ii]
imap_fname = hr[ii] + mid[0] + end + '.npy'
nmap_fname = hr[ii] + mid[1] + end + '.npy'
#mock_fname = hr[ii] + 'mock_map_' + end + '.npy'
process_list[ii%n_new] = mp.Process(
target=self.process_map,
args=(imap_fname, nmap_fname, ii))
#args=(imap_fname, nmap_fname, mock_fname))
process_list[ii%n_new].start()
return 0
def process_map(self, imap_fname, nmap_fname, ii, mock_fname=None):
params = self.params
sigma = params['sigma']
mu = params['mu']
out_root = params['output_root']
in_root = params['input_root']
imap = algebra.load(in_root + imap_fname)
imap = algebra.make_vect(imap)
#print imap.flatten().mean()
imap = imap - imap.flatten().mean()
if imap.axes != ('freq', 'ra', 'dec') :
raise ce.DataError('AXES ERROR!')
print ' :: Set Noise to Gaussian'
np.random.seed()
nmap = algebra.info_array(
sigma*np.random.randn(imap.shape[0],imap.shape[1], imap.shape[2])+mu)
nmap.axes = imap.axes
nmap = algebra.make_vect(nmap)
nmap.info = imap.info
if nmap.axes != ('freq', 'ra', 'dec') :
raise ce.DataError('AXES ERROR!')
## add noise to map ##
imap = imap + nmap
non0 = nmap.nonzero()
nmap[non0] = (1./sigma)**2
#if mock_fname != None:
# mmap = algebra.info_array(
# 2.*np.random.randn(imap.shape[0],imap.shape[1], imap.shape[2])-0.5)
# mmap.axes = imap.axes
# mmap = algebra.make_vect(mmap)
# box, nbox, mbox = self.fill(imap, nmap, mmap)
# pkrm_nfname = out_root + 'fftbox_' + mock_fname
# algebra.save(pkrm_nfname, mbox)
#else:
# box, nbox = self.fill(imap, nmap)
hr = params['hr']
mid = params['mid']
last = params['last']
pol_str = params['polarizations'][0]
end = pol_str
if len(last)!=0:
end = end + last[ii]
end = end + '_' + str(ii)
imap_fname = hr[ii] + mid[0] + end + '.npy'
nmap_fname = hr[ii] + mid[1] + end + '.npy'
pkrm_fname = out_root + imap_fname
algebra.save(pkrm_fname, imap)
pkrm_nfname = out_root + nmap_fname
algebra.save(pkrm_nfname, nmap)
# print mmap.shape
# print 'Removing Peak... Map:' + hr_str[:-1]
# self.pkrm(imap,nmap,
# out_root+'pkrm'+hr_str+'dirty_map_'+pol_str+'.png', threshold=2.5)
#print imap.flatten().max()
#print imap.flatten().min()
#print box.flatten().max()
#print box.flatten().min()
def fill(self, imap, nmap, mmap=None):
params = self.params
mapshape = np.array(imap.shape)
r = self.fq2r(imap.get_axis('freq'))
ra = imap.get_axis('ra')*deg2rad
de = imap.get_axis('dec')*deg2rad
ra0= ra[int(ra.shape[0]/2)]
ra = ra - ra0
dra= ra.ptp()/ra.shape[0]
dde= de.ptp()/de.shape[0]
#print r.min(), r.max()
#print self.xyz(ra.min(), de.min(), r.min())
#print self.xyz(ra.max(), de.min(), r.min())
#print self.xyz(ra.min(), de.max(), r.min())
#print self.xyz(ra.max(), de.max(), r.min())
#print self.xyz(ra.min(), de.min(), r.max())
#print self.xyz(ra.max(), de.min(), r.max())
#print self.xyz(ra.min(), de.max(), r.max())
#print self.xyz(ra.max(), de.max(), r.max())
###return 0
mapinf = [ra.min(), dra, de.min(), dde]
mapinf = np.array(mapinf)
box = algebra.info_array(sp.zeros(params['boxshape']))
box.axes = ('x','y','z')
box = algebra.make_vect(box)
box_xrange = params['Xrange']
box_yrange = params['Yrange']
box_zrange = params['Zrange']
box_unit = params['boxunit']
box_disc = params['discrete']
box_x = np.arange(box_xrange[0], box_xrange[1], box_unit/box_disc)
box_y = np.arange(box_yrange[0], box_yrange[1], box_unit/box_disc)
box_z = np.arange(box_zrange[0], box_zrange[1], box_unit/box_disc)
#print box_x.shape
#print box_y.shape
#print box_z.shape
boxshape = np.array(box.shape)*box_disc
boxinf0 = [0, 0, 0]
boxinf0 = np.array(boxinf0)
boxinf1 = [boxshape[0], boxshape[1], boxshape[2]]
boxinf1 = np.array(boxinf1)
print "MapPrepare: Filling the FFT BOX"
MakePower.Filling(
imap, r, mapinf, box, boxinf0, boxinf1, box_x, box_y, box_z)
nbox = algebra.info_array(sp.zeros(params['boxshape']))
nbox.axes = ('x','y','z')
nbox = algebra.make_vect(nbox)
#nbox = algebra.info_array(sp.ones(params['boxshape']))
#nbox.axes = ('x','y','z')
#nbox = algebra.make_vect(nbox)
MakePower.Filling(
nmap, r, mapinf, nbox, boxinf0, boxinf1, box_x, box_y, box_z)
if mmap != None:
mbox = algebra.info_array(sp.zeros(params['boxshape']))
mbox.axes = ('x','y','z')
mbox = algebra.make_vect(mbox)
MakePower.Filling(
mmap, r, mapinf, mbox, boxinf0, boxinf1, box_x, box_y, box_z)
return box, nbox, mbox
else:
return box, nbox
#return imap, nmap
def pkrm(self, imap, nmap, fname, threshold=2.0):
freq = imap.get_axis('freq')/1.e6
if self.plot==True:
plt.figure(figsize=(8,8))
plt.subplot(211)
plt.title('Map with peak remove')
plt.xlabel('Frequece (MHz)')
plt.ylabel('$\Delta$ T(Kelvin) Without Foreground')
#for i in range(13,14):
# for j in range(25, 26):
for i in range(0,imap.shape[2]):
for j in range(0, imap.shape[1]):
plt.plot(freq, imap.swapaxes(0,2)[i][j])
dsigma = 50
while(dsigma>0.01):
sigma = imap.std(0)
for i in range(0, imap.shape[0]):
good = [np.fabs(imap[i]).__lt__(threshold*sigma)]
choicelist = [imap[i]]
imap[i] = np.select(good, choicelist)
choicelist = [nmap[i]]
nmap[i] = np.select(good, choicelist)
dsigma = (sigma.__sub__(imap.std(0))).max()
#print dsigma
#print '\n'
if self.plot==True:
plt.subplot(212)
plt.xlabel('Frequece (MHz)')
plt.ylabel('$\Delta$ T(Kelvin) Without Foreground')
#for i in range(13,14):
# for j in range(25, 26):
for i in range(0,imap.shape[2]):
for j in range(0, imap.shape[1]):
plt.plot(freq, imap.swapaxes(0,2)[i][j])
plt.savefig(fname, format='png')
plt.show()
#plt.ylim(-0.0001,0.0001)
def xyzv(self, ra, de, r, ra0=0.):
x = r*sin(0.5*pi-de)*cos(ra-ra0)
y = r*sin(0.5*pi-de)*sin(ra-ra0)
z = r*cos(0.5*pi-de)
v = r**2*sin(0.5*pi-de)
return x, y, z, v
def fq2r(self, freq, freq0=1.4e9 , c_H0 = 2.99e3, Omegam=0.27, Omegal=0.73):
"""change the freq to distence"""
zz = freq0/freq - 1.
for i in range(0, zz.shape[0]):
zz[i] = c_H0*self.funcdl(zz[i], Omegam, Omegal)
return zz
def discrete(self, array):
"""discrete the data pixel into small size"""
newarray = sp.zeros(self.params['discrete']*(array.shape[0]-1)+1)
for i in range(0, array.shape[0]-1):
delta = (array[i+1]-array[i])/float(self.params['discrete'])
for j in range(0, self.params['discrete']):
newarray[i*self.params['discrete']+j] = array[i] + j*delta
newarray[-1] = array[-1]
return newarray
def funcdl(self, z, omegam, omegal):
func = lambda z, omegam, omegal: \
((1.+z)**2*(1.+omegam*z)-z*(2.+z)*omegal)**(-0.5)
dl, dlerr = integrate.quad(func, 0, z, args=(omegam, omegal))
if omegam+omegal>1. :
k = (omegam+omegal-1.)**(0.5)
return sin(k*dl)/k
elif omegam+omegal<1.:
k = (1.-omegam-omegal)**(0.5)
return sinh(k*dl)/k
elif omegam+omegal==1.:
return dl
if __name__ == '__main__':
import sys
if len(sys.argv)==2 :
MakeNoise(str(sys.argv[1])).execute()
elif len(sys.argv)>2 :
print 'Maximun one argument, a parameter file name.'
else :
MakeNoise().execute()