def diff(array, data, vectors): """This is a function used to optimise the U matrix. It evaluates the difference between the rotated set of vectors and their corosponding vectors in reciprical space. """ x = array[0] y = array[1] z = array[2] angle = array[3] axis = Vector([x, y, z]) U = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(axis, angle)) data = rotate_list(U, data) index_list=[] for i, dat in enumerate(data): diffs = [] dat = dat for j, vector in enumerate(vectors): diffs.append((dat-vector).length()) index = diffs.index(min(dnp.abs(diffs))) index_list.append(index) targets = [0]*len(data) for i, idx in enumerate(index_list): targets[i] = vectors[idx] total = 0 for i, dat in enumerate(data): total += dnp.abs((dat - targets[i]).length()) return total
def baseline(self,xdataset, ydataset, smoothness): '''find the baseline y value for a peak in y dataset''' ymaxindex=ydataset.argMax() if smoothness > 1: wnd = dnp.ones(smoothness, dtype=dnp.float64)/smoothness ydataset = dnp.convolve(ydataset, wnd, 'same') result=dnp.gradient(ydataset, xdataset) leftresult=result[:ymaxindex] rightresult=result[ymaxindex+1:] leftminderivativeindex=dnp.abs(leftresult).argmin() rightminderivativeindex=dnp.abs(rightresult).argmin() leftbasey=ydataset.getElementDoubleAbs(leftminderivativeindex) rightbasey=ydataset.getElementDoubleAbs(rightminderivativeindex+1+leftresult.shape[0]) basey=(leftbasey+rightbasey)/2 return basey
def baseline(self,xdataset, ydataset, smoothness): '''find the baseline y value for a peak in y dataset''' xdataset = dnp.asarray(xdataset) ydataset = dnp.asarray(ydataset) ymaxindex=ydataset.argmax() #TODO result=dnp.gradient(ydataset,xdataset) #derivative(xdataset, ydataset, smoothness) leftresult=result[:ymaxindex] rightresult=result[ymaxindex+1:] leftminderivativeindex=dnp.abs(leftresult).argmin() rightminderivativeindex=dnp.abs(rightresult).argmin() leftbasey=ydataset[leftminderivativeindex] rightbasey=ydataset[rightminderivativeindex+1+leftresult.shape[0]] basey=(leftbasey+rightbasey)/2 return basey
def diff(array, data, vectors): """This is a function used to optimise the U matrix. It evaluates """ x = array[0] y = array[1] z = array[2] # z = 1.0 angle = array[3] axis = Vector([x, y, z]) U = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(axis, angle)) data = rotate_list(U, data) index_list=[] for i, dat in enumerate(data): diffs = [] dat = dat for j, vector in enumerate(vectors): diffs.append((dat-vector).length()) index = diffs.index(min(dnp.abs(diffs))) index_list.append(index) targets = [0]*len(data) for i, idx in enumerate(index_list): targets[i] = vectors[idx] total = 0 for i, dat in enumerate(data): total += (dat - targets[i]).length() return total*1000
def ideal_two_theta_gap(index, grouped_reflections, ideal_two_theta): group = grouped_reflections[index] two_theta = group[0][4] if two_theta < ideal_two_theta[1] or two_theta > ideal_two_theta[2]: return 0 else: return dnp.abs(two_theta-ideal_two_theta[0])
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 baseline(self, xdataset, ydataset, smoothness): '''find the baseline y value for a peak in y dataset''' ymaxindex = ydataset.argMax() if smoothness > 1: wnd = dnp.ones(smoothness, dtype=dnp.float64) / smoothness ydataset = dnp.convolve(ydataset, wnd, 'same') result = dnp.gradient(ydataset, xdataset) leftresult = result[:ymaxindex] rightresult = result[ymaxindex + 1:] leftminderivativeindex = dnp.abs(leftresult).argmin() rightminderivativeindex = dnp.abs(rightresult).argmin() leftbasey = ydataset.getElementDoubleAbs(leftminderivativeindex) rightbasey = ydataset.getElementDoubleAbs(rightminderivativeindex + 1 + leftresult.shape[0]) basey = (leftbasey + rightbasey) / 2 return basey
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 check_third_vector(targets, found_vectors, correct_length, r): """Checks the third vector is on the 'right' side of the other two using the cross product of the other two. """ M = scm.matrix.sqr(found_vectors[0].elems + found_vectors[1].elems + found_vectors[2].elems) print 'M', M if dnp.abs(M.determinant()) < 5*10 ** -3: # Vectors are coplanar so cannot use cross product method but # can use angles alone unless two of the vectors are anti # parallel in which case there is a four fold rotational # symmetry so the choice is arbitary. dot0 = found_vectors[0].normalize().dot( found_vectors[2].normalize()) dot1 = found_vectors[1].normalize().dot( found_vectors[2].normalize()) angle0 = float(dnp.rad2deg(arccos(dot0))) angle1 = float(dnp.rad2deg(arccos(dot1))) for vector in correct_length: if round(found_vectors[2].length(), r) == round( vector.length(), r): t_dot0 = targets[0].normalize().dot(vector.normalize()) t_angle0 = float(dnp.rad2deg(arccos(t_dot0))) t_dot1 = targets[1].normalize().dot(vector.normalize()) t_angle1 = float(dnp.rad2deg(arccos(t_dot1))) if angle0 - angle_accuracy < t_angle0 < angle0 + angle_accuracy and \ angle1 - angle_accuracy < t_angle1 < angle1 + angle_accuracy: return targets return [] # Reset targets as wrong combination was found else: target_plane_normal = (targets[0].cross( targets[1])).normalize() found_plane_normal = (found_vectors[0].cross( found_vectors[1])).normalize() found_dot = found_vectors[2].normalize().dot( found_plane_normal) # Find third target dot0 = found_vectors[0].normalize().dot( found_vectors[2].normalize()) dot1 = found_vectors[1].normalize().dot( found_vectors[2].normalize()) angle0 = float(dnp.rad2deg(arccos(dot0))) angle1 = float(dnp.rad2deg(arccos(dot1))) for vector in correct_length: if round(found_vectors[2].length(), r) == round( vector.length(), r): t_dot0 = targets[0].normalize().dot(vector.normalize()) t_angle0 = float(dnp.rad2deg(arccos(t_dot0))) t_dot1 = targets[1].normalize().dot(vector.normalize()) t_angle1 = float(dnp.rad2deg(arccos(t_dot1))) if angle0-angle_accuracy < t_angle0 < angle0 + angle_accuracy and \ angle1 - angle_accuracy < t_angle1 < angle1 + angle_accuracy: target_dot = vector.normalize().dot( target_plane_normal) if target_dot < 0 and found_dot < 0 or\ target_dot > 0 and found_dot > 0: targets.append(vector) return targets return [] # Reset targets as wrong combination was found
def two_theta_gap(index, grouped_reflections): group = grouped_reflections[index] current_2theta = group[0][4] gaps =[] try: previous_group = grouped_reflections[index-1] previous_2theta = previous_group[0][4] gaps.append(dnp.abs(current_2theta-previous_2theta)) except IndexError: pass try: next_group = grouped_reflections[index+1] next_2theta = next_group[0][4] gaps.append(dnp.abs(current_2theta-next_2theta)) except IndexError: pass return min(gaps)
def baseline(self, xdataset, ydataset, smoothness): '''find the baseline y value for a peak in y dataset''' xdataset = dnp.asarray(xdataset) ydataset = dnp.asarray(ydataset) ymaxindex = ydataset.argmax() #TODO result = dnp.gradient(ydataset, xdataset) #derivative(xdataset, ydataset, smoothness) leftresult = result[:ymaxindex] rightresult = result[ymaxindex + 1:] leftminderivativeindex = dnp.abs(leftresult).argmin() rightminderivativeindex = dnp.abs(rightresult).argmin() leftbasey = ydataset[leftminderivativeindex] rightbasey = ydataset[rightminderivativeindex + 1 + leftresult.shape[0]] basey = (leftbasey + rightbasey) / 2 return basey
def moments(data): """ Returns (height, x, y, sigma_x, sigma_y) the gaussian parameters of a 2D distribution by calculating its moments """ total = data.sum() xyIndices = dnp.indices(data.shape) X=xyIndices[1]; Y=xyIndices[0]; x = (X*data).sum()/total y = (Y*data).sum()/total row = data[int(y), :] sigma_x = math.sqrt(dnp.abs((dnp.arange(row.size)-x)**2*row).sum()/row.sum()) col = data[:, int(x)] sigma_y = math.sqrt( dnp.abs((dnp.arange(col.size)-y)**2*col).sum()/col.sum() ) height = data.max() return [height, x, y, sigma_x, sigma_y];
def testPoly(self): fr = fit.polyfit(self.x, self.y, 1) print 'Poly: ', fr # print polynomial coeffs self.checkitems([3.2, 0.35], fr, 1) fr = fit.polyfit(self.x, self.z, 2) print 'Poly: ', fr # print polynomial coeffs self.checkitems([3.2, -12.2, 0.35], fr, 1) v = fit.polyval(fr, [0,1]) print 'value is', dnp.abs(v-0.3) self.checkitems([0.1, -9], v, 1)
def testSavingBits(self): d = dnp.arange(12 * 32).reshape((12, 32)) b = dnp.abs(dnp.array(d, dnp.int8)) b[b < 0] = 0 print(b.min(), b.max()) self.save('uint.tiff', d, bits=32, signed=False) self.save('ushort.tiff', d, bits=16, signed=False) self.save('ubyte.tiff', b, bits=8, signed=False) self.save('int.tiff', d, bits=32) self.save('short.tiff', d, bits=16) self.save('byte.tiff', dnp.array(d, dnp.int8), bits=8) self.save('double.tiff', d, bits=33) self.save('float.tiff', d, bits=33) self.save('short.png', d, bits=16) self.save('byte.png', b, bits=8)
def testSavingBits(self): d = dnp.arange(12*32).reshape((12,32)) b = dnp.abs(dnp.array(d, dnp.int8)) b[b < 0] = 0 print b.min(), b.max() dnp.io.save(OutTestFolder+'uint.tiff', d, bits=32, signed=False) dnp.io.save(OutTestFolder+'ushort.tiff', d, bits=16, signed=False) dnp.io.save(OutTestFolder+'ubyte.tiff', b, bits=8, signed=False) dnp.io.save(OutTestFolder+'int.tiff', d, bits=32) dnp.io.save(OutTestFolder+'short.tiff', d, bits=16) dnp.io.save(OutTestFolder+'byte.tiff', dnp.array(d, dnp.int8), bits=8) dnp.io.save(OutTestFolder+'double.tiff', d, bits=33) dnp.io.save(OutTestFolder+'float.tiff', d, bits=33) dnp.io.save(OutTestFolder+'short.png', d, bits=16) dnp.io.save(OutTestFolder+'byte.png', b, bits=8)
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 diff(U, data, vectors): """This is a function used to optimise the U matrix. It evaluates """ data = rm.rotate_list(U, data) index_list=[] for i, dat in enumerate(data): diffs = [] dat = dat for j, vector in enumerate(vectors): diffs.append((dat-vector).length()) index = diffs.index(min(dnp.abs(diffs))) index_list.append(index) targets = [0]*len(data) for i, idx in enumerate(index_list): targets[i] = vectors[idx] total = 0 for i, dat in enumerate(data): total += (dat - targets[i]).length() return total
def findBases(self, xdataset, ydataset, delta, smoothness): bases=[] peaks=self.findPeaksAndTroughs(ydataset, delta)[0] yslices=[] xslices=[] startindex=0 for index,value in peaks: #@UnusedVariable yslices.append(ydataset[startindex:index]) xslices.append(xdataset[startindex:index]) startindex=index+1 if smoothness > 1: wnd = dnp.ones(smoothness, dtype=dnp.float64)/smoothness for xset, yset in xslices, yslices: if smoothness > 1: yset = dnp.convolve(yset, wnd, 'same') result=dnp.gradient(yset, xset) minimumderivativeindex=dnp.abs(result).argmin() bases.append((xset[minimumderivativeindex],yset[minimumderivativeindex])) return bases
def findBases(self, xdataset, ydataset, delta, smoothness): bases = [] peaks = self.findPeaksAndTroughs(ydataset, delta)[0] yslices = [] xslices = [] startindex = 0 for index, value in peaks: #@UnusedVariable yslices.append(ydataset[startindex:index]) xslices.append(xdataset[startindex:index]) startindex = index + 1 if smoothness > 1: wnd = dnp.ones(smoothness, dtype=dnp.float64) / smoothness for xset, yset in xslices, yslices: if smoothness > 1: yset = dnp.convolve(yset, wnd, 'same') result = dnp.gradient(yset, xset) minimumderivativeindex = dnp.abs(result).argmin() bases.append( (xset[minimumderivativeindex], yset[minimumderivativeindex])) return bases
def findBasePoints(self, xdataset, ydataset, delta, smoothness): xdataset = dnp.asarray(xdataset) ydataset = dnp.asarray(ydataset) peaks=self.findPeaksAndTroughs(ydataset, delta)[0] #print peaks yslices=[] xslices=[] startindex=0 for index,value in peaks: #@UnusedVariable yslices.append(ydataset[startindex:index]) xslices.append(xdataset[startindex:index]) startindex=index+1 yslices.append(ydataset[startindex:]) xslices.append(xdataset[startindex:]) bases=[] for xset, yset in zip(xslices, yslices): result=dnp.gradient(yset, xset) minimumderivativeindex=dnp.abs(result).argmin() bases.append((xset[minimumderivativeindex],yset[minimumderivativeindex])) #print "Base Points (position, value) : ", bases return bases
def findBasePoints(self, xdataset, ydataset, delta, smoothness): xdataset = dnp.asarray(xdataset) ydataset = dnp.asarray(ydataset) peaks = self.findPeaksAndTroughs(ydataset, delta)[0] #print peaks yslices = [] xslices = [] startindex = 0 for index, value in peaks: #@UnusedVariable yslices.append(ydataset[startindex:index]) xslices.append(xdataset[startindex:index]) startindex = index + 1 yslices.append(ydataset[startindex:]) xslices.append(xdataset[startindex:]) bases = [] for xset, yset in zip(xslices, yslices): result = dnp.gradient(yset, xset) minimumderivativeindex = dnp.abs(result).argmin() bases.append( (xset[minimumderivativeindex], yset[minimumderivativeindex])) #print "Base Points (position, value) : ", bases return bases
def diff(array, data, vectors): x = array[0] y = array[1] z = array[2] angle = array[3] axis = rm.Vector(x,y,z) U = rm.Rotator(axis, angle) data = rm.rotate_list(U, data) index_list=[] for i, dat in enumerate(data): diffs = [] dat = dat for j, vector in enumerate(vectors): diffs.append((dat-vector).modulus()) index = diffs.index(min(dnp.abs(diffs))) index_list.append(index) targets = [0]*len(data) for i, idx in enumerate(index_list): targets[i] = vectors[idx] total = 0 for i, dat in enumerate(data): total += (dat - targets[i]).modulus() return total
fr.parameters fr[0] fr.residual d3x = data3['entry1']['default']['x'][...] d3y = data3['entry1']['default']['y'][...] ds = dnp.fft.fft(d3y) dnp.plot.line(d3x,[ds.real,ds.imag]) dnp.plot.line(d3x,[dnp.fft.fftshift(ds.real),dnp.fft.fftshift(ds.imag)]) scan sg -1 1 0.001 data4 = dnp.io.load("<File name from the beginning of the scan>”) d4y = data4['entry1']['default']['y'][...] d4x = data4['entry1']['default']['x'][...] ds = dnp.fft.fft(d4y) dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds).real]) ds[500:1500] = 0 dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds).real]) ds[250:1750] = 0 dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds).real]) ds[50:1950] = 0 filter = dnp.arange(1,-1,(-2./2001.)) dnp.plot.line(d4x,filter) filter = dnp.abs(filter) dnp.plot.line(d4x,filter) dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds*dnp.power(filter,1)).real]) dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds*dnp.power(filter,2)).real])
def test_finding_the_U_matrix(self): import scisoftpy as dnp import Crystal as c import functions as f import matplotlib.pyplot as plt import finding_the_rotation_matrix as rm import copy import scitbx.math as scm from scitbx.matrix import col as Vector from scitbx.matrix import sqr as Rotator def add_rot_error(vector): stddev = 0.1 # rand_rotation_x = rm.Rotator(rm.Vector(1),stddev)#,dnp.random.normal(0,stddev)) # rand_rotation_y = rm.Rotator(rm.Vector(0,1),-stddev)#, dnp.random.normal(0,stddev)) # rand_rotation_z = rm.Rotator(rm.Vector(0,0,1),stddev)#,dnp.random.normal(0,stddev)) rand_axis = Vector([dnp.random.random(),dnp.random.random(),dnp.random.random()]) rand_rotation = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(rand_axis, stddev, deg=True)) vector = rand_rotation * vector return vector def random_rotation(): rand_rotation_x = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([1.0,0,0]) ,dnp.random.randint(0,361), deg=True)) rand_rotation_y = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([0,1.0,0]),dnp.random.randint(0,361), deg=True)) rand_rotation_z = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([0.0,0.0,1.0]),dnp.random.randint(0,361), deg=True)) return rand_rotation_z*rand_rotation_y*rand_rotation_x def diff(U, data, vectors): """This is a function used to optimise the U matrix. It evaluates """ data = rm.rotate_list(U, data) index_list=[] for i, dat in enumerate(data): diffs = [] dat = dat for j, vector in enumerate(vectors): diffs.append((dat-vector).length()) index = diffs.index(min(dnp.abs(diffs))) index_list.append(index) targets = [0]*len(data) for i, idx in enumerate(index_list): targets[i] = vectors[idx] total = 0 for i, dat in enumerate(data): total += (dat - targets[i]).length() return total mycrys = c.Crystal() mycrys.load_cif('NiCO3_icsd_61067.cif') l = f.group_reflections(mycrys) vectors =[] l_index = dnp.random.randint(len(l)) vectors = f.momentum_transfer_vectors(l[l_index], mycrys) # plt.show() all_vectors=[] for i, group in enumerate(l): all_g = f.momentum_transfer_vectors(group, mycrys) all_vectors += all_g fig = plt.figure() ax = fig.add_subplot(311, projection='3d') ax.set_title('Unedited Reciprocal Space') f.plot_vectors(all_vectors, fig, ax) all_vectors_copy = copy.deepcopy(all_vectors) dot = 0 while dot<10**(-5) or 179.5<dot<180.5 or dot==None: i = dnp.random.randint(len(vectors)) j=i k=i while i == j: j = dnp.random.randint(len(vectors)) while i==k or j==k: k = dnp.random.randint(len(vectors)) if k!=j and k!=i: if dnp.abs(180-dnp.rad2deg(dnp.arccos(vectors[k].normalize().dot(vectors[i].normalize()))))%180.0 < 1: k=i if dnp.abs(180-dnp.rad2deg(dnp.arccos(vectors[k].normalize().dot(vectors[j].normalize()))))%180.0 < 1: k=j dot = dnp.rad2deg(dnp.arccos(vectors[i].normalize().dot(vectors[j].normalize()))) print dot print l_index, i, j, k print l[l_index][i][0] print l[l_index][j][0] print l[l_index][k][0] mock_data = [vectors[i], vectors[j], vectors[k]] mock_data = [add_rot_error(dat) for dat in mock_data] rand_rot = random_rotation() mock_data = rm.rotate_list(rand_rot, mock_data) all_vectors_copy = rm.rotate_list(rand_rot, all_vectors_copy) ax = fig.add_subplot(312, projection='3d') ax.set_title('Randomly Rotated Reciprocal Space') f.plot_vectors(all_vectors_copy, fig, ax) U = rm.find_U_matrix(mock_data, mycrys, optimise_U=True) rotator = U difference = diff(U, all_vectors_copy, all_vectors) print 'diff', difference all_vectors_copy = rm.rotate_list(rotator, all_vectors_copy) ax = fig.add_subplot(313, projection='3d') ax.set_title('Randomly Rotated Reciprocal Space after U Matrix') f.plot_vectors(all_vectors_copy, fig, ax) # print 'Orientation', rand_rot # print 'U matrix', rotator plt.show()
# [mu, sigma, peak, gf] = fineGaussianFitting(y, "Plot 2"); [mu0, sigma0, peak, gf] = fineGaussianFitting(y); 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);
def ideal_two_theta_gap(index, grouped_reflections, ideal_two_theta): group = grouped_reflections[index] two_theta = group[0][4] return dnp.abs(two_theta-ideal_two_theta)
def test_at_random(cif_list, old_filter=False): l = [] i = 0 j = i k = i print 'Choosing a cif file...' while len(l) < 2: mycrys = c.Crystal() cif_indx = dnp.random.randint(len(cif_list)) cif = cif_list[cif_indx] mycrys.load_cif(cif) if old_filter: l = f.group_reflections(mycrys, refl = 'allowed') else: l = f.group_reflections(mycrys) if len(l)==0: print 'Chose {0}'.format(cif) print 'NEW FILTER: CASE FAILLED DUE TO NO REFLECITONS GETTING THROUGH FILTER' print print log = open('test_log_with_error.txt', 'a') log.write('Chose {0}'.format(cif)+'\n') log.write('NEW FILTER: CASE FAILLED DUE TO NO REFLECITONS GETTING THROUGH FILTER') log.write('\n \n') log.close() del cif_list[cif_indx] return cif_list new_l = [] for old_l in l: if len(old_l) > 2: new_l.append(old_l) l = new_l print 'Chose {0} now creating list of all vectors...'.format(cif) log = open('test_log_with_error.txt', 'a') log.write('Chose cif file: {0}'.format(cif)) log.write('\n \n') log.close() all_vectors = [] for i, group in enumerate(l): all_g = f.momentum_transfer_vectors(group, mycrys) all_vectors += all_g # fig = plt.figure() # ax = fig.add_subplot(311, projection='3d') # f.plot_vectors(all_vectors, fig, ax) print 'Created list of all vectors and it contains {0} vectors, now choosing reflections...'.format(len(all_vectors)) mock_data = [] while mock_data == []: if len(l) == 0: log = open('test_log_with_error.txt', 'a') log.write("Couldn't find a good set of reflections so starting again...") log.write('\n \n') log.close() print "Couldn't find a good set of reflections so starting again..." return 'Start over' l_index = dnp.random.randint(len(l)) vectors = f.momentum_transfer_vectors(l[l_index], mycrys) combos = [] for permutation in permutations(range(len(vectors)), 3): combos.append(permutation) dot = 0 while dot<10**(-5) or 179.5<dot<180.5: if len(combos) == 0: del l[l_index] mock_data = [] break indx = dnp.random.randint(len(combos)) i, j, k = combos[indx] del combos[indx] dot = dnp.rad2deg(arccos(vectors[i].normalize().dot(vectors[j].normalize()))) if dnp.abs(180-dnp.rad2deg(arccos(vectors[k].normalize().dot(vectors[i].normalize()))))%180.0 < 1: dot = 0 elif dnp.abs(180-dnp.rad2deg(arccos(vectors[k].normalize().dot(vectors[j].normalize()))))%180.0 < 1: dot = 0 mock_data = [vectors[i], vectors[j], vectors[k]] if len(combos) == 0 and dot<10**(-5) or 179.5<dot<180.5: del l[l_index] mock_data = [] break print i, j, k , l_index, len(l) log = open('test_log_with_error.txt', 'a') log.write('Chose reflections {0}, {1} and {2}'.format(l[l_index][i][0], l[l_index][j][0], l[l_index][k][0])) log.write('\n \n') log.close() print 'Chose reflections {0}, {1} and {2}, now making mock data...'.format(l[l_index][i][0], l[l_index][j][0], l[l_index][k][0]) mock_data = [add_rot_error(dat) for dat in mock_data] rand_rot = random_rotation() mock_data = rm.rotate_list(rand_rot, mock_data) all_vectors_rot = rm.rotate_list(rand_rot, all_vectors) # ax = fig.add_subplot(312, projection='3d') # f.plot_vectors(all_vectors_rot, fig, ax) print 'Created mock data. Now running tests...' if test_data(mock_data, all_vectors_rot, all_vectors, mycrys,i, j, k, old_filter, vectors): return True else: return False
rand_rotation_y = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([0,1.0,0]),dnp.random.randint(0,361), deg=True)) rand_rotation_z = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([0.0,0.0,1.0]),dnp.random.randint(0,361), deg=True)) return rand_rotation_z*rand_rotation_y*rand_rotation_x mycrys = c.Crystal() mycrys.load_cif('NiCO3_icsd_61067.cif') l = f.group_reflections(mycrys) vectors =[] # l_index = 16#3 # vectors = f.momentum_transfer_vectors(l[l_index], mycrys) while len(vectors)<3 or len(vectors)>7: l_index = dnp.random.randint(len(l)) vectors = f.momentum_transfer_vectors(l[l_index], mycrys) if len(vectors)>4: M = scm.matrix.sqr(vectors[0].elems + vectors[2].elems + vectors[4].elems) if dnp.abs(M.determinant())<10**-3 and len(vectors)>4: vectors = [0,1,2,3,4,5,6,7,8]#len9>8 # f.plot_vectors(vectors) # plt.show() all_vectors=[] for i, group in enumerate(l): all_g = f.momentum_transfer_vectors(group, mycrys) all_vectors += all_g f.plot_vectors(all_vectors) # fig = plt.figure() # ax = fig.add_subplot(311, projection='3d') # f.plot_vectors(all_vectors, fig, ax) # all_vectors_copy = copy.deepcopy(all_vectors) # dot = 0 # while dot<10**(-5) or 179.9<dot<180.1: # i = dnp.random.randint(len(vectors))
def testPolyVal(self): fr = fit.polyfit(self.x, self.y, 1) v = fit.polyval(fr, [0,1]) print 'value is', dnp.abs(v-0.3) self.checkitems([0.36, 3.55], v, 0)
def testPolyVal(self): fr = fit.polyfit(self.x, self.y, 1) v = fit.polyval(fr, [0, 1]) print('value is', dnp.abs(v - 0.3)) self.checkitems([0.36, 3.55], v, 0)
def test_finding_the_U_matrix(self): import scisoftpy as dnp import Crystal as c import functions as f import matplotlib.pyplot as plt import finding_the_rotation_matrix as rm import copy import scitbx.math as scm from scitbx.matrix import col as Vector from scitbx.matrix import sqr as Rotator def add_rot_error(vector): stddev = 0.1 # rand_rotation_x = rm.Rotator(rm.Vector(1),stddev)#,dnp.random.normal(0,stddev)) # rand_rotation_y = rm.Rotator(rm.Vector(0,1),-stddev)#, dnp.random.normal(0,stddev)) # rand_rotation_z = rm.Rotator(rm.Vector(0,0,1),stddev)#,dnp.random.normal(0,stddev)) rand_axis = Vector([dnp.random.random(),dnp.random.random(),dnp.random.random()]) rand_rotation = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(rand_axis, stddev, deg=True)) vector = rand_rotation * vector return vector def random_rotation(): rand_rotation_x = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([1.0,0,0]) ,dnp.random.randint(0,361), deg=True)) rand_rotation_y = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([0,1.0,0]),dnp.random.randint(0,361), deg=True)) rand_rotation_z = Rotator(scm.r3_rotation_axis_and_angle_as_matrix(Vector([0.0,0.0,1.0]),dnp.random.randint(0,361), deg=True)) return rand_rotation_z*rand_rotation_y*rand_rotation_x mycrys = c.Crystal() mycrys.load_cif('NiCO3_icsd_61067.cif') l = f.group_reflections(mycrys) vectors =[] # l_index = 16#3 # vectors = f.momentum_transfer_vectors(l[l_index], mycrys) while len(vectors)<3 or len(vectors)>7: l_index = dnp.random.randint(len(l)) vectors = f.momentum_transfer_vectors(l[l_index], mycrys) if len(vectors)>4: M = scm.matrix.sqr(vectors[0].elems + vectors[2].elems + vectors[4].elems) if dnp.abs(M.determinant())<10**-3 and len(vectors)>4: vectors = [0,1,2,3,4,5,6,7,8]#len9>8 f.plot_vectors(vectors) # plt.show() all_vectors=[] for i, group in enumerate(l): all_g = f.momentum_transfer_vectors(group, mycrys) all_vectors += all_g fig = plt.figure() ax = fig.add_subplot(311, projection='3d') f.plot_vectors(all_vectors, fig, ax) all_vectors_copy = copy.deepcopy(all_vectors) dot = 0 while dot<10**(-5) or 179.9<dot<180.1: i = dnp.random.randint(len(vectors)) j=i k=i while i == j: j = dnp.random.randint(len(vectors)) while i==k or j==k: k = dnp.random.randint(len(vectors)) if k!=j and k!=i: if dnp.abs(180-dnp.rad2deg(dnp.arccos(vectors[k].normalize().dot(vectors[i].normalize()))))%180.0 < 1: k=i if dnp.abs(180-dnp.rad2deg(dnp.arccos(vectors[k].normalize().dot(vectors[j].normalize()))))%180.0 < 1: k=j dot = dnp.rad2deg(dnp.arccos(vectors[i].normalize().dot(vectors[j].normalize()))) print l_index, i, j, k i =0 j=4 k=3 mock_data = [vectors[i], vectors[j], vectors[k]] mock_data = [add_rot_error(dat) for dat in mock_data] rand_rot = random_rotation() mock_data = rm.rotate_list(rand_rot, mock_data) all_vectors_copy = rm.rotate_list(rand_rot, all_vectors_copy) ax = fig.add_subplot(312, projection='3d') f.plot_vectors(all_vectors_copy, fig, ax) U = rm.find_U_matrix(mock_data, mycrys) rotator = U all_vectors_copy = rm.rotate_list(rotator, all_vectors_copy) ax = fig.add_subplot(313, projection='3d') f.plot_vectors(all_vectors_copy, fig, ax) # print 'Orientation', rand_rot # print 'U matrix', rotator plt.show()