def runErrorFromThread(self,errorMessage): self.running = False self.paused = False self.pbnStartModel.setText(QString("Start Simulation Run...")) MessageBox.warning(mainwindow = self.mainwindow, text = "There was a problem running the simulation.", detailed_text = errorMessage)
def on_buttonBox_accepted(self): viz_params = self._get_viz_spec() if viz_params is None: self.close() return dataset_name = viz_params['dataset_name'] viz_type = viz_params['visualization_type'] close = True if (viz_type == 'mapnik_map' or viz_type == 'mapnik_animated_map') and dataset_name not in self.spatial_datasets: MessageBox.warning(mainwindow = self.mainwindow, text = "That indicator cannot be visualized as a map.", detailed_text = ('The dataset %s is either not spatial or cannot be ' 'rendered as a grid. If the latter, please try ' 'exporting to an external GIS tool.'%dataset_name)) close = False else: self._update_xml_from_dict(self.base_node, viz_params) update_batch_indicator_visualization(self.base_node) if close: self.close()
def init_for_variable(self, variable, validator, existing_variables): ''' Prepare the editor to edit a variable. @param variable The variable to edit (dict) @param validator A VariableValidator object used for data/syntax checking @param existing_variables a list of tuples with (dataset, variable name) for all existing variables. Used for checking for unique names. ''' self.original_variable = variable # keep a reference to the original node... self.variable = variable.copy( ) # ...but manipulate a temporary variable self.validator = validator self.existing_variables = existing_variables or [] self.leVarName.setText(variable['name']) index = self.cboVarType.findText(variable['source']) if index < 0: # variable source was not in the combo box text = 'Failed to edit variable %s' % variable['name'] details = ( 'Variable "%s" (dataset: %s) has an unknown type value (%s).\n' 'Please select a new type and update/save the variable.' % (variable['name'], variable['dataset'], variable['source'])) MessageBox.warning(self, text, details) self.cboVarType.setCurrentIndex(index) if variable['use'] == 'model variable': self.rbUseModel.setChecked(True) elif variable['use'] == 'indicator': self.rbUseIndicator.setChecked(True) else: self.rbUseBoth.setChecked(True) self.le_var_def.document().setPlainText(variable['definition']) self.leVarName.selectAll() self.leVarName.setFocus()
def _scanForRuns(self): data_path = self.project.data_path() if not os.path.exists(data_path): MessageBox.warning(mainwindow = self.base_widget, text="Project data path %s doesn't exist. " % data_path + \ "Simulation runs in the Results tab cannot be updated." ) return run_manager = get_run_manager() run_manager.clean_runs() self._sync_base_year_data(run_manager) run_manager.close() added_runs, removed_runs = sync_available_runs(self.project) added_msg = removed_msg = None if len(added_runs) > 0: added_msg = ('The following simulation runs have been ' 'automatically added to the results manager:\n\n%s' % '\n'.join(added_runs)) if len(removed_runs) > 0: removed_msg = ('The following simulation runs have been ' 'automatically removed from the results manager:\n\n%s' % '\n'.join(removed_runs)) if added_msg or removed_msg: ## The idea is to leave the run information to services db & cache, and ## we don't need to save the newly added runs, once we set the temporary # self.project.dirty = True text = 'The list of simulation runs has been automatically updated.' detailed_text = '%s\n%s' % (added_msg or '', removed_msg or '') logger.log_status(text+'\n'+detailed_text)
def check_variable(self, check='syntax'): ''' validate the variable and display the result in i dialog box if check_syntax is True a syntax check is performed, otherwise a data check is performed. ''' if check == 'syntax': func = batch_check_syntax elif check == 'data': func = batch_check_data else: raise ValueError( 'check_variable() got an unknown value for argument "check"; "%s"' % check) self._update_variable_from_fields() dummy, result, msgs = func([ self.variable, ], self.validator)[0] if result is True: text = '%s check OK' % check MessageBox.information(mainwindow=self, text=text) else: text = 'Encountered a %s error' % check MessageBox.warning(mainwindow=self, text=text, detailed_text='\n '.join(msgs))
def on_buttonBox_accepted(self): viz_params = self._get_viz_spec() if viz_params is None: self.close() return dataset_name = viz_params['dataset_name'] viz_type = viz_params['visualization_type'] close = True if (viz_type == 'mapnik_map' or viz_type == 'mapnik_animated_map' ) and dataset_name not in self.spatial_datasets: MessageBox.warning( mainwindow=self.mainwindow, text="That indicator cannot be visualized as a map.", detailed_text=( 'The dataset %s is either not spatial or cannot be ' 'rendered as a grid. If the latter, please try ' 'exporting to an external GIS tool.' % dataset_name)) close = False else: self._update_xml_from_dict(self.base_node, viz_params) update_batch_indicator_visualization(self.base_node) if close: self.close()
def runErrorFromThread(self, errorMessage): self.running = False self.paused = False self.pbnStartModel.setText(QString("Start Estimation...")) MessageBox.warning(mainwindow=self, text="There was a problem with the estimation.", detailed_text=errorMessage)
def init_for_variable(self, variable, validator, existing_variables): ''' Prepare the editor to edit a variable. @param variable The variable to edit (dict) @param validator A VariableValidator object used for data/syntax checking @param existing_variables a list of tuples with (dataset, variable name) for all existing variables. Used for checking for unique names. ''' self.original_variable = variable # keep a reference to the original node... self.variable = variable.copy() # ...but manipulate a temporary variable self.validator = validator self.existing_variables = existing_variables or [] self.leVarName.setText(variable['name']) self.le_var_def.document().setPlainText(variable['definition']) index = self.cboVarType.findText(variable['source']) if index < 0: # variable source was not in the combo box text = 'Failed to edit variable %s' % variable['name'] details = ('Variable "%s" (dataset: %s) has an unknown type value (%s).\n' 'Please select a new type and update/save the variable.' % (variable['name'], variable['dataset'], variable['source'])) MessageBox.warning(self, text, details) # This triggers _update_variable_from_fields and hence requires # name and definition to be set already self.cboVarType.setCurrentIndex(index) if variable['use'] == 'model variable': self.rbUseModel.setChecked(True) elif variable['use'] == 'indicator': self.rbUseIndicator.setChecked(True) else: self.rbUseBoth.setChecked(True) self.leVarName.selectAll() self.leVarName.setFocus()
def delete_run(self, run_node, force=False): ''' Remove a run both from the services database and from the model. @param run_node (Element): the node to remove. ''' # Prevent the user from removing base years cache_directory = run_node.find('cache_directory').text if cache_directory.endswith('base_year_data') and not force: msg = ('Removing the base year data directory is restricted from ' 'within OpusGUI since doing so will make it impossible to ' 'run any simulations or estimations.') MessageBox.warning(mainwindow = self.view, text = 'Cannot remove base year data', detailed_text = msg) return try: run_manager = get_run_manager() run_id = run_node.get('run_id') try: run_id = int(run_id) except: run_id = -1 run_manager.delete_everything_for_this_run(run_id, cache_directory) run_manager.close() self.project.delete_node(run_node) # self.model.remove_node(run_node) except Exception, ex: # TODO catch more specific error? MessageBox.warning(self.view, 'Could not remove run', str(ex))
def on_buttonBox_accepted(self): path = str(self.lePath.text()) if not os.path.exists(path): msg = 'Cannot import run from %s: path does not exist' % path logger.log_warning(msg) MessageBox.warning(mainwindow=self, text=msg, detailed_text='') else: self.resultManagerBase.import_run(path) self.close()
def import_run(self, cache_directory, run_info={}): run_manager = get_run_manager() retval, retmsg = run_manager.import_run_from_cache(cache_directory) if not retval: MessageBox.warning(mainwindow = self.base_widget, text = retmsg, detailed_text = '') else: sync_available_runs(self.project)
def on_buttonBox_accepted(self): path = str(self.lePath.text()) if not os.path.exists(path): msg = 'Cannot import run from %s: path does not exist' % path logger.log_warning(msg) MessageBox.warning(mainwindow = self, text = msg, detailed_text = '') else: self.resultManagerBase.import_run(path) 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 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 _validate_names(self, show_error_message=False): ''' go through all nest and equation names and ensure that there are no collisions. Returns True if all the names are valid and False otherwise. If @param show_error_message is True an error message of the name errors is displayed.''' # Check for colliding names among the nest and equations colliding_names = set() nodes_to_inspect = self.submodel_node.findall('.//nest') nodes_to_inspect.extend(self.submodel_node.findall('.//equation')) for inspected_node in nodes_to_inspect: # get all sibling names with the same tag sibling_names = [ node.get('name') for node in inspected_node.getparent() if node is not inspected_node and node.tag == inspected_node.tag ] # if there is a name collision, add the name to the set of found colliding names if inspected_node.get('name') in sibling_names: parent_node = inspected_node.getparent() if parent_node.tag == 'nest': desc = '<%s>/%s' % (parent_node.get('name'), inspected_node.get('name')) else: desc = '%s' % inspected_node.get('name') colliding_names.add(desc) # the concept of colliding names might be confusing so we want to be clear on what is # happening and (more importantly) how to solve it if colliding_names: if not show_error_message: return False str_collide_list = ''.join( ['<li>%s</li>\n' % name for name in colliding_names]) short_msg = 'Name collisions found.' longer_msg = ''' <qt> Colliding names: <b> <ul> %s </ul> </b> <p>A name collision is when there are two items with the same name, the same type and the same level.</p> For example: <ul> <li>MY_NEST</li> <li><ul><li>MY_EQUATION</li><li>MY_EQUATION</li></ul></li> </ul> <p>will cause a name collision, while this example;</p> <ul> <li>MY_NEST</li> <li><ul><li>MY_EQUATION</li></ul></li> <li>MY_OTHER_NEST</li> <li> <ul><li>MY_EQUATION</li></ul></li> </ul> <p>is fine since the two equations with the same name are on different levels.</p> <p>To correct this error please give unique names for the above mentioned equations and/or nests.</p></qt>''' % str_collide_list MessageBox.warning(self, short_msg, longer_msg) return False return True
def on_indicatorBox(self): indicator_name = str(self.diagnostic_indicator_name.currentText()) dataset_name = self.diagnostic_dataset_name.currentText() indicator_type = str(self.diagnostic_indicator_type.currentText()) year = str(self.diagnostic_year.currentText()) if year == '': return year = int(year) if dataset_name not in self.spatial_datasets and indicator_type == 'map': MessageBox.warning( mainwindow=self.mainwindow, text="That indicator cannot be visualized as a map.", detailed_text=( 'The dataset %s is either not spatial or cannot be ' 'rendered as a grid. If the latter, please try ' 'exporting to an external GIS tool.' % dataset_name)) return cache_directory = self.model.config['cache_directory'] params = {'indicators': [indicator_name]} if indicator_type == 'table': indicator_type = 'tab' params['output_type'] = 'tab' # JLM > commented this out for when implemented animations # else: # indicator_type = 'mapnik_map' visualizations = [(indicator_type, str(dataset_name), params)] self.batch_processor = BatchProcessor(self.project) self.batch_processor.guiElement = self self.batch_processor.set_data(visualizations=visualizations, source_data_name=self.model.run_name, years=[year, year], cache_directory=cache_directory) self.diagnosticThread = OpusGuiThread( parentThread=self.mainwindow, parentGuiElement=self, thread_object=self.batch_processor) # Use this signal from the thread if it is capable of producing its own status signal QObject.connect(self.diagnosticThread, SIGNAL("runFinished(PyQt_PyObject)"), self.visualizationsCreated) QObject.connect(self.diagnosticThread, SIGNAL("runError(PyQt_PyObject)"), self.runErrorFromThread) self.diagnosticThread.start()
def load_table(self, visualization, limit=10000): storage = StorageFactory().get_storage( type='%s_storage' % visualization.output_type, storage_location=visualization.storage_location) table_data = storage.load_table(table_name=visualization.table_name) try: primary_keys = visualization.indicators[0].primary_keys except: primary_keys = [] keys = primary_keys + [ key for key in table_data.keys() if key not in primary_keys ] num_rows = min(len(table_data[keys[0]]), limit) num_cols = len(keys) self.tableWidget.clear() self.tableWidget.setColumnCount(num_cols) self.tableWidget.setRowCount(num_rows) j = 0 for key in keys: col = QTableWidgetItem() col.setText(QString(key)) self.tableWidget.setHorizontalHeaderItem(j, col) j += 1 self.tableWidget.resizeColumnsToContents() order = sorted(enumerate(table_data[keys[0]]), lambda (i, v), (j, v2): int(v * 100) - int(v2 * 100)) for i, (idx, v) in enumerate(order): row = QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(i, row) j = 0 for key in keys: item = QTableWidgetItem() item.setText(QString(str(table_data[key][idx]))) self.tableWidget.setItem(i, j, item) j += 1 if i > limit: msg = 'The table %s has been truncated to %i rows because of memory limitations.' % ( visualization.table_name, limit) detailed_msg = '<qt>To view the full results, open the following file:<br><br><small>%s</small></qt>' % visualization.get_file_path( ) MessageBox.warning(mainwindow=self, text=msg, detailed_text=detailed_msg) break
def on_indicatorBox(self): indicator_name = str(self.diagnostic_indicator_name.currentText()) dataset_name = self.diagnostic_dataset_name.currentText() indicator_type = str(self.diagnostic_indicator_type.currentText()) year = str(self.diagnostic_year.currentText()) if year == "": return year = int(year) if dataset_name not in self.spatial_datasets and indicator_type == "map": MessageBox.warning( mainwindow=self.mainwindow, text="That indicator cannot be visualized as a map.", detailed_text=( "The dataset %s is either not spatial or cannot be " "rendered as a grid. If the latter, please try " "exporting to an external GIS tool." % dataset_name ), ) return cache_directory = self.model.config["cache_directory"] params = {"indicators": [indicator_name]} if indicator_type == "table": indicator_type = "tab" params["output_type"] = "tab" # JLM > commented this out for when implemented animations # else: # indicator_type = 'mapnik_map' visualizations = [(indicator_type, str(dataset_name), params)] self.batch_processor = BatchProcessor(self.project) self.batch_processor.guiElement = self self.batch_processor.set_data( visualizations=visualizations, source_data_name=self.model.run_name, years=[year, year], cache_directory=cache_directory, ) self.diagnosticThread = OpusGuiThread( parentThread=self.mainwindow, parentGuiElement=self, thread_object=self.batch_processor ) # Use this signal from the thread if it is capable of producing its own status signal QObject.connect(self.diagnosticThread, SIGNAL("runFinished(PyQt_PyObject)"), self.visualizationsCreated) QObject.connect(self.diagnosticThread, SIGNAL("runError(PyQt_PyObject)"), self.runErrorFromThread) self.diagnosticThread.start()
def _show_problem_variables(self): ''' shows the current list of problem variables ''' if not self.problem_variables: MessageBox.information(self, 'All tested variables passed.') return msg = '<qt>' for dummy, err_msg in self.problem_variables: msg += '<br/>'.join(err_msg) msg += '<br/><br/>' msg += '</qt>' txt = 'Found problems with %d of the variables' % len(self.problem_variables) MessageBox.warning(self, txt, msg)
def _show_problem_variables(self): ''' shows the current list of problem variables ''' if not self.problem_variables: MessageBox.information(self, 'All tested variables passed.') return msg = '<qt>' for dummy, err_msg in self.problem_variables: msg += '<br/>'.join(err_msg) msg += '<br/><br/>' msg += '</qt>' txt = 'Found problems with %d of the variables' % len( self.problem_variables) MessageBox.warning(self, txt, msg)
def load_table(self, visualization, limit = 10000): storage = StorageFactory().get_storage( type = '%s_storage'%visualization.output_type, storage_location = visualization.storage_location) table_data = storage.load_table( table_name = visualization.table_name) try: primary_keys = visualization.indicators[0].primary_keys except: primary_keys = [] keys = primary_keys + [key for key in table_data.keys() if key not in primary_keys] num_rows = min(len(table_data[keys[0]]), limit) num_cols = len(keys) self.tableWidget.clear() self.tableWidget.setColumnCount(num_cols) self.tableWidget.setRowCount(num_rows) j = 0 for key in keys: col = QTableWidgetItem() col.setText(QString(key)) self.tableWidget.setHorizontalHeaderItem(j,col) j += 1 self.tableWidget.resizeColumnsToContents() order = sorted(enumerate(table_data[keys[0]]), lambda (i,v),(j,v2): int(v*100)-int(v2*100)) for i, (idx,v) in enumerate(order): row = QTableWidgetItem() self.tableWidget.setVerticalHeaderItem(i,row) j = 0 for key in keys: item = QTableWidgetItem() item.setText(QString(str(table_data[key][idx]))) self.tableWidget.setItem(i,j,item) j += 1 if i > limit: msg = 'The table %s has been truncated to %i rows because of memory limitations.'%(visualization.table_name,limit) detailed_msg = '<qt>To view the full results, open the following file:<br><br><small>%s</small></qt>'%visualization.get_file_path() MessageBox.warning(mainwindow = self, text = msg, detailed_text = detailed_msg) break
def _validate_names(self, show_error_message = False): ''' go through all nest and equation names and ensure that there are no collisions. Returns True if all the names are valid and False otherwise. If @param show_error_message is True an error message of the name errors is displayed.''' # Check for colliding names among the nest and equations colliding_names = set() nodes_to_inspect = self.submodel_node.findall('.//nest') nodes_to_inspect.extend(self.submodel_node.findall('.//equation')) for inspected_node in nodes_to_inspect: # get all sibling names with the same tag sibling_names = [node.get('name') for node in inspected_node.getparent() if node is not inspected_node and node.tag == inspected_node.tag] # if there is a name collision, add the name to the set of found colliding names if inspected_node.get('name') in sibling_names: parent_node = inspected_node.getparent() if parent_node.tag == 'nest': desc = '<%s>/%s' % (parent_node.get('name'), inspected_node.get('name')) else: desc = '%s' % inspected_node.get('name') colliding_names.add(desc) # the concept of colliding names might be confusing so we want to be clear on what is # happening and (more importantly) how to solve it if colliding_names: if not show_error_message: return False str_collide_list = ''.join(['<li>%s</li>\n' % name for name in colliding_names]) short_msg = 'Name collisions found.' longer_msg = ''' <qt> Colliding names: <b> <ul> %s </ul> </b> <p>A name collision is when there are two items with the same name, the same type and the same level.</p> For example: <ul> <li>MY_NEST</li> <li><ul><li>MY_EQUATION</li><li>MY_EQUATION</li></ul></li> </ul> <p>will cause a name collision, while this example;</p> <ul> <li>MY_NEST</li> <li><ul><li>MY_EQUATION</li></ul></li> <li>MY_OTHER_NEST</li> <li> <ul><li>MY_EQUATION</li></ul></li> </ul> <p>is fine since the two equations with the same name are on different levels.</p> <p>To correct this error please give unique names for the above mentioned equations and/or nests.</p></qt>'''% str_collide_list MessageBox.warning(self, short_msg, longer_msg) return False return True
def check_variable(self, check = 'syntax'): ''' validate the variable and display the result in i dialog box if check_syntax is True a syntax check is performed, otherwise a data check is performed. ''' if check == 'syntax': func = batch_check_syntax elif check == 'data': func = batch_check_data else: raise ValueError('check_variable() got an unknown value for argument "check"; "%s"' % check) self._update_variable_from_fields() dummy, result, msgs = func([self.variable,], self.validator)[0] if result is True: text = '%s check OK' % check MessageBox.information(mainwindow = self, text = text) else: text = 'Encountered a %s error' % check MessageBox.warning(mainwindow = self, text = text, detailed_text = '\n '.join(msgs))
def runErrorFromThread(self,errorMessage): MessageBox.warning(mainwindow = self.mainwindow, text = "There was a problem running the batch.", detailed_text = errorMessage)
def _sync_base_year_data(self, run_manager=None): """ synchronize base_year_data information in xml_configuration with run_activity table. Information in xml_configuration takes precedent, because we assume users don't directly modify data in serivecs.run_activity table """ # TODO baseyear_dir is somewhat hard-coded; it would be better to read # from xml_configuration instead, but there is no such node currently run_name = 'base_year_data' baseyear_dir = os.path.join(self.project.data_path(), run_name) baseyear_dir = os.path.normpath(baseyear_dir) if not os.path.exists(baseyear_dir): MessageBox.warning(mainwindow = self.base_widget, text="base_year_data directory %s doesn't exist. " % baseyear_dir ) return import glob years = [int(os.path.basename(year_dir)) for year_dir in glob.glob(os.path.join(baseyear_dir, '[0-9][0-9][0-9][0-9]'))] if not years: MessageBox.warning(mainwindow = self.base_widget, text="base_year_data directory %s doesn't contain any year sub-directory. " % baseyear_dir ) return start_year = min(years) end_year = max(years) base_year = end_year # default to the last year in baseyear_dir # and update it with information found in scenario_manager scenario_manager_node = self.project.find('scenario_manager') for base_year_node in scenario_manager_node.findall('.//base_year'): try: base_year = int(base_year_node.text.strip()) break except (TypeError, ValueError): continue resources = { 'cache_directory': baseyear_dir, 'description': 'base year data', 'base_year': base_year, 'years': (start_year, end_year) } if run_manager is None: run_manager = get_run_manager() base_year_data_db = run_manager.get_runs(run_name=run_name, process_name=get_host_name()) if len(base_year_data_db) == 0: run_id = run_manager._get_new_run_id() elif len(base_year_data_db) >= 1: for idx, row in enumerate(base_year_data_db): if idx==0: run_id = row[0] else: run_manager.delete_everything_for_this_run(row[0]) resources_db = run_manager.get_resources_for_run_id_from_history(run_id) if resources_db.get('cache_directory', '') == baseyear_dir and \ resources_db.get('base_year', -1) == base_year: #all good, we don't need to do anything return else: resources_db.merge(resources) resources = resources_db run_manager.add_row_to_history(run_id = run_id, resources = resources, status = 'done', run_name = run_name)