コード例 #1
0
def extract_parameters(args):
    """Extracts the parameters to outfile"""

    from genx import model
    from genx import diffev
    from genx 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()

    iprint("Loading file: %s " % args.infile)
    io.load_file(args.infile, mod, opt, config)
    iprint("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"""

    from genx import model
    from genx import diffev
    from genx 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()

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

    iprint("Simualting..")
    mod.simulate()
    iprint("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()

    iprint('Saving the model to %s' % args.outfile)
    io.save_file(args.outfile, mod, opt, config)
コード例 #3
0
ファイル: api.py プロジェクト: aglavic/genx
def load(fname, compile=True):
    model = Model()
    optimizer = DiffEv()
    io.load_file(fname, model, optimizer, _config)
    if compile:
        model.compile_script()
    optimizer.model = model
    return model, optimizer
コード例 #4
0
def modify_file(args):
    """Modify a GenX file given command line arguments"""
    from genx import model
    from genx import diffev
    from genx 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()

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

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

        if args.outfile:
            iprint('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)
        iprint("Simualting..")
        mod.simulate()
        if 0 > args.data_set or args.data_set >= len(mod.data):
            iprint(
                "The selected data set does not exist - select one between 0 and %d"
                % (len(mod.data) - 1))
            return
        iprint('Exporting data set %d into ASCII file %s' %
               (args.data_set, save_datafile))
        mod.data[args.data_set].save_file(save_datafile)
コード例 #5
0
ファイル: event_handlers.py プロジェクト: kibbi10/genx
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 as e:
        ShowModelErrorDialog(frame, e.__str__())
    except Exception as e:
        outp = io.StringIO()
        traceback.print_exc(200, outp)
        val = outp.getvalue()
        outp.close()
        print('Error in loading the file ', path, '. Pyton traceback:\n ', val)
        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 = io.StringIO()
        traceback.print_exc(200, outp)
        val = outp.getvalue()
        outp.close()
        print('Error in loading config for the plots. Pyton traceback:\n ',
              val)
        ShowErrorDialog(
            frame,
            'Could not read the config for the plots. Python Error:\n%s' %
            (val, ))
    try:
        frame.paramter_grid.ReadConfig()
    except Exception as e:
        outp = io.StringIO()
        traceback.print_exc(200, outp)
        val = outp.getvalue()
        outp.close()
        print(
            'Error in loading config for parameter grid. Pyton traceback:\n ',
            val)
        ShowErrorDialog(
            frame,
            'Could not read the config for the parameter grid. Python Error:\n%s'
            % (val, ))
    else:
        # Update the Menu choice
        frame.mb_view_grid_slider.Check(
            frame.paramter_grid.GetValueEditorSlider())
    # Letting the plugin do their stuff...
    try:
        frame.plugin_control.OnOpenModel(None)
    except Exception as e:
        outp = io.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)
コード例 #6
0
ファイル: sld_errorbars.py プロジェクト: Traecp/genx-py3
def sld_mc(args):
    """ Function to start fitting from the command line.

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

    mod = genx.model.Model()
    config = io.Config()

    config_loaded = False
    for loc in appdirs.user_config_dir(
            'genx',
            'MattsBjorck'), os.path.expanduser("~"), appdirs.site_config_dir(
                'genx', 'MattsBjorck').replace('xdg', ''):
        try:
            config.load_default(os.path.join(loc, 'genx.conf'))
        except IOError:
            pass
        else:
            config_loaded = True
            break

    if not config_loaded:
        raise IOError('Could not load default config file', 'genx.conf')

    opt = genx.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
    from genx import model
    from genx import diffev
    from genx 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:
                iprint("Calculating error bars")
                calc_errorbars(config, mod, opt)
            if args.outfile:
                iprint("Saving to %s" % args.outfile)
                io.save_file(args.outfile, mod, opt, config)

        opt.set_autosave_func(autosave)

    if rank == 0:
        iprint('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:
        iprint('Simulating model...')
    mod.simulate()

    # Sets up the fitting ...
    if rank == 0:
        iprint('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:
        iprint('Saving the initial model to %s' % args.outfile)
        io.save_file(args.outfile, mod, opt, config)

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

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

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

    if rank == 0:
        iprint('Fitting successfully completed')
コード例 #8
0
def start_interactive(args):
    ''' Start genx in interactive mode (with the gui)

    :param args:
    :return:
    '''
    debug('enter start_interactive')
    activate_excepthook()
    # Fix blurry text on Windows 10
    import ctypes
    try:
        ctypes.windll.shcore.SetProcessDpiAwareness(True)
    except:
        pass

    from genx import genx_gui
    if args.infile.endswith('.gx') or args.infile.endswith('.hgx'):
        debug('start GUI setup with file to load')
        app = genx_gui.MyApp(False, 0)
        # Create the window
        app.Yield()
        frame = app.TopWindow
        # load a model on start
        from genx import filehandling as io
        import traceback
        from io import StringIO
        from genx.event_handlers import ShowModelErrorDialog, ShowErrorDialog, get_pages, \
            _post_new_model_event, set_title
        from genx 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 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 open the file. Python Error:' \
                                   '\n%s'%(val,))
        else:
            app.Yield()
            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()
                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)
        app.Yield()
        # 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)
        app.Yield()
        # Just a force update of the data_list
        frame.data_list.list_ctrl.SetItemCount(
            frame.data_list.list_ctrl.data_cont.get_count())
        # Updating the imagelist as well
        frame.data_list.list_ctrl._UpdateImageList()
        frame.plot_data.plot_data(frame.model.data)
        frame.paramter_grid.SetParameters(frame.model.parameters)
        debug('setup complete, start WX MainLoop')
        app.MainLoop()
    elif args.infile == '':
        debug('start GUI setup')
        app = genx_gui.MyApp(True, 0)
        debug('setup complete, start WX MainLoop')
        app.MainLoop()
    else:
        iprint('Wrong file ending on infile, should be .gx or .hgx. Exiting.')
    debug('leave start_interactive')