def AutoSave(self): '''DoAutoSave(self) --> None Function that conducts an autosave of the model. ''' io.save_gx(self.parent.model.get_filename(), self.parent.model, \ self.optimizer, self.config)
def AutoSave(self): '''DoAutoSave(self) --> None Function that conducts an autosave of the model. ''' io.save_gx(self.model.get_filename(), self.model, \ self.optimizer, self.config)
def save(frame, event): ''' save(frame, event) --> None Event handler for saving a model file ... ''' frame.model.set_script(frame.script_editor.GetText()) fname = frame.model.get_filename() # If model hasn't been saved if fname == '': # Proceed with calling save as save_as(frame, event) else: # If it has been saved just save it try: io.save_gx(fname, frame.model, frame.solver_control.optimizer,\ frame.config) except modellib.IOError as e: ShowModelErrorDialog(frame, e.__str__()) except Exception as e: outp = StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, 'Could not save the file. Python Error:'\ '\n%s'%(val,)) set_title(frame) frame.main_frame_statusbar.SetStatusText('Model saved to file', 1)
def save(frame, event): ''' save(frame, event) --> None Event handler for saving a model file ... ''' frame.model.set_script(frame.script_editor.GetText()) fname = frame.model.get_filename() # If model hasn't been saved if fname == '': # Proceed with calling save as save_as(frame, event) else: # If it has been saved just save it try: io.save_gx(fname, frame.model, frame.solver_control.optimizer,\ frame.config) except modellib.IOError, e: ShowModelErrorDialog(frame, e.__str__()) except Exception, e: outp = StringIO.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, 'Could not save the file. Python Error:'\ '\n%s'%(val,))
def save_as(frame, event): '''save_as(frame, event) --> None Event handler for save as ... ''' dlg = wx.FileDialog(frame, message="Save As", defaultFile="",\ wildcard="GenX File (*.gx)|*.gx",\ style=wx.SAVE | wx.CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: frame.model.set_script(frame.script_editor.GetText()) fname = dlg.GetPath() result = True if os.path.exists(fname): filepath, filename = os.path.split(fname) result = ShowQuestionDialog(frame, \ 'The file %s already exists. Do you wish to overwrite it?'%filename\ , 'Overwrite?') if result: try: io.save_gx(fname, frame.model, frame.solver_control.optimizer,\ frame.config) except modellib.IOError, e: ShowModelErrorDialog(frame, e.__str__()) except Exception, e: outp = StringIO.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, 'Could not save the file. Python Error:'\ '\n%s'%(val,)) set_title(frame)
def make_gx_file(): mod = model.Model() config = io.Config() opt = diffev.DiffEv() par=parameters.Parameters() make_par_file() par.set_ascii_input_new(os.path.join(dump_path_locator(),'par_table.tab')) io.load_gx('temp_DONOT_delete.gx',mod,opt,config) mod.script=make_script_file() mod.parameters=par mod.data,num_ctr=make_data_file() for i in range(len(mod.data.items)-num_ctr): mod.data.items[i+num_ctr].use=False io.save_gx(gx_file_name,mod,opt,config) print('gx file is saved in the current workdir!\n') print('##########Basic model info##########') print('Resonant element: '+RAXR_EL) print('Number of Gaussian peaks in CTR: '+str(NUMBER_GAUSSIAN_PEAK)) print('Number of Gaussian peaks in RAXR: '+str(NUMBER_GAUSSIAN_PEAK_FREEZE)) print('Number of RAXR datasets: '+str(NUMBER_RAXS_SPECTRA)) print('RAXR data fit mode: '+{'\'MI\''[1:3]:'Model independent fit','\'MD\''[1:3]:'Model dependent fit'}[RAXR_FIT_MODE[1:3]])
def make_gx_file(): mod = model.Model() config = io.Config() opt = diffev.DiffEv() par = parameters.Parameters() make_par_file() par.set_ascii_input_new(os.path.join(dump_path_locator(), 'table.tab')) io.load_gx('temp_DONOT_delete.gx', mod, opt, config) mod.script = make_script_file() mod.parameters = par mod.data, num_ctr = make_data_file() for i in range(len(mod.data.items) - num_ctr): mod.data.items[i + num_ctr].use = False io.save_gx(gx_file_name, mod, opt, config) print('gx file is saved in the current workdir!\n') print('##########Basic model info##########') print('Domain number: ' + str(len(domain_setup_HLT)) + ' half layer domains and ' + str(len(domain_setup_FLT)) + ' full layer domains') print('sorbate: ' + sorbate) local_structure_db = eval(LOCAL_STRUCTURE_MATCH_LIB) for key in local_structure_db.keys(): if sorbate in local_structure_db[key]: print('local_structure: ' + key) break else: pass binding_mode_db = [] for i in range(len(binding_mode)): if binding_mode[i][0].startswith('clean'): binding_mode_db.append('Clean_surface') elif binding_mode[i][0].startswith('CS'): binding_mode_db.append('Corner-sharing') elif binding_mode[i][0].startswith('ES'): binding_mode_db.append('Edge-sharing') elif binding_mode[i][0].startswith('TD'): binding_mode_db.append('Tridentate-binding') elif binding_mode[i][0].startswith('OS'): binding_mode_db.append('Outersphere binding') print('Binding_mode: ' + ','.join(binding_mode_db))
def save_as(frame, event): '''save_as(frame, event) --> None Event handler for save as ... ''' dlg = wx.FileDialog(frame, message="Save As", defaultFile="",\ wildcard="GenX File (*.gx)|*.gx",\ style=wx.FD_SAVE | wx.FD_CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: frame.model.set_script(frame.script_editor.GetText()) fname = dlg.GetPath() base, ext = os.path.splitext(fname) if ext == '': ext = '.gx' # print(base,ext) fname = base + ext result = True if os.path.exists(fname): filepath, filename = os.path.split(fname) result = ShowQuestionDialog(frame, \ 'The file %s already exists. Do you wish to overwrite it?'%filename\ , 'Overwrite?') if result: try: io.save_gx(fname, frame.model, frame.solver_control.optimizer,\ frame.config) except modellib.IOError as e: ShowModelErrorDialog(frame, e.__str__()) except Exception as e: outp = StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, 'Could not save the file. Python Error:'\ '\n%s'%(val,)) set_title(frame) dlg.Destroy()
best_fom=mod_raxr_mi.fom for i in range(len(mod_raxr_mi.parameters.data)): if mod_raxr_mi.parameters.data[i][2]==True: print 'scan ',mod_raxr_mi.parameters.data[i][0],' now' left,right=float(mod_raxr_mi.parameters.data[i][3]),float(mod_raxr_mi.parameters.data[i][4]) values=np.arange(left,right,(right-left)/scan_number) best=float(mod_raxr_mi.parameters.data[i][1]) fom_container=[] for value in values: mod_raxr_mi.parameters.data[i][1]=value mod_raxr_mi.simulate() fom_container.append(mod_raxr_mi.fom) if not np.where(values==value)[0][0]%10: print 'doing scan ',np.where(values==value)[0][0],'FOM=',mod_raxr_mi.fom values_sub=np.compress(np.array(fom_container) < best_fom*error_bar_level,np.array(values)) if len(values_sub)==0: mod_raxr_mi.parameters.data[i][1]=best print 'the range of this parameter is too large, either make a finer scan or shrink the range' else: left_error,right_error=np.min(values_sub)-best,np.max(values_sub)-best if right_error<0: right_error=right-best mod_raxr_mi.parameters.data[i][1]=best mod_raxr_mi.parameters.data[i][5]='(%3.6f,%3.6f)'%(left_error,right_error) #mod_raxr_mi.simulate() print 'after scan fom is ',mod_raxr_mi.fom print 'the error bar based on FOM scan is: %3.6f(%3.6f,%3.6f)'%(best,left_error,right_error) io.save_gx(file_path_raxr_mi.replace('.gx','_error_calculated.gx'),mod_raxr_mi,opt_raxr_mi,config_raxr_mi)
error_values = [] for index in range(n_elements): (error_low, error_high) = opt.calc_error_bar(\ index, error_bar_level) error_str = '(%.3e, %.3e,)'%(error_low, error_high) error_values.append(error_str) mod.parameters.set_error_pars(error_values) #export table if do_final_tab: print 'saving parameter table to file...' mod.export_table(os.path.join(mainpath,name + '_final.tab')) #save updated .gx file print 'saving updated .gx file (this may take a while)...' filehandling.save_gx(filename, mod, opt, config) # export atom table (label, element, x, y, z, occ, u) if do_atom_tab: print 'saving atom table to file...' exportfile = os.path.join(mainpath,name + '_atom_tab.dat') f = open(exportfile,'w') atomdata = mod.script_module.sample.create_uc_output() id = atomdata[6] el = atomdata[5] x = atomdata[0] y = atomdata[1] z = atomdata[2] oc = atomdata[3]
mod.simulate() temp_fom.append(mod.fom) fom_max, fom_min = max(temp_fom), min(temp_fom) fom_max_index, fom_min_index = temp_fom.index(fom_max), temp_fom.index( fom_min) par_max, par_min = np.arange( left, right, (right - left) / 5)[fom_max_index], np.arange( left, right, (right - left) / 5)[fom_min_index] slopes.append(abs((fom_max - fom_min) / (par_max - par_min))) print mod.parameters.get_value( i, 0 ), "par_max,par_min=", par_max, par_min, "sensitivity=", slopes[-1] mod.parameters.set_value(i, 1, val_init) else: slopes.append(-1000 + i) indexs = np.array(slopes).argsort()[::-1] print indexs for index in indexs: names.append(mod.parameters.get_value(index, 0)) values.append(mod.parameters.get_value(index, 1)) switch.append(mod.parameters.get_value(index, 2)) low.append(mod.parameters.get_value(index, 3)) high.append(mod.parameters.get_value(index, 4)) for i in range(len(indexs)): mod.parameters.set_value(i, 0, names[i]) mod.parameters.set_value(i, 1, values[i]) mod.parameters.set_value(i, 2, switch[i]) mod.parameters.set_value(i, 3, low[i]) mod.parameters.set_value(i, 4, high[i]) io.save_gx(sys.argv[2], mod, opt, config)
mod_raxr_mi = model.Model() config_raxr_mi = io.Config() opt_raxr_mi = diffev.DiffEv() mod_raxr_md = model.Model() config_raxr_md = io.Config() opt_raxr_md = diffev.DiffEv() io.load_gx(file_path_raxr_mi,mod_raxr_mi,opt_raxr_mi,config_raxr_mi) io.load_gx(file_path_raxr_md,mod_raxr_md,opt_raxr_md,config_raxr_md) first_grid_mi,first_grid_md=None,None for i in range(len(mod_raxr_mi.parameters.data)): if mod_raxr_mi.parameters.data[i][0]=='rgh_raxs.setA1': first_grid_mi=i break else: print mod_raxr_mi.parameters.data[i][0] for i in range(len(mod_raxr_md.parameters.data)): if mod_raxr_md.parameters.data[i][0]=='rgh_raxs.setA1': first_grid_md=i break else: print mod_raxr_md.parameters.data[i][0] print first_grid_mi,first_grid_md for i in range(first_grid_md,len(mod_raxr_md.parameters.data)): for k in range(6): mod_raxr_md.parameters.set_value(i,k,mod_raxr_mi.parameters.get_value(first_grid_mi+i-first_grid_md,k)) io.save_gx(file_path_raxr_md.replace('.gx','_merged.gx'),mod_raxr_md,opt_raxr_md,config_raxr_md)
for val in np.arange(left, right, (right - left) / 5): mod.parameters.set_value(i, 1, val) mod.simulate() temp_fom.append(mod.fom) fom_max, fom_min = max(temp_fom), min(temp_fom) fom_max_index, fom_min_index = temp_fom.index(fom_max), temp_fom.index(fom_min) par_max, par_min = ( np.arange(left, right, (right - left) / 5)[fom_max_index], np.arange(left, right, (right - left) / 5)[fom_min_index], ) slopes.append(abs((fom_max - fom_min) / (par_max - par_min))) print mod.parameters.get_value(i, 0), "par_max,par_min=", par_max, par_min, "sensitivity=", slopes[-1] mod.parameters.set_value(i, 1, val_init) else: slopes.append(-1000 + i) indexs = np.array(slopes).argsort()[::-1] print indexs for index in indexs: names.append(mod.parameters.get_value(index, 0)) values.append(mod.parameters.get_value(index, 1)) switch.append(mod.parameters.get_value(index, 2)) low.append(mod.parameters.get_value(index, 3)) high.append(mod.parameters.get_value(index, 4)) for i in range(len(indexs)): mod.parameters.set_value(i, 0, names[i]) mod.parameters.set_value(i, 1, values[i]) mod.parameters.set_value(i, 2, switch[i]) mod.parameters.set_value(i, 3, low[i]) mod.parameters.set_value(i, 4, high[i]) io.save_gx(sys.argv[2], mod, opt, config)
opt.get_create_trial()) except io.OptionError, e: print 'Could not write option solver.create trial' else: print 'Could not write config to file' ### end of block: save config # build outfile name outfile = infile outfile = outfile.replace('.gx', '_ran.gx') #outfile = '%s_%s_%s_kr%.2f_km%.2f_pf%.2f_run%d_ran.gx' % (outfile, trial,fom, kr, km, pf,iter) #outfile=outfile+fom+str(iter)+'_ran.gx' #outfile = '%s_%s_run%d.gx' % (outfile, TAG, iter) if rank == 0: print 'Saving the initial model to %s' % outfile io.save_gx(outfile, mod, opt, config) print '' print 'Settings:' print '---------' print 'Number of fit parameters = %s' % len(opt.best_vec) print 'FOM function = %s' % mod.fom_func.func_name print '' print 'opt.km = %s' % opt.km print 'opt.kr = %s' % opt.kr print 'opt.create_trial = %s' % opt.create_trial.im_func print '' print 'opt.use_parallel_processing = %s' % opt.use_parallel_processing print '' print 'opt.use_max_generations = %s' % opt.use_max_generations
sys.path.append('C:\\apps\\genx_pc_qiu') sys.path.append('C:\\apps\\genx_pc_qiu\\lib') sys.path.append('/u1/uaf/cqiu/genx_pc_qiu') import model,diffev,time,fom_funcs import filehandling as io import glob import numpy as np first_grid=54 grid_gap=20 path='/import/c/w/cqiu/temp_gx_files/' gx_files=glob.glob(path+'*_May8.gx')#file must be sortable to have the code work correctly gx_files.sort() mod = model.Model() config = io.Config() opt = diffev.DiffEv() io.load_gx(gx_files[0],mod,opt,config) for gx_file in gx_files[1:]: print "processing ",gx_file i=gx_files.index(gx_file) begin_grid=first_grid+grid_gap*i mod_temp = model.Model() config_temp = io.Config() opt_temp = diffev.DiffEv() io.load_gx(gx_file,mod_temp,opt_temp,config_temp) for grid_index in range(begin_grid,begin_grid+grid_gap): for k in range(5): mod.parameters.set_value(grid_index,k,mod_temp.parameters.get_value(grid_index,k)) io.save_gx(path+"combined_model_file.gx",mod,opt,config)
try: config.set('solver', 'create trial',\ opt.get_create_trial()) except io.OptionError, e: print 'Could not write option solver.create trial' else: print 'Could not write config to file' ### end of block: save config # build outfile names outfile = infile.replace('.gx', str(rank / (size / split_jobs)) + '_ran.gx') if rank_group == 0: print 'Saving the initial model to %s' % outfile io.save_gx(outfile, mod, opt, config) if rank == 0: print '' print 'Settings:' print '---------' print 'Number of fit parameters = %s' % len(opt.best_vec) print 'FOM function = %s' % mod.fom_func.func_name print '' print 'opt.km = %s' % opt.km print 'opt.kr = %s' % opt.kr print 'opt.create_trial = %s' % opt.create_trial.im_func print '' print 'opt.use_parallel_processing = %s' % opt.use_parallel_processing print '' print 'opt.use_max_generations = %s' % opt.use_max_generations
opt.get_create_trial()) except io.OptionError, e: print 'Could not write option solver.create trial' else: print 'Could not write config to file' ### end of block: save config # build outfile name outfile = infile outfile = outfile.replace('.gx','') #outfile = '%s_%s_%s_kr%.2f_km%.2f_pf%.2f_run%d.gx' % (outfile, trial,fom, kr, km, pf,iter) outfile=outfile+fom+str(iter)+'_ran.gx' #outfile = '%s_%s_run%d.gx' % (outfile, TAG, iter) if rank==0: print 'Saving the initial model to %s'%outfile io.save_gx(outfile, mod, opt, config) print '' print 'Settings:' print '---------' print 'Number of fit parameters = %s' % len(opt.best_vec) print 'FOM function = %s' % mod.fom_func.func_name print '' print 'opt.km = %s' % opt.km print 'opt.kr = %s' % opt.kr print 'opt.create_trial = %s' % opt.create_trial.im_func print '' print 'opt.use_parallel_processing = %s' % opt.use_parallel_processing print '' print 'opt.use_max_generations = %s' % opt.use_max_generations
try: config.set('solver', 'create trial',\ opt.get_create_trial()) except io.OptionError, e: print 'Could not write option solver.create trial' else: print 'Could not write config to file' ### end of block: save config # build outfile name outfile = infile outfile = outfile.replace('.gx','') outfile = '%s_%s_kr%.2f_km%.2f_run%d.gx' % (outfile, fom, kr, km, iter) print 'Saving the initial model to %s'%outfile filehandling.save_gx(outfile, mod, opt, config) print '' print 'Settings:' print '---------' print 'Number of fit parameters = %s' % len(opt.best_vec) print 'FOM function = %s' % mod.fom_func.func_name print '' print 'opt.km = %s' % opt.km print 'opt.kr = %s' % opt.kr print 'opt.create_trial = %s' % opt.create_trial.im_func print '' print 'opt.use_parallel_processing = %s' % opt.use_parallel_processing print 'opt.chunksize = %s' % opt.chunksize print 'opt.processes = %s' % opt.processes
best = float(mod_raxr_mi.parameters.data[i][1]) fom_container = [] for value in values: mod_raxr_mi.parameters.data[i][1] = value mod_raxr_mi.simulate() fom_container.append(mod_raxr_mi.fom) if not np.where(values == value)[0][0] % 10: print 'doing scan ', np.where( values == value)[0][0], 'FOM=', mod_raxr_mi.fom values_sub = np.compress( np.array(fom_container) < best_fom * error_bar_level, np.array(values)) if len(values_sub) == 0: mod_raxr_mi.parameters.data[i][1] = best print 'the range of this parameter is too large, either make a finer scan or shrink the range' else: left_error, right_error = np.min(values_sub) - best, np.max( values_sub) - best if right_error < 0: right_error = right - best mod_raxr_mi.parameters.data[i][1] = best mod_raxr_mi.parameters.data[i][5] = '(%3.6f,%3.6f)' % (left_error, right_error) #mod_raxr_mi.simulate() print 'after scan fom is ', mod_raxr_mi.fom print 'the error bar based on FOM scan is: %3.6f(%3.6f,%3.6f)' % ( best, left_error, right_error) io.save_gx(file_path_raxr_mi.replace('.gx', '_error_calculated.gx'), mod_raxr_mi, opt_raxr_mi, config_raxr_mi)
def autosave(): #print 'Updating the parameters' mod.parameters.set_value_pars(opt.best_vec) io.save_gx(outfile, mod, opt, config)
try: config.set('solver', 'create trial',\ opt.get_create_trial()) except io.OptionError, e: print 'Could not write option solver.create trial' else: print 'Could not write config to file' ### end of block: save config # build outfile names outfile = infile.replace('.gx',str(rank/(size/split_jobs))+'_ran.gx') if rank_group==0: print 'Saving the initial model to %s'%outfile io.save_gx(outfile, mod, opt, config) if rank==0: print '' print 'Settings:' print '---------' print 'Number of fit parameters = %s' % len(opt.best_vec) print 'FOM function = %s' % mod.fom_func.func_name print '' print 'opt.km = %s' % opt.km print 'opt.kr = %s' % opt.kr print 'opt.create_trial = %s' % opt.create_trial.im_func print '' print 'opt.use_parallel_processing = %s' % opt.use_parallel_processing print '' print 'opt.use_max_generations = %s' % opt.use_max_generations