def getChiScan():
	'''
		get the \chi^2 scan 
		1st line: 
		2nd line: settings
		3rd line: best fit
		4-end lines: scan
	'''
	
	### Constants
	sizeX = 100
	sizeY = 100
	name = '.scan.dat'
	
	### Create a file in /tmp with the scan minus the two first lines
	idx = 0
	f     = open(path__+name)
	tmp_f = open('/tmp/scan.txt','w')
	for line in f:
		if (idx>=2): tmp_f.write(line)
		idx += 1
	tmp_f.close()
	f.close()
	
	### Data
	data = numpy.loadtxt('/tmp/scan.txt')
	
	### Best Fit
	alphaPara_bestFit = data[0][11]
	alphaPerp_bestFit = data[0][12]
	chi2_bestFit      = data[0][-1]
	print alphaPara_bestFit, alphaPerp_bestFit, chi2_bestFit
	
	### Scan
	alphaPara = data[1:,11]
	alphaPerp = data[1:,12]
	chi2      = data[1:,-1]
	
	### Put data in 2D array
	alphaPara_2D = numpy.zeros( shape=(sizeX,sizeY) )
	alphaPerp_2D = numpy.zeros( shape=(sizeX,sizeY) )
	chi2_2D      = numpy.zeros( shape=(sizeX,sizeY) )
	for k in numpy.arange(0,chi2.size):
		i = k/sizeY
		j = k%sizeY
		alphaPara_2D[i][j] = alphaPara[k]
		alphaPerp_2D[i][j] = alphaPerp[k]
		chi2_2D[i][j]      = chi2[k]
	
	edge = [0.5, 1.5, 0.5, 1.5]

	### Get best fit plus bootstrap
	nbBoot = 10000
	bestFit  = numpy.zeros( shape=(2,nbBoot+1) )
	chi2Boot = numpy.zeros( shape=(nbBoot+1) )
	bestFit[0,0] = alphaPerp_bestFit
	bestFit[1,0] = alphaPara_bestFit
	chi2Boot[0]  = chi2_bestFit
	for i in range(0,nbBoot):
		try:
			data     = numpy.loadtxt('/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/BACKUP_2015_01_15/FitsFile_DR12_Guy/BaoFit_q_f/Bootstraps/boot_'+str(i).zfill(4)+'/bao2D.save.pars')
			dataChi2 = numpy.loadtxt('/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/BACKUP_2015_01_15/FitsFile_DR12_Guy/BaoFit_q_f/Bootstraps/boot_'+str(i).zfill(4)+'/bao2D.fit.chisq')
			bestFit[0,i+1] = data[12,1]
			bestFit[1,i+1] = data[11,1]
			chi2Boot[i+1]  = dataChi2[0]
			#print '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy/BaoFit_q_f/Bootstraps/boot_'+str(i).zfill(4)+'/bao2D.save.pars'
			#print bestFit[0,i+1], bestFit[1,i+1]
		except Exception:
			print i


	### Get the delta of chi^2
	chi2_2D[ (chi2_2D==0.) ] = float('nan')
	chi2_2D[ (chi2_2D!=0.) ] = chi2_2D[ (chi2_2D!=0.) ]-chi2_bestFit

	
	### Plot the data
	plotChi2Scan(chi2_2D,edge,True, bestFit,'\\alpha_{\\perp}','\\alpha_{\\parallel}','\\Delta \\chi^{2} = \\chi^{2}-\\chi^{2}_{best \\, fit}')

	###
	print numpy.mean(bestFit[0,1:][ bestFit[0,1:]>0. ])
	print numpy.mean(bestFit[1,1:])
	functionChi2 = interpolate.interp2d(alphaPerp_2D, alphaPara_2D, chi2_2D)
	chi2Realisation = numpy.asarray( [ functionChi2(bestFit[0,i],  bestFit[1,i]) for i in range(0,nbBoot+1) ] )

	### Get the Delta chi^2 value for fiducial model
	chi2Fiducial = functionChi2(1.,1.)
	print chi2Fiducial
	print chi2Realisation[ chi2Realisation >= chi2Fiducial ].size

	plt.hist(bestFit[0,1:][ bestFit[0,1:]>0. ], bins=20)
	plt.show()
	plt.hist(bestFit[1,1:][ bestFit[0,1:]>0. ], bins=20)
	plt.show()
	plt.hist(chi2Boot[1:][ bestFit[0,1:]>0. ]-chi2Boot[0], bins=20)
	plt.show()
	plt.hist(chi2Realisation[ bestFit[0,1:]>0. ], bins=20)
	plt.show()

	myTools.fitAGaussian(bestFit[0,1:][ bestFit[0,1:]>0.6 ], 50, [1,0.,1.], False)
	myTools.fitAGaussian(bestFit[0,1:][ bestFit[0,1:]>0.6 ], 50, [1,0.,1.], True)
	myTools.fitAGaussian(bestFit[1,1:][ bestFit[0,1:]>0.6 ], 50, [1,0.,1.], False)
	myTools.fitAGaussian(bestFit[1,1:][ bestFit[0,1:]>0.6 ], 50, [1,0.,1.], True)
	myTools.fitAGaussian(chi2Boot[1:][ bestFit[0,1:]>0.6 ]-chi2Boot[0], 50, [1,0.,50.], False)
	myTools.fitAGaussian(chi2Boot[1:][ bestFit[0,1:]>0.6 ]-chi2Boot[0], 50, [1,0.,50.], True)

	from scipy.stats import chisqprob
	### Get number under one sigma, two sigma ...
	nbParametrDiff = 2
	print '  parameter number difference  = ', nbParametrDiff
	tmp_a = chi2Realisation[1:]
	print tmp_a.size

	print 1.-chisqprob(2.3, 2)
	sigma = [2.30, 6.18, 11.83, 19.33, 28.74]
	
	for i in range(0,5):
		print ' Delta Chi^2 = ', sigma[i], '  sigma = ', i+1, ' nbReal = ', tmp_a[ tmp_a<sigma[i] ].size, '  expected = ', tmp_a.size*(1.-chisqprob(sigma[i], 2)), chisqprob(sigma[i], 2)

	return
def getHistoResiduals():
    """
	
	"""

    ###
    yyy = xi_resAll__[numpy.isfinite(xi_resAll__)]
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.hist(yyy, bins=100)
    plt.ticklabel_format(style="sci", axis="z", scilimits=(0, 0))
    plt.grid()
    plt.xlabel(r"$(\xi_{simu}-\xi_{fit})/\sigma_{simu}$")
    plt.ylabel(r"$\#$")
    plt.rc("font", **{"size": "30"})
    plt.tick_params(axis="both", which="major", labelsize=30)
    plt.grid(True, which="both")
    mng = plt.get_current_fig_manager()
    textstr = "$Nb=%d$ \n $\mu=%.5e  +/-  %.2e$ \n $\sigma=%.5e  +/-  %.2e$" % (
        yyy.size,
        numpy.mean(yyy),
        numpy.std(yyy) / numpy.sqrt(yyy.size),
        numpy.std(yyy),
        numpy.std(yyy) / numpy.sqrt(yyy.size),
    )
    props = dict(boxstyle="round", facecolor="wheat", alpha=0.5)
    ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=30, verticalalignment="top", bbox=props)
    plt.show()

    ### Fit a gaussian
    myTools.fitAGaussian(yyy, 100, [1, 0.0, 1.0], False)
    myTools.fitAGaussian(yyy, 100, [1, 0.0, 1.0], True)

    ### Constants
    nbBins = 100

    ### Plot the residuals as a function of bin index
    xxx = numpy.arange(0.0, nbBin__)
    yyy = xi_dat__ - xi_fit__
    yyy[(xi_fit__ == 0.0)] = 0.0
    yer = xi_err__
    yer[(xi_fit__ == 0.0)] = 0.0
    plt.errorbar(xxx, yyy, yerr=yer, linestyle="", marker="o", color="blue")
    plt.xlabel(r"$index \, bin$")
    plt.ylabel(r"$\xi_{data}-\xi_{fit}$")
    myTools.deal_with_plot(False, False, True)
    plt.show()
    ### Plot the residuals/err as a function of bin index
    xxx = numpy.arange(0.0, nbBin__)
    yyy = (xi_dat__ - xi_fit__) / xi_err__
    yyy[(xi_fit__ == 0.0)] = 0.0
    plt.errorbar(xxx, yyy, linestyle="", marker="o", color="blue")
    plt.xlabel(r"$index \, bin$")
    plt.ylabel(r"$\frac{\xi_{data}-\xi_{fit}}{\sigma}$")
    myTools.deal_with_plot(False, False, True)
    plt.show()

    ### Plot the distribution
    yyy = (xi_dat__[(xi_err__ != 0.0)] - xi_fit__[(xi_err__ != 0.0)]) / xi_err__[(xi_err__ != 0.0)]

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.hist(yyy, bins=nbBins)
    plt.ticklabel_format(style="sci", axis="z", scilimits=(0, 0))
    plt.grid()

    plt.xlabel(r"$\frac{data-fit}{\sigma_{data}}$")
    plt.ylabel(r"$\#$")

    plt.rc("font", **{"size": "30"})
    plt.tick_params(axis="both", which="major", labelsize=30)
    plt.grid(True, which="both")

    mng = plt.get_current_fig_manager()
    # mng.resize(*mng.window.maxsize())
    textstr = "$\mu=%.5e$\n$\sigma=%.5e$" % (numpy.mean(yyy), numpy.std(yyy))
    props = dict(boxstyle="round", facecolor="wheat", alpha=0.5)
    ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=30, verticalalignment="top", bbox=props)

    plt.show()

    # fitAGaussian(yyy, 50, [1,0.,1.],'\\frac{data-fit}{\\sigma_{data}}','\\#')

    return
Beispiel #3
0
def plotBosseBOSS():

	dic_class = {
		'minXi': 0.,
		'maxXi': 200.,
		'nbBin': 50,
		'nbBinM': 25,
		'nb_Sub_Sampling': 80,
		'size_bin_calcul_s': 1.,
		'size_bin_calcul_m': 0.02,
                'nb_wedges'               : 4,
		'remove_residuales' : 'lambda_OBS',
		'index_multipole_max' : 4,
		'path_to_txt_file_folder': '',
		'correlation': 'q_f',
		'f1': 'LYA',
		'f2': '',
		'q1': 'QSO',
		'q2': '',
		'name' : 'Mean \, Simulation'
	}
	dic_simu = {
		'path_to_simu' : '/home/gpfs/manip/mnt0607/bao/hdumasde/Mock_JMLG/v1575/',
		'nb_box' : 10,
		'nb_simu' : 10,
		'prefix' : '_withRand_XYZ_nicolasEstimator'
	}
	#dic_class['path_to_txt_file_folder'] = '/home/gpfs/manip/mnt0607/bao/hdumasde/Mock_JMLG/v1575/Box_000/Simu_000/Results_withRand_XYZ_nicolasEstimator/'
	#corr4 = correlation_3D.Correlation3D(dic_class)
	#corr4.save_list_realisation_simulation(dic_class, dic_simu)
        #corr4.set_values_on_mean_simulation(dic_simu)

	dic_class['q1'] = 'QSO'
        dic_class['remove_residuales'] = 'lambda_OBS'
        #dic_class['path_to_txt_file_folder'] = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy_nicolasEstimator_coAdd_2016_05_26/'
	dic_class['path_to_txt_file_folder'] = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy_nicolasEstimator_2016_05_26/' 
        dic_class['name'] = 'LYA - LYA'
        corr = correlation_3D.Correlation3D(dic_class)
	#corr.save_list_realisation('subsampling', 80)
	corr.set_error_on_covar_matrix('subsampling')
        #print corr._meanZ

	#corr4.print_null_test()
	#print ''
	#corr4.print_null_test('subsampling')
	#print ''
        #corr4.print_null_test('subsampling','_from_fit')

	###
	#dic_class['q1'] = 'QSO'
	#dic_class['remove_residuales'] = 'lambda_OBS'
	#dic_class['path_to_txt_file_folder'] = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy_nicolasEstimator_coAdd_2016_05_26/'
	#dic_class['path_to_txt_file_folder'] = '/home/gpfs/manip/mnt0607/bao/hdumasde/Results/Txt/FitsFile_DR12_Guy_nicolasEstimator_2016_05_26/'
	#dic_class['name'] = 'residuals \, LYA - QSO \, (shuffle \, Forest)'
	#dic_class['name'] = 'Sky - Sky'
	#corr5 = correlation_3D.Correlation3D(dic_class)
	#corr5.set_values_on_mean_simulation_or_realisation_type(None,'randomQSOXYZ')
	#corr5.save_list_realisation('shuffleForest', 1000)
	#corr5.save_list_realisation('subsampling', 80)
	#corr5.set_error_on_covar_matrix('subsampling')
	#print corr5._meanZ

        #corr5.print_null_test()
        #print ''
        #corr5.print_null_test('randomQSOXYZ')
	#print ''
        #corr5.print_null_test('randomQSOXYZ','_from_fit')
	#return

	#corr5.plot_cov_cor_matrix('subsampling','1D')
	#corr5._xi1D[:,1] /= corr5._xi1D[:,2]
	#corr5._xi2D[:,:,1] /= corr5._xi2D[:,:,2]
	#corr5.plot_1d(0)
	#corr5.plot_2d(0)
	#corr5.plot_2d(1)
	#corr4.plot_2d(2)


	###
	corr_1 = copy.deepcopy(corr)
	corr_1._name = 'randomQSO \, XYZ'
	corr_1.set_values_on_mean_simulation_or_realisation_type(None,'randomQSOXYZ')
	###
	corr_2 = copy.deepcopy(corr)
	corr_2._name = 'randomQSO'
        corr_2.set_values_on_mean_simulation_or_realisation_type(None,'randomQSO')
	###
	corr_3 = copy.deepcopy(corr)
	corr_3._name = 'shuffleForest'
        corr_3.set_values_on_mean_simulation_or_realisation_type(None,'shuffleForest')
	###
	corr_4 = copy.deepcopy(corr)
	corr_4._name = 'shuffleQSO'
        corr_4.set_values_on_mean_simulation_or_realisation_type(None,'shuffleQSO')
	
	corr_1.print_null_test()
        print ''
        corr_1.print_null_test('randomQSOXYZ')
        print ''
        corr_1.print_null_test('randomQSOXYZ','_from_fit')

	color = ['blue','red','green','orange','black','blue']
	plt.hist(corr_1._xi2D[:,:,1].flatten()/corr_1._xi2D[:,:,2].flatten(),histtype='step',bins=30,label=r'$'+corr_1._name+'$', color=color[0])
        plt.hist(corr_2._xi2D[:,:,1].flatten()/corr_2._xi2D[:,:,2].flatten(),histtype='step',bins=30,label=r'$'+corr_2._name+'$', color=color[1])
        plt.hist(corr_3._xi2D[:,:,1].flatten()/corr_3._xi2D[:,:,2].flatten(),histtype='step',bins=30,label=r'$'+corr_3._name+'$', color=color[2])
        plt.hist(corr_4._xi2D[:,:,1].flatten()/corr_4._xi2D[:,:,2].flatten(),histtype='step',bins=30,label=r'$'+corr_4._name+'$', color=color[3])
	#plt.hist(corr._xi2D[:,:,1].flatten()/corr._xi2D[:,:,2].flatten(),histtype='step',bins=30,label=r'$'+corr._name+'$',color=color[4])
	plt.show()

	myTools.fitAGaussian( (corr._xi2D[:,:,1]/corr._xi2D[:,:,2]).flatten(), 50,p0=[1,0.,1.],log=True)
	myTools.fitAGaussian( (corr_1._xi2D[:,:,1]/corr_1._xi2D[:,:,2]).flatten(), 50, p0=[1,0.,1.],log=True)
	myTools.fitAGaussian( (corr_2._xi2D[:,:,1]/corr_2._xi2D[:,:,2]).flatten(), 50,p0=[1,0.,1.],log=True)
	myTools.fitAGaussian( (corr_3._xi2D[:,:,1]/corr_3._xi2D[:,:,2]).flatten(), 50, p0=[1,0.,1.],log=True)
	myTools.fitAGaussian( (corr_4._xi2D[:,:,1]/corr_4._xi2D[:,:,2]).flatten(), 50, p0=[1,0.,1.],log=True)




	corr_1.plot_1d(0,[corr_2,corr_3,corr_4],False)
	corr_1.plot_1d(1,[corr_2,corr_3,corr_4],False)
	corr_1.plot_1d(2,[corr_2,corr_3,corr_4],False)
	corr_1.plot_we(0,[corr_2,corr_3,corr_4],False)
	corr_1.plot_we(1,[corr_2,corr_3,corr_4],False)
	corr_1.plot_we(2,[corr_2,corr_3,corr_4],False)


	for i in numpy.arange(0,50):
		corr.plot_slice_2d(i,None,[corr6])


	return