def RunSimulation(self): """ Simulation control routine. Check user response to begin simulation and display messages""" ans = cdml.dlgSimpleMsg('INFO', 'Running a simulation may take some time. Do you want to continue?', wx.YES_NO, wx.ICON_INFORMATION, Parent = self) if ans == wx.ID_NO: return TheProgressDialog = None try: def ExtractTraceBack(): try: # Extract Traceback if exists TraceBackText = self.tab2_tc_TraceBack.GetValue() if TraceBackText == '': SimulationTraceBack = None else: SimulationTraceBack = DB.pickle.loads(TraceBackText) except: raise ValueError, 'TraceBack Error: Could not properly extract TraceBack - please make sure a proper TraceBack was entered' SimulationTraceBack = None return SimulationTraceBack # version 2.5 does not support canceling simulation TheProgressDialog = cdml.ProgressDialogTimeElapsed(Parent = self, StartTimerUponCreation = False, AllowCancel = DB.SystemSupportsProcesses) # Define the Function to run on a thread/process def GenerationStartMiniScript(): "Compile and execute the generation" prj = DB.Projects[self.idPrj] PopID = prj.PrimaryPopulationSetID NumberOfRepetitions = prj.NumberOfRepetitions Pop = DB.PopulationSets[PopID] if Pop.IsDistributionBased(): SimulationTraceBack = ExtractTraceBack() if SimulationTraceBack == None: PopulationTraceBack = None else: PopulationTraceBack = SimulationTraceBack[-1] # if the population is distribution based, then # Compile the generation script with default options ScriptFileNameFullPath = Pop.CompilePopulationGeneration(GeneratedPopulationSize = NumberOfRepetitions, GenerationFileNamePrefix = None, OutputFileNamePrefix = None , RandomStateFileNamePrefix = None, GenerationOptions = None , RecreateFromTraceBack = PopulationTraceBack) # run the generation script DeleteScriptFileAfterRun = not cdml.GetAdminMode() (ProcessList, PipeList) = Pop.RunPopulationGeneration(GenerationFileName = ScriptFileNameFullPath, NumberOfProcessesToRun = -1, DeleteScriptFileAfterRun = DeleteScriptFileAfterRun) else: # otherwise don't run anything (ProcessList, PipeList) = (None,None) return (ProcessList, PipeList) def GenerationEndMiniScript(ProcessList, PipeList): "Complete Generation by collecting results" prj = DB.Projects[self.idPrj] PopID = prj.PrimaryPopulationSetID Pop = DB.PopulationSets[PopID] if (ProcessList, PipeList) == (None,None): # if nothing was run, then return no replacement population RetVal = None else: # If a process was run, return the replacement population RetVal = Pop.CollectResults(ProcessList, PipeList) # Collect the results return RetVal def SimulationStartMiniScript(): "Compile and execute the simulation" SimulationTraceBack = ExtractTraceBack() prj = DB.Projects[self.idPrj] # if an override population is defined, this means it was # generated from distributions and therefore repetitions should # be 1 as the number of repetitions defined the population size if self.TempOverridePopulationSet == None: OverrideRepetitionCount = None else: OverrideRepetitionCount = 1 ScriptFileName = prj.CompileSimulation(OverrideRepetitionCount = OverrideRepetitionCount, OverridePopulationSet = self.TempOverridePopulationSet, RecreateFromTraceBack = SimulationTraceBack) # run the simulation once without collecting results # also do nto delete the compiled script file if in Admin mode DeleteScriptFileAfterRun = not cdml.GetAdminMode() (ProcessList, PipeList) = prj.RunSimulation(ScriptFileName, NumberOfProcessesToRun = -1, DeleteScriptFileAfterRun = DeleteScriptFileAfterRun) return (ProcessList, PipeList) def SimulationEndMiniScript(ProcessList, PipeList): "Complete Simulation by collecting results" prj = DB.Projects[self.idPrj] RetVal = prj.CollectResults(ProcessList, PipeList) return RetVal ThreadObject = cdml.WorkerThread(GenerationStartMiniScript,GenerationEndMiniScript) # Tell the dialog box what to do when cancel is pressed TheProgressDialog.FunctionToRunUponCancel = ThreadObject.StopProcess # Now start the timer for the progress dialog box TheProgressDialog.StartTimer() # wait until thread/process exits Info = ThreadObject.WaitForJob() # Cancel through the dialog box is no longer possible TheProgressDialog.FunctionToRunUponCancel = None # Figure out if cancel was pressed: WasCanceled = TheProgressDialog.WasCanceled if not WasCanceled: # set the override population to the result - None means no override self.TempOverridePopulationSet = Info # Now actually run the simulation ThreadObject = cdml.WorkerThread(SimulationStartMiniScript,SimulationEndMiniScript) # Tell the dialog box what to do when cancel is pressed TheProgressDialog.FunctionToRunUponCancel = ThreadObject.StopProcess # wait until thread/process exits Info = ThreadObject.WaitForJob() # Cancel through the dialog box is no longer possible TheProgressDialog.FunctionToRunUponCancel = None # Properly destroy the dialog WasCanceled = TheProgressDialog.WasCanceled # Properly destroy the dialog TheProgressDialog.Destroy() TheProgressDialog = None if WasCanceled: cdml.dlgSimpleMsg('INFO', 'The simulation was canceled by request!', wx.OK, wx.ICON_INFORMATION, Parent = self) elif Info.ProjectID == self.idPrj: cdml.dlgSimpleMsg('INFO', 'The simulation has finished successfully!', wx.OK, wx.ICON_INFORMATION, Parent = self) else: raise ValueError, 'ASSERTION ERROR: wrong project ID returned' except: cdml.dlgErrorMsg(Parent = self) # Release the override population if set self.TempOverridePopulationSet = None # Properly destroy the progress dialog box if not done before if TheProgressDialog != None: TheProgressDialog.Destroy()
def DisplayText(self, FormatOptions=None): """ displays the text """ if FormatOptions == None: FormatOptions = self.FormatOptions TheProgressDialog = None try: # version 2.5 does not support canceling simulation TheProgressDialog = cdml.ProgressDialogTimeElapsed( Parent=self, StartTimerUponCreation=False, AllowCancel=DB.SystemSupportsProcesses) # Define the Function to run on a thread def ReportGenerationStartMiniScript(): "Generate the report and copy the text" # Define an encapsulating function so no arguments will be needed def FunctionToRunAsProcess(): ReturnVal = self.ReportObject.GenerateReport(FormatOptions) return ReturnVal # Now run this function as a process - if possible (ProcessList, PipeList) = DB.RunFunctionAsProcess(FunctionToRunAsProcess) return (ProcessList, PipeList) def ReportGenerationEndMiniScript(ProcessList, PipeList): "Copy the text" ReturnVal = PipeList[0].recv() return ReturnVal ThreadObject = cdml.WorkerThread(ReportGenerationStartMiniScript, ReportGenerationEndMiniScript) # Tell the dialog box what to do when cancel is pressed TheProgressDialog.FunctionToRunUponCancel = ThreadObject.StopProcess # Now start the timer for the progress dialog box TheProgressDialog.StartTimer() # wait until thread/process exits Info = ThreadObject.WaitForJob() # Cancel through the dialog box is no longer possible TheProgressDialog.FunctionToRunUponCancel = None # Properly destroy the dialog WasCanceled = TheProgressDialog.WasCanceled TheProgressDialog.Destroy() TheProgressDialog = None if WasCanceled: self.ReportText = 'The report generation operation was canceled by user request!' else: self.ReportText = Info except: (ExceptType, ExceptValue, ExceptTraceback) = sys.exc_info() self.ReportText = 'Report Generation Error : The report could not be generated due to an error during generation. Here are Additional Details: ' + str( ExceptValue) # Properly destroy the progress dialog box if not done before if TheProgressDialog != None: TheProgressDialog.Destroy() # Clear previous text self.tc_ReportDisplay.SetReadOnly(False) self.tc_ReportDisplay.ClearAll() # Actually display the text self.tc_ReportDisplay.AddText(str(self.ReportText)) self.tc_ReportDisplay.SetReadOnly(True) # Calculate the horizontal scroll width. Default is the number reported # by the system that should be 2000 pixels MaxPixelWidth = self.tc_ReportDisplay.GetScrollWidth() # Split the text lines to allow counting their length TextLines = self.ReportText.split('\n') for TextLine in TextLines: PixelLineWidth = self.tc_ReportDisplay.TextWidth( stc.STC_STYLE_DEFAULT, TextLine) MaxPixelWidth = max(MaxPixelWidth, PixelLineWidth) self.tc_ReportDisplay.SetScrollWidth(MaxPixelWidth) self.tc_ReportDisplay.ScrollToLine(0) self.tc_ReportDisplay.ScrollToColumn(0) return True
def SpecialRecordAdd(self, ClickedPanel): """ Add a special record - In this case this means a new population set Generated from a distribution population set """ if ClickedPanel != None: RecordID = ClickedPanel.Key if (RecordID == 0 or RecordID == None) or not DB.PopulationSets.has_key(RecordID): ReturnRecord = None else: if not DB.PopulationSets[RecordID].IsDistributionBased(): # This is a data population set cdml.dlgSimpleMsg( 'ERROR', 'This population set is not based on distribution and therefore cannot be used to generate new population data', Parent=self) ReturnRecord = None else: # This means this population set is defined by distributions dlg = wx.NumberEntryDialog( self, 'Define population size', 'Please enter the size of the population to generate ', 'Population Size', 1000, 0, 100000) dlg.CenterOnScreen() if dlg.ShowModal() != wx.ID_OK: # If 'Cancel' button is clicked ReturnRecord = None else: NumberOfIndividuals = dlg.GetValue( ) # Get selection index dlg.Destroy() PopulationTraceBack = None # When in admin mode, also ask for traceback info AskForTraceBack = cdml.GetAdminMode() if AskForTraceBack: TraceBackText = cdml.dlgTextEntry( Message= 'Please enter the Pickled TraceBack information as appear in the hidden report of the population you are trying to reconstruct ', Caption='Enter Pickled TraceBack Info', DefaultValue='', Parent=self) if TraceBackText == '': PopulationTraceBack = None else: try: PopulationTraceBack = DB.pickle.loads( TraceBackText) except: raise ValueError, 'TraceBack Error: Could not properly extract TraceBack - please make sure a proper TraceBack was entered' PopulationTraceBack = None TheProgressDialog = None try: # version 2.5 does not support canceling simulation TheProgressDialog = cdml.ProgressDialogTimeElapsed( Parent=self, StartTimerUponCreation=False, AllowCancel=DB.SystemSupportsProcesses) # Define the Function to run on a thread/process def GenerationStartMiniScript(): "Compile and execute the generation" Pop = DB.PopulationSets[RecordID] # Compile the generation script with default options ScriptFileNameFullPath = Pop.CompilePopulationGeneration( GeneratedPopulationSize=NumberOfIndividuals, GenerationFileNamePrefix=None, OutputFileNamePrefix=None, RandomStateFileNamePrefix=None, GenerationOptions=None, RecreateFromTraceBack=PopulationTraceBack) # run the generation script DeleteScriptFileAfterRun = not cdml.GetAdminMode( ) (ProcessList, PipeList) = Pop.RunPopulationGeneration( GenerationFileName=ScriptFileNameFullPath, NumberOfProcessesToRun=-1, DeleteScriptFileAfterRun= DeleteScriptFileAfterRun) return (ProcessList, PipeList) def GenerationEndMiniScript(ProcessList, PipeList): "Complete Generation by collecting results" Pop = DB.PopulationSets[RecordID] # Collect the results RetVal = Pop.CollectResults( ProcessList, PipeList) return RetVal ThreadObject = cdml.WorkerThread( GenerationStartMiniScript, GenerationEndMiniScript) # Tell the dialog box what to do when cancel is pressed TheProgressDialog.FunctionToRunUponCancel = ThreadObject.StopProcess # Now start the timer for the progress dialog box TheProgressDialog.StartTimer() # wait until thread/process exits Info = ThreadObject.WaitForJob() # Cancel through the dialog box is no longer possible TheProgressDialog.FunctionToRunUponCancel = None # Properly destroy the dialog WasCanceled = TheProgressDialog.WasCanceled TheProgressDialog.Destroy() TheProgressDialog = None if WasCanceled: cdml.dlgSimpleMsg( 'INFO', 'The Population generation was canceled by request!', wx.OK, wx.ICON_INFORMATION, Parent=self) ReturnRecord = None else: cdml.dlgSimpleMsg( 'INFO', 'The Population generation has finished successfully! After you press OK your cursor will be focused on the new population set.', wx.OK, wx.ICON_INFORMATION, Parent=self) ReturnRecord = Info except: cdml.dlgErrorMsg() ReturnRecord = None # Properly destroy the progress dialog box if not done before if TheProgressDialog != None: TheProgressDialog.Destroy() return ReturnRecord