def scan_parameter(frame, row): ''' scan_parameter(frame, row) --> None Scans the parameter in row row [int] from max to min in the number of steps given by dialog input. ''' if not frame.model.is_compiled(): ShowNotificationDialog(frame, 'Please conduct a simulation before' +\ ' scanning a parameter. The script needs to be compiled.') return try: step = wx.GetNumberFromUser(message='Input the number of evaluation points for the scan',\ prompt='Steps', caption='', value=50, min=2, max=1000) frame.main_frame_statusbar.SetStatusText('Scanning parameter', 1) x, y = frame.solver_control.ScanParameter(row, step) fs, pars = frame.model.get_sim_pars() bestx = frame.model.parameters.get_data()[row][1] besty = frame.model.fom frame.plot_fomscan.SetPlottype('scan') frame.plot_fomscan.Plot((x, y, bestx, besty,\ frame.solver_control.fom_error_bars_level)) frame.plot_notebook.SetSelection(3) except Exception as e: outp = io.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, val) frame.main_frame_statusbar.SetStatusText('Fatal Error - scan fom', 1) else: frame.main_frame_statusbar.SetStatusText('Scanning finished', 1)
def evaluate(frame, event): '''evaluate(frame, event) --> None Envent handler for only evaluating the Sim function - no recompiling ''' frame.flag_simulating = True frame.main_frame_statusbar.SetStatusText('Simulating...', 1) # Compile is not necessary when using simualate... #frame.model.compile_script() try: frame.model.simulate(compile=False) except modellib.GenericError as e: ShowModelErrorDialog(frame, str(e)) frame.main_frame_statusbar.SetStatusText('Error in simulation', 1) except Exception as e: outp = io.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, val) frame.main_frame_statusbar.SetStatusText('Fatal Error - simulate', 1) else: _post_sim_plot_event(frame, frame.model, 'Simulation') frame.main_frame_statusbar.SetStatusText('Simulation Sucessful', 1) frame.plugin_control.OnSimulate(None) frame.flag_simulating = False
def set_possible_parameters_in_grid(frame): # Now we should find the parameters that we can use to # in the grid try: pardict = frame.model.get_possible_parameters() except Exception as e: outp = io.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() #ShowErrorDialog(frame, val) ShowErrorDialog(frame, val,\ 'simulate - model.get_possible_parameters') frame.main_frame_statusbar.SetStatusText('Fatal Error', 0) return try: frame.paramter_grid.SetParameterSelections(pardict) except Exception as e: ShowErrorDialog(frame, str(e),\ 'simulate - parameter_grid.SetParameterSelection') frame.main_frame_statusbar.SetStatusText('Fatal Error', 0) return # Set the function for which the parameter can be evaluated with frame.paramter_grid.SetEvalFunc(frame.model.eval_in_model)
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 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 project_fom_parameter(frame, row): '''project_fom_parameter(frame, row) --> None Plots the project fom given by the row row [int] ''' if not frame.solver_control.IsFitted(): ShowNotificationDialog( frame, 'Please conduct a fit before' + ' scanning a parameter. The script needs to be compiled and foms have' + ' to be collected.') return frame.main_frame_statusbar.SetStatusText('Trying to project fom', 1) try: x, y = frame.solver_control.ProjectEvals(row) if len(x) == 0 or len(y) == 0: ShowNotificationDialog( frame, 'Please conduct a fit before' + ' projecting a parameter. The script needs to be compiled and foms have' + ' to be collected.') return elif frame.model.fom is None or np.isnan(frame.model.fom): ShowNotificationDialog( frame, 'The model must be simulated (FOM is not a valid number)') return fs, pars = frame.model.get_sim_pars() bestx = pars[row] besty = frame.model.fom frame.plot_fomscan.SetPlottype('project') frame.plot_fomscan.Plot((x, y, bestx, besty,\ frame.solver_control.fom_error_bars_level)) frame.plot_notebook.SetSelection(3) except Exception as e: outp = io.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, val) frame.main_frame_statusbar.SetStatusText('Fatal Error - project fom', 1) else: frame.main_frame_statusbar.SetStatusText('Projected fom plotted', 1)
def import_script(frame, event): '''import_script(frame, event) --> None imports a script from the file given by a file dialog box ''' dlg = wx.FileDialog(frame, message="Import script", defaultFile="",\ wildcard="Python files (*.py)|*.py|All files (*.*)|*.*",\ style=wx.FD_OPEN | wx.FD_CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: try: frame.model.import_script(dlg.GetPath()) #frame.model.import_script(fname) except modellib.IOError as e: ShowModelErrorDialog(frame, str(e)) frame.main_frame_statusbar.SetStatusText(\ 'Error when importing script', 1) return except Exception as e: ShowErrorDialog(frame, str(e),\ 'import script - model.import_script') frame.main_frame_statusbar.SetStatusText('Fatal Error', 1) return 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,)) else: frame.main_frame_statusbar.SetStatusText(\ 'Script imported from file', 1) dlg.Destroy() # Post event to tell that the model has changed _post_new_model_event(frame, frame.model)
def do_simulation(frame): # Just a debugging output... # print frame.script_editor.GetText() frame.main_frame_statusbar.SetStatusText('Simulating...', 1) frame.model.set_script(frame.script_editor.GetText()) # Compile is not necessary when using simualate... #frame.model.compile_script() try: frame.model.simulate() except modellib.GenericError as e: ShowModelErrorDialog(frame, str(e)) frame.main_frame_statusbar.SetStatusText('Error in simulation', 1) except Exception as e: outp = io.StringIO() traceback.print_exc(200, outp) val = outp.getvalue() outp.close() ShowErrorDialog(frame, val) frame.main_frame_statusbar.SetStatusText('Fatal Error - simulate', 1) else: _post_sim_plot_event(frame, frame.model, 'Simulation') frame.plugin_control.OnSimulate(None) frame.main_frame_statusbar.SetStatusText('Simulation Sucessful', 1)
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)