def _apply_variable_changes(self):
        ''' apply the changes the user made to the expression library '''
        # Case  XML (before)     Editor (after)   Change     Action
        # ----------------------------------------------------------
        #  A    <none>           local            created    create
        #  B    local            inherited        reverted   delete
        #  C    local            <none>           deleted    delete

        # TODO: also check into the possibility that an inherited variable can have it's name
        # changed (in this case don't overwrite the orginal variable's name. Instead create a new
        # variable with the new name.

        dirty_variables = [var for var in self.model.variables if var['dirty']]
        # case A
        create_set = [var for var in dirty_variables if var['originalnode'] is None]
        delete_set = []
        for xml_node in self.original_nodes:
            if xml_node.get('inherited') is not None: # only care about local or shadowing nodes
                continue
            for variable in self.model.variables:
                if variable['originalnode'] is xml_node: # original is represented in variable list
                    if variable['inherited']:
                        delete_set.append(xml_node) # Case B
                    break # stop looking
            else: # did not find node
                delete_set.append(xml_node) # case C
        # the rest of the variables should just be updated
        update_set = [var for var in dirty_variables if
                      not var['originalnode'] in delete_set and var not in create_set]

        # Apply the changes to each set of nodes
        expression_lib = self.project.find('general/expression_library')
        for variable in create_set:
            # print 'CREATE SET ', variable
            node = node_from_variable(variable)
            # print '  node', node
            self.project.insert_node(node, expression_lib)

        for variable in update_set:
            node = node_from_variable(variable)
            original_node = variable['originalnode'] # reference to the actual XML node
            # print 'UPDATE SET %s (original %s)' %(variable['name'], original_node)
            self.project.make_local(original_node)
            for key in node.attrib:
                if not key == 'inherited':
                    original_node.set(key, node.get(key))
            if 'dataset' in original_node.attrib:
                del original_node.attrib['dataset']
            original_node.text = node.text

        for node in delete_set:
            # print 'DELETE SET %s' % (node)
            self.project.delete_node(node)
        self.initialize()
        something_changed = bool(update_set or create_set or delete_set)
        update_mainwindow_savestate(something_changed)
        get_mainwindow_instance().emit(SIGNAL('variables_updated'))
        return True
 def copy_selected_to_parent(self):
     '''
     Copy the selected item to the first parent configuration
     '''
     if not self.has_selected_item():
         return
     if not get_mainwindow_instance().okToCloseProject(
             'copying the node to the parent configuration (reload required)'
     ):
         return
     self.model.copy_to_parent(self.selected_index())
     get_mainwindow_instance().reloadProject()
 def move_selected_to_parent(self):
     '''
     Move the selected item to the first parent configuration
     '''
     if not self.has_selected_item():
         return
     if not get_mainwindow_instance().okToCloseProject(
             'moving the node to the parent configuration (reload required)'
     ):
         return
     self.model.move_to_parent(self.selected_index())
     self.project.save()
     get_mainwindow_instance().reloadProject()
Exemple #4
0
    def _apply_variable_changes(self):
        ''' apply the changes the user made to the expression library '''
        # TODO: also check into the possibility that an inherited variable can have its name
        # changed (in this case don't overwrite the original variable's name. Instead create a new
        # variable with the new name.)

        dirty_variables = [var for var in self.model.all_variables if var['dirty']]

        # partition dirty variables into create, delete and update sets
        create_set = [var for var in dirty_variables if var['originalnode'] is None]
        delete_set = [var for var in dirty_variables if var['delete'] and var['originalnode'] is not None]
        # the rest of the variables should just be updated
        update_set = [var for var in dirty_variables if
                      not var['originalnode'] in delete_set and var not in create_set]
        
        # verify if we have a partition
        assert(set([str(var) for var in create_set]) | set([str(var) for var in delete_set]) | set([str(var) for var in update_set]) 
               == set([str(var) for var in dirty_variables]))
        assert(set([str(var) for var in create_set]) & set([str(var) for var in delete_set]) & set([str(var) for var in update_set]) 
               == set())

        # Apply the changes to each set of nodes
        expression_lib = self.project.find('general/expression_library')
        for variable in create_set:
            # print 'CREATE SET ', variable
            node = node_from_variable(variable)
            # print '  node', node
            self.project.insert_node(node, expression_lib)

        for variable in update_set:
            node = node_from_variable(variable)
            original_node = variable['originalnode'] # reference to the actual XML node
            # print 'UPDATE SET %s (original %s)' %(variable['name'], original_node)
            self.project.make_local(original_node)
            for key in node.attrib:
                if not key == 'inherited':
                    original_node.set(key, node.get(key))
            if 'dataset' in original_node.attrib:
                del original_node.attrib['dataset']
            original_node.text = node.text

        for variable in delete_set:
            # print 'DELETE SET %s' % (node)
            self.project.delete_node(variable['originalnode'])
        self.initialize()
        something_changed = bool(update_set or create_set or delete_set)
        update_mainwindow_savestate(something_changed)
        get_mainwindow_instance().emit(SIGNAL('variables_updated'))
        return True
    def _init_run(self, run_name):
        # Fire up a new thread and run the model
        self.pbnStartModel.setText(QString("Pause simulation run..."))
        # References to the GUI elements for status for this run...
        self.progressBarTotal = self.runProgressBarTotal
        self.progressBarYear = self.runProgressBarYear
        self.progressBarModel = self.runProgressBarModel

        # self.pbnRemoveModel.setEnabled(False)
        # self.pbnStartModel.setEnabled(False)

        # Initializing values
        self.progressBarTotal.setValue(0)
        self.progressBarYear.setValue(0)
        self.progressBarModel.setValue(0)
        self.progressBarTotal.setRange(0, 0)
        self.progressBarYear.setRange(0, 0)
        self.progressBarModel.setRange(0, 0)

        batch_name = str(self.cboOptionalIndicatorBatch.currentText())
        if batch_name == "(None)":
            batch_name = None

        self.runThread = RunModelThread(get_mainwindow_instance(), self, batch_name, run_name)

        # Use this signal from the thread if it is capable of producing its own status signal
        QObject.connect(self.runThread, SIGNAL("runFinished(PyQt_PyObject)"), self.runFinishedFromThread)
        QObject.connect(self.runThread, SIGNAL("runError(PyQt_PyObject)"), self.runErrorFromThread)
        # Use this timer to call a function in the thread to check status if the thread is unable
        # to produce its own signal above
        self.timer = QTimer()
        QObject.connect(self.timer, SIGNAL("timeout()"), self.runStatusFromThread)
        self.timer.start(1000)
        self.running = True
        self.paused = False
 def __init__(self, mainwindow, modelguielement, batch_name = None, run_name = None):
     QThread.__init__(self, mainwindow)
     self.modelguielement = modelguielement
     self.modelguielement.model.run_name = run_name
     self.batch_name = batch_name
     self.run_name = run_name
     self.project = get_mainwindow_instance().project
Exemple #7
0
    def __init__(self, project, parent_widget):
        QWidget.__init__(self, parent_widget)
        self.setupUi(self)

        self.project = project

        #TODO handle this as the variables are handled
        try:
            obj = get_manager_instance('results').xml_controller.model
            self.connect(obj, SIGNAL("layoutChanged()"),
                         self._setup_simulation_data)
        except AttributeError:
            pass

        def test():
            print 'Variables updated'

        self.connect(get_mainwindow_instance(), SIGNAL('variables_updated'),
                     test)

        tool_tip = ('If checked, indicator results will automatically be\n'
                    'created for the currently selected simulation run,\n'
                    'indicator, and years. If unchecked, click\n'
                    'Generate results in order to make results available.')
        self.cb_auto_gen.setToolTip(tool_tip)

        self.inGui = False
        self.logFileKey = 0
        self.available_years_for_simulation_runs = {}

        self.current_indicator = ''
        self.current_indicator_dataset = ''
        self.current_run = ''
        self.current_year = ''
        self._update_current_label()

        self.running_key = None
        self.queued_results = None

        self.setup = True

        self._setup_available_indicators()
        self._setup_simulation_data()

        self.setup = False

        self.already_browsed = {}

        self.generating_results = False
        if self.cb_auto_gen.isChecked():
            self.on_pb_generate_results_clicked()
        # self.pb_export_results.setEnabled(False)
        self.tabwidget_visualizations.removeTab(0)
        self.tabIcon = IconLibrary.icon('table')
        self.tabLabel = "Result Browser"
    def __init__(self, project, parent_widget):
        QWidget.__init__(self, parent_widget)
        self.setupUi(self)

        self.project = project

        # TODO handle this as the variables are handled
        try:
            obj = get_manager_instance("results").xml_controller.model
            self.connect(obj, SIGNAL("layoutChanged()"), self._setup_simulation_data)
        except AttributeError:
            pass

        def test():
            print "Variables updated"

        self.connect(get_mainwindow_instance(), SIGNAL("variables_updated"), test)

        tool_tip = (
            "If checked, indicator results will automatically be\n"
            "created for the currently selected simulation run,\n"
            "indicator, and years. If unchecked, click\n"
            "Generate results in order to make results available."
        )
        self.cb_auto_gen.setToolTip(tool_tip)

        self.inGui = False
        self.logFileKey = 0
        self.available_years_for_simulation_runs = {}

        self.current_indicator = ""
        self.current_indicator_dataset = ""
        self.current_run = ""
        self.current_year = ""
        self._update_current_label()

        self.running_key = None
        self.queued_results = None

        self.setup = True

        self._setup_available_indicators()
        self._setup_simulation_data()

        self.setup = False

        self.already_browsed = {}

        self.generating_results = False
        if self.cb_auto_gen.isChecked():
            self.on_pb_generate_results_clicked()
        # self.pb_export_results.setEnabled(False)
        self.tabwidget_visualizations.removeTab(0)
        self.tabIcon = IconLibrary.icon("table")
        self.tabLabel = "Result Browser"
Exemple #9
0
 def __init__(self,
              mainwindow,
              modelguielement,
              batch_name=None,
              run_name=None):
     QThread.__init__(self, mainwindow)
     self.modelguielement = modelguielement
     self.modelguielement.model.run_name = run_name
     self.batch_name = batch_name
     self.run_name = run_name
     self.project = get_mainwindow_instance().project
Exemple #10
0
 def __init__(self, mainwindow, modelguielement, batch_name = None, run_name = None):
     QThread.__init__(self, mainwindow)
     self.modelguielement = modelguielement
     self.modelguielement.model.run_name = run_name
     self.batch_name = batch_name
     self.run_name = run_name
     self.project = get_mainwindow_instance().project
     
     self.restart = False
     self.run_id = None
     self.config = None
     self.restart_year = None        
Exemple #11
0
 def __init__(self, mainwindow, modelguielement, batch_name = None, run_name = None):
     QThread.__init__(self, mainwindow)
     self.modelguielement = modelguielement
     self.modelguielement.model.run_name = run_name
     self.batch_name = batch_name
     self.run_name = run_name
     self.project = get_mainwindow_instance().project
     
     self.restart = False
     self.run_id = None
     self.config = None
     self.restart_year = None        
def _action_before_continue(question, buttons, parent_widget):
    ''' base for dialogs that ask users to close with dirty data '''
    if parent_widget is None:
        parent_widget = get_mainwindow_instance()
    ok_answers = [QMessageBox.Apply, QMessageBox.Save, QMessageBox.Yes]
    answer = QMessageBox.question(parent_widget, "Warning", question, *buttons)
    if answer in ok_answers:
        return YES
    elif answer == QMessageBox.Discard:
        return NO
    elif answer == QMessageBox.Cancel:
        return CANCEL
    return UNKNOWN_ANSWER
Exemple #13
0
def _action_before_continue(question, buttons, parent_widget):
    ''' base for dialogs that ask users to close with dirty data '''
    if parent_widget is None:
        parent_widget = get_mainwindow_instance()
    ok_answers = [QMessageBox.Apply, QMessageBox.Save, QMessageBox.Yes]
    answer = QMessageBox.question(parent_widget, "Warning", question, *buttons)
    if answer in ok_answers:
        return YES
    elif answer == QMessageBox.Discard:
        return NO
    elif answer == QMessageBox.Cancel:
        return CANCEL
    return UNKNOWN_ANSWER
Exemple #14
0
    def on_pbnStartModel_released(self):

        if self.running == True and self.paused == False:
            # Take care of pausing a run
            self.paused = True
            self.timer.stop()
            self.runThread.pause()
            self.pbnStartModel.setText(QString("Resume Estimation..."))

        elif self.running == True and self.paused == True:
            # Need to resume a paused run
            self.paused = False
            self.timer.start(1000)
            self.runThread.resume()
            self.pbnStartModel.setText(QString("Pause Estimation..."))

        elif self.running == False:
            self.logFileKey = 0
            # Update the XMLConfig
            self.manager.project.update_xml_config()
            # Fire up a new thread and run the estimation
            # References to the GUI elements for status for this run...
            self.progressBar = self.runProgressBar
            self.statusLabel = self.runStatusLabel
            self.pbnStartModel.setText(QString("Pause Estimation..."))
            self.progressBar.setValue(0)
            self.statusLabel.setText(QString("Estimation initializing..."))
            self.runThread = RunEstimationThread(get_mainwindow_instance(),
                                                 self)
            # Use this signal from the thread if it is capable of producing its own status signal
            QObject.connect(self.runThread,
                            SIGNAL("estimationFinished(PyQt_PyObject)"),
                            self.runFinishedFromThread)
            QObject.connect(self.runThread,
                            SIGNAL("estimationError(PyQt_PyObject)"),
                            self.runErrorFromThread)
            # Use this timer to call a function in the thread to check status if the thread is unable
            # to produce its own signal above
            self.timer = QTimer()
            QObject.connect(self.timer, SIGNAL("timeout()"),
                            self.runStatusFromThread)
            self.timer.start(1000)
            self.running = True
            self.paused = False
            self.runThread.start()
        else:
            print "Unexpected state in the estimation run..."
    def on_pbnStartModel_clicked(self):

        if self.running == True and self.paused == False:
            # Take care of pausing a run
            self.paused = True
            self.timer.stop()
            self.runThread.pause()
            self.pbnStartModel.setText(QString("Resume Estimation..."))

        elif self.running == True and self.paused == True:
            # Need to resume a paused run
            self.paused = False
            self.timer.start(1000)
            self.runThread.resume()
            self.pbnStartModel.setText(QString("Pause Estimation..."))

        elif self.running == False:
            self.logFileKey = 0
            # Update the XMLConfig
            self.manager.project.update_xml_config()
            # Fire up a new thread and run the estimation
            # References to the GUI elements for status for this run...
            self.progressBar = self.runProgressBar
            self.statusLabel = self.runStatusLabel
            self.pbnStartModel.setText(QString("Pause Estimation..."))
            self.progressBar.setValue(0)
            self.statusLabel.setText(QString("Estimation initializing..."))
            self.runThread = RunEstimationThread(get_mainwindow_instance(), self)
            # Use this signal from the thread if it is capable of producing its own status signal
            QObject.connect(self.runThread, SIGNAL("estimationFinished(PyQt_PyObject)"),
                            self.runFinishedFromThread)
            QObject.connect(self.runThread, SIGNAL("estimationError(PyQt_PyObject)"),
                            self.runErrorFromThread)
            # Use this timer to call a function in the thread to check status if the thread is unable
            # to produce its own signal above
            self.timer = QTimer()
            QObject.connect(self.timer, SIGNAL("timeout()"),
                            self.runStatusFromThread)
            self.timer.start(1000)
            self.running = True
            self.paused = False
            self.runThread.start()
        else:
            print "Unexpected state in the estimation run..."
    def _init_run(self, run_name):
        # Fire up a new thread and run the model
        self.pbnStartModel.setText(QString("Pause simulation run..."))
        # References to the GUI elements for status for this run...
        self.progressBarTotal = self.runProgressBarTotal
        self.progressBarYear = self.runProgressBarYear
        self.progressBarModel = self.runProgressBarModel

        #self.pbnRemoveModel.setEnabled(False)
        #self.pbnStartModel.setEnabled(False)

        # Initializing values
        self.progressBarTotal.setValue(0)
        self.progressBarYear.setValue(0)
        self.progressBarModel.setValue(0)
        self.progressBarTotal.setRange(0, 0)
        self.progressBarYear.setRange(0, 0)
        self.progressBarModel.setRange(0, 0)

        batch_name = str(self.cboOptionalIndicatorBatch.currentText())
        if batch_name == '(None)':
            batch_name = None

        self.runThread = RunModelThread(get_mainwindow_instance(), self,
                                        batch_name, run_name)

        # Use this signal from the thread if it is capable of producing its own status signal
        QObject.connect(self.runThread, SIGNAL("runFinished(PyQt_PyObject)"),
                        self.runFinishedFromThread)
        QObject.connect(self.runThread, SIGNAL("runError(PyQt_PyObject)"),
                        self.runErrorFromThread)
        # Use this timer to call a function in the thread to check status if the thread is unable
        # to produce its own signal above
        self.timer = QTimer()
        QObject.connect(self.timer, SIGNAL("timeout()"),
                        self.runStatusFromThread)
        self.timer.start(1000)
        self.running = True
        self.paused = False
    def _restart_selected_run(self):
        assert self.has_selected_item()
        run_node = self.selected_item().node
        # ask for start_year, end_year
        # start_year default to the last year of years run
        # need to avoid insert auto generate run directory again
#        original_start_year = int(run_node.find('start_year').text)
#        original_end_year = int(run_node.find('end_year').text)
#        restart_year = original_end_year + 1
#        end_year = restart_year + 1

        run_name = run_node.get('name')
        scenario_name = run_node.find('scenario_name').text
        run_id = run_node.get('run_id')
        try:
            run_id = int(run_id)
        except ValueError:
            raise ValueError, "run_id for run %s is invalid: %s; the run cannot be restarted." % \
                  (run_name, run_id)
        
        run_manager = get_run_manager()
        config = run_manager.get_resources_for_run_id_from_history(run_id)

        xml_config = self.project.xml_config
        opusgui = get_mainwindow_instance()
        scenario_manager = get_manager_instance('scenario_manager')
        opusmodel = OpusModel(scenario_manager, xml_config, scenario_name)
        tab_widget = SimulationGuiElementRestart(mainwindow=opusgui, 
                                                 runManager=scenario_manager, 
                                                 model=opusmodel, 
                                                 xml_config=xml_config,
                                                 run_id=run_id,
                                                 run_name=run_name)
        tab_widget.config = config
#        tab_widget.start_year = start_year
#        tab_widget.end_year = end_year
        scenario_manager._attach_tab(tab_widget)
        opusgui.update()
    def _restart_selected_run(self):
        assert self.has_selected_item()
        run_node = self.selected_item().node
        # ask for start_year, end_year
        # start_year default to the last year of years run
        # need to avoid insert auto generate run directory again
        #        original_start_year = int(run_node.find('start_year').text)
        #        original_end_year = int(run_node.find('end_year').text)
        #        restart_year = original_end_year + 1
        #        end_year = restart_year + 1

        run_name = run_node.get('name')
        scenario_name = run_node.find('scenario_name').text
        run_id = run_node.get('run_id')
        try:
            run_id = int(run_id)
        except ValueError:
            raise ValueError, "run_id for run %s is invalid: %s; the run cannot be restarted." % \
                  (run_name, run_id)

        run_manager = get_run_manager()
        config = run_manager.get_resources_for_run_id_from_history(run_id)

        xml_config = self.project.xml_config
        opusgui = get_mainwindow_instance()
        scenario_manager = get_manager_instance('scenario_manager')
        opusmodel = OpusModel(scenario_manager, xml_config, scenario_name)
        tab_widget = SimulationGuiElementRestart(mainwindow=opusgui,
                                                 runManager=scenario_manager,
                                                 model=opusmodel,
                                                 xml_config=xml_config,
                                                 run_id=run_id,
                                                 run_name=run_name)
        tab_widget.config = config
        #        tab_widget.start_year = start_year
        #        tab_widget.end_year = end_year
        scenario_manager._attach_tab(tab_widget)
        opusgui.update()
    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)
                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" '
                        "but there is no module with that name, or module returned import error." % import_path
                    ),
                )
                return

        ExecuteToolSetGui(get_mainwindow_instance(), tool_config_nodes, tool_config_to_tool_name).show()

    def exportXMLToFile(self):
        """ NO DOCUMENTATION """
        assert self.has_selected_item()

        # Ask the users where they want to save the file
        start_dir = ""
        opus_home = os.environ.get("OPUS_HOME")
        if opus_home:
            start_dir_test = os.path.join(opus_home, "project_configs")
            if start_dir_test:
                start_dir = start_dir_test
        configDialog = QFileDialog()
        filter_str = QString("*.xml")
        fd = configDialog.getSaveFileName(
Exemple #21
0
    def on_pbnStartModel_released(self):
        duplicate = False
        self.diagnostic_go_button.setEnabled(True)

        if self.running and not self.paused:
            # Take care of pausing a run
            success = self.runThread.pause()
            if success:
                self.paused = True
                self.timer.stop()
                self.pbnStartModel.setText(QString("Resume simulation run..."))
        elif self.running and self.paused:
            # Need to resume a paused run
            success = self.runThread.resume()
            if success:
                self.paused = False
                self.timer.start(1000)
                self.pbnStartModel.setText(QString("Pause simulation run..."))
        elif not self.running:
            run_name = str(self.leRunName.text())
            if run_name == '':
                run_name = None
            else:
                run_id = None
                run_nodes = get_available_run_nodes(self.project)
                for run_node in run_nodes:
                    existing_run_name = run_node.tag
                    if run_name == existing_run_name:
                        duplicate = True
                        r = run_node.get('run_id')
                        if r is not None:
                            run_id = int(r)
                        break
                if duplicate:
                    dlg_dup = OverwriteRunDialog(self)

                    if dlg_dup.exec_() == QDialog.Rejected:
                        return
                    delete_simulation_run(
                        self.project,
                        run_node.tag)  # todo change to run_node.get('name')
            # Update the XML
            self.project.update_xml_config()
            self.updateConfigAndGuiForRun()

            # Fire up a new thread and run the model
            self.pbnStartModel.setText(QString("Pause simulation run..."))
            # References to the GUI elements for status for this run...
            self.progressBarTotal = self.runProgressBarTotal
            self.progressBarYear = self.runProgressBarYear
            self.progressBarModel = self.runProgressBarModel

            #self.pbnRemoveModel.setEnabled(False)
            #self.pbnStartModel.setEnabled(False)

            # Initializing values
            self.progressBarTotal.setValue(0)
            self.progressBarYear.setValue(0)
            self.progressBarModel.setValue(0)
            self.progressBarTotal.setRange(0, 0)
            self.progressBarYear.setRange(0, 0)
            self.progressBarModel.setRange(0, 0)

            batch_name = str(self.cboOptionalIndicatorBatch.currentText())
            if batch_name == '(None)':
                batch_name = None

            self.runThread = RunModelThread(get_mainwindow_instance(), self,
                                            batch_name, run_name)

            if duplicate and run_id is not None:
                from opus_core.services.run_server.run_manager import RunManager as ServicesRunManager
                run_manager = ServicesRunManager(
                    ServicesDatabaseConfiguration())
                run_manager.delete_everything_for_this_run(run_id=run_id)
                run_manager.close()

            # Use this signal from the thread if it is capable of producing its own status signal
            QObject.connect(self.runThread,
                            SIGNAL("runFinished(PyQt_PyObject)"),
                            self.runFinishedFromThread)
            QObject.connect(self.runThread, SIGNAL("runError(PyQt_PyObject)"),
                            self.runErrorFromThread)
            # Use this timer to call a function in the thread to check status if the thread is unable
            # to produce its own signal above
            self.timer = QTimer()
            QObject.connect(self.timer, SIGNAL("timeout()"),
                            self.runStatusFromThread)
            self.timer.start(1000)
            self.running = True
            self.paused = False
            self.runThread.start()
        else:
            print "Unexpected state in the model run..."
Exemple #22
0
 def __init__(self,opusTool):
     QThread.__init__(self, get_mainwindow_instance())
     self.opusTool = opusTool
    def on_pbnStartModel_released(self):
        duplicate = False
        self.diagnostic_go_button.setEnabled(True)

        if self.running and not self.paused:
            # Take care of pausing a run
            success = self.runThread.pause()
            if success:
                self.paused = True
                self.timer.stop()
                self.pbnStartModel.setText(QString("Resume simulation run..."))
        elif self.running and self.paused:
            # Need to resume a paused run
            success = self.runThread.resume()
            if success:
                self.paused = False
                self.timer.start(1000)
                self.pbnStartModel.setText(QString("Pause simulation run..."))
        elif not self.running:
            run_name = str(self.leRunName.text())
            if run_name == '':
                run_name = None
            else:
                run_id = None
                run_nodes = get_available_run_nodes(self.project)
                for run_node in run_nodes:
                    existing_run_name = run_node.tag
                    if run_name == existing_run_name:
                        duplicate = True
                        r = run_node.get('run_id')
                        if r is not None:
                            run_id = int(r)
                        break
                if duplicate:
                    dlg_dup = OverwriteRunDialog(self)

                    if dlg_dup.exec_() == QDialog.Rejected:
                        return
                    delete_simulation_run(self.project, run_node.tag) # todo change to run_node.get('name')
            # Update the XML
            self.project.update_xml_config()
            self.updateConfigAndGuiForRun()

            # Fire up a new thread and run the model
            self.pbnStartModel.setText(QString("Pause simulation run..."))
            # References to the GUI elements for status for this run...
            self.progressBarTotal = self.runProgressBarTotal
            self.progressBarYear = self.runProgressBarYear
            self.progressBarModel = self.runProgressBarModel

            #self.pbnRemoveModel.setEnabled(False)
            #self.pbnStartModel.setEnabled(False)

            # Initializing values
            self.progressBarTotal.setValue(0)
            self.progressBarYear.setValue(0)
            self.progressBarModel.setValue(0)
            self.progressBarTotal.setRange(0,0)
            self.progressBarYear.setRange(0,0)
            self.progressBarModel.setRange(0,0)

            batch_name = str(self.cboOptionalIndicatorBatch.currentText())
            if batch_name == '(None)':
                batch_name = None

            self.runThread = RunModelThread(get_mainwindow_instance(),
                                            self,
                                            batch_name,
                                            run_name)

            if duplicate and run_id is not None:
                from opus_core.services.run_server.run_manager import RunManager as ServicesRunManager
                run_manager = ServicesRunManager(ServicesDatabaseConfiguration())
                run_manager.delete_everything_for_this_run(run_id = run_id)
                run_manager.close()


            # Use this signal from the thread if it is capable of producing its own status signal
            QObject.connect(self.runThread, SIGNAL("runFinished(PyQt_PyObject)"),
                            self.runFinishedFromThread)
            QObject.connect(self.runThread, SIGNAL("runError(PyQt_PyObject)"),
                            self.runErrorFromThread)
            # Use this timer to call a function in the thread to check status if the thread is unable
            # to produce its own signal above
            self.timer = QTimer()
            QObject.connect(self.timer, SIGNAL("timeout()"),
                            self.runStatusFromThread)
            self.timer.start(1000)
            self.running = True
            self.paused = False
            self.runThread.start()
        else:
            print "Unexpected state in the model run..."
                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

        ExecuteToolSetGui(get_mainwindow_instance(),
            tool_config_nodes,
            tool_config_to_tool_name).show()


    def add_custom_menu_items_for_node(self, node, menu):
        index = self.model.index_for_node(node)
        cnt = self.model.rowCount(index.parent())

        istop = index.row() == 0
        isbottom = index.row() == cnt-1
        isonly = cnt == 1

        # Tool files are the "actual" tools
        if node.tag == "tool":
            menu.addAction(self.actExecToolFile)
Exemple #25
0
 def addNewSimulationElement(self, model):
     ''' Initialize and add a simulation runner tab '''
     tab_widget = SimulationGuiElement(get_mainwindow_instance(), self,
                                       model, self.project.xml_config)
     self._attach_tab(tab_widget)
Exemple #26
0
    def _run_finished(self, success):
        key = self.running_key
        self.running_key = None

        size = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.tabwidget_visualizations.setSizePolicy(size)

        name = '%s/%s/%s' % key
        new_tab = QTabWidget(self.tabwidget_visualizations)
        self.tabwidget_visualizations.addTab(new_tab, name)

        map_widget = None
        tab_widget = None
        for (visualization_type,
             visualizations) in self.batch_processor.get_visualizations():
            if len(visualizations) > 0:
                if visualization_type == 'mapnik_map':
                    viz = visualizations[0]
                    map_widget = ViewImageForm(viz, new_tab)
                    map_widget.setSizePolicy(size)
                elif visualization_type == 'mapnik_animated_map':
                    viz = visualizations[0]
                    map_widget = ViewAnimationForm(viz, new_tab)
                    map_widget.setSizePolicy(size)
                elif visualization_type == 'table_per_year':
                    viz = visualizations[0]
                    tab_widget = ViewTableForm(viz, new_tab)
                    tab_widget.setSizePolicy(size)
#            else:
#                map_widget = self.tabMap
#                tab_widget = self.tabTable

#        if not map_widget or not tab_widget: return

#        self.tabMap = map_widget
#        self.tabTable = tab_widget

        if tab_widget:
            new_tab.addTab(tab_widget, "Table")

        if map_widget:
            new_tab.addTab(map_widget, "Map")

        self.already_browsed[key] = (tab_widget, map_widget)

        #        self.lblViewIndicator.setText(QString(key[1]))
        #        self.lblViewRun.setText(QString(key[0]))
        #        self.lblViewYear.setText(QString(repr(key[2])))

        swap = self.queued_results is not None and self.queued_results[
            0] == 'swap'

        if self.queued_results is not None and not swap:
            self.running_key = self.queued_results[0]

            logger.log_note(
                'Generating queued results for %s on run %s for year %i' %
                self.running_key)
            self.batch_processor = self.queued_results[1]
            self.queued_results = None

            runThread = 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
            QObject.connect(runThread, SIGNAL("runFinished(PyQt_PyObject)"),
                            self._run_finished)
            QObject.connect(runThread, SIGNAL("runError(PyQt_PyObject)"),
                            self._run_error)
            runThread.start()
        else:
            #            if swap:
            #                (map_widget, tab_widget) = self.queued_results[1]
            #
            ##                self.swap_visualizations(map_widget, tab_widget)
            #                name = '%s/%s/%s'%key
            #        #        self.swap_visualizations(map_widget, tab_widget)
            #                self.add_visualization(map_widget = map_widget, tab_widget = tab_widget, name = name)

            self.queued_results = None

            self.generating_results = False
            self.pb_generate_results.setText('Results Generated')
Exemple #27
0
    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)
                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" '
                     'but there is no module with that name, or module returned import error.'
                     % import_path))
                return

        ExecuteToolSetGui(get_mainwindow_instance(), tool_config_nodes,
                          tool_config_to_tool_name).show()

    def exportXMLToFile(self):
        ''' NO DOCUMENTATION '''
        assert self.has_selected_item()

        # Ask the users where they want to save the file
        start_dir = ''
        opus_home = os.environ.get('OPUS_HOME')
        if opus_home:
            start_dir_test = os.path.join(opus_home, 'project_configs')
            if start_dir_test:
                start_dir = start_dir_test
        configDialog = QFileDialog()
        filter_str = QString("*.xml")
    def _apply_variable_changes(self):
        ''' apply the changes the user made to the expression library '''
        # TODO: also check into the possibility that an inherited variable can have its name
        # changed (in this case don't overwrite the original variable's name. Instead create a new
        # variable with the new name.)

        dirty_variables = [
            var for var in self.model.all_variables if var['dirty']
        ]

        # partition dirty variables into create, delete and update sets
        create_set = [
            var for var in dirty_variables if var['originalnode'] is None
        ]
        delete_set = [
            var for var in dirty_variables
            if var['delete'] and var['originalnode'] is not None
        ]
        # the rest of the variables should just be updated
        update_set = [
            var for var in dirty_variables
            if not var['originalnode'] in delete_set and var not in create_set
        ]

        # verify if we have a partition
        assert (set([str(var) for var in create_set])
                | set([str(var) for var in delete_set])
                | set([str(var) for var in update_set]) == set(
                    [str(var) for var in dirty_variables]))
        assert (set([str(var) for var in create_set])
                & set([str(var) for var in delete_set])
                & set([str(var) for var in update_set]) == set())

        # Apply the changes to each set of nodes
        expression_lib = self.project.find('general/expression_library')
        for variable in create_set:
            # print 'CREATE SET ', variable
            node = node_from_variable(variable)
            # print '  node', node
            self.project.insert_node(node, expression_lib)

        for variable in update_set:
            node = node_from_variable(variable)
            original_node = variable[
                'originalnode']  # reference to the actual XML node
            # print 'UPDATE SET %s (original %s)' %(variable['name'], original_node)
            self.project.make_local(original_node)
            for key in node.attrib:
                if not key == 'inherited':
                    original_node.set(key, node.get(key))
            if 'dataset' in original_node.attrib:
                del original_node.attrib['dataset']
            original_node.text = node.text

        for variable in delete_set:
            # print 'DELETE SET %s' % (node)
            self.project.delete_node(variable['originalnode'])
        self.initialize()
        something_changed = bool(update_set or create_set or delete_set)
        update_mainwindow_savestate(something_changed)
        get_mainwindow_instance().emit(SIGNAL('variables_updated'))
        return True
    def _run_finished(self, success):
        key = self.running_key
        self.running_key = None

        size = QSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
        self.tabwidget_visualizations.setSizePolicy(size)

        name = '%s/%s/%s'%key
        new_tab = QTabWidget(self.tabwidget_visualizations)
        self.tabwidget_visualizations.addTab(new_tab, name)

        map_widget = None
        tab_widget = None
        for (visualization_type, visualizations) in self.batch_processor.get_visualizations():
            if len(visualizations) > 0:
                if visualization_type == 'mapnik_map':
                    viz = visualizations[0]
                    map_widget = ViewImageForm(viz, new_tab)
                    map_widget.setSizePolicy(size)
                elif visualization_type == 'mapnik_animated_map':
                    viz = visualizations[0]
                    map_widget = ViewAnimationForm(viz, new_tab)
                    map_widget.setSizePolicy(size)
                elif visualization_type == 'table_per_year':
                    viz = visualizations[0]
                    tab_widget = ViewTableForm(viz, new_tab)
                    tab_widget.setSizePolicy(size)
#            else:
#                map_widget = self.tabMap
#                tab_widget = self.tabTable

#        if not map_widget or not tab_widget: return

#        self.tabMap = map_widget
#        self.tabTable = tab_widget

        if tab_widget:
            new_tab.addTab(tab_widget, "Table")

        if map_widget:
            new_tab.addTab(map_widget, "Map")

        self.already_browsed[key] = (tab_widget, map_widget)

#        self.lblViewIndicator.setText(QString(key[1]))
#        self.lblViewRun.setText(QString(key[0]))
#        self.lblViewYear.setText(QString(repr(key[2])))

        swap = self.queued_results is not None and self.queued_results[0] == 'swap'

        if self.queued_results is not None and not swap:
            self.running_key = self.queued_results[0]

            logger.log_note('Generating queued results for %s on run %s for year %i'%self.running_key)
            self.batch_processor = self.queued_results[1]
            self.queued_results = None

            runThread = 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
            QObject.connect(runThread, SIGNAL("runFinished(PyQt_PyObject)"), self._run_finished)
            QObject.connect(runThread, SIGNAL("runError(PyQt_PyObject)"), self._run_error)
            runThread.start()
        else:
#            if swap:
#                (map_widget, tab_widget) = self.queued_results[1]
#
##                self.swap_visualizations(map_widget, tab_widget)
#                name = '%s/%s/%s'%key
#        #        self.swap_visualizations(map_widget, tab_widget)
#                self.add_visualization(map_widget = map_widget, tab_widget = tab_widget, name = name)

            self.queued_results = None

            self.generating_results = False
            self.pb_generate_results.setText('Results Generated')
Exemple #31
0
 def __init__(self,opusTool):
     QThread.__init__(self, get_mainwindow_instance())
     self.opusTool = opusTool
 def addNewSimulationElement(self, model):
     ''' Initialize and add a simulation runner tab '''
     tab_widget = SimulationGuiElement(get_mainwindow_instance(),
                                       self, model, self.project.xml_config)
     self._attach_tab(tab_widget)