示例#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
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)
示例#3
0
def open_model(frame, path):
    #print "open_model"
    frame.model.new_model()
    frame.paramter_grid.PrepareNewModel()
    # Update all components so all the traces are gone.
    #_post_new_model_event(frame, frame.model)
    try:
        io.load_file(path, frame.model, frame.solver_control.optimizer, frame.config)
    except modellib.IOError, e:
        ShowModelErrorDialog(frame, e.__str__())
示例#4
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)
示例#5
0
def start_interactive(args):
    ''' Start genx in interactive mode (with the gui)

    :param args:
    :return:
    '''
    try:
        import genx_gui
    except ImportError:
        import genx
        genx_path = os.path.split(os.path.abspath(genx.__file__))[0]
        sys.path.insert(0, genx_path)
        import genx_gui
    if args.infile.endswith('.gx') or args.infile.endswith('.hgx'):
        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(args.infile)
        try:
            io.load_file(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,))
示例#6
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()
示例#7
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'
示例#8
0
                lab.pack(side=LEFT)
                ent.pack(side=RIGHT, expand=YES, fill=X)
                entries[field] = ent
            return entries


if __name__ == '__main__':
    root = Tk()
    ents = makeform(root, fields)
    root.bind('<Return>', (lambda event, e=ents: fetch(e)))
    b1 = Button(root, text='Convert IPA Equ',
                command=(lambda e=ents: IPAEquivalent(e)))

    b1.pack(side=LEFT, padx=5, pady=5)
    b2 = Button(root, text='fileinputoutput',
                command=(lambda e=ents: load_file(e)))
    b2.pack(side=LEFT, padx=5, pady=5)

    b4 = Button(root, text='Syllable@Underlying Word',
                command=(lambda e=ents: Syllabify(e)))
    b4.pack(side=LEFT, padx=5, pady=5)
    b5 = Button(root, text='Label@Underlying Word',
                command=(lambda e=ents: Labeling(e)))
    b5.pack(side=LEFT, padx=5, pady=5)

    b7 = Button(root, text='Phoneme Level',
                command=(lambda e=ents: Phoneme(e)))
    b7.pack(side=LEFT, padx=5, pady=5)

    b3 = Button(root, text='Quit', command=root.quit)
    b3.pack(side=LEFT, padx=5, pady=5)