def save(frame, event): ''' save(frame, event) --> None Event handler for saving a model file ... ''' update_for_save(frame) 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_file(fname, 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() 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 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)
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)
def save_as(frame, event): '''save_as(frame, event) --> None Event handler for save as ... ''' if sys.version_info.major == 3: dlg = wx.FileDialog( frame, message="Save As", defaultFile="", wildcard="HDF5 GenX File (*.hgx)|*.hgx|GenX File (*.gx3)|*.gx3", style=wx.FD_SAVE #| wx.FD_CHANGE_DIR ) else: dlg = wx.FileDialog( frame, message="Save As", defaultFile="", wildcard="HDF5 GenX File (*.hgx)|*.hgx|GenX File (*.gx)|*.gx", style=wx.FD_SAVE #| wx.FD_CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: update_for_save(frame) fname = dlg.GetPath() base, ext = os.path.splitext(fname) if ext == '': ext = '.hgx' 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_file(fname, 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() ShowErrorDialog( frame, 'Could not save the file. Python Error:\n%s' % (val, )) set_title(frame) dlg.Destroy()
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)
def save(fname, model, optimizer): io.save_file(fname, model, optimizer, _config)
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')