def Main(i,fitted_data):
    saveDirectory = '/home/kaspar/Desktop/1nmlow'
    fileDirectory = '/home/kaspar/Desktop/1nmlow/'
    pulsetime = 340*10**(-15)     
    
    mybuttonslist = []
    f = ReadAllFiles(fileDirectory)[i]
    data = pd.read_csv(fileDirectory+f) # grab the data
    z = data.iloc[0:,0]/10**3 # z in meters
    ydata = data.iloc[0:,2] #the transmission
    guess = [0.0006,0.25]
    params, params_covariance = optimize.curve_fit(ClosedScan,z, ydata,guess)
    print f
    print params
    print params_covariance
    energy,L,alpah = GetParams(f)
    z0 = params[0]
    q0 = params[1]
    fig, ax = plt.subplots()
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    mycolumn = "PtSe$_2$" +str(L*10**9) +"nm closed scan "+str(energy/10**-9)+"nJ"
    plt.title(mycolumn) # title 
    myscat = plt.scatter(z,ydata)
    improvedZ = np.linspace(-0.05,0.05,500)
    myfit, = plt.plot(improvedZ,ClosedScan(improvedZ,params[0],params[1]),color='red')
    center = plt.axvline(x=0, color='black')
    plt.subplots_adjust(right = 0.75, bottom=0.25)
    a0 = q0
    f0 = z0
    
    
    axcolor = 'lightgoldenrodyellow'
    axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
    axamp  = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
    xvals = plt.axes([0.92, 0.25, 0.03, 0.65], axisbg=axcolor)
    yvals = plt.axes([0.85, 0.25, 0.03, 0.65], axisbg=axcolor)  
    xslider = VertSlider(xvals, 'x offset', -0.01, 0.01, valinit=0,valfmt='%.3f')
    yslider = VertSlider(yvals, 'y offset', -0.3, 0.3, valinit=0,valfmt='%.4f')
    
    
    
    myZ0 = Slider(axfreq, 'Z0', 0.00001, 0.05, valinit=f0,valfmt='%.5f')
    myQ0 = Slider(axamp, 'q0', -1, 1, valinit=a0,valfmt='%.5f')
    
    def update(val):
        z0 = myZ0.val
        q0 = myQ0.val
        yshift = yslider.val
        xshift = xslider.val
        shiftedy = ClosedScan(improvedZ,z0,q0)+yshift
        myfit.set_ydata(shiftedy)
        myfit.set_xdata(improvedZ+xshift)
        fig.canvas.draw_idle()
        
    myZ0.on_changed(update)
    myQ0.on_changed(update)
    xslider.on_changed(update)
    yslider.on_changed(update)
    
    resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
    button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
    def reset(event):
        myZ0.reset()
        myQ0.reset()
        xslider.reset()
        yslider.reset()
    button.on_clicked(reset)
    
    
    
    plt.show(block=False)

    answer = raw_input('Hit enter to continue\n')
    
    z0 = myZ0.val
    q0 = myQ0.val
    yshift = yslider.val
    xshift = xslider.val
    plt.close()
    
    
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    mycolumn = "PtSe$_2$" +str(L*10**9) +"nm closed scan "+str(energy/10**-9)+"nJ"
    plt.title(mycolumn) # title 
    myscat = plt.scatter(z,ydata)
    improvedZ = np.linspace(-0.05,0.05,500)
    myfit, = plt.plot(improvedZ+xshift,ClosedScan(improvedZ,z0,q0)+yshift,color='red')
    z0 = z0*10**3
    plt.text(0.8,0.8,"$Z_0 (mm)$ ="+str('%.2f' % z0)+"\n"+" $\Delta \Phi_0$ = "+str('%.2f' % q0)+'\n' ,horizontalalignment='center',
				    verticalalignment='center',
				    transform = ax.transAxes)
    name = re.sub('correct.csv','',f)
    plt.savefig( saveDirectory+f+"closed.png")
    plt.close()
    
    return button
Ejemplo n.º 2
0
def Main(myfile, fitted_data, saveDirectory, fileDirectory):
    """The main function of the program"""
    f = myfile  # ith file name from list of files in directory
    data = pd.read_csv(fileDirectory + f)  # grab the data
    z = data.iloc[0:, 0] / 10**3  # z in meters
    ydata = data.iloc[0:, 1]  #the transmission

    global energy, L, alpha, leff, pulsetime  # define some globals
    pulsetime = 340 * 10**(-15)  # the pulse time
    energy, L, alpha = GetParams(f)
    leff = L_eff(L, alpha)

    #########################
    # fitting of data

    guess = [0.009, 0.5]  # initial guess
    params, params_covariance = optimize.curve_fit(OpenScanv3, z, ydata,
                                                   guess)  # the fitting

    z0 = params[0]  # 1st param
    b = params[1]  # the NL absorbtion coefficent -> 2nd param
    if z0 < 0:
        z0 = -z0  # make usre it hasnt made z0 negative

    w0 = BeamWaist(z0)  # the beam width
    i0 = Intensity(w0, energy, pulsetime)  # the intensity at focus

    # the one sigma error for the paramters
    perr = np.sqrt(np.diag(params_covariance))
    w0Err = BeamWaistErr(w0, z0, perr[0])
    i0Err = IntensityErr(i0, w0Err)
    bErr = perr[1]

    #print the initial values
    print params
    print perr
    print w0, w0Err
    print i0, i0Err
    print b, bErr

    #create a plot of the transmission and fitted curve
    fig, ax = plt.subplots()
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')

    # example SampleX
    samplename = f.split('_')[0]

    mytitle = str(samplename) + str(L * 10**9) + "nm open scan " + str(
        energy / 10**-9) + "nJ"  # title
    plt.title(mytitle)  # title
    myscat = plt.scatter(z, ydata)  # data scatter
    plt.ylim(ax.get_ylim())
    improvedZ = np.linspace(-0.05, 0.05, 500)  # the x data for the fit
    myfit, = plt.plot(improvedZ,
                      OpenScanv3(improvedZ, params[0], params[1]),
                      color='red')  # plot the fitted curve
    plt.subplots_adjust(right=0.75, bottom=0.25)  # alignment

    # intial slider values
    a0 = b
    f0 = z0

    # make sliders for interative plot
    axcolor = 'lightgoldenrodyellow'
    axz0 = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)  # z0 slider
    axb0 = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)  # beta slider
    xvals = plt.axes([0.92, 0.25, 0.03, 0.65],
                     axisbg=axcolor)  # x and y adjsutment sliders
    yvals = plt.axes([0.85, 0.25, 0.03, 0.65], axisbg=axcolor)
    # slider implementation
    xslider = VertSlider(xvals,
                         'x offset',
                         -0.01,
                         0.01,
                         valinit=0,
                         valfmt='%.3f')
    yslider = VertSlider(yvals,
                         'y offset',
                         -0.3,
                         0.3,
                         valinit=0,
                         valfmt='%.4f')
    myZ0 = Slider(axz0, 'Z0', 0.00001, 0.05, valinit=f0, valfmt='%.5f')
    myB0 = Slider(axb0,
                  'beta',
                  -0.000005,
                  0.000005,
                  valinit=a0,
                  valfmt='%.10f')

    # update function for matplotlibs sliders
    def update(val):
        z0 = myZ0.val
        q0 = myB0.val
        yshift = yslider.val
        xshift = xslider.val
        shiftedy = OpenScanv3(improvedZ, z0, q0) + yshift
        myfit.set_ydata(shiftedy)
        myfit.set_xdata(improvedZ + xshift)
        fig.canvas.draw_idle()

    myZ0.on_changed(update)
    myB0.on_changed(update)
    xslider.on_changed(update)
    yslider.on_changed(update)

    # reset button for sliders
    resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
    button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')

    def reset(event):
        myZ0.reset()
        myB0.reset()
        xslider.reset()
        yslider.reset()

    button.on_clicked(reset)

    #show the plot
    plt.show(block=False)

    # wait for user to finish fitting exit and enter input...
    answer = raw_input('Hit enter to continue\n')

    # retrive slider values
    z0 = myZ0.val
    b = myB0.val
    yshift = yslider.val
    xshift = xslider.val
    # close plot to prevent memory overload
    plt.close()

    # ensure z0 is not less then 0 again
    if z0 < 0:
        z0 = -z0

    w0 = BeamWaist(z0)  # the beam width
    i0 = Intensity(w0, energy, pulsetime)  # the intensity at focus

    # the one sigma error
    perr = np.sqrt(np.diag(params_covariance))
    w0Err = BeamWaistErr(w0, z0, perr[0])
    i0Err = IntensityErr(i0, w0Err)
    bErr = perr[1]

    # print the final versions
    print "Z0 = ", z0, "beta = ", b
    print "err0r = ", perr
    print "w0 = ", w0, w0Err
    print "i0 = ", i0, i0Err
    print "b = ", b, bErr

    #make the final plot
    fig, ax = plt.subplots()
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    plt.title(mytitle)  # title
    plt.scatter(z, ydata)
    improvedZ = np.linspace(-0.05, 0.05, 500)
    myfit, = plt.plot(improvedZ + xshift,
                      OpenScanv3(improvedZ, z0, b) + yshift,
                      color='red')
    # format some of the data into the standard form
    z0 = z0 * 10**3
    w0 = w0 * 10**6
    i0 = i0 / (10**(9) * 100 * 100)
    energy = energy / 10**-9
    L = L * 10**9

    # add some text showing the values
    plt.text(0.8,
             0.8,
             "$Z_0 (mm)$ =" + str('%.2f' % z0) + "\n" + '$w_0(\mu m)$ = ' +
             str('%.2f' % w0),
             horizontalalignment='center',
             verticalalignment='center',
             transform=ax.transAxes)
    plt.text(0.2,
             0.8,
             "$I_0 (GW cm^{-2}) = $" + str('%.2f' % i0) + "\n" +
             r"$\beta = $" + str(b),
             horizontalalignment='center',
             verticalalignment='center',
             transform=ax.transAxes)

    # make a sname for the entry to be put into a csv
    name = str(re.sub('.csv', '', f))  # remove .csv from the title
    plt.savefig(saveDirectory + name + '.png',
                bboxinches='tight')  # save image
    plt.close()

    print name, L, energy, params[0], perr[0], params[1], perr[
        1], z0, b, xshift, yshift, w0, w0Err, i0, i0Err, alpha

    params[0] = params[0] * 10**3  # ensure z0 is in meters for the csv
    perr[0] = perr[0] * 10**3

    # add an entry to the csv
    fitted_data.loc[i] = [
        name, L, energy, params[0], perr[0], params[1], perr[1], z0, b, xshift,
        yshift, w0, w0Err, i0, i0Err, alpha
    ]
    return button  # return button this ensures the plot works (if removed the reset button will not work)
Ejemplo n.º 3
0
def Main(i, fitted_data):
    saveDirectory = '/home/kaspar/Desktop/1nmlow'
    fileDirectory = '/home/kaspar/Desktop/1nmlow/'
    pulsetime = 340 * 10**(-15)

    mybuttonslist = []
    f = ReadAllFiles(fileDirectory)[i]
    data = pd.read_csv(fileDirectory + f)  # grab the data
    z = data.iloc[0:, 0] / 10**3  # z in meters
    ydata = data.iloc[0:, 2]  #the transmission
    guess = [0.0006, 0.25]
    params, params_covariance = optimize.curve_fit(ClosedScan, z, ydata, guess)
    print f
    print params
    print params_covariance
    energy, L, alpah = GetParams(f)
    z0 = params[0]
    q0 = params[1]
    fig, ax = plt.subplots()
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    mycolumn = "PtSe$_2$" + str(L * 10**9) + "nm closed scan " + str(
        energy / 10**-9) + "nJ"
    plt.title(mycolumn)  # title
    myscat = plt.scatter(z, ydata)
    improvedZ = np.linspace(-0.05, 0.05, 500)
    myfit, = plt.plot(improvedZ,
                      ClosedScan(improvedZ, params[0], params[1]),
                      color='red')
    center = plt.axvline(x=0, color='black')
    plt.subplots_adjust(right=0.75, bottom=0.25)
    a0 = q0
    f0 = z0

    axcolor = 'lightgoldenrodyellow'
    axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
    axamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)
    xvals = plt.axes([0.92, 0.25, 0.03, 0.65], axisbg=axcolor)
    yvals = plt.axes([0.85, 0.25, 0.03, 0.65], axisbg=axcolor)
    xslider = VertSlider(xvals,
                         'x offset',
                         -0.01,
                         0.01,
                         valinit=0,
                         valfmt='%.3f')
    yslider = VertSlider(yvals,
                         'y offset',
                         -0.3,
                         0.3,
                         valinit=0,
                         valfmt='%.4f')

    myZ0 = Slider(axfreq, 'Z0', 0.00001, 0.05, valinit=f0, valfmt='%.5f')
    myQ0 = Slider(axamp, 'q0', -1, 1, valinit=a0, valfmt='%.5f')

    def update(val):
        z0 = myZ0.val
        q0 = myQ0.val
        yshift = yslider.val
        xshift = xslider.val
        shiftedy = ClosedScan(improvedZ, z0, q0) + yshift
        myfit.set_ydata(shiftedy)
        myfit.set_xdata(improvedZ + xshift)
        fig.canvas.draw_idle()

    myZ0.on_changed(update)
    myQ0.on_changed(update)
    xslider.on_changed(update)
    yslider.on_changed(update)

    resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
    button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')

    def reset(event):
        myZ0.reset()
        myQ0.reset()
        xslider.reset()
        yslider.reset()

    button.on_clicked(reset)

    plt.show(block=False)

    answer = raw_input('Hit enter to continue\n')

    z0 = myZ0.val
    q0 = myQ0.val
    yshift = yslider.val
    xshift = xslider.val
    plt.close()

    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    mycolumn = "PtSe$_2$" + str(L * 10**9) + "nm closed scan " + str(
        energy / 10**-9) + "nJ"
    plt.title(mycolumn)  # title
    myscat = plt.scatter(z, ydata)
    improvedZ = np.linspace(-0.05, 0.05, 500)
    myfit, = plt.plot(improvedZ + xshift,
                      ClosedScan(improvedZ, z0, q0) + yshift,
                      color='red')
    z0 = z0 * 10**3
    plt.text(0.8,
             0.8,
             "$Z_0 (mm)$ =" + str('%.2f' % z0) + "\n" + " $\Delta \Phi_0$ = " +
             str('%.2f' % q0) + '\n',
             horizontalalignment='center',
             verticalalignment='center',
             transform=ax.transAxes)
    name = re.sub('correct.csv', '', f)
    plt.savefig(saveDirectory + f + "closed.png")
    plt.close()

    return button
def Main(myfile,fitted_data,saveDirectory,fileDirectory):
    """The main function of the program"""
    f =  myfile# ith file name from list of files in directory
    data = pd.read_csv(fileDirectory+f) # grab the data
    z = data.iloc[0:,0]/10**3 # z in meters
    ydata = data.iloc[0:,1] #the transmission    
    
    global energy,L,alpha,leff,pulsetime # define some globals
    pulsetime = 340*10**(-15) # the pulse time
    energy,L,alpha = GetParams(f)
    leff = L_eff(L,alpha)
    
    
    #########################
    # fitting of data
    
    guess= [0.009,0.5] # initial guess
    params, params_covariance = optimize.curve_fit(OpenScanv3,z, ydata, guess) # the fitting
     
    z0 = params[0] # 1st param
    b = params[1] # the NL absorbtion coefficent -> 2nd param
    if z0 < 0:
        z0 = -z0 # make usre it hasnt made z0 negative



    w0 = BeamWaist(z0) # the beam width
    i0 = Intensity(w0,energy,pulsetime) # the intensity at focus        
    
        
    # the one sigma error for the paramters
    perr = np.sqrt(np.diag(params_covariance))        
    w0Err = BeamWaistErr(w0,z0,perr[0])
    i0Err = IntensityErr(i0,w0Err)
    bErr = perr[1]



    #print the initial values
    print params
    print perr
    print w0,w0Err
    print i0,i0Err
    print b,bErr
    
    #create a plot of the transmission and fitted curve
    fig, ax = plt.subplots()
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    
    # example SampleX
    samplename =  f.split('_')[0]    
    
    mytitle =  str(samplename)+str(L*10**9) +"nm open scan "+str(energy/10**-9)+"nJ" # title
    plt.title(mytitle) # title 
    myscat = plt.scatter(z,ydata) # data scatter
    plt.ylim( ax.get_ylim() )  
    improvedZ = np.linspace(-0.05,0.05,500) # the x data for the fit
    myfit, = plt.plot(improvedZ,OpenScanv3(improvedZ,params[0],params[1]),color='red') # plot the fitted curve
    plt.subplots_adjust(right = 0.75, bottom=0.25) # alignment
    
    # intial slider values 
    a0 = b 
    f0 = z0
    
    # make sliders for interative plot
    axcolor = 'lightgoldenrodyellow'
    axz0 = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor) # z0 slider
    axb0  = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor) # beta slider
    xvals = plt.axes([0.92, 0.25, 0.03, 0.65], axisbg=axcolor) # x and y adjsutment sliders
    yvals = plt.axes([0.85, 0.25, 0.03, 0.65], axisbg=axcolor)  
    # slider implementation
    xslider = VertSlider(xvals, 'x offset', -0.01, 0.01, valinit=0,valfmt='%.3f')
    yslider = VertSlider(yvals, 'y offset', -0.3, 0.3, valinit=0,valfmt='%.4f')
    myZ0 = Slider(axz0, 'Z0', 0.00001, 0.05, valinit=f0, valfmt='%.5f')
    myB0 = Slider(axb0, 'beta', -0.000005, 0.000005, valinit=a0,valfmt='%.10f')
    
    
    # update function for matplotlibs sliders
    def update(val):
        z0 = myZ0.val
        q0 = myB0.val
        yshift = yslider.val
        xshift = xslider.val
        shiftedy = OpenScanv3(improvedZ,z0,q0)+yshift
        myfit.set_ydata(shiftedy)
        myfit.set_xdata(improvedZ+xshift)
        fig.canvas.draw_idle()
        
    myZ0.on_changed(update)
    myB0.on_changed(update)
    xslider.on_changed(update)
    yslider.on_changed(update)
    
    
    # reset button for sliders
    resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
    button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')
    def reset(event):
        myZ0.reset()
        myB0.reset()
        xslider.reset()
        yslider.reset()
    button.on_clicked(reset)
    
    
    #show the plot
    plt.show(block=False)
    
    # wait for user to finish fitting exit and enter input...
    answer = raw_input('Hit enter to continue\n')
    
    # retrive slider values
    z0 = myZ0.val
    b = myB0.val
    yshift = yslider.val
    xshift = xslider.val
    # close plot to prevent memory overload    
    plt.close()
    
    # ensure z0 is not less then 0 again
    if z0 < 0:
        z0 = -z0
   
    w0 = BeamWaist(z0) # the beam width
    i0 = Intensity(w0,energy,pulsetime) # the intensity at focus        

    
   	
         
    # the one sigma error
    perr = np.sqrt(np.diag(params_covariance))        
    w0Err = BeamWaistErr(w0,z0,perr[0])
    i0Err = IntensityErr(i0,w0Err)
    bErr = perr[1]

    # print the final versions
    print "Z0 = ",z0,"beta = ",b
    print "err0r = ",perr
    print "w0 = ",w0,w0Err
    print "i0 = ",i0,i0Err
    print "b = ", b,bErr
    
    #make the final plot
    fig, ax = plt.subplots()
    plt.ylabel('Transmission')
    plt.xlabel('z(m)')
    plt.title(mytitle) # title 
    plt.scatter(z,ydata)
    improvedZ = np.linspace(-0.05,0.05,500)
    myfit, = plt.plot(improvedZ+xshift,OpenScanv3(improvedZ,z0,b)+yshift,color='red')
    # format some of the data into the standard form
    z0 = z0*10**3
    w0 = w0*10**6
    i0 = i0/(10**(9)*100*100)
    energy = energy/10**-9
    L = L*10**9
    
    # add some text showing the values
    plt.text(0.8,0.8,"$Z_0 (mm)$ ="+str('%.2f' % z0)+"\n"+'$w_0(\mu m)$ = '+str('%.2f' % w0),horizontalalignment='center',
				    verticalalignment='center',
				    transform = ax.transAxes)
    plt.text(0.2,0.8,"$I_0 (GW cm^{-2}) = $"+str('%.2f' % i0)+"\n"+r"$\beta = $"+ str(b),horizontalalignment='center',
				    verticalalignment='center',
				    transform = ax.transAxes)
    
    # make a sname for the entry to be put into a csv
    name = str(re.sub('.csv','',f)) # remove .csv from the title
    plt.savefig(saveDirectory+name+'.png',bboxinches = 'tight') # save image
    plt.close()
    
    print name,L,energy,params[0],perr[0],params[1],perr[1],z0,b,xshift,yshift,w0,w0Err,i0,i0Err,alpha
    
    
    params[0] = params[0]*10**3 # ensure z0 is in meters for the csv
    perr[0] = perr[0]*10**3
    
    
    
    # add an entry to the csv
    fitted_data.loc[i] = [name,L,energy,params[0],perr[0],params[1],perr[1],z0,b,xshift,yshift,w0,w0Err,i0,i0Err,alpha]
    return button # return button this ensures the plot works (if removed the reset button will not work)