def __init__(self, *args): unittest.TestCase.__init__(self, *args) self.x = dnp.linspace(0, 5, 20) dnp.random.seed(12347) self.n = dnp.random.randn(20) * 0.1 self.y = 3.2 * self.x + 0.35 + self.n self.z = 3.2 * self.x * self.x - 12.2 * self.x + 0.35 + self.n
def __init__(self, *args): unittest.TestCase.__init__(self, *args) self.x = dnp.linspace(0, 5, 20) dnp.random.seed(12347) self.n = dnp.random.randn(20)*0.1 self.y = 3.2*self.x + 0.35 + self.n self.z = 3.2*self.x*self.x - 12.2*self.x + 0.35 + self.n
def testSpace(self): print "test space" dds = np.linspace(0, 11, 12) self.checkitems(range(12), dds) dds = np.linspace(0, 11, 12, dtype=np.int32) self.checkitems(range(12), dds, toInt) dds = np.linspace(0, 5, 11) self.checkitems([0.5*i for i in range(11)], dds) dds = np.linspace(0, 5, 11, dtype=np.int32) self.checkitems([i/2 for i in range(11)], dds, toInt) import math dds = np.logspace(0,2,5) self.checkitems([math.pow(10.,0.5*i) for i in range(5)], dds) dds = np.logspace(0,2,5, dtype=np.int32) self.checkitems([1, 3, 10, 31, 100], dds, toInt)
def get_chi_steps(two_theta, starting_chi_value=100.0, final_chi_value=0.0): theta = dnp.radians(two_theta / 2.0) # converts to radians and halfs to theta radius = LENGTH * float(dnp.sin(theta)) delta_chi = float(dnp.rad2deg((WIDTH / radius))) number_of_steps = dnp.abs(final_chi_value-starting_chi_value)/(delta_chi*0.5) number_of_steps = int(number_of_steps)+1 return dnp.linspace(starting_chi_value, final_chi_value, number_of_steps)
def demoRealDataFitting(): m6Data=dnp.io.load("/dls/i06-1/data/2012/si7816-1/68917.dat", formats=['srs'], asdict=True); # m6Data=dnp.io.load("/dls/i06-1/data/2012/si7816-1/68918.dat", formats=['srs'], asdict=True); x, y=m6Data['m6qg'], m6Data['ca62sr']; # [mu, sigma, peak, gf] = fineGaussianFitting(y, x, "Plot 2"); [mu, sigma, peak, gf] = fineGaussianFitting(y, x); # One vertical line on the peak point: # xx, yy=dnp.array([mu-1, mu+1]), dnp.array([0, peak]); xx, yy=dnp.array([mu-1, mu, mu+1]), dnp.array([0, peak, 0]); xxx, yyy=dnp.array([mu]), dnp.array([peak]); # To find the closest data point to the peak; cPos=(dnp.abs(x-mu)).minpos()[0]; cX, cY=x[cPos], y[cPos]; print("To plot the fitted data." ) x1=dnp.linspace(x[0], x[x.size-1], 500); y1=myGaussianFunc(mu, sigma, peak, [x1]); #Line plot does not work, #dnp.plot.line(x, [y, y1] ) # plot line of evaluated function #dnp.plot.updateline(xx, yy) dnp.plot.points(x, y, None, 5); sleep(1); dnp.plot.addpoints(x1, y1, None, 1) sleep(1); dnp.plot.addpoints(xxx, yyy, None, 10); return [mu, sigma, peak, gf];
def testSpace(self): print "test space" dds = np.linspace(0, 11, 12) self.checkitems(range(12), dds) dds = np.linspace(0, 11, 12, dtype=np.int32) self.checkitems(range(12), dds, toInt) dds = np.linspace(0, 5, 11) self.checkitems([0.5 * i for i in range(11)], dds) dds = np.linspace(0, 5, 11, dtype=np.int32) self.checkitems([i / 2 for i in range(11)], dds, toInt) import math dds = np.logspace(0, 2, 5) self.checkitems([math.pow(10., 0.5 * i) for i in range(5)], dds) dds = np.logspace(0, 2, 5, dtype=np.int32) self.checkitems([1, 3, 10, 31, 100], dds, toInt)
def readTestInputs( filename="height.txt", idealname="ideal.txt", visit_directory="/dls/i12/data/2014/cm4963-2", input_directory="tmp", printres=False, ): infilename = "%s/%s/%s" % (visit_directory, input_directory, filename) idealfilename = "%s/%s/%s" % (visit_directory, input_directory, idealname) inlist = [] ideallist = [] try: infile = open(infilename, "r") except: print ("File %s not opened properly" % infilename) return None n = 0 for line in infile: if printres: print n, line n = n + 1 inlist.append(float(line)) infile.close() try: infile = open(idealfilename, "r") except: print ("File %s not opened properly" % idealfilename) return None for line in infile: if printres: print n, line n = n + 1 ideallist.append(float(line)) infile.close() if printres: print inlist print ideallist inarray = dnp.array(inlist) idealarray = dnp.array(ideallist) print len(inarray) angarray = dnp.linspace(0, dnp.pi * 2.0, len(inarray), True) horizarray = 2500.0 * dnp.cos(angarray) ofile = open("%s/%s/testpoints.txt" % (visit_directory, input_directory), "w") ofile.write("angle,X,idealY,noisyY\n") for i in range(0, len(angarray)): ofile.write("%g,%g,%g,%g\n" % (angarray[i], horizarray[i], idealarray[i], inarray[i])) ofile.close() return (horizarray, inarray, idealarray)
def m6qgmax(): print("Move exit slit s6y to -6.5") s6y.moveTo(-6.5) q0 = m6qg.getPosition() print("Current m6qg position: " + str(q0)) print("Scan m6qg ...") m6Data = m6qgscan(q0) # m6Data=GuassianScan(); x, y = m6Data['m6qg'], m6Data['ca62sr'] # x, y=m6Data['x'], m6Data['y']; # [mu, sigma, peak, gf] = fineGaussianFitting(y, x, "Plot 1"); [mu, sigma, peak, gf] = fineGaussianFitting(y, x) # One vertical line on the peak point: # xx, yy=dnp.array([mu-1, mu+1]), dnp.array([0, peak]); xx, yy = dnp.array([mu - 1, mu, mu + 1]), dnp.array([0, peak, 0]) xxx, yyy = dnp.array([mu]), dnp.array([peak]) # To find the closest data point to the peak; cPos = (dnp.abs(x - mu)).minPos()[0] cX, cY = x[cPos], y[cPos] # data according to the model x1 = dnp.linspace(x[0], x[x.size - 1], 500) y1 = myGaussianFunc(mu, sigma, peak, [x1]) print("To plot the fitted data.") dnp.plot.points(x, y, None, 5) sleep(1) dnp.plot.addpoints(x1, y1, None, 1) sleep(1) dnp.plot.addpoints(xxx, yyy, None, 10) print("The peak position is: (%g, %g)" % (mu, peak)) ans = queryYesNo( "Do you want to move m6qg to the estimated peak position?", "no") if ans is 'yes': m6qg.moveTo(mu) print "m6qg:" + str(m6qg.getPosition()) return
def readTestInputs(filename='height.txt',idealname='ideal.txt',visit_directory='/dls/i12/data/2014/cm4963-2',input_directory='tmp',printres=False): infilename="%s/%s/%s"%(visit_directory,input_directory,filename) idealfilename="%s/%s/%s"%(visit_directory,input_directory,idealname) inlist=[] ideallist=[] try: infile=open(infilename,'r') except: print("File %s not opened properly"%infilename) return(None) n=0 for line in infile: if printres: print n,line n=n+1 inlist.append(float(line)) infile.close() try: infile=open(idealfilename,'r') except: print("File %s not opened properly"%idealfilename) return(None) for line in infile: if printres: print n,line n=n+1 ideallist.append(float(line)) infile.close() if printres: print inlist print ideallist inarray=dnp.array(inlist) idealarray=dnp.array(ideallist) print len(inarray) angarray=((dnp.linspace(0,dnp.pi*2.0,len(inarray),True))) horizarray=2500.0*dnp.cos(angarray) ofile=open("%s/%s/testpoints.txt"%(visit_directory,input_directory),'w') ofile.write("angle,X,idealY,noisyY\n") for i in range(0,len(angarray)): ofile.write("%g,%g,%g,%g\n"%(angarray[i],horizarray[i],idealarray[i],inarray[i])) ofile.close() return(horizarray,inarray,idealarray)
def dofourier(xarray, yarray, printres=False): nvals = len(yarray) avht = yarray.mean() angles = dnp.linspace(0, 2 * dnp.pi, len(yarray)) # print angles ycarray = yarray - avht cosines = dnp.cos(angles) sines = dnp.sin(angles) cosint = cosines * ycarray sinint = sines * ycarray if printres: print "Yarray:", yarray print "Ycarray:", ycarray print "Sinint:", sinint print "Cosint:", cosint cosum = cosint[:-1].sum() sinsum = sinint[:-1].sum() cosfactor = cosum / float(nvals - 1) sinfactor = sinsum / float(nvals - 1) if printres: print "sinfactor, cosfactor: %.4f %.4f" % (sinfactor, cosfactor) ratio = sinfactor / cosfactor delta = math.atan(ratio) deltadeg = math.degrees(delta) if printres: print "Delta degrees %06.4f" % deltadeg halfmag = math.sqrt(sinfactor ** 2 + cosfactor ** 2) mag = 2.0 * halfmag if printres: print "Magnitude: %06.4f" % mag print "vertical centre %7.4f" % avht calcresult = avht + mag * dnp.cos(angles - delta) if printres: print "Calcresult:", calcresult for idx in range(0, len(yarray)): print yarray[idx], calcresult[idx] return (avht, mag, delta, calcresult)
def dofourier(xarray,yarray,printres=False): nvals=len(yarray) avht=yarray.mean() angles=dnp.linspace(0,2*dnp.pi,len(yarray)) #print angles ycarray=yarray-avht cosines=dnp.cos(angles) sines=dnp.sin(angles) cosint=cosines*ycarray sinint=sines*ycarray if printres: print "Yarray:",yarray print "Ycarray:",ycarray print "Sinint:",sinint print "Cosint:",cosint cosum=cosint[:-1].sum() sinsum=sinint[:-1].sum() cosfactor=cosum/float(nvals-1) sinfactor=sinsum/float(nvals-1) if printres: print "sinfactor, cosfactor: %.4f %.4f"%(sinfactor,cosfactor) ratio=sinfactor/cosfactor delta=math.atan(ratio) deltadeg=math.degrees(delta) if printres: print"Delta degrees %06.4f"%deltadeg halfmag=math.sqrt(sinfactor**2+cosfactor**2) mag=2.0*halfmag if printres: print"Magnitude: %06.4f"%mag print "vertical centre %7.4f"%avht calcresult=avht+mag*dnp.cos(angles-delta) if printres: print"Calcresult:",calcresult for idx in range(0,len(yarray)): print yarray[idx],calcresult[idx] return(avht,mag,delta,calcresult)
def testMyGaussianFunc(): t = dnp.linspace(0, 99, 100)# dataset of 40 coordinate points between 0 and 5 dnp.plot.line(t, myGaussianFunc(10, 3, 10, [t]) ) # plot line of evaluated function
n=x.size; a=(x[n-1] - x[0])/(n-1); b=x[0]; mu=a*mu0+b; sigma=a*sigma0; #One vertical line on the peak point: #xx, yy=dnp.array([mu-1, mu+1]), dnp.array([0, peak]); xx, yy=dnp.array([mu-1, mu, mu+1]), dnp.array([0, peak, 0]); xxx, yyy=dnp.array([mu]), dnp.array([peak]); #To find the closest data point to the peak; cPos=(dnp.abs(x-mu)).minPos()[0]; cX, cY=x[cPos], y[cPos]; print("To plot the fitted data." ) x1=dnp.linspace(x[0], x[n-1], 500); y1=myGaussianFunc(mu, sigma, peak, [x1]); #Line plot does not work #dnp.plot.line(x, [y, y1] ) # plot line of evaluated function #dnp.plot.updateline(xx, yy) dnp.plot.points(x, y, None, 5); sleep(1); dnp.plot.addpoints(x1, y1, None, 1) sleep(1); dnp.plot.addpoints(xxx, yyy, None, 10); ########################