def plotRock(ax, xdat, ydat, yerrdat, norm, labelstr, lc, fc, **kwargs):
    xdat = np.array(xdat)
    ydat = np.array(ydat) /norm
    yerrdat = np.array(yerrdat)/norm

    # We will plot how much above the norm is A2/A1
    # in units of the norm 
    ydat = ydat - 1.
    maxy = ydat.max()
    ax.errorbar( xdat, ydat/maxy, yerr=yerrdat/maxy, \
               capsize=0., elinewidth = 1. ,\
               fmt='.', ecolor=lc, mec=fc, \
               mew=1., ms=5.,\
               marker='o', mfc='lightblue', \
               label=labelstr+', $S_{\mathbf{\pi}}$=%.2g'%(maxy+1))

    Gaus = True 
    if Gaus: 
        # Fit data with a Gaussian
        fitdat = np.transpose( np.vstack( (xdat,ydat/maxy)))
        sig0 = kwargs.pop('sig0', 100.)
        p0 = [1.0, 0.1, sig0]
        fun = fitlibrary.fitdict['GaussianNoOffset'].function
        pG, errorG = fitlibrary.fit_function( p0,  fitdat, fun)
        #print "Fitting with Gaussian:"
        print "...Fitting with sig0=%.2f --> %.2f"%(sig0, pG[2])
        print 
        #print pG
        #print errorG
        fitX, fitY = fitlibrary.plot_function(pG, \
                     np.linspace( xdat.min(), xdat.max(),120),fun) 
        ax.plot(  fitX, fitY, '-', c=lc, lw=1.0)

        erad = True
        if erad: 
            # Returns 1/e^2 radius
            return np.abs(pG[2]), errorG[2]
        else:
            return 2.3548*np.abs(pG[2]), 2.3548*errorG[2]
    else:
        # Fit data with a Lorentzian
        fitdat = np.transpose( np.vstack( (xdat,ydat/maxy)))
        sig0 = kwargs.pop('sig0', 100.)
        print "...Fitting with sig0=%.2f"%sig0
        p0 = [1.0, 0.1, sig0]
        fun = fitlibrary.fitdict['LorentzianNoOffset'].function
        pL, errorL = fitlibrary.fit_function( p0,  fitdat, fun)
        print "Fitting with Lorentzian:"
        print p0
        print pL
        #print errorL
        fitX, fitY = fitlibrary.plot_function(pL, \
                     np.linspace( xdat.min(), xdat.max(),120),fun) 
        ax.plot(  fitX, fitY, '-', c=lc, lw=1.0)
        return np.abs(pL[2]), errorL[2]
def plotRock(ax, xdat, ydat, yerrdat, norm, labelstr, lc, fc):
    xdat = np.array(xdat)
    ydat = np.array(ydat) /norm
    yerrdat = np.array(yerrdat)/norm

    # We will plot how much above the norm is A2/A1
    # in units of the norm 
    ydat = ydat - 1.
    maxy = ydat.max()
    ax.errorbar( xdat, ydat/maxy, yerr=yerrdat/maxy, \
               capsize=0., elinewidth = 1. ,\
               fmt='.', ecolor=lc, mec=fc, \
               mew=1., ms=5.,\
               marker='o', mfc='lightblue', \
               label=labelstr+', $B_{\mathrm{tof}}$=%.2g'%(maxy+1))
    # Fit data with a Gaussian
    fitdat = np.transpose( np.vstack( (xdat,ydat/maxy)))
    p0 = [1.0, 0., 10., 0.1]
    fun = fitlibrary.fitdict['Gaussian'].function
    pG, errorG = fitlibrary.fit_function( p0,  fitdat, fun)
    #print "Fitting with Gaussian:"
    #print pG
    #print errorG
    fitX, fitY = fitlibrary.plot_function(pG, \
                 np.linspace( xdat.min(), xdat.max(),120),fun) 
    ax.plot(  fitX, fitY, '-', c=lc, lw=1.0)
    return np.sqrt(2.)*pG[2], np.sqrt(2.)*errorG[2]
def tof_temperature( points, tfermi, number, i, ax_tof, ax_T, ax_TF, axTFN, legend_dict): 
  shots  = points[:,k['SEQ:shot']] 
  p0 = [ 50., 1/5.] 
  fitfun = fitlibrary.fitdict['Temperature'].function
  
  tofdat = points[:,k['ODT:odttof']]
  sizdat = points[:,k['CPP:ax0w']]
  
  try: 
    Tfit, Terror = fitlibrary.fit_function( p0, points[:, [k['ODT:odttof'],k['CPP:ax0w']]], fitfun) 
    getTcmd = 'getTrange -T %.2f %04d:%04d' % (depth/5., sorted(shots)[0], sorted(shots)[-1] )
    getT = subprocess.Popen(getTcmd.split(), stdout=subprocess.PIPE).communicate()[0]
  except:
    Tfit =  [0., 0.]
    Terror = [0., 0.]
 
  #print points[:, [k['ODT:odttof'],k['CPP:ax0w']]]
  #print "Python Fit:  T = %.3f +/- %.3f uK " % (T, Terr)
  #print "Gnuplot Fit: T = %.3f +/- %.3f uK " % (float(getT.split()[3]), float(getT.split()[4]))
  
  T = unc.ufloat(( Tfit[1], Terror[1]))
 
  TfitX, TfitY = fitlibrary.plot_function( Tfit, tofdat, fitfun, xlim=(0.,6.0))
  ax_tof.plot( tofdat, sizdat, 'o', color=colors[i], markeredgewidth=0.8, markersize=4)
  ax_tof.plot( TfitX, TfitY, '-', color=colors[i], markeredgewidth=0.3, markersize=12, alpha=0.5)

  #******* T TOF
  xT = numpy.array( [points[0,k['FESHBACH:bias']]*6.8 ] )
  yT = numpy.array( [T.nominal_value] )
  yTerr = numpy.array( [T.std_dev()] )
  if yTerr[0] < maxerror* yT[0]:
    for ax in ax_T:
      ax.errorbar(xT, yT, yerr=yTerr, fmt='o', ecolor=colors[i], markeredgecolor=colors[i], markerfacecolor="None", markeredgewidth=2., markersize=8, alpha=1.0)

  #******* T/TF TOF
  TF = T/tfermi
  yT = numpy.array( [TF.nominal_value] )
  yTerr = numpy.array( [TF.std_dev()] )
  if yTerr[0] < maxerror* yT[0]:
    for ax in ax_TF:
      plot1 = ax.errorbar(xT, yT, yerr=yTerr, fmt='o', ecolor=colors[i], markeredgecolor=colors[i], markerfacecolor="None", markeredgewidth=2., markersize=8, alpha=1.0)
      if 'Ballistic Expansion' not in legend_dict.keys():
        legend_dict['Ballistic Expansion'] = plot1

  #******* T/TF vs. N TOF
  xT = numpy.array( [number.nominal_value] )
  yT = numpy.array( [TF.nominal_value] )
  xTerr = numpy.array( [number.std_dev()] ) 
  yTerr = numpy.array( [TF.std_dev()] )
  if yTerr[0] < maxerror* yT[0]:
    axTFN.errorbar(xT,yT, xerr=xTerr, yerr=yTerr, fmt='o', ecolor=colors[i], markeredgecolor=colors[i], markerfacecolor="None", markeredgewidth=2., markersize=8, alpha=1.0)


  return T, TF, TfitX, TfitY
 def fit(self,data):
     fitdata, n = self.limits(data)
     if n == 0:
         display("No points in the specified range [x0:xf], [y0:yf]")
         return None,None
     if self.func == 'Gaussian':
         if not self.dofit:
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.gaus1d_function)
         else:
             display("Fitting to a Gaussian")
             self.a, self.ae=fitlibrary.fit_function(self.a0[:,0],fitdata,fitlibrary.gaus1d_function)
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.gaus1d_function)
     if self.func == 'Sine':
         if not self.dofit:
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.sine_function)
         else:
             display("Fitting to a Sine")
             self.a, self.ae=fitlibrary.fit_function(self.a0[:,0],fitdata,fitlibrary.sine_function)
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.sine_function)
     if self.func == 'ExpSine':
         if not self.dofit:
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.expsine_function)
         else:
             display("Fitting to a ExpSine")
             self.a, self.ae=fitlibrary.fit_function(self.a0[:,0],fitdata,fitlibrary.expsine_function)
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.expsine_function)
     if self.func == 'Temperature':
         if not self.dofit:
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.temperature_function)
         else:
             display("Fitting to a ExpSine")
             self.a, self.ae=fitlibrary.fit_function(self.a0[:,0],fitdata,fitlibrary.temperature_function)
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.temperature_function)
     if self.func == 'Exp':
         if not self.dofit:
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.exp_function)
         else:
             display("Fitting to a Exp")
             self.a, self.ae=fitlibrary.fit_function(self.a0[:,0],fitdata,fitlibrary.exp_function)
             return fitlibrary.plot_function(self.a[:,0] , fitdata[:,0],fitlibrary.exp_function)
Beispiel #5
0
def tof_temperature( points, tfermi, number, i, ax_tof, ax_T, ax_TF, axTFN, legend_dict, xT): 

  shots  = points[:,k['SEQ:shot']] 
  fcpow = points[0,k['EVAP:finalcpow']]
  U0    = points[0,k['ODTCALIB:maxdepth']]
 
  tofdat = points[:,k['ODT:odttof']]
  sizdat = points[:,k['CPP:ax0w']]
  
  mintofindex = tofdat.argmin()
  s_guess = sizdat[mintofindex]
  T_guess =fcpow/10. *U0 / 5.
  print "Fitting TOF Temperature, starting with guess r0 = %.3f, T = %.3f" % (s_guess, T_guess)
  p0 = [ s_guess, T_guess] 
  fitfun = fitlibrary.fitdict['Temperature'].function


  todelete=[]
  tofdat_clean =[]
  sizdat_clean =[]
  for j,tof in enumerate(tofdat):
    if sizdat[j] > 400. or sizdat[j] < 0.:
      todelete.append(j)
    else:
      tofdat_clean.append( tofdat[j] )
      sizdat_clean.append( sizdat[j] ) 

  tofdat = numpy.array(tofdat_clean)
  sizdat = numpy.array(sizdat_clean)

  fitdat= numpy.transpose(numpy.array( [tofdat.tolist(), sizdat.tolist()] ))
  
  try: 
    Tfit, Terror = fitlibrary.fit_function( p0, fitdat, fitfun)
    #getTcmd = 'getTrange -T %.2f %04d:%04d' % (depth/5., sorted(shots)[0], sorted(shots)[-1] )
    #getT = subprocess.Popen(getTcmd.split(), stdout=subprocess.PIPE).communicate()[0]
  except:
    Tfit =  [0., 0.]
    Terror = [0., 0.]
 
  #print points[:, [k['ODT:odttof'],k['CPP:ax0w']]]
  #print "Python Fit:  T = %.3f +/- %.3f uK " % (T, Terr)
  #print "Gnuplot Fit: T = %.3f +/- %.3f uK " % (float(getT.split()[3]), float(getT.split()[4]))
  
  T = unc.ufloat(( Tfit[1], Terror[1]))
 
  TfitX, TfitY = fitlibrary.plot_function( Tfit, tofdat, fitfun, xlim=(0.,6.0))
  ax_tof.plot( tofdat, sizdat, 'o', color=colors[i], markeredgewidth=0.8, markersize=4)
  ax_tof.plot( TfitX, TfitY, '-', color=colors[i], markeredgewidth=0.3, markersize=12, alpha=0.5)

  #******* T TOF
  #xT = numpy.array( [points[0,k['EVAP:image']]/1000. ] )
  yT = numpy.array( [T.nominal_value] )
  yTerr = numpy.array( [T.std_dev()] )
  if yTerr[0] < maxerror* yT[0]:
    for ax in ax_T:
      ax.errorbar(xT, yT, yerr=yTerr, fmt='o', ecolor=colors[i], markeredgecolor=colors[i], markerfacecolor="None", markeredgewidth=2., markersize=8, alpha=1.0)

  #******* T/TF TOF
  TF = T/tfermi
  yT = numpy.array( [TF.nominal_value] )
  yTerr = numpy.array( [TF.std_dev()] )
  if yTerr[0] < maxerror* yT[0]:
    for ax in ax_TF:
      plot1 = ax.errorbar(xT, yT, yerr=yTerr, fmt='o', ecolor=colors[i], markeredgecolor=colors[i], markerfacecolor="None", markeredgewidth=2., markersize=8, alpha=1.0)
      if 'Ballistic Expansion' not in legend_dict.keys():
        legend_dict['Ballistic Expansion'] = plot1

  #******* T/TF vs. N TOF
  xT = numpy.array( [number.nominal_value] )
  yT = numpy.array( [TF.nominal_value] )
  xTerr = numpy.array( [number.std_dev()] ) 
  yTerr = numpy.array( [TF.std_dev()] )
  if yTerr[0] < maxerror* yT[0]:
    axTFN.errorbar(xT,yT, xerr=xTerr, yerr=yTerr, fmt='o', ecolor=colors[i], markeredgecolor=colors[i], markerfacecolor="None", markeredgewidth=2., markersize=8, alpha=1.0)


  return T, TF, TfitX, TfitY
Beispiel #6
0
def plotkey_ratio(
    ax,
    gk,
    key,
    cond1,
    cond2,
    dat,
    base,
    marker="o",
    ms=5.0,
    mec="def",
    mfc="def",
    labelstr="def",
    fit=False,
    save=False,
):
    if mec == "def":
        mec = gdat[gk]["ec"]
    if mfc == "def":
        mfc = gdat[gk]["fc"]
    if labelstr == "def":
        labelstr = gdat[gk]["label"]

    dat1 = np.copy(dat)
    for c in cond1:
        dat1 = dat1[dat1[:, K(c[0])] == c[1]]
    dat2 = np.copy(dat)
    for c in cond2:
        dat2 = dat2[dat2[:, K(c[0])] == c[1]]

    plotkey = "DIMPLELATTICE:knob05"

    x1 = np.unique(dat1[:, K(plotkey)])
    x2 = np.unique(dat2[:, K(plotkey)])

    set1 = set(x1.tolist())
    set2 = set(x2.tolist())
    common = list(set1 & set2)

    num = dat1
    den = dat2
    rval = []
    rerr = []
    for c in common:
        numi = num[num[:, K(plotkey)] == c][:, K(key)]
        deni = den[den[:, K(plotkey)] == c][:, K(key)]
        val = ufloat((np.mean(numi), stats.sem(numi))) / ufloat((np.mean(deni), stats.sem(deni)))
        rval.append(val.nominal_value)
        rerr.append(val.std_dev())
    rval = np.array(rval) / base
    rerr = np.array(rerr) / base
    xc = np.array(common)
    print "\t%s: x points in common = " % key, len(xc)
    if len(xc) > 0:
        ax.errorbar(
            fx(xc),
            rval / base,
            yerr=rerr / base,
            capsize=0.0,
            elinewidth=1.0,
            fmt=".",
            ecolor=mec,
            mec=mec,
            mew=1.0,
            marker=marker,
            mfc=mfc,
            ms=ms,
            label=labelstr,
        )
    if save:
        fname = key.replace(":", "_") + "_" + labelstr[-3:] + ".rawdatNOTCorrected"
        X = np.transpose(np.vstack((fx(xc), rval / base, rerr / base)))
        np.savetxt(fname, X, fmt="%10.2f", delimiter="\t", newline="\n")

    if fit:
        fitdat = np.transpose(np.vstack((fx(xc), rval / base)))
        fun = fitlibrary.fitdict["Gaussian"].function
        p0 = [0.6, 0.01, 20.0, 1.0]
        pFit, error = fitlibrary.fit_function(p0, fitdat, fun)
        fitX, fitY = fitlibrary.plot_function(pFit, fitdat[:, 0], fun)
        # ax.plot( fitX, fitY, '-', color='gray', lw=4.5,zorder=1)
        print "1/e width Gaussian = ", pFit[2]

        fitdat = np.transpose(np.vstack((fx(xc), rval / base - 1.0)))
        fun = fitlibrary.fitdict["GaussianNoOffset"].function
        p0 = [0.5, 0.0, 12.0]
        pFit, error = fitlibrary.fit_function(p0, fitdat, fun)
        pFit = [0.6134, -3.027, 20.7434]  # 190a0 data
        # pFit = [0.5, 0.0, 12.] #400a0
        fitX, fitY = fitlibrary.plot_function(pFit, fitdat[:, 0], fun)
        ax.plot(fitX, fitY + 1.0, "-", color="gray", lw=6.5, zorder=1)
        print "1/e width GaussianNoOffset = ", pFit[2]
        fwhm = 2 * pFit[2] * np.sqrt(-1 * np.log(1.0 / 2.0))
        print "FWHM = ", fwhm
        print "pFit = ", pFit
        ax.text(0.05, 0.05, "FWHM=%.0f mrad" % (fwhm * np.pi / 180.0 * 1000), transform=ax.transAxes, fontsize=10)
    Vpfit, Verror = fitlibrary.fit_function( p0, d[:, [0,2]], fit.function)  

    Hleg = "%s\nwH = %.2f um at %.0f MIL, M2=%.2f" % ( dat, Hpfit[0], Hpfit[1], Hpfit[2] ) 
    Vleg = "wV = %.2f um at %.0f MIL, M2=%.2f" % ( Vpfit[0], Vpfit[1], Vpfit[2] )
   
    astigmatism = Hpfit[1] - Vpfit[1] 
    postdat = numpy.array( [[ i, Hpfit[0], Vpfit[0], astigmatism]] )
    ax2.plot( postdat[:,0], postdat[:,1], 'o', color=colors[i], markeredgewidth=0.3, markersize=12)
    ax2.plot( postdat[:,0], postdat[:,2], 'x', color=colors[i], markeredgewidth=1.0, markersize=12)
    ax3.plot( postdat[:,0], postdat[:,3], '^', markerfacecolor="None", markeredgecolor=colors[i], markeredgewidth=1.0, markersize=12)
    
   
    print "\t" + Hleg
    print "\t" + Vleg 

    HfitX, HfitY = fitlibrary.plot_function( Hpfit, d[:,0], fit.function)
    VfitX, VfitY = fitlibrary.plot_function( Vpfit, d[:,0], fit.function)

    msize=8
    ax1.plot( d[:,0], d[:,1], 'o', color = colors[i], markersize=msize, markeredgewidth=0.3, label=Hleg)
    ax1.plot( d[:,0], d[:,2], 'x', color = colors[i], markersize=msize, markeredgewidth=1.0, label=Vleg )

    ax1.plot( HfitX, HfitY, linestyle = '-', color = colors[i])
    ax1.plot( VfitX, VfitY, linestyle = '-', color = colors[i])
    
    
    Hlegb = "%s\nX Position of Waist" %dat
    Vlegb = "Y Position of Waist" 

    camPixSize = 4.65