def __init__(self, parameters, experiment_data): # init var_list and table self.parameters = parameters self.para_path = os.path.abspath( os.path.dirname(parameters)) + '/' + parameters self.experiment_data = experiment_data self.data_path = os.path.abspath( os.path.dirname(experiment_data)) + '/' + experiment_data init_para = initialization_parameters(self.parameters) init_para._check_parameters() init_para._check_sheetname() init_data = initialization_rhkl(self.experiment_data) init_data._check_filename() # real time read xlsx, real time refresh self.var_list = init_para._var_list() self.var_table = init_para._var_table() # import factors self.properties = pd.read_excel(self.para_path, sheet_name='factors', index_col=0) # init experiment data iq and intensity self.iq, self.i = init_data._read_rhkl(experiment_data) # whick rod self.h = self.properties.at['h', 'data'] self.k = self.properties.at['k', 'data'] # inti parameters self.absorption = self.properties.at['absorption', 'data'] self.roughness = self.properties.at['roughness', 'data'] self.scale = self.properties.at['scale', 'data'] self.intensity = self.properties.at['intensity', 'data'] # interface roughness self.iroughness = self.properties.at['interface roughness', 'data'] # atomic form factor mode control # contral parameters self.ubr_index = tc.contral_vars(self.var_list)
def track_difference(self): var_list_rei = initialization_parameters._var_list(self.parameters) track_flag = 0 try: for slabi in var_list_rei: for keyi in var_list_rei[slabi]: # compare every key value in new excel and old excel if keyi in [ 'absorption', 'atoms_num', 'layers_num', 'roughness' ]: # refreshed data and old data value_rei = var_list_rei[slabi][keyi] value_old = self.var_list[slabi][keyi] # if same,continue. if change,flag = 1 if np.equal(value_rei, value_old): continue else: track_flag = 1 # these parameters are matrix elif keyi in [ 'dis', 'dw', 'ions', 'lattice', 'nel', 'ocu', 'pos' ]: # refreshed matrix and old matrix if False in np.equal( var_list_rei[slabi][keyi], self.var_list[slabi][keyi]).as_matrix(): track_flag = 1 break else: continue # beak from the inner loop if track_flag: break # break from the outer loop if track_flag: break except: track_flag = 1 properties = pd.read_excel(self.para_path, sheet_name='factors', index_col=0) if False in np.equal(properties, self.properties).as_matrix(): track_flag = 1 # refresh if track_flag == 1: init_para = initialization_parameters(self.parameters) init_data = initialization_rhkl(self.experiment_data) self.var_list = init_para._var_list() self.var_table = init_para._var_table() # import factors self.properties = pd.read_excel(self.para_path, sheet_name='factors', index_col=0) # init experiment data iq and intensity self.iq, self.i = init_data._read_rhkl(self.experiment_data) # whick rod self.h = self.properties.at['h', 'data'] self.k = self.properties.at['k', 'data'] # inti parameters self.absorption = self.properties.at['absorption', 'data'] self.roughness = self.properties.at['roughness', 'data'] self.scale = self.properties.at['scale', 'data'] self.intensity = self.properties.at['intensity', 'data'] # interface roughness self.iroughness = self.properties.at['interface roughness', 'data'] # contral parameters self.ubr_index = tc.contral_vars(self.var_list) return self, track_flag
def dw_refine(self, p_mask=13, n_mask=3, weight=1, key_list=['dw']): # Prepare the data mask # yin mask to mask the bragg peaks bn_mask = np.mat(tc.bragg_mask(self.iq, n_mask, 1, mode='yin')) # yang mask to refine the signal around bragg peaks only bp_mask = np.mat(tc.bragg_mask(self.iq, p_mask, 1, mode='yang')) bgm = np.asarray(np.multiply(bn_mask, bp_mask).T).reshape(-1) * weight # calculate ss tot.For only seveal signal points are calculate # mean value is np.sum(abs(i)*bgm)/np.sum(bgm) ss_tot = np.sum( (abs(self.i) * bgm - np.sum(abs(self.i) * bgm) / np.sum(bgm))**2) # initialize the variable about the lattice slab_list = np.unique(self.var_table['slab_list']).tolist() ubr_index = tc.contral_vars(self.var_list) subr, sindex = tc.initialize_contral_vars(ubr_index, slab_list, ['dw']) # roughness cr = roughness(self.iq) r = cr.robinson_roughness(self.roughness) # substrate ctr # the used keys key_list = ['absorption', 'roughness', 'scale'] # input substrate ubr and substrate index sub_ubr, sub_index = tc.initialize_contral_vars( self.ubr_index, ['substrate'], key_list) substrate_ctr = structure.substrate_ctr(self.var_list, self.iq, sub_ubr, sub_index, self.absorption, self.h, self.h, self.scale, key_list) def d_refine(d_var): fctr = structure.film_ctr(self.var_list, self.var_table, d_var, sindex, self.iq, self.h, self.k, ['dw']) ss = np.multiply(np.mat(fctr) + substrate_ctr, np.mat(r).T) * self.intensity # calculate r square sa = np.asarray(ss).reshape(-1) ss_res = np.sum((abs(self.i) * bgm - abs(sa) * bgm)**2) varience = ss_res / ss_tot print(int(varience * 1e4) / 1e4) return varience d0 = subr re = so.minimize(d_refine, d0, method='Nelder-Mead', tol=1) self.ubr_index = tc.refresh_index(self.ubr_index, re.x, sindex) # plot the refined result fctr = structure.film_ctr(self.var_list, self.var_table, re.x, sindex, self.iq, self.h, self.k, ['dw']) ss = np.multiply(np.mat(fctr) + substrate_ctr, np.mat(r).T) plt.plot(self.iq, np.log(abs(ss * self.intensity))) plt.plot(self.iq, np.log(np.sqrt(self.i)))