def _map_variable_to_definition_node(self, variable_node): ''' creates a mapping between the variables (name, dataset) and it's definition node ''' dataset, name = get_variable_dataset_and_name(variable_node) expression_lib_nodes = get_variable_nodes_per_dataset(self.project) # Before XML 2.0 there was no dataset associated with variable selections, so some converted # files might have an empty dataset (variable name starts with a . [dot]). # in these cases we try to guess wich variable in the expression lib to associate with. # Note that built-ins like 'constant' return None as dataset, not an empty string. if dataset == '': possible_datasets = [] for dataset_name, variable_nodes in expression_lib_nodes.items(): for variable_definition_node in variable_nodes: def_variable_name = get_variable_name(variable_definition_node) if def_variable_name == name: possible_datasets.append(dataset_name) break if len(possible_datasets) == 1: dataset = possible_datasets[0] # update the variable node with the guessed dataset # variable_node.set('name', '%s.%s' %(dataset, name)) if not dataset in expression_lib_nodes: self._names_to_definition_nodes[(name, dataset)] = None else: # look through the dataset to find a variable with the same name for variable_definition_node in expression_lib_nodes[dataset]: def_variable_name = get_variable_name(variable_definition_node) if def_variable_name == name: self._names_to_definition_nodes[(name, dataset)] = variable_definition_node break else: self._names_to_definition_nodes[(name, dataset)] = None
def get_indicator(self, indicator_name, dataset_name, indicator_definition=None): if indicator_definition is not None: attribute, source = indicator_definition else: indicator_nodes = get_available_indicator_nodes(self.project) for indicator_node in indicator_nodes: dataset, name = get_variable_dataset_and_name(indicator_node) if name == indicator_name and dataset == dataset_name: attribute = (indicator_node.text or '').strip() source = indicator_node.get('source') break else: raise Exception('Could not find an indicator %s for dataset %s'\ %(indicator_name, dataset_name)) # Make sure that expressions are prepended by their names # WAS the line below, but it fails if the expression includes an argument like 'function=mean' #if attribute.find('=') == -1 and source == 'expression': if is_anonymous_autogen_name(VariableName(attribute).get_short_name()): attribute = str(indicator_name) + '=' + attribute new_indicator = Indicator(name=indicator_name, dataset_name=dataset_name, attribute=attribute) return new_indicator
def _map_variable_to_definition_node(self, variable_node): ''' creates a mapping between the variables (name, dataset) and it's definition node ''' dataset, name = get_variable_dataset_and_name(variable_node) expression_lib_nodes = get_variable_nodes_per_dataset(self.project) # Before XML 2.0 there was no dataset associated with variable selections, so some converted # files might have an empty dataset (variable name starts with a . [dot]). # in these cases we try to guess wich variable in the expression lib to associate with. # Note that built-ins like 'constant' return None as dataset, not an empty string. if dataset == '': possible_datasets = [] for dataset_name, variable_nodes in expression_lib_nodes.items(): for variable_definition_node in variable_nodes: def_variable_name = get_variable_name( variable_definition_node) if def_variable_name == name: possible_datasets.append(dataset_name) break if len(possible_datasets) == 1: dataset = possible_datasets[0] # update the variable node with the guessed dataset # variable_node.set('name', '%s.%s' %(dataset, name)) if not dataset in expression_lib_nodes: self._names_to_definition_nodes[(name, dataset)] = None else: # look through the dataset to find a variable with the same name for variable_definition_node in expression_lib_nodes[dataset]: def_variable_name = get_variable_name(variable_definition_node) if def_variable_name == name: self._names_to_definition_nodes[( name, dataset)] = variable_definition_node break else: self._names_to_definition_nodes[(name, dataset)] = None
def variable_from_node(variable_node): ''' convert a variable in node format to a variable in the variable table model format @param variable_node (Element) node to get values from @return the converted variable (dict) ''' variable = create_empty_variable() for key in ['use', 'source', 'inherited']: variable[key] = variable_node.get(key) dataset, name = get_variable_dataset_and_name(variable_node) variable['dataset'] = dataset variable['name'] = name variable['definition'] = str(variable_node.text).strip() return variable
def _setup_available_indicators(self): indicator_nodes = get_available_indicator_nodes(self.project) current_row = self.indicator_table.currentRow() self.indicator_table.clear() self.indicator_table.setColumnCount(3) self.indicator_table.setRowCount(len(indicator_nodes)) col = QTableWidgetItem() col.setText('Name') self.indicator_table.setHorizontalHeaderItem(0,col) col = QTableWidgetItem() col.setText('Dataset') self.indicator_table.setHorizontalHeaderItem(1,col) col = QTableWidgetItem() col.setText('Definition') self.indicator_table.setHorizontalHeaderItem(2,col) for i, indicator in enumerate(indicator_nodes): row = QTableWidgetItem() self.indicator_table.setVerticalHeaderItem(i, row) dataset, name = get_variable_dataset_and_name(indicator) item = QTableWidgetItem() item.setText(name) self.indicator_table.setItem(i,0,item) item = QTableWidgetItem() item.setText(dataset) self.indicator_table.setItem(i,1,item) item = QTableWidgetItem() item.setText(indicator.text or '') self.indicator_table.setItem(i,2,item) if current_row is None or current_row == -1: current_row = 0 self.indicator_table.resizeRowsToContents() self.indicator_table.resizeColumnsToContents() self.indicator_table.setCurrentCell(current_row,0) self.on_indicator_table_itemSelectionChanged()
def _setup_available_indicators(self): indicator_nodes = get_available_indicator_nodes(self.project) current_row = self.indicator_table.currentRow() self.indicator_table.clear() self.indicator_table.setColumnCount(3) self.indicator_table.setRowCount(len(indicator_nodes)) col = QTableWidgetItem() col.setText('Name') self.indicator_table.setHorizontalHeaderItem(0, col) col = QTableWidgetItem() col.setText('Dataset') self.indicator_table.setHorizontalHeaderItem(1, col) col = QTableWidgetItem() col.setText('Definition') self.indicator_table.setHorizontalHeaderItem(2, col) for i, indicator in enumerate(indicator_nodes): row = QTableWidgetItem() self.indicator_table.setVerticalHeaderItem(i, row) dataset, name = get_variable_dataset_and_name(indicator) item = QTableWidgetItem() item.setText(name) self.indicator_table.setItem(i, 0, item) item = QTableWidgetItem() item.setText(dataset) self.indicator_table.setItem(i, 1, item) item = QTableWidgetItem() item.setText(indicator.text or '') self.indicator_table.setItem(i, 2, item) if current_row is None or current_row == -1: current_row = 0 self.indicator_table.resizeRowsToContents() self.indicator_table.resizeColumnsToContents() self.indicator_table.setCurrentCell(current_row, 0) self.on_indicator_table_itemSelectionChanged()
def get_indicator(self, indicator_name, dataset_name, indicator_definition=None): if indicator_definition is not None: attribute, source = indicator_definition else: indicator_nodes = get_available_indicator_nodes(self.project) for indicator_node in indicator_nodes: dataset, name = get_variable_dataset_and_name(indicator_node) if name == indicator_name and dataset == dataset_name: attribute = (indicator_node.text or "").strip() source = indicator_node.get("source") break else: raise Exception("Could not find an indicator %s for dataset %s" % (indicator_name, dataset_name)) # Make sure that expressions are prepended by their names # WAS the line below, but it fails if the expression includes an argument like 'function=mean' # if attribute.find('=') == -1 and source == 'expression': if is_anonymous_autogen_name(VariableName(attribute).get_short_name()): attribute = str(indicator_name) + "=" + attribute new_indicator = Indicator(name=indicator_name, dataset_name=dataset_name, attribute=attribute) return new_indicator
def on_pb_generate_results_released(self): run_name = self.current_run indicator_name = self.current_indicator indicator_dataset = self.current_indicator_dataset start_year = int(self.current_year) end_year = start_year if run_name is None or indicator_name is None or start_year is None: return key = (run_name, indicator_name, start_year) if key in self.already_browsed: if not self.generating_results: (tab_widget,map_widget) = self.already_browsed[key] # self.swap_visualizations(map_widget, tab_widget) self.pb_generate_results.setText('Results Generated') else: self.queued_results = ('swap', (map_widget, tab_widget)) return self.pb_generate_results.setEnabled(False) self.pb_generate_results.setText('Generating Results...') indicator_nodes = get_available_indicator_nodes(self.project) dataset = None for indicator_node in indicator_nodes: ind_dataset, name = get_variable_dataset_and_name(indicator_node) if name == indicator_name and ind_dataset == indicator_dataset: dataset = ind_dataset break if dataset is None: raise Exception('Could not find dataset for indicator %s' % indicator_name) table_params = { 'name': None, 'output_type' : 'tab', 'indicators' : [indicator_name], } map_params = {'name':None, 'indicators':[indicator_name]} visualizations = [ ('table_per_year', dataset, table_params), ('mapnik_map', dataset, map_params) ] batch_processor = BatchProcessor(self.project) batch_processor.guiElement = self batch_processor.set_data( visualizations = visualizations, source_data_name = run_name, years = range(start_year, end_year + 1)) if not self.generating_results: self.generating_results = True logger.log_note('Generating results for %s on run %s for year %indicator_node'%(run_name, indicator_name, start_year)) self.running_key = key self.batch_processor = batch_processor batch_processor_thread = OpusGuiThread( parentThread = get_mainwindow_instance(), parentGuiElement = self, thread_object = self.batch_processor) # Use this signal from the thread if it is capable of producing its own status signal self.connect(batch_processor_thread, SIGNAL("runFinished(PyQt_PyObject)"), self._run_finished) self.connect(batch_processor_thread, SIGNAL("runError(PyQt_PyObject)"), self._run_error) batch_processor_thread.start() else: self.queued_results = (key, batch_processor)
def data(self, index, role): if not index.isValid(): return QVariant() row = index.row() header = self._headers[index.column()] node = self._variable_nodes[row] # look up the definition once for easy access by multiple roles if header == self.HEADER_DEFINITION: dataset, name = get_variable_dataset_and_name(node) if name == 'constant': definition = '<built-in constant>' elif (name, dataset) in self._names_to_definition_nodes: definition_node = self._names_to_definition_nodes[(name, dataset)] if definition_node is not None: definition = definition_node.text else: definition = '<empty definition>' else: definition = '<unknown definition>' if role == Qt.DecorationRole: if header == self.HEADER_VARIABLE: return QVariant(IconLibrary.icon('variable')) if role == Qt.DisplayRole: var_name = get_variable_name(self._variable_nodes[row]) if header == self.HEADER_VARIABLE: return QVariant(var_name) if header == self.HEADER_DEFINITION: return QVariant(definition) if header == self.HEADER_COEFF_NAME: if 'coefficient_name' in node.attrib: return QVariant(node.get('coefficient_name')) return QVariant(var_name) if header == self.HEADER_STARTING_VAL: return QVariant(node.get('starting_value') or '') if role == Qt.EditRole: if header == self.HEADER_STARTING_VAL: return self.data(index, Qt.DisplayRole) if header == self.HEADER_COEFF_NAME: # give a clear coeff name if it's not set yet if 'coefficient_name' in node.attrib: return QVariant(node.get('coefficient_name')) return QVariant() if role == Qt.CheckStateRole: if header == self.HEADER_IGNORE: return QVariant(Qt.Checked if node.get('ignore') == 'True' else Qt.Unchecked) if header == self.HEADER_FIXED: return QVariant(Qt.Checked if node.get('keep_fixed') == 'True' else Qt.Unchecked) if role == Qt.FontRole: font = QtGui.QFont() if header == self.HEADER_VARIABLE: if node.get('ignore') != 'True': font.setBold(True) return QVariant(font) elif header == self.HEADER_COEFF_NAME and node.get( 'coefficient_name'): font.setBold(True) return QVariant(font) if role == Qt.ForegroundRole: # color default values of coefficient name as light gray if header == self.HEADER_COEFF_NAME: if 'coefficient_name' not in node.attrib: return QVariant(QtGui.QColor(Qt.gray)) if role == Qt.ToolTipRole: if header == self.HEADER_DEFINITION: # defs easily get cramped so show in tool tip as well return QVariant(definition) return QVariant()
def on_pb_urbancanvas_clicked(self): run_name = self.current_run indicator_name = self.current_indicator indicator_dataset = self.current_indicator_dataset if indicator_dataset != 'parcel': MessageBox.information(mainwindow = self, text = 'Not a parcel variable. Only parcel variables can be sent to UrbanCanvas') else: start_year = int(self.current_year) end_year = start_year if run_name is None or indicator_name is None or start_year is None: return key = (run_name, indicator_name, start_year) self.pb_urbancanvas.setText('Sending to UrbanCanvas...') indicator_nodes = get_available_indicator_nodes(self.project) dataset = None for indicator_node in indicator_nodes: ind_dataset, name = get_variable_dataset_and_name(indicator_node) if name == indicator_name and ind_dataset == indicator_dataset: dataset = ind_dataset break if dataset is None: raise Exception('Could not find dataset for indicator %s' % indicator_name) table_params = { 'name': None, 'output_type' : 'tab', 'indicators' : [indicator_name], } expression_library = self.project.xml_config.get_expression_library() expression = expression_library[(dataset,name)] logger.log_note(expression) base_year = end_year project_name = self.project.name opus_data_path = self.project.xml_config.get_opus_data_path() logger.log_note(base_year) logger.log_note(project_name) logger.log_note(opus_data_path) interface = IndicatorFrameworkInterface(self.project) source_data = interface.get_source_data( source_data_name = run_name, years = [end_year,] ) cache = os.path.join(source_data.cache_directory,str(end_year)) logger.log_note(cache) storage = StorageFactory().get_storage('flt_storage',storage_location=cache) dataset_pool = DatasetPool(storage=storage, package_order=[project_name,'urbansim_parcel','urbansim','opus_core']) parcels = dataset_pool.get_dataset('parcel') parcel_ids = pd.Series(parcels.get_attribute('parcel_id')) values = pd.Series(parcels.compute_variables([expression],dataset_pool=dataset_pool).astype('float')) parcels = pd.DataFrame({"parcel_id":parcel_ids,"vl_values":values}) #parcels.set_index(keys='parcel_id',inplace=True) #parcels["vl_values"][parcels["vl_values"]==0] = np.nan parcels = parcels[parcels["vl_values"]>0] os.chdir(os.path.join(opus_data_path,project_name)) parcels.to_csv('results_browser_indicator.csv',index=False) #np.savez('results_browser_indicator',parcel_id=parcels.vl_values.index.values.astype('int32'),values=parcels.vl_values.values.astype('int32')) ##############UNCOMMENT IF WEBSERVICE IS DESIRED # parcels.save('variable_library.pkl') ##I believe 'save' was just deprectated in pandas- its now to_pickle or some such thing... change this later # web_service_path = os.path.join(os.getenv("OPUS_HOME"),'src',project_name,'scripts','web_service.py') # logger.log_note(web_service_path) # p = subprocess.Popen([sys.executable,web_service_path]) # MessageBox.information(mainwindow = self, text = 'Click OK when done viewing in UrbanCanvas') # p.kill() # self.pb_urbancanvas.setText('View in UrbanCanvas') MessageBox.information(mainwindow = self, text = 'Variable exported to the project data directory for viewing in UrbanCanvas') self.pb_urbancanvas.setText('View in UrbanCanvas')
def on_pb_urbancanvas_clicked(self): run_name = self.current_run indicator_name = self.current_indicator indicator_dataset = self.current_indicator_dataset if indicator_dataset != 'parcel': MessageBox.information( mainwindow=self, text= 'Not a parcel variable. Only parcel variables can be sent to UrbanCanvas' ) else: start_year = int(self.current_year) end_year = start_year if run_name is None or indicator_name is None or start_year is None: return key = (run_name, indicator_name, start_year) self.pb_urbancanvas.setText('Sending to UrbanCanvas...') indicator_nodes = get_available_indicator_nodes(self.project) dataset = None for indicator_node in indicator_nodes: ind_dataset, name = get_variable_dataset_and_name( indicator_node) if name == indicator_name and ind_dataset == indicator_dataset: dataset = ind_dataset break if dataset is None: raise Exception('Could not find dataset for indicator %s' % indicator_name) table_params = { 'name': None, 'output_type': 'tab', 'indicators': [indicator_name], } expression_library = self.project.xml_config.get_expression_library( ) expression = expression_library[(dataset, name)] logger.log_note(expression) base_year = end_year project_name = self.project.name opus_data_path = self.project.xml_config.get_opus_data_path() logger.log_note(base_year) logger.log_note(project_name) logger.log_note(opus_data_path) interface = IndicatorFrameworkInterface(self.project) source_data = interface.get_source_data(source_data_name=run_name, years=[ end_year, ]) cache = os.path.join(source_data.cache_directory, str(end_year)) logger.log_note(cache) storage = StorageFactory().get_storage('flt_storage', storage_location=cache) dataset_pool = DatasetPool(storage=storage, package_order=[ project_name, 'urbansim_parcel', 'urbansim', 'opus_core' ]) parcels = dataset_pool.get_dataset('parcel') parcel_ids = pd.Series(parcels.get_attribute('parcel_id')) values = pd.Series( parcels.compute_variables( [expression], dataset_pool=dataset_pool).astype('float')) parcels = pd.DataFrame({ "parcel_id": parcel_ids, "vl_values": values }) #parcels.set_index(keys='parcel_id',inplace=True) #parcels["vl_values"][parcels["vl_values"]==0] = np.nan parcels = parcels[parcels["vl_values"] > 0] os.chdir(os.path.join(opus_data_path, project_name)) parcels.to_csv('results_browser_indicator.csv', index=False) #np.savez('results_browser_indicator',parcel_id=parcels.vl_values.index.values.astype('int32'),values=parcels.vl_values.values.astype('int32')) ##############UNCOMMENT IF WEBSERVICE IS DESIRED # parcels.save('variable_library.pkl') ##I believe 'save' was just deprectated in pandas- its now to_pickle or some such thing... change this later # web_service_path = os.path.join(os.getenv("OPUS_HOME"),'src',project_name,'scripts','web_service.py') # logger.log_note(web_service_path) # p = subprocess.Popen([sys.executable,web_service_path]) # MessageBox.information(mainwindow = self, text = 'Click OK when done viewing in UrbanCanvas') # p.kill() # self.pb_urbancanvas.setText('View in UrbanCanvas') MessageBox.information( mainwindow=self, text= 'Variable exported to the project data directory for viewing in UrbanCanvas' ) self.pb_urbancanvas.setText('View in UrbanCanvas')
def on_pb_generate_results_clicked(self): run_name = self.current_run indicator_name = self.current_indicator indicator_dataset = self.current_indicator_dataset start_year = int(self.current_year) end_year = start_year if run_name is None or indicator_name is None or start_year is None: return key = (run_name, indicator_name, start_year) if key in self.already_browsed: if not self.generating_results: (tab_widget, map_widget) = self.already_browsed[key] # self.swap_visualizations(map_widget, tab_widget) self.pb_generate_results.setText('Results Generated') else: self.queued_results = ('swap', (map_widget, tab_widget)) return self.pb_generate_results.setEnabled(False) self.pb_generate_results.setText('Generating Results...') indicator_nodes = get_available_indicator_nodes(self.project) dataset = None for indicator_node in indicator_nodes: ind_dataset, name = get_variable_dataset_and_name(indicator_node) if name == indicator_name and ind_dataset == indicator_dataset: dataset = ind_dataset break if dataset is None: raise Exception('Could not find dataset for indicator %s' % indicator_name) table_params = { 'name': None, 'output_type': 'tab', 'indicators': [indicator_name], } map_params = {'name': None, 'indicators': [indicator_name]} visualizations = [('table_per_year', dataset, table_params), ('mapnik_map', dataset, map_params)] batch_processor = BatchProcessor(self.project) batch_processor.guiElement = self batch_processor.set_data(visualizations=visualizations, source_data_name=run_name, years=range(start_year, end_year + 1)) if not self.generating_results: self.generating_results = True logger.log_note( 'Generating results for %s on run %s for year %indicator_node' % (run_name, indicator_name, start_year)) self.running_key = key self.batch_processor = batch_processor batch_processor_thread = OpusGuiThread( parentThread=get_mainwindow_instance(), parentGuiElement=self, thread_object=self.batch_processor) # Use this signal from the thread if it is capable of producing its own status signal self.connect(batch_processor_thread, SIGNAL("runFinished(PyQt_PyObject)"), self._run_finished) self.connect(batch_processor_thread, SIGNAL("runError(PyQt_PyObject)"), self._run_error) batch_processor_thread.start() else: self.queued_results = (key, batch_processor)
def _setup_indicators(self, existing_indicators = []): if self.dataset_name is None: return indicator_nodes = get_available_indicator_nodes(self.project) # self.xml_helper.get_available_indicator_names(attributes = ['dataset']) current_row = self.twAvailableIndicators.currentRow() self.twAvailableIndicators.clear() self.twAvailableIndicators.setColumnCount(2) self.twAvailableIndicators.horizontalHeader().setStretchLastSection(True) self.twIndicatorsToVisualize.horizontalHeader().setStretchLastSection(True) while self.twAvailableIndicators.rowCount() > 0: self.twAvailableIndicators.removeRow(0) while self.twIndicatorsToVisualize.rowCount() > 0: self.twIndicatorsToVisualize.removeRow(0) # self.twAvailableIndicators.setRowCount(len(indicators) - len(existing_indicators)) col = QTableWidgetItem() col.setText(QString('Name')) self.twAvailableIndicators.setHorizontalHeaderItem(0,col) #col = QTableWidgetItem() #col.setText(QString('Dataset')) #self.twAvailableIndicators.setHorizontalHeaderItem(1,col) col = QTableWidgetItem() col.setText(QString('Definition')) self.twAvailableIndicators.setHorizontalHeaderItem(1,col) self.indicator_nodes = {} for indicator in indicator_nodes: dataset, name = get_variable_dataset_and_name(indicator) self.indicator_nodes[name] = indicator if name not in existing_indicators: if self.dataset_name == dataset: item = QTableWidgetItem() item.setText(name) row = self.twAvailableIndicators.rowCount() self.twAvailableIndicators.insertRow(row) #self.twAvailableIndicators.setVerticalHeaderItem(row,QTableWidgetItem()) self.twAvailableIndicators.setItem(row,0,item) #item = QTableWidgetItem() #item.setText(dataset) #self.twAvailableIndicators.setItem(i,1,item) item = QTableWidgetItem() item.setText(indicator.text or '') self.twAvailableIndicators.setItem(row,1,item) else: if self.dataset_name != dataset: logger.log_warning('Visualization configured incorrectly. Cannot have indicators for different datasets. Skipping indicator %s'%str(name)) continue item = QTableWidgetItem() item.setText(name) row = self.twIndicatorsToVisualize.rowCount() self.twIndicatorsToVisualize.insertRow(row) self.twIndicatorsToVisualize.setItem(row, 0, item) if current_row is None or current_row == -1: current_row = 0 self.twAvailableIndicators.setCurrentCell(current_row,0)
def _setup_indicators(self, existing_indicators=[]): if self.dataset_name is None: return indicator_nodes = get_available_indicator_nodes(self.project) # self.xml_helper.get_available_indicator_names(attributes = ['dataset']) current_row = self.twAvailableIndicators.currentRow() self.twAvailableIndicators.clear() self.twAvailableIndicators.setColumnCount(2) self.twAvailableIndicators.horizontalHeader().setStretchLastSection( True) self.twIndicatorsToVisualize.horizontalHeader().setStretchLastSection( True) while self.twAvailableIndicators.rowCount() > 0: self.twAvailableIndicators.removeRow(0) while self.twIndicatorsToVisualize.rowCount() > 0: self.twIndicatorsToVisualize.removeRow(0) # self.twAvailableIndicators.setRowCount(len(indicators) - len(existing_indicators)) col = QTableWidgetItem() col.setText(QString('Name')) self.twAvailableIndicators.setHorizontalHeaderItem(0, col) #col = QTableWidgetItem() #col.setText(QString('Dataset')) #self.twAvailableIndicators.setHorizontalHeaderItem(1,col) col = QTableWidgetItem() col.setText(QString('Definition')) self.twAvailableIndicators.setHorizontalHeaderItem(1, col) self.indicator_nodes = {} for indicator in indicator_nodes: dataset, name = get_variable_dataset_and_name(indicator) self.indicator_nodes[name] = indicator if name not in existing_indicators: if self.dataset_name == dataset: item = QTableWidgetItem() item.setText(name) row = self.twAvailableIndicators.rowCount() self.twAvailableIndicators.insertRow(row) #self.twAvailableIndicators.setVerticalHeaderItem(row,QTableWidgetItem()) self.twAvailableIndicators.setItem(row, 0, item) #item = QTableWidgetItem() #item.setText(dataset) #self.twAvailableIndicators.setItem(i,1,item) item = QTableWidgetItem() item.setText(indicator.text or '') self.twAvailableIndicators.setItem(row, 1, item) else: if self.dataset_name != dataset: logger.log_warning( 'Visualization configured incorrectly. Cannot have indicators for different datasets. Skipping indicator %s' % str(name)) continue item = QTableWidgetItem() item.setText(name) row = self.twIndicatorsToVisualize.rowCount() self.twIndicatorsToVisualize.insertRow(row) self.twIndicatorsToVisualize.setItem(row, 0, item) if current_row is None or current_row == -1: current_row = 0 self.twAvailableIndicators.setCurrentCell(current_row, 0)
def data(self, index, role): if not index.isValid(): return QVariant() row = index.row() header = self._headers[index.column()] node = self._variable_nodes[row] # look up the definition once for easy access by multiple roles if header == self.HEADER_DEFINITION: dataset, name = get_variable_dataset_and_name(node) if name == 'constant': definition = '<built-in constant>' elif (name, dataset) in self._names_to_definition_nodes: definition_node = self._names_to_definition_nodes[(name, dataset)] if definition_node is not None: definition = definition_node.text else: definition = '<empty definition>' else: definition = '<unknown definition>' if role == Qt.DecorationRole: if header == self.HEADER_VARIABLE: return QVariant(IconLibrary.icon('variable')) if role == Qt.DisplayRole: var_name = get_variable_name(self._variable_nodes[row]) if header == self.HEADER_VARIABLE: return QVariant(var_name) if header == self.HEADER_DEFINITION: return QVariant(definition) if header == self.HEADER_COEFF_NAME: if 'coefficient_name' in node.attrib: return QVariant(node.get('coefficient_name')) return QVariant(var_name) if header == self.HEADER_STARTING_VAL: return QVariant(node.get('starting_value') or '') if role == Qt.EditRole: if header == self.HEADER_STARTING_VAL: return self.data(index, Qt.DisplayRole) if header == self.HEADER_COEFF_NAME: # give a clear coeff name if it's not set yet if 'coefficient_name' in node.attrib: return QVariant(node.get('coefficient_name')) return QVariant() if role == Qt.CheckStateRole: if header == self.HEADER_IGNORE: return QVariant(Qt.Checked if node.get('ignore') == 'True' else Qt.Unchecked) if header == self.HEADER_FIXED: return QVariant(Qt.Checked if node.get('keep_fixed') == 'True' else Qt.Unchecked) if role == Qt.FontRole: font = QtGui.QFont() if header == self.HEADER_VARIABLE: if node.get('ignore') != 'True': font.setBold(True) return QVariant(font) elif header == self.HEADER_COEFF_NAME and node.get('coefficient_name'): font.setBold(True) return QVariant(font) if role == Qt.ForegroundRole: # color default values of coefficient name as light gray if header == self.HEADER_COEFF_NAME: if 'coefficient_name' not in node.attrib: return QVariant(QtGui.QColor(Qt.gray)) if role == Qt.ToolTipRole: if header == self.HEADER_DEFINITION: # defs easily get cramped so show in tool tip as well return QVariant(definition) return QVariant()