-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmockSED.py
451 lines (426 loc) · 16.1 KB
/
mockSED.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import os
import sys
import types
import importlib
import numpy as np
#np.seterr(all="ignore")
import george
from george import kernels
import matplotlib
import matplotlib.pyplot as plt
matplotlib_version = eval(matplotlib.__version__.split(".")[0])
if matplotlib_version > 1:
plt.style.use("classic")
import sedfit.SED_Toolkit as sedt
from astropy.table import Table
from sedfit.fitter import basicclass as bc
from sedfit import sedclass as sedsc
from sedfit import model_functions as sedmf
from sedfit.fit_functions import logLFunc_gp, logLFunc
def dataPerturb(x, sigma, pert=True, maxIter=10):
"""
Perturb the data assuming it is a Gaussian distribution around the detected
values with standard deviation as the uncertainties.
"""
if pert:
xp = sigma * np.random.randn(len(np.atleast_1d(x))) + x
counter = 0
while np.any(xp<=0):
xp = sigma * np.random.randn(len(np.atleast_1d(x))) + x
counter += 1
if counter > maxIter:
raise ValueError("The data is too noisy...")
else:
xp = x
return xp
def randomRange(low, high):
"""
Calculate the random number in range [low, high).
Parameters
----------
low : float
The lower boundary.
high: float
The upper boundary.
Returns
-------
r : float
The random number in [low, high).
Notes
-----
None.
"""
assert high >= low
rg = high - low
r = low + rg * np.random.rand()
return r
def configImporter(configfile):
"""
This function import the provided configure file.
Parameters
----------
configfile : string
The name of the configure file (with the path).
Returns
-------
config : module object
The imported module.
Notes
-----
None.
"""
pathList = configfile.split("/")
configPath = "/".join(pathList[0:-1])
sys.path.append(configPath)
configName = pathList[-1].split(".")[0]
config = importlib.import_module(configName)
return config
def mocker(sedData, sedModel, sysUnc=None, uncModel=None, silent=True,
pert=True, nonDetect=True):
"""
This function is to generate a mock SED according to a given observed SED.
Basically, the flux densities will be replaced by the model value while the
wavelength and uncertainties of the data will be kept.
Parameters
----------
sedData : SEDClass object
The data set of SED.
sedModel : ModelCombiner object
The combined model. The parameters are set to generate the mock SED.
sysUnc : dict or None, by default
{
"pht": [([nBgn, nEnd], frac), ([nBgn, nEnd], frac), ...],
"spc": frac
}
uncModel : dict or None, by default
{
"lnf" : float, (-inf, 0]
The ln of f, the imperfectness of the model.
"lna" : float, (-inf, 1]
The ln of a, the amplitude of the residual correlation.
"lntau" : float, (-inf, 1]
The ln of tau, the scale length of the residual correlation.
}
pert : bool, default: True
Perturb the data according to the uncertainty if True.
nonDetect : bool, default: True
Replace the upperlimits of the sedData to the mock SED.
Returns
-------
mockPck : dict
The dict of mock data.
sed : the concatenated wave, flux and sigma of the SED.
pht : the photometric wave, flux, sigma and band.
spc : the spectra wave, flux and sigma.
"""
################################################################################
# Data #
################################################################################
#->Generate the mock data
waveModel = sedModel.get_xList()
fluxModel = sedModel.combineResult()
mockPht0 = np.array(sedData.model_pht(waveModel, fluxModel))
mockSpc0 = np.array(sedData.model_spc(sedModel.combineResult))
#->Make sure the perturbed data not likely be too far away, or even negative.
#For photometric data
mockPhtSigma = np.array(sedData.get_dsList("e"))
fltr_sigma = mockPht0 < 3.0*mockPhtSigma #sedsigma
if np.any(fltr_sigma):
print("[mocker] Warning: There are some bands with flux less than 3*sigma!")
mockPhtSigma[fltr_sigma] = mockPht0[fltr_sigma] / 3.0
mockPht = dataPerturb(mockPht0, mockPhtSigma, pert)
#For spectroscopic data
mockSpcSigma = np.array(sedData.get_csList("e"))
fltr_sigma = mockSpc0 < 3.0*mockSpcSigma
if np.any(fltr_sigma):
mockSpcSigma[fltr_sigma] = mockSpc0[fltr_sigma] / 3.0
mockSpc = dataPerturb(mockSpc0, mockSpcSigma, pert)
mockPhtWave = np.array(sedData.get_dsList("x"))
mockSpcWave = np.array(sedData.get_csList("x"))
#->Systematic uncertainties
if not sysUnc is None:
sysSpc = sysUnc["spc"]
mockSpc = (1 + randomRange(-sysSpc, sysSpc)) * mockSpc
sysPhtList = sysUnc["pht"]
for phtRg, frac in sysPhtList:
#print phtRg, frac, mockPht[phtRg[0]:phtRg[1]]
mockPht[phtRg[0]:phtRg[1]] = (1 + randomRange(-frac, frac)) * mockPht[phtRg[0]:phtRg[1]]
#->Model imperfectness & spectral residual correlation
if not uncModel is None:
e = np.e
#For the photometric data
if sedData.check_dsData():
f = e**uncModel["lnf"]
mockPht = (1 + randomRange(-f, f)) * mockPht
else:
f = 0
#For the spectral data
if sedData.check_csData():
a = e**uncModel["lna"]
tau = e**uncModel["lntau"]
gp = george.GP(a * kernels.ExpSquaredKernel(tau))
mockSpc = (1 + randomRange(-f, f)) * mockSpc
mockSpc += gp.sample(mockSpcWave)
#->Add the upperlimits
if nonDetect:
phtflux = np.array(sedData.get_dsList("y"))
phtflag = np.array(sedData.get_dsList("f"))
fltr_undct = phtflag == 1
mockPht[fltr_undct] = phtflux[fltr_undct]
mockPhtSigma[fltr_undct] = -1
mockSedFlux = np.concatenate([mockPht, mockSpc])
mockSedWave = np.concatenate([mockPhtWave, mockSpcWave])
mockSedSigma = np.concatenate([mockPhtSigma, mockSpcSigma])
mockPhtBand = sedData.get_unitNameList()
mockSpcBand = np.zeros_like(mockSpcWave)
mockSedBand = np.concatenate([mockPhtBand, mockSpcBand])
mockPck = {
"sed": (mockSedWave, mockSedFlux, mockSedSigma, mockSedBand),
"pht": (mockPhtWave, mockPht, mockPhtSigma, mockPhtBand),
"spc": (mockSpcWave, mockSpc, mockSpcSigma, mockSpcBand),
}
print "mockPht", mockPht
return mockPck
def sedLnLike(sedData, sedModel, uncModel):
"""
Calculate the lnlike of the SED.
"""
mockPars = sedModel.get_parVaryList()
if uncModel is None:
lnlike = logLFunc(mockPars, sedData, sedModel)
else:
mockPars = list(mockPars)
mockPars.append(uncModel["lnf"])
mockPars.append(uncModel["lna"])
mockPars.append(uncModel["lntau"])
lnlike = logLFunc_gp(mockPars, sedData, sedModel)
return lnlike
def PlotMockSED(sedData, mockData, sedModel):
waveModel = sedModel.get_xList()
sedPhtFlux = sedData.get_List("y")
xmin = np.min(waveModel)
xmax = np.max(waveModel)
ymin = np.min(sedPhtFlux) / 10.0
ymax = np.max(sedPhtFlux) * 10.0
FigAx = sedData.plot_sed()
FigAx = mockData.plot_sed(FigAx=FigAx, phtColor="r", spcColor="r")
FigAx = sedModel.plot(FigAx=FigAx)
fig, ax = FigAx
ax.set_xlim([xmin, xmax])
ax.set_ylim([ymin, ymax])
return (fig, ax)
def gsm_mocker(configName, targname=None, redshift=None, distance=None, sedFile=None,
mockPars=None, uncModel=None, plot=False, cal_lnlike=False, **kwargs):
"""
The wrapper of mocker() function. If the targname, redshift and sedFile
are provided as arguments, they will be used overriding the values in the
config file saved in configName. If they are not provided, then, the values
in the config file will be used.
Parameters
----------
configName : str
The full path of the config file.
targname : str or None by default
The name of the target.
redshift : float (optional)
The redshift of the target.
distance : float (optional)
The distance of the target.
sedFile : str or None by default
The full path of the sed data file.
mockPars : list
The parameters to generate the mock SED.
uncModel : dict or None, by default
{
"lnf" : float, (-inf, 0]
The ln of f, the imperfectness of the model.
"lna" : float, (-inf, 1]
The ln of a, the amplitude of the residual correlation.
"lntau" : float, (-inf, 1]
The ln of tau, the scale length of the residual correlation.
}
plot : bool, default: False
Plot the SED to visually check if True.
Returns
-------
mock : tuple
The (wavelength, flux, sigma, band) of the mock data in the observed frame.
lnlike : float (optional)
The lnlikelihood of the mock SED and the model.
FigAx : tuple (optional)
The (fig, ax) of the SED figure, if plot is True.
Notes
-----
None.
"""
#config = importlib.import_module(configName.split(".")[0])
config = configImporter(configName)
if targname is None:
assert redshift is None
assert sedFile is None
targname = config.targname
redshift = config.redshift
sedFile = config.sedFile
else:
assert not redshift is None
assert not sedFile is None
print("#--------------------------------#")
print("Target: {0}".format(targname))
print("SED file: {0}".format(sedFile))
print("Config file: {0}".format(configName))
print("#--------------------------------#")
try:
silent = config.silent
except:
silent = False
############################################################################
# Data #
############################################################################
dataDict = config.dataDict
sedPck = sedt.Load_SED(sedFile)
sedData = sedsc.setSedData(targname, redshift, distance, dataDict, sedPck, silent)
############################################################################
# Model #
############################################################################
modelDict = config.modelDict
print("The model info:")
parCounter = 0
for modelName in modelDict.keys():
print("[{0}]".format(modelName))
model = modelDict[modelName]
for parName in model.keys():
param = model[parName]
if not isinstance(param, types.DictType):
continue
elif param["vary"]:
print("-- {0}, {1}".format(parName, param["type"]))
parCounter += 1
else:
pass
print("Varying parameter number: {0}".format(parCounter))
print("#--------------------------------#")
#->Build up the model
funcLib = sedmf.funcLib
waveModel = config.waveModel
try:
parAddDict_all = config.parAddDict_all
except:
parAddDict_all = {}
parAddDict_all["DL"] = sedData.dl
parAddDict_all["z"] = redshift
parAddDict_all["frame"] = "rest"
sedModel = bc.Model_Generator(modelDict, funcLib, waveModel, parAddDict_all)
sedModel.updateParList(mockPars) #Set the model with the desiring parameters.
############################################################################
# Mock #
############################################################################
mockPck = mocker(sedData, sedModel, **kwargs)
#->Reform the result and switch back to the observed frame
sed = mockPck["sed"]
pht = mockPck["pht"]
spc = mockPck["spc"]
sed = sedt.SED_to_obsframe(sed, redshift)
pht = sedt.SED_to_obsframe(pht, redshift)
spc = sedt.SED_to_obsframe(spc, redshift)
phtwave = pht[0]
phtband = pht[3]
spcwave = spc[0]
mockSedWave = sed[0]
mockSedFlux = sed[1]
mockSedSigma = sed[2]
mockSedBand = np.concatenate([phtband, np.zeros_like(spcwave, dtype="int")])
mock = (mockSedWave, mockSedFlux, mockSedSigma, mockSedBand)
result = [mock]
#->Calculate the lnlike
mockDict = {
"phtName": dataDict["phtName"],
"spcName": dataDict["spcName"],
"bandList_use": dataDict["bandList_use"],
"bandList_ignore": dataDict["bandList_ignore"],
"frame": "obs",
}
mockPck = {
"sed": sed,
"pht": pht,
"spc": spc
}
mockData = sedsc.setSedData(targname, redshift, distance, mockDict, mockPck, silent)
if cal_lnlike:
lnlike = sedLnLike(mockData, sedModel, uncModel)
result.append(lnlike)
#->Plot
if plot:
FigAx = PlotMockSED(sedData, mockData, sedModel)
result.append(FigAx)
return result
if __name__ == "__main__":
filename = "haha/aaa/bbb"
pathList = filename.split("/")
print pathList
configPath = "/".join(pathList[0:-1])
print configPath
'''
#-->Generate Mock Data
parTable = Table.read("/Volumes/Transcend/Work/PG_MCMC/pg_clu_qpahVar/compile_pg_clu.ipac", format="ascii.ipac")
infoTable = Table.read("targlist/targlist_rq.ipac", format="ascii.ipac")
#print parTable.colnames
if os.path.isdir("configs"):
sys.path.append("configs/")
configName = "config_mock_clu"
mockSub = "test"
parNameList = ['logMs', 'logOmega', 'T', 'logL', 'i', 'tv', 'q', 'N0', 'sigma', 'Y',
'logumin', 'qpah', 'gamma', 'logMd']
comments = """
#This mock SED is created from {0} at redshift {1}.
#The uncertainties of the data are the real uncertainties of the sources.
#The systematics: WISE:{S[0]}, PACS:{S[1]}, SPIRE:{S[2]}, MIPS:{S[3]}.
#The config file in use is {2}.
#lnlikelihood = {3}
#parNames = {4}
#inputPars = {5}
"""
#->WISE (Jarrett2011), PACS(Balog2014), SPIRE(Pearson2013), Spitzer(MIPS handbook)
sysUnc = {
#"spc": 0.05,
"spc": 0.00,
#"pht": [([0, 2], 0.03), ([2, 5], 0.05), ([5, 8], 0.05)]
"pht": [([0, 2], 0.00), ([2, 5], 0.00), ([5, 8], 0.00)] #Check for the accurate case
}
#loop_T = 0
nRuns = 1 #len(parTable)
for loop_T in range(nRuns):
targname = infoTable["Name"][loop_T]
redshift = infoTable["z"][loop_T]
sedFile = infoTable["sed"][loop_T]
fltr_Target = parTable["Name"]==targname
#Load the mock parameters
mockPars = []
for parName in parNameList:
mockPars.append(parTable["{0}_C".format(parName)][fltr_Target][0])
#print parTable[loop_T]
gsmPck = gsm_mocker(configName, targname, redshift, sedFile=sedFile,
mockPars=mockPars, sysUnc=sysUnc, #uncModel=[-np.inf, -np.inf, -np.inf],
pert=False, plot=True, cal_lnlike=True)
mock, lnlike, FigAx = gsmPck
plt.savefig("mock/{0}_mock.png".format(targname), bbox_inches="tight")
plt.close()
print("--------lnlike={0:.5f}".format(lnlike))
#->Save mock file
wave = mock[0]
flux = mock[1]
sigma = mock[2]
band = mock[3]
mockTable = Table([wave, flux, sigma, band],
names=['wavelength', 'flux', 'sigma', "band"])
mockTable["wavelength"].format = "%.3f"
mockTable["flux"].format = "%.3f"
mockTable["sigma"].format = "%.3f"
mockName = "mock/{0}_{1}.msed".format(targname, mockSub)
mockTable.write(mockName, format="ascii", delimiter="\t", overwrite=True)
#f = open("mock/{0}_{1}.msed".format(mockName, mockSub), "w")
f = open(mockName, "a")
suList = [sysUnc["pht"][0][1], sysUnc["pht"][1][1], sysUnc["pht"][2][1], sysUnc["spc"]]
cmnt = comments.format(targname, redshift, configName, lnlike, parNameList, mockPars, S=suList)
f.writelines(cmnt)
f.close()
'''