def test_(self): ## test method names begin 'test*' param = {} param['theta_min'] = 0 param['theta_max'] = 5 param['wavelength'] = 0.2647 param['unit_cell_phase_0'] = [8.5312,4.8321,10.125,90.00,92.031,90.00] param['sgno'] = 4 param['sgname_phase_0'] = 'P21' param['cell_choice_phase_0'] = 'standard' hkl = reflections.gen_miller(param,0) self.assertEqual(len(hkl),498)
def test_calc_intensity(self): myinput = check_input.parse_input(input_file='simul.inp') myinput.read() myinput.check() myinput.initialize() myinput.param['structure_phase_0'] = 'oPPA.cif' myinput.param['structure_datablock'] = 'oPPA' xtal_structure = reflections.open_structure(myinput.param,0) hkl = reflections.gen_miller(myinput.param,0) hkl = reflections.calc_intensity(hkl,xtal_structure)
def test_add_intensity(self): param = {} param['theta_min'] = 0 param['theta_max'] = 3 param['wavelength'] = 0.2647 param['unit_cell_phase_0'] = [8.5312,4.8321,10.125,90.00,92.031,90.00] param['cell_choice_phase_0']='standard' param['sgno_phase_0'] = 4 param['sgname_phase_0'] = 'p21' hkl = reflections.gen_miller(param,0) hkl2 = reflections.add_intensity(hkl,param) self.assertEqual(n.sum(hkl2[:,3]),len(hkl2)*2**15) param['structure_int'] = 2**14 hkl2 = reflections.add_intensity(hkl,param) self.assertEqual(n.sum(hkl2[:,3]),len(hkl2)*2**14)
def intensity(inp): """ Reject peaks based on intensity Jette Oddershede, August 27 2008 Absorption correction added December 2011 """ if inp.files['structure_file'] != None: inp.param['structure_phase_0'] = inp.files['structure_file'] xtal_structure = reflections.open_structure(inp.param,0) hkl = reflections.gen_miller(inp.param,0) hkl = reflections.calc_intensity(hkl,xtal_structure) # print hkl if inp.fit['abs_mu'] > 0: xmin = min(inp.fit['abs_xlim'])*1e3 xmax = max(inp.fit['abs_xlim'])*1e3 ymin = min(inp.fit['abs_ylim'])*1e3 ymax = max(inp.fit['abs_ylim'])*1e3 for i in range(inp.no_grains): x = deepcopy(inp.values['x%s' %i]) y = deepcopy(inp.values['y%s' %i]) z = deepcopy(inp.values['z%s' %i]) #Make sure that grain not outside of bounds for doing the correction if inp.fit['abs_mu'] > 0: if x < xmin: x = xmin elif x > xmax: x = xmax if y < ymin: y = ymin elif y > ymax: y = ymax for j in range(inp.nrefl[i]): h = inp.h[i][j] k = inp.k[i][j] l = inp.l[i][j] # Absorption correction start if inp.fit['abs_mu'] > 0: w = inp.w[inp.id[i][j]] dety = inp.dety[inp.id[i][j]] detz = inp.detz[inp.id[i][j]] Omega = tools.form_omega_mat_general(w*n.pi/180.,inp.values['wx']*n.pi/180.,inp.values['wy']*n.pi/180.) R = tools.detect_tilt(inp.values['tx'],inp.values['ty'],inp.values['tz']) d_out = n.dot(R,n.array([[0], [(dety-inp.values['cy'])*inp.values['py']], [(detz-inp.values['cz'])*inp.values['pz']]])) d_out = d_out + n.array([[inp.values['L']],[0],[0]]) - n.dot(Omega,n.array([[x],[y],[z]])) d_out = n.dot(n.transpose(Omega),d_out/n.sqrt(n.sum(d_out**2))) d_in = n.dot(n.transpose(Omega),n.array([[-1],[0],[0]])) #from grain to source! # distance to bounding planes assuming d_in and d_out unit vectors xdist_in = 1e6 xdist_out = 1e6 ydist_in = 1e6 ydist_out = 1e6 if d_in[0] < 0: xdist_in = (x-xmin)/abs(d_in[0,0]) elif d_in[0] > 0: xdist_in = (xmax-x)/abs(d_in[0,0]) if d_in[1] < 0: ydist_in = (y-ymin)/abs(d_in[1,0]) elif d_in[1] > 0: ydist_in = (ymax-y)/abs(d_in[1,0]) dist_in = min(xdist_in,ydist_in) if d_out[0] < 0: xdist_out = (x-xmin)/abs(d_out[0,0]) elif d_out[0] > 0: xdist_out = (xmax-x)/abs(d_out[0,0]) if d_out[1] < 0: ydist_out = (y-ymin)/abs(d_out[1,0]) elif d_out[1] > 0: ydist_out = (ymax-y)/abs(d_out[1,0]) dist_out = min(xdist_out,ydist_out) dist = dist_in + dist_out # abs_mu in mm-1 and dist in microns exponential = n.exp(inp.fit['abs_mu']*dist*1e-3) #print h,k,l,w,x,y,z,dist_in*1e-3,dist_out*1e-3,dist*1e-3,exponential else: exponential = 1. # Absorption correction end value = False for m in range(len(hkl)): if hkl[m][0] == h and hkl[m][1] == k and hkl[m][2] == l: inp.volume[i][j] = inp.F2vol[inp.id[i][j]]/hkl[m][3]*exponential value = True break else: pass if value == False: # print i+1,j+1,h,k,l,'should never go here, ask osho for help...' inp.volume[i][j] = -1 data = deepcopy(inp.volume) minvol = [] maxvol = [] for i in range(inp.no_grains): if i+1 in inp.fit['skip']: pass else: rej = [] newreject = 1 while newreject > 0: tmp = len(rej) mad(data[i],rej,inp.fit['rej_vol']) newreject = len(rej) - tmp avgdata = n.sum(data[i])/len(data[i]) sigdata = spread(data[i]) if len(rej) > 1: avgrej = n.sum(rej)/len(rej) sigrej = spread(rej) elif len(rej) == 1: avgrej = rej[0] sigrej = 0 else: avgrej = 0 sigrej = 0 if len(data[i]) > 0: minvol.append(max(0,min(data[i]))) maxvol.append(max(data[i])) else: minvol.append(0) maxvol.append(-1) if i+1 not in inp.fit['skip']: inp.fit['skip'].append(i+1) # print '\n',i, avgdata, sigdata, len(data[i]), minvol[i], maxvol[i],'\n ',avgrej, sigrej, len(reject)#,'\n',data[i],'\n',reject delete = 0 for i in range(inp.no_grains): if i+1 in inp.fit['skip']: pass else: for j in range(inp.nrefl[i]-1,-1,-1): if inp.volume[i][j] < minvol[i] or inp.volume[i][j] > maxvol[i]: reject(inp,i,j,'intensity') delete = delete + 1 print 'Rejected', delete, 'peaks because of different intensity scales' insignificant(inp)
def intensity(inp): """ Reject peaks based on intensity Jette Oddershede, August 27 2008 """ if inp.files['structure_file'] != None: inp.param['structure_phase_0'] = inp.files['structure_file'] xtal_structure = reflections.open_structure(inp.param, 0) hkl = reflections.gen_miller(inp.param, 0) hkl = reflections.calc_intensity(hkl, xtal_structure) # print hkl for i in range(inp.no_grains): for j in range(inp.nrefl[i]): h = inp.h[i][j] k = inp.k[i][j] l = inp.l[i][j] value = False for m in range(len(hkl)): if hkl[m][0] == h and hkl[m][1] == k and hkl[m][2] == l: inp.volume[i][j] = inp.F2vol[inp.id[i][j]] / hkl[m][3] value = True break else: pass if value == False: # print i+1,j+1,h,k,l,'should never go here, ask osho for help...' inp.volume[i][j] = -1 data = deepcopy(inp.volume) minvol = [] maxvol = [] for i in range(inp.no_grains): if i + 1 in inp.fit['skip']: pass else: rej = [] newreject = 1 while newreject > 0: tmp = len(rej) mad(data[i], rej, inp.fit['rej_vol']) newreject = len(rej) - tmp avgdata = n.sum(data[i]) / len(data[i]) sigdata = spread(data[i]) if len(rej) > 1: avgrej = n.sum(rej) / len(rej) sigrej = spread(rej) elif len(rej) == 1: avgrej = rej[0] sigrej = 0 else: avgrej = 0 sigrej = 0 if len(data[i]) > 0: minvol.append(max(0, min(data[i]))) maxvol.append(max(data[i])) else: minvol.append(0) maxvol.append(-1) if i + 1 not in inp.fit['skip']: inp.fit['skip'].append(i + 1) # print '\n',i, avgdata, sigdata, len(data[i]), minvol[i], maxvol[i],'\n ',avgrej, sigrej, len(reject)#,'\n',data[i],'\n',reject delete = 0 for i in range(inp.no_grains): if i + 1 in inp.fit['skip']: pass else: for j in range(inp.nrefl[i] - 1, -1, -1): if inp.volume[i][j] < minvol[i] or inp.volume[i][ j] > maxvol[i]: reject(inp, i, j, 'intensity') delete = delete + 1 print('Rejected', delete, 'peaks because of different intensity scales') insignificant(inp)
def find_refl(inp): """ From U, (x,y,z) and B determined for the far-field case calculate the possible reflection on the near-field detector output[grainno][reflno]=[h,k,l,omega,dety,detz,tth,eta] """ S = n.array([[1, 0, 0],[0, 1, 0],[0, 0, 1]]) R = tools.detect_tilt(inp.param['tilt_x'], inp.param['tilt_y'], inp.param['tilt_z']) inp.possible = [] # if structure info is given use this if inp.files['structure_file'] != None: inp.param['structure_phase_0'] = inp.files['structure_file'] xtal_structure = reflections.open_structure(inp.param,0) HKL = reflections.gen_miller(inp.param,0) else: inp.param['unit_cell_phase_0'] = inp.unit_cell inp.param['sgno_phase_0'] = inp.fit['sgno'] HKL = reflections.gen_miller(inp.param,0) for grainno in range(inp.no_grains): inp.possible.append([]) if grainno+1 not in inp.fit['skip']: B = tools.epsilon_to_b(n.array([inp.values['epsaa%s' %grainno], inp.values['epsab%s' %grainno], inp.values['epsac%s' %grainno], inp.values['epsbb%s' %grainno], inp.values['epsbc%s' %grainno], inp.values['epscc%s' %grainno]]), inp.unit_cell) U = tools.rod_to_u([inp.rod[grainno][0]+inp.values['rodx%s' %grainno], inp.rod[grainno][1]+inp.values['rody%s' %grainno], inp.rod[grainno][2]+inp.values['rodz%s' %grainno]]) gr_pos = n.array([inp.values['x%s' %grainno], inp.values['y%s' %grainno], inp.values['z%s' %grainno]]) for hkl in HKL: Gc = n.dot(B,hkl[0:3]) Gw = n.dot(S,n.dot(U,Gc)) tth = tools.tth2(Gw,inp.param['wavelength']) # print hkl[0:3],tth*180./n.pi costth = n.cos(tth) (Omega, Eta) = tools.find_omega_general(inp.param['wavelength']/(4.*n.pi)*Gw, tth, inp.values['wx']*n.pi/180, inp.values['wy']*n.pi/180) if len(Omega) > 0: for solution in range(len(Omega)): omega = Omega[solution] eta = Eta[solution] for i in range(len(inp.fit['w_limit'])//2): if (inp.fit['w_limit'][2*i]*n.pi/180) < omega and\ omega < (inp.fit['w_limit'][2*i+1]*n.pi/180): # form Omega rotation matrix Om = tools.form_omega_mat_general(omega,inp.values['wx']*n.pi/180,inp.values['wy']*n.pi/180) Gt = n.dot(Om,Gw) # Calc crystal position at present omega [tx,ty,tz]= n.dot(Om,gr_pos) # Calc detector coordinate for peak (dety, detz) = detector.det_coor(Gt,costth, inp.param['wavelength'], inp.param['distance'], inp.param['y_size'], inp.param['z_size'], inp.param['y_center'], inp.param['z_center'], R,tx,ty,tz) #If peak within detector frame store it in possible if (-0.5 < dety) and\ (dety < inp.fit['dety_size']-0.5) and\ (-0.5 < detz) and\ (detz < inp.fit['detz_size']-0.5): inp.possible[grainno].append([hkl[0], hkl[1], hkl[2], omega*180/n.pi, dety, detz, tth, eta])
def run(options): # Check for print_input try: options.print_input except: options.print_input = None if options.print_input: print(help_input.show_input()) sys.exit() # Check if filename is specified try: options.filename except: options.filename = None if options.filename == None: print("\nNo input file supplied [-i filename]\n") #Gaelle comment : ? sys.exit() and add raise error instead sys.exit() #print 'options = ',options #print '\n' # Check killfile does not exist try: options.killfile except: options.killfile = None if options.killfile is not None and os.path.exists(options.killfile): print("The purpose of the killfile option is to create that file") print("only when you want PolyXsim to stop") print("If the file already exists when you start PolyXsim, it is") print("stopped immediately") raise ValueError("Your killfile " + options.killfile + " already exists") # Is the input file available? # Read and check input # Make instance of parse_input class print('Reading input\n') myinput = check_input.parse_input(input_file=options.filename) myinput.read() # read input file print('Checking input\n') myinput.check() # check validity of input check_input.interrupt(options.killfile) # if myinput.missing == True: # if problem exit # print('MISSING ITEMS') # sys.exit() if len(myinput.errors) > 0: myinput.show_errors() sys.exit() print('Initialize parameters etc\n') myinput.initialize() # if ok initialize check_input.interrupt(options.killfile) # Generate reflections hkl = [] for phase in myinput.param['phase_list']: if ('structure_phase_%i' % phase) in myinput.param: xtal_structure = reflections.open_structure(myinput.param, phase) #print 'UNIT CELL', myinput.param['unit_cell_phase_%i' %phase] print('Generating miller indices') hkl_tmp = reflections.gen_miller(myinput.param, phase) if myinput.param['structure_factors'] != 0: print('Structure factor calculation') hkl.append( reflections.calc_intensity(hkl_tmp, xtal_structure, options.killfile)) else: hkl.append(reflections.add_intensity(hkl, myinput.param)) print('No structure factor calculation') else: hkl_tmp = reflections.gen_miller(myinput.param, phase) hkl.append(reflections.add_intensity(hkl_tmp, myinput.param)) check_input.interrupt(options.killfile) # if options.killfile is not None and os.path.exists(options.killfile): # raise KeyboardInterrupt() generate_grains.generate_grains(myinput.param) check_input.interrupt(options.killfile) print('Write grains file') file_io.write_grains(myinput.param) check_input.interrupt(options.killfile) print('Write res file') file_io.write_res(myinput.param) check_input.interrupt(options.killfile) if '.hkl' in myinput.param['output']: print('Write hkl file') file_io.write_hkl(myinput.param, hkl) if '.fcf' in myinput.param['output']: print('Write fcf file') file_io.write_fcf(myinput.param, hkl) if '.ubi' in myinput.param['output']: print('Write UBI file') file_io.write_ubi(myinput.param) if '.par' in myinput.param['output']: print('Write detector.par file') file_io.write_par(myinput.param) check_input.interrupt(options.killfile) # Determine the reflection parameters for grains graindata = find_refl.find_refl(myinput.param, hkl, options.killfile) graindata.frameinfo = myinput.frameinfo print('Determine reflections positions') graindata.run() if '.ref' in myinput.param['output']: print('Write reflection file') graindata.save() if '.gve' in myinput.param['output']: print('Write g-vector file') graindata.write_gve() if '.ini' in myinput.param['output']: print('Write GrainSpotter ini file - Remember it is just a template') graindata.write_ini() if '.flt' in myinput.param['output']: print('Write filtered peaks file') graindata.write_flt() if myinput.param['make_image'] == 1: if myinput.param['peakshape'][0] == 2: image = make_imagestack.make_image(graindata, options.killfile) image.setup_odf() image.make_image_array() image.make_image() image.correct_image() else: image = make_image.make_image(graindata, options.killfile) image.make_image()