Beispiel #1
0
def my_custom_prior(u):
    # prior distributions transform from 0:1 to the parameter range
    # here: a gaussian prior distribution, cut below 1/above 3
    x = scipy.stats.norm(1.9, 0.15).ppf(u)
    if x < 1.:
        x = 1
    if x > 3:
        x = 3
    return x


# 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
    # 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),
]

# we want nH to come out in logarithmic values, without offset of 22
# so we shift the existing jeffreys prior transformation:
Beispiel #2
0
def my_custom_prior(u):
    # prior distributions transform from 0:1 to the parameter range
    # here: a gaussian prior distribution, cut below 1/above 3
    x = scipy.stats.norm(1.9, 0.15).ppf(u)
    if x < 1.:
        x = 1
    if x > 3:
        x = 3
    return x


# 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)
m.powerlaw.norm.values = ",,1e-10,1e-10,1e1,1e1"

def my_custom_prior(u):
	# prior distributions transform from 0:1 to the parameter range
	# here: a gaussian prior distribution, cut below 1/above 3
	x = scipy.stats.norm(1.9, 0.15).ppf(u)
	if x < 1.:
		x = 1
	if x > 3:
		x = 3
	return x

# 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)
Beispiel #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])


m.gaussian.Sigma.values = ",,0.001,0.001,1,1"

def my_custom_prior(u):
	# prior distributions transform from 0:1 to the parameter range
	# here: a gaussian prior distribution, cut below 1/above 3
	x = scipy.stats.norm(1.9, 0.15).ppf(u)
	if x < 1.:
		x = 1
	if x > 3:
		x = 3
	return x

# 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
	# 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),
]

# we want nH to come out in logarithmic values, without offset of 22
# so we shift the existing jeffreys prior transformation:
Beispiel #6
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