예제 #1
0
def extract_parameters(args):
    """Extracts the parameters to outfile"""

    import model
    import diffev
    import filehandling as io

    # Open the genx file
    mod = model.Model()
    config = io.Config()
    config.load_default(
        os.path.split(os.path.abspath(__file__))[0] + 'genx.conf')
    opt = diffev.DiffEv()

    print "Loading file: %s " % args.infile
    io.load_file(args.infile, mod, opt, config)
    print "File loaded"

    names, values = mod.parameters.get_sim_pars()

    if args.outfile == '':
        outfile = sys.stdout
        fout = open(outfile, 'w')
    else:
        outfile = args.outfile
        if os.path.isfile(outfile):
            fout = open(outfile, 'a')
        else:
            fout = open(outfile, 'w')
            # Add header
            fout.write('\t'.join([name for name in names]) + '\n')
    fout.write('\t'.join(['%f' % val for val in values]) + '\n')
    fout.close()
예제 #2
0
파일: solvergui.py 프로젝트: klauer/genx
    def __init__(self, parent, config=None):
        # Create the optimizer we are using. In this case the standard
        # Differential evolution optimizer.
        self.optimizer = diffev.DiffEv()
        # Store the parent we need this to bind the different components to
        # the optimization algorithm.
        self.parent = parent
        self.config = config

        # Just storage of the starting values for the paramters before
        # the fit is started so the user can go back to the start values
        self.start_parameter_values = None
        # The level used for error bar calculations
        self.fom_error_bars_level = 1.05

        # Setup the output functions.
        self.optimizer.set_text_output_func(self.TextOutput)
        self.optimizer.set_plot_output_func(self.PlotOutput)
        self.optimizer.set_parameter_output_func(self.ParameterOutput)
        self.optimizer.set_fitting_ended_func(self.FittingEnded)
        self.optimizer.set_autosave_func(self.AutoSave)

        self.parent.Bind(EVT_FITTING_ENDED, self.OnFittingEnded)

        # Now load the default configuration
        self.ReadConfig()
예제 #3
0
def create_simulated_data(args):
    """Function to create simulated data from the model and add it to data.y"""

    import model
    import diffev
    import filehandling as io

    from scipy.stats import poisson

    mod = model.Model()
    config = io.Config()
    config.load_default(
        os.path.split(os.path.abspath(__file__))[0] + 'genx.conf')
    opt = diffev.DiffEv()

    print "Loading file: %s " % args.infile
    io.load_file(args.infile, mod, opt, config)
    #io.load_opt_config(opt, config)
    print "File loaded"

    print "Simualting.."
    mod.simulate()
    print "Storing simualted data"
    for data_set in mod.data:
        data_set.y_raw = poisson.rvs(data_set.y_sim)
        data_set.y_command = 'y'
        data_set.run_y_command()
        data_set.error_command = 'sqrt(where(y > 0, y, 1.0))'
        data_set.run_error_command()

    print 'Saving the model to %s' % args.outfile
    io.save_file(args.outfile, mod, opt, config)
예제 #4
0
def make_par_file():
    mod = model.Model()
    config = io.Config()
    opt = diffev.DiffEv()
    mod.script=make_script_file()
    #mod.simulate()
    try:
        mod.simulate()
    except:
       pass
예제 #5
0
def modify_file(args):
    """Modify a GenX file given command line arguments"""
    import model
    import diffev
    import filehandling as io

    # Open the genx file
    mod = model.Model()
    config = io.Config()
    config.load_default(
        os.path.split(os.path.abspath(__file__))[0] + 'genx.conf')
    opt = diffev.DiffEv()

    print "Loading file: %s " % args.infile
    io.load_file(args.infile, mod, opt, config)
    print "File loaded"

    if args.datafile:
        datafile = os.path.abspath(args.datafile)
        if 0 > args.data_set or args.data_set >= len(mod.data):
            print "The selected data set does not exist - select one between 0 and %d" % (
                len(mod.data) - 1)
            return
        print 'Loading dataset %s into data set %d' % (datafile, args.data_set)
        mod.data[args.data_set].loadfile(datafile)

        if args.outfile:
            print 'Saving the fit to %s' % args.outfile
            io.save_file(args.outfile, mod, opt, config)

    elif args.save_datafile:
        save_datafile = os.path.abspath(args.save_datafile)
        print "Simualting.."
        mod.simulate()
        if 0 > args.data_set or args.data_set >= len(mod.data):
            print "The selected data set does not exist - select one between 0 and %d" % (
                len(mod.data) - 1)
            return
        print 'Exporting data set %d into ASCII file %s' % (args.data_set,
                                                            save_datafile)
        mod.data[args.data_set].save_file(save_datafile)
예제 #6
0
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]])
예제 #7
0
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))
예제 #8
0
use_boundaries = True
use_autosave = True
autosave_interval = 50
max_log = 600000

# Sleep time between generations
sleep_time = 0.000001

###############################################################################
# End of parameter section
#-------------------------
# DO NOT MODIFY CODE BELOW
###############################################################################
mod = model.Model()
config = io.Config()
opt = diffev.DiffEv()


def autosave():
    #print 'Updating the parameters'
    mod.parameters.set_value_pars(opt.best_vec)
    io.save_gx(outfile, mod, opt, config)


opt.set_autosave_func(autosave)

par_list = [(trial,f,rm,i) for trial in create_trial for f in fom_list for rm in krkmPf_list \
            for i in iter_list]

tmp_fom = []
tmp_trial_vec = []
예제 #9
0
    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()
예제 #10
0
import os,sys
sys.path.append('P://apps//genx_pc_qiu')
sys.path.append('P://apps//genx_pc_qiu//models')
sys.path.append('P://apps//genx_pc_qiu//lib')

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]
예제 #11
0
def sld_mc(args):
    """ Function to start fitting from the command line.

    :param args:
    :return:
    """
    import model
    import diffev
    import filehandling as io

    mod = model.Model()
    config = io.Config()
    config.load_default(
        os.path.split(os.path.abspath(__file__))[0] + 'genx.conf')
    opt = diffev.DiffEv()

    print 'Loading model %s...' % args.infile
    io.load_file(args.infile, mod, opt, config)
    io.load_opt_config(opt, config)
    # Hack the script so that it simulates the SLD instead of the data
    mod.script = re.sub(r"SimSpecular\((.*)\,(.*)\)",
                        r"SimSLD(data[0].x, None,\2)", mod.script)
    print "Hacking the model script. Resulting script:"
    print mod.script

    # Simulate, this will also compile the model script
    print 'Compiling model...'
    mod.compile_script()

    (funcs, vals, minvals, maxvals) = mod.get_fit_pars()
    vals = np.array(vals)
    boundaries = [
        row[5] for row in mod.parameters.data if not row[0] == '' and row[2]
    ]
    print boundaries
    boundaries = np.array([eval(s) for s in boundaries])
    minvals, maxvals = boundaries[:, 0] + vals, boundaries[:, 1] + vals
    min_SLD = []
    max_SLD = []
    z = np.arange(args.min, args.max, args.step)
    # Change the x-data so that it contain the z values instead.
    for d in mod.data:
        d.x = z

    def extreme_dict(cur, extreme, func):
        """Makes a comparison of cur and extreme through func (np.min or np.max) and returns the result as a dict"""
        return_dic = {}
        for key in extreme:
            if key not in ['z', 'SLD unit']:
                #print cur[key].shape
                return_dic[key] = func(np.c_[cur[key], extreme[key]], axis=1)
            else:
                return_dic[key] = cur[key]
        return return_dic

    print "Calculating sld's..."
    missed = 0
    for i in range(args.runs):
        current_vals = minvals + (maxvals - minvals) * np.random.random_sample(
            len(funcs))
        [func(val) for func, val in zip(funcs, current_vals)]

        mod.script_module._sim = False
        current_sld = mod.script_module.Sim(mod.data)
        same_shape = all([
            sld['z'].shape == msld['z'].shape
            for sld, msld in zip(current_sld, min_SLD)
        ])
        if i == 0:
            min_SLD = [sld for sld in current_sld]
            max_SLD = [sld for sld in current_sld]
        elif same_shape:
            min_SLD = [
                extreme_dict(sld, msld, np.min)
                for sld, msld in zip(current_sld, min_SLD)
            ]
            max_SLD = [
                extreme_dict(sld, msld, np.max)
                for sld, msld in zip(current_sld, max_SLD)
            ]
        else:
            missed += 1

        sys.stdout.write("\r Progress: %d%%" %
                         (float(i) * 100 / float(args.runs)))
        sys.stdout.flush()

    print ' '
    print missed, " simulations was discarded due to wrong size."
    print "Saving the data to file..."
    for sim in range(len(min_SLD)):
        new_filename = (args.outfile + '%03d' % sim + '.dat')
        save_array = np.array([min_SLD[sim]['z']])
        header = 'z\t'
        for key in min_SLD[sim]:
            if key != 'z' and key != 'SLD unit':
                save_array = np.r_[save_array, [min_SLD[sim][key].real],
                                   [min_SLD[sim][key].imag],
                                   [max_SLD[sim][key].real],
                                   [max_SLD[sim][key].imag]]
                header += 'min(%s.real)\tmin(%s.imag)\tmax(%s.real)\tmax(%s.imag)\t' % (
                    key, key, key, key)
        f = open(new_filename, 'w')
        f.write(
            "# Monte Carlo estimation of SLD bounds with script sld_errorbars.py model taken from file: %s\n"
            % args.infile)
        f.write("# File created: %s\n" % time.ctime())
        f.write("# Simulated SLD for data set: %s\n" % mod.data[sim].name)
        f.write("# Headers: \n")
        f.write('#' + header + '\n')
        np.savetxt(f, save_array.transpose(), delimiter='\t')
        f.close()
예제 #12
0
def start_fitting(args, rank=0):
    """ Function to start fitting from the command line.

    :param args:
    :return:
    """
    import time
    import model
    import diffev
    import filehandling as io

    mod = model.Model()
    config = io.Config()
    config.load_default(
        os.path.split(os.path.abspath(__file__))[0] + 'genx.conf')
    opt = diffev.DiffEv()

    if rank == 0:

        def autosave():
            #print 'Updating the parameters'
            mod.parameters.set_value_pars(opt.best_vec)
            if args.error:
                print "Calculating error bars"
                calc_errorbars(config, mod, opt)
            if args.outfile:
                print "Saving to %s" % args.outfile
                io.save_file(args.outfile, mod, opt, config)

        opt.set_autosave_func(autosave)

    if rank == 0:
        print 'Loading model %s...' % args.infile
    io.load_file(args.infile, mod, opt, config)
    io.load_opt_config(opt, config)
    # has to be used in order to save everything....
    if args.esave:
        config.set('solver', 'save all evals', True)
    # Simulate, this will also compile the model script
    if rank == 0:
        print 'Simulating model...'
    mod.simulate()

    # Sets up the fitting ...
    if rank == 0:
        print 'Setting up the optimizer...'
    set_optimiser_pars(opt, args)
    opt.reset()
    opt.init_fitting(mod)
    opt.init_fom_eval()
    opt.set_sleep_time(0.0)

    if args.outfile and rank == 0:
        print 'Saving the initial model to %s' % args.outfile
        io.save_file(args.outfile, mod, opt, config)

    # To start the fitting
    if rank == 0:
        print 'Fitting starting...'
        t1 = time.time()
    #print opt.use_mpi, opt.use_parallel_processing
    opt.optimize()
    if rank == 0:
        t2 = time.time()
        print 'Fitting finished!'
        print 'Time to fit: ', (t2 - t1) / 60., ' min'

    if rank == 0:
        print 'Updating the parameters'
        mod.parameters.set_value_pars(opt.best_vec)

    if args.outfile and rank == 0:
        if args.error:
            print 'Calculating errorbars'
            calc_errorbars(config, mod, opt)
        print 'Saving the fit to %s' % args.outfile
        opt.set_use_mpi(False)
        io.save_file(args.outfile, mod, opt, config)

    if rank == 0:
        print 'Fitting successfully completed'