def main(): """ NAME plot_2cdfs.py DESCRIPTION makes plots of cdfs of data in input file SYNTAX plot_2cdfs.py [-h][command line options] OPTIONS -h prints help message and quits -f FILE1 FILE2 -t TITLE -fmt [svg,eps,png,pdf,jpg..] specify format of output figure, default is svg """ fmt='svg' title="" if '-h' in sys.argv: print main.__doc__ sys.exit() if '-f' in sys.argv: ind=sys.argv.index('-f') file=sys.argv[ind+1] X=numpy.loadtxt(file) file=sys.argv[ind+2] X2=numpy.loadtxt(file) # else: # X=numpy.loadtxt(sys.stdin,dtype=numpy.float) else: print '-f option required' print main.__doc__ sys.exit() if '-fmt' in sys.argv: ind=sys.argv.index('-fmt') fmt=sys.argv[ind+1] if '-t' in sys.argv: ind=sys.argv.index('-t') title=sys.argv[ind+1] CDF={'X':1} pmagplotlib.plot_init(CDF['X'],5,5) pmagplotlib.plotCDF(CDF['X'],X,'','r','') pmagplotlib.plotCDF(CDF['X'],X2,title,'b','') D,p=scipy.stats.ks_2samp(X,X2) if p<.05: print D,p,' not rejected at 95%' else: print D,p,' rejected at 95%' pmagplotlib.drawFIGS(CDF) ans= raw_input('S[a]ve plot, <Return> to quit ') if ans=='a': files={'X':'CDF_.'+fmt} pmagplotlib.saveP(CDF,files)
def main(): """ NAME plot_cdfs.py DESCRIPTION makes plots of cdfs of data in input file SYNTAX plot_cdfs.py [-h][command line options] OPTIONS -h prints help message and quits -f FILE -t TITLE -fmt [svg,eps,png,pdf,jpg..] specify format of output figure, default is svg """ fmt = "svg" title = "" if "-h" in sys.argv: print main.__doc__ sys.exit() if "-f" in sys.argv: ind = sys.argv.index("-f") file = sys.argv[ind + 1] X = numpy.loadtxt(file) # else: # X=numpy.loadtxt(sys.stdin,dtype=numpy.float) else: print "-f option required" print main.__doc__ sys.exit() if "-fmt" in sys.argv: ind = sys.argv.index("-fmt") fmt = sys.argv[ind + 1] if "-t" in sys.argv: ind = sys.argv.index("-t") title = sys.argv[ind + 1] CDF = {"X": 1} pmagplotlib.plot_init(CDF["X"], 5, 5) pmagplotlib.plotCDF(CDF["X"], X, title, "r", "") pmagplotlib.drawFIGS(CDF) ans = raw_input("S[a]ve plot, <Return> to quit ") if ans == "a": files = {"X": "CDF_." + fmt} pmagplotlib.saveP(CDF, files)
def main(): """ NAME plot_cdfs.py DESCRIPTION makes plots of cdfs of data in input file SYNTAX plot_cdfs.py [-h][command line options] OPTIONS -h prints help message and quits -f FILE -t TITLE """ title="" if '-h' in sys.argv: print main.__doc__ sys.exit() if '-f' in sys.argv: ind=sys.argv.index('-f') specfile=sys.argv[ind+1] if '-t' in sys.argv: ind=sys.argv.index('-t') title=sys.argv[ind+1] X=[] f=open(specfile,'rU') for line in f.readlines(): rec=line.split() X.append(float(rec[0])) CDF={'X':1} pmagplotlib.plot_init(CDF['X'],5,5) pmagplotlib.plotCDF(CDF['X'],X,title,'r','') pmagplotlib.drawFIGS(CDF) try: raw_input('Return to save all figures, cntl-d to quit') except EOFError: print "Good bye" sys.exit() files={} for key in CDF.keys(): files[key]=(key+'.svg') pmagplotlib.saveP(CDF,files)
def main(): """ NAME pick_AC_specimens.py DESCRIPTION finds whether anisotropy correction yeilds more tightly grouped intensities than uncorrected data. picks either all corrected or all uncorrected data and puts in pmag_specimen format file SYNTAX pick_AC_specimens.py [-h][-i][-fu TFILE][-fc AFILE][-F FILE] OPTIONS -h prints help message and quits -i allows interactive setting of file names -fu TFILE uncorrected pmag_specimen format file with thellier interpretations created by thellier_magic_redo.py -fc AFILE anisotropy corrected pmag_specimen format file created by thellier_magic_redo.py -fcr CRIT pmag_criteria.txt format file with acceptance criteria -opt SIG use the optimizer_thelpars.txt file for criteria -F FILE pmag_specimens format output file with "best" set of data DEFAULTS TFILE: thellier_specimens.txt AFILE: AC_specimens.txt FILE: pmag_specimens.txt """ tspec="thellier_specimens.txt" aspec="AC_specimens.txt" ofile="pmag_specimens.txt" critfile="pmag_criteria.txt" ACSamplist,Samplist,sigmin=[],[],10000 GoodSamps,SpecOuts=[],[] P={'cdf':1} pmagplotlib.plot_init(P['cdf'],5,5) # get arguments from command line if '-h' in sys.argv: print main.__doc__ sys.exit() if '-fu' in sys.argv: ind=sys.argv.index('-fu') tspec=sys.argv[ind+1] if '-fc' in sys.argv: ind=sys.argv.index('-fc') aspec=sys.argv[ind+1] if '-fcr' in sys.argv: ind=sys.argv.index('-fcr') critfile=sys.argv[ind+1] if '-opt' in sys.argv: ind=sys.argv.index('-opt') critfile='optimum_thelpars.txt' sigcutoff=sys.argv[ind+1] if '-F' in sys.argv: ind=sys.argv.index('-F') ofile=sys.argv[ind+1] if '-i' in sys.argv: file=raw_input(" thellier_specimnens.txt file [thellier_specimens.txt]: ") if file!="":tfile=file file=raw_input(" AC_specimnens.txt file [AC_specimens.txt]: ") if file!="":afile=file file=raw_input(" pmag_specimnens.txt file [pmag_specimens.txt]: ") if file!="":ofile=file # read in pmag_specimens file Specs,file_type=pmag.magic_read(tspec) Speclist=pmag.get_specs(Specs) ACSpecs,file_type=pmag.magic_read(aspec) ACspeclist=pmag.get_specs(ACSpecs) Crits,file_type=pmag.magic_read(critfile) keys=['specimen_int_mad','specimen_drats','specimen_fvds','specimen_b_beta','specimen_Z','specimen_md','specimen_dang'] accept={} for crit in Crits: if critfile!='optimum_thelpars.txt': if crit['pmag_criteria_code']=='IE-SPEC': for key in keys: accept[key]=float(crit[key]) # assign acceptance criteria break else: if float(crit['sample_int_sigma_perc'])==float(sigcutoff): for key in keys: accept[key]=float(crit[key]) Diff=[] for aspec in ACSpecs: grade,kill=pmag.grade(aspec,accept) if grade==len(accept): print 'AC: ',aspec["er_specimen_name"],'%i'%(1e6*float(aspec["specimen_int"])) aint=(1e6*float(aspec["specimen_int"])) for spec in Specs: if spec["er_specimen_name"]==aspec['er_specimen_name']: print 'UC: ',spec["er_specimen_name"],'%i'%(1e6*float(spec["specimen_int"])) int=(1e6*float(spec["specimen_int"])) Diff.append(100.*abs(aint-int)/aint) x,s=pmag.gausspars(Diff) print x,s Diff.sort() print Diff[0],Diff[-1] pmagplotlib.plotCDF(P['cdf'],Diff,'% Difference','r') pmagplotlib.drawFIGS(P) raw_input()
def main(): """ NAME watsonsV.py DESCRIPTION calculates Watson's V statistic from input files INPUT FORMAT takes dec/inc as first two columns in two space delimited files SYNTAX watsonsV.py [command line options] OPTIONS -h prints help message and quits -f FILE (with optional second) -f2 FILE (second file) -ant, flip antipodal directions to opposite direction in first file if only one file or flip all in second, if two files -P (don't save or show plot) -sav save figure and quit silently -fmt [png,svg,eps,pdf,jpg] format for saved figure OUTPUT Watson's V and the Monte Carlo Critical Value Vc. in plot, V is solid and Vc is dashed. """ Flip=0 show,plot=1,0 fmt='svg' file2="" if '-h' in sys.argv: # check if help is needed print main.__doc__ sys.exit() # graceful quit if '-ant' in sys.argv: Flip=1 if '-sav' in sys.argv: show,plot=0,1 # don't display, but do save plot if '-fmt' in sys.argv: ind=sys.argv.index('-fmt') fmt=sys.argv[ind+1] if '-P' in sys.argv: show=0 # don't display or save plot if '-f' in sys.argv: ind=sys.argv.index('-f') file1=sys.argv[ind+1] data=numpy.loadtxt(file1).transpose() D1=numpy.array([data[0],data[1]]).transpose() else: print "-f is required" print main.__doc__ sys.exit() if '-f2' in sys.argv: ind=sys.argv.index('-f2') file2=sys.argv[ind+1] data2=numpy.loadtxt(file2).transpose() D2=numpy.array([data2[0],data2[1]]).transpose() if Flip==1: D2,D=pmag.flip(D2) # D2 are now flipped if len(D2)!=0: if len(D)!=0: D2=numpy.concatenate(D,D2) # put all in D2 elif len(D)!=0: D2=D else: print 'length of second file is zero' sys.exit() elif Flip==1:D2,D1=pmag.flip(D1) # peel out antipodal directions, put in D2 # counter,NumSims=0,5000 # # first calculate the fisher means and cartesian coordinates of each set of Directions # pars_1=pmag.fisher_mean(D1) pars_2=pmag.fisher_mean(D2) # # get V statistic for these # V=pmag.vfunc(pars_1,pars_2) # # do monte carlo simulation of datasets with same kappas, but common mean # Vp=[] # set of Vs from simulations if show==1:print "Doing ",NumSims," simulations" for k in range(NumSims): counter+=1 if counter==50: if show==1:print k+1 counter=0 Dirp=[] # get a set of N1 fisher distributed vectors with k1, calculate fisher stats for i in range(pars_1["n"]): Dirp.append(pmag.fshdev(pars_1["k"])) pars_p1=pmag.fisher_mean(Dirp) # get a set of N2 fisher distributed vectors with k2, calculate fisher stats Dirp=[] for i in range(pars_2["n"]): Dirp.append(pmag.fshdev(pars_2["k"])) pars_p2=pmag.fisher_mean(Dirp) # get the V for these Vk=pmag.vfunc(pars_p1,pars_p2) Vp.append(Vk) # # sort the Vs, get Vcrit (95th one) # Vp.sort() k=int(.95*NumSims) if show==1: print "Watson's V, Vcrit: " print ' %10.1f %10.1f'%(V,Vp[k]) if show==1 or plot==1: CDF={'cdf':1} pmagplotlib.plot_init(CDF['cdf'],5,5) pmagplotlib.plotCDF(CDF['cdf'],Vp,"Watson's V",'r',"") pmagplotlib.plotVs(CDF['cdf'],[V],'g','-') pmagplotlib.plotVs(CDF['cdf'],[Vp[k]],'b','--') if plot==0:pmagplotlib.drawFIGS(CDF) files={} if file2!="": files['cdf']='WatsonsV_'+file1+'_'+file2+'.'+fmt else: files['cdf']='WatsonsV_'+file1+'.'+fmt if pmagplotlib.isServer: black = '#000000' purple = '#800080' titles={} titles['cdf']='Cumulative Distribution' CDF = pmagplotlib.addBorders(CDF,titles,black,purple) pmagplotlib.saveP(CDF,files) elif plot==0: ans=raw_input(" S[a]ve to save plot, [q]uit without saving: ") if ans=="a": pmagplotlib.saveP(CDF,files) if plot==1: # save and quit silently pmagplotlib.saveP(CDF,files)
def watson_common_mean(Data1,Data2,NumSims=5000,plot='no'): """ Conduct a Watson V test for a common mean on two declination, inclination data sets This function calculates Watson's V statistic from input files through Monte Carlo simulation in order to test whether two populations of directional data could have been drawn from a common mean. The critical angle between the two sample mean directions and the corresponding McFadden and McElhinny (1990) classification is printed. Required Arguments ---------- Data1 : a list of directional data [dec,inc] Data2 : a list of directional data [dec,inc] Optional Arguments ---------- NumSims : number of Monte Carlo simulations (default is 5000) plot : the default is no plot ('no'). Putting 'yes' will the plot the CDF from the Monte Carlo simulations. """ pars_1=pmag.fisher_mean(Data1) pars_2=pmag.fisher_mean(Data2) cart_1=pmag.dir2cart([pars_1["dec"],pars_1["inc"],pars_1["r"]]) cart_2=pmag.dir2cart([pars_2['dec'],pars_2['inc'],pars_2["r"]]) Sw=pars_1['k']*pars_1['r']+pars_2['k']*pars_2['r'] # k1*r1+k2*r2 xhat_1=pars_1['k']*cart_1[0]+pars_2['k']*cart_2[0] # k1*x1+k2*x2 xhat_2=pars_1['k']*cart_1[1]+pars_2['k']*cart_2[1] # k1*y1+k2*y2 xhat_3=pars_1['k']*cart_1[2]+pars_2['k']*cart_2[2] # k1*z1+k2*z2 Rw=np.sqrt(xhat_1**2+xhat_2**2+xhat_3**2) V=2*(Sw-Rw) # keep weighted sum for later when determining the "critical angle" # let's save it as Sr (notation of McFadden and McElhinny, 1990) Sr=Sw # do monte carlo simulation of datasets with same kappas as data, # but a common mean counter=0 Vp=[] # set of Vs from simulations for k in range(NumSims): # get a set of N1 fisher distributed vectors with k1, # calculate fisher stats Dirp=[] for i in range(pars_1["n"]): Dirp.append(pmag.fshdev(pars_1["k"])) pars_p1=pmag.fisher_mean(Dirp) # get a set of N2 fisher distributed vectors with k2, # calculate fisher stats Dirp=[] for i in range(pars_2["n"]): Dirp.append(pmag.fshdev(pars_2["k"])) pars_p2=pmag.fisher_mean(Dirp) # get the V for these Vk=pmag.vfunc(pars_p1,pars_p2) Vp.append(Vk) # sort the Vs, get Vcrit (95th percentile one) Vp.sort() k=int(.95*NumSims) Vcrit=Vp[k] # equation 18 of McFadden and McElhinny, 1990 calculates the critical # value of R (Rwc) Rwc=Sr-(Vcrit/2) # following equation 19 of McFadden and McElhinny (1990) the critical # angle is calculated. If the observed angle (also calculated below) # between the data set means exceeds the critical angle the hypothesis # of a common mean direction may be rejected at the 95% confidence # level. The critical angle is simply a different way to present # Watson's V parameter so it makes sense to use the Watson V parameter # in comparison with the critical value of V for considering the test # results. What calculating the critical angle allows for is the # classification of McFadden and McElhinny (1990) to be made # for data sets that are consistent with sharing a common mean. k1=pars_1['k'] k2=pars_2['k'] R1=pars_1['r'] R2=pars_2['r'] critical_angle=np.degrees(np.arccos(((Rwc**2)-((k1*R1)**2) -((k2*R2)**2))/ (2*k1*R1*k2*R2))) D1=(pars_1['dec'],pars_1['inc']) D2=(pars_2['dec'],pars_2['inc']) angle=pmag.angle(D1,D2) print "Results of Watson V test: " print "" print "Watson's V: " '%.1f' %(V) print "Critical value of V: " '%.1f' %(Vcrit) if V<Vcrit: print '"Pass": Since V is less than Vcrit, the null hypothesis' print 'that the two populations are drawn from distributions' print 'that share a common mean direction can not be rejected.' elif V>Vcrit: print '"Fail": Since V is greater than Vcrit, the two means can' print 'be distinguished at the 95% confidence level.' print "" print "M&M1990 classification:" print "" print "Angle between data set means: " '%.1f'%(angle) print "Critical angle for M&M1990: " '%.1f'%(critical_angle) if V>Vcrit: print "" elif V<Vcrit: if critical_angle<5: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'A'" elif critical_angle<10: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'B'" elif critical_angle<20: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'C'" else: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'INDETERMINATE;" if plot=='yes': CDF={'cdf':1} #pmagplotlib.plot_init(CDF['cdf'],5,5) p1 = pmagplotlib.plotCDF(CDF['cdf'],Vp,"Watson's V",'r',"") p2 = pmagplotlib.plotVs(CDF['cdf'],[V],'g','-') p3 = pmagplotlib.plotVs(CDF['cdf'],[Vp[k]],'b','--') pmagplotlib.drawFIGS(CDF)
def bootstrap_common_mean(Data1,Data2,NumSims=1000): """ Conduct a bootstrap test (Tauxe, 2010) for a common mean on two declination, inclination data sets This function modifies code from PmagPy for use calculating and plotting bootstrap statistics. Three plots are generated (one for x, one for y and one for z). If the 95 percent confidence bounds for each component overlap each other, the two directions are not significantly different. Arguments ---------- Data1 : a list of directional data [dec,inc] Data2 : a list of directional data [dec,inc] NumSims : number of bootstrap samples (default is 1000) """ counter=0 BDI1=pmag.di_boot(Data1) BDI2=pmag.di_boot(Data2) cart1= pmag.dir2cart(BDI1).transpose() X1,Y1,Z1=cart1[0],cart1[1],cart1[2] cart2= pmag.dir2cart(BDI2).transpose() X2,Y2,Z2=cart2[0],cart2[1],cart2[2] print "Here are the results of the bootstrap test for a common mean:" fignum = 1 fig = plt.figure(figsize=(9,3)) fig = plt.subplot(1,3,1) minimum = int(0.025*len(X1)) maximum = int(0.975*len(X1)) X1,y=pmagplotlib.plotCDF(fignum,X1,"X component",'r',"") bounds1=[X1[minimum],X1[maximum]] pmagplotlib.plotVs(fignum,bounds1,'r','-') X2,y=pmagplotlib.plotCDF(fignum,X2,"X component",'b',"") bounds2=[X2[minimum],X2[maximum]] pmagplotlib.plotVs(fignum,bounds2,'b','--') plt.ylim(0,1) plt.subplot(1,3,2) Y1,y=pmagplotlib.plotCDF(fignum,Y1,"Y component",'r',"") bounds1=[Y1[minimum],Y1[maximum]] pmagplotlib.plotVs(fignum,bounds1,'r','-') Y2,y=pmagplotlib.plotCDF(fignum,Y2,"Y component",'b',"") bounds2=[Y2[minimum],Y2[maximum]] pmagplotlib.plotVs(fignum,bounds2,'b','--') plt.ylim(0,1) plt.subplot(1,3,3) Z1,y=pmagplotlib.plotCDF(fignum,Z1,"Z component",'r',"") bounds1=[Z1[minimum],Z1[maximum]] pmagplotlib.plotVs(fignum,bounds1,'r','-') Z2,y=pmagplotlib.plotCDF(fignum,Z2,"Z component",'b',"") bounds2=[Z2[minimum],Z2[maximum]] pmagplotlib.plotVs(fignum,bounds2,'b','--') plt.ylim(0,1) plt.tight_layout() plt.show()
def main(): """ NAME revtest_MM1990.py DESCRIPTION calculates Watson's V statistic from input files through Monte Carlo simulation in order to test whether normal and reversed populations could have been drawn from a common mean (equivalent to watsonV.py). Also provides the critical angle between the two sample mean directions and the corresponding McFadden and McElhinny (1990) classification. INPUT FORMAT takes dec/inc as first two columns in two space delimited files (one file for normal directions, one file for reversed directions). SYNTAX revtest_MM1990.py [command line options] OPTIONS -h prints help message and quits -f FILE -f2 FILE -P (don't plot the Watson V cdf) OUTPUT Watson's V between the two populations and the Monte Carlo Critical Value Vc. M&M1990 angle, critical angle and classification Plot of Watson's V CDF from Monte Carlo simulation (red line), V is solid and Vc is dashed. """ D1, D2 = [], [] plot = 1 Flip = 1 if "-h" in sys.argv: # check if help is needed print main.__doc__ sys.exit() # graceful quit if "-P" in sys.argv: plot = 0 if "-f" in sys.argv: ind = sys.argv.index("-f") file1 = sys.argv[ind + 1] f1 = open(file1, "rU") for line in f1.readlines(): rec = line.split() Dec, Inc = float(rec[0]), float(rec[1]) D1.append([Dec, Inc, 1.0]) f1.close() if "-f2" in sys.argv: ind = sys.argv.index("-f2") file2 = sys.argv[ind + 1] f2 = open(file2, "rU") print "be patient, your computer is doing 5000 simulations..." for line in f2.readlines(): rec = line.split() Dec, Inc = float(rec[0]), float(rec[1]) D2.append([Dec, Inc, 1.0]) f2.close() # take the antipode for the directions in file 2 D2_flip = [] for rec in D2: d, i = (rec[0] - 180.0) % 360.0, -rec[1] D2_flip.append([d, i, 1.0]) pars_1 = pmag.fisher_mean(D1) pars_2 = pmag.fisher_mean(D2_flip) cart_1 = pmag.dir2cart([pars_1["dec"], pars_1["inc"], pars_1["r"]]) cart_2 = pmag.dir2cart([pars_2["dec"], pars_2["inc"], pars_2["r"]]) Sw = pars_1["k"] * pars_1["r"] + pars_2["k"] * pars_2["r"] # k1*r1+k2*r2 xhat_1 = pars_1["k"] * cart_1[0] + pars_2["k"] * cart_2[0] # k1*x1+k2*x2 xhat_2 = pars_1["k"] * cart_1[1] + pars_2["k"] * cart_2[1] # k1*y1+k2*y2 xhat_3 = pars_1["k"] * cart_1[2] + pars_2["k"] * cart_2[2] # k1*z1+k2*z2 Rw = numpy.sqrt(xhat_1 ** 2 + xhat_2 ** 2 + xhat_3 ** 2) V = 2 * (Sw - Rw) # # keep weighted sum for later when determining the "critical angle" let's save it as Sr (notation of McFadden and McElhinny, 1990) # Sr = Sw # # do monte carlo simulation of datasets with same kappas, but common mean # counter, NumSims = 0, 5000 Vp = [] # set of Vs from simulations for k in range(NumSims): # # get a set of N1 fisher distributed vectors with k1, calculate fisher stats # Dirp = [] for i in range(pars_1["n"]): Dirp.append(pmag.fshdev(pars_1["k"])) pars_p1 = pmag.fisher_mean(Dirp) # # get a set of N2 fisher distributed vectors with k2, calculate fisher stats # Dirp = [] for i in range(pars_2["n"]): Dirp.append(pmag.fshdev(pars_2["k"])) pars_p2 = pmag.fisher_mean(Dirp) # # get the V for these # Vk = pmag.vfunc(pars_p1, pars_p2) Vp.append(Vk) # # sort the Vs, get Vcrit (95th percentile one) # Vp.sort() k = int(0.95 * NumSims) Vcrit = Vp[k] # # equation 18 of McFadden and McElhinny, 1990 calculates the critical value of R (Rwc) # Rwc = Sr - (Vcrit / 2) # # following equation 19 of McFadden and McElhinny (1990) the critical angle is calculated. # k1 = pars_1["k"] k2 = pars_2["k"] R1 = pars_1["r"] R2 = pars_2["r"] critical_angle = numpy.degrees( numpy.arccos(((Rwc ** 2) - ((k1 * R1) ** 2) - ((k2 * R2) ** 2)) / (2 * k1 * R1 * k2 * R2)) ) D1_mean = (pars_1["dec"], pars_1["inc"]) D2_mean = (pars_2["dec"], pars_2["inc"]) angle = pmag.angle(D1_mean, D2_mean) # # print the results of the test # print "" print "Results of Watson V test: " print "" print "Watson's V: " "%.1f" % (V) print "Critical value of V: " "%.1f" % (Vcrit) if V < Vcrit: print '"Pass": Since V is less than Vcrit, the null hypothesis that the two populations are drawn from distributions that share a common mean direction (antipodal to one another) cannot be rejected.' elif V > Vcrit: print '"Fail": Since V is greater than Vcrit, the two means can be distinguished at the 95% confidence level.' print "" print "M&M1990 classification:" print "" print "Angle between data set means: " "%.1f" % (angle) print "Critical angle of M&M1990: " "%.1f" % (critical_angle) if V > Vcrit: print "" elif V < Vcrit: if critical_angle < 5: print "The McFadden and McElhinny (1990) classification for this test is: 'A'" elif critical_angle < 10: print "The McFadden and McElhinny (1990) classification for this test is: 'B'" elif critical_angle < 20: print "The McFadden and McElhinny (1990) classification for this test is: 'C'" else: print "The McFadden and McElhinny (1990) classification for this test is: 'INDETERMINATE;" if plot == 1: CDF = {"cdf": 1} pmagplotlib.plot_init(CDF["cdf"], 5, 5) p1 = pmagplotlib.plotCDF(CDF["cdf"], Vp, "Watson's V", "r", "") p2 = pmagplotlib.plotVs(CDF["cdf"], [V], "g", "-") p3 = pmagplotlib.plotVs(CDF["cdf"], [Vp[k]], "b", "--") pmagplotlib.drawFIGS(CDF) files, fmt = {}, "svg" if file2 != "": files["cdf"] = "WatsonsV_" + file1 + "_" + file2 + "." + fmt else: files["cdf"] = "WatsonsV_" + file1 + "." + fmt if pmagplotlib.isServer: black = "#000000" purple = "#800080" titles = {} titles["cdf"] = "Cumulative Distribution" CDF = pmagplotlib.addBorders(CDF, titles, black, purple) pmagplotlib.saveP(CDF, files) else: ans = raw_input(" S[a]ve to save plot, [q]uit without saving: ") if ans == "a": pmagplotlib.saveP(CDF, files)
def watson_common_mean(Data1, Data2, NumSims=5000, plot='no'): """ Conduct a Watson V test for a common mean on two declination, inclination data sets This function calculates Watson's V statistic from input files through Monte Carlo simulation in order to test whether two populations of directional data could have been drawn from a common mean. The critical angle between the two sample mean directions and the corresponding McFadden and McElhinny (1990) classification is printed. Required Arguments ---------- Data1 : a list of directional data [dec,inc] Data2 : a list of directional data [dec,inc] Optional Arguments ---------- NumSims : number of Monte Carlo simulations (default is 5000) plot : the default is no plot ('no'). Putting 'yes' will the plot the CDF from the Monte Carlo simulations. """ pars_1 = pmag.fisher_mean(Data1) pars_2 = pmag.fisher_mean(Data2) cart_1 = pmag.dir2cart([pars_1["dec"], pars_1["inc"], pars_1["r"]]) cart_2 = pmag.dir2cart([pars_2['dec'], pars_2['inc'], pars_2["r"]]) Sw = pars_1['k'] * pars_1['r'] + pars_2['k'] * pars_2['r'] # k1*r1+k2*r2 xhat_1 = pars_1['k'] * cart_1[0] + pars_2['k'] * cart_2[0] # k1*x1+k2*x2 xhat_2 = pars_1['k'] * cart_1[1] + pars_2['k'] * cart_2[1] # k1*y1+k2*y2 xhat_3 = pars_1['k'] * cart_1[2] + pars_2['k'] * cart_2[2] # k1*z1+k2*z2 Rw = np.sqrt(xhat_1**2 + xhat_2**2 + xhat_3**2) V = 2 * (Sw - Rw) # keep weighted sum for later when determining the "critical angle" # let's save it as Sr (notation of McFadden and McElhinny, 1990) Sr = Sw # do monte carlo simulation of datasets with same kappas as data, # but a common mean counter = 0 Vp = [] # set of Vs from simulations for k in range(NumSims): # get a set of N1 fisher distributed vectors with k1, # calculate fisher stats Dirp = [] for i in range(pars_1["n"]): Dirp.append(pmag.fshdev(pars_1["k"])) pars_p1 = pmag.fisher_mean(Dirp) # get a set of N2 fisher distributed vectors with k2, # calculate fisher stats Dirp = [] for i in range(pars_2["n"]): Dirp.append(pmag.fshdev(pars_2["k"])) pars_p2 = pmag.fisher_mean(Dirp) # get the V for these Vk = pmag.vfunc(pars_p1, pars_p2) Vp.append(Vk) # sort the Vs, get Vcrit (95th percentile one) Vp.sort() k = int(.95 * NumSims) Vcrit = Vp[k] # equation 18 of McFadden and McElhinny, 1990 calculates the critical # value of R (Rwc) Rwc = Sr - (Vcrit / 2) # following equation 19 of McFadden and McElhinny (1990) the critical # angle is calculated. If the observed angle (also calculated below) # between the data set means exceeds the critical angle the hypothesis # of a common mean direction may be rejected at the 95% confidence # level. The critical angle is simply a different way to present # Watson's V parameter so it makes sense to use the Watson V parameter # in comparison with the critical value of V for considering the test # results. What calculating the critical angle allows for is the # classification of McFadden and McElhinny (1990) to be made # for data sets that are consistent with sharing a common mean. k1 = pars_1['k'] k2 = pars_2['k'] R1 = pars_1['r'] R2 = pars_2['r'] critical_angle = np.degrees( np.arccos(((Rwc**2) - ((k1 * R1)**2) - ((k2 * R2)**2)) / (2 * k1 * R1 * k2 * R2))) D1 = (pars_1['dec'], pars_1['inc']) D2 = (pars_2['dec'], pars_2['inc']) angle = pmag.angle(D1, D2) print "Results of Watson V test: " print "" print "Watson's V: " '%.1f' % (V) print "Critical value of V: " '%.1f' % (Vcrit) if V < Vcrit: print '"Pass": Since V is less than Vcrit, the null hypothesis' print 'that the two populations are drawn from distributions' print 'that share a common mean direction can not be rejected.' elif V > Vcrit: print '"Fail": Since V is greater than Vcrit, the two means can' print 'be distinguished at the 95% confidence level.' print "" print "M&M1990 classification:" print "" print "Angle between data set means: " '%.1f' % (angle) print "Critical angle for M&M1990: " '%.1f' % (critical_angle) if V > Vcrit: print "" elif V < Vcrit: if critical_angle < 5: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'A'" elif critical_angle < 10: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'B'" elif critical_angle < 20: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'C'" else: print "The McFadden and McElhinny (1990) classification for" print "this test is: 'INDETERMINATE;" if plot == 'yes': CDF = {'cdf': 1} #pmagplotlib.plot_init(CDF['cdf'],5,5) p1 = pmagplotlib.plotCDF(CDF['cdf'], Vp, "Watson's V", 'r', "") p2 = pmagplotlib.plotVs(CDF['cdf'], [V], 'g', '-') p3 = pmagplotlib.plotVs(CDF['cdf'], [Vp[k]], 'b', '--') pmagplotlib.drawFIGS(CDF)
def bootstrap_common_mean(Data1, Data2, NumSims=1000): """ Conduct a bootstrap test (Tauxe, 2010) for a common mean on two declination, inclination data sets This function modifies code from PmagPy for use calculating and plotting bootstrap statistics. Three plots are generated (one for x, one for y and one for z). If the 95 percent confidence bounds for each component overlap each other, the two directions are not significantly different. Arguments ---------- Data1 : a list of directional data [dec,inc] Data2 : a list of directional data [dec,inc] NumSims : number of bootstrap samples (default is 1000) """ counter = 0 BDI1 = pmag.di_boot(Data1) BDI2 = pmag.di_boot(Data2) cart1 = pmag.dir2cart(BDI1).transpose() X1, Y1, Z1 = cart1[0], cart1[1], cart1[2] cart2 = pmag.dir2cart(BDI2).transpose() X2, Y2, Z2 = cart2[0], cart2[1], cart2[2] print "Here are the results of the bootstrap test for a common mean:" fignum = 1 fig = plt.figure(figsize=(9, 3)) fig = plt.subplot(1, 3, 1) minimum = int(0.025 * len(X1)) maximum = int(0.975 * len(X1)) X1, y = pmagplotlib.plotCDF(fignum, X1, "X component", 'r', "") bounds1 = [X1[minimum], X1[maximum]] pmagplotlib.plotVs(fignum, bounds1, 'r', '-') X2, y = pmagplotlib.plotCDF(fignum, X2, "X component", 'b', "") bounds2 = [X2[minimum], X2[maximum]] pmagplotlib.plotVs(fignum, bounds2, 'b', '--') plt.ylim(0, 1) plt.subplot(1, 3, 2) Y1, y = pmagplotlib.plotCDF(fignum, Y1, "Y component", 'r', "") bounds1 = [Y1[minimum], Y1[maximum]] pmagplotlib.plotVs(fignum, bounds1, 'r', '-') Y2, y = pmagplotlib.plotCDF(fignum, Y2, "Y component", 'b', "") bounds2 = [Y2[minimum], Y2[maximum]] pmagplotlib.plotVs(fignum, bounds2, 'b', '--') plt.ylim(0, 1) plt.subplot(1, 3, 3) Z1, y = pmagplotlib.plotCDF(fignum, Z1, "Z component", 'r', "") bounds1 = [Z1[minimum], Z1[maximum]] pmagplotlib.plotVs(fignum, bounds1, 'r', '-') Z2, y = pmagplotlib.plotCDF(fignum, Z2, "Z component", 'b', "") bounds2 = [Z2[minimum], Z2[maximum]] pmagplotlib.plotVs(fignum, bounds2, 'b', '--') plt.ylim(0, 1) plt.tight_layout() plt.show()
def main(): """ NAME find_EI.py DESCRIPTION Applies series of assumed flattening factor and "unsquishes" inclinations assuming tangent function. Finds flattening factor that gives elongation/inclination pair consistent with TK03. Finds bootstrap confidence bounds SYNTAX find_EI.py [command line options] OPTIONS -h prints help message and quits -i allows interactive input of file name -f FILE specify input file name -nb N specify number of bootstraps - the more the better, but slower!, default is 1000 -fmt [svg,png,eps,pdf..] change plot format, default is svg INPUT dec/inc pairs, delimited with space or tabs OUTPUT four plots: 1) equal area plot of original directions 2) Elongation/inclination pairs as a function of f, data plus 25 bootstrap samples 3) Cumulative distribution of bootstrapped optimal inclinations plus uncertainties. Estimate from original data set plotted as solid line 4) Orientation of principle direction through unflattening NOTE: If distribution does not have a solution, plot labeled: Pathological. Some bootstrap samples may have valid solutions and those are plotted in the CDFs and E/I plot. """ fmt,nb='svg',1000 if '-i' in sys.argv: file=raw_input("Enter file name for processing: ") elif '-f' in sys.argv: ind=sys.argv.index('-f') file=sys.argv[ind+1] else: print main.__doc__ sys.exit() if '-nb' in sys.argv: ind=sys.argv.index('-nb') nb=int(sys.argv[ind+1]) if '-fmt' in sys.argv: ind=sys.argv.index('-fmt') fmt=sys.argv[ind+1] data=numpy.loadtxt(file) upper,lower=int(round(.975*nb)),int(round(.025*nb)) E,I=[],[] PLTS={'eq':1,'ei':2,'cdf':3,'v2':4} pmagplotlib.plot_init(PLTS['eq'],6,6) pmagplotlib.plot_init(PLTS['ei'],5,5) pmagplotlib.plot_init(PLTS['cdf'],5,5) pmagplotlib.plot_init(PLTS['v2'],5,5) pmagplotlib.plotEQ(PLTS['eq'],data,'Data') pmagplotlib.drawFIGS(PLTS) ppars=pmag.doprinc(data) Io=ppars['inc'] n=ppars["N"] Es,Is,Fs,V2s=find_f(data) Inc,Elong=Is[-1],Es[-1] pmagplotlib.plotEI(PLTS['ei'],Es,Is,Fs[-1]) pmagplotlib.plotV2s(PLTS['v2'],V2s,Is,Fs[-1]) b=0 print "Bootstrapping.... be patient" while b<nb: bdata=pmag.pseudo(data) Es,Is,Fs,V2s=find_f(bdata) if b<25: pmagplotlib.plotEI(PLTS['ei'],Es,Is,Fs[-1]) if Es[-1]!=0: ppars=pmag.doprinc(bdata) I.append(abs(Is[-1])) E.append(Es[-1]) b+=1 if b%25==0:print b,' out of ',nb I.sort() E.sort() Eexp=[] for i in I: Eexp.append(EI(i)) if Inc==0: title= 'Pathological Distribution: '+'[%7.1f, %7.1f]' %(I[lower],I[upper]) else: title= '%7.1f [%7.1f, %7.1f]' %( Inc, I[lower],I[upper]) pmagplotlib.plotEI(PLTS['ei'],Eexp,I,1) pmagplotlib.plotCDF(PLTS['cdf'],I,'Inclinations','r',title) pmagplotlib.plotVs(PLTS['cdf'],[I[lower],I[upper]],'b','--') pmagplotlib.plotVs(PLTS['cdf'],[Inc],'g','-') pmagplotlib.plotVs(PLTS['cdf'],[Io],'k','-') pmagplotlib.drawFIGS(PLTS) print "Io Inc I_lower, I_upper, Elon, E_lower, E_upper" print '%7.1f %s %7.1f _ %7.1f ^ %7.1f: %6.4f _ %6.4f ^ %6.4f' %(Io, " => ", Inc, I[lower],I[upper], Elong, E[lower],E[upper]) ans= raw_input("S[a]ve plots - <return> to quit: ") if ans!='a': print "\n Good bye\n" sys.exit() files={} files['eq']='findEI_eq.'+fmt files['ei']='findEI_ei.'+fmt files['cdf']='findEI_cdf.'+fmt files['v2']='findEI_v2.'+fmt pmagplotlib.saveP(PLTS,files)
def main(): """ NAME watsonsV.py DESCRIPTION calculates Watson's V statistic from input files INPUT FORMAT takes dec/inc as first two columns in two space delimited files SYNTAX watsonsV.py [command line options] OPTIONS -h prints help message and quits -f FILE (with optional second) -f2 FILE (second file) -ant, flip antipodal directions in FILE to opposite direction -P (don't plot) OUTPUT Watson's V and the Monte Carlo Critical Value Vc. in plot, V is solid and Vc is dashed. """ D1,D2=[],[] Flip=0 plot=1 if '-h' in sys.argv: # check if help is needed print main.__doc__ sys.exit() # graceful quit if '-ant' in sys.argv: Flip=1 if '-P' in sys.argv: plot=0 if '-f' in sys.argv: ind=sys.argv.index('-f') file1=sys.argv[ind+1] f=open(file1,'rU') for line in f.readlines(): rec=line.split() Dec,Inc=float(rec[0]),float(rec[1]) D1.append([Dec,Inc,1.]) f.close() if '-f2' in sys.argv: ind=sys.argv.index('-f2') file2=sys.argv[ind+1] f=open(file2,'rU') for line in f.readlines(): if '\t' in line: rec=line.split('\t') # split each line on space to get records else: rec=line.split() # split each line on space to get records Dec,Inc=float(rec[0]),float(rec[1]) if Flip==0: D2.append([Dec,Inc,1.]) else: D1.append([Dec,Inc,1.]) f.close() if Flip==1: D1,D2=pmag.flip(D1) # counter,NumSims=0,5000 # # first calculate the fisher means and cartesian coordinates of each set of Directions # pars_1=pmag.fisher_mean(D1) pars_2=pmag.fisher_mean(D2) # # get V statistic for these # V=pmag.vfunc(pars_1,pars_2) # # do monte carlo simulation of datasets with same kappas, but common mean # Vp=[] # set of Vs from simulations if plot==1:print "Doing ",NumSims," simulations" for k in range(NumSims): counter+=1 if counter==50: if plot==1:print k+1 counter=0 Dirp=[] # get a set of N1 fisher distributed vectors with k1, calculate fisher stats for i in range(pars_1["n"]): Dirp.append(pmag.fshdev(pars_1["k"])) pars_p1=pmag.fisher_mean(Dirp) # get a set of N2 fisher distributed vectors with k2, calculate fisher stats Dirp=[] for i in range(pars_2["n"]): Dirp.append(pmag.fshdev(pars_2["k"])) pars_p2=pmag.fisher_mean(Dirp) # get the V for these Vk=pmag.vfunc(pars_p1,pars_p2) Vp.append(Vk) # # sort the Vs, get Vcrit (95th one) # Vp.sort() k=int(.95*NumSims) print "Watson's V, Vcrit: " print ' %10.1f %10.1f'%(V,Vp[k]) if plot==1: CDF={'cdf':1} pmagplotlib.plot_init(CDF['cdf'],5,5) pmagplotlib.plotCDF(CDF['cdf'],Vp,"Watson's V",'r',"") pmagplotlib.plotVs(CDF['cdf'],[V],'g','-') pmagplotlib.plotVs(CDF['cdf'],[Vp[k]],'b','--') pmagplotlib.drawFIGS(CDF) files,fmt={},'svg' if file2!="": files['cdf']='WatsonsV_'+file1+'_'+file2+'.'+fmt else: files['cdf']='WatsonsV_'+file1+'.'+fmt if pmagplotlib.isServer: black = '#000000' purple = '#800080' titles={} titles['cdf']='Cumulative Distribution' CDF = pmagplotlib.addBorders(CDF,titles,black,purple) pmagplotlib.saveP(CDF,files) else: ans=raw_input(" S[a]ve to save plot, [q]uit without saving: ") if ans=="a": pmagplotlib.saveP(CDF,files)
def main(): """ NAME find_EI.py DESCRIPTION Applies assumed flattening factor and "unsquishes" inclinations assuming tangent function. Finds flattening factor that gives elongation/inclination pair consistent with TK03. Finds bootstrap confidence bounds SYNTAX find_EI.py [-h][-i] [-f FILE] INPUT dec/inc pairs OUTPUT three plots: 1) equal area plot of original directions 2) Elongation/inclination pairs as a function of f, data plus 25 bootstrap samples 3) Cumulative distribution of bootstrapped optimal inclinations plus uncertainties. Estimate from original data set plotted as solid line """ if '-i' in sys.argv: file=raw_input("Enter file name for processing: ") f=open(file,'rU') elif '-f' in sys.argv: ind=sys.argv.index('-f') file=sys.argv[ind+1] f=open(file,'rU') else: print main.__doc__ sys.exit() rseed,nb,data=10,5000,[] upper,lower=int(round(.975*nb)),int(round(.025*nb)) E,I=[],[] PLTS={'eq':1,'ei':2,'cdf':3,'v2':4} pmagplotlib.plot_init(PLTS['eq'],6,6) pmagplotlib.plot_init(PLTS['ei'],5,5) pmagplotlib.plot_init(PLTS['cdf'],5,5) pmagplotlib.plot_init(PLTS['v2'],5,5) random.seed(rseed) for line in f.readlines(): rec=line.split() dec=float(rec[0]) inc=float(rec[1]) rec=[dec,inc,1.] data.append(rec) pmagplotlib.plotEQ(PLTS['eq'],data,'Data') ppars=pmag.doprinc(data) Io=ppars['inc'] n=ppars["N"] Es,Is,Fs,V2s=find_f(data) Inc,Elong=Is[-1],Es[-1] pmagplotlib.plotEI(PLTS['ei'],Es,Is,Fs[-1]) pmagplotlib.plotV2s(PLTS['v2'],V2s,Is,Fs[-1]) b=0 print "Bootstrapping.... be patient" while b<nb: bdata=[] for j in range(n): boot=random.randint(0,n-1) random.jumpahead(rseed) bdata.append(data[boot]) Es,Is,Fs,V2s=find_f(bdata) if b<25: pmagplotlib.plotEI(PLTS['ei'],Es,Is,Fs[-1]) if Es[-1]!=0: ppars=pmag.doprinc(bdata) I.append(abs(Is[-1])) E.append(Es[-1]) b+=1 if b%25==0:print b,' out of ',nb I.sort() E.sort() Eexp=[] for i in I: Eexp.append(EI(i)) title= '%7.1f [%7.1f, %7.1f]' %( Inc, I[lower],I[upper]) pmagplotlib.plotEI(PLTS['ei'],Eexp,I,1) pmagplotlib.plotCDF(PLTS['cdf'],I,'Inclinations','r',title) pmagplotlib.plotVs(PLTS['cdf'],[I[lower],I[upper]],'b','--') pmagplotlib.plotVs(PLTS['cdf'],[Inc],'g','-') pmagplotlib.plotVs(PLTS['cdf'],[Io],'k','-') print "Io Inc I_lower, I_upper, Elon, E_lower, E_upper" print '%7.1f %s %7.1f _ %7.1f ^ %7.1f: %6.4f _ %6.4f ^ %6.4f' %(Io, " => ", Inc, I[lower],I[upper], Elong, E[lower],E[upper]) try: raw_input("Return to save plots - <return> to quit: ") except EOFError: print "\n Good bye\n" sys.exit() files={} files['eq']='findEI_eq.svg' files['ei']='findEI_ei.svg' files['cdf']='findEI_cdf.svg' files['v2']='findEI_v2.svg' pmagplotlib.saveP(PLTS,files)
def common_dir_MM90(dir1,dir2,NumSims=5000,plot='no'): dir1['r']=get_R(dir1) dir2['r']=get_R(dir2) #largely based on iWatsonV routine of Swanson-Hyell in IPMag cart_1=pmag.dir2cart([dir1["dec"],dir1["inc"],dir1["r"]]) cart_2=pmag.dir2cart([dir2['dec'],dir2['inc'],dir2["r"]]) Sw=dir1['k']*dir1['r']+dir2['k']*dir2['r'] # k1*r1+k2*r2 xhat_1=dir1['k']*cart_1[0]+dir2['k']*cart_2[0] # k1*x1+k2*x2 xhat_2=dir1['k']*cart_1[1]+dir2['k']*cart_2[1] # k1*y1+k2*y2 xhat_3=dir1['k']*cart_1[2]+dir2['k']*cart_2[2] # k1*z1+k2*z2 Rw=np.sqrt(xhat_1**2+xhat_2**2+xhat_3**2) V=2*(Sw-Rw) # keep weighted sum for later when determining the "critical angle" # let's save it as Sr (notation of McFadden and McElhinny, 1990) Sr=Sw # do monte carlo simulation of datasets with same kappas as data, # but a common mean counter=0 Vp=[] # set of Vs from simulations for k in range(NumSims): # get a set of N1 fisher distributed vectors with k1, # calculate fisher stats Dirp=[] for i in range(int(dir1["n"])): Dirp.append(pmag.fshdev(dir1["k"])) pars_p1=pmag.fisher_mean(Dirp) # get a set of N2 fisher distributed vectors with k2, # calculate fisher stats Dirp=[] for i in range(int(dir2["n"])): Dirp.append(pmag.fshdev(dir2["k"])) pars_p2=pmag.fisher_mean(Dirp) # get the V for these Vk=pmag.vfunc(pars_p1,pars_p2) Vp.append(Vk) # sort the Vs, get Vcrit (95th percentile one) Vp.sort() k=int(.95*NumSims) Vcrit=Vp[k] # equation 18 of McFadden and McElhinny, 1990 calculates the critical # value of R (Rwc) Rwc=Sr-(Vcrit/2) # following equation 19 of McFadden and McElhinny (1990) the critical # angle is calculated. If the observed angle (also calculated below) # between the data set means exceeds the critical angle the hypothesis # of a common mean direction may be rejected at the 95% confidence # level. The critical angle is simply a different way to present # Watson's V parameter so it makes sense to use the Watson V parameter # in comparison with the critical value of V for considering the test # results. What calculating the critical angle allows for is the # classification of McFadden and McElhinny (1990) to be made # for data sets that are consistent with sharing a common mean. k1=dir1['k'] k2=dir2['k'] R1=dir1['r'] R2=dir2['r'] critical_angle=np.degrees(np.arccos(((Rwc**2)-((k1*R1)**2) -((k2*R2)**2))/ (2*k1*R1*k2*R2))) D1=(dir1['dec'],dir1['inc']) D2=(dir2['dec'],dir2['inc']) angle=pmag.angle(D1,D2) if V<Vcrit: outcome='Pass' if critical_angle<5: MM90class='A' elif critical_angle<10: MM90class='B' elif critical_angle<20: MM90class='C' else: MM90class='INDETERMINATE' else: outcome='Fail' MM90class='FAIL' result=pd.Series([outcome,V,Vcrit,angle[0],critical_angle,MM90class], index=['Outcome','VWatson','Vcrit','angle','critangle','MM90class']) if plot=='yes': CDF={'cdf':1} #pmagplotlib.plot_init(CDF['cdf'],5,5) p1 = pmagplotlib.plotCDF(CDF['cdf'],Vp,"Watson's V",'r',"") p2 = pmagplotlib.plotVs(CDF['cdf'],[V],'g','-') p3 = pmagplotlib.plotVs(CDF['cdf'],[Vp[k]],'b','--') pmagplotlib.drawFIGS(CDF) return result