def open(frame, event): ''' open(frame, event) --> None Event handler for opening a model file... ''' # Check so the model is saved before quitting if not frame.model.saved: ans = ShowQuestionDialog(frame, 'If you continue any changes in' 'your model will not be saved.', 'Model not saved') if not ans: return dlg = wx.FileDialog(frame, message="Open", defaultFile="",\ wildcard="GenX File (*.gx)|*.gx",\ style=wx.OPEN | wx.CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() try: io.load_gx(path, 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 open the file. Python Error:'\ '\n%s'%(val,)) return
def open_model(frame, path): frame.model.new_model() # Update all components so all the traces are gone. _post_new_model_event(frame, frame.model) try: io.load_gx(path, frame.model,\ frame.solver_control.optimizer,\ frame.config) except modellib.IOError, e: ShowModelErrorDialog(frame, e.__str__())
def open_model(frame, path): frame.model.new_model() # Update all components so all the traces are gone. _post_new_model_event(frame, frame.model) #io.load_gx(path, frame.model,\ # frame.solver_control.optimizer,\ # frame.config) try: io.load_gx(path, frame.model,\ frame.solver_control.optimizer,\ frame.config) except modellib.IOError as e: print(e.__str__()) ShowModelErrorDialog(frame, e.__str__()) except Exception as e: #outp = StringIO.StringIO() outp = StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, 'Could not open the file. Python Error:'\ '\n%s'%(val,)) return try: [p.ReadConfig() for p in get_pages(frame)] except Exception as e: outp = StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() print(val) ShowErrorDialog( frame, 'Could not read the config for the' ' plots. Python Error:\n%s' % (val, )) # Letting the plugin do their stuff... try: frame.plugin_control.OnOpenModel(None) except Exception as e: outp = StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, 'Problems when plugins processed model.'\ ' Python Error:\n%s'%(val,)) frame.main_frame_statusbar.SetStatusText('Model loaded from file',\ 1) # Post an event to update everything else _post_new_model_event(frame, frame.model) # Needs to put it to saved since all the widgets will have # been updated frame.model.saved = True 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))
sys.path.append('P://apps//genx_pc_qiu//lib') import numpy as np import model,diffev import filehandling as io path='P://temp_model' file_path_raxr_mi=os.path.join(path,'Pb_3domains_cmp_1_1_2_waterpair_bv_constrained_run1_May30_ran.gx') scan_number=50 error_bar_level=1.05 mod_raxr_mi = model.Model() config_raxr_mi = io.Config() opt_raxr_mi = diffev.DiffEv() io.load_gx(file_path_raxr_mi,mod_raxr_mi,opt_raxr_mi,config_raxr_mi) first_grid_mi,first_grid_md=None,None print 'scan number is ',scan_number print 'error bar level is ',error_bar_level mod_raxr_mi.simulate() 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])
tmp_fom=[] tmp_trial_vec=[] tmp_pop_vec=[] tmp_fom_vec=[] for pars in par_list: trial=pars[0] fom = pars[1] km = pars[2][0] # km kr = pars[2][1] # kr pf=pars[2][2] iter = pars[3] # Load the model ... if rank==0: print 'Loading model %s...'%infile io.load_gx(infile, mod, opt, config) # Simulate, this will also compile the model script if rank==0: print 'Simulating model...' mod.simulate() # Setting up the solver eval('mod.set_fom_func(fom_funcs.%s)' % fom) # Lets set the solver parameters: try: opt.set_create_trial(trial) except: print 'Warning: create_trial is not defined in script.' try: opt.set_kr(kr) except:
import os, sys os.chdir("C:\\apps\\genx\\") import model, diffev, time, fom_funcs import filehandling as io import glob import numpy as np mod = model.Model() config = io.Config() opt = diffev.DiffEv() io.load_gx(sys.argv[1], mod, opt, config) slopes = [] names, values, switch, low, high = [], [], [], [], [] for i in range(mod.parameters.get_len_rows()): if (mod.parameters.get_value(i, 0) != '') & (mod.parameters.get_value( i, 2) == True): left, right = mod.parameters.get_value(i, 3), mod.parameters.get_value( i, 4) val_init = mod.parameters.get_value(i, 1) temp_fom = [] 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]
for filename in sys.argv[1:]: print "\n\n--------------------" print "Now exporting: %s" %filename name = os.path.basename(filename) # filename without path name = os.path.splitext(name)[0] # remove the filename extension mainpath = os.path.dirname(filename) # path of file withouth filename import model, diffev, filehandling, time mod = model.Model() opt = diffev.DiffEv() config = filehandling.Config() # Load the model ... print 'Loading model %s...'%filename filehandling.load_gx(filename, mod, opt, config) # Simulate, this will also compile the model script print 'Simulating model...' mod.simulate() # Setting up the solver #opt.pickle_load(mod.load_addition('optimizer')) # export the fom as function of generation if do_fom_log: print 'saving fom log to file...' exportfile = os.path.join(mainpath,name + '_fom_log.dat') np.savetxt(exportfile, opt.fom_log) # export the fom array of all individuals
import os, sys os.chdir("C:\\apps\\genx\\") import model, diffev, time, fom_funcs import filehandling as io import glob import numpy as np mod = model.Model() config = io.Config() opt = diffev.DiffEv() io.load_gx(sys.argv[1], mod, opt, config) slopes = [] names, values, switch, low, high = [], [], [], [], [] for i in range(mod.parameters.get_len_rows()): if (mod.parameters.get_value(i, 0) != "") & (mod.parameters.get_value(i, 2) == True): left, right = mod.parameters.get_value(i, 3), mod.parameters.get_value(i, 4) val_init = mod.parameters.get_value(i, 1) temp_fom = [] 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)))
import model,diffev import filehandling as io path='P:\\My stuff\\Models\\CTR models\\Zr models for final publication\\refit results\\GenX' file_path_raxr_mi=os.path.join(path,'Good_MI_RAXR_refit_Zr_100mM_NaCl_6O_run2_Apr3combined_ran.gx') file_path_raxr_md=os.path.join(path,'MD_RAXR_refit_Zr_100mM_NaCl_6O_run2_Apr3_best_1_bin_R1_weighted_2_kr0.90_km0.90_pf0.80_run1_ran.gx') 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:
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)
def extract_xy_2(data, i): x, y, err, index_use = [], [], [], [] each = data[i] x = x + list(each.x) y = y + list(each.y) err = err + list(each.error) index_use.append(i) return x, y, err, index_use for i in i_container: #looping through 21 RAXR datasets normally mod = model.Model() config = io.Config() opt = diffev.DiffEv() if i == 0: io.load_gx(infile, mod, opt, config) else: io.load_gx(outfile, mod, opt, config) data_sets = mod.data.items bounds = [] x, y, err, index_use = extract_xy_2(data_sets, i + 1) bounds, best_vec, par_indx = extract_bounds_2(mod.parameters, i + 1) #set the data used and the associated pars columns for j in range(len(data_sets)): if j not in index_use: mod.data.items[j].use = False else: mod.data.items[j].use = True for j in range(mod.parameters.get_len_rows()):
line_start_pos=loc while str_text[loc]!='\n': if str_text[loc]=='#': comment_pos=loc elif str_text[loc]=='=': equal_sign_pos=loc else: pass loc+=1 line_end_pos=loc if comment_pos==None: comment_pos=line_end_pos text_to_be_feed=key_text+'='+str(replace_value)+str_text[comment_pos:line_end_pos] str_text=str_text.replace(str_text[line_start_pos:line_end_pos],text_to_be_feed,1) return str_text if __name__=="__main__": mod = model.Model() config = io.Config() opt = diffev.DiffEv() io.load_gx(gx_file_path,mod,opt,config) mod.script=replace_script_section(mod.script,'running_mode','0') for i in range(len(mod.data.items)): mod.data.items[i].use=True print('Simulate and dump files now!') mod.simulate() print('Plot files are dumpt to pocket!') print('Plot the results now!') plot_all(plot_e_model=plot_e_model,plot_e_FS=plot_e_FS,plot_ctr=plot_ctr,plot_raxr=plot_raxr,plot_AP_Q=plot_AP_Q) pyplot.show()
if len(sys.argv)==2 and sys.argv[1].endswith('.gx'): app = genx_gui.MyApp(False, 0) # Create the window app.Yield() frame=app.TopWindow # load a model on start import filehandling as io import StringIO, traceback from event_handlers import ShowModelErrorDialog, ShowErrorDialog, get_pages, \ _post_new_model_event, set_title import model as modellib path = os.path.abspath(sys.argv[1]) try: io.load_gx(path, 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 open the file. Python Error:'\ '\n%s'%(val,)) else: app.Yield() try: [p.ReadConfig() for p in get_pages(frame)] except Exception, e:
if len(sys.argv) == 2 and sys.argv[1].endswith('.gx'): app = genx_gui.MyApp(False, 0) # Create the window app.Yield() frame = app.TopWindow # load a model on start import filehandling as io import StringIO, traceback from event_handlers import ShowModelErrorDialog, ShowErrorDialog, get_pages, \ _post_new_model_event, set_title import model as modellib path = os.path.abspath(sys.argv[1]) try: io.load_gx(path, 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 open the file. Python Error:'\ '\n%s'%(val,)) else: app.Yield() try: [p.ReadConfig() for p in get_pages(frame)] except Exception, e:
import os, sys os.chdir("C:\\apps\\genx\\") import model, diffev, time, fom_funcs import filehandling as io import glob #python create_tab_files D:\\Google Drive\\modeling files\\GenX model file\\fitting with brute force\\RAXS\\ * RAXS path = sys.argv[1] files = glob.glob(path + sys.argv[2]) for file in files: mod = model.Model() config = io.Config() opt = diffev.DiffEv() io.load_gx(file, mod, opt, config) content = mod.parameters.get_ascii_output() file_list = file.rsplit('\\')[-1].rsplit('_') tab_file_name = sys.argv[3] + '_' + file_list[4] + '_R' + file_list[-1][ -4] + '.tab' f = open(path + tab_file_name, 'w') f.write(content) f.close()
tmp_fom = [] tmp_trial_vec = [] tmp_pop_vec = [] tmp_fom_vec = [] for pars in par_list: trial = pars[0] fom = pars[1] km = pars[2][0] # km kr = pars[2][1] # kr pf = pars[2][2] iter = pars[3] # Load the model ... if rank == 0: print 'Loading model %s...' % infile io.load_gx(infile, mod, opt, config) for spectra in spectras: # Simulate, this will also compile the model script if rank == 0: print 'Simulating model...' mod.simulate() # Setting up the solver eval('mod.set_fom_func(fom_funcs.%s)' % fom) # Lets set the solver parameters (same for all processors): try: opt.set_create_trial(trial) except: print 'Warning: create_trial is not defined in script.' try:
tmp_fom=[] tmp_trial_vec=[] tmp_pop_vec=[] tmp_fom_vec=[] for pars in par_list: trial=pars[0] fom = pars[1] km = pars[2][0] # km kr = pars[2][1] # kr pf=pars[2][2] iter = pars[3] # Load the model ... if rank==0: print 'Loading model %s...'%infile io.load_gx(infile, mod, opt, config) for spectra in spectras: # Simulate, this will also compile the model script if rank==0: print 'Simulating model...' mod.simulate() # Setting up the solver eval('mod.set_fom_func(fom_funcs.%s)' % fom) # Lets set the solver parameters (same for all processors): try: opt.set_create_trial(trial) except: print 'Warning: create_trial is not defined in script.'