def Fit_IQ_Noise(freq, sweepcomplexIQ): """ Fit Circle Rotate and move data to the origin """ n = len(freq) x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(sweepcomplexIQ.real, sweepcomplexIQ.imag) Z_ii = (x_c + 1j*y_c - sweepcomplexIQ)#*np.exp(-1j * np.arctan(y_c/x_c)) """ ### Fit result and data #Fitter.plot_data_circle(complexIQ.real, complexIQ.imag, x_c, y_c, radius) plt.plot(Z_ii.real, Z_ii.imag) plt.show() """ ### Fit Phase data_phase = np.arctan2(Z_ii.imag, Z_ii.real) rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[n-1]) Z_iir = Z_ii*np.exp(1j * rotate_angle) phase_rotated = np.arctan2(Z_iir.imag, Z_iir.real) theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5 para_phase = theta0_guess, fr_guess, Qr_guess theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase) print fit_report_phase #filename = '20160518_-10dBm_4.99347845_0.0025_707.804mK.csv' #folder = '../../../MeasurementResult/20160516_Nb154nmCry3/' #span = 2e6 #a = SimpleQ(folder, filename, span) #b = MillionQi(folder, filename, span) #result = Fit_SingleKID(folder, filename, span) #result = Fit_SingleKID_Lo(folder, filename, span) #result = Fit_7parameter(folder, filename) #print result
def FuncFitting_alignTwoVar(x, gen, ann): #this function finds the optimal value of gen and ann to minimize the residuals #For the user's inspection: print("Guessing gen=", gen) print("Guessing ann=", ann) Su.rewriteInput_TwoVar(gen, ann) incr()#increase the counter, ready to start the .exe. #Wait for the executable to run, orderToEndAt = 7 folderName = "VaryingTwoVariables/" Su.runExecutable(folderName, orderToEndAt) Chi = np.zeros(6) #calculate the residual of the file retrieved from the same folder Chi[0] = np.sqrt(Fi.Residual(1000, 'slow', str("./calibration_fixed/"+folderName))) Chi[1] = np.sqrt(Fi.Residual( 900, 'slow', str("./calibration_fixed/"+folderName))) Chi[2] = np.sqrt(Fi.Residual( 800, 'slow', str("./calibration_fixed/"+folderName))) Chi[3] = np.sqrt(Fi.Residual( 700, 'slow', str("./calibration_fixed/"+folderName))) Chi[4] = np.sqrt(Fi.Residual(1000, 'fast', str("./calibration_fixed/"+folderName))) Chi[5] = np.sqrt(Fi.Residual( 900, 'fast', str("./calibration_fixed/"+folderName))) return Chi
def Fit_IQ_Sweep(freq, complexIQ): n = len(freq) """ Fit Circle Rotate and move data to the origin """ x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(complexIQ.real, complexIQ.imag) Z_ii = (x_c + 1j*y_c - complexIQ)#*np.exp(-1j * np.arctan(y_c/x_c)) """ ### Fit result and data #Fitter.plot_data_circle(complexIQ.real, complexIQ.imag, x_c, y_c, radius) plt.plot(Z_ii.real, Z_ii.imag) plt.show() """ ### Fit Phase data_phase = np.arctan2(Z_ii.imag, Z_ii.real) rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[n-1]) Z_iir = Z_ii*np.exp(1j * rotate_angle) phase_rotated = np.arctan2(Z_iir.imag, Z_iir.real) theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5 para_phase = theta0_guess, fr_guess, Qr_guess theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase) print fit_report_phase ### Qc and Qc error Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis Qc_err0 = np.sqrt(2*(x_c**4*x_c_err**2 + y_c**4*y_c_err**2))/(x_c**2 + y_c**2) Qc_err1 = np.sqrt(Qc_err0**2/(x_c**2+y_c**2) + radius**2*radius_err**2)/(np.sqrt(x_c**2+y_c**2)+radius) Qc_err2 = np.sqrt(Qc_err1**2 + radius_err**2) Qc_err = np.sqrt(Qc_err2**2 + Qr_err**2) ### Qi and Qi error Qi = Qr * Qc/(Qc - Qr) Qi_err0 = np.sqrt(Qc_err**2 + Qr_err**2) Qi_err1 = np.sqrt(Qc**2*Qc_err**2 + Qr**2*Qr_err**2)/(Qc-Qr) Qi_err = np.sqrt(Qi_err0**2 + Qi_err1**2) return x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report, theta0, theta0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, Qi, Qi_err, fit_report_phase
def Fit_SingleKID_Lo(folder, filename, span): ### Lorentizian fitting result, MeasState = Read_File(folder, filename) freq, linear, phase, mag = Get_Data(result, span) A1, A1_err, A2, A2_err, A3, A3_err, A4, A4_err, fr, fr_err, Qr, Qr_err, report = Fitter.Fit_SkewedLorentizian(freq, mag) fitcurve = [10*np.log10(A1 + A2*(f-fr) + (A3 + A4*(f-fr))/(1 + 4*Qr*Qr*((f-fr)/fr)**2)) for f in freq] minvalue = np.power(10, (np.amin(mag) - np.amax(mag))/10) Qi = Qr / minvalue Qc = Qr*Qi/(Qi - Qr) print Qr, Qi, Qc plt.plot(freq, mag, '.', linewidth=2, label="Data") plt.plot(freq, fitcurve, 'r-', linewidth=1, label="Fit") plt.xlabel('Normalized Frequency') plt.ylabel('S21 (dB)') plt.legend(loc=3) plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) plt.show()
(Q_mixercalibrated - np.mean(Q_mixercalibrated))**2) ### Get index of maximum point and ### cut measurement data 500 points left from the peak and ### 2500 points right from the peak numleft = 500 numright = 2500 index = IQ.argmax() Ievent = I_mixercalibrated[index - numleft:index + numright] Qevent = Q_mixercalibrated[index - numleft:index + numright] amplitude = np.asarray(IQ[index - numleft:index + numright]) phase = np.arctan2(Qevent, Ievent) ### Transfer number to time with microsecond unit t = num[index - numleft:index + numright] / fs * 1e6 a, a_err, A, A_err, t0, t0_err, tau, tau_err, taur, taur_err, fit_report, residual = Fitter.Fit_CosmicRay( t, amplitude) fitresult = Fitter.Fit_CosmicRay_model(t, t0, a, A, tau, taur) ### Plot data and fitting result and residual font = { 'family': 'serif', 'color': 'darkred', 'weight': 'normal', 'size': 10, } with PdfPages(cosraydata_folder + cosraydata_file + '.pdf') as pdf: plt.figure(figsize=(18.5, 10.5)) plt.subplot(2, 1, 1) plt.plot(t, fitresult, '.r')
#Save_KID() #Measure_KID(4.8361742E9, -30) #Save_KID() break print Temp # Heater OFF LS350.SetHeaterRange(0) # Plot both two parts ''' plt.plot(x_array, y_array_real) plt.plot(x_array, y_array_imag) plt.show() ''' ''' x = np.r_[y_array_real] y = np.r_[y_array_imag] fitpara=np.array(Fitter.leastsq_circle(x,y)) Fitter.plot_data_circle(x,y, fitpara[0], fitpara[1], fitpara[2]) plt.show() # Move origin dot to the fitted center of measured data # Rotating and translating to the origin print fitpara x_c = fitpara[0] y_c = fitpara[1] radius = fitpara[2] Z_i = x + 1j*y
import Fitter as Fit import numpy as np import matplotlib.pyplot as plt a = 2 alpha = np.pi / 2. tau = 0 phi0 = 0 f0 = 1e9 Qi = 5e10 Qc = 1e5 Qr = 1. / (1 / Qc + 1 / Qi) freq = np.linspace(f0 * 0.9995, f0 * 1.0005, 1001) curve = Fit.Fit_7para_Model(a, alpha, tau, phi0, f0, Qr, Qc, freq) plt.subplot(2, 2, 1) plt.plot(curve.real, curve.imag) plt.axis('equal') plt.subplot(2, 2, 2) plt.plot(freq / f0, np.arctan2(curve.imag, curve.real), 'r.') plt.xlim([0.9995, 1.0005]) ax = plt.gca() ax.ticklabel_format(useOffset=False) plt.subplot(2, 2, 3) plt.plot(freq / f0, curve.real, 'r.') plt.xlim([0.9995, 1.0005]) ax = plt.gca() ax.ticklabel_format(useOffset=False)
for i in range(0,len(result)-2): freq[i] = float(result[i+2][0]) linear[i] = float(result[i+2][1]) phase[i] = float(result[i+2][2]) real[i] = linear[i] * np.cos(np.deg2rad(phase[i])) imag[i] = linear[i] * np.sin(np.deg2rad(phase[i])) """ plt.plot(freq, linear) plt.plot(freq, phase) plt.plot(freq, real) #plt.plot(real, imag) plt.show() """ ### Fit Circle fitpara=np.array(Fitter.leastsq_circle(real,imag)) Fitter.plot_data_circle(real, imag, fitpara[0], fitpara[1], fitpara[2]) plt.show() print fitpara # Move origin dot to the fitted center of measured data # Rotating and translating to the origin x_c = fitpara[0] y_c = fitpara[1] radius = fitpara[2] Z_i = [0]*(len(result)-2) for i in range(0,len(result)-2): Z_i[i] = real[i] + 1j*imag[i] Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c)) plt.plot(Z_ii.real, Z_ii.imag, 'r.', mew=0.1)
def atmSig(vols, xs, f): return Fitter.cubic(xs, vols, f)
# defining list of (absolute) noise levels -> noise RMSes noiseList = [0.01, 0.2, 0.4, 0.6, 0.8] # running "experiments" with different noise levels for noiseLevel in noiseList: # creating a random (gaussian) noise generator noiseGen = NoiseGenerator(noiseLevel) # creating a "data factory" following the signalShape with some noise given by noiseGen dataGen = DataGenerator(signalShape, 10, (-2, 2), 100, noiseGen) # generating data data = dataGen.generateData() # fitting an appropriate data model and evaluating output fitOutput_A = Fitter.fit(data, polynomial3) statAnalysisOutput_A = StatAnalyser.evaluate(fitOutput_A) # fitting wrong data model and evaluating output fitOutput_B = Fitter.fit(data, wrongSignalShape) statAnalysisOutput_B = StatAnalyser.evaluate(fitOutput_B) # adding both fitting outcomes to printing list plotter.addDataToPlot(data, fitOutput_A, statAnalysisOutput_A, noiseLevel) plotter.addDataToPlot(data, fitOutput_B, statAnalysisOutput_B, noiseLevel) # printing the data plotter.plot()
x_dim = params['x_dim'].value y_dim = params['y_dim'].value angle = params['angle'].value DataDis = np.sqrt((I - x_c)**2 + (Q - y_c)**2) DataAngle = np.arctan2(Q - y_c, I - x_c) Angle = DataAngle + angle r = np.sqrt((x_dim**2 * y_dim**2) / ((x_dim * np.sin(Angle))**2 + (y_dim * np.cos(Angle))**2)) residual = DataDis - r return residual # create a set of Parameters params = Parameters() params.add('x_c', value=0, min=-0.2, max=0.2) params.add('y_c', value=0, min=-0.2, max=0.2) params.add('x_dim', value=0.1, min=0.01, max=0.3) params.add('y_dim', value=0.1, min=0.01, max=0.3) params.add('angle', value=-0.2, min=-np.pi, max=np.pi) # do fit, here with leastsq model result = minimize(EllipseFit, params, args=(I, Q)) print(fit_report(result)) Fitter.plot_data_ellipse(I, Q, result.params['x_c'].value, result.params['y_c'].value, result.params['x_dim'].value, result.params['y_dim'].value, result.params['angle'].value) plt.show()
for freq in range(RF_Freq_Start / RF_Freq_Interval, RF_Freq_End / RF_Freq_Interval + 1): ### read this file name_of_file = RF_Power + '_' + str(freq * 10) + 'MHz.csv' result = [] with open(folder + name_of_file, 'r') as f: for line in f: result.append(map(str, line.split(','))) I = [float(result[i][1]) for i in range(0, len(result))] Q = [ float(result[i][2].replace("\n", "")) for i in range(0, len(result)) ] ### fit I, Q with lmfit and get five values and their 1-sigma standard error x_c, x_c_err, y_c, y_c_err, x_dim, x_dim_err, y_dim, y_dim_err, angle, angle_err = Fitter.Fit_Ellipse( I, Q) ellipse_array.append([ x_c, x_c_err, y_c, y_c_err, x_dim, x_dim_err, y_dim, y_dim_err, angle, angle_err ]) ### A_I = np.sqrt(x_dim**2 * np.cos(angle)**2 + y_dim**2 * np.sin(angle)**2) A_Q = np.sqrt(x_dim**2 * np.sin(angle)**2 + y_dim**2 * np.cos(angle)**2) alpha_1 = np.arctan2(y_dim * np.sin(angle), x_dim * np.cos(angle)) alpha_2 = np.pi - np.arctan2(y_dim * np.cos(angle), x_dim * np.sin(angle)) gamma = alpha_1 - alpha_2 mixer_para_array.append([A_I, A_Q, alpha_1, alpha_2, gamma])
numn = 5 numpy.random.seed(68) m = Model.ModelNNKelly(hidden_layers=[11, 17], output_layer=[ 'ImH', 'ImHu', 'ImHd', 'ReH', 'ReHu', 'ReHd', 'ImHt', 'ImHtu', 'ImHtd', 'ImE', 'ImEu', 'ImEd', 'ReE', 'ReEu', 'ReEd', 'ImEt', 'ImEtu', 'ImEtd' ], useDR=['ReHu', 'ReHd', 'ReEu', 'ReEd'], flavored=['ImH', 'ReH', 'ImHt', 'ImE', 'ReE', 'ImEt']) _lg.info('New model created: {}'.format(m.__class__)) th = Approach.BM10tw2(m) th.name = "fNNDR-J15-p22" f = Fitter.FitterBrain(pts, th, nnets=numn, nbatch=30, minprob=0.0000001) th.fitpoints = f.fitpoints f.verbose = 1 _lg.info('Start fit to {} data points'.format(len(f.fitpoints))) f.fitgood() _lg.info('{} NNs {} {:.1f}/{} p={:.3g}'.format(numn, th.name, *th.chisq(f.fitpoints))) _lg.info('Done. Emailing log file.') utils.mailfile('*****@*****.**', '*****@*****.**', '[CAL] {}: fit {} is done'.format(basename, th.name), logfilename)
'BulkGravToZZToZlepZinv_narrow_3500' : [200,400,4000], 'BulkGravToZZToZlepZinv_narrow_4000' : [200,400,4500], 'BulkGravToZZToZlepZinv_narrow_4500' : [200,400,5000], } fin = ROOT.TFile('study_mt.root') fout = ROOT.TFile("fit_mt.root", 'recreate') c1 = ROOT.TCanvas("c1","c1") c1.SetBottomMargin(0.15) c1.SetLeftMargin(0.15) c1.Print("fit_mt.ps[") # initialize fitter fitter = Fitter(poi = ['x']) h1 = [] h2 = [] h3 = [] h4 = [] lg = [] frame = [] func1 = [] func2 = [] func3 = [] func4 = [] data1 = [] data2 = [] data3 = [] data4 = []
def Fit_7parameterIQ2(freq, comp, para_guess): #Fit Circle #Rotate and move data to the origin n = len(freq) I = comp.real Q = comp.imag x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(I, Q) Z_i = [I[i] + 1j*Q[i] for i in range(0, n)] Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c)) #Fitter.plot_data_circle(Z_ii.real, Z_ii.imag, x_c, y_c, radius) #plt.show() ### Fit Phase data_phase = np.arctan2(Z_ii.imag, Z_ii.real) rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[len(Z_ii)-1]) Z_iic = Z_ii*np.exp(1j * rotate_angle) phase_rotated = np.arctan2(Z_iic.imag, Z_iic.real) ### Phase fit theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5 para_phase = theta0_guess, fr_guess, Qr_guess theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase) print fit_report_phase Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis Qi = Qr * Qc/(Qc - Qr) print Qi """ fitphasecurve = [-theta0 + 2*np.arctan(2 * Qr * (1 - freq[i]/fr)) for i in range(0, n)] plt.plot(freq, phase_rotated) plt.plot(freq, fitphasecurve, 'r') plt.show() """ a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult = Fitter.Fit_7para(freq, comp, para_guess) sevenpara_result = a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult fit7paracurve = a * np.exp(1j*alpha) * np.exp(-2*np.pi*1j*freq*tau) * (1 - (Qr/Qc*np.exp(1j*phi0))/(1 + 2*1j*Qr*(freq-fr)/fr)) #plt.plot(comp.real, comp.imag) #plt.plot(fit7paracurve.real, fit7paracurve.imag, 'r') #plt.show() Qi = sevenpara_result[10] * sevenpara_result[12]/(sevenpara_result[12] - sevenpara_result[10]) print Qi finalresult = [] finalresult.append(a) finalresult.append(a_err/a) finalresult.append(alpha) finalresult.append(alpha_err/alpha) finalresult.append(tau) finalresult.append(tau_err/(tau+1)) finalresult.append(phi0) finalresult.append(phi0_err/phi0) finalresult.append(fr) finalresult.append(fr_err/fr) finalresult.append(Qr) finalresult.append(Qr_err/Qr) finalresult.append(Qc) finalresult.append(Qc_err/Qc) finalresult.append(x_c) finalresult.append(x_c_err/x_c) finalresult.append(y_c) finalresult.append(y_c_err/y_c) finalresult.append(radius) finalresult.append(radius_err/radius) return finalresult
def Fit_7parameterIQ1(freq, comp, tau): a = 1 alpha = 0 phi0 = 0#theta0-np.arctan2(x_c, y_c)+rotate_angle + np.pi estimateparas = [a,alpha,tau,phi0,freq[len(freq)/2],5e4,5e4] a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult = Fitter.Fit_7para(freq, comp, estimateparas) sevenpara_result = a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult #fit7paracurve = a * np.exp(1j*alpha) * np.exp(-2*np.pi*1j*freq*tau) * (1 - (Qr/Qc*np.exp(1j*phi0))/(1 + 2*1j*Qr*(freq-fr)/fr)) #plt.plot(real,imag) #plt.plot(fitIQ.real, fitIQ.imag, '+') #plt.plot(fit7paracurve.real, fit7paracurve.imag, 'r') #plt.show() Qi = sevenpara_result[10] * sevenpara_result[12]/(sevenpara_result[12] - sevenpara_result[10]) print Qi finalresult = [] finalresult.append(a) finalresult.append(a_err/a) finalresult.append(alpha) finalresult.append(alpha_err/alpha) finalresult.append(tau) finalresult.append(tau_err/tau) finalresult.append(phi0) finalresult.append(phi0_err/phi0) finalresult.append(fr) finalresult.append(fr_err/fr) finalresult.append(Qr) finalresult.append(Qr_err/Qr) finalresult.append(Qc) finalresult.append(Qc_err/Qc) finalresult.append(Qi) return finalresult
def Fit_7parameter(folder, filename, span): ### Read data from file result, MeasState = Read_File(folder, filename) freq, linear, phase, mag = Get_Data(result, span) n = len(freq) real = [linear[i] * np.cos(np.deg2rad(phase[i])) for i in range(0, n)] imag = [linear[i] * np.sin(np.deg2rad(phase[i])) for i in range(0, n)] comp = [real[i]+imag[i]*1j for i in range(0, n)] """ Fit Circle Rotate and move data to the origin """ x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(real, imag) Z_i = [real[i] + 1j*imag[i] for i in range(0, n)] Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c)) #Fitter.plot_data_circle(Z_ii.real, Z_ii.imag, x_c, y_c, radius) #plt.show() ### Fit Phase data_phase = np.arctan2(Z_ii.imag, Z_ii.real) rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[len(Z_ii)-1]) Z_iic = Z_ii*np.exp(1j * rotate_angle) phase_rotated = np.arctan2(Z_iic.imag, Z_iic.real) ### Phase fit theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5 para_phase = theta0_guess, fr_guess, Qr_guess theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase) print fit_report_phase Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis Qi = Qr * Qc/(Qc - Qr) print Qi """ fitphasecurve = [-theta0 + 2*np.arctan(2 * Qr * (1 - freq[i]/fr)) for i in range(0, n)] plt.plot(freq, phase_rotated) plt.plot(freq, fitphasecurve, 'r') plt.show() """ #print "theta0", params['theta0'].value #print "Temperature (mK)", MeasState[0], "fr from MagS21 (GHz)", MeasState[1], "fr from phase fit (GHz)", fr #print "Qr", Qr, "Qi", Qi, "Qc", Qc #print "Circle fit residue", circle_residue #, "Phase fit residue", result.residual a = 3.636 alpha = 0.381 tau = 0 phi0 = theta0-np.arctan2(x_c, y_c)+rotate_angle + np.pi estimateparas = [a,alpha,tau,phi0,fr,Qr,Qc] a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult = Fitter.Fit_7para(freq, comp, estimateparas) sevenpara_result = a, a_err, alpha, alpha_err, tau, tau_err, phi0, phi0_err, fr, fr_err, Qr, Qr_err, Qc, Qc_err, sevenparafittingresult fit7paracurve = sevenpara_result[0]*np.exp(1j*sevenpara_result[2]) * np.exp(-2*np.pi*1j*freq*sevenpara_result[4]) * (1 - (sevenpara_result[10]/sevenpara_result[12]*np.exp(1j*sevenpara_result[6]))/(1 + 2*1j*sevenpara_result[10]*(freq-sevenpara_result[8])/sevenpara_result[8])) #plt.plot(real,imag) #plt.plot(fit7paracurve.real, fit7paracurve.imag, 'r') #plt.show() Qi = sevenpara_result[10] * sevenpara_result[12]/(sevenpara_result[12] - sevenpara_result[10]) print Qi finalresult = [] finalresult.append(MeasState[2]) finalresult.append(MeasState[0]) finalresult.append(MeasState[1]) finalresult.append(fr/1e9) finalresult.append(fr_err/fr) finalresult.append(Qr) finalresult.append(Qr_err/Qr) finalresult.append(Qc) finalresult.append(Qi) finalresult.append(filename) return finalresult
I = [float(result[i][1]) for i in range(0,len(result))] Q = [float(result[i][2].replace("\n", "")) for i in range(0,len(result))] ### fit I, Q with lmfit and get five values and their 1-sigma standard error x_c, x_c_err, y_c, y_c_err, x_dim, x_dim_err, y_dim, y_dim_err, angle, angle_err = Fitter.Fit_Ellipse(I,Q) ellipse_array.append([x_c, x_c_err, y_c, y_c_err, x_dim, x_dim_err, y_dim, y_dim_err, angle, angle_err]) ### A_I = np.sqrt(x_dim**2 * np.cos(angle)**2 + y_dim**2 * np.sin(angle)**2) A_Q = np.sqrt(x_dim**2 * np.sin(angle)**2 + y_dim**2 * np.cos(angle)**2) alpha_1 = np.arctan2(y_dim*np.sin(angle), x_dim*np.cos(angle)) alpha_2 = np.pi - np.arctan2(y_dim*np.cos(angle), x_dim*np.sin(angle)) gamma = alpha_1 - alpha_2 mixer_para_array.append([A_I, A_Q, alpha_1, alpha_2, gamma]) ### fit result and plot data Fitter.plot_data_ellipse(I,Q, x_c, y_c, x_dim, y_dim, angle) plt.gca().add_patch(Rectangle((x_c-A_I, y_c-A_Q), 2*A_I, 2*A_Q, fill=None, alpha=1)) plt.plot([x_c+x_dim*np.cos(angle), x_c-x_dim*np.cos(angle)], [y_c-x_dim*np.sin(angle), y_c+x_dim*np.sin(angle)], 'b-', lw=2) plt.plot([x_c+y_dim*np.sin(angle), x_c-y_dim*np.sin(angle)], [y_c+y_dim*np.cos(angle), y_c-y_dim*np.cos(angle)], 'b-', lw=2) plt.plot() ### title for each figure with RF frequency plt.title(str(freq*10) + 'MHz') pdf.savefig() # saves the current figure into a pdf page plt.close() #plt.show() ### save fitting result fitfile = open(folder + fit_file_name + '.csv', 'w') fwrite = csv.writer(fitfile) fwrite.writerow(('freq MHz', 'x_c', 'x_c_err', 'y_c', 'y_c_err', 'x_r', 'x_r_err', 'y_r', 'y_r_err', 'angle', 'angle_err', 'A_I', 'A_Q', 'alpha_1', 'alpha_2', 'gamma')) for i in range(0, len(ellipse_array)):
def map_datatab2structure(self): """If the PEATDB record has a structure, then we allow the user to map each datatab to a specific part of the protein. One can map a datatab to an atom, a residue, a chain, or define a structural group and map to it""" if not self.parent: import tkMessageBox tkMessageBox.showinfo( "No PEAT", "This option is only available when Ekin is started from PEAT", parent=self.ekin_win) return # # Do we have a record name # if not self.protein: import tkMessageBox tkMessageBox.showinfo( "No PEAT record", "This option is only available when Ekin has been started by clicking a PEAT record", parent=self.ekin_win) return # # Is there a structure? # error = None if not self.parent.data.has_key('DBinstance'): error = 1 else: DB = self.parent.data['DBinstance'].DB if not DB[self.protein].has_key('Structure'): error = 1 else: print 'Trying to get PDB' self.pdblines, X = self.parent.get_structure( self.protein, 'Structure') if not self.pdblines: error = 1 if error: import tkMessageBox tkMessageBox.showinfo( "No Structure in PEAT", "This option is only available when the PEAT record has a structure", parent=self.ekin_win) return # # Open the mapping window # mapper_win = Toplevel() mapper_win.title('Map datatab to structure. %s - %s' % (self.protein, self.field)) self.set_geometry(self.ekin_win, mapper_win) # # Mapping Manager # row = 0 Label(mapper_win, text='Mapping Manager', bg='lightblue').grid(row=row, column=0, columnspan=3, sticky='news') row = row + 1 Label(mapper_win, textvariable=self.currentdataset.get()).grid(row=row, column=0, columnspan=3, sticky='news') # # Headers # #row=row+1 #Label(mapper_win,text='Structural group type').grid(row=row,column=0,sticky='news') #Label(mapper_win,text='Structural element').grid(row=row,column=1,sticky='news') #Label(mapper_win,text='Datatab property').grid(row=row,column=2,sticky='news') # # Structural groupings for this protein # #if not DB[self.protein].has_key('structgroups'): # DB[self.protein]['structgroups']={} #structgroups=DB[self.protein]['structgroups'].keys() # # Load the residue definitions # import Protool.mutate self.M_instance = Protool.mutate.Mutate(onlydefs=1) self.AAdefs = self.M_instance.aadefs # # Struct group types # row = row + 1 listbox_height = 5 self.group_type_box = Pmw.ScrolledListBox( mapper_win, items=['Residues', 'Atoms', 'Titratable groups'], labelpos='nw', label_text='Group type', listbox_height=listbox_height, usehullsize=1, hull_width=200, hull_height=100, selectioncommand=self.update_elements) self.group_type_box.grid(row=row, column=0, columnspan=1, sticky='news') self.group_type_box.configure(listbox_bg='white') self.group_type_box.configure(listbox_selectmode='single') self.group_type_box.configure(listbox_exportselection=0) # # # Dropdown list of elements of each structgroup type # self.group_elements_box = Pmw.ScrolledListBox( mapper_win, items=[], labelpos='nw', label_text='Group Elements', listbox_height=listbox_height, usehullsize=1, hull_width=200, hull_height=100) self.group_elements_box.grid(row=row, column=1, columnspan=1, sticky='news') self.group_elements_box.configure(listbox_bg='white') self.group_elements_box.configure(listbox_selectmode='extended') self.group_elements_box.configure(listbox_exportselection=0) # Parameters that we can map to structgroups import Fitter self.FIT = Fitter.FITTER('1 pKa 2 Chemical shifts', self) self.dataprops = ['Data source'] + self.FIT.parameter_names self.data_prop_box = Pmw.ScrolledListBox(mapper_win, items=self.dataprops, labelpos='nw', label_text='Data properties', listbox_height=listbox_height, usehullsize=1, hull_width=200, hull_height=100) self.data_prop_box.grid(row=row, column=2, columnspan=1, sticky='news') self.data_prop_box.configure(listbox_bg='white') self.data_prop_box.configure(listbox_selectmode='extended') self.data_prop_box.configure(listbox_exportselection=0) # # List of existing mappings # row = row + 1 datatab = self.currentdataset.get() print 'Loading this datatab in mapper', datatab mappings = self.get_structmappings(datatab) self.mapping_box = Pmw.ScrolledListBox(mapper_win, items=mappings, labelpos='nw', label_text='Existing mappings', listbox_height=6, usehullsize=1, hull_width=200, hull_height=200) self.mapping_box.grid(row=row, column=0, columnspan=3, sticky='news') self.mapping_box.configure(listbox_selectmode='single') self.mapping_box.configure(listbox_bg='white') # # Buttons # row = row + 1 Button(mapper_win, text='Create mapping', bg='lightgreen', borderwidth=2, relief=GROOVE, command=self.create_mapping).grid(row=row, column=0, sticky='news', padx=2, pady=2) Button(mapper_win, text='Delete mapping', bg='yellow', borderwidth=2, relief=GROOVE, command=self.delete_mapping).grid(row=row, column=1, sticky='news', padx=2, pady=2) Button(mapper_win, text='Export', bg='#CFECEC', borderwidth=2, relief=GROOVE, command=self.export_dialog).grid(row=row, column=2, sticky='news', padx=2, pady=2) row = row + 1 Button(mapper_win, text='Close', borderwidth=2, relief=GROOVE, command=self.close_mapper_window).grid(row=row, column=1, columnspan=2, sticky='news', padx=2, pady=2) # # Structural group manager # #row=row+1 #Label(mapper_win,text='Structural Group Manager',bg='lightblue').grid(row=row,column=0,columnspan=3,sticky='news') #import os, sys #PEAT_dir=os.path.split(__file__)[0] #sys.path.append(PEAT_dir) #import protein_selector #row=row+1 #SEL=protein_selector.select_residue(mapper_win,self.pdblines) #SEL.box.grid(row=row,column=0) ## #row=row+1 #Label(mapper_win,text='Atoms').grid(row=row,column=1) #row=row+1 #Button(mapper_win,text='Create new structural grouping',command=self.create_new_structgroup).grid(row=row,column=0) #Button(mapper_win,text='Add to structural grouping',command=self.add_to_structgroup).grid(row=row,column=1) #Button(mapper_win,text='Close',command=mapper_win.destroy).grid(row=row,column=2,sticky='news') mapper_win.rowconfigure(2, weight=1) self.mapper_win = mapper_win self.mapper_win.transient(master=self.ekin_win) return
manager = JobManager.JobManager(atomList) manager.runLowJobs(toCalculate) manager.runNormalJobs(toCalculate) manager.runDOSJobs(toCalculate) # Create structures.in and structures.holdout files for each atom. uncleFileMaker = MakeUncleFiles.MakeUncleFiles(atomList) uncleFileMaker.makeUncleFiles() # Get all the structs that have been through VASP calculations for each atom. These # should be sorted by formation energy during the work done by makeUncleFiles() [vaspStructs, failedStructs] = uncleFileMaker.getStructureList() structuresInLengths = uncleFileMaker.getStructuresInLengths() # Perform a fit to the VASP data in structures.in for each atom. fitter = Fitter.Fitter(atomList, fitStructs, fitSubsets, structuresInLengths, uncleOutput) fitter.makeFitDirectories() fitter.fitVASPData(iteration) # Perform a ground state search on the fit for each atom. gss = GSS.GSS(atomList, volRange, plotTitle, xlabel, ylabel, uncleOutput) gss.makeGSSDirectories() gss.performGroundStateSearch(iteration) gss.makePlots(iteration) gssStructs = gss.getAllGSSStructures(iteration, failedStructs) # Check the lowest 100 hundred structs from VASP against the lowest 100 structs from UNCLE # for each atom. If they match, then that atom has converged and we remove it from the # lists. removeAtoms = [] removeGss = []
def MillionQi(folder, filename, span): ### Read data from file result, MeasState = Read_File(folder, filename) freq, linear, phase, mag = Get_Data(result, span) n = len(freq) real = [linear[i] * np.cos(np.deg2rad(phase[i])) for i in range(0, n)] imag = [linear[i] * np.sin(np.deg2rad(phase[i])) for i in range(0, n)] complext = np.asarray([real[i] + 1j*imag[i] for i in range(0, n)]) inverset = 1/complext para_guess = 1,0,1e5,1e5,np.median(freq),0 a, a_err, phi0, phi0_err, Qi, Qi_err, Qc_scaled, Qc_scaled_err, fr, fr_err, aphi0, aphi0_err, result, Fit_result = Fitter.Fit_Million(freq, inverset, para_guess) fitcurve = a * (1 + Qi/Qc_scaled * np.exp(1j*phi0) * 1/(1 + 1j*2*Qi*(freq/fr-1)))* np.exp(1j*aphi0) #plt.plot(real, imag,'.') plt.plot(inverset.real, inverset.imag,'.') plt.plot(fitcurve.real, fitcurve.imag,'.r') #plt.plot(freq, real) #plt.plot(freq, inverset.real, 'r') #plt.plot(freq, imag) #plt.plot(freq, inverset.imag, 'r') plt.show() print np.rad2deg(phi0), len(inverset), len(complext)
"data/2011fe_bolom.txt", # Type Ia "data/1994D_bolom.txt", # Type Ia "data/1991T_bolom.txt", # Type Ia "data/1991T-mod_bolom.txt" # Type Ia - removed very late time ] for file in files: m_init = 1.4 * m_sun vsc_init = 0.6e9 data_skip = 1 tries = 0 while True: try: Fitter.fit(file, data_skip=data_skip, kappa_function=kappa_nagy, m_init=m_init, vsc_init=vsc_init, m_max=2 * m_sun) except OverflowError: # something went bad if tries > 10: print("Giving up...") break tries += 1 print( "Something went bad, trying again with randomised initial params" ) m_init = uniform(0.1 * m_sun, 10 * m_sun) vsc_init = uniform(0.01e9, 1e9) continue else: # successful fit, move on to next file break
def Fit_SingleKID(folder, filename, span): ### Read data from file result, MeasState = Read_File(folder, filename) freq, linear, phase, mag = Get_Data(result, span) n = len(freq) real = [linear[i] * np.cos(np.deg2rad(phase[i])) for i in range(0, n)] imag = [linear[i] * np.sin(np.deg2rad(phase[i])) for i in range(0, n)] x_c, x_c_err, y_c, y_c_err, radius, radius_err, circle_fit_report = Fitter.Fit_Circle(real, imag) Z_i = [real[i] + 1j*imag[i] for i in range(0, n)] Z_ii = (x_c + 1j*y_c - Z_i)*np.exp(-1j * np.arctan(y_c/x_c)) #Fitter.plot_data_circle(real, imag, x_c, y_c, radius) #plt.show() ### Fit Phase data_phase = np.arctan2(Z_ii.imag, Z_ii.real) rotate_angle = np.pi-np.angle(Z_ii[0] + Z_ii[len(Z_ii)-1]) Z_iic = Z_ii*np.exp(1j * rotate_angle) phase_rotated = np.arctan2(Z_iic.imag, Z_iic.real) ### Phase fit theta0_guess, fr_guess, Qr_guess = 0, np.median(freq), 1e5 para_phase = theta0_guess, fr_guess, Qr_guess theta0, theta0_err, fr, fr_err, Qr, Qr_err, fit_report_phase = Fitter.Fit_Phase(freq, phase_rotated, para_phase) print fit_report_phase ### Qc and Qc error Qc = (np.absolute(x_c + 1j*y_c) + radius)/2/radius*Qr # Ref: Gao Thesis Qc_err0 = np.sqrt(2*(x_c**4*x_c_err**2 + y_c**4*y_c_err**2))/(x_c**2 + y_c**2) Qc_err1 = np.sqrt(Qc_err0**2/(x_c**2+y_c**2) + radius**2*radius_err**2)/(np.sqrt(x_c**2+y_c**2)+radius) Qc_err2 = np.sqrt(Qc_err1**2 + radius_err**2) Qc_err = np.sqrt(Qc_err2**2 + Qr_err**2) ### Qi and Qi error Qi = Qr * Qc/(Qc - Qr) Qi_err0 = np.sqrt(Qc_err**2 + Qr_err**2) Qi_err1 = np.sqrt(Qc**2*Qc_err**2 + Qr**2*Qr_err**2)/(Qc-Qr) Qi_err = np.sqrt(Qi_err0**2 + Qi_err1**2) print Qi_err, Qc_err, x_c_err, y_c_err, radius_err, Qr_err fitcurve = [-theta0 + 2*np.arctan(2 * Qr * (1 - freq[i]/fr)) for i in range(0, n)] #plt.plot(freq, data_phase) #plt.plot(freq, phase_rotated) #plt.plot(freq, fitcurve, 'r') #plt.show() #print "theta0", params['theta0'].value #print "Temperature (mK)", MeasState[0], "fr from MagS21 (GHz)", MeasState[1], "fr from phase fit (GHz)", fr print "Qr", Qr, "Qi", Qi, "Qc", Qc #print "Circle fit residue", circle_residue #, "Phase fit residue", result.residual finalresult = [] finalresult.append(MeasState[2]) finalresult.append(MeasState[0]) finalresult.append(MeasState[1]) finalresult.append(fr/1e9) finalresult.append(fr_err) finalresult.append(Qr) finalresult.append(Qr_err) finalresult.append(Qc) finalresult.append(Qc_err) finalresult.append(Qi) finalresult.append(Qi_err) finalresult.append(filename) return finalresult
""" Created on Sat Mar 12 2016 Author: Cedric Vallee """ import pandas as pd import csv import Fitter as fitter from sklearn.ensemble import RandomForestClassifier from sklearn.ensemble import GradientBoostingClassifier from sklearn.linear_model import LogisticRegression # Split the global matrix "result" into a training and a testing set df = pd.read_csv('../matrix.csv', sep=',') df = df.fillna(0) train, test = fitter.split(df,0.5) variables = ['MatchDico', 'TextBlob', 'delta_sale', 'delta_at'] target = ['Actual'] # MODEL 1 - Random Forest Classifier rf = RandomForestClassifier(n_estimators=100) print('RANDOM FOREST CLASSIFIER') fitter.fit_model(train, test, target, variables, rf) # MODEL 2 - Gradient Boosting Classifier gb = GradientBoostingClassifier(n_estimators=100) print print('GRADIENT BOOSTING CLASSIFIER') fitter.fit_model(train, test, target, variables, gb) # MODEL 3 - Logistic Regression
def bsVolFitting(): nopts = 11 xs= [90.0,92.0,94.0,96.0,98.0,100.0,102.0,104.0,106.0,108.0,110.0] nxs = np.array(xs) f = 100.0 ws= list(map(lambda x: Utils.dnorm(log(x/f)*10.0), xs)) nws = np.array(ws) t = 0.5 xss = map(lambda x: [f,x,t], xs) a = 0.3 a0 = 0.1 b = 1.0 r = -0.45 r0=0.05 vv = 2.3 vv0 = 1.0 ts= list(map(lambda x: lsig(f, x, t, a, b, r, vv), xs)) nts = np.array(ts) inc = 0.0001 b0 = [r0,vv0] b1up = [r0+inc,vv0] b1dn = [r0-inc,vv0] b2up = [r0,vv0+inc] b2dn = [r0,vv0-inc] atmsig = Fitter.cubic(nxs, nts, f) def err(r, vv, x, y): alpha = alphaFromRhoV1(f, t, atmsig*f, r, vv) return lsig(f, x, t, alpha, b, r, vv) - y def derr(r,vv,x,y): dr=adnumber(r) dvv=adnumber(vv) return np.array([err(dr,vv,x,y).d(dr), err(r,dvv,x,y).d(dvv)]) def sumerrs(ps): return sum(map(lambda x, y, w: w*err(ps[0], ps[1], x, y)**2.0, nxs, nts, nws)) def dsumerrs(ps): dr = adnumber(ps[0]) dvv = adnumber(ps[1]) return np.array([sumerrs([dr, ps[1]]).d(dr), sumerrs([ps[0], dvv]).d(dvv)]) res = minimize(sumerrs, [-0.1,2.0], tol = 0.0000001, jac=dsumerrs, bounds=[(-1.0,1.0), (0.0,10.0)], method='SLSQP', options={'disp': True}) rout = res.x[0] vvout = res.x[1] alphaout = alpha = alphaFromRhoV1(f, t, atmsig*f, rout, vvout) print('xs = ' + str(nxs)) print('ts = ' + str(nts)) fits = list(map(lambda x, y: lsig(f, x, t, alphaout, b, rout, vvout) , nxs, nts)) print('fit = ' + str(fits)) print('errs(fit) = ' + str(sumerrs([rout, vvout]))) print('errs(act) = ' + str(sumerrs([r,vv]))) plt.plot(xs, fits,xs,ts) plt.title('Least-squares fit to noisy data') plt.legend(['Fit', 'Actual']) plt.show() return res
plotter = Plotter("noiseEffectStudy") # defining list of (absolute) noise levels -> noise RMSes noiseList = [0.01, 0.2, 0.4, 0.6, 0.8] # running "experiments" with different noise levels for noiseLevel in noiseList: # creating a random (gaussian) noise generator noiseGen = NoiseGenerator(noiseLevel) # creating a "data factory" following the signalShape with some noise given by noiseGen dataGen = DataGenerator(signalShape, 10, (-2, 2), 100, noiseGen) # generating data data = dataGen.generateData() # fitting an appropriate data model and evaluating output fitOutput_A = Fitter.fit(data, polynomial3) statAnalysisOutput_A = StatAnalyser.evaluate(fitOutput_A) # fitting wrong data model and evaluating output fitOutput_B = Fitter.fit(data, wrongSignalShape) statAnalysisOutput_B = StatAnalyser.evaluate(fitOutput_B) # adding both fitting outcomes to printing list plotter.addDataToPlot(data, fitOutput_A, statAnalysisOutput_A, noiseLevel) plotter.addDataToPlot(data, fitOutput_B, statAnalysisOutput_B, noiseLevel) # printing the data plotter.plot()
from SinusGenerator import * from Fitter import * from Plotter import * print "Program initialized" app = SinusGenerator(0, 9.0, 0.5, 1000) app.generateSinus() app.addNoise() fitter = Fitter(app.getX(), app.getY()) fitter.fitSinus() app.setParams(fitter.getParams()) app.generateSinusWithParams() plot = Plotter(app.getX(), app.getYAfterHit(), app.getY()) print "Thanks for using!"