# define prior
transformations = [
	# jeffreys prior for nH (but see below)
	bxa.create_jeffreys_prior_for(m, m.wabs.nH),
	# jeffreys prior for scale variable
	bxa.create_jeffreys_prior_for(m, m.powerlaw.norm),
	# custom gaussian prior function for photon index
	bxa.create_custom_prior_for(  m, m.powerlaw.PhoIndex, my_custom_prior),
	# and possibly many more
]

# we want nH to come out in logarithmic values, without offset of 22
# so we shift the existing jeffreys prior transformation:
# first get the old transformation
prevtransform = transformations[0]['transform']
# shift it for storage
transformations[0]['transform'] = lambda x: prevtransform(x) + 22
# before putting it into xspec, we have to shift back and exponentiate
transformations[0]['aftertransform'] = lambda x: 10**(x - 22)

# send it off!
bxa.standard_analysis(transformations,
	outputfiles_basename = outputfiles_basename,
	verbose=True, # show a bit of progress
	resume=True, # MultiNest supports resuming a crashed/aborted run
	#skipsteps = 'marginals,qq,unconvolved,summary'.split(',')
	)


Exemplo n.º 2
0

# define prior
transformations = [
    # jeffreys prior for nH (but see below)
    bxa.create_jeffreys_prior_for(m, m.wabs.nH),
    # jeffreys prior for scale variable
    bxa.create_jeffreys_prior_for(m, m.powerlaw.norm),
    # custom gaussian prior function for photon index
    bxa.create_custom_prior_for(m, m.powerlaw.PhoIndex, my_custom_prior),
    # and possibly many more
]

# we want nH to come out in logarithmic values, without offset of 22
# so we shift the existing jeffreys prior transformation:
# first get the old transformation
prevtransform = transformations[0]['transform']
# shift it for storage
transformations[0]['transform'] = lambda x: prevtransform(x) + 22
# before putting it into xspec, we have to shift back and exponentiate
transformations[0]['aftertransform'] = lambda x: 10**(x - 22)

# send it off!
bxa.standard_analysis(
    transformations,
    outputfiles_basename=outputfiles_basename,
    verbose=True,  # show a bit of progress
    resume=True,  # MultiNest supports resuming a crashed/aborted run
    #skipsteps = 'marginals,qq,unconvolved,summary'.split(',')
)
Exemplo n.º 3
0
    bxa.create_jeffreys_prior_for(m, m.wabs.nH),
    # jeffreys prior for scale variable
    bxa.create_jeffreys_prior_for(m, m.powerlaw.norm),
    # custom gaussian prior function for photon index
    bxa.create_custom_prior_for(m, m.powerlaw.PhoIndex, my_custom_prior),
    # and possibly many more
    # jeffreys prior for scale variable
    bxa.create_jeffreys_prior_for(m, m.gaussian.norm),
    # uniform prior for location variable
    bxa.create_uniform_prior_for(m, m.gaussian.LineE),
    # jeffreys prior for scale variable
    bxa.create_jeffreys_prior_for(m, m.gaussian.Sigma),
]
outputfiles_basename = 'simplest3-'
bxa.standard_analysis(transformations,
                      outputfiles_basename=outputfiles_basename,
                      verbose=True,
                      resume=False)
#AllModels.calcFlux("8. 40000.0 err") #参数需要一个能量的范围,之前在拟合光谱时我们设置了一个范围,我们暂时用它。
#AllModels.calcFlux('1:**-200.,40000.0,err-** 2-3:**-8.,800.,err-**')#不能这么用
#par3=AllModels(1)(3)#第一个模型的第三个参数
print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')
#print(par3.values)
#print(par3.error)
#value = par3.values[0]
#value_arr1,value_arr2,ffff = par3.error
print('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$')

#AllModels.calcFlux(".1 10.0 err")
#AllModels.calcLumin(".1 10. .05 err")

#flux = AllData(1).flux              #计算流量,‘1’代表第一个光谱的流量
Exemplo n.º 4
0
from xspec import *
import bxa.xspec as bxa


Plot.xAxis='keV'
Plot.yLog=True

alldatastr='n4.pha'
AllData(alldatastr)
AllData.ignore('1:**-8.0,800.0-**  2:**-200.0,40000.0-**')
Fit.statMethod='cstat'
m = Model("grbm")
m.grbm.alpha.values = ',,-10,-10,5,10'
m.grbm.beta.values = ',,-10,-10,10,20'
m.grbm.tem.values = ',,1e-10,1e-5,1e5,1e6'


print(m.grbm.tem.values)

transformations = [bxa.create_uniform_prior_for( m, m.grbm.alpha),
                   bxa.create_uniform_prior_for( m, m.grbm.beta),
                   bxa.create_jeffreys_prior_for( m, m.grbm.tem)]

outputfiles_basename = 'simplest3-'
bxa.standard_analysis(transformations,outputfiles_basename = outputfiles_basename,verbose=True, resume=False,skipsteps = ['convolved'])

par3=AllModels(1)(3)
print(par3.values[0],par3.error[0])


Exemplo n.º 5
0
Plot.xAxis = 'keV'

s = Spectrum('example-file.fak')
s.ignore("**"); s.notice("0.2-8.0")
m = Model("pow")

# set model parameters ranges
#                         val, delta, min, bottom, top, max
m.powerlaw.norm.values = ",,1e-10,1e-10,1e1,1e1" # 10^-10 .. 10
m.powerlaw.PhoIndex.values = ",,1,1,3,3"       #     1 .. 3

# define prior
transformations = [
	# uniform prior for Photon Index (see other example for 
	# something more advanced)
	bxa.create_uniform_prior_for( m, m.powerlaw.PhoIndex),
	# jeffreys prior for scale variable
	bxa.create_jeffreys_prior_for(m, m.powerlaw.norm),
	# and possibly many more parameters here
]
# where to store intermediate and final results? this is the prefix used
outputfiles_basename = 'simplest-'

# send it off!
bxa.standard_analysis(transformations,
	outputfiles_basename = outputfiles_basename,
	verbose=True, # show a bit of progress
	resume=True, # MultiNest supports resuming a crashed/aborted run
	)

Exemplo n.º 6
0
s = Spectrum('example-file.fak')
s.ignore("**")
s.notice("0.2-8.0")
m = Model("pow")

# set model parameters ranges
#                         val, delta, min, bottom, top, max
m.powerlaw.norm.values = ",,1e-10,1e-10,1e1,1e1"  # 10^-10 .. 10
m.powerlaw.PhoIndex.values = ",,1,1,3,3"  #     1 .. 3

# define prior
transformations = [
    # uniform prior for Photon Index (see other example for
    # something more advanced)
    bxa.create_uniform_prior_for(m, m.powerlaw.PhoIndex),
    # jeffreys prior for scale variable
    bxa.create_jeffreys_prior_for(m, m.powerlaw.norm),
    # and possibly many more parameters here
]
# where to store intermediate and final results? this is the prefix used
outputfiles_basename = 'simplest-'

# send it off!
bxa.standard_analysis(
    transformations,
    outputfiles_basename=outputfiles_basename,
    verbose=True,  # show a bit of progress
    resume=True,  # MultiNest supports resuming a crashed/aborted run
)
Exemplo n.º 7
0
def xspec_fit_kernel(filelist,datadir,savedir,num = 0,flux = False):
	os.chdir(datadir)
	Plot.xAxis='keV'
	Plot.yLog=True
	alldatastr = ' '.join(filelist)
	AllData.clear()
	AllModels.clear()
	AllData(alldatastr)
	#AllData.show()
	Fit.statMethod='cstat'
	
	AllData.ignore('1:**-200.0,40000.0-** 2-4:**-8.0,800.0-**')#我觉得这里可以留一个接口。
	m = Model('grbm')
	

	#--------------------------------------------
	'''
	设置grbm模型的三个参数的范围。
	'''
	#m.grbm.alpha.values = ',,-10,-10,5,5'
	#m.grbm.beta.values = ',,-10,-10,10,10'
	m.grbm.tem.values = ',,1e-10,1e-10,1e6,1e6'
	#--------------------------------------------
	transformations = [bxa.create_uniform_prior_for(m,m.grbm.alpha),
			   bxa.create_uniform_prior_for(m,m.grbm.beta),
			   #bxa.create_uniform_prior_for(m,m.grbm.tem)]
			   bxa.create_jeffreys_prior_for(m,m.grbm.tem)]
	outputdir = savedir+'results'+str(num)+'/'
	if os.path.exists(outputdir) == False:
		os.makedirs(outputdir)
	os.chdir(outputdir)
	outputfiles_basename = 'simpest_'
	
	bxa.standard_analysis(transformations,outputfiles_basename = outputfiles_basename,verbose =True,resume=False,skipsteps = ['convolved','qq'])
	par3=AllModels(1)(3)#第一个模型的第三个参数
	value = par3.values[0]
	value_arr1,value_arr2,ffff = par3.error
	Plot('eeufspec')
	flux_list = []
	for i in range(len(filelist)):
		print(i)
		if(flux):
			flux = AllData(i+1).flux
			flux_list.append(flux)
		energies=Plot.x(i+1)
		rates=Plot.y(i+1)
		folded=Plot.model(i+1)
		xErrs=Plot.xErr(i+1)
		yErrs=Plot.yErr(i+1)
		plt.errorbar(energies,rates,xerr=xErrs,yerr=yErrs,zorder=1,ls='None')
		plt.plot(energies,folded,color='black',zorder=2)
	plt.axvline(x = value,color = 'r')
	plt.axvline(x = value_arr1,color = 'g')
	plt.axvline(x = value_arr2,color = 'g')
	plt.xlabel('Energy KeV')
	plt.ylabel(r'${KeV^{2} (Photons cm^{-2}s^{-1}keV^{-1})}$')
	plt.xscale('log')
	plt.yscale('log')
	plt.savefig(savedir + 'foldedspec.png')
	#plt.close()
	if flux :

		return value,value_arr1,value_arr2,np.array(flux_list).T
	else:
		return value,value_arr1,value_arr2