def run(self): # Call the tool, passing in the callbacks # # There are really two ways to call the tool # 1) Call the tool as a system command # 2) Call the run() function of the tool # # The first option has the benifit of the standard __main__ syntax, # but the second option allows for use of the callbacks. # # The prefered method for opus tools is the second option # # EXAMPLE # try: importString = "from %s import opusRun" % (self.toolInclude) #print importString exec(importString) self.params = self.buildparams() if self.startingCallback != None: self.startingCallback() success = opusRun(self.progressCallback, self.logCallback, self.toolVars) #@UndefinedVariable if self.finishedCallback != None: self.progressCallback(100) self.finishedCallback(success) except ImportError, e: print "ImportError: %s" % formatExceptionInfo() success = False self.logCallback("Error importing %s: %s" % (self.toolInclude, formatExceptionInfo())) self.finishedCallback(success)
def run(self): # Call the tool, passing in the callbacks # # There are really two ways to call the tool # 1) Call the tool as a system command # 2) Call the run() function of the tool # # The first option has the benifit of the standard __main__ syntax, # but the second option allows for use of the callbacks. # # The prefered method for opus tools is the second option # # EXAMPLE # try: importString = "from %s import opusRun" % (self.toolInclude) #print importString exec(importString) self.params = self.buildparams() if self.startingCallback != None: self.startingCallback() success = opusRun(self.progressCallback,self.logCallback,self.toolVars) #@UndefinedVariable if self.finishedCallback != None: self.progressCallback(100) self.finishedCallback(success) except ImportError, e: print "ImportError: %s" % formatExceptionInfo() success = False self.logCallback("Error importing %s: %s" % (self.toolInclude, formatExceptionInfo()) ) self.finishedCallback(success)
def saveProjectAs(self): ''' Save the project configuration under a different name ''' try: # get the location for the new config file on disk start_dir = paths.OPUS_PROJECT_CONFIGS_PATH configDialog = QFileDialog() filter_str = QString("*.xml") fd = configDialog.getSaveFileName(self, QString("Save As..."), QString(start_dir), filter_str) # Check for cancel if not fd: return filename = QString(fd) # append xml extension if no extension was given if not filename.endsWith('.xml') and len(filename.split('.')) == 1: filename = filename + '.xml' if not self.saveProject(filename): return # hack: open the project right after save to properly change the # 'active project' related parameters self.openProject(filename) except: errorMessage = formatExceptionInfo(custom_message = \ 'Unexpected error saving config') QMessageBox.warning(self, 'Warning', errorMessage)
def execToolFile(self): ''' Show a dialog box that lets the user configure and execute a tool. ''' assert self.has_selected_item() tool_lib_node = get_tool_library_node(self.project) params = {'tool_path': get_path_to_tool_modules(self.project)} tool_node = self.selected_item().node try: module_name = tool_node.find('class_module').text import_path = params['tool_path'] + '.' + module_name importString = "from %s import opusRun" % (import_path) exec(importString) except Exception, e: print e MessageBox.error(mainwindow = self.view, text = 'Invalid module name', detailed_text = ('This tool points to a module named "%s", ' % import_path + \ 'but there is no module with that name, or module returned import error: %s. ' \ % formatExceptionInfo() )) return
def execBatch(self): ''' Present a dialog to execute a set of tool configurations ''' assert self.has_selected_item() # Node representing the set to execute tool_set_node = self.selected_item().node # map tool config nodes in set -> name of the hooked node tool_config_to_tool_name = {} tool_config_nodes = tool_set_node[:] for tool_config_node in tool_config_nodes: hook_node = tool_config_node.find('tool_hook') hooked_tool_name = str(hook_node.text or '').strip() hooked_tool_node = get_tool_node_by_name(self.project, hooked_tool_name) module_name = hooked_tool_node.find('class_module').text try: module_name = hooked_tool_node.find('class_module').text tool_path = get_path_to_tool_modules(self.project) import_path = tool_path + '.' + module_name importString = "from %s import opusRun" % (import_path) exec(importString) tool_config_to_tool_name[tool_config_node] = import_path except Exception, e: MessageBox.error(mainwindow = self.view, text = 'Invalid module name', detailed_text = ('This tool points to a module named "%s", ' % import_path + \ 'but there is no module with that name, or module returned import error: %s. ' \ % formatExceptionInfo() )) return
def saveProjectAs(self): ''' Save the project configuration under a different name ''' try: # get the location for the new config file on disk start_dir = paths.OPUS_PROJECT_CONFIGS_PATH configDialog = QFileDialog() filter_str = QString("*.xml") fd = configDialog.getSaveFileName(self,QString("Save As..."), QString(start_dir), filter_str) # Check for cancel if not fd: return filename = QString(fd) # append xml extension if no extension was given if not filename.endsWith('.xml') and len(filename.split('.')) == 1: filename = filename + '.xml' if not self.saveProject(filename): return # hack: open the project right after save to properly change the # 'active project' related parameters self.openProject(filename) except: errorMessage = formatExceptionInfo(custom_message = \ 'Unexpected error saving config') QMessageBox.warning(self, 'Warning', errorMessage)
def _get_viz_spec(self): viz_type = self.cboVizType.currentText() translation = self._get_output_types(viz_type) dataset_name = self.dataset_name output_type = QString(translation[str(self.cboOutputType.currentText())]) indicators = QString(str(self._get_column_values(column = 0))) vals = { 'indicators': indicators, 'output_type': output_type, 'dataset_name': dataset_name, 'visualization_type': QString(self._get_type_mapper()[str(viz_type)]) } if output_type == 'mapnik_map' or output_type == 'mapnik_animated_map': vals['mapnik_bucket_labels'] = self.mapnik_options['mapnik_bucket_labels'] vals['mapnik_bucket_colors'] = self.mapnik_options['mapnik_bucket_colors'] vals['mapnik_bucket_ranges'] = self.mapnik_options['mapnik_bucket_ranges'] vals['mapnik_resolution'] = self.mapnik_options['mapnik_resolution'] vals['mapnik_page_dims'] = self.mapnik_options['mapnik_page_dims'] vals['mapnik_map_lower_left'] = self.mapnik_options['mapnik_map_lower_left'] vals['mapnik_map_upper_right'] = self.mapnik_options['mapnik_map_upper_right'] vals['mapnik_legend_lower_left'] = self.mapnik_options['mapnik_legend_lower_left'] vals['mapnik_legend_upper_right'] = self.mapnik_options['mapnik_legend_upper_right'] elif output_type == 'fixed_field': try: fixed_field_params = QString(str(self._get_column_values(column = 1))) except: errorInfo = formatExceptionInfo() logger.log_error(errorInfo) MessageBox.error(mainwindow = self, text = 'Could not get fixed field parameters for all columns', detailed_text = '') return None vals['fixed_field_specification'] = fixed_field_params vals['id_format'] = self.leOption1.text() elif output_type == 'sql': vals['database_name'] = self.leOption1.text() elif output_type == 'esri': vals['storage_location'] = self.leOption1.text() elif output_type in ('tab', 'xls'): if self.rbSingleTable.isChecked(): output_style = Table.ALL elif self.rbTablePerIndicator.isChecked(): output_style = Table.PER_ATTRIBUTE elif self.rbTablePerYear.isChecked(): output_style = Table.PER_YEAR vals['output_style'] = QString(str(output_style)) if self.appendTypeCheckBox.isChecked(): vals['append_col_type'] = True else: vals['append_col_type'] = False if output_type == 'xls': vals['storage_location'] = self.leOption1.text() return vals
def on_buttonBox_accepted(self): path = str(self.lePath.text()) if not os.path.exists(path): msg = 'Cannot import, %s does not exist' % path logger.log_warning(msg) MessageBox.warning(mainwindow=self, text=msg, detailed_text='') else: cache_directory = path years = [] for dir in os.listdir(cache_directory): if len(dir) == 4 and dir.isdigit(): years.append(int(dir)) if years == []: msg = 'Cannot import, %s has no run data' % path logger.log_warning(msg) MessageBox.warning(mainwindow=self, text=msg, detailed_text='') else: start_year = min(years) end_year = max(years) project_name = os.environ['OPUSPROJECTNAME'] run_name = os.path.basename(path) server_config = ServicesDatabaseConfiguration() run_manager = RunManager(server_config) run_id = run_manager._get_new_run_id() resources = { 'cache_directory': cache_directory, 'description': '', 'years': (start_year, end_year), 'project_name': project_name } try: run_manager.add_row_to_history(run_id=run_id, resources=resources, status='done', run_name=run_name) update_available_runs(self.project) logger.log_status( 'Added run %s of project %s to run_activity table' % (run_name, project_name)) except: errorInfo = formatExceptionInfo() logger.log_error(errorInfo) MessageBox.error( mainwindow=self, text= 'Could not add run %s of project %s to run_activity table' % (run_name, project_name), detailed_text=errorInfo) self.close()
def on_buttonBox_accepted(self): path = str(self.lePath.text()) if not os.path.exists(path): msg = 'Cannot import, %s does not exist' % path logger.log_warning(msg) MessageBox.warning(mainwindow = self, text = msg, detailed_text = '') else: cache_directory = path years = [] for dir in os.listdir(cache_directory): if len(dir) == 4 and dir.isdigit(): years.append(int(dir)) if years == []: msg = 'Cannot import, %s has no run data'%path logger.log_warning(msg) MessageBox.warning(mainwindow = self, text = msg, detailed_text = '') else: start_year = min(years) end_year = max(years) project_name = os.environ['OPUSPROJECTNAME'] run_name = os.path.basename(path) server_config = ServicesDatabaseConfiguration() run_manager = RunManager(server_config) run_id = run_manager._get_new_run_id() resources = { 'cache_directory': cache_directory, 'description': '', 'years': (start_year, end_year), 'project_name': project_name } try: run_manager.add_row_to_history(run_id = run_id, resources = resources, status = 'done', run_name = run_name) update_available_runs(self.project) logger.log_status('Added run %s of project %s to run_activity table'%(run_name, project_name)) except: errorInfo = formatExceptionInfo() logger.log_error(errorInfo) MessageBox.error(mainwindow = self, text = 'Could not add run %s of project %s to run_activity table'%(run_name, project_name), detailed_text = errorInfo) self.close()
def run(self, args = {}): succeeded = False if self.errorCallback is not None: self.generator.errorCallback = self.errorCallback self.visualizer.errorCallback = self.errorCallback # try: # import pydevd;pydevd.settrace() # except: # pass try: self.visualizations = [] for (visualization_type, dataset_name, params) in self.visualization_configurations: indicator_results = [] indicators = self._get_indicators(params) for indicator_name in indicators: try: self.generator.set_data(self.source_data_name, indicator_name, dataset_name, self.years, cache_directory = self.cache_directory) self.generator.run() indicator = {'indicator_name':indicator_name, 'dataset_name':dataset_name, 'source_data_name':self.source_data_name, 'years':self.years} indicator_results.append(indicator) except: logger.log_warning('could not generate indicator %s'%indicator_name) self.visualizer.indicator_type = visualization_type self.visualizer.indicators = indicator_results viz_args = self._get_viz_args(visualization_type, params, indicators) self.visualizer.run(args = viz_args, cache_directory = self.cache_directory) if self.visualizer.get_visualizations() != []: self.visualizations.append((visualization_type, self.visualizer.get_visualizations())) succeeded = True except: succeeded = False errorInfo = formatExceptionInfo(custom_message = 'Unexpected error in the batch processor') self.errorCallback(errorInfo) if self.finishedCallback: self.finishedCallback(succeeded)
def run(self, args, cache_directory = None): succeeded = False try: # find the directory containing the eugene xml configurations not_visualized = self._visualize(args, cache_directory = cache_directory) succeeded = True except: succeeded = False errorInfo = formatExceptionInfo(custom_message = 'Unexpected error in the results visualizer') if self.errorCallback is not None: self.errorCallback(errorInfo) if self.finishedCallback is not None: self.finishedCallback(succeeded)
def run(self, args={}, raise_exception=False): succeeded = False try: # try: # import pydevd;pydevd.settrace() # except: # pass self._generate_results() succeeded = True except Exception, e: succeeded = False errorinfo = formatExceptionInfo(custom_message="Unexpected error in the result generator") self.errorCallback(errorinfo) if raise_exception: raise e
def run(self, args={}, raise_exception=False): succeeded = False try: # try: # import pydevd;pydevd.settrace() # except: # pass self._generate_results() succeeded = True except Exception, e: succeeded = False errorinfo = formatExceptionInfo( custom_message='Unexpected error in the result generator') self.errorCallback(errorinfo) if raise_exception: raise e
def run(self): # Start the estimation. This code adapted from urbansim/tools/start_estimation.py # statusdir is a temporary directory into which to write a status file # regarding the progress of the simulation - the progress bar reads this file statusfile = None succeeded = False try: # get the configuration for estimations estimation_section = self.xml_config.get_section('model_manager') estimation_config = estimation_section['estimation_config'] self.config = estimation_config # TODO: put this option into post run dialog save_results = estimation_config['save_estimation_results'] # If we've paused the estimation, wait 10 seconds, and see if we are unpaused. If we've cancelled, # exit the loop. Note that this is a fairly coarse level of pause/resume/stop (at the level of # estimating an entire model, rather than a bit of a model). while self.paused and not self.cancelled: time.sleep(10) if not self.cancelled: self.er = EstimationRunner( model=self.model_name, specification_module=None, xml_configuration=self.xml_config, model_group=self.model_group, configuration=None, save_estimation_results=save_results) self.running = True self.er.estimate() self.running = False succeeded = True except: succeeded = False self.running = False errorInfo = formatExceptionInfo( custom_message='Unexpected Error From Estimation') self.errorCallback(errorInfo) if statusfile is not None: os.remove(statusfile) del self.er gc.collect() self.finishedCallback(succeeded)
def run(self): # Start the estimation. This code adapted from urbansim/tools/start_estimation.py # statusdir is a temporary directory into which to write a status file # regarding the progress of the simulation - the progress bar reads this file statusfile = None succeeded = False try: # get the configuration for estimations estimation_section = self.xml_config.get_section('model_manager') estimation_config = estimation_section['estimation_config'] self.config = estimation_config # TODO: put this option into post run dialog save_results = estimation_config['save_estimation_results'] # If we've paused the estimation, wait 10 seconds, and see if we are unpaused. If we've cancelled, # exit the loop. Note that this is a fairly coarse level of pause/resume/stop (at the level of # estimating an entire model, rather than a bit of a model). while self.paused and not self.cancelled: time.sleep(10) if not self.cancelled: self.er = EstimationRunner(model = self.model_name, specification_module = None, xml_configuration = self.xml_config, model_group = self.model_group, configuration = None, save_estimation_results=save_results) self.running = True self.er.estimate() self.running = False succeeded = True except: succeeded = False self.running = False errorInfo = formatExceptionInfo(custom_message = 'Unexpected Error From Estimation') self.errorCallback(errorInfo) if statusfile is not None: os.remove(statusfile) del self.er self.er = None gc.collect() self.finishedCallback(succeeded)
def _get_viz_spec(self): viz_type = self.cboVizType.currentText() translation = self._get_output_types(viz_type) dataset_name = self.dataset_name output_type = QString(translation[str( self.cboOutputType.currentText())]) indicators = QString(str(self._get_column_values(column=0))) vals = { 'indicators': indicators, 'output_type': output_type, 'dataset_name': dataset_name, 'visualization_type': QString(self._get_type_mapper()[str(viz_type)]) } if output_type == 'mapnik_map' or output_type == 'mapnik_animated_map': vals['mapnik_bucket_labels'] = self.mapnik_options[ 'mapnik_bucket_labels'] vals['mapnik_bucket_colors'] = self.mapnik_options[ 'mapnik_bucket_colors'] vals['mapnik_bucket_ranges'] = self.mapnik_options[ 'mapnik_bucket_ranges'] vals['mapnik_resolution'] = self.mapnik_options[ 'mapnik_resolution'] vals['mapnik_page_dims'] = self.mapnik_options['mapnik_page_dims'] vals['mapnik_map_lower_left'] = self.mapnik_options[ 'mapnik_map_lower_left'] vals['mapnik_map_upper_right'] = self.mapnik_options[ 'mapnik_map_upper_right'] vals['mapnik_legend_lower_left'] = self.mapnik_options[ 'mapnik_legend_lower_left'] vals['mapnik_legend_upper_right'] = self.mapnik_options[ 'mapnik_legend_upper_right'] elif output_type == 'fixed_field': try: fixed_field_params = QString( str(self._get_column_values(column=1))) except: errorInfo = formatExceptionInfo() logger.log_error(errorInfo) MessageBox.error( mainwindow=self, text='Could not get fixed field parameters for all columns', detailed_text='') return None vals['fixed_field_specification'] = fixed_field_params vals['id_format'] = self.leOption1.text() elif output_type == 'sql': vals['database_name'] = self.leOption1.text() elif output_type == 'esri': vals['storage_location'] = self.leOption1.text() elif output_type == 'tab': if self.rbSingleTable.isChecked(): output_style = Table.ALL elif self.rbTablePerIndicator.isChecked(): output_style = Table.PER_ATTRIBUTE elif self.rbTablePerYear.isChecked(): output_style = Table.PER_YEAR vals['output_style'] = QString(str(output_style)) return vals
def run(self): # Run the Eugene model using the XML version of the Eugene configuration. # This code adapted from opus_core/tools/start_run.py # statusdir is a temporary directory into which to write a status file # regarding the progress of the simulation - the progress bar reads this file statusdir = None succeeded = False run_id = None try: config = self.xml_config.get_run_configuration(str(self.scenariotorun)) # self.xmltreeobject.toolboxbase.opus_core_xml_configuration.get_run_configuration(str(self.scenariotorun)) cache_directory_root = config['creating_baseyear_cache_configuration'].cache_directory_root run_name = self.get_run_name(config = config, run_name = self.run_name) config['cache_directory'] = os.path.join(cache_directory_root, run_name) #insert_auto_generated_cache_directory_if_needed(config) (self.start_year, self.end_year) = config['years'] run_manager = get_run_manager() run_manager.setup_new_run(cache_directory = config['cache_directory'], configuration = config) statusdir = run_manager.get_current_cache_directory() self.statusfile = os.path.join(statusdir, 'status.txt') self.currentLogfileYear = self.start_year self.currentLogfileKey = 0 self.config = config config['status_file_for_gui'] = self.statusfile self.commandfile = os.path.join(statusdir, 'command.txt') config['command_file_for_gui'] = self.commandfile # To test delay in writing the first log file entry... # time.sleep(5) self.running = True self.run_name = run_name self.run_manager = run_manager run_id = run_manager.run_id self.startedCallback(run_id = run_id, run_name = run_name, scenario_name = self.scenariotorun, run_resources = config, status = 'running') run_manager.run_run(config, run_name=run_name, scenario_name=self.scenariotorun) self.running = False succeeded = True except SimulationRunError: self.running = False succeeded = False except: self.running = False succeeded = False errorInfo = formatExceptionInfo(custom_message = 'Unexpected Error From Model') self.errorCallback(errorInfo) if self.statusfile is not None: gc.collect() # adding try/except, windows sometimes has a lock on this file # when it does, os.remove will cause the simulation to appear # as if it has crashed, when in fact it simply could not delete # status.txt try: os.remove(self.statusfile) except: pass self.finishedCallback(succeeded, run_name = run_name)
def run(self): # Run the Eugene model using the XML version of the Eugene configuration. # This code adapted from opus_core/tools/start_run.py # statusdir is a temporary directory into which to write a status file # regarding the progress of the simulation - the progress bar reads this file statusdir = None succeeded = False run_id = None try: config = self.xml_config.get_run_configuration(str( self.modeltorun)) # self.xmltreeobject.toolboxbase.opus_core_xml_configuration.get_run_configuration(str(self.modeltorun)) cache_directory_root = config[ 'creating_baseyear_cache_configuration'].cache_directory_root run_name = self.get_run_name(config=config, run_name=self.run_name) config['cache_directory'] = os.path.join(cache_directory_root, run_name) #insert_auto_generated_cache_directory_if_needed(config) (self.start_year, self.end_year) = config['years'] run_manager = get_run_manager() run_manager.setup_new_run( cache_directory=config['cache_directory'], configuration=config) statusdir = run_manager.get_current_cache_directory() self.statusfile = os.path.join(statusdir, 'status.txt') self.currentLogfileYear = self.start_year self.currentLogfileKey = 0 self.config = config config['status_file_for_gui'] = self.statusfile self.commandfile = os.path.join(statusdir, 'command.txt') config['command_file_for_gui'] = self.commandfile # To test delay in writing the first log file entry... # time.sleep(5) self.running = True self.run_name = run_name self.run_manager = run_manager self.startedCallback(run_id=run_id, run_name=run_name, scenario_name=self.modeltorun, run_resources=config) run_id = run_manager.run_run(config, run_name=run_name) self.running = False succeeded = True except SimulationRunError: self.running = False succeeded = False except: self.running = False succeeded = False errorInfo = formatExceptionInfo( custom_message='Unexpected Error From Model') self.errorCallback(errorInfo) if self.statusfile is not None: gc.collect() # adding try/except, windows sometimes has a lock on this file # when it does, os.remove will cause the simulation to appear # as if it has crashed, when in fact it simply could not delete # status.txt try: os.remove(self.statusfile) except: pass self.finishedCallback(succeeded, run_name=run_name)
# The first option has the benifit of the standard __main__ syntax, # but the second option allows for use of the callbacks. # # The prefered method for opus tools is the second option # # EXAMPLE # try: importString = "from %s import opusRun" % (self.toolInclude) #print importString exec(importString) self.params = self.buildparams() if self.startingCallback != None: self.startingCallback() success = opusRun(self.progressCallback,self.logCallback,self.toolVars) #@UndefinedVariable if self.finishedCallback != None: self.progressCallback(100) self.finishedCallback(success) except ImportError, e: print "ImportError: %s" % formatExceptionInfo() success = False self.logCallback("Error importing %s: %s" % (self.toolInclude, formatExceptionInfo()) ) self.finishedCallback(success) except: success = False errorInfo = formatExceptionInfo(custom_message = 'Unexpected Error From Tool',plainText=True) #self.logCallback("Unexpected Error From Tool %s" % (self.toolInclude)) self.logCallback(errorInfo) self.finishedCallback(success)
# # The prefered method for opus tools is the second option # # EXAMPLE # try: importString = "from %s import opusRun" % (self.toolInclude) #print importString exec(importString) self.params = self.buildparams() if self.startingCallback != None: self.startingCallback() success = opusRun(self.progressCallback, self.logCallback, self.toolVars) #@UndefinedVariable if self.finishedCallback != None: self.progressCallback(100) self.finishedCallback(success) except ImportError, e: print "ImportError: %s" % formatExceptionInfo() success = False self.logCallback("Error importing %s: %s" % (self.toolInclude, formatExceptionInfo())) self.finishedCallback(success) except: success = False errorInfo = formatExceptionInfo( custom_message='Unexpected Error From Tool', plainText=True) #self.logCallback("Unexpected Error From Tool %s" % (self.toolInclude)) self.logCallback(errorInfo) self.finishedCallback(success)